// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/GSV/GSVRun.cpp
// Function: gaps::GSVRun::ReadImagePoseFile(gaps::RNArray<gaps::GSVPanorama*> const&)
int GSVRun::
ReadImagePoseFile(const RNArray<GSVPanorama *>& panoramas)
{
  // Create filename
  char filename[4096];
  sprintf(filename, "%s/%s/image_pose.txt", scene->RawDataDirectoryName(), Name());

  // Open file
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    RNFail("Unable to open image pose file: %s\n", filename);
    return 0;
  }

  // Read pose for each column
  int line_count = 0;
  const int buffer_size = 16*1024;
  char buffer[buffer_size];
  GSVCamera *camera = NULL;
  GSVSegment *segment = NULL;
  GSVTapestry *tapestry = NULL;
  GSVPanorama *panorama = NULL;
  GSVImage *image = NULL;
  while (fgets(buffer, buffer_size, fp)) {
    // Parse line
    line_count++;
    if (buffer[0] == '\0') continue;
    if (buffer[0] == '#') continue;
    int panorama_index, camera_index, column_index;
    double timestamp, position_x, position_y, position_z;
    double quaternion_x, quaternion_y, quaternion_z, quaternion_w; 
    if (sscanf(buffer, "%d%d%d%lf%lf%lf%lf%lf%lf%lf%lf", 
      &panorama_index, &camera_index, &column_index, &timestamp,
      &position_x, &position_y, &position_z,
      &quaternion_x, &quaternion_y, &quaternion_z, &quaternion_w) != 11) {
      RNFail("Error reading line %d from %s\n", line_count, filename);
      return 0;
    }

    // Get camera
    if (column_index == 0) {
      camera = NULL;
      if (camera_index < NCameras()) {
        camera = Camera(camera_index);
      }
    }

     // Get panorama and segment
    if (column_index == 0) {
      panorama = NULL;
      segment = NULL;
      if (panorama_index < panoramas.NEntries()) {
        panorama = panoramas.Kth(panorama_index);
        segment = panorama->Segment();
      }
    }

    // Get tapestry
    if (column_index == 0) {
      tapestry = NULL;
      if (segment) {
        if (camera_index < segment->NTapestries()) {
          tapestry = segment->Tapestry(camera_index);
        }
      }
    }

    // Create image
    if (column_index == 0) {
      image = new GSVImage();
      if (panorama) panorama->InsertImage(image);
      if (tapestry) tapestry->InsertImage(image);
    }

    // Create pose
    R3Point viewpoint(position_x, position_y, position_z);
    R3Quaternion orientation(quaternion_w, quaternion_x, quaternion_y, quaternion_z);
    orientation.Rotate(R3Quaternion(R3posx_vector, RN_PI));
    GSVPose pose(viewpoint, orientation);

    // Set image pose and timestamp
    if (camera) {
      static GSVPose pose0;
      static RNScalar timestamp0;
      if (column_index == 0) {
        pose0 = pose;
        timestamp0 = timestamp;
      }
      else if (column_index == camera->ImageWidth()-1) {
        image->SetPose(pose0, pose);
        image->SetTimestamp(timestamp0, timestamp);
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x50f8, %rsp           # imm = 0x50F8
movq %rsi, %r12
movq %rdi, %r14
movq 0x8(%rdi), %rax
movq 0x48(%rdi), %rcx
movq 0x20(%rax), %rdx
leaq 0x114409(%rip), %rsi    # 0x14cdb3
xorl %ebx, %ebx
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x27140
leaq 0x135a7e(%rip), %rsi    # 0x16e443
movq %r15, %rdi
callq 0x27b20
testq %rax, %rax
je 0x38d16
movq %rax, %r15
movq %r12, 0x28(%rsp)
leaq 0x10f0(%rsp), %rdi
movl $0x4000, %esi           # imm = 0x4000
movq %rax, %rdx
callq 0x27fc0
testq %rax, %rax
je 0x38d07
leaq 0x11f8f5(%rip), %rax    # 0x1582f8
movsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0x10f0(%rsp), %rbp
xorl %r13d, %r13d
movq $0x0, 0x18(%rsp)
xorl %r12d, %r12d
incl %r12d
movzbl 0x10f0(%rsp), %eax
cmpl $0x23, %eax
je 0x38a38
testl %eax, %eax
jne 0x38a55
movq %rbp, %rdi
movl $0x4000, %esi           # imm = 0x4000
movq %r15, %rdx
callq 0x27fc0
incl %r12d
testq %rax, %rax
jne 0x38a27
jmp 0x38d07
subq $0x8, %rsp
movq %rbp, %rdi
leaq 0x114389(%rip), %rsi    # 0x14cdec
leaq 0x2c(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x1c(%rsp), %r8
leaq 0x38(%rsp), %r9
xorl %eax, %eax
leaq 0x48(%rsp), %r10
pushq %r10
leaq 0x58(%rsp), %r10
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
leaq 0x78(%rsp), %r10
pushq %r10
leaq 0x88(%rsp), %r10
pushq %r10
leaq 0x98(%rsp), %r10
pushq %r10
leaq 0xa8(%rsp), %r10
pushq %r10
callq 0x27900
addq $0x40, %rsp
cmpl $0xb, %eax
jne 0x38d40
cmpl $0x0, 0x14(%rsp)
jne 0x38b6a
movslq 0x20(%rsp), %rax
cmpl 0x34(%r14), %eax
jge 0x38aea
movq 0x28(%r14), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, 0x18(%rsp)
jmp 0x38af3
movq $0x0, 0x18(%rsp)
movq 0x28(%rsp), %rdx
movslq 0x24(%rsp), %rcx
cmpl 0xc(%rdx), %ecx
jge 0x38b26
movq (%rdx), %rdx
movq (%rdx,%rcx,8), %rbx
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x38b28
cmpl 0x3c(%rcx), %eax
jge 0x38b28
movq 0x30(%rcx), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, 0x8(%rsp)
jmp 0x38b31
xorl %ebx, %ebx
movq $0x0, 0x8(%rsp)
movl $0xc8, %edi
callq 0x27930
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
callq 0x3cf34
testq %rbx, %rbx
je 0x38b58
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3ca6c
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x38b6a
movq %r13, %rsi
callq 0x3e74e
movq %r13, 0x8(%rsp)
movsd 0x70(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
leaq 0x78(%rsp), %rdi
callq 0xd43de
movsd 0x40(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd 0x50(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
callq 0xc17ca
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
leaq 0x11e1d3(%rip), %rsi    # 0x156d98
movsd 0x38(%rsp), %xmm0
callq 0xc1a82
movq %r13, %rdi
movq %rbx, %rsi
callq 0xc256a
movq %rbx, %rdi
leaq 0x78(%rsp), %rsi
movq %r13, %rdx
callq 0x44fa2
cmpq $0x0, 0x18(%rsp)
leaq 0x1a2830(%rip), %rdi    # 0x1db428
leaq 0x1a27e9(%rip), %r13    # 0x1db3e8
je 0x38c94
movb 0x1a281d(%rip), %al     # 0x1db428
testb %al, %al
je 0x38cbc
movl 0x14(%rsp), %eax
testl %eax, %eax
je 0x38c5e
movq 0x18(%rsp), %rcx
movl 0x2c(%rcx), %ecx
leal -0x1(%rcx), %edx
testl %ecx, %ecx
movl $0x78f, %ecx            # imm = 0x78F
cmovsl %ecx, %edx
cmpl %edx, %eax
jne 0x38c94
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3d822
movsd 0x1a27e4(%rip), %xmm0   # 0x1db430
movsd 0x30(%rsp), %xmm1
movq 0x8(%rsp), %rdi
callq 0x3d886
jmp 0x38c94
leaq 0x1a278b(%rip), %rdi    # 0x1db3f0
leaq 0x98(%rsp), %rsi
callq 0xd4746
leaq 0x1a278f(%rip), %rdi    # 0x1db408
leaq 0xb0(%rsp), %rsi
callq 0xc1a58
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x1a279c(%rip)   # 0x1db430
movq %rbx, %rdi
callq 0x450ee
movq %rbp, %rdi
movl $0x4000, %esi           # imm = 0x4000
movq %r15, %rdx
callq 0x27fc0
testq %rax, %rax
movq 0x8(%rsp), %r13
jne 0x38a24
jmp 0x38d07
callq 0x281c0
testl %eax, %eax
je 0x38c13
movq %r13, %rdi
callq 0x44f68
leaq 0xc416(%rip), %rdi      # 0x450ee
leaq 0x1a2709(%rip), %r13    # 0x1db3e8
movq %r13, %rsi
leaq 0x19ccff(%rip), %rdx    # 0x1d59e8
callq 0x27830
leaq 0x1a2733(%rip), %rdi    # 0x1db428
callq 0x275f0
leaq 0x90(%rsp), %rbx
jmp 0x38c13
movq %r15, %rdi
callq 0x27920
movl $0x1, %ebx
jmp 0x38d2c
leaq 0x1140ab(%rip), %rdi    # 0x14cdc8
leaq 0xf0(%rsp), %rsi
xorl %eax, %eax
callq 0xfb6f2
movl %ebx, %eax
addq $0x50f8, %rsp           # imm = 0x50F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1140c4(%rip), %rdi    # 0x14ce0b
xorl %ebx, %ebx
leaq 0xf0(%rsp), %rdx
movl %r12d, %esi
xorl %eax, %eax
callq 0xfb6f2
jmp 0x38d2c
movq %rax, %rbx
leaq 0x1a26c1(%rip), %rdi    # 0x1db428
callq 0x275d0
jmp 0x38d83
movq %rax, %rbx
movl $0xc8, %esi
movq %r13, %rdi
callq 0x27950
jmp 0x38d90
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x450ee
movq %rbx, %rdi
callq 0x28150