// File: /workspace/github_cpp_makefile_star2_O2/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 0xf51d9(%rip), %rsi     # 0x12bdb3
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x27140
leaq 0x1164b0(%rip), %rsi    # 0x14d0a3
movq %rbx, %rdi
callq 0x27b20
testq %rax, %rax
je 0x36ee7
movq %rax, %r15
movq %r12, 0x28(%rsp)
leaq 0x1005e5(%rip), %rax    # 0x1371f8
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0x10f0(%rsp), %r12
leaq 0x30(%rsp), %rbx
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
xorl %r13d, %r13d
incl %ebp
movq %r12, %rdi
movl $0x4000, %esi           # imm = 0x4000
movq %r15, %rdx
callq 0x27fe0
testq %rax, %rax
je 0x36eda
movzbl 0x10f0(%rsp), %eax
cmpl $0x23, %eax
je 0x36c38
testl %eax, %eax
je 0x36c38
subq $0x8, %rsp
movq %r12, %rdi
leaq 0xf517a(%rip), %rsi     # 0x12bdec
leaq 0x2c(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x1c(%rsp), %r8
leaq 0x40(%rsp), %r9
xorl %eax, %eax
pushq %rbx
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 0x36f13
cmpl $0x0, 0x14(%rsp)
jne 0x36d73
movslq 0x20(%rsp), %rax
cmpl 0x34(%r14), %eax
jge 0x36cf3
movq 0x28(%r14), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, 0x18(%rsp)
jmp 0x36cfc
movq $0x0, 0x18(%rsp)
movq 0x28(%rsp), %rdx
movslq 0x24(%rsp), %rcx
cmpl 0xc(%rdx), %ecx
jge 0x36d2f
movq (%rdx), %rdx
movq (%rdx,%rcx,8), %rbx
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x36d31
cmpl 0x3c(%rcx), %eax
jge 0x36d31
movq 0x30(%rcx), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, 0x8(%rsp)
jmp 0x36d3a
xorl %ebx, %ebx
movq $0x0, 0x8(%rsp)
movl $0xc8, %edi
callq 0x27940
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
callq 0x3a9ec
testq %rbx, %rbx
je 0x36d61
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3a54a
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x36d73
movq %r13, %rsi
callq 0x3bf94
movq %r13, 0x8(%rsp)
movsd 0x70(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
leaq 0x78(%rsp), %rdi
callq 0xc0dfc
movsd 0x30(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd 0x50(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
callq 0xb0524
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
leaq 0xfeefa(%rip), %rsi     # 0x135cc8
movsd 0x40(%rsp), %xmm0
callq 0xb0812
movq %r13, %rdi
movq %rbx, %rsi
callq 0xb1206
movq %rbx, %rdi
leaq 0x78(%rsp), %rsi
movq %r13, %rdx
callq 0x41d1e
cmpq $0x0, 0x18(%rsp)
leaq 0x18d647(%rip), %rdi    # 0x1c4448
leaq 0x18d600(%rip), %r13    # 0x1c4408
je 0x36e78
movb 0x18d638(%rip), %al     # 0x1c4448
testb %al, %al
je 0x36e8f
movl 0x14(%rsp), %eax
testl %eax, %eax
je 0x36e5f
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 0x36e78
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3b1c0
movsd 0x18d603(%rip), %xmm0   # 0x1c4450
movsd 0x38(%rsp), %xmm1
movq 0x8(%rsp), %rdi
callq 0x3b1f8
jmp 0x36e78
movq %r13, %rdi
movq %rbx, %rsi
callq 0x376de
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x18d5d8(%rip)   # 0x1c4450
movq %rbx, %rdi
callq 0x41e6a
movq 0x8(%rsp), %r13
leaq 0x30(%rsp), %rbx
jmp 0x36c38
callq 0x281e0
testl %eax, %eax
je 0x36e14
movq %r13, %rdi
callq 0x41ce4
leaq 0xafbf(%rip), %rdi      # 0x41e6a
leaq 0x18d556(%rip), %r13    # 0x1c4408
movq %r13, %rsi
leaq 0x187b4c(%rip), %rdx    # 0x1bea08
callq 0x27830
leaq 0x18d580(%rip), %rdi    # 0x1c4448
callq 0x275f0
leaq 0x90(%rsp), %rbx
jmp 0x36e14
movq %r15, %rdi
callq 0x27930
pushq $0x1
popq %rbx
jmp 0x36eff
leaq 0xf4eda(%rip), %rdi     # 0x12bdc8
xorl %ebx, %ebx
leaq 0xf0(%rsp), %rsi
xorl %eax, %eax
callq 0xe434a
movl %ebx, %eax
addq $0x50f8, %rsp           # imm = 0x50F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf4ef1(%rip), %rdi     # 0x12be0b
xorl %ebx, %ebx
leaq 0xf0(%rsp), %rdx
movl %ebp, %esi
xorl %eax, %eax
callq 0xe434a
jmp 0x36eff
movq %rax, %rbx
leaq 0x18d50f(%rip), %rdi    # 0x1c4448
callq 0x275d0
jmp 0x36f55
movq %rax, %rbx
movl $0xc8, %esi
movq %r13, %rdi
callq 0x27960
jmp 0x36f62
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x41e6a
movq %rbx, %rdi
callq 0x28170