// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x7d305(%rip), %rsi     # 0x9caf3
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x1fcb3
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x6630a
leaq 0x18(%rsp), %rdi
callq 0x6630a
movq $0x0, 0x8(%rsp)
xorl %ecx, %ecx
xorl %r13d, %r13d
xorl %edx, %edx
movq %r14, 0x10(%rsp)
movl %edx, 0x4(%rsp)
movl %ecx, %r12d
leaq 0x50(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe760
testq %rax, %rax
je 0x1fbd9
leaq 0x4f(%rsp), %r15
movsbl 0x1(%r15), %ebp
incq %r15
movl %ebp, %edi
callq 0xe080
testl %eax, %eax
jne 0x1f852
movzbl %bpl, %eax
movl %r12d, %ecx
movl 0x4(%rsp), %edx
testl %eax, %eax
je 0x1f82b
movl %r12d, %ecx
movl 0x4(%rsp), %edx
cmpl $0x23, %eax
je 0x1f82b
movq %r15, %rdi
leaq 0x74058(%rip), %rsi     # 0x938e2
callq 0xe740
testq %rax, %rax
je 0x1fbd9
movq %rax, %rbp
testl %r13d, %r13d
jne 0x1f987
cmpl $0x0, 0x8(%rsp)
jne 0x1fa3f
movq %rbp, %rdi
leaq 0x7402c(%rip), %rsi     # 0x938e5
callq 0xe670
movl $0x0, %ecx
movq %rcx, 0x8(%rsp)
movl %r12d, %ecx
movl $0x0, %r13d
pushq $0x1
popq %rdx
testl %eax, %eax
je 0x1f82b
movq %rbp, %rdi
leaq 0x7400b(%rip), %rsi     # 0x938f1
callq 0xe670
movl $0x0, %ecx
movq %rcx, 0x8(%rsp)
pushq $0x1
popq %rcx
movl $0x0, %r13d
movl 0x4(%rsp), %edx
testl %eax, %eax
je 0x1f82b
cmpl $0x0, 0x4(%rsp)
je 0x1f93b
movq %rbp, %rdi
leaq 0x7d5d7(%rip), %rsi     # 0x9cef2
callq 0xe670
movl $0x0, %ecx
movq %rcx, 0x8(%rsp)
movl %r12d, %ecx
pushq $0x1
popq %rdx
movl %edx, %r13d
testl %eax, %eax
je 0x1f82b
movl $0x0, %eax
movq %rax, 0x8(%rsp)
movl $0x0, %ecx
movl $0x0, %r13d
movl 0x4(%rsp), %edx
testl %r12d, %r12d
je 0x1f82b
movq %rbp, %rdi
leaq 0x73f99(%rip), %rsi     # 0x93900
callq 0xe670
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movq %rcx, 0x8(%rsp)
pushq $0x1
popq %rcx
xorl %r13d, %r13d
movl 0x4(%rsp), %edx
jmp 0x1f82b
movb (%rbp), %al
movl %r12d, %ecx
movl $0x0, %r13d
movl 0x4(%rsp), %edx
cmpb $0x5d, %al
je 0x1f82b
testb %al, %al
je 0x1fa1e
movsbl %al, %edi
callq 0xe080
testl %eax, %eax
jne 0x1fa1e
movq %rbp, %rdi
callq 0xe5a0
movslq 0xbb34a(%rip), %rax     # 0xdad08
leaq 0xbb34b(%rip), %rcx     # 0xdad10
movsd %xmm0, (%rcx,%rax,8)
incl %eax
movl %eax, 0xbb336(%rip)     # 0xdad08
cmpl $0x3, %eax
jne 0x1fa1e
movsd 0xbb331(%rip), %xmm0    # 0xdad10
movsd 0xbb331(%rip), %xmm1    # 0xdad18
movsd 0xbb331(%rip), %xmm2    # 0xdad20
leaq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0x37b4c
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x22bfc
andl $0x0, 0xbb2ea(%rip)     # 0xdad08
xorl %edi, %edi
leaq 0x73ebb(%rip), %rsi     # 0x938e2
callq 0xe740
testq %rax, %rax
je 0x1fbb3
movq %rax, %rbp
movb (%rax), %al
jmp 0x1f99f
movb (%rbp), %al
movl $0x0, %ecx
movq %rcx, 0x8(%rsp)
movl $0x0, %ecx
movl $0x0, %r13d
movl 0x4(%rsp), %edx
cmpb $0x5d, %al
je 0x1f82b
testb %al, %al
je 0x1fb43
movsbl %al, %edi
callq 0xe080
testl %eax, %eax
jne 0x1fb43
movb 0xbb2b7(%rip), %al      # 0xdad38
testb %al, %al
je 0x1fb63
movq %rbp, %rdi
callq 0xe730
testl %eax, %eax
js 0x1fab1
movq 0x28(%rsp), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rsi
leaq 0xbb281(%rip), %rdi     # 0xdad28
callq 0x22bfc
jmp 0x1fb43
movq 0xbb270(%rip), %rax     # 0xdad28
movq (%rax), %rbp
pushq $0x2
popq %r14
movslq 0xbb26e(%rip), %rax     # 0xdad34
cmpq %rax, %r14
jge 0x1fb30
movq 0xbb256(%rip), %rax     # 0xdad28
movq -0x8(%rax,%r14,8), %r13
cmpq %r13, %rbp
je 0x1fb2b
movq (%rax,%r14,8), %r15
cmpq %r15, %r13
je 0x1fb2b
cmpq %r15, %rbp
je 0x1fb2b
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1fb2b
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x22bfc
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x22bfc
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x22bfc
incq %r14
jmp 0x1fabf
leaq 0xbb1f1(%rip), %rdi     # 0xdad28
xorl %esi, %esi
callq 0x66528
movq 0x10(%rsp), %r14
xorl %r13d, %r13d
xorl %edi, %edi
leaq 0x73d93(%rip), %rsi     # 0x938e2
callq 0xe740
testq %rax, %rax
je 0x1fbc5
movq %rax, %rbp
movb (%rax), %al
jmp 0x1fa63
leaq 0xbb1ce(%rip), %rdi     # 0xdad38
callq 0xe840
testl %eax, %eax
je 0x1fa89
leaq 0xbb1aa(%rip), %rdi     # 0xdad28
callq 0x6630a
leaq 0x467ee(%rip), %rdi     # 0x66378
leaq 0xbb197(%rip), %rsi     # 0xdad28
leaq 0xba900(%rip), %rdx     # 0xda498
callq 0xe3c0
leaq 0xbb194(%rip), %rdi     # 0xdad38
callq 0xe2f0
movq 0x10(%rsp), %r14
jmp 0x1fa89
movl %r12d, %ecx
pushq $0x1
popq %rax
movl %eax, %r13d
movl 0x4(%rsp), %edx
jmp 0x1f82b
pushq $0x1
popq %rax
movq %rax, 0x8(%rsp)
movl %r12d, %ecx
movl 0x4(%rsp), %edx
jmp 0x1f82b
xorl %r14d, %r14d
movslq 0x24(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %r14
jg 0x1fc90
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %r13
movq 0x8(%rax,%r14,8), %r12
movq 0x10(%rax,%r14,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 0x1fc87
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 0x1fc87
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, %r14
jmp 0x1fbdc
movq 0x10(%rsp), %rdi
callq 0xe440
leaq 0x18(%rsp), %rdi
callq 0x66378
leaq 0x28(%rsp), %rdi
callq 0x66378
pushq $0x1
popq %rbx
jmp 0x1fcc6
leaq 0x7373b(%rip), %rdi     # 0x933f5
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebx, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xbb054(%rip), %rdi     # 0xdad38
callq 0xe2e0
jmp 0x1fd03
movq %rax, %rbx
jmp 0x1fd0d
jmp 0x1fd00
jmp 0x1fd00
jmp 0x1fd00
jmp 0x1fd00
jmp 0x1fd00
jmp 0x1fd00
jmp 0x1fd00
jmp 0x1fd00
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x66378
leaq 0x28(%rsp), %rdi
callq 0x66378
movq %rbx, %rdi
callq 0xe810
nop