// File: /workspace/github_cpp_makefile_star2_O2/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 $0x408, %rsp            # imm = 0x408
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x967b5(%rip), %rsi     # 0xae713
movq %rbx, %rdi
callq 0x11700
testq %rax, %rax
je 0x18568
movq %rax, %r14
movq %r15, 0x90(%rsp)
leaq 0xf0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x11660
movq %r14, %rdi
callq 0x11030
movq %rax, %r15
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11660
leaq 0x1(%r15), %rdi
callq 0x11040
movq %rax, %r12
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r15, %rdx
movq %r14, %rcx
callq 0x118f0
cmpq %r15, %rax
jne 0x1857d
movb $0x0, (%r12,%r15)
leaq 0xe0(%rsp), %rdi
movq %r12, %rsi
callq 0x11b80
movq %r12, %rdi
callq 0x117a0
movq %r14, %rdi
callq 0x115b0
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0x74836
leaq 0x2f8(%rsp), %rdi
callq 0x6e8ba
leaq 0x2f8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
xorl %ecx, %ecx
callq 0x6ea1c
testb %al, %al
je 0x18592
andq $0x0, 0x58(%rsp)
andq $0x0, 0x50(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x6f024
testb %al, %al
je 0x185a7
leaq 0x90ac5(%rip), %rdx     # 0xa8b2d
leaq 0x58(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x18703
testl %eax, %eax
je 0x185a7
leaq 0x90ab3(%rip), %rdx     # 0xa8b3c
leaq 0x50(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x18703
testl %eax, %eax
je 0x185a7
movq 0x58(%rsp), %rdi
callq 0x75688
mulsd 0x8ff63(%rip), %xmm0    # 0xa8018
xorpd %xmm1, %xmm1
movsd %xmm0, 0x80(%rsp)
ucomisd %xmm0, %xmm1
jae 0x185f5
leaq 0x936a5(%rip), %rax     # 0xab778
movsd (%rax), %xmm0
xorpd 0x8ff71(%rip), %xmm0    # 0xa8050
movapd %xmm0, 0x100(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x278(%rsp), %r15
leaq 0x1f8(%rsp), %r12
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl %ebp, 0x2c(%rsp)
movq 0x30(%rsp), %rdi
callq 0x73cc6
cmpl %eax, 0x14(%rsp)
jae 0x1862d
cmpl $0x2, %ebp
jl 0x18147
movl 0x14(%rsp), %eax
xorl %edx, %edx
divl %ebp
testl %edx, %edx
jne 0x1855f
andq $0x0, 0x48(%rsp)
leaq 0x48(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x14(%rsp), %edx
callq 0x1878e
movq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x6f024
testb %al, %al
je 0x1855f
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
andq $0x0, 0xa0(%rsp)
andq $0x0, 0x40(%rsp)
movb $0x0, 0xa8(%rsp)
andq $0x0, 0x38(%rsp)
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
leaq 0x909d8(%rip), %rdx     # 0xa8b89
callq 0x18703
leaq 0x178(%rsp), %rbp
testl %eax, %eax
je 0x1860a
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
leaq 0x909be(%rip), %rdx     # 0xa8b93
callq 0x18703
testl %eax, %eax
je 0x1860a
movq 0x38(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x6f01c
testb %al, %al
je 0x1860a
movq 0x20(%rsp), %rdi
callq 0x73cc6
cmpl $0x4, %eax
jne 0x1860a
leaq 0x110(%rsp), %rdi
leaq 0x9010e(%rip), %rsi     # 0xa8329
movq %r15, %rdx
callq 0x196d0
movq 0x40(%rsp), %r13
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
callq 0x7012a
movq 0x60(%rsp), %rdi
callq 0x6dd04
testl %eax, %eax
jne 0x182c9
movq %r12, %rdi
movq %r13, %rsi
callq 0x7012a
movq %r15, %rdi
movq %r12, %rsi
leaq 0x9095d(%rip), %rdx     # 0xa8bbc
callq 0x1970b
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x11740
movq %r15, %rdi
callq 0x11270
movq %r12, %rdi
callq 0x11270
movq 0x60(%rsp), %rdi
callq 0x6dd04
testl %eax, %eax
jne 0x182c9
movq %r12, %rdi
movq %r13, %rsi
callq 0x7012a
movq %r15, %rdi
movq %r12, %rsi
leaq 0x90cd3(%rip), %rdx     # 0xa8f7a
callq 0x1970b
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x11740
movq %r15, %rdi
callq 0x11270
movq %r12, %rdi
callq 0x11270
cmpl $0x0, 0x1c(%rsp)
jle 0x182d7
cmpl $0x0, 0x18(%rsp)
jg 0x18347
movq %r15, %rdi
callq 0x66ec8
movq 0x60(%rsp), %rsi
movq %r15, %rdi
callq 0x66ee4
testl %eax, %eax
je 0x1832c
movl 0x278(%rsp), %eax
movl 0x27c(%rsp), %ecx
movl %eax, 0x1c(%rsp)
testl %eax, %eax
setg %al
movl %ecx, 0x18(%rsp)
testl %ecx, %ecx
setg %cl
movb $0x1, %bpl
testb %cl, %al
jne 0x1832e
xorl %ebp, %ebp
leaq 0x908a1(%rip), %rdi     # 0xa8bc1
movq %rbx, %rsi
xorl %eax, %eax
callq 0x6e732
jmp 0x1832e
xorl %ebp, %ebp
movq %r15, %rdi
callq 0x67414
testb %bpl, %bpl
je 0x1852c
leaq 0x178(%rsp), %rbp
leaq 0x130(%rsp), %rdi
leaq 0x92e7a(%rip), %rsi     # 0xab1d0
callq 0x648fc
cvtsi2sdl 0x1c(%rsp), %xmm1
movsd 0x8fcaf(%rip), %xmm0    # 0xa8018
mulsd %xmm0, %xmm1
movsd %xmm1, 0x88(%rsp)
movsd 0x80(%rsp), %xmm0
callq 0x11340
movsd 0x88(%rsp), %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x130(%rsp)
movsd %xmm1, 0x150(%rsp)
movsd %xmm2, 0x140(%rsp)
cvtsi2sdl 0x18(%rsp), %xmm0
mulsd 0x8fc5a(%rip), %xmm0    # 0xa8018
movsd %xmm0, 0x158(%rsp)
movq %r15, %rdi
leaq 0x91967(%rip), %rsi     # 0xa9d38
callq 0x42c50
movq %r15, %r14
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x184a0
andq $0x0, 0x1f8(%rsp)
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movl %r13d, %edx
callq 0x1878e
movq 0x20(%rsp), %rdi
callq 0x6f01c
testb %al, %al
je 0x18481
movq 0x1f8(%rsp), %rbp
movq %rbp, %rdi
callq 0x73cc6
cmpl $0x4, %eax
jne 0x18481
movq %r15, %r12
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0x18462
andq $0x0, 0x178(%rsp)
leaq 0x178(%rsp), %rdi
movq %rbp, %rsi
movl %r15d, %edx
callq 0x1878e
movq 0x178(%rsp), %rdi
callq 0x75688
movsd %xmm0, (%r14,%r15,8)
incq %r15
jmp 0x18428
incq %r13
addq $0x20, %r14
movq %r12, %r15
leaq 0x1f8(%rsp), %r12
leaq 0x178(%rsp), %rbp
jmp 0x183dc
leaq 0x90765(%rip), %rdi     # 0xa8bed
movl 0x14(%rsp), %esi
xorl %eax, %eax
callq 0x6e732
cmpq $0x4, %r13
setae %bpl
jmp 0x1852e
movq %r12, %rdi
leaq 0x9188e(%rip), %rsi     # 0xa9d38
callq 0x42c50
movq %r12, %rdi
movapd 0x100(%rsp), %xmm0
callq 0x43cdc
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x446e5
movq %r12, %rdi
movq %rbp, %rsi
callq 0x442d8
movl $0x210, %edi            # imm = 0x210
callq 0x115c0
movq %rax, %r14
movq 0x60(%rsp), %rsi
movq 0x110(%rsp), %rdx
movl 0x18(%rsp), %eax
movl %eax, (%rsp)
xorpd %xmm0, %xmm0
movq %r14, %rdi
leaq 0x130(%rsp), %rcx
movq %r12, %r8
movl 0x1c(%rsp), %r9d
callq 0x1bbe0
movq 0x90(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x10(%rax)
movb $0x1, %bpl
jmp 0x1852e
xorl %ebp, %ebp
leaq 0x60(%rsp), %rdi
callq 0x11270
leaq 0x110(%rsp), %rdi
callq 0x11270
leaq 0x98(%rsp), %rdi
callq 0x11270
testb %bpl, %bpl
movl 0x2c(%rsp), %ebp
je 0x18632
incl 0x14(%rsp)
jmp 0x1811e
leaq 0x9057a(%rip), %rdi     # 0xa8ae9
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x6e732
jmp 0x185e1
xorl %ebp, %ebp
leaq 0x90580(%rip), %rdi     # 0xa8b06
movq %rbx, %rsi
xorl %eax, %eax
callq 0x6e732
jmp 0x185d4
xorl %ebp, %ebp
leaq 0x9057e(%rip), %rdi     # 0xa8b19
movq %rbx, %rsi
xorl %eax, %eax
callq 0x6e732
jmp 0x185ba
xorl %ebp, %ebp
leaq 0x90593(%rip), %rdi     # 0xa8b43
movq %rbx, %rsi
xorl %eax, %eax
callq 0x6e732
leaq 0x2f8(%rsp), %rdi
callq 0x1972a
leaq 0xb8(%rsp), %rdi
callq 0x74cb0
leaq 0xe0(%rsp), %rdi
callq 0x11270
movl %ebp, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
leaq 0x90567(%rip), %rdi     # 0xa8b65
movq %rbx, %rsi
xorl %eax, %eax
callq 0x6e732
jmp 0x185ba
leaq 0x90593(%rip), %rdi     # 0xa8ba4
xorl %ebp, %ebp
movl 0x14(%rsp), %esi
xorl %eax, %eax
callq 0x6e732
leaq 0x98(%rsp), %rdi
callq 0x11270
jmp 0x185ba
pushq $0x1
popq %rbp
jmp 0x185ba
xorl %ebp, %ebp
jmp 0x185ba
jmp 0x1868f
jmp 0x1864e
jmp 0x186ad
movq %rax, %rbx
movl $0x210, %esi            # imm = 0x210
movq %r14, %rdi
callq 0x115d0
jmp 0x186b0
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
callq 0x11270
jmp 0x186b0
jmp 0x186ad
jmp 0x186ad
jmp 0x186ad
jmp 0x186ad
jmp 0x186ad
jmp 0x186ad
movq %rax, %rbx
leaq 0x278(%rsp), %rdi
callq 0x67414
jmp 0x186b0
jmp 0x186ad
jmp 0x186ad
jmp 0x1869f
movq %rax, %rbx
jmp 0x186ba
jmp 0x1868f
jmp 0x186ad
jmp 0x1869f
movq %rax, %rbx
jmp 0x186c7
jmp 0x1869f
movq %rax, %rbx
jmp 0x186e1
jmp 0x186a6
jmp 0x1869f
movq %rax, %rbx
jmp 0x186d4
jmp 0x186a6
movq %rax, %rbx
jmp 0x186ee
jmp 0x186ad
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x11270
leaq 0x110(%rsp), %rdi
callq 0x11270
leaq 0x98(%rsp), %rdi
callq 0x11270
leaq 0x2f8(%rsp), %rdi
callq 0x1972a
leaq 0xb8(%rsp), %rdi
callq 0x74cb0
leaq 0xe0(%rsp), %rdi
callq 0x11270
movq %rbx, %rdi
callq 0x11a80