// File: /workspace/github_cpp_makefile_star2_O2/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 0x7f66d(%rip), %rsi     # 0x9caf3
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x1d6a6
movq %rax, %r14
movq %r15, 0x28(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x6630a
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
leaq 0x78(%rsp), %r15
movq %r14, %rdi
leaq 0x7b9a1(%rip), %rsi     # 0x98e60
leaq 0x90(%rsp), %rdx
xorl %eax, %eax
callq 0xe870
cmpl $0x1, %eax
jne 0x1d6be
movabsq $0x78657472657623, %rax # imm = 0x78657472657623
cmpq %rax, 0x90(%rsp)
je 0x1d5a2
movdqa 0x90(%rsp), %xmm0
pcmpeqb 0x75d80(%rip), %xmm0    # 0x93280
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x1d69d
movq %r14, %rdi
leaq 0x76188(%rip), %rsi     # 0x936a1
movq %r15, %rdx
leaq 0x30(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x8(%rsp), %r9
xorl %eax, %eax
callq 0xe870
cmpl $0x4, %eax
jne 0x1d781
movslq 0x30(%rsp), %rcx
movq 0x8(%rbx), %rax
movq (%rax,%rcx,8), %rbp
movslq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %r13
cmpq %r13, %rbp
je 0x1d4b5
movslq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %r12
cmpq %r12, %rbp
je 0x1d4b5
cmpq %r13, %r12
je 0x1d4b5
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
je 0x1d676
movl 0x78(%rsp), %ecx
movl %ecx, 0x98(%rax)
jmp 0x1d69d
movq %r14, %rdi
leaq 0x7c031(%rip), %rsi     # 0x995dd
leaq 0x10(%rsp), %rdx
leaq 0x8(%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 0xe870
addq $0x20, %rsp
cmpl $0x8, %eax
jne 0x1d7b8
movq 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
movq %r15, %rdi
callq 0x37b4c
movsd 0x58(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
leaq 0x30(%rsp), %rdi
callq 0x3830c
movsd 0x30(%rsp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq (%rbx), %rax
jne 0x1d664
jp 0x1d664
movsd 0x38(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x1d664
jp 0x1d664
movsd 0x40(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x1d664
jp 0x1d664
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
jmp 0x1d69d
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
jmp 0x1d69d
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x22bfc
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x22bfc
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x22bfc
incl 0x4(%rsp)
jmp 0x1d4b5
leaq 0x75cbd(%rip), %rdi     # 0x9336a
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
jmp 0x1d7a4
xorl %ebp, %ebp
movslq 0x24(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %rbp
jg 0x1d774
movq 0x18(%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 0x1d76b
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 0x1d76b
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
jmp 0x1d6c0
movq %r14, %rdi
callq 0xe440
pushq $0x1
popq %rbx
jmp 0x1d79a
leaq 0x75da2(%rip), %rdi     # 0x9352a
xorl %ebx, %ebx
movl 0x4(%rsp), %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
leaq 0x18(%rsp), %rdi
callq 0x66378
movl %ebx, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x75d2c(%rip), %rdi     # 0x934eb
xorl %ebx, %ebx
movl 0x4(%rsp), %esi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x1d79a
jmp 0x1d7e9
jmp 0x1d7e9
jmp 0x1d7e9
jmp 0x1d7e9
jmp 0x1d7e9
jmp 0x1d7e9
jmp 0x1d7e9
jmp 0x1d7e9
jmp 0x1d7e9
jmp 0x1d7e9
jmp 0x1d7e9
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x66378
movq %rbx, %rdi
callq 0xe810