// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/RGBD/RGBDConfiguration.cpp
// Function: gaps::RGBDConfiguration::WriteConfigurationStream(_IO_FILE*, int) const
int RGBDConfiguration::
WriteConfigurationStream(FILE *fp, int write_every_kth_image) const
{
  // Write header
  fprintf(fp, "n_images %d\n", NImages());
  if (Name()) fprintf(fp, "sequence %s\n", Name());
  if (DatasetFormat()) fprintf(fp, "dataset %s\n", DatasetFormat());
  if (color_directory) fprintf(fp, "color_directory %s\n", color_directory);
  if (depth_directory) fprintf(fp, "depth_directory %s\n", depth_directory);
  if (category_directory) fprintf(fp, "category_directory %s\n", category_directory);
  if (instance_directory) fprintf(fp, "instance_directory %s\n", instance_directory);
  if (texture_directory) fprintf(fp, "texture_directory %s\n", texture_directory);
  
  // Write blank line
  fprintf(fp, "\n");

  // Write surfaces
  for (int i = 0; i < NSurfaces(); i++) {
    RGBDSurface *surface = Surface(i);
    // surface->WriteChannels();
    if (surface->rectangle) {
      R3Point centroid = surface->rectangle->Centroid();
      R3Vector normal = surface->rectangle->Normal();
      R3Vector up = surface->rectangle->Axis(1);
      RNLength rx = surface->rectangle->Radius(0);
      RNLength ry = surface->rectangle->Radius(1);
      const char *texture_filename = (surface->TextureFilename()) ? surface->TextureFilename() : "-";
      fprintf(fp, "rectangle  %s  %g   %g %g %g   %g %g %g   %g %g %g   %g %g %g\n",
        texture_filename, surface->WorldTexelSpacing(),
        centroid.X(), centroid.Y(), centroid.Z(),
        normal.X(), normal.Y(), normal.Z(),
        up.X(), up.Y(), up.Z(), rx, ry, surface->thickness);
    }
    else if (surface->mesh) {
      const char *texture_filename = (surface->TextureFilename()) ? surface->TextureFilename() : "-";
      const char *mesh_filename = (surface->MeshFilename()) ? surface->MeshFilename() : "-";
      fprintf(fp, "mesh  %s  %s  %g\n", texture_filename, mesh_filename, surface->WorldTexelSpacing());
    }
  }
  
  // Write blank line
  fprintf(fp, "\n");

  // Write images
  int image_width = 0;
  int image_height = 0;
  R3Matrix intrinsics_matrix = R3identity_matrix;
  for (int i = 0; i < NImages(); i++) {
    if ((write_every_kth_image > 1) && ((i % write_every_kth_image) != 0)) continue;
    RGBDImage *image = Image(i);

    // Write image dimensions
    if ((image->NPixels(RN_X) > 0) && (image->NPixels(RN_Y) > 0)) {
      if ((image->NPixels(RN_X) != image_width) || (image->NPixels(RN_Y) != image_height)) {
        fprintf(fp, "depth_resolution %d %d\n", image->NPixels(RN_X), image->NPixels(RN_Y));
        image_width = image->NPixels(RN_X);
        image_height = image->NPixels(RN_Y);
      }
    }

    // Write intrinsics matrix
    if (!image->Intrinsics().IsZero()) {
      if (image->Intrinsics() != intrinsics_matrix) {
        intrinsics_matrix = image->Intrinsics();
        fprintf(fp, "intrinsics_matrix  %g %g %g  %g %g %g  %g %g %g\n",
          intrinsics_matrix[0][0], intrinsics_matrix[0][1], intrinsics_matrix[0][2],
          intrinsics_matrix[1][0], intrinsics_matrix[1][1], intrinsics_matrix[1][2],
          intrinsics_matrix[2][0], intrinsics_matrix[2][1], intrinsics_matrix[2][2]);
      }
    }

    // Write images
    // if (!image->WriteChannels()) return 0;

    // Write image with extrinsics
    R4Matrix m = image->CameraToWorld().Matrix();
    const char *depth_filename = (image->DepthFilename()) ? image->DepthFilename() : "-";
    const char *color_filename = (image->ColorFilename()) ? image->ColorFilename() : "-";
    fprintf(fp, "image %s %s  %g %g %g %g  %g %g %g %g  %g %g %g %g  %g %g %g %g\n",
       depth_filename, color_filename, 
       m[0][0], m[0][1], m[0][2], m[0][3],
       m[1][0], m[1][1], m[1][2], m[1][3], 
       m[2][0], m[2][1], m[2][2], m[2][3],
       m[3][0], m[3][1], m[3][2], m[3][3]);
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movl %edx, 0x4c(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movl 0x14(%rdi), %edx
leaq 0x9135a(%rip), %rsi     # 0xa88cb
movq %r14, %rdi
xorl %eax, %eax
callq 0x118a0
movq 0x28(%r15), %rdx
testq %rdx, %rdx
je 0x17595
leaq 0x9134d(%rip), %rsi     # 0xa88d8
movq %r14, %rdi
xorl %eax, %eax
callq 0x118a0
movq 0x58(%r15), %rdx
testq %rdx, %rdx
je 0x175af
leaq 0x91340(%rip), %rsi     # 0xa88e5
movq %r14, %rdi
xorl %eax, %eax
callq 0x118a0
movq 0x30(%r15), %rdx
testq %rdx, %rdx
je 0x175c9
leaq 0x91332(%rip), %rsi     # 0xa88f1
movq %r14, %rdi
xorl %eax, %eax
callq 0x118a0
movq 0x38(%r15), %rdx
testq %rdx, %rdx
je 0x175e3
leaq 0x9132c(%rip), %rsi     # 0xa8905
movq %r14, %rdi
xorl %eax, %eax
callq 0x118a0
movq 0x40(%r15), %rdx
testq %rdx, %rdx
je 0x175fd
leaq 0x91326(%rip), %rsi     # 0xa8919
movq %r14, %rdi
xorl %eax, %eax
callq 0x118a0
movq 0x48(%r15), %rdx
testq %rdx, %rdx
je 0x17617
leaq 0x91323(%rip), %rsi     # 0xa8930
movq %r14, %rdi
xorl %eax, %eax
callq 0x118a0
movq 0x50(%r15), %rdx
testq %rdx, %rdx
je 0x17631
leaq 0x91320(%rip), %rsi     # 0xa8947
movq %r14, %rdi
xorl %eax, %eax
callq 0x118a0
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x11790
xorl %ebx, %ebx
leaq 0xb0(%rsp), %r13
movq %r15, 0x60(%rsp)
movslq 0x24(%r15), %rax
cmpq %rax, %rbx
jge 0x1779f
movq 0x18(%r15), %rax
movq (%rax,%rbx,8), %rbp
movq 0x80(%rbp), %rsi
testq %rsi, %rsi
je 0x17746
movq (%rsi), %rax
movq %r13, %rdi
callq *0x68(%rax)
movq 0x80(%rbp), %rsi
addq $0x50, %rsi
leaq 0x68(%rsp), %rdi
callq 0x4651c
movq 0x80(%rbp), %rsi
addq $0x38, %rsi
leaq 0x130(%rsp), %rdi
callq 0x4651c
movq 0x38(%rbp), %r12
movq 0x80(%rbp), %rax
movups 0x68(%rax), %xmm0
movaps %xmm0, 0x50(%rsp)
testq %r12, %r12
leaq 0x90c65(%rip), %rax     # 0xa8329
cmoveq %rax, %r12
movq %rbp, %rdi
callq 0x1561e
movsd 0xb0(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm2
movsd 0xc0(%rsp), %xmm3
movsd 0x68(%rsp), %xmm4
movsd 0x70(%rsp), %xmm5
movsd 0x78(%rsp), %xmm6
movsd 0x130(%rsp), %xmm7
movups 0x138(%rsp), %xmm8
movsd 0x98(%rbp), %xmm9
movsd %xmm9, 0x20(%rsp)
movaps 0x50(%rsp), %xmm9
movups %xmm9, 0x10(%rsp)
movups %xmm8, (%rsp)
movq %r14, %rdi
leaq 0x91223(%rip), %rsi     # 0xa895d
movq %r12, %rdx
movb $0x8, %al
callq 0x118a0
jmp 0x17797
cmpq $0x0, 0x88(%rbp)
je 0x17797
movq 0x38(%rbp), %r12
testq %r12, %r12
leaq 0x90bcb(%rip), %rax     # 0xa8329
cmoveq %rax, %r12
movq %r13, %r15
movq 0x40(%rbp), %r13
testq %r13, %r13
cmoveq %rax, %r13
movq %rbp, %rdi
callq 0x1561e
movq %r14, %rdi
leaq 0x9121a(%rip), %rsi     # 0xa899c
movq %r12, %rdx
movq %r13, %rcx
movq %r15, %r13
movq 0x60(%rsp), %r15
movb $0x1, %al
callq 0x118a0
incq %rbx
jmp 0x1764b
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x11790
leaq 0x93a1f(%rip), %rsi     # 0xab1d0
leaq 0x68(%rsp), %rdi
callq 0x648fc
xorl %r12d, %r12d
movl $0x0, 0x50(%rsp)
xorl %ebp, %ebp
movl 0x4c(%rsp), %ecx
movslq 0x14(%r15), %rax
cmpq %rax, %r12
jge 0x1798d
cmpl $0x2, %ecx
jl 0x177ed
movl %r12d, %eax
xorl %edx, %edx
divl %ecx
testl %edx, %edx
jne 0x17985
movq 0x8(%r15), %rax
movq (%rax,%r12,8), %rbx
movl 0x28(%rbx), %edx
testl %edx, %edx
jle 0x17828
movl 0x2c(%rbx), %ecx
testl %ecx, %ecx
jle 0x17828
cmpl %ebp, %edx
jne 0x1780d
cmpl 0x50(%rsp), %ecx
je 0x17828
movq %r14, %rdi
leaq 0x91197(%rip), %rsi     # 0xa89ae
xorl %eax, %eax
callq 0x118a0
movl 0x28(%rbx), %ebp
movl 0x2c(%rbx), %eax
movl %eax, 0x50(%rsp)
leaq 0x140(%rbx), %r13
movq %r13, %rdi
callq 0x649e6
testl %eax, %eax
jne 0x178b9
movq %r13, %rdi
leaq 0x68(%rsp), %rsi
callq 0x196be
testl %eax, %eax
je 0x178b9
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
callq 0x651e4
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movsd 0x80(%rsp), %xmm3
movsd 0x88(%rsp), %xmm4
movsd 0x90(%rsp), %xmm5
movsd 0x98(%rsp), %xmm6
movsd 0xa0(%rsp), %xmm7
movsd 0xa8(%rsp), %xmm8
movsd %xmm8, (%rsp)
movq %r14, %rdi
leaq 0x91114(%rip), %rsi     # 0xa89c6
movb $0x8, %al
callq 0x118a0
leaq 0x38(%rbx), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x42c50
movq 0x1e0(%rbx), %rdx
testq %rdx, %rdx
leaq 0x90a4e(%rip), %rax     # 0xa8329
cmoveq %rax, %rdx
movq 0x1d8(%rbx), %rcx
testq %rcx, %rcx
cmoveq %rax, %rcx
movsd 0xb0(%rsp), %xmm0
movsd 0xb8(%rsp), %xmm1
movsd 0xc0(%rsp), %xmm2
movsd 0xc8(%rsp), %xmm3
movsd 0xd0(%rsp), %xmm4
movsd 0xd8(%rsp), %xmm5
movsd 0xe0(%rsp), %xmm6
movsd 0xe8(%rsp), %xmm7
movups 0xf0(%rsp), %xmm8
movups 0x100(%rsp), %xmm9
movups 0x110(%rsp), %xmm10
movups 0x120(%rsp), %xmm11
movups %xmm11, 0x30(%rsp)
movups %xmm10, 0x20(%rsp)
movups %xmm9, 0x10(%rsp)
movups %xmm8, (%rsp)
movq %r14, %rdi
leaq 0x9107d(%rip), %rsi     # 0xa89f7
movb $0x8, %al
callq 0x118a0
movl 0x4c(%rsp), %ecx
incq %r12
jmp 0x177cc
pushq $0x1
popq %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq