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

  // Check if have texcoords
  RNBoolean has_texcoords = FALSE;
  for (int i = 0; i < NVertices(); i++) {
    R3MeshVertex *vertex = Vertex(i);
    const R2Point& t = VertexTextureCoords(vertex);
    if (!t.IsZero()) { has_texcoords = TRUE; break; }
  }

  // Write vertices
  for (int i = 0; i < NVertices(); i++) {
    R3MeshVertex *vertex = Vertex(i);
    const R3Point& p = VertexPosition(vertex);
    const R2Point& t = VertexTextureCoords(vertex);
    if (has_texcoords) fprintf(fp, "vt %g %g\n", t.X(), t.Y());
    fprintf(fp, "v %g %g %g\n", p.X(), p.Y(), p.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);
    if (has_texcoords) {
      fprintf(fp, "f %d/%d %d/%d %d/%d\n",
        v0->id + 1, v0->id + 1,
        v1->id + 1, v1->id + 1,
        v2->id + 1, v2->id + 1);
    }
    else {
      fprintf(fp, "f %d %d %d\n",
        v0->id + 1, v1->id + 1, v2->id + 1);
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x78351(%rip), %rsi     # 0x98769
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x20555
movq %rax, %r14
movl 0x14(%rbx), %eax
movq 0x8(%rbx), %rcx
movslq %eax, %r13
xorl %esi, %esi
testl %r13d, %r13d
movl $0x0, %edx
cmovgl %r13d, %edx
xorpd %xmm0, %xmm0
movq %rsi, %rbp
cmpq %rsi, %rdx
je 0x20472
movq (%rcx,%rbp,8), %rsi
movsd 0x48(%rsi), %xmm1
ucomisd %xmm0, %xmm1
jne 0x20472
jp 0x20472
movsd 0x50(%rsi), %xmm1
leaq 0x1(%rbp), %rsi
ucomisd %xmm0, %xmm1
jne 0x20472
jnp 0x20448
xorl %r15d, %r15d
cltq
cmpq %rax, %r15
jge 0x204d6
movq 0x8(%rbx), %rax
movq (%rax,%r15,8), %r12
cmpq %r13, %rbp
jge 0x204a8
movsd 0x48(%r12), %xmm0
movsd 0x50(%r12), %xmm1
movq %r14, %rdi
leaq 0x73505(%rip), %rsi     # 0x939a6
movb $0x2, %al
callq 0xe6a0
movsd 0x18(%r12), %xmm0
movsd 0x20(%r12), %xmm1
movsd 0x28(%r12), %xmm2
movq %r14, %rdi
leaq 0x734e9(%rip), %rsi     # 0x939b0
movb $0x3, %al
callq 0xe6a0
incq %r15
movl 0x14(%rbx), %eax
jmp 0x20475
leaq 0x734f4(%rip), %r12     # 0x939d1
xorl %r15d, %r15d
movslq 0x34(%rbx), %rax
cmpq %rax, %r15
jge 0x20548
movq 0x28(%rbx), %rax
movq (%rax,%r15,8), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rax
movl 0x70(%rcx), %edx
incl %edx
movl 0x70(%rsi), %r8d
incl %r8d
movl 0x70(%rax), %r10d
incl %r10d
movq %r14, %rdi
cmpq %r13, %rbp
jge 0x20533
leaq 0x7349d(%rip), %rsi     # 0x939bc
movl %edx, %ecx
movl %r8d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r10
callq 0xe6a0
popq %rcx
popq %rdx
jmp 0x20543
movq %r12, %rsi
movl %r8d, %ecx
movl %r10d, %r8d
xorl %eax, %eax
callq 0xe6a0
incq %r15
jmp 0x204e0
movq %r14, %rdi
callq 0xe440
pushq $0x1
popq %rbx
jmp 0x20568
leaq 0x72e0e(%rip), %rdi     # 0x9336a
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop