// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/RGBD/RGBDConfiguration.cpp
// Function: gaps::RGBDConfiguration::WriteNeRFFile(char const*, int) const
int RGBDConfiguration::
WriteNeRFFile(const char *filename, int write_every_kth_image) const
{
  // Open file
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    RNFail("Unable to open NeRF file %s\n", filename);
    return 0;
  }

  // Write header
  double xfov = (NImages() > 0) ? Image(0)->XFov() : 0;
  fprintf(fp, "{\n");
  fprintf(fp, "  \"camera_angle_x\": %.12f,\n", 2 * xfov);
  fprintf(fp, "  \"frames\": [\n");

  // Write images
  for (int i = 0; i < NImages(); i++) {
    if ((write_every_kth_image > 1) && ((i % write_every_kth_image) != 0)) continue;
    RGBDImage *image = Image(i);
    if (!image->ColorFilename()) continue;

    // Get filename
    char filename[1024] = { '\0' };
    strncpy(filename, image->ColorFilename(), 1023);
    char *endp = strrchr(filename, '.');
    if (endp) *endp = '\0';

    // Print opening curly bracket for image
    fprintf(fp, "    {\n");

    // Print file path
    if (!color_directory) fprintf(fp, "      \"file_path\": \"%s\",\n", filename);
    else fprintf(fp, "      \"file_path\": \"%s/%s\",\n", color_directory, filename);

    // Print rotation (what is this?)
    // XXX
    
    // Print transformation matrix
    R4Matrix m = R4identity_matrix;
    m.XRotate(RN_PI_OVER_TWO);
    m = m * image->CameraToWorld().Matrix();
    fprintf(fp, "      \"transform_matrix\":\n");
    fprintf(fp, "        [");
    for (int j = 0; j < 4; j++) {
      fprintf(fp, "          [\n");
      for (int k = 0; k < 4; k++) {
        fprintf(fp, "            %.12f", m[j][k]);
        if (k < 3) fprintf(fp, ",");
        fprintf(fp, "\n");
      }
      fprintf(fp, "          ]");
      if (j < 3) fprintf(fp, ",");
      fprintf(fp, "\n");
    }
    fprintf(fp, "        ]");

    // Print trailing curly bracket for image
    if (i < NImages()-1) fprintf(fp, "    },\n");
    else fprintf(fp, "    }\n");
  }
  
  // Write trailer
  fprintf(fp, "  ]\n");
  fprintf(fp, "}\n");
  
  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x528, %rsp            # imm = 0x528
