// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::ReadSupportHierarchyFile(char const*, gaps::R3SceneNode*)
int R3Scene::
ReadSupportHierarchyFile(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 scene
  char buffer[1024];
  int line_count = 0;
  RNArray<R3SceneNode *> nodes;
  while (fgets(buffer, 1023, fp)) {
    // Increment line counter
    line_count++;

    // Skip white space
    char *bufferp = buffer;
    while (isspace(*bufferp)) bufferp++;

    // Skip blank lines and comments
    if (*bufferp == '#') continue;
    if (*bufferp == '\0') continue;

    // Get keyword
    char keyword[256];
    if (sscanf(bufferp, "%s", keyword) != 1) {
      RNFail("Syntax error on line %d in file %s", line_count, filename);
      return 0;
    }

    // Check keyword
    if (!strcmp(keyword, "newModel")) {
      // Read fields
      int model_index;
      char model_name[1024];
      if (sscanf(bufferp, "%s%d%s", keyword, &model_index, model_name) != (unsigned int) 3) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Create node
      assert(nodes.NEntries() == model_index);
      R3SceneNode *node = new R3SceneNode(this);
      node->SetName(model_name);
      parent_node->InsertChild(node);
      nodes.Insert(node);

      // Read obj file
      char model_filename[2048];
      sprintf(model_filename, "../models/%s.obj", model_name);
      if (!ReadObj(this, node, model_filename)) return 0;
    }
    else if (!strcmp(keyword, "parentIndex")) {
      // Read fields
      int parent_index;
      if (sscanf(bufferp, "%s%d", keyword, &parent_index) != (unsigned int) 2) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Check parent index
      if (parent_index < 0) {
        if (nodes.NEntries() != 1) {
          RNFail("Root node was not first in file %s", filename);
          return 0;
        }
      }
      else {
        // Just checking
        if (parent_index >= nodes.NEntries()) {
          RNFail("Invalid parent node index %d on line %d in file %s", parent_index, line_count, filename);
          return 0;
        }

        // Set last node's parent
        R3SceneNode *node = nodes.Tail();
        R3SceneNode *parent = nodes.Kth(parent_index);
        R3SceneNode *previous_parent = node->Parent();
        if (parent != previous_parent) {
          if (previous_parent) previous_parent->RemoveChild(node);
          if (parent) parent->InsertChild(node);
        }
      }
    }
    else if (!strcmp(keyword, "transform")) {
      // Read fields
      double m[16];
      if (sscanf(bufferp, "%s%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", 
        keyword, &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]) != (unsigned int) 17) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // NOTE: Our transformation is stored relative to parent's coordinate system
      // So, we must compute transformation from inverse of transform from parent node to world coordinates so that can 
      // convert file's absolute transform (which goes from node's coordinates to world coordinates)
      // to our transform (which goes from node's coordinates to parent node's coordinates)
      R3Affine transformation = R3identity_affine;
      R3SceneNode *node = nodes.Tail();
      R3SceneNode *ancestor = node->Parent();
      while (ancestor) {
        transformation.InverseTransform(ancestor->Transformation());
        ancestor = ancestor->Parent();
      }

      // Set last node's transformation
      // Note that file's matrix is for post-multiplication, while ours is for pre-multiplication, so need flip
      R4Matrix matrix(m); matrix.Flip();
      transformation.Transform(R3Affine(matrix, 0));
      node->SetTransformation(transformation);
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1238, %rsp           # imm = 0x1238
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movq %rdx, 0x28(%rsp)
testq %rdx, %rdx
jne 0x2ab72
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x28(%rsp)
leaq 0xa122a(%rip), %rsi     # 0xcbda3
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x21ab0
testq %rax, %rax
je 0x2aefb
movq %rax, %r12
leaq 0x18(%rsp), %rdi
callq 0x7c1c6
leaq 0x530(%rsp), %rbx
leaq 0xa30(%rsp), %r14
xorl %ebp, %ebp
leaq 0x630(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdx
callq 0x21f50
testq %rax, %rax
je 0x2af13
leaq 0x62f(%rsp), %r15
movsbl 0x1(%r15), %r13d
incq %r15
movl %r13d, %edi
callq 0x210b0
testl %eax, %eax
jne 0x2abd4
movzbl %r13b, %eax
incl %ebp
testl %eax, %eax
je 0x2abae
cmpl $0x23, %eax
je 0x2abae
movq %r15, %rdi
leaq 0xba05a(%rip), %rsi     # 0xe4c5b
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21890
cmpl $0x1, %eax
jne 0x2af20
movq %rbx, %rdi
leaq 0x9b1c8(%rip), %rsi     # 0xc5de6
pushq $0x9
popq %rdx
callq 0x21920
testl %eax, %eax
je 0x2ad54
movq %rbx, %rdi
leaq 0x9b1cf(%rip), %rsi     # 0xc5e07
pushq $0xc
popq %rdx
callq 0x21920
testl %eax, %eax
je 0x2adf8
movq %rbx, %rdi
leaq 0x9b21c(%rip), %rsi     # 0xc5e6e
pushq $0xa
popq %rdx
callq 0x21920
testl %eax, %eax
jne 0x2abae
subq $0x8, %rsp
movq %r15, %rdi
leaq 0x9b208(%rip), %rsi     # 0xc5e78
movq %rbx, %rdx
leaq 0x38(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x48(%rsp), %r9
xorl %eax, %eax
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
callq 0x21890
addq $0x70, %rsp
cmpl $0x11, %eax
jne 0x2af65
movq %r14, %rdi
leaq 0x10b97e(%rip), %rsi    # 0x1366a0
callq 0x34464
movslq 0x24(%rsp), %rax
movq 0x18(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r15
movq %r15, %r13
movq 0x10(%r13), %r13
testq %r13, %r13
je 0x2ae76
leaq 0x58(%r13), %rsi
movq %r14, %rdi
callq 0x66ab4
jmp 0x2ad39
movq %r15, %rdi
leaq 0x9b091(%rip), %rsi     # 0xc5def
movq %rbx, %rdx
leaq 0x30(%rsp), %rcx
leaq 0x130(%rsp), %r8
xorl %eax, %eax
callq 0x21890
cmpl $0x3, %eax
jne 0x2af37
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218d0
movq %rax, %r15
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x37a1e
movq %r15, %rdi
leaq 0x130(%rsp), %rsi
callq 0x38b2e
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x3898c
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x2737a
movq %r14, %rdi
leaq 0x9b02a(%rip), %rsi     # 0xc5df6
leaq 0x130(%rsp), %rdx
xorl %eax, %eax
callq 0x21140
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2ede6
testl %eax, %eax
jne 0x2abae
jmp 0x2af91
movq %r15, %rdi
leaq 0x9b011(%rip), %rsi     # 0xc5e13
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x21890
cmpl $0x2, %eax
jne 0x2af4e
movslq 0xa30(%rsp), %rsi
movl 0x24(%rsp), %eax
testq %rsi, %rsi
js 0x2aeda
cmpl %eax, %esi
jge 0x2af7c
movq 0x18(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r15
movq (%rcx,%rsi,8), %r13
movq 0x10(%r15), %rdi
cmpq %rdi, %r13
je 0x2abae
testq %rdi, %rdi
je 0x2ae5d
movq %r15, %rsi
callq 0x37de8
testq %r13, %r13
je 0x2abae
movq %r13, %rdi
movq %r15, %rsi
callq 0x3898c
jmp 0x2abae
leaq 0xb0(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x6886c
leaq 0xb0(%rsp), %rdi
callq 0x69502
leaq 0x130(%rsp), %r13
movq %r13, %rdi
leaq 0xb0(%rsp), %rsi
xorl %edx, %edx
callq 0x66808
movq %r14, %rdi
movq %r13, %rsi
callq 0x669c2
movq %r13, %rdi
callq 0x66856
movq %r15, %rdi
movq %r14, %rsi
callq 0x38b02
movq %r14, %rdi
callq 0x66856
jmp 0x2abae
cmpl $0x1, %eax
je 0x2abae
leaq 0x9af2e(%rip), %rdi     # 0xc5e18
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x7e83e
jmp 0x2af91
leaq 0x9a724(%rip), %rdi     # 0xc5626
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x7e83e
jmp 0x2af9d
movq %r12, %rdi
callq 0x218c0
pushq $0x1
popq %rbx
jmp 0x2af93
leaq 0x9ae9c(%rip), %rdi     # 0xc5dc3
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x7e83e
jmp 0x2af91
leaq 0x9ae85(%rip), %rdi     # 0xc5dc3
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x7e83e
jmp 0x2af91
leaq 0x9ae6e(%rip), %rdi     # 0xc5dc3
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x7e83e
jmp 0x2af91
leaq 0x9ae57(%rip), %rdi     # 0xc5dc3
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x7e83e
jmp 0x2af91
leaq 0x9aeb8(%rip), %rdi     # 0xc5e3b
movl %ebp, %edx
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x7e83e
xorl %ebx, %ebx
leaq 0x18(%rsp), %rdi
callq 0x7c234
movl %ebx, %eax
addq $0x1238, %rsp           # imm = 0x1238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2afe7
jmp 0x2afe7
jmp 0x2afe7
jmp 0x2afe7
jmp 0x2afe7
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x66856
jmp 0x2afef
jmp 0x2afec
jmp 0x2afe7
jmp 0x2afe7
movq %rax, %rbx
movl $0x1b0, %esi            # imm = 0x1B0
movq %r15, %rdi
callq 0x218f0
jmp 0x2affc
jmp 0x2afec
movq %rax, %rbx
jmp 0x2affc
movq %rax, %rbx
leaq 0xa30(%rsp), %rdi
callq 0x66856
leaq 0x18(%rsp), %rdi
callq 0x7c234
movq %rbx, %rdi
callq 0x220e0