// File: /workspace/github_cpp_makefile_star2_O3/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), %r14
movl $0x3fc, %edx            # imm = 0x3FC
movq %r14, %rdi
callq 0x112d0
movq %r14, %rdi
movl $0x2e, %esi
callq 0x11640
testq %rax, %rax
je 0x19bf9
xorl %ebx, %ebx
movb %bl, (%rax)
leaq 0x50(%rsp), %rdi
callq 0x11260
movb %bl, 0x54(%rsp,%rax)
movl $0x6c746d2e, 0x50(%rsp,%rax) # imm = 0x6C746D2E
movq 0x8(%rsp), %rax
cmpq $0x0, 0x50(%rax)
je 0x19cd1
leaq 0xa403a(%rip), %rsi     # 0xbdc4a
leaq 0x50(%rsp), %rdi
callq 0x11700
testq %rax, %rax
je 0x19ede
movq %rax, %r14
movq %r12, 0x10(%rsp)
movq 0x8(%rsp), %rdx
movl 0x24(%rdx), %eax
testl %eax, %eax
jle 0x19cbf
leaq 0xa21e1(%rip), %r12     # 0xbbe23
leaq 0xa21ec(%rip), %r13     # 0xbbe35
leaq 0xa21ef(%rip), %rbp     # 0xbbe3f
xorl %ebx, %ebx
movq 0x18(%rdx), %rcx
movq (%rcx,%rbx,8), %r15
cmpq $0x0, 0x38(%r15)
je 0x19cb4
movq %r14, %rdi
movq %r12, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x11880
movl $0x9, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x11b80
movq 0x8(%rsp), %rax
movq 0x50(%rax), %rdx
movq 0x38(%r15), %rcx
movq %r14, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x11880
movl $0xa, %edi
movq %r14, %rsi
callq 0x11790
movq 0x8(%rsp), %rdx
movl 0x24(%rdx), %eax
incq %rbx
movslq %eax, %rcx
cmpq %rcx, %rbx
jl 0x19c52
movq %r14, %rdi
callq 0x115a0
leaq 0x50(%rsp), %r14
movq 0x10(%rsp), %r12
leaq 0xa3f72(%rip), %rsi     # 0xbdc4a
movq %r12, %rdi
callq 0x11700
testq %rax, %rax
je 0x19ed0
movq %rax, %r15
movq %r14, %rdi
movl $0x2f, %esi
callq 0x11640
leaq 0x1(%rax), %rdx
testq %rax, %rax
cmoveq %r14, %rdx
leaq 0xa20af(%rip), %rsi     # 0xbbdba
movq %r15, %rdi
xorl %eax, %eax
callq 0x11880
movq 0x8(%rsp), %rcx
cmpl $0x0, 0x24(%rcx)
jle 0x19ec1
leaq 0x40(%rsp), %r14
xorl %edx, %edx
movq 0x18(%rcx), %rax
movq %rdx, 0x18(%rsp)
movq (%rax,%rdx,8), %rdx
movl 0x28(%rdx), %r13d
movl 0x2c(%rdx), %ebp
testl %r13d, %r13d
sete %al
testl %ebp, %ebp
sete %cl
orb %al, %cl
jne 0x19ea7
movq 0x88(%rdx), %rbx
testq %rbx, %rbx
je 0x19ea7
movq %rdx, 0x10(%rsp)
movq %r15, %rdi
leaq 0xa2054(%rip), %rsi     # 0xbbdc5
movq 0x18(%rsp), %r12
movl %r12d, %edx
xorl %eax, %eax
callq 0x11880
movq %r15, %rdi
leaq 0xa2048(%rip), %rsi     # 0xbbdd2
movl %r12d, %edx
xorl %eax, %eax
callq 0x11880
cmpl $0x0, 0x14(%rbx)
movq %r15, %r12
leaq 0x30(%rsp), %r15
jle 0x19e4a
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %rbp
movq %r15, %rdi
callq 0x6f0d4
leaq 0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x6f0e0
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %r15, %rdx
callq *0x40(%rax)
movsd 0x30(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
divsd 0x20(%rsp), %xmm1
movq %r12, %rdi
leaq 0xa1fd1(%rip), %rsi     # 0xbbde4
movb $0x2, %al
callq 0x11880
movsd 0x18(%rbp), %xmm0
movsd 0x20(%rbp), %xmm1
movsd 0x28(%rbp), %xmm2
movq %r12, %rdi
leaq 0xa1fbb(%rip), %rsi     # 0xbbdee
movb $0x3, %al
callq 0x11880
incq %r13
movslq 0x14(%rbx), %rax
cmpq %rax, %r13
jl 0x19dc4
cmpl $0x0, 0x34(%rbx)
movq %r12, %r15
jle 0x19ea7
xorl %r12d, %r12d
movq 0x28(%rbx), %rax
movq (%rax,%r12,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 %r15, %rdi
leaq 0xa1f73(%rip), %rsi     # 0xbbdfa
movl %ecx, %edx
movl %r8d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r10
callq 0x11880
addq $0x10, %rsp
incq %r12
movslq 0x34(%rbx), %rax
cmpq %rax, %r12
jl 0x19e56
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x8(%rsp), %rcx
movslq 0x24(%rcx), %rax
cmpq %rax, %rdx
jl 0x19d2b
movq %r15, %rdi
callq 0x115a0
movl $0x1, %ebx
jmp 0x19ef3
leaq 0xa1ecc(%rip), %rdi     # 0xbbda3
xorl %ebx, %ebx
movq %r12, %rsi
jmp 0x19eec
leaq 0xa1ebe(%rip), %rdi     # 0xbbda3
xorl %ebx, %ebx
leaq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x77f42
movl %ebx, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop