// File: /workspace/github_cpp_makefile_star2_O2/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, %r15d
movq %rsi, %r14
movq %rdi, %r12
leaq 0x92355(%rip), %rsi     # 0xaab5a
movq %r14, %rdi
callq 0x11700
testq %rax, %rax
je 0x18835
movq %rax, %rbx
cmpl $0x0, 0x14(%r12)
movq %r12, 0x10(%rsp)
jle 0x1884d
movq 0x8(%r12), %rax
movq (%rax), %rdi
callq 0x19758
addsd %xmm0, %xmm0
jmp 0x18851
leaq 0x902ad(%rip), %rdi     # 0xa8ae9
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x6e732
jmp 0x18b39
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0x903da(%rip), %rdi     # 0xa8c37
pushq $0x2
popq %rsi
pushq $0x1
popq %r13
movq %r13, %rdx
movq %rbx, %rcx
callq 0x11bb0
leaq 0x90392(%rip), %rsi     # 0xa8c08
movq %rbx, %rdi
movsd (%rsp), %xmm0
movb $0x1, %al
callq 0x118a0
leaq 0x90398(%rip), %rdi     # 0xa8c24
pushq $0xe
popq %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x11bb0
leaq 0x92ed7(%rip), %rax     # 0xab778
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
pushq $0xa
popq %r12
leaq 0x903ed(%rip), %rbp     # 0xa8ca3
xorl %esi, %esi
movl %r15d, 0xc(%rsp)
movq 0x10(%rsp), %rcx
movslq 0x14(%rcx), %rax
cmpq %rax, %rsi
jge 0x18b00
cmpl $0x2, %r15d
jl 0x188e4
movl %esi, %eax
xorl %edx, %edx
divl %r15d
testl %edx, %edx
jne 0x18af8
movq 0x8(%rcx), %rax
movq (%rax,%rsi,8), %r14
cmpq $0x0, 0x1d8(%r14)
je 0x18af8
movq %rsi, (%rsp)
movl $0x400, %edx            # imm = 0x400
leaq 0x120(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x11310
movq 0x1d8(%r14), %rsi
movl $0x3ff, %edx            # imm = 0x3FF
movq %r15, %rdi
callq 0x112e0
movq %r15, %rdi
pushq $0x2e
popq %rsi
callq 0x11650
testq %rax, %rax
je 0x1893c
movb $0x0, (%rax)
leaq 0x902f0(%rip), %rdi     # 0xa8c33
pushq $0x6
popq %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x11bb0
movq 0x10(%rsp), %rax
movq 0x30(%rax), %rdx
movq %rbx, %rdi
testq %rdx, %rdx
je 0x1897a
leaq 0x902eb(%rip), %rsi     # 0xa8c54
leaq 0x120(%rsp), %rcx
xorl %eax, %eax
callq 0x118a0
jmp 0x18990
leaq 0x902b9(%rip), %rsi     # 0xa8c3a
leaq 0x120(%rsp), %rdx
xorl %eax, %eax
callq 0x118a0
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
leaq 0x91396(%rip), %rsi     # 0xa9d38
callq 0x42c50
movq %r15, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x43cdc
addq $0x38, %r14
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x446e5
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x442d8
leaq 0x90294(%rip), %rdi     # 0xa8c71
pushq $0x1a
popq %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x11bb0
leaq 0x9029a(%rip), %rdi     # 0xa8c8c
pushq $0x9
popq %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x11bb0
movq %r15, %r14
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0x18aa3
leaq 0x9027f(%rip), %rdi     # 0xa8c96
pushq $0xc
popq %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x11bb0
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x18a62
movsd (%r14,%r13,8), %xmm0
movq %rbx, %rdi
movq %rbp, %rsi
movb $0x1, %al
callq 0x118a0
cmpq $0x3, %r13
je 0x18a52
pushq $0x2c
popq %rdi
movq %rbx, %rsi
callq 0x11790
movl %r12d, %edi
movq %rbx, %rsi
callq 0x11790
incq %r13
jmp 0x18a28
leaq 0x9024c(%rip), %rdi     # 0xa8cb5
pushq $0xb
popq %rsi
pushq $0x1
popq %r13
movq %r13, %rdx
movq %rbx, %rcx
callq 0x11bb0
cmpq $0x3, %r15
je 0x18a8c
pushq $0x2c
popq %rdi
movq %rbx, %rsi
callq 0x11790
movl %r12d, %edi
movq %rbx, %rsi
callq 0x11790
incq %r15
addq $0x20, %r14
jmp 0x18a06
leaq 0x9020d(%rip), %rdi     # 0xa8cb7
pushq $0x9
popq %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x11bb0
movq 0x10(%rsp), %rax
movslq 0x14(%rax), %rax
decq %rax
cmpq %rax, (%rsp)
jge 0x18ad5
leaq 0x901f0(%rip), %rdi     # 0xa8cc1
pushq $0x7
jmp 0x18ade
leaq 0x901ed(%rip), %rdi     # 0xa8cc9
pushq $0x6
popq %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x11bb0
movl 0xc(%rsp), %r15d
movq 0x10(%rsp), %rcx
movq (%rsp), %rsi
incq %rsi
jmp 0x188c2
leaq 0x901c9(%rip), %rdi     # 0xa8cd0
pushq $0x4
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
movq %rbx, %rcx
callq 0x11bb0
leaq 0x901ad(%rip), %rdi     # 0xa8ccd
pushq $0x2
popq %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x11bb0
movq %rbx, %rdi
callq 0x115b0
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop