// File: 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 $0x15c8, %rsp           # imm = 0x15C8
movq %rdi, 0x15b8(%rsp)
movq %rsi, 0x15b0(%rsp)
movq %rdx, 0x15a8(%rsp)
movq 0x15b8(%rsp), %rax
movq %rax, 0xf8(%rsp)
cmpq $0x0, 0x15a8(%rsp)
jne 0x33347
movq 0xf8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x15a8(%rsp)
movq 0x15b0(%rsp), %rdi
leaq 0x161f4d(%rip), %rsi    # 0x1952a3
callq 0x22c10
movq %rax, 0x15a0(%rsp)
cmpq $0x0, %rax
jne 0x3338f
movq 0x15b0(%rsp), %rsi
leaq 0x15b356(%rip), %rdi    # 0x18e6ce
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x15c4(%rsp)
jmp 0x33c50
movl $0x0, 0x119c(%rsp)
leaq 0x1188(%rsp), %rdi
callq 0x48140
movq 0x15a0(%rsp), %rdx
leaq 0x11a0(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
callq 0x230f0
movq %rax, 0xf0(%rsp)
jmp 0x333cb
movq 0xf0(%rsp), %rax
cmpq $0x0, %rax
je 0x33c0d
movl 0x119c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x119c(%rsp)
leaq 0x11a0(%rsp), %rax
movq %rax, 0x1170(%rsp)
movq 0x1170(%rsp), %rax
movsbl (%rax), %edi
callq 0x220c0
cmpl $0x0, %eax
je 0x33442
movq 0x1170(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1170(%rsp)
jmp 0x333fe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1180(%rsp)
movl %eax, 0x117c(%rsp)
jmp 0x33c41
movq 0x1170(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x33457
jmp 0x333a7
movq 0x1170(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x3346c
jmp 0x333a7
movq 0x1170(%rsp), %rdi
leaq 0x1070(%rsp), %rdx
leaq 0x17adc4(%rip), %rsi    # 0x1ae247
movb $0x0, %al
callq 0x229a0
cmpl $0x1, %eax
je 0x334c9
movl 0x119c(%rsp), %esi
movq 0x15b0(%rsp), %rdx
leaq 0x15b9fb(%rip), %rdi    # 0x18eea0
xorl %eax, %eax
callq 0xf3b00
jmp 0x334ae
movl $0x0, 0x15c4(%rsp)
movl $0x1, 0x106c(%rsp)
jmp 0x33c32
leaq 0x1070(%rsp), %rdi
leaq 0x15b9eb(%rip), %rsi    # 0x18eec3
callq 0x22ec0
cmpl $0x0, %eax
jne 0x33686
movq 0x1170(%rsp), %rdi
leaq 0x1070(%rsp), %rdx
leaq 0xc60(%rsp), %r8
leaq 0x15b9c7(%rip), %rsi    # 0x18eecc
leaq 0x1068(%rsp), %rcx
movb $0x0, %al
callq 0x229a0
cmpl $0x3, %eax
je 0x33553
movl 0x119c(%rsp), %esi
movq 0x15b0(%rsp), %rdx
leaq 0x15b971(%rip), %rdi    # 0x18eea0
xorl %eax, %eax
callq 0xf3b00
jmp 0x33538
movl $0x0, 0x15c4(%rsp)
movl $0x1, 0x106c(%rsp)
jmp 0x33c32
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x229e0
movq %rax, 0xe8(%rsp)
jmp 0x33567
movq 0xf8(%rsp), %rsi
movq 0xe8(%rsp), %rdi
callq 0x555e0
jmp 0x3357e
movq 0xe8(%rsp), %rax
movq %rax, 0xc58(%rsp)
movq 0xc58(%rsp), %rdi
leaq 0xc60(%rsp), %rsi
callq 0x57850
jmp 0x335a5
movq 0x15a8(%rsp), %rdi
movq 0xc58(%rsp), %rsi
callq 0x57450
jmp 0x335bc
movq 0xc58(%rsp), %rsi
leaq 0x1188(%rsp), %rdi
callq 0x48340
jmp 0x335d3
leaq 0x15b8f9(%rip), %rsi    # 0x18eed3
xorl %eax, %eax
movl %eax, 0xe0(%rsp)
leaq 0x450(%rsp), %rdi
movq %rdi, 0xd8(%rsp)
leaq 0xc60(%rsp), %rdx
callq 0x22160
movq 0xf8(%rsp), %rdi
movq 0xd8(%rsp), %rdx
movl 0xe0(%rsp), %eax
movq 0xc58(%rsp), %rsi
movl %eax, %ecx
callq 0x3a9e0
movl %eax, 0xe4(%rsp)
jmp 0x3362f
movl 0xe4(%rsp), %eax
cmpl $0x0, %eax
jne 0x33681
movl $0x0, 0x15c4(%rsp)
movl $0x1, 0x106c(%rsp)
jmp 0x33c32
movq 0xe8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1180(%rsp)
movl %eax, 0x117c(%rsp)
movl $0x1b0, %esi            # imm = 0x1B0
callq 0x22a00
jmp 0x33c41
jmp 0x33c08
leaq 0x1070(%rsp), %rdi
leaq 0x15b84f(%rip), %rsi    # 0x18eee4
callq 0x22ec0
cmpl $0x0, %eax
jne 0x338c2
movq 0x1170(%rsp), %rdi
leaq 0x1070(%rsp), %rdx
leaq 0x15b836(%rip), %rsi    # 0x18eef0
leaq 0x44c(%rsp), %rcx
movb $0x0, %al
callq 0x229a0
cmpl $0x2, %eax
je 0x33708
movl 0x119c(%rsp), %esi
movq 0x15b0(%rsp), %rdx
leaq 0x15b7bc(%rip), %rdi    # 0x18eea0
xorl %eax, %eax
callq 0xf3b00
jmp 0x336ed
movl $0x0, 0x15c4(%rsp)
movl $0x1, 0x106c(%rsp)
jmp 0x33c32
cmpl $0x0, 0x44c(%rsp)
jge 0x3376c
leaq 0x1188(%rsp), %rdi
callq 0x2b8b0
movl %eax, 0xd4(%rsp)
jmp 0x33728
movl 0xd4(%rsp), %eax
cmpl $0x1, %eax
je 0x33767
movq 0x15b0(%rsp), %rsi
leaq 0x15b7b2(%rip), %rdi    # 0x18eef5
xorl %eax, %eax
callq 0xf3b00
jmp 0x3374c
movl $0x0, 0x15c4(%rsp)
movl $0x1, 0x106c(%rsp)
jmp 0x33c32
jmp 0x338bd
movl 0x44c(%rsp), %eax
movl %eax, 0xcc(%rsp)
leaq 0x1188(%rsp), %rdi
callq 0x2b8b0
movl %eax, 0xd0(%rsp)
jmp 0x33790
movl 0xcc(%rsp), %eax
movl 0xd0(%rsp), %ecx
cmpl %ecx, %eax
jl 0x337e3
movl 0x44c(%rsp), %esi
movl 0x119c(%rsp), %edx
movq 0x15b0(%rsp), %rcx
leaq 0x15b759(%rip), %rdi    # 0x18ef18
xorl %eax, %eax
callq 0xf3b00
jmp 0x337c8
movl $0x0, 0x15c4(%rsp)
movl $0x1, 0x106c(%rsp)
jmp 0x33c32
leaq 0x1188(%rsp), %rdi
callq 0x48390
movq %rax, 0xc0(%rsp)
jmp 0x337fa
movq 0xc0(%rsp), %rax
movq %rax, 0x440(%rsp)
movl 0x44c(%rsp), %esi
leaq 0x1188(%rsp), %rdi
callq 0x2bd10
movq %rax, 0xb8(%rsp)
jmp 0x33828
movq 0xb8(%rsp), %rax
movq %rax, 0x438(%rsp)
movq 0x440(%rsp), %rdi
callq 0x49480
movq %rax, 0xb0(%rsp)
jmp 0x3384f
movq 0xb0(%rsp), %rax
movq %rax, 0x430(%rsp)
movq 0x438(%rsp), %rax
cmpq 0x430(%rsp), %rax
je 0x338bb
cmpq $0x0, 0x430(%rsp)
je 0x33895
movq 0x430(%rsp), %rdi
movq 0x440(%rsp), %rsi
callq 0x55d10
jmp 0x33893
jmp 0x33895
cmpq $0x0, 0x438(%rsp)
je 0x338b9
movq 0x438(%rsp), %rdi
movq 0x440(%rsp), %rsi
callq 0x57450
jmp 0x338b7
jmp 0x338b9
jmp 0x338bb
jmp 0x338bd
jmp 0x33c06
leaq 0x1070(%rsp), %rdi
leaq 0x15b67a(%rip), %rsi    # 0x18ef4b
callq 0x22ec0
cmpl $0x0, %eax
jne 0x33c04
movq 0x1170(%rsp), %rdi
leaq 0x1070(%rsp), %rdx
leaq 0x3b0(%rsp), %rcx
leaq 0x3b0(%rsp), %r8
addq $0x8, %r8
leaq 0x3b0(%rsp), %r9
addq $0x10, %r9
leaq 0x3b0(%rsp), %rax
addq $0x18, %rax
leaq 0x3b0(%rsp), %rsi
addq $0x20, %rsi
movq %rsi, 0x90(%rsp)
leaq 0x3b0(%rsp), %rsi
addq $0x28, %rsi
movq %rsi, 0x98(%rsp)
leaq 0x3b0(%rsp), %rsi
addq $0x30, %rsi
movq %rsi, 0xa0(%rsp)
leaq 0x3b0(%rsp), %rbp
addq $0x38, %rbp
leaq 0x3b0(%rsp), %r13
addq $0x40, %r13
leaq 0x3b0(%rsp), %r12
addq $0x48, %r12
leaq 0x3b0(%rsp), %r15
addq $0x50, %r15
leaq 0x3b0(%rsp), %r14
addq $0x58, %r14
leaq 0x3b0(%rsp), %rbx
addq $0x60, %rbx
leaq 0x3b0(%rsp), %r11
addq $0x68, %r11
leaq 0x3b0(%rsp), %r10
addq $0x70, %r10
leaq 0x3b0(%rsp), %rsi
addq $0x78, %rsi
movq %rsi, 0xa8(%rsp)
leaq 0x15b583(%rip), %rsi    # 0x18ef55
movq %rax, (%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xa8(%rsp), %rax
movq %rbp, 0x20(%rsp)
movq %r13, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movq %r14, 0x40(%rsp)
movq %rbx, 0x48(%rsp)
movq %r11, 0x50(%rsp)
movq %r10, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movb $0x0, %al
callq 0x229a0
cmpl $0x11, %eax
je 0x33a78
movl 0x119c(%rsp), %esi
movq 0x15b0(%rsp), %rdx
leaq 0x15b44c(%rip), %rdi    # 0x18eea0
xorl %eax, %eax
callq 0xf3b00
jmp 0x33a5d
movl $0x0, 0x15c4(%rsp)
movl $0x1, 0x106c(%rsp)
jmp 0x33c32
leaq 0x1caf51(%rip), %rsi    # 0x1fe9d0
leaq 0x2a0(%rsp), %rdi
callq 0x48c80
jmp 0x33a8e
leaq 0x1188(%rsp), %rdi
callq 0x48390
movq %rax, 0x88(%rsp)
jmp 0x33aa5
movq 0x88(%rsp), %rax
movq %rax, 0x298(%rsp)
movq 0x298(%rsp), %rdi
callq 0x49480
movq %rax, 0x80(%rsp)
jmp 0x33acc
movq 0x80(%rsp), %rax
movq %rax, 0x290(%rsp)
cmpq $0x0, 0x290(%rsp)
je 0x33b4b
movq 0x290(%rsp), %rdi
callq 0x2bae0
movq %rax, 0x78(%rsp)
jmp 0x33afb
movq 0x78(%rsp), %rsi
leaq 0x2a0(%rsp), %rdi
callq 0xc1f30
jmp 0x33b0f
movq 0x290(%rsp), %rdi
callq 0x49480
movq %rax, 0x70(%rsp)
jmp 0x33b23
movq 0x70(%rsp), %rax
movq %rax, 0x290(%rsp)
jmp 0x33adc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1180(%rsp)
movl %eax, 0x117c(%rsp)
jmp 0x33bf5
leaq 0x210(%rsp), %rdi
leaq 0x3b0(%rsp), %rsi
callq 0xc6350
jmp 0x33b62
leaq 0x210(%rsp), %rdi
callq 0xc7650
jmp 0x33b71
leaq 0x100(%rsp), %rdi
leaq 0x210(%rsp), %rsi
xorl %edx, %edx
callq 0xc1a60
jmp 0x33b8a
leaq 0x2a0(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0xc1dd0
jmp 0x33ba1
leaq 0x100(%rsp), %rdi
callq 0xc1b00
movq 0x298(%rsp), %rdi
leaq 0x2a0(%rsp), %rsi
callq 0x577d0
jmp 0x33bc5
leaq 0x2a0(%rsp), %rdi
callq 0xc1b00
jmp 0x33c04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1180(%rsp)
movl %eax, 0x117c(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xc1b00
leaq 0x2a0(%rsp), %rdi
callq 0xc1b00
jmp 0x33c41
jmp 0x33c06
jmp 0x33c08
jmp 0x333a7
movq 0x15a0(%rsp), %rdi
callq 0x229d0
jmp 0x33c1c
movl $0x1, 0x15c4(%rsp)
movl $0x1, 0x106c(%rsp)
leaq 0x1188(%rsp), %rdi
callq 0x48210
jmp 0x33c50
leaq 0x1188(%rsp), %rdi
callq 0x48210
jmp 0x33c69
movl 0x15c4(%rsp), %eax
addq $0x15c8, %rsp           # imm = 0x15C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1180(%rsp), %rdi
callq 0x23290
nopw %cs:(%rax,%rax)