// File: 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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2720, %rsp           # imm = 0x2720
movq %rdi, 0x2710(%rsp)
movq %rsi, 0x2708(%rsp)
movq %rdx, 0x2700(%rsp)
movq 0x2710(%rsp), %rax
movq %rax, 0xb8(%rsp)
cmpq $0x0, 0x2700(%rsp)
jne 0x357a6
movq 0xb8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x2700(%rsp)
movq 0x2708(%rsp), %rdi
leaq 0x15faee(%rip), %rsi    # 0x1952a3
callq 0x22c10
movq %rax, 0x26f8(%rsp)
cmpq $0x0, %rax
jne 0x357ee
movq 0x2708(%rsp), %rsi
leaq 0x158ef7(%rip), %rdi    # 0x18e6ce
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x271c(%rsp)
jmp 0x3627d
movl $0x0, 0x16ec(%rsp)
movl $0x0, 0x16e8(%rsp)
leaq 0x16f0(%rsp), %rdi
movq 0x26f8(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x230f0
cmpq $0x0, %rax
je 0x36265
movl 0x16ec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x16ec(%rsp)
leaq 0x16f0(%rsp), %rax
movq %rax, 0x16e0(%rsp)
movq 0x16e0(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0xb7(%rsp)
je 0x3587c
movq 0x16e0(%rsp), %rax
movsbl (%rax), %edi
callq 0x220c0
cmpl $0x0, %eax
setne %al
movb %al, 0xb7(%rsp)
movb 0xb7(%rsp), %al
testb $0x1, %al
jne 0x35889
jmp 0x3589f
movq 0x16e0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x16e0(%rsp)
jmp 0x35849
cmpq $0x0, 0x16e0(%rsp)
jne 0x358af
jmp 0x35804
movq 0x16e0(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x358c4
jmp 0x35804
movq 0x16e0(%rsp), %rdi
leaq 0x6e0(%rsp), %rax
leaq 0x1592ee(%rip), %rsi    # 0x18ebc9
leaq 0x6d8(%rsp), %rdx
leaq 0x6d0(%rsp), %rcx
leaq 0x6c8(%rsp), %r8
leaq 0x6c0(%rsp), %r9
leaq 0x6b8(%rsp), %r13
leaq 0x6b0(%rsp), %r12
leaq 0x6a8(%rsp), %r15
leaq 0x6a0(%rsp), %r14
leaq 0x698(%rsp), %rbx
leaq 0x690(%rsp), %r11
leaq 0x688(%rsp), %r10
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movb $0x0, %al
callq 0x229a0
cmpl $0xc, %eax
je 0x35993
movl 0x16ec(%rsp), %esi
movq 0x2708(%rsp), %rdx
leaq 0x1597d1(%rip), %rdi    # 0x18f14d
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x271c(%rsp)
jmp 0x3627d
movl $0x1b0, %edi            # imm = 0x1B0
callq 0x229e0
movq 0xb8(%rsp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0xa8(%rsp)
callq 0x555e0
jmp 0x359ba
movq 0xa8(%rsp), %rax
movq %rax, 0x680(%rsp)
movq 0x2700(%rsp), %rdi
movq 0x680(%rsp), %rsi
callq 0x57450
leaq 0x660(%rsp), %rdi
callq 0x49140
leaq 0x650(%rsp), %rdi
callq 0x48d30
jmp 0x359fb
movl $0x68, %edi
callq 0x229e0
movq %rax, 0xa0(%rsp)
jmp 0x35a0f
movsd 0x6d8(%rsp), %xmm0
movsd 0x6d0(%rsp), %xmm1
movsd 0x698(%rsp), %xmm2
leaq 0x630(%rsp), %rdi
callq 0xcc870
jmp 0x35a39
movq 0xa0(%rsp), %rdi
leaq 0x630(%rsp), %rsi
callq 0xb06c0
jmp 0x35a50
movq 0xa0(%rsp), %rax
movq %rax, 0x648(%rsp)
movq 0x648(%rsp), %rsi
leaq 0x650(%rsp), %rdi
callq 0x48f60
jmp 0x35a77
movl $0x68, %edi
callq 0x229e0
movq %rax, 0x98(%rsp)
jmp 0x35a8b
movsd 0x6c8(%rsp), %xmm0
movsd 0x6c0(%rsp), %xmm1
movsd 0x698(%rsp), %xmm2
leaq 0x610(%rsp), %rdi
callq 0xcc870
jmp 0x35ab5
movq 0x98(%rsp), %rdi
leaq 0x610(%rsp), %rsi
callq 0xb06c0
jmp 0x35acc
movq 0x98(%rsp), %rax
movq %rax, 0x628(%rsp)
movq 0x628(%rsp), %rsi
leaq 0x650(%rsp), %rdi
callq 0x48f60
jmp 0x35af3
movl $0x68, %edi
callq 0x229e0
movq %rax, 0x90(%rsp)
jmp 0x35b07
movsd 0x6b8(%rsp), %xmm0
movsd 0x6b0(%rsp), %xmm1
movsd 0x698(%rsp), %xmm2
leaq 0x5f0(%rsp), %rdi
callq 0xcc870
jmp 0x35b31
movq 0x90(%rsp), %rdi
leaq 0x5f0(%rsp), %rsi
callq 0xb06c0
jmp 0x35b48
movq 0x90(%rsp), %rax
movq %rax, 0x608(%rsp)
movq 0x608(%rsp), %rsi
leaq 0x650(%rsp), %rdi
callq 0x48f60
jmp 0x35b6f
movl $0x68, %edi
callq 0x229e0
movq %rax, 0x88(%rsp)
jmp 0x35b83
movsd 0x6a8(%rsp), %xmm0
movsd 0x6a0(%rsp), %xmm1
movsd 0x698(%rsp), %xmm2
leaq 0x5d0(%rsp), %rdi
callq 0xcc870
jmp 0x35bad
movq 0x88(%rsp), %rdi
leaq 0x5d0(%rsp), %rsi
callq 0xb06c0
jmp 0x35bc4
movq 0x88(%rsp), %rax
movq %rax, 0x5e8(%rsp)
movq 0x5e8(%rsp), %rsi
leaq 0x650(%rsp), %rdi
callq 0x48f60
jmp 0x35beb
movl $0x88, %edi
callq 0x229e0
movq %rax, 0x80(%rsp)
jmp 0x35bff
movq 0x80(%rsp), %rdi
movq 0x648(%rsp), %rsi
movq 0x628(%rsp), %rdx
movq 0x608(%rsp), %rcx
callq 0xb1110
jmp 0x35c26
movq 0x80(%rsp), %rax
movq %rax, 0x5c8(%rsp)
movq 0x5c8(%rsp), %rsi
leaq 0x660(%rsp), %rdi
callq 0x49190
jmp 0x35c4d
movl $0x88, %edi
callq 0x229e0
movq %rax, 0x78(%rsp)
jmp 0x35c5e
movq 0x78(%rsp), %rdi
movq 0x648(%rsp), %rsi
movq 0x608(%rsp), %rdx
movq 0x5e8(%rsp), %rcx
callq 0xb1110
jmp 0x35c82
movq 0x78(%rsp), %rax
movq %rax, 0x5c0(%rsp)
movq 0x5c0(%rsp), %rsi
leaq 0x660(%rsp), %rdi
callq 0x49190
jmp 0x35ca6
movl $0x70, %edi
callq 0x229e0
movq %rax, 0x70(%rsp)
jmp 0x35cb7
movq 0x70(%rsp), %rdi
leaq 0x650(%rsp), %rsi
leaq 0x660(%rsp), %rdx
callq 0xacc40
jmp 0x35cd3
movq 0x70(%rsp), %rax
movq %rax, 0x5b8(%rsp)
movq 0x5b8(%rsp), %rsi
movq (%rsi), %rax
movq 0x88(%rax), %rax
leaq 0x568(%rsp), %rdi
callq *%rax
jmp 0x35cfe
leaq 0x5a0(%rsp), %rdi
leaq 0x568(%rsp), %rsi
callq 0xb8c60
jmp 0x35d15
leaq 0x568(%rsp), %rdi
callq 0x2c1a0
movsd 0x690(%rsp), %xmm0
movsd 0x698(%rsp), %xmm1
subsd %xmm1, %xmm0
leaq 0x1c95f1(%rip), %rsi    # 0x1ff330
leaq 0x538(%rsp), %rdi
callq 0x2b630
jmp 0x35d4e
leaq 0x550(%rsp), %rdi
leaq 0x5a0(%rsp), %rsi
leaq 0x538(%rsp), %rdx
callq 0xcce30
jmp 0x35d6d
movl $0xe8, %edi
callq 0x229e0
movq %rax, 0x68(%rsp)
jmp 0x35d7e
movb $0x1, 0x4f7(%rsp)
movq 0x5b8(%rsp), %rsi
movq (%rsi), %rax
movq 0x88(%rax), %rax
leaq 0x4f8(%rsp), %rdi
callq *%rax
jmp 0x35da4
leaq 0x4f8(%rsp), %rdi
callq 0x2b5b0
movsd %xmm0, 0x60(%rsp)
jmp 0x35db9
movq 0x68(%rsp), %rdi
movsd 0x60(%rsp), %xmm0
movsd 0x15826c(%rip), %xmm1   # 0x18e038
mulsd %xmm1, %xmm0
leaq 0x5a0(%rsp), %rsi
leaq 0x550(%rsp), %rdx
callq 0xb4190
jmp 0x35de7
movb $0x0, 0x4f7(%rsp)
leaq 0x4f8(%rsp), %rdi
callq 0x2c1a0
movq 0x68(%rsp), %rax
movq %rax, 0x530(%rsp)
movl $0xb8, %edi
callq 0x229e0
movq %rax, 0x58(%rsp)
jmp 0x35e1a
movsd 0x688(%rsp), %xmm1
movsd 0x15877d(%rip), %xmm0   # 0x18e5a8
addsd %xmm0, %xmm1
leaq 0x4d0(%rsp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
callq 0xef340
jmp 0x35e44
movq 0x58(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
leaq 0x4d0(%rsp), %rsi
xorps %xmm0, %xmm0
movsd 0x1581b0(%rip), %xmm2   # 0x18e010
movaps %xmm2, %xmm1
callq 0x646f0
jmp 0x35e6a
movq 0xb8(%rsp), %rdi
movq 0x58(%rsp), %rax
movq %rax, 0x4e8(%rsp)
movq 0x4e8(%rsp), %rsi
callq 0x2ccd0
jmp 0x35e8e
movl $0x30, %edi
callq 0x229e0
movq %rax, 0x50(%rsp)
jmp 0x35e9f
movq 0x50(%rsp), %rdi
movq 0x4e8(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x63e20
jmp 0x35eb7
movq 0xb8(%rsp), %rdi
movq 0x50(%rsp), %rax
movq %rax, 0x4c8(%rsp)
movq 0x4c8(%rsp), %rsi
callq 0x2cbd0
jmp 0x35edb
movl $0x60, %edi
callq 0x229e0
movq %rax, 0x48(%rsp)
jmp 0x35eec
movq 0x48(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x58f20
jmp 0x35efc
movq 0x48(%rsp), %rax
movq %rax, 0x4c0(%rsp)
movq 0x4c0(%rsp), %rdi
movq 0x4c8(%rsp), %rsi
callq 0x59350
jmp 0x35f20
movq 0x4c0(%rsp), %rdi
movq 0x5b8(%rsp), %rsi
callq 0x59370
jmp 0x35f37
movq 0x4c0(%rsp), %rdi
movq 0x530(%rsp), %rsi
callq 0x59370
jmp 0x35f4e
movq 0x680(%rsp), %rdi
movq 0x4c0(%rsp), %rsi
callq 0x57510
jmp 0x35f65
movl 0x16e8(%rsp), %edx
movl %edx, %eax
incl %eax
movl %eax, 0x16e8(%rsp)
movsd 0x688(%rsp), %xmm0
movsd 0x158630(%rip), %xmm1   # 0x18e5b8
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
leaq 0x1591d3(%rip), %rsi    # 0x18f16a
xorl %eax, %eax
leaq 0xc0(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x22160
movq 0x40(%rsp), %rsi
movq 0x680(%rsp), %rdi
callq 0x57850
jmp 0x35fbf
leaq 0x650(%rsp), %rdi
callq 0x49340
leaq 0x660(%rsp), %rdi
callq 0x492a0
jmp 0x35804
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0x1b0, %esi            # imm = 0x1B0
callq 0x22a00
jmp 0x36295
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
jmp 0x36256
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
jmp 0x36249
movq 0xa0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0x68, %esi
callq 0x22a00
jmp 0x36249
movq 0x98(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0x68, %esi
callq 0x22a00
jmp 0x36249
movq 0x90(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0x68, %esi
callq 0x22a00
jmp 0x36249
movq 0x88(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0x68, %esi
callq 0x22a00
jmp 0x36249
movq 0x80(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0x88, %esi
callq 0x22a00
jmp 0x36249
movq 0x78(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0x88, %esi
callq 0x22a00
jmp 0x36249
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0x70, %esi
callq 0x22a00
jmp 0x36249
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
leaq 0x568(%rsp), %rdi
callq 0x2c1a0
jmp 0x36249
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
jmp 0x361bf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
leaq 0x4f8(%rsp), %rdi
callq 0x2c1a0
testb $0x1, 0x4f7(%rsp)
jne 0x361cb
jmp 0x361da
movq 0x68(%rsp), %rdi
movl $0xe8, %esi
callq 0x22a00
jmp 0x36249
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0xb8, %esi
callq 0x22a00
jmp 0x36249
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0x30, %esi
callq 0x22a00
jmp 0x36249
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x678(%rsp)
movl %eax, 0x674(%rsp)
movl $0x60, %esi
callq 0x22a00
leaq 0x650(%rsp), %rdi
callq 0x49340
leaq 0x660(%rsp), %rdi
callq 0x492a0
jmp 0x36295
movq 0x26f8(%rsp), %rdi
callq 0x229d0
movl $0x1, 0x271c(%rsp)
movl 0x271c(%rsp), %eax
addq $0x2720, %rsp           # imm = 0x2720
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x678(%rsp), %rdi
callq 0x23290
nopw %cs:(%rax,%rax)