// File: /workspace/github_cpp_makefile_star2_O1/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 $0x5118, %rsp           # imm = 0x5118
movq %rsi, %r12
movq %rdi, %r14
movq 0x8(%rdi), %rax
movq 0x48(%rdi), %rcx
movq 0x20(%rax), %rdx
leaq 0x116258(%rip), %rsi    # 0x14edb2
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x27140
leaq 0x137870(%rip), %rsi    # 0x1703e3
movq %rbx, %rdi
callq 0x27b30
testq %rax, %rax
je 0x38f42
movq %rax, %r15
movq %r12, 0x58(%rsp)
leaq 0x121725(%rip), %rax    # 0x15a2b8
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x1110(%rsp), %rbp
leaq 0x48(%rsp), %rbx
leaq 0x116259(%rip), %r13    # 0x14ee0a
xorl %r12d, %r12d
movq $0x0, 0x28(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
movq %rbp, %rdi
movl $0x4000, %esi           # imm = 0x4000
movq %r15, %rdx
callq 0x27fd0
testq %rax, %rax
je 0x38f33
incl %r12d
movzbl 0x1110(%rsp), %eax
cmpl $0x23, %eax
je 0x38c0e
testl %eax, %eax
jne 0x38c2b
movq %rbp, %rdi
movl $0x4000, %esi           # imm = 0x4000
movq %r15, %rdx
callq 0x27fd0
incl %r12d
testq %rax, %rax
jne 0x38bfd
jmp 0x38f33
subq $0x8, %rsp
movq %rbp, %rdi
leaq 0x1161b2(%rip), %rsi    # 0x14edeb
leaq 0x44(%rsp), %rdx
leaq 0x24(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x58(%rsp), %r9
xorl %eax, %eax
pushq %rbx
leaq 0x78(%rsp), %r10
pushq %r10
leaq 0x88(%rsp), %r10
pushq %r10
leaq 0x98(%rsp), %r10
pushq %r10
leaq 0xa8(%rsp), %r10
pushq %r10
leaq 0xb8(%rsp), %r10
pushq %r10
leaq 0xc8(%rsp), %r10
pushq %r10
callq 0x27900
addq $0x40, %rsp
movl %eax, %ebx
cmpl $0xb, %eax
jne 0x38cc4
movl %ebx, 0x14(%rsp)
movl 0x18(%rsp), %eax
testl %eax, %eax
movq 0x40(%rsp), %rbx
jne 0x38ce7
movslq 0x1c(%rsp), %rcx
cmpl 0x34(%r14), %ecx
jge 0x38cde
movq 0x28(%r14), %rdx
movq (%rdx,%rcx,8), %rcx
movq %rcx, 0x28(%rsp)
jmp 0x38ce7
movq %r13, %rdi
movl %r12d, %esi
leaq 0x110(%rsp), %rdx
xorl %eax, %eax
callq 0xfd64a
jmp 0x38ec9
movq $0x0, 0x28(%rsp)
testl %eax, %eax
jne 0x38d17
movslq 0x3c(%rsp), %rcx
movq 0x58(%rsp), %rdx
cmpl 0xc(%rdx), %ecx
jge 0x38d0c
movq (%rdx), %rdx
movq (%rdx,%rcx,8), %rdx
movq %rdx, 0x30(%rsp)
movq 0x8(%rdx), %rbx
jmp 0x38d17
xorl %ebx, %ebx
movq $0x0, 0x30(%rsp)
testl %eax, %eax
jne 0x38d42
testq %rbx, %rbx
je 0x38d39
movslq 0x1c(%rsp), %rcx
cmpl 0x3c(%rbx), %ecx
jge 0x38d39
movq 0x30(%rbx), %rdx
movq (%rdx,%rcx,8), %rcx
movq %rcx, 0x20(%rsp)
jmp 0x38d42
movq $0x0, 0x20(%rsp)
testl %eax, %eax
jne 0x38d87
movl $0xc8, %edi
callq 0x27930
movq %rax, 0x8(%rsp)
movq %rax, %rdi
xorl %esi, %esi
callq 0x3d2b4
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x38d73
movq 0x8(%rsp), %rsi
callq 0x3cdc6
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x38d87
movq 0x8(%rsp), %rsi
callq 0x3eb92
movq %rbx, 0x40(%rsp)
movsd 0x90(%rsp), %xmm0
movsd 0x88(%rsp), %xmm1
movsd 0x80(%rsp), %xmm2
leaq 0x98(%rsp), %rdi
callq 0xd5e2e
movsd 0x48(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x70(%rsp), %xmm2
movsd 0x68(%rsp), %xmm3
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
callq 0xc2ebe
leaq 0xb0(%rsp), %r13
movq %r13, %rdi
leaq 0x11ff82(%rip), %rsi    # 0x158d70
movsd 0x60(%rsp), %xmm0
callq 0xc3182
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc3c7c
movq %r13, %rdi
leaq 0x98(%rsp), %rsi
movq %rbx, %rdx
callq 0x4546e
cmpq $0x0, 0x28(%rsp)
movl 0x14(%rsp), %ebx
je 0x38eba
movb 0x1a460b(%rip), %al     # 0x1dd438
testb %al, %al
je 0x38edc
movl 0x18(%rsp), %eax
testl %eax, %eax
je 0x38e84
movq 0x28(%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 0x38eba
movq 0x8(%rsp), %rdi
leaq 0x1a4596(%rip), %rsi    # 0x1dd3f8
movq %r13, %rdx
callq 0x3dbfc
movsd 0x1a45ce(%rip), %xmm0   # 0x1dd440
movsd 0x50(%rsp), %xmm1
movq 0x8(%rsp), %rdi
callq 0x3dc60
jmp 0x38eba
leaq 0x1a4575(%rip), %rdi    # 0x1dd400
leaq 0xb8(%rsp), %rsi
callq 0xd619c
leaq 0x1a4579(%rip), %rdi    # 0x1dd418
leaq 0xd0(%rsp), %rsi
callq 0xc3158
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x1a4586(%rip)   # 0x1dd440
movq %r13, %rdi
callq 0x455ba
leaq 0x115f41(%rip), %r13    # 0x14ee0a
cmpl $0xb, %ebx
leaq 0x48(%rsp), %rbx
je 0x38be1
jmp 0x38f6e
leaq 0x1a4555(%rip), %rdi    # 0x1dd438
callq 0x281d0
testl %eax, %eax
je 0x38e35
leaq 0x1a4501(%rip), %rdi    # 0x1dd3f8
callq 0x45434
leaq 0xc6b7(%rip), %rdi      # 0x455ba
leaq 0x1a44ee(%rip), %rsi    # 0x1dd3f8
leaq 0x19eae7(%rip), %rdx    # 0x1d79f8
callq 0x27830
leaq 0x1a451b(%rip), %rdi    # 0x1dd438
callq 0x275f0
leaq 0xb0(%rsp), %r13
movl 0x14(%rsp), %ebx
jmp 0x38e35
movq %r15, %rdi
callq 0x27920
movl $0x1, %ebx
jmp 0x38f5a
leaq 0x115e7e(%rip), %rdi    # 0x14edc7
xorl %ebx, %ebx
leaq 0x110(%rsp), %rsi
xorl %eax, %eax
callq 0xfd64a
movl %ebx, %eax
addq $0x5118, %rsp           # imm = 0x5118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x38f5a
movq %rax, %rbx
leaq 0x1a44bc(%rip), %rdi    # 0x1dd438
callq 0x275d0
jmp 0x38f9a
movq %rax, %rbx
movl $0xc8, %esi
movq 0x8(%rsp), %rdi
callq 0x27950
jmp 0x38fa7
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x455ba
movq %rbx, %rdi
callq 0x28160
nop