// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/RGBD/RGBDConfiguration.cpp
// Function: gaps::RGBDConfiguration::WriteObjFile(char const*) const
int RGBDConfiguration::
WriteObjFile(const char *filename) const
{
  // Write mtl file
  char mtl_filename[1024];
  strncpy(mtl_filename, filename, 1020);
  char *endp = strrchr(mtl_filename, '.');
  if (endp) { *endp = '\0'; strcat(mtl_filename, ".mtl"); }
  if (!WriteMtlFile(this, mtl_filename)) return 0;

  // Open obj file
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Write material library
  char *startp = strrchr(mtl_filename, '/');
  if (startp) startp++;
  else startp = mtl_filename;
  fprintf(fp, "mtllib %s\n", startp);

  // Write surfaces
  for (int s = 0; s < NSurfaces(); s++) {
    RGBDSurface *surface = Surface(s);
    int width = surface->NTexels(RN_X);
    int height = surface->NTexels(RN_Y);
    if ((width == 0) || (height == 0)) continue;
    
    // Write mesh
    R3Mesh *mesh = surface->mesh;
    if (mesh) {
      // Write material reference
      fprintf(fp, "g surface%d\n", s);
      fprintf(fp, "usemtl surface%d\n", s);

      // Write vertices
      for (int i = 0; i < mesh->NVertices(); i++) {
        R3MeshVertex *vertex = mesh->Vertex(i);
        const R3Point& p = mesh->VertexPosition(vertex);
        R2Point t, s = mesh->VertexTextureCoords(vertex);
        surface->TransformSurfaceToTexture(s, t);
        fprintf(fp, "vt %g %g\n", t.X() / width, t.Y() / height);
        fprintf(fp, "v %g %g %g\n", p.X(), p.Y(), p.Z());
      }

      // Write faces
      for (int i = 0; i < mesh->NFaces(); i++) {
        R3MeshFace *face = mesh->Face(i);
        R3MeshVertex *v0 = mesh->VertexOnFace(face, 0);
        R3MeshVertex *v1 = mesh->VertexOnFace(face, 1);
        R3MeshVertex *v2 = mesh->VertexOnFace(face, 2);
        int i0 = mesh->VertexID(v0) + 1;
        int i1 = mesh->VertexID(v1) + 1;
        int i2 = mesh->VertexID(v2) + 1;
        fprintf(fp, "f %d/%d %d/%d %d/%d\n", i0, i0, i1, i1, i2, i2);
      }
    }
  }
  
  // Close obj file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rsi, %r12
movq %rdi, 0x8(%rsp)
leaq 0x50(%rsp), %r15
movl $0x3fc, %edx            # imm = 0x3FC
movq %r15, %rdi
callq 0x112e0
pushq $0x2e
popq %rsi
movq %r15, %rdi
callq 0x11650
testq %rax, %rax
je 0x18cdf
xorl %ebx, %ebx
movb %bl, (%rax)
leaq 0x50(%rsp), %rdi
callq 0x11250
movb %bl, 0x54(%rsp,%rax)
movl $0x6c746d2e, 0x50(%rsp,%rax) # imm = 0x6C746D2E
movq 0x8(%rsp), %rax
cmpq $0x0, 0x50(%rax)
je 0x18da9
leaq 0x91e64(%rip), %rsi     # 0xaab5a
leaq 0x50(%rsp), %rdi
callq 0x11700
testq %rax, %rax
je 0x18f9c
movq %rax, %r14
movq %r12, 0x10(%rsp)
pushq $0x1
popq %rbx
leaq 0x900a4(%rip), %r15     # 0xa8dbf
pushq $0xa
popq %r12
xorl %r13d, %r13d
movq 0x8(%rsp), %rcx
movslq 0x24(%rcx), %rax
cmpq %rax, %r13
jge 0x18d97
movq 0x18(%rcx), %rax
movq (%rax,%r13,8), %rbp
cmpq $0x0, 0x38(%rbp)
je 0x18d92
movq %r14, %rdi
leaq 0x9005a(%rip), %rsi     # 0xa8da3
movl %r13d, %edx
xorl %eax, %eax
callq 0x118a0
leaq 0x9005b(%rip), %rdi     # 0xa8db5
pushq $0x9
popq %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x11bb0
movq 0x8(%rsp), %rax
movq 0x50(%rax), %rdx
movq 0x38(%rbp), %rcx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x118a0
movl %r12d, %edi
movq %r14, %rsi
callq 0x11790
movq 0x8(%rsp), %rcx
incq %r13
jmp 0x18d27
movq %r14, %rdi
callq 0x115b0
leaq 0x50(%rsp), %r15
movq 0x10(%rsp), %r12
leaq 0x91daa(%rip), %rsi     # 0xaab5a
movq %r12, %rdi
callq 0x11700
testq %rax, %rax
je 0x18f8e
movq %rax, %r14
pushq $0x2f
popq %rsi
movq %r15, %rdi
callq 0x11650
leaq 0x1(%rax), %rdx
testq %rax, %rax
cmoveq %r15, %rdx
leaq 0x8ff59(%rip), %rsi     # 0xa8d3a
movq %r14, %rdi
xorl %eax, %eax
callq 0x118a0
xorl %edx, %edx
movq 0x8(%rsp), %rcx
movslq 0x24(%rcx), %rax
cmpq %rax, %rdx
jge 0x18f81
movq 0x18(%rcx), %rax
movq %rdx, 0x18(%rsp)
movq (%rax,%rdx,8), %rdx
movl 0x28(%rdx), %ebx
movl 0x2c(%rdx), %r13d
testl %ebx, %ebx
sete %al
testl %r13d, %r13d
sete %cl
orb %al, %cl
jne 0x18f74
movq 0x88(%rdx), %r12
testq %r12, %r12
je 0x18f74
movq %rdx, 0x10(%rsp)
movq %r14, %rdi
leaq 0x8ff00(%rip), %rsi     # 0xa8d45
movq 0x18(%rsp), %r15
movl %r15d, %edx
xorl %eax, %eax
callq 0x118a0
movq %r14, %rdi
leaq 0x8fef4(%rip), %rsi     # 0xa8d52
movl %r15d, %edx
xorl %eax, %eax
callq 0x118a0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x28(%rsp)
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %ebx, %ebx
movslq 0x14(%r12), %rax
cmpq %rax, %rbx
jge 0x18f1f
movq 0x8(%r12), %rax
movq (%rax,%rbx,8), %r13
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
callq 0x6663e
leaq 0x48(%r13), %rsi
movq %r14, %r15
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x6664a
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %r15, %r14
movq %rbp, %rdx
callq *0x40(%rax)
movsd 0x30(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
divsd 0x20(%rsp), %xmm1
movq %r15, %rdi
leaq 0x8fe77(%rip), %rsi     # 0xa8d64
movb $0x2, %al
callq 0x118a0
movsd 0x18(%r13), %xmm0
movsd 0x20(%r13), %xmm1
movsd 0x28(%r13), %xmm2
movq %r15, %rdi
leaq 0x8fe5e(%rip), %rsi     # 0xa8d6e
movb $0x3, %al
callq 0x118a0
incq %rbx
jmp 0x18e7f
xorl %ebx, %ebx
movslq 0x34(%r12), %rax
cmpq %rax, %rbx
jge 0x18f74
movq 0x28(%r12), %rax
movq (%rax,%rbx,8), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdx
movq 0x18(%rax), %rax
movl 0x70(%rcx), %ecx
incl %ecx
movl 0x70(%rdx), %r8d
incl %r8d
movl 0x70(%rax), %r10d
incl %r10d
movq %r14, %rdi
leaq 0x8fe1d(%rip), %rsi     # 0xa8d7a
movl %ecx, %edx
movl %r8d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r10
callq 0x118a0
popq %rcx
popq %rdx
incq %rbx
jmp 0x18f21
movq 0x18(%rsp), %rdx
incq %rdx
jmp 0x18ded
movq %r14, %rdi
callq 0x115b0
pushq $0x1
popq %rbx
jmp 0x18fb1
leaq 0x8fd8e(%rip), %rdi     # 0xa8d23
xorl %ebx, %ebx
movq %r12, %rsi
jmp 0x18faa
leaq 0x8fd80(%rip), %rdi     # 0xa8d23
xorl %ebx, %ebx
leaq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x6e732
movl %ebx, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop