// File: /workspace/github_cpp_makefile_star2_O3/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 0x89ec1(%rip), %rsi     # 0xabdd3
movq %r15, %rdi
callq 0xe530
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x223bd
leaq 0x28(%rsp), %rdi
callq 0x6f80c
leaq 0x8(%rsp), %rdi
callq 0x6f80c
leaq 0x50(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq 0x20(%rsp), %rdx
callq 0xe730
testq %rax, %rax
je 0x222db
movq $0x0, 0x18(%rsp)
leaq 0x809c9(%rip), %r12     # 0xa2932
xorl %r14d, %r14d
movl $0x0, 0x4(%rsp)
xorl %r13d, %r13d
leaq 0x4f(%rsp), %r15
movsbl 0x1(%r15), %ebp
incq %r15
movl %ebp, %edi
callq 0xe080
testl %eax, %eax
jne 0x21f7c
movzbl %bpl, %eax
testl %eax, %eax
je 0x2229d
cmpl $0x23, %eax
je 0x2229d
movq %r15, %rdi
movq %r12, %rsi
callq 0xe710
testq %rax, %rax
je 0x222db
movq %rax, %rbp
testl %r14d, %r14d
jne 0x2204f
testl %r13d, %r13d
jne 0x220fe
movq %rbp, %rdi
leaq 0x8095e(%rip), %rsi     # 0xa2935
callq 0xe640
xorl %r13d, %r13d
testl %eax, %eax
je 0x22276
movq %rbp, %rdi
leaq 0x80950(%rip), %rsi     # 0xa2941
callq 0xe640
testl %eax, %eax
je 0x22292
cmpl $0x0, 0x18(%rsp)
je 0x2201c
movq %rbp, %rdi
leaq 0x8a1c3(%rip), %rsi     # 0xac1d2
callq 0xe640
testl %eax, %eax
je 0x222c9
xorl %r14d, %r14d
cmpl $0x0, 0x4(%rsp)
je 0x222bc
movq %rbp, %rdi
leaq 0x8091c(%rip), %rsi     # 0xa2950
callq 0xe640
xorl %r13d, %r13d
testl %eax, %eax
sete %r13b
movl $0x1, 0x4(%rsp)
jmp 0x2229d
movb (%rbp), %al
cmpb $0x5d, %al
leaq 0xc3ca5(%rip), %r14     # 0xe5d00
je 0x2229a
testb %al, %al
je 0x220e1
movsbl %al, %edi
callq 0xe080
testl %eax, %eax
jne 0x220e1
movq %rbp, %rdi
callq 0xe580
movslq 0xc3c78(%rip), %rax     # 0xe5cf8
movsd %xmm0, (%r14,%rax,8)
incl %eax
movl %eax, 0xc3c6a(%rip)     # 0xe5cf8
cmpl $0x3, %eax
jne 0x220e1
movsd 0xc3c65(%rip), %xmm0    # 0xe5d00
movsd 0xc3c65(%rip), %xmm1    # 0xe5d08
movsd 0xc3c65(%rip), %xmm2    # 0xe5d10
leaq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0x3c532
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movl 0x34(%rsp), %edx
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x6f8a2
movl $0x0, 0xc3c17(%rip)     # 0xe5cf8
xorl %edi, %edi
movq %r12, %rsi
callq 0xe710
testq %rax, %rax
je 0x22282
movq %rax, %rbp
movb (%rax), %al
jmp 0x22061
movb (%rbp), %al
cmpb $0x5d, %al
jne 0x22115
xorl %r13d, %r13d
movl $0x0, 0x4(%rsp)
jmp 0x2229a
testb %al, %al
je 0x2220f
movsbl %al, %edi
callq 0xe080
testl %eax, %eax
jne 0x2220f
movb 0xc3bf5(%rip), %al      # 0xe5d28
testb %al, %al
je 0x2222b
movq %rbp, %rdi
callq 0xe700
testl %eax, %eax
js 0x22169
movq 0x28(%rsp), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rsi
movl 0xc3bcc(%rip), %edx     # 0xe5d24
leaq 0xc3bb9(%rip), %rdi     # 0xe5d18
callq 0x6f8a2
jmp 0x2220f
cmpl $0x3, 0xc3bb4(%rip)     # 0xe5d24
jl 0x22201
movq 0xc3b9b(%rip), %rax     # 0xe5d18
movq (%rax), %rbp
movl $0x2, %r14d
movq 0xc3b8b(%rip), %rax     # 0xe5d18
movq -0x8(%rax,%r14,8), %r15
cmpq %r15, %rbp
je 0x221f2
movq (%rax,%r14,8), %r13
cmpq %r13, %r15
je 0x221f2
cmpq %r13, %rbp
je 0x221f2
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x221f2
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x6f8a2
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x6f8a2
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x6f8a2
incq %r14
movslq 0xc3b28(%rip), %rax     # 0xe5d24
cmpq %rax, %r14
jl 0x22186
leaq 0xc3b10(%rip), %rdi     # 0xe5d18
xorl %esi, %esi
callq 0x6fa6c
xorl %r14d, %r14d
xorl %edi, %edi
movq %r12, %rsi
callq 0xe710
testq %rax, %rax
je 0x2228a
movq %rax, %rbp
movb (%rax), %al
jmp 0x22115
leaq 0xc3af6(%rip), %rdi     # 0xe5d28
callq 0xe810
testl %eax, %eax
je 0x2213b
leaq 0xc3ad2(%rip), %rdi     # 0xe5d18
callq 0x6f80c
leaq 0x4d624(%rip), %rdi     # 0x6f876
leaq 0xc3abf(%rip), %rsi     # 0xe5d18
leaq 0xc3228(%rip), %rdx     # 0xe5488
callq 0xe3b0
leaq 0xc3abc(%rip), %rdi     # 0xe5d28
callq 0xe2e0
jmp 0x2213b
movl $0x1, %eax
movq %rax, 0x18(%rsp)
jmp 0x2229a
movl $0x1, %r14d
jmp 0x2229d
movl $0x1, %r13d
jmp 0x2229d
movl $0x1, 0x4(%rsp)
xorl %r14d, %r14d
leaq 0x50(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq 0x20(%rsp), %rdx
callq 0xe730
testq %rax, %rax
jne 0x21f77
jmp 0x222db
xorl %r13d, %r13d
movl $0x0, 0x4(%rsp)
jmp 0x2229d
movl $0x1, %r14d
movl $0x1, %eax
movq %rax, 0x18(%rsp)
jmp 0x2229d
cmpl $0x3, 0x14(%rsp)
jl 0x22398
xorl %r14d, %r14d
movq 0x8(%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 0x22382
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 0x22382
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
movslq 0x14(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %r14
jle 0x222e9
movq 0x20(%rsp), %rdi
callq 0xe430
leaq 0x8(%rsp), %rdi
callq 0x6f876
leaq 0x28(%rsp), %rdi
callq 0x6f876
movl $0x1, %ebx
jmp 0x223d0
leaq 0x80081(%rip), %rdi     # 0xa2445
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
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 0xc393a(%rip), %rdi     # 0xe5d28
callq 0xe2d0
jmp 0x2240d
movq %rax, %rbx
jmp 0x22417
jmp 0x2240a
jmp 0x2240a
jmp 0x2240a
jmp 0x2240a
jmp 0x2240a
jmp 0x2240a
jmp 0x2240a
jmp 0x2240a
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x6f876
leaq 0x28(%rsp), %rdi
callq 0x6f876
movq %rbx, %rdi
callq 0xe7e0
nop