// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::ReadRectangleFile(char const*, gaps::R3SceneNode*)
int R3Scene::
ReadRectangleFile(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;
  int assignment_index = 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 name[4096];
    double x1, y1, x2, y2, x3, y3, x4, y4, zmin, zmax, score;
    if (sscanf(bufferp, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%s", 
       &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4, &zmin, &zmax, &score, name) != (unsigned int) 12) {
      RNFail("Error parsing line %d of %s\n", line_number, filename);
      return 0;
    }

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

    // Create shape element
    RNArray<R3Triangle *> triangles;
    RNArray<R3TriangleVertex *> vertices;
    R3TriangleVertex *v1 = new R3TriangleVertex(R3Point(x1, y1, zmin)); vertices.Insert(v1);
    R3TriangleVertex *v2 = new R3TriangleVertex(R3Point(x2, y2, zmin)); vertices.Insert(v2);
    R3TriangleVertex *v3 = new R3TriangleVertex(R3Point(x3, y3, zmin)); vertices.Insert(v3);
    R3TriangleVertex *v4 = new R3TriangleVertex(R3Point(x4, y4, zmin)); vertices.Insert(v4);
    R3Triangle *t1 = new R3Triangle(v1, v2, v3); triangles.Insert(t1);
    R3Triangle *t2 = new R3Triangle(v1, v3, v4); triangles.Insert(t2);
    R3TriangleArray *base = new R3TriangleArray(vertices, triangles);
    R3Point base_centroid = base->BBox().Centroid();
    R3Point top_centroid = base_centroid + (zmax - zmin) * R3posz_vector;
    R3Cylinder *marker = new R3Cylinder(base_centroid, top_centroid, 0.01 * base->BBox().DiagonalRadius());
    R3Brdf *brdf = new R3Brdf(RNRgb(0, 0.25 + score, 0));
    InsertBrdf(brdf);
    R3Material *material = new R3Material(brdf);
    InsertMaterial(material);
    R3SceneElement *element = new R3SceneElement();
    element->SetMaterial(material);
    element->InsertShape(base);
    element->InsertShape(marker);
    node->InsertElement(element);
    
    // Set node name
    char node_name[1024];
    sprintf(node_name, "A%d_S%03d", assignment_index++, (int) (1000*score));
    node->SetName(node_name);
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x24f8, %rsp           # imm = 0x24F8
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movq %rdx, 0x40(%rsp)
testq %rdx, %rdx
jne 0x2d0da
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x40(%rsp)
leaq 0xb3fa2(%rip), %rsi     # 0xe1083
movq %r14, %rdi
callq 0x21ab0
testq %rax, %rax
je 0x2d604
movq %r14, 0x60(%rsp)
leaq 0x4f0(%rsp), %rbx
movq %rbx, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, 0x48(%rsp)
movq %rax, %rdx
callq 0x21f30
testq %rax, %rax
je 0x2d5f3
movq 0x8(%rsp), %rax
leaq 0x38(%rax), %rcx
movq %rcx, 0x70(%rsp)
addq $0x28, %rax
movq %rax, 0x68(%rsp)
leaq 0xf0(%rsp), %r13
xorl %eax, %eax
movl $0x0, 0x28(%rsp)
incl %eax
movl %eax, 0x2c(%rsp)
movb 0x4f0(%rsp), %bpl
testb %bpl, %bpl
je 0x2d186
leaq 0x4f0(%rsp), %rbx
movsbl %bpl, %edi
callq 0x210b0
testl %eax, %eax
je 0x2d17c
movb 0x1(%rbx), %bpl
incq %rbx
testb %bpl, %bpl
jne 0x2d161
jmp 0x2d186
cmpb $0x23, %bpl
je 0x2d5cc
movq %rbx, %rdi
leaq 0xad99c(%rip), %rsi     # 0xdab2c
leaq 0xb8(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
leaq 0xa8(%rsp), %r8
leaq 0xa0(%rsp), %r9
xorl %eax, %eax
leaq 0x14f0(%rsp), %r10
pushq %r10
leaq 0x60(%rsp), %r10
pushq %r10
leaq 0x88(%rsp), %r10
pushq %r10
leaq 0x38(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
leaq 0xd0(%rsp), %r10
pushq %r10
callq 0x21890
addq $0x40, %rsp
cmpl $0xc, %eax
jne 0x2d619
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218c0
movq %rax, %rbx
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x3b560
movq 0x40(%rsp), %rdi
movq %rbx, 0x50(%rsp)
movq %rbx, %rsi
callq 0x3c97e
leaq 0x30(%rsp), %rdi
callq 0x8758c
leaq 0x10(%rsp), %rdi
callq 0x8758c
movl $0x68, %edi
callq 0x218c0
movq %rax, %r14
movsd 0xb8(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movq %r13, %rdi
callq 0x753ca
movq %r14, %rdi
movq %r13, %rsi
callq 0x69528
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x87622
movl $0x68, %edi
callq 0x218c0
movq %rax, %r12
movsd 0xa8(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movq %r13, %rdi
callq 0x753ca
movq %r12, %rdi
movq %r13, %rsi
callq 0x69528
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x87622
movl $0x68, %edi
callq 0x218c0
movq %rax, %rbx
movsd 0x98(%rsp), %xmm0
movsd 0x90(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movq %r13, %rdi
callq 0x753ca
movq %rbx, %rdi
movq %r13, %rsi
callq 0x69528
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x87622
movl $0x68, %edi
callq 0x218c0
movq %rax, %r15
movsd 0x88(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movq %r13, %rdi
callq 0x753ca
movq %r15, %rdi
movq %r13, %rsi
callq 0x69528
movl 0x1c(%rsp), %edx
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x87622
movl $0x88, %edi
callq 0x218c0
movq %rax, %rbp
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x69a40
movl 0x3c(%rsp), %edx
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x87622
movl $0x88, %edi
callq 0x218c0
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x69a40
movl 0x3c(%rsp), %edx
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x87622
movl $0x70, %edi
callq 0x218c0
movq %rax, %r14
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x67b88
movq (%r14), %rax
movq %r13, %rdi
movq %r14, %rsi
callq *0x88(%rax)
leaq 0xc0(%rsp), %rdi
movq %r13, %rsi
callq 0x6d4f2
movq %r13, %rdi
callq 0x6dafc
movsd 0x78(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
movq %r13, %rdi
leaq 0xaf9a4(%rip), %rsi     # 0xdcdd0
callq 0x767fa
leaq 0xd8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movq %r13, %rdx
callq 0x7570f
movl $0xe8, %edi
callq 0x218c0
movq %rax, %r12
movq (%r14), %rax
movq %r13, %rdi
movq %r14, %rsi
callq *0x88(%rax)
movq %r13, %rdi
callq 0x6d330
mulsd 0xacbab(%rip), %xmm0    # 0xda020
mulsd 0xacbab(%rip), %xmm0    # 0xda028
movq %r12, %rdi
leaq 0xc0(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
callq 0x6b25c
movq %r13, %rdi
callq 0x6dafc
movl $0xb8, %edi
callq 0x218c0
movq %rax, %rbx
movsd 0x58(%rsp), %xmm1
addsd 0xad020(%rip), %xmm1    # 0xda4d8
xorpd %xmm0, %xmm0
xorpd %xmm2, %xmm2
movq %r13, %rdi
callq 0x87400
xorpd %xmm0, %xmm0
movq %rbx, %rdi
movq %r13, %rsi
movsd 0xacb3e(%rip), %xmm1    # 0xda018
movapd %xmm1, %xmm2
xorl %edx, %edx
callq 0x43ea4
movq 0x8(%rsp), %rax
movq %rax, 0x8(%rbx)
movl 0x44(%rax), %edx
movl %edx, 0x10(%rbx)
movq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x87622
movl $0x30, %edi
callq 0x218c0
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x43ace
movq 0x8(%rsp), %rax
movq %rax, (%r15)
movl 0x34(%rax), %edx
movl %edx, 0x8(%r15)
movq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x87622
movl $0x60, %edi
callq 0x218c0
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
callq 0x3d514
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3d7b6
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3d7bc
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3d7bc
movq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c9ce
movsd 0x58(%rsp), %xmm0
mulsd 0xacf56(%rip), %xmm0    # 0xda4e0
cvttsd2si %xmm0, %ecx
movq %r13, %rdi
leaq 0xadb25(%rip), %rsi     # 0xdb0bd
movl 0x28(%rsp), %ebx
movl %ebx, %edx
xorl %eax, %eax
callq 0x21140
movq 0x50(%rsp), %rdi
movq %r13, %rsi
callq 0x3cc1a
incl %ebx
movl %ebx, 0x28(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x875f6
leaq 0x30(%rsp), %rdi
callq 0x875f6
leaq 0x4f0(%rsp), %rbx
movq %rbx, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq 0x48(%rsp), %rdx
callq 0x21f30
testq %rax, %rax
movl 0x2c(%rsp), %eax
jne 0x2d146
movq 0x48(%rsp), %rdi
callq 0x218b0
movl $0x1, %ebx
jmp 0x2d632
leaq 0xad04b(%rip), %rdi     # 0xda656
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x8a0fe
jmp 0x2d632
leaq 0xada80(%rip), %rdi     # 0xdb0a0
xorl %ebx, %ebx
movl 0x2c(%rsp), %esi
movq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x8a0fe
movl %ebx, %eax
addq $0x24f8, %rsp           # imm = 0x24F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r13
movl $0x60, %esi
jmp 0x2d715
movq %rax, %r13
jmp 0x2d6e1
movq %rax, %r13
movl $0x30, %esi
jmp 0x2d706
jmp 0x2d73b
movq %rax, %r13
jmp 0x2d748
movq %rax, %r13
movl $0x88, %esi
jmp 0x2d722
jmp 0x2d73b
movq %rax, %r13
movl $0x88, %esi
movq %rbp, %rdi
jmp 0x2d734
movq %rax, %r13
movl $0x1b0, %esi            # imm = 0x1B0
movq %rbx, %rdi
callq 0x218e0
jmp 0x2d752
movq %rax, %r13
movl $0x70, %esi
jmp 0x2d731
jmp 0x2d73b
movq %rax, %r13
leaq 0xf0(%rsp), %rdi
callq 0x6dafc
jmp 0x2d73e
jmp 0x2d73b
jmp 0x2d73b
movq %rax, %r13
leaq 0xf0(%rsp), %rdi
callq 0x6dafc
movl $0xe8, %esi
jmp 0x2d722
jmp 0x2d73b
jmp 0x2d73b
movq %rax, %r13
movl $0xb8, %esi
jmp 0x2d715
jmp 0x2d73b
jmp 0x2d73b
jmp 0x2d73b
jmp 0x2d73b
movq %rax, %r13
movl $0x68, %esi
movq %r15, %rdi
jmp 0x2d734
jmp 0x2d73b
movq %rax, %r13
movl $0x68, %esi
movq %rbx, %rdi
jmp 0x2d734
movq %rax, %r13
movl $0x68, %esi
movq %r12, %rdi
jmp 0x2d734
jmp 0x2d73b
movq %rax, %r13
movl $0x68, %esi
movq %r14, %rdi
callq 0x218e0
jmp 0x2d73e
movq %rax, %r13
leaq 0x10(%rsp), %rdi
callq 0x875f6
leaq 0x30(%rsp), %rdi
callq 0x875f6
movq %r13, %rdi
callq 0x220c0