movl %edx, %r12d
movq %rsi, %r14
movq %rdi, %r13
leaq 0xa6369(%rip), %rsi     # 0xbfc4a
movq %r14, %rdi
callq 0x11710
testq %rax, %rax
je 0x19937
movq %rax, %rbx
cmpl $0x0, 0x14(%r13)
jle 0x1994f
movq 0x8(%r13), %rax
movq (%rax), %rax
movsd 0x140(%rax), %xmm1
leaq 0xed3da(%rip), %rcx     # 0x106ce8
movsd (%rcx), %xmm0
movapd 0xa3736(%rip), %xmm2    # 0xbd050
xorpd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jb 0x19955
ucomisd %xmm1, %xmm0
jb 0x19955
leaq 0xed3c7(%rip), %rax     # 0x106cf8
movsd (%rax), %xmm0
jmp 0x1996e
leaq 0xa422b(%rip), %rdi     # 0xbdb69
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x79092
jmp 0x19c79
xorpd %xmm0, %xmm0
jmp 0x19972
xorps %xmm0, %xmm0
cvtsi2sdl 0x28(%rax), %xmm0
mulsd 0xa36bb(%rip), %xmm0    # 0xbd020
divsd %xmm1, %xmm0
callq 0x114c0
addsd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xa4338(%rip), %rdi     # 0xbdcb7
movl $0x2, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11b90
leaq 0xa42f0(%rip), %rsi     # 0xbdc88
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x11890
leaq 0xa42f5(%rip), %rdi     # 0xbdca4
movl $0xe, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11b90
cmpl $0x0, 0x14(%r13)
jle 0x19c3a
leaq 0xa6e85(%rip), %rax     # 0xc0858
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0xa433f(%rip), %r15     # 0xbdd23
xorl %ebp, %ebp
movl %r12d, 0x8(%rsp)
movq %r13, 0x18(%rsp)
cmpl $0x2, %r12d
jl 0x19a05
movl %ebp, %eax
xorl %edx, %edx
divl %r12d
testl %edx, %edx
jne 0x19c2a
movq 0x8(%r13), %rax
movq (%rax,%rbp,8), %r14
cmpq $0x0, 0x1d8(%r14)
je 0x19c2a
movl $0x400, %edx            # imm = 0x400
leaq 0x120(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x11310
movq 0x1d8(%r14), %rsi
movl $0x3ff, %edx            # imm = 0x3FF
movq %r12, %rdi
callq 0x112d0
movq %r12, %rdi
movl $0x2e, %esi
callq 0x11640
testq %rax, %rax
je 0x19a5b
movb $0x0, (%rax)
movl $0x6, %esi
movl $0x1, %edx
leaq 0xa4247(%rip), %rdi     # 0xbdcb3
movq %rbx, %rcx
callq 0x11b90
movq 0x30(%r13), %rdx
movq %rbx, %rdi
testq %rdx, %rdx
je 0x19a98
leaq 0xa424d(%rip), %rsi     # 0xbdcd4
leaq 0x120(%rsp), %rcx
xorl %eax, %eax
callq 0x11890
jmp 0x19aae
leaq 0xa421b(%rip), %rsi     # 0xbdcba
leaq 0x120(%rsp), %rdx
xorl %eax, %eax
callq 0x11890
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
leaq 0xa5348(%rip), %rsi     # 0xbee08
callq 0x48742
movq %r12, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x49b02
addq $0x38, %r14
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x4a733
movq %r12, %rdi
movq %r13, %rsi
callq 0x4a326
movl $0x1a, %esi
movl $0x1, %edx
leaq 0xa41eb(%rip), %rdi     # 0xbdcf1
movq %rbx, %rcx
callq 0x11b90
movl $0x9, %esi
movl $0x1, %edx
leaq 0xa41ed(%rip), %rdi     # 0xbdd0c
movq %rbx, %rcx
callq 0x11b90
movq %r12, %r14
xorl %r13d, %r13d
movl $0xc, %esi
movl $0x1, %edx
leaq 0xa41d8(%rip), %rdi     # 0xbdd16
movq %rbx, %rcx
callq 0x11b90
xorl %r12d, %r12d
movsd (%r14,%r12,8), %xmm0
movq %rbx, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x11890
cmpq $0x3, %r12
je 0x19b6f
movl $0x2c, %edi
movq %rbx, %rsi
callq 0x117a0
movl $0xa, %edi
movq %rbx, %rsi
callq 0x117a0
incq %r12
cmpq $0x4, %r12
jne 0x19b49
movl $0xb, %esi
movl $0x1, %edx
leaq 0xa419f(%rip), %rdi     # 0xbdd35
movq %rbx, %rcx
callq 0x11b90
cmpq $0x3, %r13
je 0x19bb1
movl $0x2c, %edi
movq %rbx, %rsi
callq 0x117a0
movl $0xa, %edi
movq %rbx, %rsi
callq 0x117a0
incq %r13
addq $0x20, %r14
cmpq $0x4, %r13
jne 0x19b2d
movl $0x9, %esi
movl $0x1, %edx
leaq 0xa4157(%rip), %rdi     # 0xbdd37
movq %rbx, %rcx
callq 0x11b90
movq 0x18(%rsp), %r13
movslq 0x14(%r13), %rax
decq %rax
cmpq %rax, %rbp
jge 0x19c0c
movl $0x7, %esi
movl $0x1, %edx
leaq 0xa4137(%rip), %rdi     # 0xbdd41
jmp 0x19c1d
movl $0x6, %esi
movl $0x1, %edx
leaq 0xa412c(%rip), %rdi     # 0xbdd49
movq %rbx, %rcx
callq 0x11b90
movl 0x8(%rsp), %r12d
incq %rbp
movslq 0x14(%r13), %rax
cmpq %rax, %rbp
jl 0x199f0
leaq 0xa410f(%rip), %rdi     # 0xbdd50
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11b90
leaq 0xa40f3(%rip), %rdi     # 0xbdd4d
movl $0x2, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11b90
movq %rbx, %rdi
callq 0x115a0
movl $0x1, %ebx
movl %ebx, %eax
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop