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

  // Read file
  int nverts = 0;
  int nfaces = 0;
  int nedges = 0;
  int line_count = 0;
  int vertex_count = 0;
  int face_count = 0;
  char buffer[1024];
  char header[64];
  RNArray<R3MeshVertex *> degenerate_triangle_vertices;
  while (fgets(buffer, 1023, fp)) {
    // Increment line counter
    line_count++;

    // Skip white space
    char *bufferp = buffer;
    while (isspace(*bufferp)) bufferp++;

    // Skip blank lines and comments
    if (*bufferp == '#') continue;
    if (*bufferp == '\0') continue;

    // Check section
    if (nverts == 0) {
      // Read header keyword
      if (strstr(bufferp, "OFF")) {
        // Check if counts are on first line
        int tmp;
        if (sscanf(bufferp, "%s%d%d%d", header, &tmp, &nfaces, &nedges) == 4) {
          nverts = tmp;
        }
      }
      else {
        // Read counts from second line
        if ((sscanf(bufferp, "%d%d%d", &nverts, &nfaces, &nedges) != 3) || (nverts == 0)) {
          RNFail("Syntax error reading header on line %d in file %s\n", line_count, filename);
          fclose(fp);
          return 0;
        }
      }
    }
    else if (vertex_count < nverts) {
      // Read vertex coordinates
      double x, y, z;
      if (sscanf(bufferp, "%lf%lf%lf", &x, &y, &z) != 3) {
        RNFail("Syntax error with vertex coordinates on line %d in file %s\n", line_count, filename);
        fclose(fp);
        return 0;
      }

      // Create vertex
      CreateVertex(R3Point(x, y, z));

      // Increment counter
      vertex_count++;
    }
    else if (face_count < nfaces) {
      // Read number of vertices in face 
      int face_nverts = 0;
      bufferp = strtok(bufferp, " \t");
      if (bufferp) face_nverts = atoi(bufferp);
      else {
        RNFail("Syntax error with face on line %d in file %s\n", line_count, filename);
        fclose(fp);
        return 0;
      }

      // Read vertex indices for face
      R3MeshVertex *v1 = NULL;
      R3MeshVertex *v2 = NULL;
      R3MeshVertex *v3 = NULL;
      for (int i = 0; i < face_nverts; i++) {
        bufferp = strtok(NULL, " \t");
        if (bufferp) {
          R3MeshVertex *v = Vertex(atoi(bufferp));
          if (!v1) v1 = v;
          else v3 = v;
        }
        else {
          RNFail("Syntax error with face on line %d in file %s\n", line_count, filename);
          fclose(fp);
          return 0;
        }

        // Create triangle
        if (v1 && v2 && v3 && (v1 != v2) && (v2 != v3) && (v1 != v3)) {
          if (!CreateFace(v1, v2, v3)) {
            // 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);
          }
        }

        // Move to next triangle
        v2 = v3;
      }

      // Increment counter
      face_count++;
    }
    else {
      // Should never get here
      RNFail("Found extra text starting at line %d in file %s\n", line_count, filename);
      break;
    }
  }

  // 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 $0x5c8, %rsp            # imm = 0x5C8
