// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1258, %rsp           # imm = 0x1258
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
testq %rdx, %rdx
jne 0x2bfc4
movq (%r15), %r14
leaq 0xb7078(%rip), %rsi     # 0xe3043
movq %rbx, %rdi
callq 0x21ac0
testq %rax, %rax
je 0x2c4f8
movq %rax, %r12
movq %r15, 0x48(%rsp)
movq %r14, 0x40(%rsp)
movq %rbx, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x886bc
leaq 0xe50(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdx
callq 0x21f40
testq %rax, %rax
je 0x2c4e9
leaq 0x10f12b(%rip), %rax    # 0x13b148
addq $0x10, %rax
movq %rax, 0x38(%rsp)
movl $0x88, %eax
addq 0x112fa6(%rip), %rax    # 0x13efd8
movq %rax, 0x28(%rsp)
leaq 0xe50(%rsp), %rbp
leaq 0x11b63a(%rip), %rax    # 0x147680
addq $0x8, %rax
movq %rax, 0x30(%rsp)
xorl %r13d, %r13d
leaq 0xe4f(%rsp), %r14
movsbl 0x1(%r14), %r15d
incq %r14
movl %r15d, %edi
callq 0x210b0
testl %eax, %eax
jne 0x2c05a
movzbl %r15b, %eax
incl %r13d
movl $0x2, %r15d
testl %eax, %eax
je 0x2c4ca
cmpl $0x23, %eax
je 0x2c4ca
movq %r14, %rdi
leaq 0xcfec5(%rip), %rsi     # 0xfbf5b
leaq 0xd0(%rsp), %rdx
xorl %eax, %eax
callq 0x21890
cmpl $0x1, %eax
jne 0x2c262
movq 0xd0(%rsp), %rax
movabsq $0x6c65646f4d77656e, %rcx # imm = 0x6C65646F4D77656E
xorq %rcx, %rax
movzbl 0xd8(%rsp), %ecx
orq %rax, %rcx
je 0x2c283
movq 0xd0(%rsp), %rax
movabsq $0x6e49746e65726170, %rcx # imm = 0x6E49746E65726170
xorq %rcx, %rax
movl 0xd8(%rsp), %ecx
xorq $0x786564, %rcx         # imm = 0x786564
orq %rax, %rcx
je 0x2c33f
movq 0xd0(%rsp), %rax
movabsq $0x726f66736e617274, %rcx # imm = 0x726F66736E617274
xorq %rcx, %rax
movzwl 0xd8(%rsp), %ecx
xorq $0x6d, %rcx
orq %rax, %rcx
jne 0x2c494
subq $0x8, %rsp
movq %r14, %rdi
leaq 0xb0d60(%rip), %rsi     # 0xdce98
leaq 0xd8(%rsp), %rdx
leaq 0x58(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x68(%rsp), %r9
xorl %eax, %eax
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
callq 0x21890
addq $0x70, %rsp
movl %eax, %ebx
cmpl $0x11, %eax
jne 0x2c3fa
movq 0x38(%rsp), %rax
movq %rax, 0x250(%rsp)
leaq 0x258(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x72bb6
leaq 0x2d8(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x72bb6
leaq 0x11b461(%rip), %rax    # 0x147680
movq 0x108(%rax), %rax
movq %rax, 0x358(%rsp)
movslq 0x1c(%rsp), %rax
movq 0x10(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r15
movq %r15, 0x20(%rsp)
movq 0x10(%r15), %r15
testq %r15, %r15
je 0x2c412
leaq 0x58(%r15), %rsi
leaq 0x250(%rsp), %rdi
callq 0x70ad0
jmp 0x2c242
movl $0x1, %r15d
leaq 0xb0b74(%rip), %rdi     # 0xdcde3
movl %r13d, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x8b53a
jmp 0x2c4ca
movq %r14, %rdi
leaq 0xb0b82(%rip), %rsi     # 0xdce0f
leaq 0xd0(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0xa50(%rsp), %r8
xorl %eax, %eax
callq 0x21890
cmpl $0x3, %eax
jne 0x2c3c4
movq %rbp, %rbx
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218c0
movq 0x48(%rsp), %r15
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
callq 0x3bab8
movq %r14, %rdi
leaq 0xa50(%rsp), %rsi
callq 0x3d0ca
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x3ce8e
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x88752
leaq 0x250(%rsp), %rbp
movq %rbp, %rdi
leaq 0xb0b04(%rip), %rsi     # 0xdce16
leaq 0xa50(%rsp), %rdx
xorl %eax, %eax
callq 0x21140
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x30f33
testl %eax, %eax
movq %rbx, %rbp
jne 0x2c494
jmp 0x2c4c4
movq %r14, %rdi
leaq 0xb0aea(%rip), %rsi     # 0xdce33
leaq 0xd0(%rsp), %rdx
leaq 0x250(%rsp), %rcx
xorl %eax, %eax
callq 0x21890
cmpl $0x2, %eax
jne 0x2c3df
movslq 0x250(%rsp), %rsi
testq %rsi, %rsi
js 0x2c48d
movslq 0x1c(%rsp), %rax
cmpl %eax, %esi
jge 0x2c499
movq 0x10(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %rbx
movq (%rcx,%rsi,8), %r14
movq 0x10(%rbx), %rdi
cmpq %rdi, %r14
je 0x2c494
testq %rdi, %rdi
je 0x2c3ab
movq %rbx, %rsi
callq 0x3bf3a
testq %r14, %r14
je 0x2c494
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3ce8e
jmp 0x2c494
leaq 0xb0a18(%rip), %rdi     # 0xdcde3
movl %r13d, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x8b53a
jmp 0x2c4c4
leaq 0xb09fd(%rip), %rdi     # 0xdcde3
movl %r13d, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x8b53a
jmp 0x2c4c4
leaq 0xb09e2(%rip), %rdi     # 0xdcde3
movl %r13d, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x8b53a
jmp 0x2c480
leaq 0x1d0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x72caa
leaq 0x1d0(%rsp), %rdi
callq 0x73a54
leaq 0xa50(%rsp), %r15
movq %r15, %rdi
leaq 0x1d0(%rsp), %rsi
xorl %edx, %edx
callq 0x7082a
leaq 0x250(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x709e6
movq %r15, %rdi
callq 0x70878
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x3d06c
leaq 0x250(%rsp), %rdi
callq 0x70878
movl $0x1, %r15d
cmpl $0x11, %ebx
je 0x2c494
jmp 0x2c4ca
cmpl $0x1, 0x1c(%rsp)
jne 0x2c4b1
xorl %r15d, %r15d
jmp 0x2c4ca
leaq 0xb09bb(%rip), %rdi     # 0xdce5b
movl %r13d, %edx
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x8b53a
jmp 0x2c4c4
leaq 0xb0980(%rip), %rdi     # 0xdce38
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x8b53a
movl $0x1, %r15d
testb $0x1, %r15b
jne 0x2c50d
movq %rbp, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdx
callq 0x21f40
testq %rax, %rax
jne 0x2c052
movq %r12, %rdi
callq 0x218b0
movl $0x1, %ebp
jmp 0x2c50f
leaq 0xb0147(%rip), %rdi     # 0xdc646
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8b53a
jmp 0x2c519
xorl %ebp, %ebp
leaq 0x10(%rsp), %rdi
callq 0x88726
movl %ebp, %eax
addq $0x1258, %rsp           # imm = 0x1258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c573
movq %rax, %rbx
leaq 0xa50(%rsp), %rdi
callq 0x70878
jmp 0x2c57b
jmp 0x2c578
jmp 0x2c573
movq %rax, %rbx
leaq 0x250(%rsp), %rdi
callq 0x70f00
jmp 0x2c588
jmp 0x2c573
jmp 0x2c573
movq %rax, %rbx
movl $0x1b0, %esi            # imm = 0x1B0
movq %r14, %rdi
callq 0x218e0
jmp 0x2c588
jmp 0x2c573
jmp 0x2c578
jmp 0x2c573
movq %rax, %rbx
jmp 0x2c588
movq %rax, %rbx
leaq 0x250(%rsp), %rdi
callq 0x70878
leaq 0x10(%rsp), %rdi
callq 0x88726
movq %rbx, %rdi
callq 0x220d0