// File: /workspace/github_cpp_makefile_star2_O3/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 0xa4681(%rip), %rsi     # 0xbdc4a
movq %r14, %rdi
callq 0x11700
testq %rax, %rax
je 0x1961f
movq %rax, %rbx
cmpl $0x0, 0x14(%r13)
jle 0x19637
movq 0x8(%r13), %rax
movq (%rax), %rax
movsd 0x140(%rax), %xmm1
leaq 0xeb6e2(%rip), %rcx     # 0x104cd8
movsd (%rcx), %xmm0
movapd 0xa1a4e(%rip), %xmm2    # 0xbb050
xorpd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jb 0x1963d
ucomisd %xmm1, %xmm0
jb 0x1963d
leaq 0xeb6cf(%rip), %rax     # 0x104ce8
movsd (%rax), %xmm0
jmp 0x19656
leaq 0xa2543(%rip), %rdi     # 0xbbb69
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x77f42
jmp 0x19991
xorpd %xmm0, %xmm0
jmp 0x1965a
xorps %xmm0, %xmm0
cvtsi2sdl 0x28(%rax), %xmm0
mulsd 0xa19d3(%rip), %xmm0    # 0xbb020
divsd %xmm1, %xmm0
callq 0x114c0
addsd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xa2650(%rip), %rdi     # 0xbbcb7
movl $0x2, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11b80
leaq 0xa2608(%rip), %rsi     # 0xbbc88
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x11880
leaq 0xa260d(%rip), %rdi     # 0xbbca4
movl $0xe, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11b80
movl 0x14(%r13), %ecx
testl %ecx, %ecx
jle 0x19952
leaq 0xa51ac(%rip), %rax     # 0xbe868
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0xa2656(%rip), %r15     # 0xbbd23
xorl %ebp, %ebp
movl %r12d, 0x8(%rsp)
movq %r13, 0x18(%rsp)
cmpl $0x2, %r12d
jl 0x196ee
movl %ebp, %eax
xorl %edx, %edx
divl %r12d
testl %edx, %edx
jne 0x19943
movq 0x8(%r13), %rax
movq (%rax,%rbp,8), %r14
cmpq $0x0, 0x1d8(%r14)
je 0x19943
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 0x19744
movb $0x0, (%rax)
movl $0x6, %esi
movl $0x1, %edx
leaq 0xa255e(%rip), %rdi     # 0xbbcb3
movq %rbx, %rcx
callq 0x11b80
movq 0x30(%r13), %rdx
movq %rbx, %rdi
testq %rdx, %rdx
je 0x19781
leaq 0xa2564(%rip), %rsi     # 0xbbcd4
leaq 0x120(%rsp), %rcx
xorl %eax, %eax
callq 0x11880
jmp 0x19797
leaq 0xa2532(%rip), %rsi     # 0xbbcba
leaq 0x120(%rsp), %rdx
xorl %eax, %eax
callq 0x11880
leaq 0x20(%rsp), %r12
movq %r12, %rdi
leaq 0xa3662(%rip), %rsi     # 0xbce08
callq 0x47efe
movq %r12, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x492be
addq $0x38, %r14
leaq 0xa0(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x49eef
movq %r12, %rdi
movq %r13, %rsi
callq 0x49ae2
movl $0x1a, %esi
movl $0x1, %edx
leaq 0xa2502(%rip), %rdi     # 0xbbcf1
movq %rbx, %rcx
callq 0x11b80
movl $0x9, %esi
movl $0x1, %edx
leaq 0xa2504(%rip), %rdi     # 0xbbd0c
movq %rbx, %rcx
callq 0x11b80
movq %r12, %r14
xorl %r13d, %r13d
movl $0x1, %r12d
movl $0xc, %esi
movl $0x1, %edx
leaq 0xa24e9(%rip), %rdi     # 0xbbd16
movq %rbx, %rcx
callq 0x11b80
movq %r13, %rax
shlq $0x5, %rax
movsd 0x20(%rsp,%rax), %xmm0
movq %rbx, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x11880
movl $0x2c, %edi
movq %rbx, %rsi
callq 0x11790
movl $0xa, %edi
movq %rbx, %rsi
callq 0x11790
movsd (%r14,%r12,8), %xmm0
movq %rbx, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x11880
incq %r12
cmpq $0x4, %r12
jne 0x1984f
movl $0xa, %edi
movq %rbx, %rsi
callq 0x11790
movl $0xb, %esi
movl $0x1, %edx
leaq 0xa2492(%rip), %rdi     # 0xbbd35
movq %rbx, %rcx
callq 0x11b80
cmpq $0x3, %r13
je 0x198d7
movl $0x2c, %edi
movq %rbx, %rsi
callq 0x11790
movl $0xa, %edi
movq %rbx, %rsi
callq 0x11790
incq %r13
addq $0x20, %r14
jmp 0x19816
movl $0xa, %edi
movq %rbx, %rsi
callq 0x11790
movl $0x9, %esi
movl $0x1, %edx
leaq 0xa2442(%rip), %rdi     # 0xbbd37
movq %rbx, %rcx
callq 0x11b80
movq 0x18(%rsp), %r13
movslq 0x14(%r13), %rax
decq %rax
cmpq %rax, %rbp
movl 0x8(%rsp), %r12d
jge 0x19926
movl $0x7, %esi
movl $0x1, %edx
leaq 0xa241d(%rip), %rdi     # 0xbbd41
jmp 0x19937
movl $0x6, %esi
movl $0x1, %edx
leaq 0xa2412(%rip), %rdi     # 0xbbd49
movq %rbx, %rcx
callq 0x11b80
movl 0x14(%r13), %ecx
incq %rbp
movslq %ecx, %rax
cmpq %rax, %rbp
jl 0x196d9
leaq 0xa23f7(%rip), %rdi     # 0xbbd50
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11b80
leaq 0xa23db(%rip), %rdi     # 0xbbd4d
movl $0x2, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11b80
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