// File: /workspace/github_cpp_makefile_star2_O1/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 $0x868, %rsp            # imm = 0x868
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x89f4f(%rip), %rsi     # 0xabd93
movq %r14, %rdi
callq 0xe530
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x22142
movq %r14, 0x28(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x705ec
movq %rsp, %rdi
callq 0x705ec
leaq 0x460(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x20(%rsp), %rdx
callq 0xe730
testq %rax, %rax
je 0x22063
leaq 0x460(%rsp), %r13
leaq 0x10(%rsp), %r15
movl $0x6574756f, %r12d      # imm = 0x6574756F
xorl %ebp, %ebp
incl %ebp
movq %r13, %rdi
leaq 0x86002(%rip), %rsi     # 0xa7eb4
leaq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0xe420
cmpl $0x1, %eax
jne 0x22048
movl 0x60(%rsp), %eax
movl $0x74726576, %ecx       # imm = 0x74726576
xorl %ecx, %eax
movl 0x63(%rsp), %ecx
movl $0x786574, %edx         # imm = 0x786574
xorl %edx, %ecx
orl %eax, %ecx
je 0x21fb2
movl 0x60(%rsp), %eax
xorl %r12d, %eax
movzwl 0x64(%rsp), %ecx
xorl $0x72, %ecx
orl %eax, %ecx
je 0x22048
movabsq $0x706f6f6c646e65, %rax # imm = 0x706F6F6C646E65
cmpq %rax, 0x60(%rsp)
jne 0x22048
cmpl $0x3, 0x1c(%rsp)
jl 0x22048
movl $0x2, %r14d
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq -0x8(%rax,%r14,8), %rdx
cmpq %rdx, %rsi
je 0x21f9c
movq (%rax,%r14,8), %rcx
cmpq %rcx, %rdx
je 0x21f9c
cmpq %rcx, %rsi
je 0x21f9c
movq (%rbx), %rax
movq %rbx, %rdi
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x21f92
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0x70682
movq 0x10(%rsp), %rax
movq -0x8(%rax,%r14,8), %rsi
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0x70682
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rsi
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0x70682
movq %r15, %rdi
xorl %esi, %esi
callq 0x7082c
incq %r14
movslq 0x1c(%rsp), %rax
cmpq %rax, %r14
jl 0x21f22
jmp 0x22048
movq %r13, %rdi
leaq 0x8043b(%rip), %rsi     # 0xa23f7
leaq 0x60(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x30(%rsp), %r9
xorl %eax, %eax
callq 0xe420
movl %eax, %r14d
cmpl $0x4, %eax
jne 0x22029
movq %r15, %r12
movsd 0x40(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x48(%rsp), %r15
movq %r15, %rdi
callq 0x3ca26
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %r12, %r15
movl 0x1c(%rsp), %edx
movq %r12, %rdi
movq %rax, %rsi
callq 0x70682
movl $0x6574756f, %r12d      # imm = 0x6574756F
jmp 0x2203e
leaq 0x808d5(%rip), %rdi     # 0xa2905
movq 0x28(%rsp), %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x72a1e
cmpl $0x4, %r14d
jne 0x22169
movq %r13, %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x20(%rsp), %rdx
callq 0xe730
testq %rax, %rax
jne 0x21ea6
cmpl $0x3, 0xc(%rsp)
jl 0x2211f
xorl %r13d, %r13d
movq (%rsp), %rax
movq (%rax,%r13,8), %r14
movq 0x8(%rax,%r13,8), %r12
movq 0x10(%rax,%r13,8), %r15
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x22109
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x22109
addq $0x18, %r14
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r14
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 (%rbx), %r9
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq *0x48(%r9)
addq $0x3, %r13
movslq 0xc(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %r13
jle 0x22071
movq 0x20(%rsp), %rdi
callq 0xe430
movl $0x1, %ebx
movq %rsp, %rdi
callq 0x70656
leaq 0x10(%rsp), %rdi
callq 0x70656
jmp 0x22155
leaq 0x80274(%rip), %rdi     # 0xa23bd
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x72a1e
movl %ebx, %eax
addq $0x868, %rsp            # imm = 0x868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x2212e
movq %rax, %rbx
jmp 0x2218b
jmp 0x22180
jmp 0x22180
jmp 0x22180
jmp 0x22180
jmp 0x22180
jmp 0x22180
jmp 0x22180
movq %rax, %rbx
movq %rsp, %rdi
callq 0x70656
leaq 0x10(%rsp), %rdi
callq 0x70656
movq %rbx, %rdi
callq 0xe7e0
nop