// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28f8, %rsp           # imm = 0x28F8
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
testq %rdx, %rdx
jne 0x2b7f6
movq (%r15), %rbx
leaq 0xb5886(%rip), %rsi     # 0xe1083
movq %r14, %rdi
callq 0x21ab0
testq %rax, %rax
je 0x2b84c
movq %rax, %rbp
leaq 0x18f0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdx
callq 0x21f30
testq %rax, %rax
je 0x2b855
movdqa 0x18f0(%rsp), %xmm0
pcmpeqb 0xaed18(%rip), %xmm0    # 0xda550
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x2b87c
leaq 0xaf733(%rip), %rdi     # 0xdaf7d
jmp 0x2b85c
leaq 0xaee03(%rip), %rdi     # 0xda656
jmp 0x2b85c
leaq 0xaf6eb(%rip), %rdi     # 0xdaf47
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x8a0fe
movl %ebx, %eax
addq $0x28f8, %rsp           # imm = 0x28F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x8(%rsp)
movq %rbx, 0x48(%rsp)
movq %r14, 0x18(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x8758c
leaq 0x8f0(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x21490
leaq 0x18f0(%rsp), %rbx
movq %rbx, %rdi
movl $0x800, %esi            # imm = 0x800
movq %rbp, %rdx
callq 0x21f30
testq %rax, %rax
je 0x2bd91
movq 0x8(%rsp), %rax
leaq 0x38(%rax), %rcx
movq %rcx, 0x40(%rsp)
addq $0x28, %rax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
movl $0x0, 0x4(%rsp)
incl %eax
movl %eax, (%rsp)
movb 0x18f0(%rsp), %r14b
movq %rbx, %r12
testb %r14b, %r14b
je 0x2b928
movq %r12, %rbx
movsbl %r14b, %edi
callq 0x210b0
testl %eax, %eax
je 0x2b91e
movb 0x1(%rbx), %r14b
incq %rbx
testb %r14b, %r14b
jne 0x2b903
jmp 0x2b928
cmpb $0x23, %r14b
je 0x2bd72
movq %rbx, %rdi
leaq 0xce679(%rip), %rsi     # 0xf9fab
leaq 0x4f0(%rsp), %rdx
xorl %eax, %eax
callq 0x21890
cmpl $0x1, %eax
jne 0x2bd72
movzwl 0x4f0(%rsp), %eax
cmpl $0x4d, %eax
je 0x2bbfa
cmpl $0x41, %eax
jne 0x2bcb4
movq %rbx, %rdi
leaq 0xaf637(%rip), %rsi     # 0xdafa5
leaq 0x4f0(%rsp), %rdx
leaq 0x58(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x70(%rsp), %r9
xorl %eax, %eax
leaq 0x50(%rsp), %r10
pushq %r10
pushq %r10
pushq %r10
pushq %r10
leaq 0x30(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
leaq 0x110(%rsp), %r10
pushq %r10
callq 0x21890
addq $0xa0, %rsp
cmpl $0x18, %eax
jne 0x2bdc5
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218c0
movq %rax, %rbx
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x3b560
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c97e
movslq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movq (%rcx,%rax,8), %r14
movl $0xb8, %edi
callq 0x218c0
movq %rax, %r15
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm0
addsd 0xae59e(%rip), %xmm0    # 0xda038
movapd %xmm2, %xmm1
addsd 0xaea32(%rip), %xmm1    # 0xda4d8
movsd 0xae572(%rip), %xmm3    # 0xda020
addsd %xmm3, %xmm2
leaq 0x10f0(%rsp), %r13
movq %r13, %rdi
callq 0x87400
movsd 0x10(%rsp), %xmm1
addsd 0xae550(%rip), %xmm1    # 0xda020
xorpd %xmm0, %xmm0
movq %r15, %rdi
movq %r13, %rsi
movsd 0xae536(%rip), %xmm2    # 0xda018
xorl %edx, %edx
callq 0x43ea4
movq 0x8(%rsp), %rax
movq %rax, 0x8(%r15)
movl 0x44(%rax), %edx
movl %edx, 0x10(%r15)
movq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x87622
movl $0x30, %edi
callq 0x218c0
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x43ace
movq 0x8(%rsp), %rax
movq %rax, (%r13)
movl 0x34(%rax), %edx
movl %edx, 0x8(%r13)
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x87622
movl $0x60, %edi
callq 0x218c0
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
callq 0x3d514
movq %r15, %rdi
movq %r13, %rsi
callq 0x3d7b6
movq %r15, %rdi
movq %r14, %rsi
callq 0x3d7bc
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3c9ce
movl 0x20(%rsp), %ecx
movsd 0x10(%rsp), %xmm0
mulsd 0xae959(%rip), %xmm0    # 0xda4e0
cvttsd2si %xmm0, %r8d
leaq 0x10f0(%rsp), %r14
movq %r14, %rdi
leaq 0xaf474(%rip), %rsi     # 0xdb012
movl 0x4(%rsp), %edx
xorl %eax, %eax
callq 0x21140
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3cc1a
leaq 0x470(%rsp), %r15
movq %r15, %rdi
leaq 0x70(%rsp), %rsi
callq 0x71dba
leaq 0xcf0(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x6f9e2
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3cb96
incl 0x4(%rsp)
movq %r14, %rdi
callq 0x6fa30
jmp 0x2bd72
movq %rbx, %rdi
leaq 0xaf41c(%rip), %rsi     # 0xdb020
leaq 0x4f0(%rsp), %rdx
leaq 0x64(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x58(%rsp), %r9
xorl %eax, %eax
pushq %rcx
pushq %rcx
pushq %rcx
pushq %rcx
pushq %rcx
leaq 0x98(%rsp), %r10
pushq %r10
leaq 0xd20(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0x90(%rsp), %r10
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
callq 0x21890
addq $0x50, %rsp
cmpl $0xe, %eax
jne 0x2bdad
movl 0x70(%rsp), %eax
movl $0x656e6f4e, %ecx       # imm = 0x656E6F4E
xorl %ecx, %eax
movzbl 0x74(%rsp), %ecx
orl %eax, %ecx
je 0x2bcfe
cmpb $0x0, 0x8f0(%rsp)
je 0x2bd38
leaq 0x10f0(%rsp), %rbx
movq %rbx, %rdi
leaq 0xae9b5(%rip), %rsi     # 0xda650
leaq 0x8f0(%rsp), %rdx
leaq 0x70(%rsp), %rcx
xorl %eax, %eax
callq 0x21140
jmp 0x2bd4d
movl 0x4f0(%rsp), %eax
movl $0x444d, %ecx           # imm = 0x444D
xorl %ecx, %eax
movzbl 0x4f2(%rsp), %ecx
orw %ax, %cx
jne 0x2bd72
movq %rbx, %rdi
leaq 0xb0661(%rip), %rsi     # 0xdc33e
leaq 0x4f0(%rsp), %rdx
leaq 0x8f0(%rsp), %rcx
xorl %eax, %eax
callq 0x21890
cmpl $0x2, %eax
je 0x2bd72
jmp 0x2bddf
movl $0x28, %edi
callq 0x218c0
movq %rax, %rbx
pxor %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
leaq 0x470(%rsp), %r14
movq %r14, %rdi
callq 0x753ca
pxor %xmm0, %xmm0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6a766
jmp 0x2bd61
leaq 0x10f0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x70(%rsp), %rsi
callq 0x21800
movq %rbx, %rdi
callq 0x339e1
movq %rax, %rbx
testq %rax, %rax
je 0x2bdf5
movl 0x34(%rsp), %edx
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x87622
movq %r12, %rbx
movq %r12, %rdi
movl $0x800, %esi            # imm = 0x800
movq %rbp, %rdx
callq 0x21f30
testq %rax, %rax
movl (%rsp), %eax
jne 0x2b8eb
movq %rbp, %rdi
callq 0x218b0
movl $0x1, %ebx
leaq 0x28(%rsp), %rdi
callq 0x875f6
jmp 0x2b868
leaq 0xaf28e(%rip), %rdi     # 0xdb042
movl (%rsp), %esi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x8a0fe
jmp 0x2bdf5
leaq 0xaf21b(%rip), %rdi     # 0xdafe7
xorl %ebx, %ebx
movl (%rsp), %esi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x8a0fe
jmp 0x2bd9e
leaq 0xaf28a(%rip), %rdi     # 0xdb070
movl (%rsp), %esi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x8a0fe
xorl %ebx, %ebx
jmp 0x2bd9e
jmp 0x2be5f
jmp 0x2be5f
jmp 0x2be5f
movq %rax, %r14
movl $0x28, %esi
jmp 0x2be2c
jmp 0x2be5f
jmp 0x2be5f
movq %rax, %r14
movl $0x60, %esi
jmp 0x2be51
movq %rax, %r14
movl $0x30, %esi
movq %r13, %rdi
jmp 0x2be54
movq %rax, %r14
movl $0x1b0, %esi            # imm = 0x1B0
movq %rbx, %rdi
jmp 0x2be54
movq %rax, %r14
leaq 0xcf0(%rsp), %rdi
callq 0x6fa30
jmp 0x2be62
jmp 0x2be5f
jmp 0x2be5f
jmp 0x2be5f
movq %rax, %r14
movl $0xb8, %esi
movq %r15, %rdi
callq 0x218e0
jmp 0x2be62
jmp 0x2be5f
jmp 0x2be5f
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x875f6
movq %r14, %rdi
callq 0x220c0