// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::ReadParseFile(char const*, gaps::R3SceneNode*)
int R3Scene::
ReadParseFile(const char *filename, R3SceneNode *parent_node)
{
  // Get/set parent node
  if (!parent_node) parent_node = root;

  // Open file
  FILE *fp;
  if (!(fp = fopen(filename, "r"))) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Read header
  char buffer[4096];
  if (!fgets(buffer, 4096, fp)) {
    RNFail("Unable to read object parse file %s\n", filename);
    return 0;
  }

  // Check header
  if (strncmp(buffer, "OBJECT PARSE 1.0", 16)) {
    RNFail("Error in header of oject parse file %s\n", filename);
    return 0;
  }

  // Read file
  int line_number = 0;
  int assignment_index = 0;
  RNArray<R3Shape *> shapes;
  char mesh_directory[1024] = { '\0' };
  while (fgets(buffer, 2048, fp)) {
    // Check line
    line_number++;
    char *bufferp = buffer;
    while (*bufferp && isspace(*bufferp)) bufferp++;
    if (!bufferp) continue;
    if (*bufferp == '#') continue;

    // Parse line
    char keyword[1024];
    if (sscanf(bufferp, "%s", keyword) == (unsigned int) 1) {
      if (!strcmp(keyword, "A")) {
        // Parse assignment
        double score, m[16];
        int segmentation_index, model_index, dummy;
        if (sscanf(bufferp, "%s%d%d%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%d%d%d%d", keyword, 
          &segmentation_index, &model_index, 
          &m[0], &m[1], &m[2], &m[3], &m[4], &m[5], &m[6], &m[7], 
          &m[8], &m[9], &m[10], &m[11], &m[12], &m[13], &m[14], &m[15], 
          &score, &dummy, &dummy, &dummy, &dummy) != (unsigned int) 24) {
          RNFail("Error parsing assignment at line %d of %s\n", line_number, filename);
          return 0;
        }

        // Create node
        R3SceneNode *node = new R3SceneNode(this);
        parent_node->InsertChild(node);

        // Create shape element
        R3Shape *shape = shapes.Kth(model_index);
        R3Brdf *brdf = new R3Brdf(RNRgb(0.1 + score, 0.25 + score, 0.5 + score), 0.0, 0.5 + score);
        InsertBrdf(brdf);
        R3Material *material = new R3Material(brdf);
        InsertMaterial(material);
        R3SceneElement *element = new R3SceneElement();
        element->SetMaterial(material);
        element->InsertShape(shape);
        node->InsertElement(element);

        // Set node name
        char node_name[1024];
        sprintf(node_name, "A%d_M%d_S%03d", assignment_index++, model_index, (int) (1000*score));
        node->SetName(node_name);

        // Set node transformation
        R4Matrix matrix(m); 
        R3Affine affine(matrix, 0);
        node->SetTransformation(affine);
      }
      else if (!strcmp(keyword, "M")) {
        // Parse model
        int dummy;
        double cx, cy, cz, r, h;
        char model_name[1024], mesh_name[1024];
        if (sscanf(bufferp, "%s%d%lf%lf%lf%lf%lf%s%s%d%d%d%d%d", keyword, 
          &dummy, &cx, &cy, &cz, &r, &h, model_name, mesh_name, &dummy, &dummy, &dummy, &dummy, &dummy) != (unsigned int) 14) {
          RNFail("Error parsing model at line %d of %s\n", line_number, filename);
          return 0;
        }

        // Read mesh
        char mesh_filename[2048];
        R3Shape *shape = NULL;
        if (strcmp(mesh_name, "None")) {
          if (mesh_directory[0]) sprintf(mesh_filename, "%s/%s", mesh_directory, mesh_name);
          else sprintf(mesh_filename, "%s", mesh_name);
          shape = ReadMesh(mesh_filename);
          if (!shape) return 0;
        }
        else {
          shape = new R3Sphere(R3Point(0,0,0), 0);
        }

        // Insert shape into list
        shapes.Insert(shape);
      }
      else if (!strcmp(keyword, "MD")) {
        // Parse model directory
        if (sscanf(bufferp, "%s%s", keyword, mesh_directory) != (unsigned int) 2) {
          RNFail("Error parsing model directory at line %d of %s\n", line_number, filename);
          return 0;
        }
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3068, %rsp           # imm = 0x3068
movq %rdi, 0x3058(%rsp)
movq %rsi, 0x3050(%rsp)
movq %rdx, 0x3048(%rsp)
movq 0x3058(%rsp), %rax
movq %rax, 0x138(%rsp)
cmpq $0x0, 0x3048(%rsp)
jne 0x32797
movq 0x138(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x3048(%rsp)
movq 0x3050(%rsp), %rdi
leaq 0x162afd(%rip), %rsi    # 0x1952a3
callq 0x22c10
movq %rax, 0x3040(%rsp)
cmpq $0x0, %rax
jne 0x327df
movq 0x3050(%rsp), %rsi
leaq 0x15bf06(%rip), %rdi    # 0x18e6ce
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x3064(%rsp)
jmp 0x332c4
leaq 0x2040(%rsp), %rdi
movq 0x3040(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x230f0
cmpq $0x0, %rax
jne 0x32825
movq 0x3050(%rsp), %rsi
leaq 0x15c7e6(%rip), %rdi    # 0x18eff4
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x3064(%rsp)
jmp 0x332c4
leaq 0x2040(%rsp), %rdi
leaq 0x15c7e5(%rip), %rsi    # 0x18f019
movl $0x10, %edx
callq 0x22450
cmpl $0x0, %eax
je 0x32869
movq 0x3050(%rsp), %rsi
leaq 0x15c7d8(%rip), %rdi    # 0x18f02a
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x3064(%rsp)
jmp 0x332c4
movl $0x0, 0x203c(%rsp)
movl $0x0, 0x2038(%rsp)
leaq 0x2028(%rsp), %rdi
callq 0x49490
leaq 0x1c20(%rsp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x224f0
movq 0x3040(%rsp), %rdx
leaq 0x2040(%rsp), %rdi
movl $0x800, %esi            # imm = 0x800
callq 0x230f0
movq %rax, 0x130(%rsp)
jmp 0x328c4
movq 0x130(%rsp), %rax
cmpq $0x0, %rax
je 0x33281
movl 0x203c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x203c(%rsp)
leaq 0x2040(%rsp), %rax
movq %rax, 0x1c08(%rsp)
movq 0x1c08(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0x12f(%rsp)
je 0x3292a
movq 0x1c08(%rsp), %rax
movsbl (%rax), %edi
callq 0x220c0
cmpl $0x0, %eax
setne %al
movb %al, 0x12f(%rsp)
movb 0x12f(%rsp), %al
testb $0x1, %al
jne 0x32937
jmp 0x32966
movq 0x1c08(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1c08(%rsp)
jmp 0x328f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c18(%rsp)
movl %eax, 0x1c14(%rsp)
jmp 0x332b5
cmpq $0x0, 0x1c08(%rsp)
jne 0x32976
jmp 0x328a0
movq 0x1c08(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x3298b
jmp 0x328a0
movq 0x1c08(%rsp), %rdi
leaq 0x1800(%rsp), %rdx
leaq 0x17b8a5(%rip), %rsi    # 0x1ae247
movb $0x0, %al
callq 0x229a0
cmpl $0x1, %eax
jne 0x3327c
leaq 0x1800(%rsp), %rdi
leaq 0x1695db(%rip), %rsi    # 0x19bf9c
callq 0x22ec0
cmpl $0x0, %eax
jne 0x32fa4
movq 0x1c08(%rsp), %rdi
leaq 0x1800(%rsp), %rdx
leaq 0x1770(%rsp), %r9
leaq 0x1770(%rsp), %rax
addq $0x8, %rax
movq %rax, 0xe0(%rsp)
leaq 0x1770(%rsp), %rax
addq $0x10, %rax
movq %rax, 0xe8(%rsp)
leaq 0x1770(%rsp), %rax
addq $0x18, %rax
movq %rax, 0xf0(%rsp)
leaq 0x1770(%rsp), %rax
addq $0x20, %rax
movq %rax, 0xf8(%rsp)
leaq 0x1770(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x100(%rsp)
leaq 0x1770(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x108(%rsp)
leaq 0x1770(%rsp), %rax
addq $0x38, %rax
movq %rax, 0x110(%rsp)
leaq 0x1770(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x118(%rsp)
leaq 0x1770(%rsp), %rbp
addq $0x48, %rbp
leaq 0x1770(%rsp), %r13
addq $0x50, %r13
leaq 0x1770(%rsp), %r12
addq $0x58, %r12
leaq 0x1770(%rsp), %r15
addq $0x60, %r15
leaq 0x1770(%rsp), %r14
addq $0x68, %r14
leaq 0x1770(%rsp), %rbx
addq $0x70, %rbx
leaq 0x1770(%rsp), %r11
addq $0x78, %r11
leaq 0x15c570(%rip), %rsi    # 0x18f052
leaq 0x176c(%rsp), %rcx
leaq 0x1768(%rsp), %r8
leaq 0x17f8(%rsp), %r10
leaq 0x1764(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, (%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x120(%rsp), %rax
movq %rbp, 0x40(%rsp)
movq %r13, 0x48(%rsp)
movq %r12, 0x50(%rsp)
movq %r15, 0x58(%rsp)
movq %r14, 0x60(%rsp)
movq %rbx, 0x68(%rsp)
movq %r11, 0x70(%rsp)
movq %r10, 0x78(%rsp)
movq %rax, 0x80(%rsp)
movq %rax, 0x88(%rsp)
movq %rax, 0x90(%rsp)
movq %rax, 0x98(%rsp)
movb $0x0, %al
callq 0x229a0
cmpl $0x18, %eax
je 0x32c07
movl 0x203c(%rsp), %esi
movq 0x3050(%rsp), %rdx
leaq 0x15c4b1(%rip), %rdi    # 0x18f094
xorl %eax, %eax
callq 0xf3b00
jmp 0x32bec
movl $0x0, 0x3064(%rsp)
movl $0x1, 0x1760(%rsp)
jmp 0x332a6
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x229e0
movq %rax, 0xd8(%rsp)
jmp 0x32c1b
movq 0x138(%rsp), %rsi
movq 0xd8(%rsp), %rdi
callq 0x555e0
jmp 0x32c32
movq 0xd8(%rsp), %rax
movq %rax, 0x1758(%rsp)
movq 0x3048(%rsp), %rdi
movq 0x1758(%rsp), %rsi
callq 0x57450
jmp 0x32c59
movl 0x1768(%rsp), %esi
leaq 0x2028(%rsp), %rdi
callq 0x494a0
movq %rax, 0xd0(%rsp)
jmp 0x32c77
movq 0xd0(%rsp), %rax
movq %rax, 0x1750(%rsp)
movl $0xb8, %edi
callq 0x229e0
movq %rax, 0xc8(%rsp)
jmp 0x32c9b
movsd 0x17f8(%rsp), %xmm2
movsd 0x15b39c(%rip), %xmm1   # 0x18e048
movaps %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x15b8ed(%rip), %xmm3   # 0x18e5a8
movaps %xmm2, %xmm1
addsd %xmm3, %xmm1
movsd 0x15b356(%rip), %xmm3   # 0x18e020
addsd %xmm3, %xmm2
leaq 0x1730(%rsp), %rdi
callq 0xef340
jmp 0x32cdd
movq 0xc8(%rsp), %rdi
movsd 0x17f8(%rsp), %xmm1
movsd 0x15b32a(%rip), %xmm0   # 0x18e020
addsd %xmm0, %xmm1
xorl %eax, %eax
movl %eax, %edx
leaq 0x1730(%rsp), %rsi
xorps %xmm0, %xmm0
movsd 0x15b2ff(%rip), %xmm2   # 0x18e010
callq 0x646f0
jmp 0x32d18
movq 0x138(%rsp), %rdi
movq 0xc8(%rsp), %rax
movq %rax, 0x1748(%rsp)
movq 0x1748(%rsp), %rsi
callq 0x2ccd0
jmp 0x32d3f
movl $0x30, %edi
callq 0x229e0
movq %rax, 0xc0(%rsp)
jmp 0x32d53
movq 0xc0(%rsp), %rdi
movq 0x1748(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x63e20
jmp 0x32d6e
movq 0x138(%rsp), %rdi
movq 0xc0(%rsp), %rax
movq %rax, 0x1728(%rsp)
movq 0x1728(%rsp), %rsi
callq 0x2cbd0
jmp 0x32d95
movl $0x60, %edi
callq 0x229e0
movq %rax, 0xb8(%rsp)
jmp 0x32da9
movq 0xb8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x58f20
jmp 0x32dbc
movq 0xb8(%rsp), %rax
movq %rax, 0x1720(%rsp)
movq 0x1720(%rsp), %rdi
movq 0x1728(%rsp), %rsi
callq 0x59350
jmp 0x32de3
movq 0x1720(%rsp), %rdi
movq 0x1750(%rsp), %rsi
callq 0x59370
jmp 0x32dfa
movq 0x1758(%rsp), %rdi
movq 0x1720(%rsp), %rsi
callq 0x57510
jmp 0x32e11
movl 0x2038(%rsp), %edx
movl %edx, %eax
incl %eax
movl %eax, 0x2038(%rsp)
movl 0x1768(%rsp), %ecx
movsd 0x17f8(%rsp), %xmm0
movsd 0x15b77d(%rip), %xmm1   # 0x18e5b8
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r8d
leaq 0x15c274(%rip), %rsi    # 0x18f0bf
xorl %eax, %eax
leaq 0x1320(%rsp), %rdi
movq %rdi, 0xb0(%rsp)
callq 0x22160
movq 0xb0(%rsp), %rsi
movq 0x1758(%rsp), %rdi
callq 0x57850
jmp 0x32e79
leaq 0x12a0(%rsp), %rdi
leaq 0x1770(%rsp), %rsi
callq 0xc6350
jmp 0x32e90
leaq 0x1190(%rsp), %rdi
leaq 0x12a0(%rsp), %rsi
xorl %edx, %edx
callq 0xc1a60
jmp 0x32ea9
movq 0x1758(%rsp), %rdi
leaq 0x1190(%rsp), %rsi
callq 0x577d0
jmp 0x32ec0
leaq 0x1190(%rsp), %rdi
callq 0xc1b00
jmp 0x3327a
movq 0xd8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c18(%rsp)
movl %eax, 0x1c14(%rsp)
movl $0x1b0, %esi            # imm = 0x1B0
callq 0x22a00
jmp 0x332b5
movq 0xc8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c18(%rsp)
movl %eax, 0x1c14(%rsp)
movl $0xb8, %esi
callq 0x22a00
jmp 0x332b5
movq 0xc0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c18(%rsp)
movl %eax, 0x1c14(%rsp)
movl $0x30, %esi
callq 0x22a00
jmp 0x332b5
movq 0xb8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c18(%rsp)
movl %eax, 0x1c14(%rsp)
movl $0x60, %esi
callq 0x22a00
jmp 0x332b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c18(%rsp)
movl %eax, 0x1c14(%rsp)
leaq 0x1190(%rsp), %rdi
callq 0xc1b00
jmp 0x332b5
leaq 0x1800(%rsp), %rdi
leaq 0x1624f3(%rip), %rsi    # 0x1954a6
callq 0x22ec0
cmpl $0x0, %eax
jne 0x331f9
movq 0x1c08(%rsp), %rdi
leaq 0x1800(%rsp), %rdx
leaq 0xd60(%rsp), %r11
leaq 0x960(%rsp), %r10
leaq 0x15c0e5(%rip), %rsi    # 0x18f0cd
leaq 0x118c(%rsp), %rax
leaq 0x1180(%rsp), %r8
leaq 0x1178(%rsp), %r9
leaq 0x1170(%rsp), %r15
leaq 0x1168(%rsp), %r14
leaq 0x1160(%rsp), %rbx
movq %rax, %rcx
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movb $0x0, %al
callq 0x229a0
cmpl $0xe, %eax
je 0x33092
movl 0x203c(%rsp), %esi
movq 0x3050(%rsp), %rdx
leaq 0x15c081(%rip), %rdi    # 0x18f0ef
xorl %eax, %eax
callq 0xf3b00
jmp 0x33077
movl $0x0, 0x3064(%rsp)
movl $0x1, 0x1760(%rsp)
jmp 0x332a6
movq $0x0, 0x158(%rsp)
leaq 0x960(%rsp), %rdi
leaq 0x15c068(%rip), %rsi    # 0x18f115
callq 0x22ec0
cmpl $0x0, %eax
je 0x3315d
cmpb $0x0, 0x1c20(%rsp)
je 0x330ed
leaq 0x160(%rsp), %rdi
leaq 0x1c20(%rsp), %rdx
leaq 0x960(%rsp), %rcx
leaq 0x15b5e4(%rip), %rsi    # 0x18e6c8
movb $0x0, %al
callq 0x22160
jmp 0x3310b
leaq 0x160(%rsp), %rdi
leaq 0x960(%rsp), %rdx
leaq 0x17b143(%rip), %rsi    # 0x1ae247
movb $0x0, %al
callq 0x22160
leaq 0x160(%rsp), %rdi
callq 0x3cd50
movq %rax, 0xa8(%rsp)
jmp 0x33122
movq 0xa8(%rsp), %rax
movq %rax, 0x158(%rsp)
cmpq $0x0, 0x158(%rsp)
jne 0x33158
movl $0x0, 0x3064(%rsp)
movl $0x1, 0x1760(%rsp)
jmp 0x332a6
jmp 0x331e0
movl $0x28, %edi
callq 0x229e0
movq %rax, 0xa0(%rsp)
jmp 0x33171
leaq 0x140(%rsp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xcc870
jmp 0x33189
movq 0xa0(%rsp), %rdi
leaq 0x140(%rsp), %rsi
xorps %xmm0, %xmm0
callq 0xb2bd0
jmp 0x331a3
movq 0xa0(%rsp), %rax
movq %rax, 0x158(%rsp)
jmp 0x331e0
movq 0xa0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c18(%rsp)
movl %eax, 0x1c14(%rsp)
movl $0x28, %esi
callq 0x22a00
jmp 0x332b5
movq 0x158(%rsp), %rsi
leaq 0x2028(%rsp), %rdi
callq 0x494c0
jmp 0x331f7
jmp 0x33278
leaq 0x1800(%rsp), %rdi
leaq 0x15bf12(%rip), %rsi    # 0x18f11a
callq 0x22ec0
cmpl $0x0, %eax
jne 0x33276
movq 0x1c08(%rsp), %rdi
leaq 0x1800(%rsp), %rdx
leaq 0x1c20(%rsp), %rcx
leaq 0x15d0a0(%rip), %rsi    # 0x1902d1
movb $0x0, %al
callq 0x229a0
cmpl $0x2, %eax
je 0x33274
movl 0x203c(%rsp), %esi
movq 0x3050(%rsp), %rdx
leaq 0x15beca(%rip), %rdi    # 0x18f11d
xorl %eax, %eax
callq 0xf3b00
jmp 0x3325c
movl $0x0, 0x3064(%rsp)
movl $0x1, 0x1760(%rsp)
jmp 0x332a6
jmp 0x33276
jmp 0x33278
jmp 0x3327a
jmp 0x3327c
jmp 0x328a0
movq 0x3040(%rsp), %rdi
callq 0x229d0
jmp 0x33290
movl $0x1, 0x3064(%rsp)
movl $0x1, 0x1760(%rsp)
leaq 0x2028(%rsp), %rdi
callq 0x494f0
jmp 0x332c4
leaq 0x2028(%rsp), %rdi
callq 0x494f0
jmp 0x332dd
movl 0x3064(%rsp), %eax
addq $0x3068, %rsp           # imm = 0x3068
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1c18(%rsp), %rdi
callq 0x23290
nopw (%rax,%rax)