// File: /workspace/github_cpp_makefile_star2_O2/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 $0x24e8, %rsp           # imm = 0x24E8
movq %rsi, %r14
movq %rdi, 0x18(%rsp)
movq %rdx, 0x30(%rsp)
testq %rdx, %rdx
jne 0x2bb26
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
leaq 0xa0276(%rip), %rsi     # 0xcbda3
movq %r14, %rdi
callq 0x21ab0
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x2bf9f
movq %r14, 0x60(%rsp)
leaq 0xe0(%rsp), %r13
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
leaq 0x14e0(%rsp), %rbx
movq %rbx, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq 0x38(%rsp), %rdx
callq 0x21f50
testq %rax, %rax
je 0x2bfb4
incl 0x10(%rsp)
movsbl (%rbx), %ebp
testl %ebp, %ebp
je 0x2bba4
movl %ebp, %edi
callq 0x210b0
testl %eax, %eax
je 0x2bb9e
incq %rbx
jmp 0x2bb87
cmpb $0x23, %bpl
je 0x2bb60
movq %rbx, %rdi
leaq 0x99f4e(%rip), %rsi     # 0xc5afc
leaq 0xa8(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
leaq 0x98(%rsp), %r8
leaq 0x90(%rsp), %r9
xorl %eax, %eax
leaq 0x4e0(%rsp), %r10
pushq %r10
leaq 0x60(%rsp), %r10
pushq %r10
leaq 0x78(%rsp), %r10
pushq %r10
leaq 0x20(%rsp), %r10
pushq %r10
leaq 0x90(%rsp), %r10
pushq %r10
leaq 0xa0(%rsp), %r10
pushq %r10
leaq 0xb0(%rsp), %r10
pushq %r10
leaq 0xc0(%rsp), %r10
pushq %r10
callq 0x21890
addq $0x40, %rsp
cmpl $0xc, %eax
jne 0x2bfc3
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x218d0
movq %rax, %rbx
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x37a1e
movq 0x30(%rsp), %rdi
movq %rbx, 0x40(%rsp)
movq %rbx, %rsi
callq 0x3898c
leaq 0x48(%rsp), %rdi
callq 0x7c1c6
leaq 0x20(%rsp), %rdi
callq 0x7c1c6
pushq $0x68
popq %rbx
movq %rbx, %rdi
callq 0x218d0
movq %rax, %r12
movsd 0xa8(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
movq %r13, %rdi
callq 0x6b874
movq %r12, %rdi
movq %r13, %rsi
callq 0x6098e
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x2737a
movq %rbx, %rdi
callq 0x218d0
movq %rax, %r15
movsd 0x98(%rsp), %xmm0
movsd 0x90(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
movq %r13, %rdi
callq 0x6b874
movq %r15, %rdi
movq %r13, %rsi
callq 0x6098e
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x2737a
movq %rbx, %rdi
callq 0x218d0
movq %rax, %r14
movsd 0x88(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
movq %r13, %rdi
callq 0x6b874
movq %r14, %rdi
movq %r13, %rsi
callq 0x6098e
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x2737a
movq %rbx, %rdi
callq 0x218d0
movq %rax, %rbp
movsd 0x78(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
movq %r13, %rdi
callq 0x6b874
movq %rbp, %rdi
movq %r13, %rsi
callq 0x6098e
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x2737a
movl $0x88, %edi
callq 0x218d0
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x60de0
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x2737a
movl $0x88, %edi
callq 0x218d0
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x60de0
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x2737a
pushq $0x70
popq %rdi
callq 0x218d0
movq %rax, %r12
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x5f2ae
movq (%r12), %rax
movq %r13, %rdi
movq %r12, %rsi
callq *0x88(%rax)
leaq 0xb0(%rsp), %rdi
movq %r13, %rsi
callq 0x644be
movq %r13, %rdi
callq 0x6499a
movsd 0x68(%rsp), %xmm0
subsd 0x8(%rsp), %xmm0
movq %r13, %rdi
leaq 0x9bf3d(%rip), %rsi     # 0xc7d60
callq 0x6c72a
leaq 0xc8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movq %r13, %rdx
callq 0x6bb3b
movl $0xe8, %edi
callq 0x218d0
movq %rax, %r15
movq (%r12), %rax
movq %r13, %rdi
movq %r12, %rsi
callq *0x88(%rax)
movq %r13, %rdi
callq 0x2735a
mulsd 0x991b3(%rip), %xmm0    # 0xc5020
movq %r15, %rdi
leaq 0xb0(%rsp), %rsi
leaq 0xc8(%rsp), %rdx
callq 0x62452
movq %r13, %rdi
callq 0x6499a
movl $0xb8, %edi
callq 0x218d0
movq %rax, %r14
movsd 0x58(%rsp), %xmm1
addsd 0x99630(%rip), %xmm1    # 0xc54d8
xorpd %xmm0, %xmm0
xorpd %xmm2, %xmm2
movq %r13, %rdi
callq 0x7c03e
xorpd %xmm0, %xmm0
movq %r14, %rdi
movq %r13, %rsi
movsd 0x9914e(%rip), %xmm1    # 0xc5018
movapd %xmm1, %xmm2
xorl %edx, %edx
callq 0x3f1a6
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x2788a
pushq $0x30
popq %rdi
callq 0x218d0
movq %rax, %rbp
movq %rax, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x3edf2
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x27846
pushq $0x60
popq %rdi
callq 0x218d0
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
callq 0x394c0
movq %r14, %rdi
movq %rbp, %rsi
callq 0x39714
movq %r14, %rdi
movq %r12, %rsi
callq 0x3971a
movq %r14, %rdi
movq %r15, %rsi
callq 0x3971a
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x389c8
movsd 0x58(%rsp), %xmm0
mulsd 0x99588(%rip), %xmm0    # 0xc54e0
cvttsd2si %xmm0, %ecx
movq %r13, %rdi
leaq 0x9a127(%rip), %rsi     # 0xc608d
movl 0x14(%rsp), %ebx
movl %ebx, %edx
xorl %eax, %eax
callq 0x21140
movq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x38b2e
incl %ebx
movl %ebx, 0x14(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x7c234
leaq 0x48(%rsp), %rdi
callq 0x7c234
jmp 0x2bb60
leaq 0x99680(%rip), %rdi     # 0xc5626
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x7e83e
jmp 0x2bfdc
movq 0x38(%rsp), %rdi
callq 0x218c0
pushq $0x1
popq %rbx
jmp 0x2bfdc
leaq 0x9a0a6(%rip), %rdi     # 0xc6070
xorl %ebx, %ebx
movl 0x10(%rsp), %esi
movq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x7e83e
movl %ebx, %eax
addq $0x24e8, %rsp           # imm = 0x24E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c0c6
movq %rax, %r13
pushq $0x60
jmp 0x2c0b9
movq %rax, %r13
pushq $0x30
jmp 0x2c090
movq %rax, %r13
jmp 0x2c080
jmp 0x2c0c6
movq %rax, %r13
leaq 0xe0(%rsp), %rdi
callq 0x6499a
jmp 0x2c0c9
jmp 0x2c0c6
movq %rax, %r13
pushq $0x70
jmp 0x2c09f
movq %rax, %r13
movl $0x1b0, %esi            # imm = 0x1B0
movq %rbx, %rdi
callq 0x218f0
jmp 0x2c0dd
movq %rax, %r13
jmp 0x2c0d3
jmp 0x2c0c6
jmp 0x2c055
movq %rax, %r13
movl $0x88, %esi
movq %rbx, %rdi
jmp 0x2c0bd
jmp 0x2c0c6
jmp 0x2c0c6
movq %rax, %r13
movl $0xb8, %esi
jmp 0x2c0ba
movq %rax, %r13
leaq 0xe0(%rsp), %rdi
callq 0x6499a
movl $0xe8, %esi
jmp 0x2c0ad
jmp 0x2c0c6
jmp 0x2c0c6
movq %rax, %r13
pushq $0x68
popq %rsi
movq %rbp, %rdi
jmp 0x2c0bd
jmp 0x2c0c6
jmp 0x2c0c6
movq %rax, %r13
pushq $0x68
popq %rsi
movq %r12, %rdi
jmp 0x2c0bd
jmp 0x2c0c6
movq %rax, %r13
pushq $0x68
popq %rsi
movq %r15, %rdi
jmp 0x2c0bd
jmp 0x2c0c6
movq %rax, %r13
pushq $0x68
popq %rsi
movq %r14, %rdi
callq 0x218f0
jmp 0x2c0c9
jmp 0x2c0c6
movq %rax, %r13
leaq 0x20(%rsp), %rdi
callq 0x7c234
leaq 0x48(%rsp), %rdi
callq 0x7c234
movq %r13, %rdi
callq 0x220e0
nop