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

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

  // 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);
    int m = FaceMaterial(face);
    fprintf(fp, "#shape_triangle %d %d %d %d\n", m, v0->id, v1->id, v2->id);
  }

  // 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, 0x40(%rsp)
movq 0xa0(%rsp), %rdi
leaq 0x100570(%rip), %rsi    # 0x1342f3
callq 0xf5e0
movq %rax, 0x98(%rsp)
cmpq $0x0, %rax
jne 0x33dbc
movq 0xa0(%rsp), %rsi
leaq 0xff63b(%rip), %rdi     # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0xb4(%rsp)
jmp 0x33fe0
movl $0x0, 0x94(%rsp)
movq 0x40(%rsp), %rdi
movl 0x94(%rsp), %eax
movl %eax, 0x3c(%rsp)
callq 0x135e0
movl %eax, %ecx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0x33eee
movq 0x40(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x13630
movq 0x40(%rsp), %rdi
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rsi
callq 0x13660
movq 0x40(%rsp), %rdi
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rsi
callq 0x36860
movq %rax, 0x78(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x80(%rsp), %rdi
callq 0x37820
movsd %xmm0, 0x30(%rsp)
movq 0x80(%rsp), %rdi
callq 0x37870
movsd %xmm0, 0x10(%rsp)
movq 0x80(%rsp), %rdi
callq 0x378c0
movsd %xmm0, 0x18(%rsp)
movq 0x78(%rsp), %rdi
callq 0x38c20
movsd %xmm0, 0x20(%rsp)
movq 0x78(%rsp), %rdi
callq 0x38c30
movsd %xmm0, 0x28(%rsp)
movq 0x78(%rsp), %rdi
callq 0x38c40
movq 0x8(%rsp), %rdi
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movsd 0x20(%rsp), %xmm3
movsd 0x28(%rsp), %xmm4
movaps %xmm0, %xmm5
movsd 0x30(%rsp), %xmm0
leaq 0xffb22(%rip), %rsi     # 0x1339f3
movb $0x6, %al
callq 0xf730
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x33dc7
movl $0x0, 0x74(%rsp)
movq 0x40(%rsp), %rdi
movl 0x74(%rsp), %eax
movl %eax, 0x4(%rsp)
callq 0x135a0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x33fc8
movq 0x40(%rsp), %rdi
movl 0x74(%rsp), %esi
callq 0x136d0
movq 0x40(%rsp), %rdi
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rsi
xorl %edx, %edx
callq 0x13700
movq 0x40(%rsp), %rdi
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rsi
movl $0x1, %edx
callq 0x13700
movq 0x40(%rsp), %rdi
movq %rax, 0x58(%rsp)
movq 0x68(%rsp), %rsi
movl $0x2, %edx
callq 0x13700
movq 0x40(%rsp), %rdi
movq %rax, 0x50(%rsp)
movq 0x68(%rsp), %rsi
callq 0x369e0
movl %eax, 0x4c(%rsp)
movq 0x98(%rsp), %rdi
movl 0x4c(%rsp), %edx
movq 0x60(%rsp), %rax
movl 0x70(%rax), %ecx
movq 0x58(%rsp), %rax
movl 0x70(%rax), %r8d
movq 0x50(%rsp), %rax
movl 0x70(%rax), %r9d
leaq 0xffa61(%rip), %rsi     # 0x133a12
movb $0x0, %al
callq 0xf730
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x33ef6
movq 0x98(%rsp), %rdi
callq 0xf4b0
movl $0x1, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nop