// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::ReadSTLFile(char const*)
int R3Mesh::
ReadSTLFile(const char *filename)
{
  // Open file
  FILE *fp;
  if (!(fp = fopen(filename, "r"))) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Read body
  int line_number = 0;
  char buffer[1024], cmd[1024];
  RNArray<R3MeshVertex *> vertices;
  RNArray<R3MeshVertex *> degenerate_triangle_vertices;
  while (fgets(buffer, 1024, fp)) {
    line_number++;
    if (sscanf(buffer, "%s", cmd) != 1) continue;
    if (!strcmp(cmd, "vertex")) {
      RNCoord x, y, z;
      if (sscanf(buffer, "%s%lf%lf%lf", cmd, &x, &y, &z) != 4) {
        RNFail("Error in vertex of STL file %s on line %d", filename, line_number);
        return 0;
      }

      // Make vertex
      const R3Point position(x, y, z);
      R3MeshVertex *vertex = CreateVertex(position);
      vertices.Insert(vertex);
    }
    else if (!strcmp(cmd, "outer")) {
      // Just checking
      assert(vertices.IsEmpty());
    }
    else if (!strcmp(cmd, "endloop")) {
      // Create face(s)
      for (int i = 2; i < vertices.NEntries(); i++) {
        if (vertices[0] == vertices[i-1]) continue;
        if (vertices[i-1] == vertices[i]) continue;
        if (vertices[0] == vertices[i]) continue;
        if (!CreateFace(vertices[0], vertices[i-1], vertices[i])) {
          // Must have been degeneracy (e.g., flips or three faces sharing an edge)
          // Remember for later processing (to preserve vertex indices)
          degenerate_triangle_vertices.Insert(vertices[0]);
          degenerate_triangle_vertices.Insert(vertices[i-1]);
          degenerate_triangle_vertices.Insert(vertices[i]);
        }
        vertices.Empty();
      }
    }
  }

  // Create degenerate triangles
  for (int i = 0; i <= degenerate_triangle_vertices.NEntries()-3; i+=3) {
    R3MeshVertex *v1 = degenerate_triangle_vertices.Kth(i+0);
    R3MeshVertex *v2 = degenerate_triangle_vertices.Kth(i+1);
    R3MeshVertex *v3 = degenerate_triangle_vertices.Kth(i+2);
    if (!CreateFace(v1, v2, v3)) {
      if (!CreateFace(v1, v3, v2)) {
        // Note: these vertices are allocated separately, and so they will not be deleted (memory leak)
        R3MeshVertex *v1a = CreateVertex(VertexPosition(v1));
        R3MeshVertex *v2a = CreateVertex(VertexPosition(v2));
        R3MeshVertex *v3a = CreateVertex(VertexPosition(v3));
        CreateFace(v1a, v2a, v3a);
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0x9e8, %rsp            # imm = 0x9E8
movq %rdi, 0x9d8(%rsp)
movq %rsi, 0x9d0(%rsp)
movq 0x9d8(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x9d0(%rsp), %rdi
leaq 0x105d0d(%rip), %rsi    # 0x138673
callq 0xf5e0
movq %rax, 0x9c8(%rsp)
cmpq $0x0, %rax
jne 0x3299f
movq 0x9d0(%rsp), %rsi
leaq 0x100a58(%rip), %rdi    # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x9e4(%rsp)
jmp 0x3317d
movl $0x0, 0x9c4(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x36800
leaq 0x1a0(%rsp), %rdi
callq 0x36800
jmp 0x329c6
jmp 0x329c8
movq 0x9c8(%rsp), %rdx
leaq 0x5c0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0xf7e0
movq %rax, 0x108(%rsp)
jmp 0x329ec
movq 0x108(%rsp), %rax
cmpq $0x0, %rax
je 0x32ec8
movl 0x9c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c4(%rsp)
leaq 0x5c0(%rsp), %rdi
leaq 0x1c0(%rsp), %rdx
leaq 0x101fc8(%rip), %rsi    # 0x1349ee
movb $0x0, %al
callq 0xf4a0
cmpl $0x1, %eax
je 0x32a73
jmp 0x329c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x198(%rsp)
movl %eax, 0x194(%rsp)
jmp 0x3316e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x198(%rsp)
movl %eax, 0x194(%rsp)
leaq 0x1a0(%rsp), %rdi
callq 0x36850
jmp 0x3316e
leaq 0x1c0(%rsp), %rdi
leaq 0x100ae0(%rip), %rsi    # 0x133562
callq 0xf700
cmpl $0x0, %eax
jne 0x32b82
leaq 0x5c0(%rsp), %rdi
leaq 0x1c0(%rsp), %rdx
leaq 0x100973(%rip), %rsi    # 0x13341a
leaq 0x188(%rsp), %rcx
leaq 0x180(%rsp), %r8
leaq 0x178(%rsp), %r9
movb $0x0, %al
callq 0xf4a0
cmpl $0x4, %eax
je 0x32b05
movq 0x9d0(%rsp), %rsi
movl 0x9c4(%rsp), %edx
leaq 0x100e52(%rip), %rdi    # 0x133933
xorl %eax, %eax
callq 0xd4f30
jmp 0x32aea
movl $0x0, 0x9e4(%rsp)
movl $0x1, 0x174(%rsp)
jmp 0x33152
movsd 0x188(%rsp), %xmm0
movsd 0x180(%rsp), %xmm1
movsd 0x178(%rsp), %xmm2
leaq 0x158(%rsp), %rdi
callq 0x6eb50
jmp 0x32b2f
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
leaq 0x158(%rsp), %rsi
callq *%rax
movq %rax, 0x100(%rsp)
jmp 0x32b56
movq 0x100(%rsp), %rax
movq %rax, 0x150(%rsp)
movq 0x150(%rsp), %rsi
leaq 0x1b0(%rsp), %rdi
callq 0x37410
jmp 0x32b7d
jmp 0x32ec3
leaq 0x1c0(%rsp), %rdi
leaq 0x100dcc(%rip), %rsi    # 0x13395d
callq 0xf700
cmpl $0x0, %eax
jne 0x32ba0
jmp 0x32ec1
leaq 0x1c0(%rsp), %rdi
leaq 0x100db4(%rip), %rsi    # 0x133963
callq 0xf700
cmpl $0x0, %eax
jne 0x32ebf
movl $0x2, 0x14c(%rsp)
movl 0x14c(%rsp), %eax
movl %eax, 0xf8(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x13600
movl %eax, 0xfc(%rsp)
jmp 0x32bec
movl 0xf8(%rsp), %eax
movl 0xfc(%rsp), %ecx
cmpl %ecx, %eax
jge 0x32ebd
leaq 0x1b0(%rsp), %rdi
xorl %esi, %esi
callq 0x37800
movq %rax, 0xf0(%rsp)
jmp 0x32c1b
movq 0xf0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xe0(%rsp)
movl 0x14c(%rsp), %esi
decl %esi
leaq 0x1b0(%rsp), %rdi
callq 0x37800
movq %rax, 0xe8(%rsp)
jmp 0x32c4e
movq 0xe0(%rsp), %rax
movq 0xe8(%rsp), %rcx
cmpq (%rcx), %rax
jne 0x32c68
jmp 0x32ea7
movl 0x14c(%rsp), %esi
decl %esi
leaq 0x1b0(%rsp), %rdi
callq 0x37800
movq %rax, 0xd8(%rsp)
jmp 0x32c88
movq 0xd8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xc8(%rsp)
movl 0x14c(%rsp), %esi
leaq 0x1b0(%rsp), %rdi
callq 0x37800
movq %rax, 0xd0(%rsp)
jmp 0x32cb9
movq 0xc8(%rsp), %rax
movq 0xd0(%rsp), %rcx
cmpq (%rcx), %rax
jne 0x32cd3
jmp 0x32ea7
leaq 0x1b0(%rsp), %rdi
xorl %esi, %esi
callq 0x37800
movq %rax, 0xc0(%rsp)
jmp 0x32cec
movq 0xc0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
movl 0x14c(%rsp), %esi
leaq 0x1b0(%rsp), %rdi
callq 0x37800
movq %rax, 0xb8(%rsp)
jmp 0x32d1d
movq 0xb0(%rsp), %rax
movq 0xb8(%rsp), %rcx
cmpq (%rcx), %rax
jne 0x32d37
jmp 0x32ea7
leaq 0x1b0(%rsp), %rdi
xorl %esi, %esi
callq 0x37800
movq %rax, 0xa8(%rsp)
jmp 0x32d50
movq 0xa8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movl 0x14c(%rsp), %esi
decl %esi
leaq 0x1b0(%rsp), %rdi
callq 0x37800
movq %rax, 0xa0(%rsp)
jmp 0x32d83
movq 0xa0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x88(%rsp)
movl 0x14c(%rsp), %esi
leaq 0x1b0(%rsp), %rdi
callq 0x37800
movq %rax, 0x90(%rsp)
jmp 0x32db4
movq 0x88(%rsp), %rdx
movq 0x98(%rsp), %rsi
movq 0x110(%rsp), %rdi
movq 0x90(%rsp), %rax
movq (%rax), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x80(%rsp)
jmp 0x32ded
movq 0x80(%rsp), %rax
cmpq $0x0, %rax
jne 0x32e94
leaq 0x1b0(%rsp), %rdi
xorl %esi, %esi
callq 0x37800
movq %rax, 0x78(%rsp)
jmp 0x32e15
movq 0x78(%rsp), %rax
movq (%rax), %rsi
leaq 0x1a0(%rsp), %rdi
callq 0x37410
jmp 0x32e2c
movl 0x14c(%rsp), %esi
decl %esi
leaq 0x1b0(%rsp), %rdi
callq 0x37800
movq %rax, 0x70(%rsp)
jmp 0x32e49
movq 0x70(%rsp), %rax
movq (%rax), %rsi
leaq 0x1a0(%rsp), %rdi
callq 0x37410
jmp 0x32e60
movl 0x14c(%rsp), %esi
leaq 0x1b0(%rsp), %rdi
callq 0x37800
movq %rax, 0x68(%rsp)
jmp 0x32e7b
movq 0x68(%rsp), %rax
movq (%rax), %rsi
leaq 0x1a0(%rsp), %rdi
callq 0x37410
jmp 0x32e92
jmp 0x32e94
leaq 0x1b0(%rsp), %rdi
xorl %esi, %esi
callq 0xd1d30
jmp 0x32ea5
jmp 0x32ea7
movl 0x14c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14c(%rsp)
jmp 0x32bc8
jmp 0x32ebf
jmp 0x32ec1
jmp 0x32ec3
jmp 0x329c8
movl $0x0, 0x148(%rsp)
movl 0x148(%rsp), %eax
movl %eax, 0x60(%rsp)
leaq 0x1a0(%rsp), %rdi
callq 0x13600
movl %eax, 0x64(%rsp)
jmp 0x32ef1
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jg 0x3312d
movl 0x148(%rsp), %esi
leaq 0x1a0(%rsp), %rdi
callq 0x377e0
movq %rax, 0x58(%rsp)
jmp 0x32f1f
movq 0x58(%rsp), %rax
movq %rax, 0x140(%rsp)
movl 0x148(%rsp), %esi
incl %esi
leaq 0x1a0(%rsp), %rdi
callq 0x377e0
movq %rax, 0x50(%rsp)
jmp 0x32f49
movq 0x50(%rsp), %rax
movq %rax, 0x138(%rsp)
movl 0x148(%rsp), %esi
addl $0x2, %esi
leaq 0x1a0(%rsp), %rdi
callq 0x377e0
movq %rax, 0x48(%rsp)
jmp 0x32f74
movq 0x110(%rsp), %rdi
movq 0x48(%rsp), %rax
movq %rax, 0x130(%rsp)
movq 0x140(%rsp), %rsi
movq 0x138(%rsp), %rdx
movq 0x130(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x40(%rsp)
jmp 0x32fb4
movq 0x40(%rsp), %rax
cmpq $0x0, %rax
jne 0x33115
movq 0x110(%rsp), %rdi
movq 0x140(%rsp), %rsi
movq 0x130(%rsp), %rdx
movq 0x138(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x38(%rsp)
jmp 0x32ff6
movq 0x38(%rsp), %rax
cmpq $0x0, %rax
jne 0x33113
movq 0x110(%rsp), %rdi
movq 0x140(%rsp), %rsi
callq 0x13660
movq %rax, 0x30(%rsp)
jmp 0x33021
movq 0x30(%rsp), %rsi
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x28(%rsp)
jmp 0x33042
movq 0x110(%rsp), %rdi
movq 0x28(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x138(%rsp), %rsi
callq 0x13660
movq %rax, 0x20(%rsp)
jmp 0x3306b
movq 0x20(%rsp), %rsi
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x18(%rsp)
jmp 0x3308c
movq 0x110(%rsp), %rdi
movq 0x18(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x130(%rsp), %rsi
callq 0x13660
movq %rax, 0x10(%rsp)
jmp 0x330b5
movq 0x10(%rsp), %rsi
movq 0x110(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x8(%rsp)
jmp 0x330d6
movq 0x110(%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x128(%rsp), %rsi
movq 0x120(%rsp), %rdx
movq 0x118(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
jmp 0x33111
jmp 0x33113
jmp 0x33115
jmp 0x33117
movl 0x148(%rsp), %eax
addl $0x3, %eax
movl %eax, 0x148(%rsp)
jmp 0x32ed3
movq 0x9c8(%rsp), %rdi
callq 0xf4b0
jmp 0x3313c
movl $0x1, 0x9e4(%rsp)
movl $0x1, 0x174(%rsp)
leaq 0x1a0(%rsp), %rdi
callq 0x36850
leaq 0x1b0(%rsp), %rdi
callq 0x36850
jmp 0x3317d
leaq 0x1b0(%rsp), %rdi
callq 0x36850
jmp 0x3318c
movl 0x9e4(%rsp), %eax
addq $0x9e8, %rsp            # imm = 0x9E8
retq
movq 0x198(%rsp), %rdi
callq 0xf890
nopl (%rax)