// File: 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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x51b8, %rsp           # imm = 0x51B8
movq %rdi, 0x51a8(%rsp)
movq %rsi, 0x51a0(%rsp)
movq 0x51a8(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x41a0(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movq 0x8(%rax), %rdi
callq 0x47cf0
movq 0x58(%rsp), %rdi
movq %rax, 0x68(%rsp)
callq 0x44310
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rdx
movq %rax, %rcx
leaq 0x2205fe(%rip), %rsi    # 0x266eb2
movb $0x0, %al
callq 0x2a280
leaq 0x41a0(%rsp), %rdi
leaq 0x23cb29(%rip), %rsi    # 0x2833f3
callq 0x2a520
movq %rax, 0x4198(%rsp)
cmpq $0x0, 0x4198(%rsp)
jne 0x46908
leaq 0x41a0(%rsp), %rsi
leaq 0x2205d6(%rip), %rdi    # 0x266ec7
movb $0x0, %al
callq 0x1cbff0
movl $0x0, 0x51b4(%rsp)
jmp 0x46e53
movl $0x0, 0x4194(%rsp)
movl $0x4000, 0x4190(%rsp) # imm = 0x4000
movq $0x0, 0x188(%rsp)
movq $0x0, 0x180(%rsp)
movq $0x0, 0x178(%rsp)
movq $0x0, 0x170(%rsp)
movq $0x0, 0x168(%rsp)
leaq 0x190(%rsp), %rdi
movq 0x4198(%rsp), %rdx
movl $0x4000, %esi           # imm = 0x4000
callq 0x29bd0
cmpq $0x0, %rax
je 0x46e3b
movl 0x4194(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4194(%rsp)
movsbl 0x190(%rsp), %eax
cmpl $0x0, %eax
jne 0x4699e
jmp 0x4695a
movsbl 0x190(%rsp), %eax
cmpl $0x23, %eax
jne 0x469ad
jmp 0x4695a
leaq 0x190(%rsp), %rdi
leaq 0x22052f(%rip), %rsi    # 0x266eeb
leaq 0x164(%rsp), %rdx
leaq 0x160(%rsp), %rcx
leaq 0x15c(%rsp), %r8
leaq 0x150(%rsp), %r9
leaq 0x148(%rsp), %r12
leaq 0x140(%rsp), %r15
leaq 0x138(%rsp), %r14
leaq 0x130(%rsp), %rbx
leaq 0x128(%rsp), %r11
leaq 0x120(%rsp), %r10
leaq 0x118(%rsp), %rax
movq %r12, (%rsp)
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movb $0x0, %al
callq 0x29150
cmpl $0xb, %eax
je 0x46a6f
movl 0x4194(%rsp), %esi
leaq 0x41a0(%rsp), %rdx
leaq 0x2204b2(%rip), %rdi    # 0x266f0a
movb $0x0, %al
callq 0x1cbff0
movl $0x0, 0x51b4(%rsp)
jmp 0x46e53
cmpl $0x0, 0x15c(%rsp)
jne 0x46abf
movq 0x58(%rsp), %rdi
movq $0x0, 0x188(%rsp)
movl 0x160(%rsp), %eax
movl %eax, 0x54(%rsp)
callq 0x44320
movl %eax, %ecx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x46abd
movq 0x58(%rsp), %rdi
movl 0x160(%rsp), %esi
callq 0x447e0
movq %rax, 0x188(%rsp)
jmp 0x46abf
cmpl $0x0, 0x15c(%rsp)
jne 0x46b36
movq $0x0, 0x170(%rsp)
movq $0x0, 0x180(%rsp)
movl 0x164(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x51a0(%rsp), %rdi
callq 0x38730
movl %eax, %ecx
movl 0x50(%rsp), %eax
cmpl %ecx, %eax
jge 0x46b34
movq 0x51a0(%rsp), %rdi
movl 0x164(%rsp), %esi
callq 0x3a400
movq %rax, 0x170(%rsp)
movq 0x170(%rsp), %rdi
callq 0x47d30
movq %rax, 0x180(%rsp)
jmp 0x46b36
cmpl $0x0, 0x15c(%rsp)
jne 0x46b99
movq $0x0, 0x178(%rsp)
cmpq $0x0, 0x180(%rsp)
je 0x46b97
movl 0x160(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0x180(%rsp), %rdi
callq 0x44b60
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x46b95
movq 0x180(%rsp), %rdi
movl 0x160(%rsp), %esi
callq 0x44810
movq %rax, 0x178(%rsp)
jmp 0x46b97
jmp 0x46b99
cmpl $0x0, 0x15c(%rsp)
jne 0x46c40
movl $0xc8, %edi
callq 0x292e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
xorl %eax, %eax
movl %eax, %esi
callq 0x4f450
jmp 0x46bc7
movq 0x40(%rsp), %rax
movq %rax, 0x168(%rsp)
cmpq $0x0, 0x170(%rsp)
je 0x46c1e
movq 0x170(%rsp), %rdi
movq 0x168(%rsp), %rsi
callq 0x4e970
jmp 0x46c1e
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
movl $0xc8, %esi
callq 0x291f0
jmp 0x46e69
cmpq $0x0, 0x178(%rsp)
je 0x46c3e
movq 0x178(%rsp), %rdi
movq 0x168(%rsp), %rsi
callq 0x523c0
jmp 0x46c40
movsd 0x148(%rsp), %xmm0
movsd 0x140(%rsp), %xmm1
movsd 0x138(%rsp), %xmm2
leaq 0xf0(%rsp), %rdi
callq 0x17fd90
movsd 0x118(%rsp), %xmm0
movsd 0x130(%rsp), %xmm1
movsd 0x128(%rsp), %xmm2
movsd 0x120(%rsp), %xmm3
leaq 0xd0(%rsp), %rdi
callq 0x15e2d0
leaq 0x2272d8(%rip), %rax    # 0x26df78
movsd (%rax), %xmm0
leaq 0xb0(%rsp), %rdi
leaq 0x6bc70d(%rip), %rsi    # 0x7033c0
callq 0x15ea20
leaq 0xd0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x160270
leaq 0x70(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
callq 0x5e450
cmpq $0x0, 0x188(%rsp)
je 0x46e20
cmpb $0x0, 0x2b8273(%rip)    # 0x2fef70
jne 0x46d44
leaq 0x2b826a(%rip), %rdi    # 0x2fef70
callq 0x296a0
cmpl $0x0, %eax
je 0x46d44
leaq 0x2b8219(%rip), %rdi    # 0x2fef30
callq 0x5e3f0
jmp 0x46d1e
leaq 0x1795b(%rip), %rdi     # 0x5e680
leaq 0x2b8204(%rip), %rsi    # 0x2fef30
leaq 0x2a8db5(%rip), %rdx    # 0x2efae8
callq 0x29fa0
leaq 0x2b8231(%rip), %rdi    # 0x2fef70
callq 0x29a40
cmpl $0x0, 0x15c(%rsp)
jne 0x46db2
leaq 0x2b81db(%rip), %rdi    # 0x2fef30
leaq 0x70(%rsp), %rsi
callq 0x47d40
jmp 0x46d61
movsd 0x150(%rsp), %xmm0
movsd %xmm0, 0x2b8206(%rip)   # 0x2fef78
jmp 0x46e1e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
leaq 0x2b81de(%rip), %rdi    # 0x2fef70
callq 0x297c0
jmp 0x46e2f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x46e2f
movl 0x15c(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x188(%rsp), %rdi
callq 0x44a60
movl %eax, 0x3c(%rsp)
jmp 0x46dd0
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x46e1c
movq 0x168(%rsp), %rdi
leaq 0x2b8142(%rip), %rsi    # 0x2fef30
leaq 0x70(%rsp), %rdx
callq 0x50640
jmp 0x46dfa
movq 0x168(%rsp), %rdi
movsd 0x2b816e(%rip), %xmm0   # 0x2fef78
movsd 0x150(%rsp), %xmm1
callq 0x506a0
jmp 0x46e1a
jmp 0x46e1c
jmp 0x46e1e
jmp 0x46e20
leaq 0x70(%rsp), %rdi
callq 0x5e680
jmp 0x4695a
leaq 0x70(%rsp), %rdi
callq 0x5e680
jmp 0x46e69
movq 0x4198(%rsp), %rdi
callq 0x297e0
movl $0x1, 0x51b4(%rsp)
movl 0x51b4(%rsp), %eax
addq $0x51b8, %rsp           # imm = 0x51B8
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x110(%rsp), %rdi
callq 0x29c10
nopw %cs:(%rax,%rax)