// File: 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:
subq $0x878, %rsp            # imm = 0x878
movq %rdi, 0x868(%rsp)
movq %rsi, 0x860(%rsp)
movq 0x868(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x860(%rsp), %rdi
leaq 0x107d8d(%rip), %rsi    # 0x138673
callq 0xf5e0
movq %rax, 0x858(%rsp)
cmpq $0x0, %rax
jne 0x3091f
movq 0x860(%rsp), %rsi
leaq 0x102ad8(%rip), %rdi    # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x874(%rsp)
jmp 0x313d6
movl $0x0, 0x44c(%rsp)
movl $0x0, 0x448(%rsp)
movl $0x0, 0x444(%rsp)
leaq 0x430(%rsp), %rdi
callq 0x36800
movq 0x858(%rsp), %rdx
leaq 0x450(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
callq 0xf7e0
movq %rax, 0x118(%rsp)
jmp 0x30971
movq 0x118(%rsp), %rax
cmpq $0x0, %rax
je 0x3112e
movl 0x44c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44c(%rsp)
leaq 0x450(%rsp), %rax
movq %rax, 0x418(%rsp)
movq 0x418(%rsp), %rax
movsbl (%rax), %edi
callq 0xf080
cmpl $0x0, %eax
je 0x309e8
movq 0x418(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x418(%rsp)
jmp 0x309a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x428(%rsp)
movl %eax, 0x424(%rsp)
jmp 0x313c7
movq 0x418(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x309fd
jmp 0x3094d
movq 0x418(%rsp), %rdi
leaq 0x3c0(%rsp), %rdx
leaq 0x103fda(%rip), %rsi    # 0x1349ee
movb $0x0, %al
callq 0xf4a0
cmpl $0x1, %eax
je 0x30a5a
movl 0x44c(%rsp), %esi
movq 0x860(%rsp), %rdx
leaq 0x1029c1(%rip), %rdi    # 0x1333f7
xorl %eax, %eax
callq 0xd4f30
jmp 0x30a3f
movl $0x0, 0x874(%rsp)
movl $0x1, 0x3bc(%rsp)
jmp 0x313b8
movsbl 0x3c0(%rsp), %eax
cmpl $0x25, %eax
jne 0x30af4
movl $0x0, 0x444(%rsp)
movl $0x0, 0x448(%rsp)
leaq 0x3c0(%rsp), %rdi
leaq 0x102c03(%rip), %rsi    # 0x133693
callq 0xf700
cmpl $0x0, %eax
jne 0x30aa7
movl $0x1, 0x448(%rsp)
jmp 0x30aef
leaq 0x3c0(%rsp), %rdi
leaq 0x102be6(%rip), %rsi    # 0x13369c
callq 0xf700
cmpl $0x0, %eax
jne 0x30acd
movl $0x1, 0x444(%rsp)
jmp 0x30aed
leaq 0x3c0(%rsp), %rdi
leaq 0x102bc8(%rip), %rsi    # 0x1336a4
callq 0xf700
cmpl $0x0, %eax
jne 0x30aeb
jmp 0x3112e
jmp 0x30aed
jmp 0x30aef
jmp 0x3094d
cmpl $0x0, 0x448(%rsp)
je 0x30bc5
movq 0x418(%rsp), %rdi
leaq 0x102bfe(%rip), %rsi    # 0x13370f
leaq 0x3b8(%rsp), %rdx
leaq 0x3b0(%rsp), %rcx
leaq 0x3a8(%rsp), %r8
leaq 0x3a0(%rsp), %r9
movb $0x0, %al
callq 0xf4a0
cmpl $0x4, %eax
je 0x30b77
movl 0x44c(%rsp), %esi
movq 0x860(%rsp), %rdx
leaq 0x1028a4(%rip), %rdi    # 0x1333f7
xorl %eax, %eax
callq 0xd4f30
jmp 0x30b5c
movl $0x0, 0x874(%rsp)
movl $0x1, 0x3bc(%rsp)
jmp 0x313b8
movsd 0x3b0(%rsp), %xmm0
movsd 0x3a8(%rsp), %xmm1
movsd 0x3a0(%rsp), %xmm2
leaq 0x388(%rsp), %rdi
callq 0x6eb50
jmp 0x30ba1
movq 0x120(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
leaq 0x388(%rsp), %rsi
callq *%rax
jmp 0x30bc0
jmp 0x31129
cmpl $0x0, 0x444(%rsp)
je 0x31127
movq 0x418(%rsp), %rdi
leaq 0x280(%rsp), %rcx
leaq 0x180(%rsp), %r8
leaq 0x102ab7(%rip), %rsi    # 0x1336a9
leaq 0x384(%rsp), %rdx
movb $0x0, %al
callq 0xf4a0
cmpl $0x3, %eax
jne 0x30c38
leaq 0x280(%rsp), %rdi
leaq 0x102a9b(%rip), %rsi    # 0x1336b0
callq 0xf700
cmpl $0x0, %eax
jne 0x30c38
leaq 0x180(%rsp), %rdi
leaq 0x102a84(%rip), %rsi    # 0x1336b2
callq 0xf700
cmpl $0x0, %eax
je 0x30c72
movl 0x44c(%rsp), %esi
movq 0x860(%rsp), %rdx
leaq 0x1027a9(%rip), %rdi    # 0x1333f7
xorl %eax, %eax
callq 0xd4f30
jmp 0x30c57
movl $0x0, 0x874(%rsp)
movl $0x1, 0x3bc(%rsp)
jmp 0x313b8
leaq 0x170(%rsp), %rdi
callq 0x36800
jmp 0x30c81
movq 0x858(%rsp), %rdx
leaq 0x450(%rsp), %rdi
movl $0x3ff, %esi            # imm = 0x3FF
callq 0xf7e0
movq %rax, 0x110(%rsp)
jmp 0x30ca5
movq 0x110(%rsp), %rax
cmpq $0x0, %rax
je 0x30e0d
movl 0x44c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44c(%rsp)
leaq 0x450(%rsp), %rdi
leaq 0x1029e2(%rip), %rsi    # 0x1336b9
callq 0xf7c0
movq %rax, 0x418(%rsp)
cmpq $0x0, 0x418(%rsp)
je 0x30e0b
movq 0x418(%rsp), %rdi
callq 0xf7b0
movl %eax, 0x16c(%rsp)
cmpl $0x0, 0x16c(%rsp)
jle 0x30d47
movq 0x120(%rsp), %rdi
movl 0x16c(%rsp), %eax
movl %eax, 0x108(%rsp)
callq 0x135e0
movl %eax, 0x10c(%rsp)
jmp 0x30d35
movl 0x108(%rsp), %eax
movl 0x10c(%rsp), %ecx
cmpl %ecx, %eax
jle 0x30da7
movl 0x44c(%rsp), %esi
movq 0x860(%rsp), %rdx
leaq 0x10295f(%rip), %rdi    # 0x1336bc
xorl %eax, %eax
callq 0xd4f30
jmp 0x30d66
movl $0x0, 0x874(%rsp)
movl $0x1, 0x3bc(%rsp)
jmp 0x31107
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x428(%rsp)
movl %eax, 0x424(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x36850
jmp 0x313c7
movq 0x120(%rsp), %rdi
movl 0x16c(%rsp), %esi
decl %esi
callq 0x13630
movq %rax, 0x100(%rsp)
jmp 0x30dc7
movq 0x100(%rsp), %rax
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rsi
leaq 0x170(%rsp), %rdi
callq 0x37410
jmp 0x30dee
xorl %eax, %eax
movl %eax, %edi
leaq 0x1028c0(%rip), %rsi    # 0x1336b9
callq 0xf7c0
movq %rax, 0x418(%rsp)
jmp 0x30ce4
jmp 0x30e0d
movl $0x2, 0x15c(%rsp)
movl 0x15c(%rsp), %eax
movl %eax, 0xf8(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x13600
movl %eax, 0xfc(%rsp)
jmp 0x30e3c
movl 0xf8(%rsp), %eax
movl 0xfc(%rsp), %ecx
cmpl %ecx, %eax
jge 0x310fc
leaq 0x170(%rsp), %rdi
xorl %esi, %esi
callq 0x37800
movq %rax, 0xf0(%rsp)
jmp 0x30e6b
movq 0xf0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xe0(%rsp)
movl 0x15c(%rsp), %esi
decl %esi
leaq 0x170(%rsp), %rdi
callq 0x37800
movq %rax, 0xe8(%rsp)
jmp 0x30e9e
movq 0xe0(%rsp), %rax
movq 0xe8(%rsp), %rcx
cmpq (%rcx), %rax
jne 0x30eb8
jmp 0x310e6
movl 0x15c(%rsp), %esi
decl %esi
leaq 0x170(%rsp), %rdi
callq 0x37800
movq %rax, 0xd8(%rsp)
jmp 0x30ed8
movq 0xd8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xc8(%rsp)
movl 0x15c(%rsp), %esi
leaq 0x170(%rsp), %rdi
callq 0x37800
movq %rax, 0xd0(%rsp)
jmp 0x30f09
movq 0xc8(%rsp), %rax
movq 0xd0(%rsp), %rcx
cmpq (%rcx), %rax
jne 0x30f23
jmp 0x310e6
leaq 0x170(%rsp), %rdi
xorl %esi, %esi
callq 0x37800
movq %rax, 0xc0(%rsp)
jmp 0x30f3c
movq 0xc0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
movl 0x15c(%rsp), %esi
leaq 0x170(%rsp), %rdi
callq 0x37800
movq %rax, 0xb8(%rsp)
jmp 0x30f6d
movq 0xb0(%rsp), %rax
movq 0xb8(%rsp), %rcx
cmpq (%rcx), %rax
jne 0x30f87
jmp 0x310e6
leaq 0x170(%rsp), %rdi
xorl %esi, %esi
callq 0x37800
movq %rax, 0xa8(%rsp)
jmp 0x30fa0
movq 0xa8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movl 0x15c(%rsp), %esi
decl %esi
leaq 0x170(%rsp), %rdi
callq 0x37800
movq %rax, 0xa0(%rsp)
jmp 0x30fd3
movq 0xa0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x88(%rsp)
movl 0x15c(%rsp), %esi
leaq 0x170(%rsp), %rdi
callq 0x37800
movq %rax, 0x90(%rsp)
jmp 0x31004
movq 0x88(%rsp), %rdx
movq 0x98(%rsp), %rsi
movq 0x120(%rsp), %rdi
movq 0x90(%rsp), %rax
movq (%rax), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x80(%rsp)
jmp 0x3103d
movq 0x80(%rsp), %rax
cmpq $0x0, %rax
jne 0x310e4
leaq 0x170(%rsp), %rdi
xorl %esi, %esi
callq 0x37800
movq %rax, 0x78(%rsp)
jmp 0x31065
movq 0x78(%rsp), %rax
movq (%rax), %rsi
leaq 0x430(%rsp), %rdi
callq 0x37410
jmp 0x3107c
movl 0x15c(%rsp), %esi
decl %esi
leaq 0x170(%rsp), %rdi
callq 0x37800
movq %rax, 0x70(%rsp)
jmp 0x31099
movq 0x70(%rsp), %rax
movq (%rax), %rsi
leaq 0x430(%rsp), %rdi
callq 0x37410
jmp 0x310b0
movl 0x15c(%rsp), %esi
leaq 0x170(%rsp), %rdi
callq 0x37800
movq %rax, 0x68(%rsp)
jmp 0x310cb
movq 0x68(%rsp), %rax
movq (%rax), %rsi
leaq 0x430(%rsp), %rdi
callq 0x37410
jmp 0x310e2
jmp 0x310e4
jmp 0x310e6
movl 0x15c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x15c(%rsp)
jmp 0x30e18
movl $0x0, 0x3bc(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x36850
movl 0x3bc(%rsp), %eax
testl %eax, %eax
jne 0x313b8
jmp 0x31125
jmp 0x31127
jmp 0x31129
jmp 0x3094d
movl $0x0, 0x158(%rsp)
movl 0x158(%rsp), %eax
movl %eax, 0x60(%rsp)
leaq 0x430(%rsp), %rdi
callq 0x13600
movl %eax, 0x64(%rsp)
jmp 0x31157
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jg 0x31393
movl 0x158(%rsp), %esi
leaq 0x430(%rsp), %rdi
callq 0x377e0
movq %rax, 0x58(%rsp)
jmp 0x31185
movq 0x58(%rsp), %rax
movq %rax, 0x150(%rsp)
movl 0x158(%rsp), %esi
incl %esi
leaq 0x430(%rsp), %rdi
callq 0x377e0
movq %rax, 0x50(%rsp)
jmp 0x311af
movq 0x50(%rsp), %rax
movq %rax, 0x148(%rsp)
movl 0x158(%rsp), %esi
addl $0x2, %esi
leaq 0x430(%rsp), %rdi
callq 0x377e0
movq %rax, 0x48(%rsp)
jmp 0x311da
movq 0x120(%rsp), %rdi
movq 0x48(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x150(%rsp), %rsi
movq 0x148(%rsp), %rdx
movq 0x140(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x40(%rsp)
jmp 0x3121a
movq 0x40(%rsp), %rax
cmpq $0x0, %rax
jne 0x3137b
movq 0x120(%rsp), %rdi
movq 0x150(%rsp), %rsi
movq 0x140(%rsp), %rdx
movq 0x148(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movq %rax, 0x38(%rsp)
jmp 0x3125c
movq 0x38(%rsp), %rax
cmpq $0x0, %rax
jne 0x31379
movq 0x120(%rsp), %rdi
movq 0x150(%rsp), %rsi
callq 0x13660
movq %rax, 0x30(%rsp)
jmp 0x31287
movq 0x30(%rsp), %rsi
movq 0x120(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x28(%rsp)
jmp 0x312a8
movq 0x120(%rsp), %rdi
movq 0x28(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x148(%rsp), %rsi
callq 0x13660
movq %rax, 0x20(%rsp)
jmp 0x312d1
movq 0x20(%rsp), %rsi
movq 0x120(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x18(%rsp)
jmp 0x312f2
movq 0x120(%rsp), %rdi
movq 0x18(%rsp), %rax
movq %rax, 0x130(%rsp)
movq 0x140(%rsp), %rsi
callq 0x13660
movq %rax, 0x10(%rsp)
jmp 0x3131b
movq 0x10(%rsp), %rsi
movq 0x120(%rsp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movq %rax, 0x8(%rsp)
jmp 0x3133c
movq 0x120(%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x138(%rsp), %rsi
movq 0x130(%rsp), %rdx
movq 0x128(%rsp), %rcx
movq (%rdi), %rax
movq 0x48(%rax), %rax
xorl %r8d, %r8d
callq *%rax
jmp 0x31377
jmp 0x31379
jmp 0x3137b
jmp 0x3137d
movl 0x158(%rsp), %eax
addl $0x3, %eax
movl %eax, 0x158(%rsp)
jmp 0x31139
movq 0x858(%rsp), %rdi
callq 0xf4b0
jmp 0x313a2
movl $0x1, 0x874(%rsp)
movl $0x1, 0x3bc(%rsp)
leaq 0x430(%rsp), %rdi
callq 0x36850
jmp 0x313d6
leaq 0x430(%rsp), %rdi
callq 0x36850
jmp 0x313e5
movl 0x874(%rsp), %eax
addq $0x878, %rsp            # imm = 0x878
retq
movq 0x428(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)