// File: /workspace/github_cpp_makefile_star2_O1/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 0x19edf
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 0x19fb4
leaq 0xa5d54(%rip), %rsi     # 0xbfc4a
leaq 0x50(%rsp), %rdi
callq 0x11710
testq %rax, %rax
je 0x1a1c1
movq %rax, %r14
movq %r12, 0x10(%rsp)
movq 0x8(%rsp), %rcx
cmpl $0x0, 0x24(%rcx)
jle 0x19fa2
leaq 0xa3efc(%rip), %r12     # 0xbde23
leaq 0xa3f07(%rip), %r13     # 0xbde35
leaq 0xa3f0a(%rip), %rbp     # 0xbde3f
xorl %ebx, %ebx
movq 0x18(%rcx), %rax
movq (%rax,%rbx,8), %r15
cmpq $0x0, 0x38(%r15)
je 0x19f96
movq %r14, %rdi
movq %r12, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x11890
movl $0x9, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x11b90
movq 0x8(%rsp), %rax
movq 0x50(%rax), %rdx
movq 0x38(%r15), %rcx
movq %r14, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x11890
movl $0xa, %edi
movq %r14, %rsi
callq 0x117a0
movq 0x8(%rsp), %rcx
incq %rbx
movslq 0x24(%rcx), %rax
cmpq %rax, %rbx
jl 0x19f37
movq %r14, %rdi
callq 0x115a0
leaq 0x50(%rsp), %r14
movq 0x10(%rsp), %r12
leaq 0xa5c8f(%rip), %rsi     # 0xbfc4a
movq %r12, %rdi
callq 0x11710
testq %rax, %rax
je 0x1a1b3
movq %rax, %r15
movq %r14, %rdi
movl $0x2f, %esi
callq 0x11640
leaq 0x1(%rax), %rdx
testq %rax, %rax
cmoveq %r14, %rdx
leaq 0xa3dcc(%rip), %rsi     # 0xbddba
movq %r15, %rdi
xorl %eax, %eax
callq 0x11890
movq 0x8(%rsp), %rcx
cmpl $0x0, 0x24(%rcx)
jle 0x1a1a4
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 0x1a18a
movq 0x88(%rdx), %rbx
testq %rbx, %rbx
je 0x1a18a
movq %rdx, 0x10(%rsp)
movq %r15, %rdi
leaq 0xa3d71(%rip), %rsi     # 0xbddc5
movq 0x18(%rsp), %r12
movl %r12d, %edx
xorl %eax, %eax
callq 0x11890
movq %r15, %rdi
leaq 0xa3d65(%rip), %rsi     # 0xbddd2
movl %r12d, %edx
xorl %eax, %eax
callq 0x11890
cmpl $0x0, 0x14(%rbx)
movq %r15, %r12
leaq 0x30(%rsp), %r15
jle 0x1a12d
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 0x6fee4
leaq 0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x6fef0
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 0xa3cee(%rip), %rsi     # 0xbdde4
movb $0x2, %al
callq 0x11890
movsd 0x18(%rbp), %xmm0
movsd 0x20(%rbp), %xmm1
movsd 0x28(%rbp), %xmm2
movq %r12, %rdi
leaq 0xa3cd8(%rip), %rsi     # 0xbddee
movb $0x3, %al
callq 0x11890
incq %r13
movslq 0x14(%rbx), %rax
cmpq %rax, %r13
jl 0x1a0a7
cmpl $0x0, 0x34(%rbx)
movq %r12, %r15
jle 0x1a18a
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 0xa3c90(%rip), %rsi     # 0xbddfa
movl %ecx, %edx
movl %r8d, %r9d
xorl %eax, %eax
pushq %r10
pushq %r10
callq 0x11890
addq $0x10, %rsp
incq %r12
movslq 0x34(%rbx), %rax
cmpq %rax, %r12
jl 0x1a139
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x8(%rsp), %rcx
movslq 0x24(%rcx), %rax
cmpq %rax, %rdx
jl 0x1a00e
movq %r15, %rdi
callq 0x115a0
movl $0x1, %ebx
jmp 0x1a1d6
leaq 0xa3be9(%rip), %rdi     # 0xbdda3
xorl %ebx, %ebx
movq %r12, %rsi
jmp 0x1a1cf
leaq 0xa3bdb(%rip), %rdi     # 0xbdda3
xorl %ebx, %ebx
leaq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x79092
movl %ebx, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq