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

  // Read file
  char buffer[1024];
  RNBoolean coordinate3_active = FALSE;
  RNBoolean coordinate3_point_active = FALSE;
  RNBoolean indexedfaceset_active = FALSE;
  RNBoolean indexedfaceset_coordindex_active = FALSE;
  RNArray<R3MeshVertex *> vertices;
  RNArray<R3MeshVertex *> degenerate_triangle_vertices;
  while (fgets(buffer, 1023, fp)) {
    // Skip white space
    char *bufferp = buffer;
    while (isspace(*bufferp)) bufferp++;

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

    // Get first token
    bufferp = strtok(bufferp, " ,");
    if (!bufferp) break;

    // Check node being parsed
    if (coordinate3_point_active) {
      if (*bufferp == ']') {
        coordinate3_point_active = FALSE;
      } 
      else {
        do {
          if (*bufferp && !isspace(*bufferp)) {
            // Read next coordinate
            static int coordinate_index = 0;
            static RNScalar coordinates[3];
            coordinates[coordinate_index] = atof(bufferp);
            coordinate_index++;

            // Create vertex if have three coordinates
            if (coordinate_index == 3) {
              R3Point position(coordinates[0], coordinates[1], coordinates[2]);
              R3MeshVertex *vertex = CreateVertex(position);
              vertices.Insert(vertex);
              coordinate_index = 0;
            }
          }
          bufferp = strtok(NULL, " ,");
        } while (bufferp);
      }
    }
    else if (indexedfaceset_coordindex_active) {
      if (*bufferp == ']') {
        indexedfaceset_active = FALSE;
        indexedfaceset_coordindex_active = FALSE;
      } 
      else {
        do {
          // Read next coordinate index
          if (*bufferp && !isspace(*bufferp)) {
            static RNArray<R3MeshVertex *> face_vertices;
            int vertex_index = atoi(bufferp);
            if (vertex_index >= 0) {
              R3MeshVertex *vertex = vertices[vertex_index];
              face_vertices.Insert(vertex);
            }
            else {
              // Create face(s)
              R3MeshVertex *v1 = face_vertices[0];
              for (int k = 2; k < face_vertices.NEntries(); k++) {
                // Get vertices
                R3MeshVertex *v2 = face_vertices[k-1];
                R3MeshVertex *v3 = face_vertices[k];

                // Check vertices
                if ((v1 == v2) || (v2 == v3) || (v1 == v3)) continue;
 
                // Create face
                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);
                }
              }
              face_vertices.Empty();
            }
          }
          bufferp = strtok(NULL, " ,");
        } while (bufferp);
      }
    }
    else {
      if (!strcmp(bufferp, "Coordinate3")) coordinate3_active = TRUE;
      else if (!strcmp(bufferp, "IndexedFaceSet")) indexedfaceset_active = TRUE;
      else if ((coordinate3_active) && !strcmp(bufferp, "point")) coordinate3_point_active = TRUE;
      else if ((indexedfaceset_active) && !strcmp(bufferp, "coordIndex")) indexedfaceset_coordindex_active = TRUE;
    }
  }

  // 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 $0x5a8, %rsp            # imm = 0x5A8
movq %rdi, 0x598(%rsp)
movq %rsi, 0x590(%rsp)
movq 0x598(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x590(%rsp), %rdi
leaq 0x10549d(%rip), %rsi    # 0x138673
callq 0xf5e0
movq %rax, 0x588(%rsp)
cmpq $0x0, %rax
jne 0x3320f
movq 0x590(%rsp), %rsi
leaq 0x100273(%rip), %rdi    # 0x13346b
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x5a4(%rsp)
jmp 0x33b27
movl $0x0, 0x17c(%rsp)
movl $0x0, 0x178(%rsp)
movl $0x0, 0x174(%rsp)
movl $0x0, 0x170(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x36800
leaq 0x150(%rsp), %rdi
callq 0x36800
jmp 0x33257
jmp 0x33259
movq 0x588(%rsp), %rdx
leaq 0x180(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
callq 0xf7e0
movq %rax, 0xa0(%rsp)
jmp 0x3327d
movq 0xa0(%rsp), %rax
cmpq $0x0, %rax
je 0x33870
leaq 0x180(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rax
movsbl (%rax), %edi
callq 0xf080
cmpl $0x0, %eax
je 0x332fc
movq 0x138(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x138(%rsp)
jmp 0x3329f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x33b18
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x33b0b
movq 0x138(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x33311
jmp 0x33259
movq 0x138(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x33326
jmp 0x33259
movq 0x138(%rsp), %rdi
leaq 0x100636(%rip), %rsi    # 0x13396b
callq 0xf7c0
movq %rax, 0x138(%rsp)
cmpq $0x0, 0x138(%rsp)
jne 0x33352
jmp 0x33870
cmpl $0x0, 0x178(%rsp)
je 0x33493
movq 0x138(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x5d, %eax
jne 0x33380
movl $0x0, 0x178(%rsp)
jmp 0x3348e
jmp 0x33382
movq 0x138(%rsp), %rax
cmpb $0x0, (%rax)
je 0x33465
movq 0x138(%rsp), %rax
movsbl (%rax), %edi
callq 0xf080
cmpl $0x0, %eax
jne 0x33465
movq 0x138(%rsp), %rdi
callq 0xf640
movslq 0x145288(%rip), %rcx    # 0x178648
leaq 0x145289(%rip), %rax    # 0x178650
movsd %xmm0, (%rax,%rcx,8)
movl 0x145276(%rip), %eax    # 0x178648
addl $0x1, %eax
movl %eax, 0x14526d(%rip)    # 0x178648
cmpl $0x3, 0x145266(%rip)    # 0x178648
jne 0x33463
movsd 0x145264(%rip), %xmm0   # 0x178650
movsd 0x145264(%rip), %xmm1   # 0x178658
movsd 0x145264(%rip), %xmm2   # 0x178660
leaq 0x120(%rsp), %rdi
callq 0x6eb50
jmp 0x3340b
movq 0xa8(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
leaq 0x120(%rsp), %rsi
callq *%rax
movq %rax, 0x98(%rsp)
jmp 0x33432
movq 0x98(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rsi
leaq 0x160(%rsp), %rdi
callq 0x37410
jmp 0x33459
movl $0x0, 0x1451e5(%rip)    # 0x178648
jmp 0x33465
xorl %eax, %eax
movl %eax, %edi
leaq 0x1004fb(%rip), %rsi    # 0x13396b
callq 0xf7c0
movq %rax, 0x138(%rsp)
cmpq $0x0, 0x138(%rsp)
jne 0x33382
jmp 0x3348e
jmp 0x3386b
cmpl $0x0, 0x170(%rsp)
je 0x337b4
movq 0x138(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x5d, %eax
jne 0x334cc
movl $0x0, 0x174(%rsp)
movl $0x0, 0x170(%rsp)
jmp 0x337af
jmp 0x334ce
movq 0x138(%rsp), %rax
cmpb $0x0, (%rax)
je 0x33786
movq 0x138(%rsp), %rax
movsbl (%rax), %edi
callq 0xf080
cmpl $0x0, %eax
jne 0x33786
cmpb $0x0, 0x145179(%rip)    # 0x178678
jne 0x33546
leaq 0x145170(%rip), %rdi    # 0x178678
callq 0xf8e0
cmpl $0x0, %eax
je 0x33546
leaq 0x14514f(%rip), %rdi    # 0x178668
callq 0x36800
jmp 0x33520
leaq 0x3329(%rip), %rdi      # 0x36850
leaq 0x14513a(%rip), %rsi    # 0x178668
leaq 0x13bfc3(%rip), %rdx    # 0x16f4f8
callq 0xf420
leaq 0x145137(%rip), %rdi    # 0x178678
callq 0xf310
movq 0x138(%rsp), %rdi
callq 0xf7b0
movl %eax, 0x114(%rsp)
cmpl $0x0, 0x114(%rsp)
jl 0x335d5
movl 0x114(%rsp), %esi
leaq 0x160(%rsp), %rdi
callq 0x37800
movq %rax, 0x90(%rsp)
jmp 0x33582
movq 0x90(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rsi
leaq 0x1450c4(%rip), %rdi    # 0x178668
callq 0x37410
jmp 0x335ab
jmp 0x33784
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
leaq 0x1450ad(%rip), %rdi    # 0x178678
callq 0xf300
jmp 0x33b0b
leaq 0x14508c(%rip), %rdi    # 0x178668
xorl %esi, %esi
callq 0x37800
movq %rax, 0x88(%rsp)
jmp 0x335ed
movq 0x88(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x100(%rsp)
movl $0x2, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
movl %eax, 0x80(%rsp)
leaq 0x145048(%rip), %rdi    # 0x178668
callq 0x13600
movl %eax, 0x84(%rsp)
jmp 0x3362e
movl 0x80(%rsp), %eax
movl 0x84(%rsp), %ecx
cmpl %ecx, %eax
jge 0x33772
movl 0xfc(%rsp), %esi
decl %esi
leaq 0x145014(%rip), %rdi    # 0x178668
callq 0x37800
movq %rax, 0x78(%rsp)
jmp 0x33660
movq 0x78(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xf0(%rsp)
movl 0xfc(%rsp), %esi
leaq 0x144fea(%rip), %rdi    # 0x178668
callq 0x37800
movq %rax, 0x70(%rsp)
jmp 0x3368a
movq 0x70(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xe8(%rsp)
movq 0x100(%rsp), %rax
cmpq 0xf0(%rsp), %rax
je 0x336d0
movq 0xf0(%rsp), %rax
cmpq 0xe8(%rsp), %rax
je 0x336d0
movq 0x100(%rsp), %rax
cmpq 0xe8(%rsp), %rax
jne 0x336d5
jmp 0x3375c
movq 0xa8(%rsp), %rdi
movq 0x100(%rsp), %rsi
movq 0xf0(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x68(%rsp)
jmp 0x33708
movq 0x68(%rsp), %rax
cmpq $0x0, %rax
jne 0x3375a
movq 0x100(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0x37410
jmp 0x3372a
movq 0xf0(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0x37410
jmp 0x33741
movq 0xe8(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0x37410
jmp 0x33758
jmp 0x3375a
jmp 0x3375c
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x3360b
leaq 0x144eef(%rip), %rdi    # 0x178668
xorl %esi, %esi
callq 0xd1d30
jmp 0x33782
jmp 0x33784
jmp 0x33786
xorl %eax, %eax
movl %eax, %edi
leaq 0x1001da(%rip), %rsi    # 0x13396b
callq 0xf7c0
movq %rax, 0x138(%rsp)
cmpq $0x0, 0x138(%rsp)
jne 0x334ce
jmp 0x337af
jmp 0x33869
movq 0x138(%rsp), %rdi
leaq 0x1001ab(%rip), %rsi    # 0x13396e
callq 0xf700
cmpl $0x0, %eax
jne 0x337dd
movl $0x1, 0x17c(%rsp)
jmp 0x33867
movq 0x138(%rsp), %rdi
leaq 0x10018e(%rip), %rsi    # 0x13397a
callq 0xf700
cmpl $0x0, %eax
jne 0x33803
movl $0x1, 0x174(%rsp)
jmp 0x33865
cmpl $0x0, 0x17c(%rsp)
je 0x33833
movq 0x138(%rsp), %rdi
leaq 0x105294(%rip), %rsi    # 0x138ab0
callq 0xf700
cmpl $0x0, %eax
jne 0x33833
movl $0x1, 0x178(%rsp)
jmp 0x33863
cmpl $0x0, 0x174(%rsp)
je 0x33861
movq 0x138(%rsp), %rdi
leaq 0x10013d(%rip), %rsi    # 0x133989
callq 0xf700
cmpl $0x0, %eax
jne 0x33861
movl $0x1, 0x170(%rsp)
jmp 0x33863
jmp 0x33865
jmp 0x33867
jmp 0x33869
jmp 0x3386b
jmp 0x33259
movl $0x0, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
movl %eax, 0x60(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x13600
movl %eax, 0x64(%rsp)
jmp 0x33899
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jg 0x33ad5
movl 0xe4(%rsp), %esi
leaq 0x150(%rsp), %rdi
callq 0x377e0
movq %rax, 0x58(%rsp)
jmp 0x338c7
movq 0x58(%rsp), %rax
movq %rax, 0xd8(%rsp)
movl 0xe4(%rsp), %esi
incl %esi
leaq 0x150(%rsp), %rdi
callq 0x377e0
movq %rax, 0x50(%rsp)
jmp 0x338f1
movq 0x50(%rsp), %rax
movq %rax, 0xd0(%rsp)
movl 0xe4(%rsp), %esi
addl $0x2, %esi
leaq 0x150(%rsp), %rdi
callq 0x377e0
movq %rax, 0x48(%rsp)
jmp 0x3391c
movq 0xa8(%rsp), %rdi
movq 0x48(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0xd8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
movq 0xc8(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x40(%rsp)
jmp 0x3395c
movq 0x40(%rsp), %rax
cmpq $0x0, %rax
jne 0x33abd
movq 0xa8(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq 0xc8(%rsp), %rdx
movq 0xd0(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x38(%rsp)
jmp 0x3399e
movq 0x38(%rsp), %rax
cmpq $0x0, %rax
jne 0x33abb
movq 0xa8(%rsp), %rdi
movq 0xd8(%rsp), %rsi
callq 0x13660
movq %rax, 0x30(%rsp)
jmp 0x339c9
movq 0x30(%rsp), %rsi
movq 0xa8(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x28(%rsp)
jmp 0x339ea
movq 0xa8(%rsp), %rdi
movq 0x28(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xd0(%rsp), %rsi
callq 0x13660
movq %rax, 0x20(%rsp)
jmp 0x33a13
movq 0x20(%rsp), %rsi
movq 0xa8(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x18(%rsp)
jmp 0x33a34
movq 0xa8(%rsp), %rdi
movq 0x18(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xc8(%rsp), %rsi
callq 0x13660
movq %rax, 0x10(%rsp)
jmp 0x33a5d
movq 0x10(%rsp), %rsi
movq 0xa8(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x8(%rsp)
jmp 0x33a7e
movq 0xa8(%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xc0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movq 0xb0(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
jmp 0x33ab9
jmp 0x33abb
jmp 0x33abd
jmp 0x33abf
movl 0xe4(%rsp), %eax
addl $0x3, %eax
movl %eax, 0xe4(%rsp)
jmp 0x3387b
movq 0x588(%rsp), %rdi
callq 0xf4b0
jmp 0x33ae4
movl $0x1, 0x5a4(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x36850
leaq 0x160(%rsp), %rdi
callq 0x36850
jmp 0x33b27
leaq 0x150(%rsp), %rdi
callq 0x36850
leaq 0x160(%rsp), %rdi
callq 0x36850
jmp 0x33b36
movl 0x5a4(%rsp), %eax
addq $0x5a8, %rsp            # imm = 0x5A8
retq
movq 0x148(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)