// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1248, %rsp           # imm = 0x1248
movq %rdx, %rbx
movq %rsi, (%rsp)
movq %rdi, 0x18(%rsp)
testq %rdx, %rdx
jne 0x2be9e
movq 0x18(%rsp), %rax
movq (%rax), %rbx
leaq 0xb51de(%rip), %rsi     # 0xe1083
movq (%rsp), %r14
movq %r14, %rdi
callq 0x21ab0
testq %rax, %rax
je 0x2c2ed
movq %rax, %r12
movq %rbx, 0x38(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x8758c
leaq 0xe40(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdx
callq 0x21f30
testq %rax, %rax
je 0x2c2db
leaq 0x10d257(%rip), %rax    # 0x139148
movl $0x88, %ecx
addq 0x1110db(%rip), %rcx    # 0x13cfd8
movq %rcx, 0x20(%rsp)
addq $0x10, %rax
movq %rax, 0x30(%rsp)
leaq 0xe3f(%rsp), %rbp
leaq 0x640(%rsp), %rbx
leaq 0x11975e(%rip), %rax    # 0x145680
addq $0x8, %rax
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
movq %rbp, %r15
movsbl 0x1(%r15), %r14d
incq %r15
movl %r14d, %edi
callq 0x210b0
testl %eax, %eax
jne 0x2bf31
movzbl %r14b, %eax
incl %r13d
testl %eax, %eax
je 0x2c2bd
cmpl $0x23, %eax
je 0x2c2bd
movq %r15, %rdi
leaq 0xce044(%rip), %rsi     # 0xf9fab
leaq 0xc0(%rsp), %rdx
xorl %eax, %eax
callq 0x21890
cmpl $0x1, %eax
jne 0x2c305
movq 0xc0(%rsp), %rax
movabsq $0x6c65646f4d77656e, %rcx # imm = 0x6C65646F4D77656E
xorq %rcx, %rax
movzbl 0xc8(%rsp), %ecx
orq %rax, %rcx
je 0x2c12a
movq 0xc0(%rsp), %rax
movabsq $0x6e49746e65726170, %rcx # imm = 0x6E49746E65726170
xorq %rcx, %rax
movl 0xc8(%rsp), %ecx
xorq $0x786564, %rcx         # imm = 0x786564
orq %rax, %rcx
je 0x2c1d7
movq 0xc0(%rsp), %rax
movabsq $0x726f66736e617274, %rcx # imm = 0x726F66736E617274
xorq %rcx, %rax
movzwl 0xc8(%rsp), %ecx
xorq $0x6d, %rcx
orq %rax, %rcx
jne 0x2c2bd
subq $0x8, %rsp
movq %r15, %rdi
leaq 0xaee9f(%rip), %rsi     # 0xdaea8
leaq 0xc8(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x50(%rsp), %r8
leaq 0x58(%rsp), %r9
xorl %eax, %eax
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
callq 0x21890
addq $0x70, %rsp
cmpl $0x11, %eax
jne 0x2c34a
movq 0x30(%rsp), %rax
movq %rax, 0x640(%rsp)
leaq 0x648(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x71cc6
leaq 0x6c8(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x71cc6
leaq 0x119592(%rip), %rax    # 0x145680
movq 0x108(%rax), %rax
movq %rax, 0x748(%rsp)
movslq 0x14(%rsp), %rax
movq 0x8(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r15
movq %r15, %r14
movq 0x10(%r14), %r14
testq %r14, %r14
je 0x2c253
leaq 0x58(%r14), %rsi
movq %rbx, %rdi
callq 0x6fc90
jmp 0x2c10f
movq %r15, %rdi
leaq 0xaeceb(%rip), %rsi     # 0xdae1f
leaq 0xc0(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x240(%rsp), %r8
xorl %eax, %eax
callq 0x21890
cmpl $0x3, %eax
jne 0x2c31c
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218c0
movq %rax, %r15
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x3b560
movq %r15, %rdi
leaq 0x240(%rsp), %rsi
callq 0x3cc1a
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x3c97e
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x87622
movq %rbx, %rdi
leaq 0xaec7b(%rip), %rsi     # 0xdae26
leaq 0x240(%rsp), %rdx
xorl %eax, %eax
callq 0x21140
movq 0x18(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x30c77
testl %eax, %eax
jne 0x2c2bd
jmp 0x2c38a
movq %r15, %rdi
leaq 0xaec62(%rip), %rsi     # 0xdae43
leaq 0xc0(%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x21890
cmpl $0x2, %eax
jne 0x2c333
movslq 0x640(%rsp), %rsi
movl 0x14(%rsp), %eax
testq %rsi, %rsi
js 0x2c2b4
cmpl %eax, %esi
jge 0x2c361
movq 0x8(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r15
movq (%rcx,%rsi,8), %r14
movq 0x10(%r15), %rdi
cmpq %rdi, %r14
je 0x2c2bd
testq %rdi, %rdi
je 0x2c241
movq %r15, %rsi
callq 0x3ba02
testq %r14, %r14
je 0x2c2bd
movq %r14, %rdi
movq %r15, %rsi
callq 0x3c97e
jmp 0x2c2bd
leaq 0x1c0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x71dba
leaq 0x1c0(%rsp), %rdi
callq 0x72b64
leaq 0x240(%rsp), %r14
movq %r14, %rdi
leaq 0x1c0(%rsp), %rsi
xorl %edx, %edx
callq 0x6f9e2
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6fb9e
movq %r14, %rdi
callq 0x6fa30
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3cb96
movq %rbx, %rdi
callq 0x6fa30
jmp 0x2c2bd
cmpl $0x1, %eax
jne 0x2c378
leaq 0xe40(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r12, %rdx
callq 0x21f30
testq %rax, %rax
jne 0x2bf2e
movq %r12, %rdi
callq 0x218b0
movl $0x1, %ebx
jmp 0x2c38c
leaq 0xae362(%rip), %rdi     # 0xda656
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x8a0fe
jmp 0x2c396
leaq 0xaeae7(%rip), %rdi     # 0xdadf3
movl %r13d, %esi
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x8a0fe
jmp 0x2c38a
leaq 0xaead0(%rip), %rdi     # 0xdadf3
movl %r13d, %esi
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x8a0fe
jmp 0x2c38a
leaq 0xaeab9(%rip), %rdi     # 0xdadf3
movl %r13d, %esi
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x8a0fe
jmp 0x2c38a
leaq 0xaeaa2(%rip), %rdi     # 0xdadf3
movl %r13d, %esi
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x8a0fe
jmp 0x2c38a
leaq 0xaeb03(%rip), %rdi     # 0xdae6b
movl %r13d, %edx
movq (%rsp), %rcx
xorl %eax, %eax
callq 0x8a0fe
jmp 0x2c38a
leaq 0xaeac9(%rip), %rdi     # 0xdae48
movq (%rsp), %rsi
xorl %eax, %eax
callq 0x8a0fe
xorl %ebx, %ebx
leaq 0x8(%rsp), %rdi
callq 0x875f6
movl %ebx, %eax
addq $0x1248, %rsp           # imm = 0x1248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c3f0
jmp 0x2c3f0
jmp 0x2c3f0
jmp 0x2c3f0
jmp 0x2c3f0
movq %rax, %rbx
leaq 0x240(%rsp), %rdi
callq 0x6fa30
jmp 0x2c3f8
jmp 0x2c3f5
movq %rax, %rbx
leaq 0x640(%rsp), %rdi
callq 0x700c4
jmp 0x2c405
jmp 0x2c3f0
movq %rax, %rbx
movl $0x1b0, %esi            # imm = 0x1B0
movq %r15, %rdi
callq 0x218e0
jmp 0x2c405
jmp 0x2c3f5
movq %rax, %rbx
jmp 0x2c405
movq %rax, %rbx
leaq 0x640(%rsp), %rdi
callq 0x6fa30
leaq 0x8(%rsp), %rdi
callq 0x875f6
movq %rbx, %rdi
callq 0x220c0
nop