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

  // Write vertex header
  fprintf(fp, "%%CORNERS\n\n");

  // Write vertices
  for (int i = 0; i < NVertices(); i++) {
    R3MeshVertex *vertex = Vertex(i);
    int id = VertexID(vertex);
    const R3Point& p = VertexPosition(vertex);
    fprintf(fp, "%d %g %g %g\n", id+1, p.X(), p.Y(), p.Z());
  }

  // Write face header
  fprintf(fp, "\n%%PLANES\n\n");

  // Write Faces
  for (int i = 0; i < NFaces(); i++) {
    R3MeshFace *face = Face(i);
    R3MeshVertex *v0 = VertexOnFace(face, 0);
    R3MeshVertex *v1 = VertexOnFace(face, 1);
    R3MeshVertex *v2 = VertexOnFace(face, 2);
    fprintf(fp, "%d / /RIGID\n%d %d %d\n\n", i, VertexID(v0) + 1, VertexID(v1) + 1, VertexID(v2) + 1);
  }

  // Write trailer
  fprintf(fp, "%%EOF\n");

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0xa0(%rsp), %rdi
leaq 0xff370(%rip), %rsi     # 0x1342f3
callq 0xf5e0
movq %rax, 0x98(%rsp)
cmpq $0x0, %rax
jne 0x34fbc
movq 0xa0(%rsp), %rsi
leaq 0xfe43b(%rip), %rdi     # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0xb4(%rsp)
jmp 0x35218
movq 0x98(%rsp), %rdi
leaq 0xfeaac(%rip), %rsi     # 0x133a77
movb $0x0, %al
callq 0xf730
movl $0x0, 0x94(%rsp)
movq 0x48(%rsp), %rdi
movl 0x94(%rsp), %eax
movl %eax, 0x44(%rsp)
callq 0x135e0
movl %eax, %ecx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x350ca
movq 0x48(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x13630
movq 0x48(%rsp), %rdi
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rsi
callq 0x36910
movq 0x48(%rsp), %rdi
movl %eax, 0x84(%rsp)
movq 0x88(%rsp), %rsi
callq 0x13660
movq %rax, 0x78(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movq 0x78(%rsp), %rdi
callq 0x37820
movsd %xmm0, 0x38(%rsp)
movq 0x78(%rsp), %rdi
callq 0x37870
movsd %xmm0, 0x30(%rsp)
movq 0x78(%rsp), %rdi
callq 0x378c0
movq 0x20(%rsp), %rdi
movl 0x2c(%rsp), %edx
movsd 0x30(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x38(%rsp), %xmm0
leaq 0xfe9d6(%rip), %rsi     # 0x133a83
movb $0x3, %al
callq 0xf730
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x34fdd
movq 0x98(%rsp), %rdi
leaq 0xfe9b7(%rip), %rsi     # 0x133a90
movb $0x0, %al
callq 0xf730
movl $0x0, 0x74(%rsp)
movq 0x48(%rsp), %rdi
movl 0x74(%rsp), %eax
movl %eax, 0x1c(%rsp)
callq 0x135a0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x351ea
movq 0x48(%rsp), %rdi
movl 0x74(%rsp), %esi
callq 0x136d0
movq 0x48(%rsp), %rdi
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rsi
xorl %edx, %edx
callq 0x13700
movq 0x48(%rsp), %rdi
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rsi
movl $0x1, %edx
callq 0x13700
movq 0x48(%rsp), %rdi
movq %rax, 0x58(%rsp)
movq 0x68(%rsp), %rsi
movl $0x2, %edx
callq 0x13700
movq 0x48(%rsp), %rdi
movq %rax, 0x50(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x60(%rsp), %rsi
callq 0x36910
movq 0x48(%rsp), %rdi
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x58(%rsp), %rsi
callq 0x36910
movq 0x48(%rsp), %rdi
addl $0x1, %eax
movl %eax, 0x18(%rsp)
movq 0x50(%rsp), %rsi
callq 0x36910
movq 0x8(%rsp), %rdi
movl 0x10(%rsp), %edx
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r8d
movl %eax, %r9d
addl $0x1, %r9d
leaq 0xfe8c9(%rip), %rsi     # 0x133a9c
movb $0x0, %al
callq 0xf730
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x350e8
movq 0x98(%rsp), %rdi
leaq 0xfe8ba(%rip), %rsi     # 0x133ab3
movb $0x0, %al
callq 0xf730
movq 0x98(%rsp), %rdi
callq 0xf4b0
movl $0x1, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopw (%rax,%rax)