// File: 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:
subq $0x588, %rsp            # imm = 0x588
movq %rdi, 0x578(%rsp)
movq %rsi, 0x570(%rsp)
movl %edx, 0x56c(%rsp)
movq 0x578(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x570(%rsp), %rdi
leaq 0x1480e0(%rip), %rsi    # 0x1688aa
callq 0x12860
movq %rax, 0x560(%rsp)
cmpq $0x0, 0x560(%rsp)
jne 0x20808
movq 0x570(%rsp), %rsi
leaq 0x146360(%rip), %rdi    # 0x166b51
movb $0x0, %al
callq 0xdf990
movl $0x0, 0x584(%rsp)
jmp 0x20c60
movq 0x28(%rsp), %rdi
callq 0x19c40
cmpl $0x0, %eax
jle 0x20833
movq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x19c90
movq %rax, %rdi
callq 0x229b0
movsd %xmm0, 0x20(%rsp)
jmp 0x2083e
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x2083e
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x558(%rsp)
movq 0x560(%rsp), %rdi
leaq 0x146443(%rip), %rsi    # 0x166c9f
movb $0x0, %al
callq 0x12a30
movq 0x560(%rsp), %rdi
movsd 0x145ce5(%rip), %xmm0   # 0x166558
mulsd 0x558(%rsp), %xmm0
leaq 0x1463ed(%rip), %rsi    # 0x166c70
movb $0x1, %al
callq 0x12a30
movq 0x560(%rsp), %rdi
leaq 0x1463f3(%rip), %rsi    # 0x166c8c
movb $0x0, %al
callq 0x12a30
movl $0x0, 0x554(%rsp)
movq 0x28(%rsp), %rdi
movl 0x554(%rsp), %eax
movl %eax, 0x1c(%rsp)
callq 0x19c40
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x20c1c
cmpl $0x1, 0x56c(%rsp)
jle 0x208f1
movl 0x554(%rsp), %eax
cltd
idivl 0x56c(%rsp)
cmpl $0x0, %edx
je 0x208f1
jmp 0x20c06
movq 0x28(%rsp), %rdi
movl 0x554(%rsp), %esi
callq 0x19c90
movq %rax, 0x548(%rsp)
movq 0x548(%rsp), %rdi
callq 0x22740
cmpq $0x0, %rax
jne 0x20922
jmp 0x20c06
leaq 0x140(%rsp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x12370
leaq 0x140(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x548(%rsp), %rdi
callq 0x22740
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movl $0x3ff, %edx            # imm = 0x3FF
callq 0x12350
leaq 0x140(%rsp), %rdi
movl $0x2e, %esi
callq 0x127a0
movq %rax, 0x138(%rsp)
cmpq $0x0, 0x138(%rsp)
je 0x20992
movq 0x138(%rsp), %rax
movb $0x0, (%rax)
movq 0x560(%rsp), %rdi
leaq 0x1462fa(%rip), %rsi    # 0x166c9b
movb $0x0, %al
callq 0x12a30
movq 0x28(%rsp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x209d4
movq 0x560(%rsp), %rdi
leaq 0x140(%rsp), %rdx
leaq 0x1462d7(%rip), %rsi    # 0x166ca2
movb $0x0, %al
callq 0x12a30
jmp 0x209fb
movq 0x28(%rsp), %rax
movq 0x560(%rsp), %rdi
movq 0x30(%rax), %rdx
leaq 0x140(%rsp), %rcx
leaq 0x1462c8(%rip), %rsi    # 0x166cbc
movb $0x0, %al
callq 0x12a30
leaq 0xb8(%rsp), %rdi
leaq 0x191f66(%rip), %rsi    # 0x1b2970
callq 0x84270
leaq 0x148afa(%rip), %rax    # 0x169510
movsd (%rax), %xmm0
leaq 0xb8(%rsp), %rdi
callq 0x85e40
movq 0x548(%rsp), %rdi
callq 0x22710
movq %rax, %rdi
callq 0x19ed0
movq %rax, %rdx
leaq 0x38(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x875b0
leaq 0xb8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x86970
movq 0x560(%rsp), %rdi
leaq 0x146267(%rip), %rsi    # 0x166cd9
movb $0x0, %al
callq 0x12a30
movq 0x560(%rsp), %rdi
leaq 0x14626c(%rip), %rsi    # 0x166cf4
movb $0x0, %al
callq 0x12a30
movl $0x0, 0x34(%rsp)
cmpl $0x4, 0x34(%rsp)
jge 0x20b9e
movq 0x560(%rsp), %rdi
leaq 0x14624d(%rip), %rsi    # 0x166cfe
movb $0x0, %al
callq 0x12a30
movl $0x0, 0x30(%rsp)
cmpl $0x4, 0x30(%rsp)
jge 0x20b45
movq 0x560(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x34(%rsp), %esi
leaq 0xb8(%rsp), %rdi
callq 0x19ba0
movq 0x8(%rsp), %rdi
movslq 0x30(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x146210(%rip), %rsi    # 0x166d0b
movb $0x1, %al
callq 0x12a30
cmpl $0x3, 0x30(%rsp)
jge 0x20b1f
movq 0x560(%rsp), %rdi
leaq 0x146dd1(%rip), %rsi    # 0x1678e9
movb $0x0, %al
callq 0x12a30
movq 0x560(%rsp), %rdi
leaq 0x14c414(%rip), %rsi    # 0x16cf42
movb $0x0, %al
callq 0x12a30
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x20ac0
movq 0x560(%rsp), %rdi
leaq 0x1461c9(%rip), %rsi    # 0x166d1d
movb $0x0, %al
callq 0x12a30
cmpl $0x3, 0x34(%rsp)
jge 0x20b78
movq 0x560(%rsp), %rdi
leaq 0x146d78(%rip), %rsi    # 0x1678e9
movb $0x0, %al
callq 0x12a30
movq 0x560(%rsp), %rdi
leaq 0x14c3bb(%rip), %rsi    # 0x16cf42
movb $0x0, %al
callq 0x12a30
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x20a97
movq 0x560(%rsp), %rdi
leaq 0x146172(%rip), %rsi    # 0x166d1f
movb $0x0, %al
callq 0x12a30
movq 0x28(%rsp), %rdi
movl 0x554(%rsp), %eax
movl %eax, 0x4(%rsp)
callq 0x19c40
movl %eax, %ecx
movl 0x4(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x20bee
movq 0x560(%rsp), %rdi
leaq 0x146144(%rip), %rsi    # 0x166d29
movb $0x0, %al
callq 0x12a30
jmp 0x20c04
movq 0x560(%rsp), %rdi
leaq 0x146134(%rip), %rsi    # 0x166d31
movb $0x0, %al
callq 0x12a30
jmp 0x20c06
movl 0x554(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x554(%rsp)
jmp 0x208ab
movq 0x560(%rsp), %rdi
leaq 0x14610d(%rip), %rsi    # 0x166d38
movb $0x0, %al
callq 0x12a30
movq 0x560(%rsp), %rdi
leaq 0x1460f4(%rip), %rsi    # 0x166d35
movb $0x0, %al
callq 0x12a30
movq 0x560(%rsp), %rdi
callq 0x126c0
movl $0x1, 0x584(%rsp)
movl 0x584(%rsp), %eax
addq $0x588, %rsp            # imm = 0x588
retq
nop