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

  // Read body
  char cmd[128];
  int command_number = 1;
  RNArray<R3MeshVertex *> degenerate_triangle_vertices;
  while (fscanf(fp, "%s", cmd) == 1) {
    if (!strcmp(cmd, "#vertex")) {
      // Read data
      double px, py, pz;
      double nx, ny, nz;
      double ts, tt;
      if (fscanf(fp, "%lf%lf%lf%lf%lf%lf%lf%lf", &px, &py, &pz, &nx, &ny, &nz, &ts, &tt) != 8) {
        RNFail("Unable to read vertex at command %d in file %s", command_number, filename);
        return 0;
      }

      // Create vertex
      R3Point point(px, py, pz);
      R3Vector normal(nx, ny, nz);
      if (normal.IsZero()) CreateVertex(point);
      else CreateVertex(point, normal);
    }
    else if (!strcmp(cmd, "#shape_triangle")) {
      // Read data
      int m;
      int i1, i2, i3;
      if (fscanf(fp, "%d%d%d%d", &m, &i1, &i2, &i3) != 4) {
        RNFail("Unable to read triangle at command %d in file %s", command_number, filename);
        return 0;
      }

      // Get vertices
      R3MeshVertex *v1 = vertices.Kth(i1);
      R3MeshVertex *v2 = vertices.Kth(i2);
      R3MeshVertex *v3 = vertices.Kth(i3);

      // Check vertices
      if ((v1 == v2) || (v2 == v3) || (v1 == v3)) continue;

      // Create face
      R3MeshFace *face = CreateFace(v1, v2, v3);
      if (!face) {
        // 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(v1);
        degenerate_triangle_vertices.Insert(v2);
        degenerate_triangle_vertices.Insert(v3);
      }

      // Set face material
      if (face) SetFaceMaterial(face, m);
    }
	
    // Increment command number
    command_number++;
  }

  // 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 $0x118, %rsp            # imm = 0x118
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x8c439(%rip), %rsi     # 0xabdd3
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x1fbc7
movq %rax, %r14
movq %r15, 0x28(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x6f80c
movl $0x1, 0x4(%rsp)
leaq 0x78(%rsp), %r15
movq %r14, %rdi
leaq 0x884dc(%rip), %rsi     # 0xa7eb0
leaq 0x90(%rsp), %rdx
xorl %eax, %eax
callq 0xe840
cmpl $0x1, %eax
jne 0x1fbdf
movabsq $0x78657472657623, %rax # imm = 0x78657472657623
cmpq %rax, 0x90(%rsp)
je 0x1fab7
movdqa 0x90(%rsp), %xmm0
pcmpeqb 0x8289b(%rip), %xmm0    # 0xa22b0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x1fbbe
movq %r14, %rdi
leaq 0x82cc3(%rip), %rsi     # 0xa26f1
movq %r15, %rdx
leaq 0x30(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x18(%rsp), %r9
xorl %eax, %eax
callq 0xe840
cmpl $0x4, %eax
jne 0x1fcaa
movslq 0x30(%rsp), %rcx
movq 0x8(%rbx), %rax
movq (%rax,%rcx,8), %r12
movslq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %r13
cmpq %r13, %r12
je 0x1f9ca
movslq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rbp
cmpq %rbp, %r12
je 0x1f9ca
cmpq %r13, %rbp
je 0x1f9ca
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
je 0x1fb8b
movl 0x78(%rsp), %ecx
movl %ecx, 0x98(%rax)
jmp 0x1fbbe
movq %r14, %rdi
leaq 0x88b6c(%rip), %rsi     # 0xa862d
leaq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x58(%rsp), %r9
xorl %eax, %eax
leaq 0x68(%rsp), %r10
pushq %r10
leaq 0x78(%rsp), %r10
pushq %r10
leaq 0x58(%rsp), %r10
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
callq 0xe840
addq $0x20, %rsp
cmpl $0x8, %eax
jne 0x1fce1
movq 0x20(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
movq %r15, %rdi
callq 0x3c532
movsd 0x58(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
leaq 0x30(%rsp), %rdi
callq 0x3cfc6
movsd 0x30(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq (%rbx), %rax
jne 0x1fb79
jp 0x1fb79
movsd 0x38(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x1fb79
jp 0x1fb79
movsd 0x40(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x1fb79
jp 0x1fb79
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
jmp 0x1fbbe
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
jmp 0x1fbbe
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x6f8a2
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x6f8a2
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x6f8a2
incl 0x4(%rsp)
jmp 0x1f9ca
leaq 0x827ec(%rip), %rdi     # 0xa23ba
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
jmp 0x1fccd
cmpl $0x3, 0x14(%rsp)
jl 0x1fc9b
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
movq (%rax,%rbp,8), %r13
movq 0x8(%rax,%rbp,8), %r12
movq 0x10(%rax,%rbp,8), %r15
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1fc85
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1fc85
addq $0x18, %r13
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rax, %r13
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 %r13, %rsi
movq %r12, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq *0x48(%r9)
addq $0x3, %rbp
movslq 0x14(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %rbp
jle 0x1fbec
movq %r14, %rdi
callq 0xe430
movl $0x1, %ebx
jmp 0x1fcc3
leaq 0x828c9(%rip), %rdi     # 0xa257a
xorl %ebx, %ebx
movl 0x4(%rsp), %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
leaq 0x8(%rsp), %rdi
callq 0x6f876
movl %ebx, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x82853(%rip), %rdi     # 0xa253b
xorl %ebx, %ebx
movl 0x4(%rsp), %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x1fcc3
jmp 0x1fd12
jmp 0x1fd12
jmp 0x1fd12
jmp 0x1fd12
jmp 0x1fd12
jmp 0x1fd12
jmp 0x1fd12
jmp 0x1fd12
jmp 0x1fd12
jmp 0x1fd12
jmp 0x1fd12
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x6f876
movq %rbx, %rdi
callq 0xe7e0
nop