// File: 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 $0x22d8, %rsp           # imm = 0x22D8
movq %rdi, 0x22c8(%rsp)
movq %rsi, 0x22c0(%rsp)
movq %rdx, 0x22b8(%rsp)
movq 0x22c8(%rsp), %rax
movq %rax, 0xe8(%rsp)
cmpq $0x0, 0x22b8(%rsp)
jne 0x36307
movq 0xe8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x22b8(%rsp)
movq 0x22c0(%rsp), %rdi
leaq 0x15ef8d(%rip), %rsi    # 0x1952a3
callq 0x22c10
movq %rax, 0x22b0(%rsp)
cmpq $0x0, %rax
jne 0x3634f
movq 0x22c0(%rsp), %rsi
leaq 0x158396(%rip), %rdi    # 0x18e6ce
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x22d4(%rsp)
jmp 0x36b5a
movl $0x0, 0x12ac(%rsp)
leaq 0x12b0(%rsp), %rdi
movq 0x22b0(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x230f0
cmpq $0x0, %rax
je 0x36b42
movl 0x12ac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x12ac(%rsp)
leaq 0x12b0(%rsp), %rax
movq %rax, 0x12a0(%rsp)
movq 0x12a0(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0xe7(%rsp)
je 0x363d2
movq 0x12a0(%rsp), %rax
movsbl (%rax), %edi
callq 0x220c0
cmpl $0x0, %eax
setne %al
movb %al, 0xe7(%rsp)
movb 0xe7(%rsp), %al
testb $0x1, %al
jne 0x363df
jmp 0x363f5
movq 0x12a0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x12a0(%rsp)
jmp 0x3639f
cmpq $0x0, 0x12a0(%rsp)
jne 0x36405
jmp 0x3635a
movq 0x12a0(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x3641a
jmp 0x3635a
movq 0x12a0(%rsp), %rdi
leaq 0xea0(%rsp), %rdx
leaq 0xa70(%rsp), %rcx
leaq 0xa70(%rsp), %r8
addq $0x8, %r8
leaq 0xa70(%rsp), %r9
addq $0x10, %r9
leaq 0xa50(%rsp), %rax
leaq 0xa50(%rsp), %rsi
addq $0x8, %rsi
movq %rsi, 0xb8(%rsp)
leaq 0xa50(%rsp), %rsi
addq $0x10, %rsi
movq %rsi, 0xc0(%rsp)
leaq 0xa30(%rsp), %rsi
movq %rsi, 0xc8(%rsp)
leaq 0xa30(%rsp), %rsi
addq $0x8, %rsi
movq %rsi, 0xd0(%rsp)
leaq 0xa30(%rsp), %rbp
addq $0x10, %rbp
leaq 0xa10(%rsp), %r13
leaq 0xa10(%rsp), %r12
addq $0x8, %r12
leaq 0xa10(%rsp), %r15
addq $0x10, %r15
leaq 0xaa0(%rsp), %rsi
movq %rsi, 0xd8(%rsp)
leaq 0x158c93(%rip), %rsi    # 0x18f174
leaq 0xa9c(%rsp), %r14
leaq 0xa98(%rsp), %rbx
leaq 0xa08(%rsp), %r11
leaq 0xa94(%rsp), %r10
movq %rax, (%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xd8(%rsp), %rax
movq %rbp, 0x28(%rsp)
movq %r13, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq %r15, 0x40(%rsp)
movq %r14, 0x48(%rsp)
movq %rbx, 0x50(%rsp)
movq %r11, 0x58(%rsp)
movq %r10, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movb $0x0, %al
callq 0x229a0
cmpl $0x12, %eax
je 0x365a7
movl 0x12ac(%rsp), %esi
movq 0x22c0(%rsp), %rdx
leaq 0x158bbd(%rip), %rdi    # 0x18f14d
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x22d4(%rsp)
jmp 0x36b5a
leaq 0xea0(%rsp), %rdi
leaq 0x158bf0(%rip), %rsi    # 0x18f1a6
callq 0x22ec0
cmpl $0x0, %eax
je 0x365ed
movl 0x12ac(%rsp), %esi
movq 0x22c0(%rsp), %rdx
leaq 0x158bd4(%rip), %rdi    # 0x18f1aa
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x22d4(%rsp)
jmp 0x36b5a
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x229e0
movq 0xe8(%rsp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0xb0(%rsp)
callq 0x555e0
jmp 0x36614
movq 0xb0(%rsp), %rax
movq %rax, 0xa00(%rsp)
movq 0x22b8(%rsp), %rdi
movq 0xa00(%rsp), %rsi
callq 0x57450
movsd 0xa08(%rsp), %xmm0
movsd 0x157f6e(%rip), %xmm1   # 0x18e5b8
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
leaq 0x158b78(%rip), %rsi    # 0x18f1d1
xorl %eax, %eax
leaq 0x1f0(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
leaq 0xaa0(%rsp), %rdx
callq 0x22160
movq 0xa0(%rsp), %rsi
movq 0xa00(%rsp), %rdi
callq 0x57850
movslq 0xa9c(%rsp), %rax
movl %eax, %edx
imulq $-0x6db6db6d, %rax, %rax # imm = 0x92492493
shrq $0x20, %rax
addl %edx, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x2, %eax
addl %ecx, %eax
movl %eax, %ecx
shll $0x3, %ecx
subl %ecx, %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x1(%rax,%rcx), %eax
cvtsi2sd %eax, %xmm0
movsd 0x157ef7(%rip), %xmm1   # 0x18e5c0
divsd %xmm1, %xmm0
movsd %xmm0, 0x1e8(%rsp)
movslq 0xa9c(%rsp), %rcx
movl %ecx, %eax
imulq $0x78787879, %rcx, %rcx # imm = 0x78787879
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
sarl $0x3, %ecx
addl %edx, %ecx
movl %ecx, %edx
shll $0x4, %edx
addl %edx, %ecx
subl %ecx, %eax
incl %eax
cvtsi2sd %eax, %xmm0
movsd 0x157eba(%rip), %xmm1   # 0x18e5c8
divsd %xmm1, %xmm0
movsd 0x1578f6(%rip), %xmm1   # 0x18e010
addsd %xmm1, %xmm0
movsd %xmm0, 0x1e0(%rsp)
movslq 0xa9c(%rsp), %rax
movl %eax, %edx
imulq $-0x7bdef7bd, %rax, %rax # imm = 0x84210843
shrq $0x20, %rax
addl %edx, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x4, %eax
addl %ecx, %eax
movl %eax, %ecx
shll $0x5, %ecx
subl %ecx, %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x1(%rax,%rcx), %eax
cvtsi2sd %eax, %xmm0
movsd 0x157e6d(%rip), %xmm1   # 0x18e5d0
divsd %xmm1, %xmm0
movsd %xmm0, 0x1d8(%rsp)
movl $0xb8, %edi
callq 0x229e0
movq %rax, 0xa8(%rsp)
movsd 0x1e8(%rsp), %xmm0
movsd 0x1e0(%rsp), %xmm1
movsd 0x1d8(%rsp), %xmm2
leaq 0x1b8(%rsp), %rdi
callq 0xef340
jmp 0x367ac
movq 0xa8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
leaq 0x1b8(%rsp), %rsi
xorps %xmm0, %xmm0
movsd 0x157845(%rip), %xmm2   # 0x18e010
movaps %xmm2, %xmm1
callq 0x646f0
jmp 0x367d5
movq 0xe8(%rsp), %rdi
movq 0xa8(%rsp), %rax
movq %rax, 0x1d0(%rsp)
movq 0x1d0(%rsp), %rsi
callq 0x2ccd0
movl $0x30, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x98(%rsp)
movq 0x1d0(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x63e20
jmp 0x36825
movq 0xe8(%rsp), %rdi
movq 0x98(%rsp), %rax
movq %rax, 0x1b0(%rsp)
movq 0x1b0(%rsp), %rsi
callq 0x2cbd0
movl $0x60, %edi
callq 0x229e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x90(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x58f20
jmp 0x3686d
movq 0x90(%rsp), %rax
movq %rax, 0x1a8(%rsp)
movq 0x1a8(%rsp), %rdi
movq 0x1b0(%rsp), %rsi
callq 0x59350
movq 0xa00(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x57510
movl $0x80, %edi
callq 0x229e0
movq %rax, 0x88(%rsp)
leaq 0x188(%rsp), %rdi
leaq 0xa70(%rsp), %rsi
callq 0xcc8f0
jmp 0x368d0
leaq 0x170(%rsp), %rdi
leaq 0xa50(%rsp), %rsi
callq 0xcdb00
jmp 0x368e7
leaq 0x158(%rsp), %rdi
leaq 0xa30(%rsp), %rsi
callq 0xcdb00
jmp 0x368fe
movq 0x88(%rsp), %rdi
movsd 0xa10(%rsp), %xmm0
movsd 0xa18(%rsp), %xmm1
movsd 0xa20(%rsp), %xmm2
leaq 0x188(%rsp), %rsi
leaq 0x170(%rsp), %rdx
leaq 0x158(%rsp), %rcx
callq 0xb4ec0
jmp 0x36940
movq 0x88(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movl $0x28, %edi
callq 0x229e0
movq %rax, 0x80(%rsp)
leaq 0x120(%rsp), %rdi
leaq 0xa70(%rsp), %rsi
callq 0xcc8f0
jmp 0x36979
movsd 0xa10(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0xa50(%rsp), %rsi
callq 0xcdb00
jmp 0x3699f
movsd 0x78(%rsp), %xmm0
leaq 0x108(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x2b630
jmp 0x369bc
leaq 0x138(%rsp), %rdi
leaq 0x120(%rsp), %rsi
leaq 0x108(%rsp), %rdx
callq 0xcce30
jmp 0x369db
movq 0x80(%rsp), %rdi
movsd 0xa10(%rsp), %xmm0
movsd 0x157be4(%rip), %xmm1   # 0x18e5d8
mulsd %xmm1, %xmm0
leaq 0x138(%rsp), %rsi
callq 0xb2bd0
jmp 0x36a07
movq 0x80(%rsp), %rax
movq %rax, 0x150(%rsp)
movq 0x1a8(%rsp), %rdi
movq 0x1a0(%rsp), %rsi
callq 0x59370
movq 0x1a8(%rsp), %rdi
movq 0x150(%rsp), %rsi
callq 0x59370
jmp 0x3635a
movq 0xb0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9f8(%rsp)
movl %eax, 0x9f4(%rsp)
movl $0x1b0, %esi            # imm = 0x1B0
callq 0x22a00
jmp 0x36b73
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9f8(%rsp)
movl %eax, 0x9f4(%rsp)
movl $0xb8, %esi
callq 0x22a00
jmp 0x36b73
movq 0x98(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9f8(%rsp)
movl %eax, 0x9f4(%rsp)
movl $0x30, %esi
callq 0x22a00
jmp 0x36b73
movq 0x90(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9f8(%rsp)
movl %eax, 0x9f4(%rsp)
movl $0x60, %esi
callq 0x22a00
jmp 0x36b73
movq 0x88(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9f8(%rsp)
movl %eax, 0x9f4(%rsp)
movl $0x80, %esi
callq 0x22a00
jmp 0x36b73
movq 0x80(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x9f8(%rsp)
movl %eax, 0x9f4(%rsp)
movl $0x28, %esi
callq 0x22a00
jmp 0x36b73
movq 0x22b0(%rsp), %rdi
callq 0x229d0
movl $0x1, 0x22d4(%rsp)
movl 0x22d4(%rsp), %eax
addq $0x22d8, %rsp           # imm = 0x22D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9f8(%rsp), %rdi
callq 0x23290