// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
leaq 0xaacd7(%rip), %rsi     # 0xc3a33
movq %rbx, %rdi
callq 0x11710
testq %rax, %rax
je 0x19571
movq %rax, %r14
movq %r15, 0xe0(%rsp)
leaq 0x80(%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 %r14, %rdi
callq 0x11030
movq %rax, %r12
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11650
leaq 0x1(%r12), %rdi
callq 0x11040
movq %rax, %r15
movl $0x1, %esi
movq %rax, %rdi
movq %r12, %rdx
movq %r14, %rcx
callq 0x118e0
cmpq %r12, %rax
jne 0x19589
movb $0x0, (%r15,%r12)
leaq 0x70(%rsp), %r12
movq 0x8(%r12), %r13
movq %r15, %rdi
callq 0x11260
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
leaq 0x80(%rsp), %r13
movq %r15, %rcx
movq %rax, %r8
callq 0x11920
movq %r15, %rdi
callq 0x117b0
movq %r14, %rdi
callq 0x115a0
leaq 0x168(%rsp), %rdi
xorl %esi, %esi
callq 0x81fbe
leaq 0x268(%rsp), %rdi
callq 0x79220
leaq 0x268(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x168(%rsp), %rdx
xorl %ecx, %ecx
callq 0x793b2
testb %al, %al
je 0x195a1
xorl %eax, %eax
movq %rax, 0x68(%rsp)
movq %rax, 0x60(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x79b6c
testb %al, %al
je 0x195b6
leaq 0xa4d23(%rip), %rdx     # 0xbdbad
leaq 0x68(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x197cb
testl %eax, %eax
je 0x195b6
leaq 0xa4d11(%rip), %rdx     # 0xbdbbc
leaq 0x60(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x197cb
testl %eax, %eax
je 0x195b6
movq 0x68(%rsp), %rdi
callq 0x830e6
mulsd 0xa4149(%rip), %xmm0    # 0xbd020
xorpd %xmm1, %xmm1
movsd %xmm0, 0xd8(%rsp)
ucomisd %xmm0, %xmm1
jae 0x19660
movq %rbx, 0xd0(%rsp)
leaq 0xc0(%rsp), %r12
leaq 0xa7953(%rip), %rax     # 0xc0858
movsd (%rax), %xmm0
xorpd 0xa413f(%rip), %xmm0    # 0xbd050
movapd %xmm0, 0x210(%rsp)
leaq 0x378(%rsp), %rbx
movl $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x80e7c
cmpl %eax, 0x10(%rsp)
jae 0x19678
cmpl $0x2, %ebp
jl 0x18f66
movl 0x10(%rsp), %eax
xorl %edx, %edx
divl %ebp
testl %edx, %edx
jne 0x19101
movq $0x0, 0x58(%rsp)
leaq 0x58(%rsp), %rdi
movq %r14, %rsi
movl 0x10(%rsp), %edx
callq 0x1986a
movq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x79b6c
movl $0x4, %r15d
testb %al, %al
je 0x190f7
movq %r12, 0xb0(%rsp)
xorl %eax, %eax
movq %rax, 0xb8(%rsp)
movb $0x0, 0xc0(%rsp)
movq %rax, 0x50(%rsp)
movq %rax, 0x28(%rsp)
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
leaq 0xa4c3b(%rip), %rdx     # 0xbdc09
callq 0x197cb
testl %eax, %eax
je 0x190c2
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
leaq 0xa4c29(%rip), %rdx     # 0xbdc13
callq 0x197cb
testl %eax, %eax
je 0x190c2
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x79b64
testb %al, %al
je 0x190c2
movq %r14, %rdi
callq 0x80e7c
cmpl $0x4, %eax
jne 0x190c2
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0xa42f0(%rip), %rsi     # 0xbd32c
leaq 0xa42ea(%rip), %rdx     # 0xbd32d
callq 0x1ab58
movq 0x50(%rsp), %r15
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x7b3a0
movq 0x30(%rsp), %rdi
callq 0x783c0
testl %eax, %eax
jne 0x19262
leaq 0x190(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x7b3a0
movq %r14, %rdi
leaq 0xa4bb3(%rip), %rsi     # 0xbdc3c
callq 0x11b60
leaq 0xf8(%rsp), %r14
movq %r14, 0xe8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x1910a
movq %rdx, 0xe8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xf8(%rsp)
jmp 0x19113
movl $0x1, %r15d
leaq 0xa4b55(%rip), %rdi     # 0xbdc24
movl 0x10(%rsp), %esi
xorl %eax, %eax
callq 0x79092
movq 0xb0(%rsp), %rdi
cmpq %r12, %rdi
je 0x190f7
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x115c0
testb $0x3, %r15b
jne 0x1967e
incl 0x10(%rsp)
jmp 0x18f3a
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 0x30(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x11750
movq 0xe8(%rsp), %rdi
cmpq %r14, %rdi
je 0x1915d
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0x190(%rsp), %rdi
leaq 0x1a0(%rsp), %rax
cmpq %rax, %rdi
je 0x19182
movq 0x1a0(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0x30(%rsp), %rdi
callq 0x783c0
testl %eax, %eax
jne 0x19262
leaq 0x190(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x7b3a0
movq %r14, %rdi
leaq 0xa4e49(%rip), %rsi     # 0xbdffa
callq 0x11b60
leaq 0xf8(%rsp), %r14
movq %r14, 0xe8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x191ea
movq %rdx, 0xe8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xf8(%rsp)
jmp 0x191f3
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 0x30(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x11750
movq 0xe8(%rsp), %rdi
cmpq %r14, %rdi
je 0x1923d
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0x190(%rsp), %rdi
leaq 0x1a0(%rsp), %rax
cmpq %rax, %rdi
je 0x19262
movq 0x1a0(%rsp), %rsi
incq %rsi
callq 0x115c0
cmpl $0x0, 0x18(%rsp)
jle 0x19274
cmpl $0x0, 0x14(%rsp)
jg 0x192fa
movl %ebp, %r14d
leaq 0xe8(%rsp), %r15
movq %r15, %rdi
callq 0x707dc
movq 0x30(%rsp), %rsi
movq %r15, %rdi
callq 0x70800
testl %eax, %eax
je 0x192d9
movl 0xe8(%rsp), %eax
movl 0xec(%rsp), %ecx
movl %eax, 0x18(%rsp)
testl %eax, %eax
setg %al
movl %ecx, 0x14(%rsp)
testl %ecx, %ecx
setg %cl
movb $0x1, %bpl
testb %cl, %al
jne 0x192db
xorl %ebp, %ebp
leaq 0xa4979(%rip), %rdi     # 0xbdc41
movq 0xd0(%rsp), %rsi
xorl %eax, %eax
callq 0x79092
jmp 0x192db
xorl %ebp, %ebp
leaq 0xe8(%rsp), %rdi
callq 0x70d66
movl $0x1, %r15d
testb %bpl, %bpl
movl %r14d, %ebp
je 0x19527
leaq 0x220(%rsp), %rdi
leaq 0xa6f9f(%rip), %rsi     # 0xc02a8
callq 0x6dfb2
movl %ebp, 0x1c(%rsp)
cvtsi2sdl 0x18(%rsp), %xmm1
movsd 0xa3d00(%rip), %xmm0    # 0xbd020
mulsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movsd 0xd8(%rsp), %xmm0
callq 0x11340
movsd 0x20(%rsp), %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x220(%rsp)
movsd %xmm1, 0x240(%rsp)
movsd %xmm2, 0x230(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x14(%rsp), %xmm0
mulsd 0xa3cae(%rip), %xmm0    # 0xbd020
movsd %xmm0, 0x248(%rsp)
leaq 0xe8(%rsp), %r13
movq %r13, %rdi
leaq 0xa5a7b(%rip), %rsi     # 0xbee08
callq 0x48742
movq 0x28(%rsp), %r15
xorl %r14d, %r14d
xorl %r12d, %r12d
leaq 0x190(%rsp), %rdi
movq %r15, 0x20(%rsp)
movq $0x0, 0x190(%rsp)
movq %r15, %rsi
movl %r14d, %edx
callq 0x1986a
movq %r15, %rdi
callq 0x79b64
testb %al, %al
je 0x19452
movq 0x190(%rsp), %rbp
movq %rbp, %rdi
callq 0x80e7c
cmpl $0x4, %eax
jne 0x19452
xorl %r15d, %r15d
movq $0x0, 0x378(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
movl %r15d, %edx
callq 0x1986a
movq 0x378(%rsp), %rdi
callq 0x830e6
movsd %xmm0, (%r13,%r15,8)
incq %r15
cmpq $0x4, %r15
jne 0x193e9
cmpq $0x3, %r14
leaq 0x1(%r14), %rax
setae %r12b
addq $0x20, %r13
movq %rax, %r14
cmpq $0x4, %rax
leaq 0x190(%rsp), %rdi
movq 0x20(%rsp), %r15
jne 0x193aa
movl $0x5, %r15d
jmp 0x1946a
leaq 0xa4814(%rip), %rdi     # 0xbdc6d
movl 0x10(%rsp), %esi
xorl %eax, %eax
callq 0x79092
movl $0x1, %r15d
leaq 0x80(%rsp), %r13
movl 0x1c(%rsp), %ebp
testb $0x1, %r12b
leaq 0xc0(%rsp), %r12
je 0x19527
leaq 0x190(%rsp), %r14
movq %r14, %rdi
leaq 0xa596e(%rip), %rsi     # 0xbee08
callq 0x48742
movq %r14, %rdi
movapd 0x210(%rsp), %xmm0
callq 0x49b02
movq %rbx, %r15
movq %rbx, %rdi
movq %r14, %rsi
leaq 0xe8(%rsp), %rdx
callq 0x4a733
movq %r14, %rdi
movq %r15, %rsi
callq 0x4a326
movl $0x210, %edi            # imm = 0x210
callq 0x115b0
movq %rax, %r14
movq 0x30(%rsp), %rsi
movq 0x90(%rsp), %rdx
movl 0x14(%rsp), %eax
movl %eax, (%rsp)
xorpd %xmm0, %xmm0
movq %r14, %rdi
leaq 0x220(%rsp), %rcx
leaq 0x190(%rsp), %r8
movl 0x18(%rsp), %r9d
callq 0x1d196
movq 0xe0(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x10(%rax)
xorl %r15d, %r15d
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x19543
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x190da
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x115c0
jmp 0x190da
leaq 0xa45f1(%rip), %rdi     # 0xbdb69
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x79092
jmp 0x1964c
xorl %ebp, %ebp
leaq 0xa45f4(%rip), %rdi     # 0xbdb86
movq %rbx, %rsi
xorl %eax, %eax
callq 0x79092
jmp 0x19632
xorl %ebp, %ebp
leaq 0xa45ef(%rip), %rdi     # 0xbdb99
movq %rbx, %rsi
xorl %eax, %eax
callq 0x79092
jmp 0x195c9
xorl %ebp, %ebp
leaq 0xa4604(%rip), %rdi     # 0xbdbc3
movq %rbx, %rsi
xorl %eax, %eax
callq 0x79092
leaq 0x360(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x195ea
movq 0x360(%rsp), %rsi
incq %rsi
callq 0x115c0
leaq 0x318(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1960b
movq 0x318(%rsp), %rsi
incq %rsi
callq 0x115c0
leaq 0x2b8(%rsp), %rdi
callq 0x1a96a
leaq 0x268(%rsp), %rdi
callq 0x1ab08
leaq 0x168(%rsp), %rdi
callq 0x82500
movq 0x70(%rsp), %rdi
cmpq %r13, %rdi
je 0x1964c
movq 0x80(%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 0xa457c(%rip), %rdi     # 0xbdbe5
movq %rbx, %rsi
xorl %eax, %eax
callq 0x79092
jmp 0x195c9
movl $0x2, %r15d
xorl %ebp, %ebp
cmpl $0x2, %r15d
sete %bpl
jmp 0x195c9
jmp 0x196a1
jmp 0x1971e
movq %rax, %rbx
movl $0x210, %esi            # imm = 0x210
movq %r14, %rdi
jmp 0x196c4
movq %rax, %rbx
movq 0x190(%rsp), %rdi
leaq 0x1a0(%rsp), %rax
cmpq %rax, %rdi
je 0x19721
movq 0x1a0(%rsp), %rsi
incq %rsi
callq 0x115c0
jmp 0x19721
jmp 0x1971e
jmp 0x1971e
jmp 0x1971e
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x70d66
jmp 0x19721
jmp 0x1971e
jmp 0x1971e
jmp 0x1971e
jmp 0x1971e
jmp 0x1971e
movq %rax, %rbx
jmp 0x1973d
jmp 0x19706
jmp 0x1971e
jmp 0x1970f
jmp 0x1971e
movq %rax, %rbx
jmp 0x19794
jmp 0x19716
jmp 0x1970f
movq %rax, %rbx
jmp 0x19762
jmp 0x1970f
jmp 0x1970f
movq %rax, %rbx
jmp 0x19787
jmp 0x19716
movq %rax, %rbx
jmp 0x197a1
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x1973d
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x19762
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x115c0
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x19787
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x115c0
leaq 0x268(%rsp), %rdi
callq 0x1a918
leaq 0x168(%rsp), %rdi
callq 0x82500
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x197c3
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x115c0
movq %rbx, %rdi
callq 0x11a70