movq %rdi, 0x5b8(%rsp)
movq %rsi, 0x5b0(%rsp)
movq 0x5b8(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x5b0(%rsp), %rdi
leaq 0x10ab1d(%rip), %rsi    # 0x138673
callq 0xf5e0
movq %rax, 0x5a8(%rsp)
cmpq $0x0, %rax
jne 0x2db8f
movq 0x5b0(%rsp), %rsi
leaq 0x1058f3(%rip), %rdi    # 0x13346b
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5c4(%rsp)
jmp 0x2e43f
movl $0x0, 0x5a4(%rsp)
movl $0x0, 0x5a0(%rsp)
movl $0x0, 0x59c(%rsp)
movl $0x0, 0x598(%rsp)
movl $0x0, 0x594(%rsp)
movl $0x0, 0x590(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x36800
movq 0x5a8(%rsp), %rdx
leaq 0x190(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
callq 0xf7e0
movq %rax, 0x78(%rsp)
jmp 0x2dbff
movq 0x78(%rsp), %rax
cmpq $0x0, %rax
je 0x2e1a8
movl 0x598(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x598(%rsp)
leaq 0x190(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x128(%rsp), %rax
movsbl (%rax), %edi
callq 0xf080
cmpl $0x0, %eax
je 0x2dc80
movq 0x128(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x128(%rsp)
jmp 0x2dc2f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x36850
jmp 0x2e44e
movq 0x128(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x2dc95
jmp 0x2dbde
movq 0x128(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x2dcaa
jmp 0x2dbde
cmpl $0x0, 0x5a4(%rsp)
jne 0x2ddad
movq 0x128(%rsp), %rdi
leaq 0x1057bc(%rip), %rsi    # 0x133483
callq 0xf110
cmpq $0x0, %rax
je 0x2dd20
movq 0x128(%rsp), %rdi
leaq 0x150(%rsp), %rdx
leaq 0x10579e(%rip), %rsi    # 0x133487
leaq 0x124(%rsp), %rcx
leaq 0x5a0(%rsp), %r8
leaq 0x59c(%rsp), %r9
movb $0x0, %al
callq 0xf4a0
cmpl $0x4, %eax
jne 0x2dd1b
movl 0x124(%rsp), %eax
movl %eax, 0x5a4(%rsp)
jmp 0x2dda8
movq 0x128(%rsp), %rdi
leaq 0x1059f5(%rip), %rsi    # 0x133724
leaq 0x5a4(%rsp), %rdx
leaq 0x5a0(%rsp), %rcx
leaq 0x59c(%rsp), %r8
movb $0x0, %al
callq 0xf4a0
cmpl $0x3, %eax
jne 0x2dd5d
cmpl $0x0, 0x5a4(%rsp)
jne 0x2dda6
movl 0x598(%rsp), %esi
movq 0x5b0(%rsp), %rdx
leaq 0x10571d(%rip), %rdi    # 0x133490
xorl %eax, %eax
callq 0xd4f30
jmp 0x2dd7c
movq 0x5a8(%rsp), %rdi
callq 0xf4b0
jmp 0x2dd8b
movl $0x0, 0x5c4(%rsp)
movl $0x1, 0x120(%rsp)
jmp 0x2e432
jmp 0x2dda8
jmp 0x2e1a3
movl 0x594(%rsp), %eax
cmpl 0x5a4(%rsp), %eax
jge 0x2de9c
movq 0x128(%rsp), %rdi
leaq 0x105941(%rip), %rsi    # 0x133711
leaq 0x118(%rsp), %rdx
leaq 0x110(%rsp), %rcx
leaq 0x108(%rsp), %r8
movb $0x0, %al
callq 0xf4a0
cmpl $0x3, %eax
je 0x2de3d
movl 0x598(%rsp), %esi
movq 0x5b0(%rsp), %rdx
leaq 0x1056b9(%rip), %rdi    # 0x1334c3
xorl %eax, %eax
callq 0xd4f30
jmp 0x2de13
movq 0x5a8(%rsp), %rdi
callq 0xf4b0
jmp 0x2de22
movl $0x0, 0x5c4(%rsp)
movl $0x1, 0x120(%rsp)
jmp 0x2e432
movsd 0x118(%rsp), %xmm0
movsd 0x110(%rsp), %xmm1
movsd 0x108(%rsp), %xmm2
leaq 0xf0(%rsp), %rdi
callq 0x6eb50
jmp 0x2de67
movq 0x80(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
leaq 0xf0(%rsp), %rsi
callq *%rax
jmp 0x2de86
movl 0x594(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x594(%rsp)
jmp 0x2e1a1
movl 0x590(%rsp), %eax
cmpl 0x5a0(%rsp), %eax
jge 0x2e17e
movl $0x0, 0xec(%rsp)
movq 0x128(%rsp), %rdi
leaq 0x105635(%rip), %rsi    # 0x1334ff
callq 0xf7c0
movq %rax, 0x128(%rsp)
cmpq $0x0, 0x128(%rsp)
je 0x2def8
movq 0x128(%rsp), %rdi
callq 0xf7b0
movl %eax, 0xec(%rsp)
jmp 0x2df41
movl 0x598(%rsp), %esi
movq 0x5b0(%rsp), %rdx
leaq 0x1055f4(%rip), %rdi    # 0x133502
xorl %eax, %eax
callq 0xd4f30
jmp 0x2df17
movq 0x5a8(%rsp), %rdi
callq 0xf4b0
jmp 0x2df26
movl $0x0, 0x5c4(%rsp)
movl $0x1, 0x120(%rsp)
jmp 0x2e432
movq $0x0, 0xe0(%rsp)
movq $0x0, 0xd8(%rsp)
movq $0x0, 0xd0(%rsp)
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0xec(%rsp), %eax
jge 0x2e16b
xorl %eax, %eax
movl %eax, %edi
leaq 0x105570(%rip), %rsi    # 0x1334ff
callq 0xf7c0
movq %rax, 0x128(%rsp)
cmpq $0x0, 0x128(%rsp)
je 0x2e006
movq 0x128(%rsp), %rdi
callq 0xf7b0
movq 0x80(%rsp), %rdi
movl %eax, %esi
callq 0x13630
movq %rax, 0x70(%rsp)
jmp 0x2dfca
movq 0x70(%rsp), %rax
movq %rax, 0xc0(%rsp)
cmpq $0x0, 0xe0(%rsp)
jne 0x2dff4
movq 0xc0(%rsp), %rax
movq %rax, 0xe0(%rsp)
jmp 0x2e004
movq 0xc0(%rsp), %rax
movq %rax, 0xd0(%rsp)
jmp 0x2e04f
movl 0x598(%rsp), %esi
movq 0x5b0(%rsp), %rdx
leaq 0x1054e6(%rip), %rdi    # 0x133502
xorl %eax, %eax
callq 0xd4f30
jmp 0x2e025
movq 0x5a8(%rsp), %rdi
callq 0xf4b0
jmp 0x2e034
movl $0x0, 0x5c4(%rsp)
movl $0x1, 0x120(%rsp)
jmp 0x2e432
cmpq $0x0, 0xe0(%rsp)
je 0x2e145
cmpq $0x0, 0xd8(%rsp)
je 0x2e145
cmpq $0x0, 0xd0(%rsp)
je 0x2e145
movq 0xe0(%rsp), %rax
cmpq 0xd8(%rsp), %rax
je 0x2e145
movq 0xd8(%rsp), %rax
cmpq 0xd0(%rsp), %rax
je 0x2e145
movq 0xe0(%rsp), %rax
cmpq 0xd0(%rsp), %rax
je 0x2e145
movq 0x80(%rsp), %rdi
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, 0x68(%rsp)
jmp 0x2e0f1
movq 0x68(%rsp), %rax
cmpq $0x0, %rax
jne 0x2e143
movq 0xe0(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0x37410
jmp 0x2e113
movq 0xd8(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0x37410
jmp 0x2e12a
movq 0xd0(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0x37410
jmp 0x2e141
jmp 0x2e143
jmp 0x2e145
movq 0xd0(%rsp), %rax
movq %rax, 0xd8(%rsp)
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x2df70
movl 0x590(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x590(%rsp)
jmp 0x2e19f
movl 0x598(%rsp), %esi
movq 0x5b0(%rsp), %rdx
leaq 0x10539c(%rip), %rdi    # 0x133530
xorl %eax, %eax
callq 0xd4f30
jmp 0x2e19d
jmp 0x2e1a8
jmp 0x2e1a1
jmp 0x2e1a3
jmp 0x2dbde
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x60(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x13600
movl %eax, 0x64(%rsp)
jmp 0x2e1d1
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jg 0x2e40d
movl 0xbc(%rsp), %esi
leaq 0x140(%rsp), %rdi
callq 0x377e0
movq %rax, 0x58(%rsp)
jmp 0x2e1ff
movq 0x58(%rsp), %rax
movq %rax, 0xb0(%rsp)
movl 0xbc(%rsp), %esi
incl %esi
leaq 0x140(%rsp), %rdi
callq 0x377e0
movq %rax, 0x50(%rsp)
jmp 0x2e229
movq 0x50(%rsp), %rax
movq %rax, 0xa8(%rsp)
movl 0xbc(%rsp), %esi
addl $0x2, %esi
leaq 0x140(%rsp), %rdi
callq 0x377e0
movq %rax, 0x48(%rsp)
jmp 0x2e254
movq 0x80(%rsp), %rdi
movq 0x48(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x40(%rsp)
jmp 0x2e294
movq 0x40(%rsp), %rax
cmpq $0x0, %rax
jne 0x2e3f5
movq 0x80(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movq 0xa8(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x38(%rsp)
jmp 0x2e2d6
movq 0x38(%rsp), %rax
cmpq $0x0, %rax
jne 0x2e3f3
movq 0x80(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x13660
movq %rax, 0x30(%rsp)
jmp 0x2e301
movq 0x30(%rsp), %rsi
movq 0x80(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x28(%rsp)
jmp 0x2e322
movq 0x80(%rsp), %rdi
movq 0x28(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0xa8(%rsp), %rsi
callq 0x13660
movq %rax, 0x20(%rsp)
jmp 0x2e34b
movq 0x20(%rsp), %rsi
movq 0x80(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x18(%rsp)
jmp 0x2e36c
movq 0x80(%rsp), %rdi
movq 0x18(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0xa0(%rsp), %rsi
callq 0x13660
movq %rax, 0x10(%rsp)
jmp 0x2e395
movq 0x10(%rsp), %rsi
movq 0x80(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x8(%rsp)
jmp 0x2e3b6
movq 0x80(%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x98(%rsp), %rsi
movq 0x90(%rsp), %rdx
movq 0x88(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
jmp 0x2e3f1
jmp 0x2e3f3
jmp 0x2e3f5
jmp 0x2e3f7
movl 0xbc(%rsp), %eax
addl $0x3, %eax
movl %eax, 0xbc(%rsp)
jmp 0x2e1b3
movq 0x5a8(%rsp), %rdi
callq 0xf4b0
jmp 0x2e41c
movl $0x1, 0x5c4(%rsp)
movl $0x1, 0x120(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x36850
movl 0x5c4(%rsp), %eax
addq $0x5c8, %rsp            # imm = 0x5C8
retq
movq 0x138(%rsp), %rdi
callq 0xf890
nopl (%rax,%rax)