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

  // Read body
  char buffer[1024];
  int line_count = 0;
  RNBoolean corners = FALSE;
  RNBoolean planes = FALSE;
  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
    if (*bufferp == '\0') continue;

    // Get keyword
    char keyword[80];
    if (sscanf(bufferp, "%s", keyword) != 1) {
      RNFail("Syntax error on line %d in file %s", line_count, filename);
      return 0;
    }

    // Check keyword
    if (keyword[0] == '%') {
      corners = planes = FALSE;
      if (!strcmp(keyword, "%CORNERS")) corners = TRUE;
      else if (!strcmp(keyword, "%PLANES")) planes = TRUE;
      else if (!strcmp(keyword, "%EOF")) break;
      continue;
    }

    // Read data
    if (corners) {
      // Read vertex coordinates
      int id;
      double x, y, z;
      if (sscanf(bufferp, "%d%lf%lf%lf", &id, &x, &y, &z) != 4) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Create vertex
      CreateVertex(R3Point(x, y, z));
    }
    else if (planes) {
      // Read plane header
      int id;
      char buffer1[256];
      char buffer2[256];
      if ((sscanf(bufferp, "%d%s%s", &id, buffer1, buffer2) != 3) ||
          (strcmp(buffer1, "/")) || (strcmp(buffer2, "/RIGID"))) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Read vertices
      RNArray<R3MeshVertex *> vertices;
      if (fgets(buffer, 1023, fp)) {
        line_count++;
        bufferp = strtok(buffer, "\t ");
        while (bufferp) {
          int id = atoi(bufferp);
          if ((id <= 0) || (id > NVertices())) {
            RNFail("Bogus vertex index on line %d in file %s", line_count, filename);
            return 0;
          }
          R3MeshVertex *v = Vertex(id-1);
          assert(v);
          vertices.Insert(v);
          bufferp = strtok(NULL, "\t ");
        }
      }

      // Create face(s)
      for (int i = 2; i < vertices.NEntries(); i++) {
        if (vertices[0] == vertices[i-1]) continue;
        if (vertices[i-1] == vertices[i]) continue;
        if (vertices[0] == vertices[i]) continue;
        if (!CreateFace(vertices[0], vertices[i-1], vertices[i])) {
          // 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(vertices[0]);
          degenerate_triangle_vertices.Insert(vertices[i-1]);
          degenerate_triangle_vertices.Insert(vertices[i]);
        }
      }
    }
  }
    
  // 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 $0x698, %rsp            # imm = 0x698
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x7e4ad(%rip), %rsi     # 0x9caf3
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x1e98f
movq %rax, %r14
movq %r15, 0x8(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x6630a
movl $0x0, (%rsp)
movl $0x0, 0x4(%rsp)
xorl %ebp, %ebp
movl %ebp, %r12d
leaq 0x290(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe760
testq %rax, %rax
je 0x1e9a7
leaq 0x28f(%rsp), %r15
movb 0x1(%r15), %r13b
incq %r15
movsbl %r13b, %edi
callq 0xe080
testl %eax, %eax
jne 0x1e6a3
leal 0x1(%r12), %ebp
testb %r13b, %r13b
je 0x1e67a
movq %r15, %rdi
leaq 0x7a795(%rip), %rsi     # 0x98e60
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0xe430
cmpl $0x1, %eax
jne 0x1ea6a
cmpb $0x25, 0x40(%rsp)
jne 0x1e72b
leaq 0x40(%rsp), %rdi
leaq 0x74f29(%rip), %rsi     # 0x9361c
pushq $0x9
popq %rdx
callq 0xe480
testl %eax, %eax
je 0x1e79b
movabsq $0x53454e414c5025, %rax # imm = 0x53454E414C5025
cmpq %rax, 0x40(%rsp)
jne 0x1e898
movl $0x0, 0x4(%rsp)
pushq $0x1
popq %rax
movl %eax, (%rsp)
jmp 0x1e67a
cmpl $0x0, 0x4(%rsp)
je 0x1e7ae
movq %r15, %rdi
leaq 0x74f52(%rip), %rsi     # 0x9368e
leaq 0x3c(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x20(%rsp), %r9
xorl %eax, %eax
callq 0xe430
cmpl $0x4, %eax
jne 0x1ea81
movsd 0x90(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
leaq 0x190(%rsp), %r15
movq %r15, %rdi
callq 0x37b4c
movq (%rbx), %rax
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x20(%rax)
jmp 0x1e67a
movl $0x0, (%rsp)
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
jmp 0x1e67a
movl $0x0, 0x4(%rsp)
cmpl $0x0, (%rsp)
je 0x1e8c8
movq %r15, %rdi
leaq 0x74e60(%rip), %rsi     # 0x9362a
leaq 0x20(%rsp), %rdx
leaq 0x190(%rsp), %rcx
leaq 0x90(%rsp), %r8
xorl %eax, %eax
callq 0xe430
cmpl $0x3, %eax
jne 0x1ea98
cmpw $0x2f, 0x190(%rsp)
jne 0x1ea98
leaq 0x90(%rsp), %rdi
leaq 0x74e24(%rip), %rsi     # 0x93631
pushq $0x7
popq %rdx
callq 0xe480
testl %eax, %eax
jne 0x1ea98
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x6630a
leaq 0x290(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %r14, %rdx
callq 0xe760
testq %rax, %rax
je 0x1e8d7
addl $0x2, %r12d
leaq 0x290(%rsp), %rdi
leaq 0x74ddd(%rip), %r13     # 0x93638
movq %r13, %rsi
callq 0xe740
testq %rax, %rax
je 0x1e8d4
movq %rax, %rdi
callq 0xe730
testl %eax, %eax
jle 0x1e95b
cmpl 0x14(%rbx), %eax
jg 0x1e95b
movq 0x8(%rbx), %rcx
movl %eax, %eax
movq -0x8(%rcx,%rax,8), %rsi
movq %r15, %rdi
callq 0x22bfc
xorl %edi, %edi
jmp 0x1e85b
leaq 0x40(%rsp), %rdi
leaq 0x74d81(%rip), %rsi     # 0x93625
pushq $0x5
popq %rdx
callq 0xe480
testl %eax, %eax
je 0x1e9a7
movl $0x0, (%rsp)
movl $0x0, 0x4(%rsp)
jmp 0x1e67a
movl $0x0, (%rsp)
jmp 0x1e67a
movl %r12d, %ebp
pushq $0x2
popq %r15
leaq 0x28(%rsp), %r12
movslq 0x1c(%rsp), %rax
cmpq %rax, %r15
jge 0x1e953
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq -0x8(%rax,%r15,8), %rdx
cmpq %rdx, %rsi
je 0x1e94e
movq (%rax,%r15,8), %rcx
cmpq %rcx, %rdx
je 0x1e94e
cmpq %rcx, %rsi
je 0x1e94e
movq (%rbx), %rax
movq %rbx, %rdi
xorl %r8d, %r8d
callq *0x48(%rax)
testq %rax, %rax
jne 0x1e94e
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0x22bfc
movq 0x10(%rsp), %rax
movq -0x8(%rax,%r15,8), %rsi
movq %r12, %rdi
callq 0x22bfc
movq 0x10(%rsp), %rax
movq (%rax,%r15,8), %rsi
movq %r12, %rdi
callq 0x22bfc
incq %r15
jmp 0x1e8e0
movb $0x1, %r15b
movl %ebp, %r12d
jmp 0x1e974
xorl %r15d, %r15d
leaq 0x74cd6(%rip), %rdi     # 0x9363b
movl %r12d, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
leaq 0x10(%rsp), %rdi
callq 0x66378
movl %r12d, %ebp
testb %r15b, %r15b
jne 0x1e67a
jmp 0x1eaad
leaq 0x749d4(%rip), %rdi     # 0x9336a
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
jmp 0x1eab9
xorl %ebp, %ebp
movslq 0x34(%rsp), %rax
addq $-0x3, %rax
cmpq %rax, %rbp
jg 0x1ea5d
movq 0x28(%rsp), %rax
movq (%rax,%rbp,8), %r13
movq 0x8(%rax,%rbp,8), %r12
movq 0x10(%rax,%rbp,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 0x1ea54
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 0x1ea54
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, %rbp
jmp 0x1e9a9
movq %r14, %rdi
callq 0xe440
pushq $0x1
popq %rbx
jmp 0x1eaaf
leaq 0x74910(%rip), %rdi     # 0x93381
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x1eaad
leaq 0x748f9(%rip), %rdi     # 0x93381
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x1eaad
leaq 0x748e2(%rip), %rdi     # 0x93381
movl %ebp, %esi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x681ca
xorl %ebx, %ebx
leaq 0x28(%rsp), %rdi
callq 0x66378
movl %ebx, %eax
addq $0x698, %rsp            # imm = 0x698
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1eaf0
jmp 0x1eaf0
jmp 0x1eaf0
jmp 0x1eae1
jmp 0x1eaf0
jmp 0x1eaf0
jmp 0x1eaf0
jmp 0x1eaf0
jmp 0x1eaf0
jmp 0x1eae1
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x66378
jmp 0x1eaf3
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x66378
movq %rbx, %rdi
callq 0xe810
nop