// File: /workspace/github_cpp_makefile_star2_O3/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 $0x138, %rsp            # imm = 0x138
movl %edx, 0x54(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movl 0x14(%rdi), %edx
leaq 0xa3950(%rip), %rsi     # 0xbb94b
movq %r14, %rdi
xorl %eax, %eax
callq 0x11880
movq 0x28(%r15), %rdx
testq %rdx, %rdx
je 0x1801f
leaq 0xa3943(%rip), %rsi     # 0xbb958
movq %r14, %rdi
xorl %eax, %eax
callq 0x11880
movq 0x58(%r15), %rdx
testq %rdx, %rdx
je 0x18039
leaq 0xa3936(%rip), %rsi     # 0xbb965
movq %r14, %rdi
xorl %eax, %eax
callq 0x11880
movq 0x30(%r15), %rdx
testq %rdx, %rdx
je 0x18053
leaq 0xa3928(%rip), %rsi     # 0xbb971
movq %r14, %rdi
xorl %eax, %eax
callq 0x11880
movq 0x38(%r15), %rdx
testq %rdx, %rdx
je 0x1806d
leaq 0xa3922(%rip), %rsi     # 0xbb985
movq %r14, %rdi
xorl %eax, %eax
callq 0x11880
movq 0x40(%r15), %rdx
testq %rdx, %rdx
je 0x18087
leaq 0xa391c(%rip), %rsi     # 0xbb999
movq %r14, %rdi
xorl %eax, %eax
callq 0x11880
movq 0x48(%r15), %rdx
testq %rdx, %rdx
je 0x180a1
leaq 0xa3919(%rip), %rsi     # 0xbb9b0
movq %r14, %rdi
xorl %eax, %eax
callq 0x11880
movq 0x50(%r15), %rdx
testq %rdx, %rdx
je 0x180bb
leaq 0xa3916(%rip), %rsi     # 0xbb9c7
movq %r14, %rdi
xorl %eax, %eax
callq 0x11880
movl $0xa, %edi
movq %r14, %rsi
callq 0x11790
cmpl $0x0, 0x24(%r15)
leaq 0xa3255(%rip), %rbp     # 0xbb329
jle 0x18252
xorl %r12d, %r12d
leaq 0xa38f9(%rip), %r13     # 0xbb9dd
movq 0x18(%r15), %rax
movq (%rax,%r12,8), %rbx
movq 0x80(%rbx), %rsi
testq %rsi, %rsi
je 0x1815c
movq (%rsi), %rax
leaq 0xa0(%rsp), %rdi
callq *0x68(%rax)
movq 0x80(%rbx), %rsi
addq $0x50, %rsi
leaq 0x58(%rsp), %rdi
callq 0x4c0a6
movq 0x80(%rbx), %rsi
addq $0x38, %rsi
leaq 0x120(%rsp), %rdi
callq 0x4c0a6
movq 0x38(%rbx), %rdx
testq %rdx, %rdx
cmoveq %rbp, %rdx
movl 0x28(%rbx), %eax
testl %eax, %eax
je 0x181a5
movsd 0x60(%rbx), %xmm0
subsd 0x50(%rbx), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
jmp 0x181a9
cmpq $0x0, 0x88(%rbx)
je 0x18242
movq 0x38(%rbx), %rdx
testq %rdx, %rdx
cmoveq %rbp, %rdx
movq 0x40(%rbx), %rcx
testq %rcx, %rcx
cmoveq %rbp, %rcx
movl 0x28(%rbx), %eax
testl %eax, %eax
je 0x1822d
movsd 0x60(%rbx), %xmm0
subsd 0x50(%rbx), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
jmp 0x18231
xorpd %xmm0, %xmm0
movq 0x80(%rbx), %rax
movsd 0x68(%rax), %xmm8
movsd 0x70(%rax), %xmm9
movsd 0xa0(%rsp), %xmm1
movsd 0xa8(%rsp), %xmm2
movsd 0xb0(%rsp), %xmm3
movsd 0x58(%rsp), %xmm4
movsd 0x60(%rsp), %xmm5
movsd 0x68(%rsp), %xmm6
movsd 0x120(%rsp), %xmm7
movups 0x128(%rsp), %xmm10
movsd 0x98(%rbx), %xmm11
movsd %xmm11, 0x20(%rsp)
movsd %xmm9, 0x18(%rsp)
movsd %xmm8, 0x10(%rsp)
movups %xmm10, (%rsp)
movq %r14, %rdi
movq %r13, %rsi
movb $0x8, %al
callq 0x11880
jmp 0x18242
xorpd %xmm0, %xmm0
movq %r14, %rdi
leaq 0xa37e1(%rip), %rsi     # 0xbba1c
movb $0x1, %al
callq 0x11880
incq %r12
movslq 0x24(%r15), %rax
cmpq %rax, %r12
jl 0x180e4
movl $0xa, %edi
movq %r14, %rsi
callq 0x11790
leaq 0xa605a(%rip), %rsi     # 0xbe2c0
leaq 0x58(%rsp), %rdi
callq 0x6d1a2
movl 0x14(%r15), %ecx
testl %ecx, %ecx
movl 0x54(%rsp), %esi
jle 0x18450
xorl %r13d, %r13d
movl $0x0, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
cmpl $0x2, %esi
jl 0x182a7
movl %r13d, %eax
xorl %edx, %edx
divl %esi
testl %edx, %edx
jne 0x18441
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %rbx
movl 0x28(%rbx), %edx
testl %edx, %edx
jle 0x182e8
movl 0x2c(%rbx), %ecx
testl %ecx, %ecx
jle 0x182e8
cmpl 0x50(%rsp), %edx
jne 0x182c9
cmpl 0x4c(%rsp), %ecx
je 0x182e8
movq %r14, %rdi
leaq 0xa375b(%rip), %rsi     # 0xbba2e
xorl %eax, %eax
callq 0x11880
movl 0x28(%rbx), %eax
movl %eax, 0x50(%rsp)
movl 0x2c(%rbx), %eax
movl %eax, 0x4c(%rsp)
leaq 0x140(%rbx), %r12
movq %r12, %rdi
callq 0x6d28c
testl %eax, %eax
jne 0x18378
movq %r12, %rdi
leaq 0x58(%rsp), %rsi
movl $0x48, %edx
callq 0x77d9b
testl %eax, %eax
je 0x18378
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0x6dbec
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movsd 0x70(%rsp), %xmm3
movsd 0x78(%rsp), %xmm4
movsd 0x80(%rsp), %xmm5
movsd 0x88(%rsp), %xmm6
movsd 0x90(%rsp), %xmm7
movsd 0x98(%rsp), %xmm8
movsd %xmm8, (%rsp)
movq %r14, %rdi
leaq 0xa36d5(%rip), %rsi     # 0xbba46
movb $0x8, %al
callq 0x11880
leaq 0x38(%rbx), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x47efe
movq 0x1e0(%rbx), %rdx
testq %rdx, %rdx
cmoveq %rbp, %rdx
movq 0x1d8(%rbx), %rcx
testq %rcx, %rcx
cmoveq %rbp, %rcx
movsd 0xa0(%rsp), %xmm0
movsd 0xa8(%rsp), %xmm1
movsd 0xb0(%rsp), %xmm2
movsd 0xb8(%rsp), %xmm3
movsd 0xc0(%rsp), %xmm4
movsd 0xc8(%rsp), %xmm5
movsd 0xd0(%rsp), %xmm6
movsd 0xd8(%rsp), %xmm7
movups 0xe0(%rsp), %xmm8
movups 0xf0(%rsp), %xmm9
movups 0x100(%rsp), %xmm10
movups 0x110(%rsp), %xmm11
movups %xmm11, 0x30(%rsp)
movups %xmm10, 0x20(%rsp)
movups %xmm9, 0x10(%rsp)
movups %xmm8, (%rsp)
movq %r14, %rdi
leaq 0xa3645(%rip), %rsi     # 0xbba77
movb $0x8, %al
callq 0x11880
movl 0x14(%r15), %ecx
movl 0x54(%rsp), %esi
incq %r13
movslq %ecx, %rax
cmpq %rax, %r13
jl 0x18293
movl $0x1, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop