// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::ReadHoppeFile(char const*)
int R3Mesh::
ReadHoppeFile(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 line_count = 0;
  char buffer[1024];
  RNArray<R3MeshVertex *> vertices;
  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;

    // Read command
    char cmd[1024];
    if (sscanf(buffer, "%s", cmd) != (unsigned int) 1) {
      fprintf(fp, "Syntax error line %d in file %s\n", line_count, filename);
      fclose(fp);
      return 0;
    }

    // Check command
    if (!strcmp(cmd, "Vertex")) {
      // Read vertex info
      int index;
      double x, y, z;
      if (sscanf(bufferp, "%s%d%lf%lf%lf", cmd, &index, &x, &y, &z) != (unsigned int) 5) {
        RNFail("Syntax error with vertex coordinates on line %d in file %s\n", line_count, filename);
        fclose(fp);
        return 0;
      }

      // Create vertex
      R3MeshVertex *vertex = CreateVertex(R3Point(x, y, z));
      vertices.Insert(vertex);
    }
    else if (!strcmp(cmd, "Face")) {
      // Read face info
      int index, i0, i1, i2;
      if (sscanf(bufferp, "%s%d%d%d%d", cmd, &index, &i0, &i1, &i2) != (unsigned int) 5) {
        RNFail("Syntax error with face info on line %d in file %s\n", line_count, filename);
        fclose(fp);
        return 0;
      }

      // Find vertices
      assert((i0 >= 0) && (i0 < vertices.NEntries()));
      assert((i1 >= 0) && (i1 < vertices.NEntries()));
      assert((i2 >= 0) && (i2 < vertices.NEntries()));
      R3MeshVertex *v0 = vertices.Kth(i0-1);
      R3MeshVertex *v1 = vertices.Kth(i1-1);
      R3MeshVertex *v2 = vertices.Kth(i2-1);

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

  // 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 $0x9a8, %rsp            # imm = 0x9A8
movq %rdi, 0x998(%rsp)
movq %rsi, 0x990(%rsp)
movq 0x998(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x990(%rsp), %rdi
leaq 0x10723d(%rip), %rsi    # 0x138673
callq 0xf5e0
movq %rax, 0x988(%rsp)
cmpq $0x0, %rax
jne 0x3146f
movq 0x990(%rsp), %rsi
leaq 0x102013(%rip), %rdi    # 0x13346b
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x9a4(%rsp)
jmp 0x31bd7
movl $0x0, 0x984(%rsp)
leaq 0x570(%rsp), %rdi
callq 0x36800
leaq 0x560(%rsp), %rdi
callq 0x36800
jmp 0x31496
jmp 0x31498
movq 0x988(%rsp), %rdx
leaq 0x580(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
callq 0xf7e0
movq %rax, 0x90(%rsp)
jmp 0x314bc
movq 0x90(%rsp), %rax
cmpq $0x0, %rax
je 0x31922
movl 0x984(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x984(%rsp)
leaq 0x580(%rsp), %rax
movq %rax, 0x548(%rsp)
movq 0x548(%rsp), %rax
movsbl (%rax), %edi
callq 0xf080
cmpl $0x0, %eax
je 0x31559
movq 0x548(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x548(%rsp)
jmp 0x314ef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x558(%rsp)
movl %eax, 0x554(%rsp)
jmp 0x31bc8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x558(%rsp)
movl %eax, 0x554(%rsp)
leaq 0x560(%rsp), %rdi
callq 0x36850
jmp 0x31bc8
movq 0x548(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x3156e
jmp 0x31498
movq 0x548(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x31583
jmp 0x31498
leaq 0x580(%rsp), %rdi
leaq 0x140(%rsp), %rdx
leaq 0x103454(%rip), %rsi    # 0x1349ee
movb $0x0, %al
callq 0xf4a0
cmpl $0x1, %eax
je 0x315f5
movq 0x988(%rsp), %rdi
movl 0x984(%rsp), %edx
movq 0x990(%rsp), %rcx
leaq 0x102121(%rip), %rsi    # 0x1336e5
xorl %eax, %eax
callq 0xf730
movq 0x988(%rsp), %rdi
callq 0xf4b0
jmp 0x315da
movl $0x0, 0x9a4(%rsp)
movl $0x1, 0x13c(%rsp)
jmp 0x31bac
leaq 0x140(%rsp), %rdi
leaq 0x102102(%rip), %rsi    # 0x133706
callq 0xf700
cmpl $0x0, %eax
jne 0x3171f
movq 0x548(%rsp), %rdi
leaq 0x140(%rsp), %rdx
leaq 0x1020e4(%rip), %rsi    # 0x13370d
leaq 0x138(%rsp), %rcx
leaq 0x130(%rsp), %r8
leaq 0x128(%rsp), %r9
leaq 0x120(%rsp), %rax
movq %rax, (%rsp)
movb $0x0, %al
callq 0xf4a0
cmpl $0x5, %eax
je 0x316a2
movl 0x984(%rsp), %esi
movq 0x990(%rsp), %rdx
leaq 0x101e54(%rip), %rdi    # 0x1334c3
xorl %eax, %eax
callq 0xd4f30
jmp 0x31678
movq 0x988(%rsp), %rdi
callq 0xf4b0
jmp 0x31687
movl $0x0, 0x9a4(%rsp)
movl $0x1, 0x13c(%rsp)
jmp 0x31bac
movsd 0x130(%rsp), %xmm0
movsd 0x128(%rsp), %xmm1
movsd 0x120(%rsp), %xmm2
leaq 0x100(%rsp), %rdi
callq 0x6eb50
jmp 0x316cc
movq 0x98(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
leaq 0x100(%rsp), %rsi
callq *%rax
movq %rax, 0x88(%rsp)
jmp 0x316f3
movq 0x88(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rsi
leaq 0x570(%rsp), %rdi
callq 0x37410
jmp 0x3171a
jmp 0x3191d
leaq 0x140(%rsp), %rdi
leaq 0x101fed(%rip), %rsi    # 0x13371b
callq 0xf700
cmpl $0x0, %eax
jne 0x3191b
movq 0x548(%rsp), %rdi
leaq 0x140(%rsp), %rdx
leaq 0x101fcd(%rip), %rsi    # 0x133720
leaq 0xfc(%rsp), %rcx
leaq 0xf8(%rsp), %r8
leaq 0xf4(%rsp), %r9
leaq 0xf0(%rsp), %rax
movq %rax, (%rsp)
movb $0x0, %al
callq 0xf4a0
cmpl $0x5, %eax
je 0x317cc
movl 0x984(%rsp), %esi
movq 0x990(%rsp), %rdx
leaq 0x101f92(%rip), %rdi    # 0x13372b
xorl %eax, %eax
callq 0xd4f30
jmp 0x317a2
movq 0x988(%rsp), %rdi
callq 0xf4b0
jmp 0x317b1
movl $0x0, 0x9a4(%rsp)
movl $0x1, 0x13c(%rsp)
jmp 0x31bac
movl 0xf8(%rsp), %esi
decl %esi
leaq 0x570(%rsp), %rdi
callq 0x377e0
movq %rax, 0x80(%rsp)
jmp 0x317ec
movq 0x80(%rsp), %rax
movq %rax, 0xe8(%rsp)
movl 0xf4(%rsp), %esi
decl %esi
leaq 0x570(%rsp), %rdi
callq 0x377e0
movq %rax, 0x78(%rsp)
jmp 0x31819
movq 0x78(%rsp), %rax
movq %rax, 0xe0(%rsp)
movl 0xf0(%rsp), %esi
decl %esi
leaq 0x570(%rsp), %rdi
callq 0x377e0
movq %rax, 0x70(%rsp)
jmp 0x31843
movq 0x70(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0xe8(%rsp), %rax
cmpq 0xe0(%rsp), %rax
je 0x31919
movq 0xe8(%rsp), %rax
cmpq 0xd8(%rsp), %rax
je 0x31919
movq 0xe0(%rsp), %rax
cmpq 0xd8(%rsp), %rax
je 0x31919
movq 0x98(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq 0xe0(%rsp), %rdx
movq 0xd8(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x68(%rsp)
jmp 0x318c5
movq 0x68(%rsp), %rax
cmpq $0x0, %rax
jne 0x31917
movq 0xe8(%rsp), %rsi
leaq 0x560(%rsp), %rdi
callq 0x37410
jmp 0x318e7
movq 0xe0(%rsp), %rsi
leaq 0x560(%rsp), %rdi
callq 0x37410
jmp 0x318fe
movq 0xd8(%rsp), %rsi
leaq 0x560(%rsp), %rdi
callq 0x37410
jmp 0x31915
jmp 0x31917
jmp 0x31919
jmp 0x3191b
jmp 0x3191d
jmp 0x31498
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x60(%rsp)
leaq 0x560(%rsp), %rdi
callq 0x13600
movl %eax, 0x64(%rsp)
jmp 0x3194b
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jg 0x31b87
movl 0xd4(%rsp), %esi
leaq 0x560(%rsp), %rdi
callq 0x377e0
movq %rax, 0x58(%rsp)
jmp 0x31979
movq 0x58(%rsp), %rax
movq %rax, 0xc8(%rsp)
movl 0xd4(%rsp), %esi
incl %esi
leaq 0x560(%rsp), %rdi
callq 0x377e0
movq %rax, 0x50(%rsp)
jmp 0x319a3
movq 0x50(%rsp), %rax
movq %rax, 0xc0(%rsp)
movl 0xd4(%rsp), %esi
addl $0x2, %esi
leaq 0x560(%rsp), %rdi
callq 0x377e0
movq %rax, 0x48(%rsp)
jmp 0x319ce
movq 0x98(%rsp), %rdi
movq 0x48(%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
movq %rax, 0x40(%rsp)
jmp 0x31a0e
movq 0x40(%rsp), %rax
cmpq $0x0, %rax
jne 0x31b6f
movq 0x98(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movq 0xc0(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x38(%rsp)
jmp 0x31a50
movq 0x38(%rsp), %rax
cmpq $0x0, %rax
jne 0x31b6d
movq 0x98(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0x13660
movq %rax, 0x30(%rsp)
jmp 0x31a7b
movq 0x30(%rsp), %rsi
movq 0x98(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x28(%rsp)
jmp 0x31a9c
movq 0x98(%rsp), %rdi
movq 0x28(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xc0(%rsp), %rsi
callq 0x13660
movq %rax, 0x20(%rsp)
jmp 0x31ac5
movq 0x20(%rsp), %rsi
movq 0x98(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x18(%rsp)
jmp 0x31ae6
movq 0x98(%rsp), %rdi
movq 0x18(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xb8(%rsp), %rsi
callq 0x13660
movq %rax, 0x10(%rsp)
jmp 0x31b0f
movq 0x10(%rsp), %rsi
movq 0x98(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x8(%rsp)
jmp 0x31b30
movq 0x98(%rsp), %rdi
movq 0x8(%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
jmp 0x31b6b
jmp 0x31b6d
jmp 0x31b6f
jmp 0x31b71
movl 0xd4(%rsp), %eax
addl $0x3, %eax
movl %eax, 0xd4(%rsp)
jmp 0x3192d
movq 0x988(%rsp), %rdi
callq 0xf4b0
jmp 0x31b96
movl $0x1, 0x9a4(%rsp)
movl $0x1, 0x13c(%rsp)
leaq 0x560(%rsp), %rdi
callq 0x36850
leaq 0x570(%rsp), %rdi
callq 0x36850
jmp 0x31bd7
leaq 0x570(%rsp), %rdi
callq 0x36850
jmp 0x31be6
movl 0x9a4(%rsp), %eax
addq $0x9a8, %rsp            # imm = 0x9A8
retq
movq 0x558(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)