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

  // Read file 
  std::string text;
  fseek(fp, 0, SEEK_END);
  long const size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  char* buffer = new char[size + 1];
  unsigned long const usize = static_cast<unsigned long const>(size);
  if (fread(buffer, 1, usize, fp) != usize) { RNFail("Unable to read %s\n", filename); return 0; }
  else { buffer[size] = 0; text = buffer; }
  delete[] buffer;

  // Close file
  fclose(fp);

  // Parse file
  Json::Value json_root;
  Json::Reader json_reader;
  if (!json_reader.parse(text, json_root, false)) {
    RNFail("Unable to parse %s\n", filename);
    return 0;
  }

  // Get top level stuff
  Json::Value *json_camera_angle_x = NULL;
  Json::Value *json_frames = NULL;
  if (!json_root.isObject() ||
      !GetJsonObjectMember(json_camera_angle_x, &json_root, "camera_angle_x") ||
      !GetJsonObjectMember(json_frames, &json_root, "frames")) {
    RNFail("NeRF file is not expected format\n", filename);
    return 0;
  }

  // Parse x fov 
  int width = 0;  // Gets filled from first image
  int height = 0; // Gets filled from first image
  double xfov = 0.5 * json_camera_angle_x->asDouble();
  if (xfov <= 0) {
    RNFail("Error parsing camera_angle_x in %s\n", filename);
    return 0;
  }

  // Parse frames
  for (Json::ArrayIndex i = 0; i < json_frames->size(); i++) {
    // Check if skipping images
    if ((read_every_kth_image > 1) && ((i % read_every_kth_image) != 0)) continue;
    
    // Get frame
    Json::Value *json_frame = NULL;
    GetJsonArrayEntry(json_frame, json_frames, i);
    if (!json_frame->isObject()) continue;

    // Parse filepath
    std::string file_path;
    Json::Value *json_file_path = NULL;
    Json::Value *json_transform = NULL;
    if (!GetJsonObjectMember(json_file_path, json_frame, "file_path") ||
        !GetJsonObjectMember(json_transform, json_frame, "transform_matrix") ||
        !json_transform->isArray() || (json_transform->size() != 4)) {
      RNFail("Error parsing frame %d\n", i);
      return 0;
    }

    // Parse image filenames
    std::string depth_filename = "-";
    std::string color_filename = json_file_path->asString();
    if (!RNFileExists(color_filename.c_str())) {
      color_filename = json_file_path->asString() + ".png";
      if (!RNFileExists(color_filename.c_str())) {
        color_filename = json_file_path->asString() + ".jpg";
      }
    }

    // Get width and height
    if ((width <= 0) || (height <= 0)) {
      R2Image color_image;
      if (!color_image.ReadFile(color_filename.c_str())) return 0;
      width = color_image.Width();
      height = color_image.Height();
      if ((width <= 0) || (height <= 0)) {
        RNFail("Error parsing image width and height in %s\n", filename);
        return 0;
      }
    }

    // Get intrinsics matrix
    R3Matrix intrinsics = R3identity_matrix;
    double focal = 0.5 * width / tan(xfov);
    intrinsics[0][0] = focal;
    intrinsics[1][1] = focal;
    intrinsics[0][2] = 0.5 * width;
    intrinsics[1][2] = 0.5 * height;
      
    // Parse rotation (what is this?)
    // XXX
    
    // Parse transformation matrix
    R4Matrix matrix = R4identity_matrix;
    for (int j = 0; j < 4; j++) {
      Json::Value *json_column = NULL;
      GetJsonArrayEntry(json_column, json_transform, j);
      if (!json_transform->isArray() || (json_column->size() != 4)) {
        RNFail("Error parsing tranform %d\n", i);
        return 0;
      }
      for (int k = 0; k < 4; k++) {
        Json::Value *json_value = NULL;
        GetJsonArrayEntry(json_value, json_column, k);
        matrix[j][k] = json_value->asDouble();
      }
    }

    // Compute camera-to-world matrix
    R4Matrix camera_to_world = R4identity_matrix;
    camera_to_world.XRotate(-RN_PI_OVER_TWO);
    camera_to_world = camera_to_world * matrix;

    // Create image
    RGBDImage *image = new RGBDImage(color_filename.c_str(), depth_filename.c_str(),
      intrinsics, camera_to_world, width, height);

    // Insert image
    InsertImage(image);
  }
  
  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3f8, %rsp            # imm = 0x3F8
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xa8fd5(%rip), %rsi     # 0xc1a63
movq %rbx, %rdi
callq 0x11700
testq %rax, %rax
je 0x1925b
movq %rax, %r15
movq %r14, 0xe0(%rsp)
leaq 0xa8(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x11650
movq %r15, %rdi
callq 0x11030
movq %rax, %r12
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11650
leaq 0x1(%r12), %rdi
callq 0x11040
movq %rax, %r14
movl $0x1, %esi
movq %rax, %rdi
movq %r12, %rdx
movq %r15, %rcx
callq 0x118d0
cmpq %r12, %rax
jne 0x19273
movb $0x0, (%r14,%r12)
leaq 0x98(%rsp), %r12
movq 0x8(%r12), %r13
movq %r14, %rdi
callq 0x11260
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
leaq 0xa8(%rsp), %r13
movq %r14, %rcx
movq %rax, %r8
callq 0x11910
movq %r14, %rdi
callq 0x117a0
movq %r15, %rdi
callq 0x115a0
leaq 0x168(%rsp), %rdi
xorl %esi, %esi
callq 0x801f8
leaq 0x268(%rsp), %rdi
callq 0x780d0
leaq 0x268(%rsp), %rdi
leaq 0x98(%rsp), %rsi
leaq 0x168(%rsp), %rdx
xorl %ecx, %ecx
callq 0x78262
testb %al, %al
je 0x1928b
xorl %eax, %eax
movq %rax, 0x70(%rsp)
movq %rax, 0x68(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x78946
testb %al, %al
je 0x192a0
leaq 0xa2feb(%rip), %rdx     # 0xbbbad
leaq 0x70(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x194b4
testl %eax, %eax
je 0x192a0
leaq 0xa2fd9(%rip), %rdx     # 0xbbbbc
leaq 0x68(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x194b4
testl %eax, %eax
je 0x192a0
movq 0x70(%rsp), %rdi
callq 0x8131c
mulsd 0xa2411(%rip), %xmm0    # 0xbb020
xorpd %xmm1, %xmm1
movsd %xmm0, 0xd8(%rsp)
ucomisd %xmm0, %xmm1
jae 0x1934d
leaq 0x88(%rsp), %r15
leaq 0xa5c33(%rip), %rax     # 0xbe868
movsd (%rax), %xmm0
xorpd 0xa240f(%rip), %xmm0    # 0xbb050
movapd %xmm0, 0x210(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x48(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
xorl %r12d, %r12d
movl %ebp, 0x24(%rsp)
movq 0x48(%rsp), %rdi
callq 0x7f26c
cmpl %eax, %r12d
jae 0x19365
cmpl $0x2, %ebp
jl 0x18c92
movl %r12d, %eax
xorl %edx, %edx
divl %ebp
testl %edx, %edx
jne 0x18e29
movq $0x0, 0x60(%rsp)
leaq 0x60(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl %r12d, %edx
callq 0x19553
movq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x78946
testb %al, %al
je 0x18e29
movq %r15, 0x78(%rsp)
xorl %eax, %eax
movq %rax, 0x80(%rsp)
movb $0x0, 0x88(%rsp)
movq %rax, 0x58(%rsp)
movq %rax, 0x50(%rsp)
leaq 0x58(%rsp), %rdi
movq %r14, %rsi
leaq 0xa2f17(%rip), %rdx     # 0xbbc09
callq 0x194b4
testl %eax, %eax
je 0x18dee
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
leaq 0xa2f05(%rip), %rdx     # 0xbbc13
callq 0x194b4
testl %eax, %eax
je 0x18dee
movq 0x50(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x7893e
testb %al, %al
je 0x18dee
movq 0x18(%rsp), %rdi
callq 0x7f26c
cmpl $0x4, %eax
jne 0x18dee
leaq 0xc8(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xa25c5(%rip), %rsi     # 0xbb329
leaq 0xa25bf(%rip), %rdx     # 0xbb32a
callq 0x1a88c
movq 0x58(%rsp), %rsi
leaq 0x28(%rsp), %rdi
movq %rsi, 0x10(%rsp)
callq 0x79efc
leaq 0xf8(%rsp), %r14
leaq 0x190(%rsp), %rbp
movq 0x28(%rsp), %rdi
callq 0x77314
testl %eax, %eax
jne 0x18f7b
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x79efc
movq %rbp, %rdi
leaq 0xa2e7f(%rip), %rsi     # 0xbbc3c
callq 0x11b50
movq %r14, 0xe8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x18e31
movq %rdx, 0xe8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xf8(%rsp)
jmp 0x18e3a
xorl %ebp, %ebp
leaq 0xa2e2d(%rip), %rdi     # 0xbbc24
movl %r12d, %esi
xorl %eax, %eax
callq 0x77f42
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x18e1b
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x115c0
testb $0x1, %bpl
movl 0x24(%rsp), %ebp
je 0x1936f
incl %r12d
jmp 0x18c6b
movupd (%rcx), %xmm0
movupd %xmm0, (%r14)
movq 0x8(%rax), %rdx
movq %rdx, 0xf0(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x28(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x11740
movq 0xe8(%rsp), %rdi
cmpq %r14, %rdi
je 0x18e84
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0x190(%rsp), %rdi
leaq 0x1a0(%rsp), %rax
cmpq %rax, %rdi
je 0x18ea9
movq 0x1a0(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0x28(%rsp), %rdi
callq 0x77314
testl %eax, %eax
jne 0x18f7b
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x79efc
movq %rbp, %rdi
leaq 0xa3128(%rip), %rsi     # 0xbbffa
callq 0x11b50
movq %r14, 0xe8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x18f03
movq %rdx, 0xe8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xf8(%rsp)
jmp 0x18f0c
movupd (%rcx), %xmm0
movupd %xmm0, (%r14)
movq 0x8(%rax), %rdx
movq %rdx, 0xf0(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x28(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x11740
movq 0xe8(%rsp), %rdi
cmpq %r14, %rdi
je 0x18f56
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0x190(%rsp), %rdi
leaq 0x1a0(%rsp), %rax
cmpq %rax, %rdi
je 0x18f7b
movq 0x1a0(%rsp), %rsi
incq %rsi
callq 0x115c0
cmpl $0x0, 0xc(%rsp)
jle 0x18f89
cmpl $0x0, 0x8(%rsp)
jg 0x19000
leaq 0xe8(%rsp), %r14
movq %r14, %rdi
callq 0x6f968
movq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x6f98c
testl %eax, %eax
je 0x18fdf
movl 0xe8(%rsp), %eax
movl 0xec(%rsp), %ecx
movl %eax, 0xc(%rsp)
testl %eax, %eax
setg %al
movl %ecx, 0x8(%rsp)
testl %ecx, %ecx
setg %cl
testb %cl, %al
jne 0x18ff3
leaq 0xa2c6c(%rip), %rdi     # 0xbbc41
movq %rbx, %rsi
xorl %eax, %eax
callq 0x77f42
leaq 0xe8(%rsp), %rdi
callq 0x6fee4
xorl %ebp, %ebp
jmp 0x19211
leaq 0xe8(%rsp), %rdi
callq 0x6fee4
leaq 0x220(%rsp), %rdi
leaq 0xa52b1(%rip), %rsi     # 0xbe2c0
callq 0x6d1a2
cvtsi2sdl 0xc(%rsp), %xmm1
movsd 0xa1ffe(%rip), %xmm0    # 0xbb020
mulsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0xd8(%rsp), %xmm0
callq 0x11340
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x220(%rsp)
movsd %xmm1, 0x240(%rsp)
movsd %xmm2, 0x230(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%rsp), %xmm0
mulsd 0xa1fac(%rip), %xmm0    # 0xbb020
movsd %xmm0, 0x248(%rsp)
leaq 0xe8(%rsp), %r13
movq %r13, %rdi
leaq 0xa3d79(%rip), %rsi     # 0xbce08
callq 0x47efe
xorl %r15d, %r15d
xorl %ebp, %ebp
movq $0x0, 0x190(%rsp)
leaq 0x190(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %r15d, %edx
callq 0x19553
movq 0x18(%rsp), %rdi
callq 0x7893e
testb %al, %al
je 0x191f0
movq 0x190(%rsp), %r14
movq %r14, %rdi
callq 0x7f26c
cmpl $0x4, %eax
jne 0x191f0
xorl %ebp, %ebp
movq $0x0, 0x378(%rsp)
leaq 0x378(%rsp), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x19553
movq 0x378(%rsp), %rdi
callq 0x8131c
movsd %xmm0, (%r13,%rbp,8)
incq %rbp
cmpq $0x4, %rbp
jne 0x190e7
cmpq $0x3, %r15
leaq 0x1(%r15), %rax
setae %bpl
addq $0x20, %r13
movq %rax, %r15
cmpq $0x4, %rax
jne 0x19099
leaq 0x190(%rsp), %rbp
movq %rbp, %rdi
leaq 0xa3cb7(%rip), %rsi     # 0xbce08
callq 0x47efe
movq %rbp, %rdi
movapd 0x210(%rsp), %xmm0
callq 0x492be
leaq 0x378(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
leaq 0xe8(%rsp), %rdx
leaq 0xa8(%rsp), %r13
leaq 0x88(%rsp), %r15
callq 0x49eef
movq %rbp, %rdi
movq %r14, %rsi
callq 0x49ae2
movl $0x210, %edi            # imm = 0x210
callq 0x115b0
movq %rax, %r14
movq 0x28(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movl 0x8(%rsp), %eax
movl %eax, (%rsp)
xorpd %xmm0, %xmm0
movq %r14, %rdi
leaq 0x220(%rsp), %rcx
movq %rbp, %r8
movl 0xc(%rsp), %r9d
callq 0x1cee2
movq 0xe0(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x10(%rax)
movb $0x1, %bpl
jmp 0x19211
leaq 0xa2a76(%rip), %rdi     # 0xbbc6d
movl %r12d, %esi
xorl %eax, %eax
callq 0x77f42
leaq 0xa8(%rsp), %r13
leaq 0x88(%rsp), %r15
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x1922d
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x18e01
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x115c0
jmp 0x18e01
leaq 0xa2907(%rip), %rdi     # 0xbbb69
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x77f42
jmp 0x19339
xorl %ebp, %ebp
leaq 0xa290a(%rip), %rdi     # 0xbbb86
movq %rbx, %rsi
xorl %eax, %eax
callq 0x77f42
jmp 0x1931c
xorl %ebp, %ebp
leaq 0xa2905(%rip), %rdi     # 0xbbb99
movq %rbx, %rsi
xorl %eax, %eax
callq 0x77f42
jmp 0x192b3
xorl %ebp, %ebp
leaq 0xa291a(%rip), %rdi     # 0xbbbc3
movq %rbx, %rsi
xorl %eax, %eax
callq 0x77f42
leaq 0x360(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x192d4
movq 0x360(%rsp), %rsi
incq %rsi
callq 0x115c0
leaq 0x318(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x192f5
movq 0x318(%rsp), %rsi
incq %rsi
callq 0x115c0
leaq 0x2b8(%rsp), %rdi
callq 0x1a688
leaq 0x268(%rsp), %rdi
callq 0x1a834
leaq 0x168(%rsp), %rdi
callq 0x80736
movq 0x98(%rsp), %rdi
cmpq %r13, %rdi
je 0x19339
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x115c0
movl %ebp, %eax
addq $0x3f8, %rsp            # imm = 0x3F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
leaq 0xa288f(%rip), %rdi     # 0xbbbe5
movq %rbx, %rsi
xorl %eax, %eax
callq 0x77f42
jmp 0x192b3
movl $0x1, %ebp
jmp 0x192b3
xorl %ebp, %ebp
jmp 0x192b3
jmp 0x1938a
jmp 0x19407
movq %rax, %rbx
movl $0x210, %esi            # imm = 0x210
movq %r14, %rdi
jmp 0x193ad
movq %rax, %rbx
movq 0x190(%rsp), %rdi
leaq 0x1a0(%rsp), %rax
cmpq %rax, %rdi
je 0x1940a
movq 0x1a0(%rsp), %rsi
incq %rsi
callq 0x115c0
jmp 0x1940a
jmp 0x19407
jmp 0x19407
jmp 0x19407
jmp 0x19407
jmp 0x19407
jmp 0x19407
jmp 0x19407
jmp 0x19407
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x6fee4
jmp 0x1940a
movq %rax, %rbx
jmp 0x19426
jmp 0x193ef
jmp 0x19407
jmp 0x193f8
jmp 0x19407
movq %rax, %rbx
jmp 0x1947a
jmp 0x193ff
jmp 0x193f8
movq %rax, %rbx
jmp 0x1944b
jmp 0x193f8
jmp 0x193f8
movq %rax, %rbx
jmp 0x1946d
jmp 0x193ff
movq %rax, %rbx
jmp 0x19487
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x19426
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x1944b
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x1946d
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x115c0
leaq 0x268(%rsp), %rdi
callq 0x1a636
leaq 0x168(%rsp), %rdi
callq 0x80736
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x194ac
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x115c0
movq %rbx, %rdi
callq 0x11a60