// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::ReadObbFile(char const*, gaps::R3SceneNode*)
int R3Scene::
ReadObbFile(const char *filename, R3SceneNode *parent_node)
{
  // Get/check 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 file
  char buffer[4096];
  int line_number = 0;
  while (fgets(buffer, 4096, fp)) {
    // Check line
    line_number++;
    char *bufferp = buffer;
    while (*bufferp && isspace(*bufferp)) bufferp++;
    if (!bufferp) continue;
    if (*bufferp == '#') continue;

    // Parse line
    char cmd[1024], name[1024];
    int category, instance, originator;
    double center[3], axis0[3], axis1[3], radii[3], confidence;
    if (sscanf(bufferp, "%s%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%d%d%lf%d%s", cmd,
      &center[0], &center[1], &center[2],
      &axis0[0], &axis0[1], &axis0[2],
      &axis1[0], &axis1[1], &axis1[2],
      &radii[0], &radii[1], &radii[2],
      &category, &instance,
      &confidence, &originator,
      name) != (unsigned int) 18) {
      RNFail("Error parsing line %d of %s\n", line_number, filename);
      return 0;
    }

    // Check cmd
    if (strcmp(cmd, "OBB")) {
      RNFail("Unrecognized command at line %d in %s\n", line_number, filename);
      return 0;
    }

    // Create node
    R3SceneNode *node = new R3SceneNode(this);
    parent_node->InsertChild(node);

    // Set node name
    char node_name[2048];
    sprintf(node_name, "%s_%03d", name, (int) (1000*confidence));
    node->SetName(node_name);

    // Create color based on category
    double r = (1 + (category % 7)) / 7.0;
    double g = 1.0 - (1 + (category % 17)) / 17.0;
    double b = (1 + (category % 31)) / 31.0;
    R3Brdf *brdf = new R3Brdf(RNRgb(r, g, b));
    InsertBrdf(brdf);
    R3Material *material = new R3Material(brdf);
    InsertMaterial(material);

    // Create element
    R3SceneElement *element = new R3SceneElement();
    element->SetMaterial(material);
    node->InsertElement(element);

    // Create shape 
    R3OrientedBox *obb = new R3OrientedBox(R3Point(center),
      R3Vector(axis0), R3Vector(axis1),
      radii[0], radii[1], radii[2]);
    R3Sphere *nose = new R3Sphere(
      R3Point(center) + radii[0] * R3Vector(axis0), 0.02 * radii[0]);
    element->InsertShape(obb);
    element->InsertShape(nose);
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2138, %rsp           # imm = 0x2138
movq %rsi, %r14
movq %rdi, %rbx
movq %rdx, 0x10(%rsp)
testq %rdx, %rdx
jne 0x2c10f
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x9fc8d(%rip), %rsi     # 0xcbda3
movq %r14, %rdi
callq 0x21ab0
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x2c4e3
movq %rbx, 0x48(%rsp)
movq %r14, 0x40(%rsp)
leaq 0x1130(%rsp), %r13
leaq 0x530(%rsp), %r14
leaq 0x118(%rsp), %rbx
xorl %r15d, %r15d
movq %r13, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq 0x18(%rsp), %rdx
callq 0x21f50
testq %rax, %rax
je 0x2c4f8
incl %r15d
movq %r13, %r12
movsbl (%r12), %ebp
testl %ebp, %ebp
je 0x2c191
movl %ebp, %edi
callq 0x210b0
testl %eax, %eax
je 0x2c18b
incq %r12
jmp 0x2c172
cmpb $0x23, %bpl
je 0x2c151
movq %r12, %rdi
leaq 0x99efc(%rip), %rsi     # 0xc6097
leaq 0x130(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x98(%rsp), %r8
leaq 0xa0(%rsp), %r9
xorl %eax, %eax
pushq %r14
leaq 0x70(%rsp), %r10
pushq %r10
leaq 0x70(%rsp), %r10
pushq %r10
leaq 0x84(%rsp), %r10
pushq %r10
leaq 0x2c(%rsp), %r10
pushq %r10
leaq 0x58(%rsp), %r10
pushq %r10
leaq 0x58(%rsp), %r10
pushq %r10
leaq 0x58(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0x100(%rsp), %r10
pushq %r10
leaq 0xd8(%rsp), %r10
pushq %r10
leaq 0xd8(%rsp), %r10
pushq %r10
leaq 0xd8(%rsp), %r10
pushq %r10
callq 0x21890
addq $0x70, %rsp
cmpl $0x12, %eax
jne 0x2c507
cmpl $0x42424f, 0x130(%rsp) # imm = 0x42424F
jne 0x2c510
movl %r15d, 0x8(%rsp)
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218d0
movq %rax, %r12
movq %rax, %rdi
movq 0x48(%rsp), %rbp
movq %rbp, %rsi
callq 0x37a1e
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3898c
movsd 0x60(%rsp), %xmm0
mulsd 0x99251(%rip), %xmm0    # 0xc54e0
cvttsd2si %xmm0, %ecx
leaq 0x930(%rsp), %r13
movq %r13, %rdi
leaq 0x99e4b(%rip), %rsi     # 0xc60f0
movq %r14, %r15
movq %r14, %rdx
xorl %eax, %eax
callq 0x21140
movq %r12, %rdi
movq %r13, %rsi
callq 0x38b2e
movl 0xc(%rsp), %ecx
movl %ecx, %eax
cltd
pushq $0x7
popq %rsi
idivl %esi
incl %edx
cvtsi2sd %edx, %xmm0
divsd 0x99211(%rip), %xmm0    # 0xc54e8
movsd %xmm0, (%rsp)
movl %ecx, %eax
cltd
pushq $0x11
popq %rsi
idivl %esi
incl %edx
cvtsi2sd %edx, %xmm1
divsd 0x991fe(%rip), %xmm1    # 0xc54f0
movsd 0x98d1e(%rip), %xmm0    # 0xc5018
addsd %xmm0, %xmm1
movsd %xmm1, 0x58(%rsp)
movl %ecx, %eax
cltd
pushq $0x1f
popq %rcx
idivl %ecx
incl %edx
cvtsi2sd %edx, %xmm0
divsd 0x991de(%rip), %xmm0    # 0xc54f8
movsd %xmm0, 0x50(%rsp)
movl $0xb8, %edi
callq 0x218d0
movq %rax, %r13
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
movsd (%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd 0x50(%rsp), %xmm2
callq 0x7c03e
xorpd %xmm0, %xmm0
movq %r13, %rdi
movq %r14, %rsi
movsd 0x98cb8(%rip), %xmm1    # 0xc5018
movaps %xmm1, %xmm2
xorl %edx, %edx
callq 0x3f1a6
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2788a
pushq $0x30
popq %rdi
callq 0x218d0
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x3edf2
movq %rbp, %rdi
movq %r14, %rsi
callq 0x27846
pushq $0x60
popq %rdi
callq 0x218d0
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
callq 0x394c0
movq %r13, %rdi
movq %r14, %rsi
callq 0x39714
movq %r12, %rdi
movq %r13, %rsi
callq 0x389c8
movl $0x80, %edi
callq 0x218d0
movq %rax, %r12
leaq 0xd0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x90(%rsp), %rsi
callq 0x6b8a2
movq %rbx, %rdi
leaq 0x70(%rsp), %rsi
callq 0x6c0c6
movq %r15, %r14
leaq 0xe8(%rsp), %r15
movq %r15, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x6c0c6
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x62ab2
pushq $0x28
popq %rdi
callq 0x218d0
movq %rax, %rbp
movq %rbx, %rdi
leaq 0x90(%rsp), %rsi
callq 0x6b8a2
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rsp)
leaq 0x100(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x6c0c6
leaq 0xe8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
movsd (%rsp), %xmm0
callq 0x6c72a
leaq 0xd0(%rsp), %rdi
movq %rbx, %rsi
leaq 0xe8(%rsp), %rdx
callq 0x6bb3b
movsd 0x20(%rsp), %xmm0
mulsd 0x99055(%rip), %xmm0    # 0xc5500
movq %rbp, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x619a0
movq %r13, %rdi
movq %r12, %rsi
callq 0x3971a
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3971a
leaq 0x1130(%rsp), %r13
movl 0x8(%rsp), %r15d
jmp 0x2c151
leaq 0x9913c(%rip), %rdi     # 0xc5626
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x7e83e
jmp 0x2c528
movq 0x18(%rsp), %rdi
callq 0x218c0
pushq $0x1
popq %rbx
jmp 0x2c528
leaq 0x99b62(%rip), %rdi     # 0xc6070
jmp 0x2c517
leaq 0x99bb2(%rip), %rdi     # 0xc60c9
xorl %ebx, %ebx
movl %r15d, %esi
movq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x7e83e
movl %ebx, %eax
addq $0x2138, %rsp           # imm = 0x2138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c55f
jmp 0x2c579
movq %rax, %rbx
movl $0x1b0, %esi            # imm = 0x1B0
jmp 0x2c567
movq %rax, %rbx
pushq $0x30
popq %rsi
movq %r14, %rdi
jmp 0x2c582
movq %rax, %rbx
pushq $0x60
popq %rsi
jmp 0x2c574
jmp 0x2c55f
movq %rax, %rbx
movl $0x80, %esi
movq %r12, %rdi
jmp 0x2c582
movq %rax, %rbx
movl $0xb8, %esi
movq %r13, %rdi
jmp 0x2c582
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %rbp, %rdi
callq 0x218f0
movq %rbx, %rdi
callq 0x220e0
nop