// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x878, %rsp            # imm = 0x878
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x7d651(%rip), %rsi     # 0x9caf3
movq %r14, %rdi
callq 0xe550
testq %rax, %rax
je 0x1f75d
movq %rax, %r13
movq %r14, 0x30(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x6630a
leaq 0x18(%rsp), %rdi
callq 0x6630a
leaq 0x470(%rsp), %r12
leaq 0x70(%rsp), %rbp
leaq 0x8(%rsp), %r14
xorl %r15d, %r15d
movq %r13, 0x38(%rsp)
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r13, %rdx
callq 0xe760
testq %rax, %rax
je 0x1f682
incl %r15d
movq %r12, %rdi
leaq 0x79951(%rip), %rsi     # 0x98e60
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe430
cmpl $0x1, %eax
jne 0x1f4e9
movq %rbp, %rdi
leaq 0x740c7(%rip), %rsi     # 0x935ef
pushq $0x7
popq %rdx
callq 0xe480
testl %eax, %eax
je 0x1f5ed
movq %rbp, %rdi
leaq 0x7439a(%rip), %rsi     # 0x938dc
pushq $0x6
popq %rdx
callq 0xe480
testl %eax, %eax
je 0x1f4e9
movabsq $0x706f6f6c646e65, %rax # imm = 0x706F6F6C646E65
cmpq %rax, 0x70(%rsp)
jne 0x1f4e9
pushq $0x2
popq %r13
movslq 0x14(%rsp), %rax
cmpq %rax, %r13
jge 0x1f678
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq -0x8(%rax,%r13,8), %rdx
cmpq %rdx, %rsi
je 0x1f5e5
movq (%rax,%r13,8), %rcx
cmpq %rcx, %rdx
je 0x1f5e5
cmpq %rcx, %rsi
je 0x1f5e5
movq (%rbx), %rax
movq %rbx, %rdi
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1f5db
movq 0x8(%rsp), %rax
movq (%rax), %rsi
leaq 0x18(%rsp), %rdi
callq 0x22bfc
movq 0x8(%rsp), %rax
movq -0x8(%rax,%r13,8), %rsi
leaq 0x18(%rsp), %rdi
callq 0x22bfc
movq 0x8(%rsp), %rax
movq (%rax,%r13,8), %rsi
leaq 0x18(%rsp), %rdi
callq 0x22bfc
movq %r14, %rdi
xorl %esi, %esi
callq 0x66528
incq %r13
jmp 0x1f563
movq %r12, %rdi
leaq 0x73dad(%rip), %rsi     # 0x933a4
movq %rbp, %rdx
leaq 0x50(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0x40(%rsp), %r9
xorl %eax, %eax
callq 0xe430
cmpl $0x4, %eax
jne 0x1f784
movl %r15d, 0x2c(%rsp)
movq %r13, %r15
movq %r12, %r13
movq %rbp, %r12
movq %r14, %rbp
movsd 0x50(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x37b4c
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rbp, %r14
movq %rbp, %rdi
movq %rax, %rsi
callq 0x22bfc
movq %r12, %rbp
movq %r13, %r12
movq %r15, %r13
movl 0x2c(%rsp), %r15d
jmp 0x1f4e9
movq 0x38(%rsp), %r13
jmp 0x1f4e9
movq %r13, %rbp
xorl %r13d, %r13d
movslq 0x24(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %r13
jg 0x1f73c
movq 0x18(%rsp), %rax
movq (%rax,%r13,8), %r12
movq 0x8(%rax,%r13,8), %r15
movq 0x10(%rax,%r13,8), %r14
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1f733
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1f733
addq $0x18, %r12
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r12
addq $0x18, %r15
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r15
addq $0x18, %r14
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq (%rbx), %r9
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq *0x48(%r9)
addq $0x3, %r13
jmp 0x1f688
movq %rbp, %rdi
callq 0xe440
pushq $0x1
popq %rbx
leaq 0x18(%rsp), %rdi
callq 0x66378
leaq 0x8(%rsp), %rdi
callq 0x66378
jmp 0x1f770
leaq 0x73c06(%rip), %rdi     # 0x9336a
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebx, %eax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x74127(%rip), %rdi     # 0x938b2
xorl %ebx, %ebx
movq 0x30(%rsp), %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x681ca
jmp 0x1f747
jmp 0x1f7b1
movq %rax, %rbx
jmp 0x1f7be
jmp 0x1f7b1
jmp 0x1f7b1
jmp 0x1f7b1
jmp 0x1f7b1
jmp 0x1f7b1
jmp 0x1f7b1
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x66378
leaq 0x8(%rsp), %rdi
callq 0x66378
movq %rbx, %rdi
callq 0xe810