// File: /workspace/github_cpp_makefile_star2_O1/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 0xa567e(%rip), %rsi     # 0xbd94b
movq %r14, %rdi
xorl %eax, %eax
callq 0x11890
movq 0x28(%r15), %rdx
testq %rdx, %rdx
je 0x182f1
leaq 0xa5671(%rip), %rsi     # 0xbd958
movq %r14, %rdi
xorl %eax, %eax
callq 0x11890
movq 0x58(%r15), %rdx
testq %rdx, %rdx
je 0x1830b
leaq 0xa5664(%rip), %rsi     # 0xbd965
movq %r14, %rdi
xorl %eax, %eax
callq 0x11890
movq 0x30(%r15), %rdx
testq %rdx, %rdx
je 0x18325
leaq 0xa5656(%rip), %rsi     # 0xbd971
movq %r14, %rdi
xorl %eax, %eax
callq 0x11890
movq 0x38(%r15), %rdx
testq %rdx, %rdx
je 0x1833f
leaq 0xa5650(%rip), %rsi     # 0xbd985
movq %r14, %rdi
xorl %eax, %eax
callq 0x11890
movq 0x40(%r15), %rdx
testq %rdx, %rdx
je 0x18359
leaq 0xa564a(%rip), %rsi     # 0xbd999
movq %r14, %rdi
xorl %eax, %eax
callq 0x11890
movq 0x48(%r15), %rdx
testq %rdx, %rdx
je 0x18373
leaq 0xa5647(%rip), %rsi     # 0xbd9b0
movq %r14, %rdi
xorl %eax, %eax
callq 0x11890
movq 0x50(%r15), %rdx
testq %rdx, %rdx
je 0x1838d
leaq 0xa5644(%rip), %rsi     # 0xbd9c7
movq %r14, %rdi
xorl %eax, %eax
callq 0x11890
movl $0xa, %edi
movq %r14, %rsi
callq 0x117a0
cmpl $0x0, 0x24(%r15)
leaq 0xa4f86(%rip), %rbp     # 0xbd32c
jle 0x18524
xorl %r12d, %r12d
leaq 0xa5627(%rip), %r13     # 0xbd9dd
movq 0x18(%r15), %rax
movq (%rax,%r12,8), %rbx
movq 0x80(%rbx), %rsi
testq %rsi, %rsi
je 0x1842e
movq (%rsi), %rax
leaq 0xa0(%rsp), %rdi
callq *0x68(%rax)
movq 0x80(%rbx), %rsi
addq $0x50, %rsi
leaq 0x58(%rsp), %rdi
callq 0x4c8ee
movq 0x80(%rbx), %rsi
addq $0x38, %rsi
leaq 0x120(%rsp), %rdi
callq 0x4c8ee
movq 0x38(%rbx), %rdx
testq %rdx, %rdx
cmoveq %rbp, %rdx
movl 0x28(%rbx), %eax
testl %eax, %eax
je 0x18477
movsd 0x60(%rbx), %xmm0
subsd 0x50(%rbx), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
jmp 0x1847b
cmpq $0x0, 0x88(%rbx)
je 0x18514
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 0x184ff
movsd 0x60(%rbx), %xmm0
subsd 0x50(%rbx), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
jmp 0x18503
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 0x11890
jmp 0x18514
xorpd %xmm0, %xmm0
movq %r14, %rdi
leaq 0xa550f(%rip), %rsi     # 0xbda1c
movb $0x1, %al
callq 0x11890
incq %r12
movslq 0x24(%r15), %rax
cmpq %rax, %r12
jl 0x183b6
movl $0xa, %edi
movq %r14, %rsi
callq 0x117a0
leaq 0xa7d70(%rip), %rsi     # 0xc02a8
leaq 0x58(%rsp), %rdi
callq 0x6dfb2
cmpl $0x0, 0x14(%r15)
movl 0x54(%rsp), %ecx
jle 0x1871e
xorl %r13d, %r13d
movl $0x0, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
cmpl $0x2, %ecx
jl 0x18578
movl %r13d, %eax
xorl %edx, %edx
divl %ecx
testl %edx, %edx
jne 0x1870e
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %rbx
movl 0x28(%rbx), %edx
testl %edx, %edx
jle 0x185b9
movl 0x2c(%rbx), %ecx
testl %ecx, %ecx
jle 0x185b9
cmpl 0x50(%rsp), %edx
jne 0x1859a
cmpl 0x4c(%rsp), %ecx
je 0x185b9
movq %r14, %rdi
leaq 0xa548a(%rip), %rsi     # 0xbda2e
xorl %eax, %eax
callq 0x11890
movl 0x28(%rbx), %eax
movl %eax, 0x50(%rsp)
movl 0x2c(%rbx), %eax
movl %eax, 0x4c(%rsp)
leaq 0x140(%rbx), %r12
movq %r12, %rdi
callq 0x6e09c
testl %eax, %eax
jne 0x18649
movq %r12, %rdi
leaq 0x58(%rsp), %rsi
movl $0x48, %edx
callq 0x78ee7
testl %eax, %eax
je 0x18649
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0x6e9fc
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 0xa5404(%rip), %rsi     # 0xbda46
movb $0x8, %al
callq 0x11890
leaq 0x38(%rbx), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x48742
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 0xa5374(%rip), %rsi     # 0xbda77
movb $0x8, %al
callq 0x11890
movl 0x54(%rsp), %ecx
incq %r13
movslq 0x14(%r15), %rax
cmpq %rax, %r13
jl 0x18564
movl $0x1, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop