// File: 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:
subq $0x268, %rsp            # imm = 0x268
movq %rdi, 0x258(%rsp)
movq %rsi, 0x250(%rsp)
movq 0x258(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x250(%rsp), %rdi
leaq 0x10a1dd(%rip), %rsi    # 0x138673
callq 0xf5e0
movq %rax, 0x248(%rsp)
cmpq $0x0, %rax
jne 0x2e4cf
movq 0x250(%rsp), %rsi
leaq 0x104f28(%rip), %rdi    # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x264(%rsp)
jmp 0x2ebc2
movl $0x1, 0x1bc(%rsp)
leaq 0x1a8(%rsp), %rdi
callq 0x36800
movq 0x248(%rsp), %rdi
leaq 0x1064f8(%rip), %rsi    # 0x1349ee
xorl %eax, %eax
leaq 0x1c0(%rsp), %rdx
callq 0xf910
movl %eax, 0xac(%rsp)
jmp 0x2e50e
movl 0xac(%rsp), %eax
cmpl $0x1, %eax
jne 0x2e92b
leaq 0x1c0(%rsp), %rdi
leaq 0x105034(%rip), %rsi    # 0x133561
callq 0xf700
cmpl $0x0, %eax
jne 0x2e6db
movq 0x248(%rsp), %rdi
movq %rsp, %rax
leaq 0x158(%rsp), %rcx
movq %rcx, 0x18(%rax)
leaq 0x160(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x168(%rsp), %rcx
movq %rcx, 0x8(%rax)
leaq 0x170(%rsp), %rcx
movq %rcx, (%rax)
leaq 0x106bef(%rip), %rsi    # 0x13516b
xorl %eax, %eax
leaq 0x190(%rsp), %rdx
leaq 0x188(%rsp), %rcx
leaq 0x180(%rsp), %r8
leaq 0x178(%rsp), %r9
callq 0xf910
movl %eax, 0xa8(%rsp)
jmp 0x2e5ac
movl 0xa8(%rsp), %eax
cmpl $0x8, %eax
je 0x2e618
movl 0x1bc(%rsp), %esi
movq 0x250(%rsp), %rdx
leaq 0x104f9b(%rip), %rdi    # 0x133569
xorl %eax, %eax
callq 0xd4f30
jmp 0x2e5d7
movl $0x0, 0x264(%rsp)
movl $0x1, 0x154(%rsp)
jmp 0x2ebb5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
leaq 0x1a8(%rsp), %rdi
callq 0x36850
jmp 0x2ebd1
movsd 0x190(%rsp), %xmm0
movsd 0x188(%rsp), %xmm1
movsd 0x180(%rsp), %xmm2
leaq 0x138(%rsp), %rdi
callq 0x6eb50
jmp 0x2e642
movsd 0x178(%rsp), %xmm0
movsd 0x170(%rsp), %xmm1
movsd 0x168(%rsp), %xmm2
leaq 0x120(%rsp), %rdi
callq 0x6fcb0
jmp 0x2e66c
leaq 0x120(%rsp), %rdi
callq 0x375a0
movl %eax, 0xa4(%rsp)
jmp 0x2e682
movl 0xa4(%rsp), %eax
cmpl $0x0, %eax
je 0x2e6af
movq 0xb0(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
leaq 0x138(%rsp), %rsi
callq *%rax
jmp 0x2e6ad
jmp 0x2e6d6
movq 0xb0(%rsp), %rdi
movq (%rdi), %rax
movq 0x28(%rax), %rax
xorl %ecx, %ecx
leaq 0x138(%rsp), %rsi
leaq 0x120(%rsp), %rdx
callq *%rax
jmp 0x2e6d4
jmp 0x2e6d6
jmp 0x2e915
leaq 0x1c0(%rsp), %rdi
leaq 0x104eae(%rip), %rsi    # 0x133598
callq 0xf700
cmpl $0x0, %eax
jne 0x2e913
movq 0x248(%rsp), %rdi
leaq 0x10501b(%rip), %rsi    # 0x133722
xorl %eax, %eax
leaq 0x11c(%rsp), %rdx
leaq 0x118(%rsp), %rcx
leaq 0x114(%rsp), %r8
leaq 0x110(%rsp), %r9
callq 0xf910
movl %eax, 0xa0(%rsp)
jmp 0x2e737
movl 0xa0(%rsp), %eax
cmpl $0x4, %eax
je 0x2e77d
movl 0x1bc(%rsp), %esi
movq 0x250(%rsp), %rdx
leaq 0x104e4f(%rip), %rdi    # 0x1335a8
xorl %eax, %eax
callq 0xd4f30
jmp 0x2e762
movl $0x0, 0x264(%rsp)
movl $0x1, 0x154(%rsp)
jmp 0x2ebb5
movq 0xb0(%rsp), %rdi
addq $0x8, %rdi
movl 0x118(%rsp), %esi
callq 0x377e0
movq %rax, 0x98(%rsp)
jmp 0x2e79f
movq 0xb0(%rsp), %rdi
movq 0x98(%rsp), %rax
movq %rax, 0x108(%rsp)
addq $0x8, %rdi
movl 0x114(%rsp), %esi
callq 0x377e0
movq %rax, 0x90(%rsp)
jmp 0x2e7d1
movq 0xb0(%rsp), %rdi
movq 0x90(%rsp), %rax
movq %rax, 0x100(%rsp)
addq $0x8, %rdi
movl 0x110(%rsp), %esi
callq 0x377e0
movq %rax, 0x88(%rsp)
jmp 0x2e803
movq 0x88(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0x108(%rsp), %rax
cmpq 0x100(%rsp), %rax
je 0x2e849
movq 0x100(%rsp), %rax
cmpq 0xf8(%rsp), %rax
je 0x2e849
movq 0x108(%rsp), %rax
cmpq 0xf8(%rsp), %rax
jne 0x2e84e
jmp 0x2e4e7
movq 0xb0(%rsp), %rdi
movq 0x108(%rsp), %rsi
movq 0x100(%rsp), %rdx
movq 0xf8(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x80(%rsp)
jmp 0x2e884
movq 0x80(%rsp), %rax
movq %rax, 0xf0(%rsp)
cmpq $0x0, 0xf0(%rsp)
jne 0x2e8e6
movq 0x108(%rsp), %rsi
leaq 0x1a8(%rsp), %rdi
callq 0x37410
jmp 0x2e8b6
movq 0x100(%rsp), %rsi
leaq 0x1a8(%rsp), %rdi
callq 0x37410
jmp 0x2e8cd
movq 0xf8(%rsp), %rsi
leaq 0x1a8(%rsp), %rdi
callq 0x37410
jmp 0x2e8e4
jmp 0x2e8e6
cmpq $0x0, 0xf0(%rsp)
je 0x2e911
movq 0xb0(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movl 0x11c(%rsp), %edx
callq 0x369c0
jmp 0x2e90f
jmp 0x2e911
jmp 0x2e913
jmp 0x2e915
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x2e4e7
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movl %eax, 0x78(%rsp)
leaq 0x1a8(%rsp), %rdi
callq 0x13600
movl %eax, 0x7c(%rsp)
jmp 0x2e954
movl 0x78(%rsp), %eax
movl 0x7c(%rsp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jg 0x2eb90
movl 0xec(%rsp), %esi
leaq 0x1a8(%rsp), %rdi
callq 0x377e0
movq %rax, 0x70(%rsp)
jmp 0x2e982
movq 0x70(%rsp), %rax
movq %rax, 0xe0(%rsp)
movl 0xec(%rsp), %esi
incl %esi
leaq 0x1a8(%rsp), %rdi
callq 0x377e0
movq %rax, 0x68(%rsp)
jmp 0x2e9ac
movq 0x68(%rsp), %rax
movq %rax, 0xd8(%rsp)
movl 0xec(%rsp), %esi
addl $0x2, %esi
leaq 0x1a8(%rsp), %rdi
callq 0x377e0
movq %rax, 0x60(%rsp)
jmp 0x2e9d7
movq 0xb0(%rsp), %rdi
movq 0x60(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0xe0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
movq 0xd0(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x58(%rsp)
jmp 0x2ea17
movq 0x58(%rsp), %rax
cmpq $0x0, %rax
jne 0x2eb78
movq 0xb0(%rsp), %rdi
movq 0xe0(%rsp), %rsi
movq 0xd0(%rsp), %rdx
movq 0xd8(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x50(%rsp)
jmp 0x2ea59
movq 0x50(%rsp), %rax
cmpq $0x0, %rax
jne 0x2eb76
movq 0xb0(%rsp), %rdi
movq 0xe0(%rsp), %rsi
callq 0x13660
movq %rax, 0x48(%rsp)
jmp 0x2ea84
movq 0x48(%rsp), %rsi
movq 0xb0(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x40(%rsp)
jmp 0x2eaa5
movq 0xb0(%rsp), %rdi
movq 0x40(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0xd8(%rsp), %rsi
callq 0x13660
movq %rax, 0x38(%rsp)
jmp 0x2eace
movq 0x38(%rsp), %rsi
movq 0xb0(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x30(%rsp)
jmp 0x2eaef
movq 0xb0(%rsp), %rdi
movq 0x30(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xd0(%rsp), %rsi
callq 0x13660
movq %rax, 0x28(%rsp)
jmp 0x2eb18
movq 0x28(%rsp), %rsi
movq 0xb0(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x20(%rsp)
jmp 0x2eb39
movq 0xb0(%rsp), %rdi
movq 0x20(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xc8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
movq 0xb8(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
jmp 0x2eb74
jmp 0x2eb76
jmp 0x2eb78
jmp 0x2eb7a
movl 0xec(%rsp), %eax
addl $0x3, %eax
movl %eax, 0xec(%rsp)
jmp 0x2e936
movq 0x248(%rsp), %rdi
callq 0xf4b0
jmp 0x2eb9f
movl $0x1, 0x264(%rsp)
movl $0x1, 0x154(%rsp)
leaq 0x1a8(%rsp), %rdi
callq 0x36850
movl 0x264(%rsp), %eax
addq $0x268, %rsp            # imm = 0x268
retq
movq 0x1a0(%rsp), %rdi
callq 0xf890
nop