// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/GSV/GSVScan.cpp
// Function: gaps::GSVScan::UpdatePoseOrientations()
void GSVScan::
UpdatePoseOrientations(void)
{
  // Get useful variables
  if (NScanlines() == 0) return;
  if (!survey) return;
  GSVLaser *laser = survey->Laser();
  if (!laser) return;
  GSVSegment *segment = survey->Segment();
  if (!segment) return;
  int mid_beam_index = laser->MaxBeamIndex()/2;
  if (mid_beam_index < 0) return;
  R3Vector front = segment->Front(Timestamp());

  // Read points
  if (!ReadPoints()) return;
  
  // Create list of points on middle beam index
  int npositions = 0;
  const int max_positions = 4096;
  R3Point positions[max_positions];
  for (int e = 0; e < NScanlines(); e++) {
    GSVScanline *scanline = Scanline(e);
    if (npositions >= max_positions) break;
    for (int k = 0; k < scanline->NPoints(); k++) {
      const GSVPoint& point = scanline->Point(k);
      if (point.BeamIndex() != mid_beam_index) continue;
      R3Vector v = point.Position() - scanline->Pose().Viewpoint();
      v.Normalize();
      if (npositions >= max_positions) break;
      positions[npositions++] = R3zero_point + v;
    }
  }

  // Release points
  if (!ReleasePoints()) return;
  
  // Compute PCA coordinate system of points
  if (npositions < 3) return;
  R3Point centroid = R3Centroid(npositions, positions);
  R3Triad principle_axes = R3PrincipleAxes(centroid, npositions, positions);

  // Compute scan orientation
  R3Vector axis2 = principle_axes[2];
  if (axis2.Dot(front) < 0) axis2.Flip();
  R3Vector axis0 = axis2 % R3posz_vector;
  axis0.Normalize();
  R3Vector axis1 = axis2 % axis0;
  axis1.Normalize();
  R3Triad triad(axis0, axis1, axis2);

  // Update pose orientation for every scanline
  for (int i = 0; i < NScanlines(); i++) {
    GSVScanline *scanline = Scanline(i);
    R3Point viewpoint = scanline->Pose().Viewpoint();
    GSVPose pose(viewpoint, triad);
    scanline->SetPose(pose);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18188, %rsp          # imm = 0x18188
cmpl $0x0, 0x34(%rdi)
je 0x3dc21
movq %rdi, %rbp
movq 0x18(%rdi), %rax
testq %rax, %rax
je 0x3dc21
movq 0x18(%rax), %rcx
testq %rcx, %rcx
je 0x3dc21
movq 0x8(%rax), %rbx
testq %rbx, %rbx
je 0x3dc21
movl 0x30(%rcx), %ecx
pushq $0x2
popq %rsi
movl %ecx, %eax
cltd
idivl %esi
cmpl $-0x1, %ecx
jl 0x3dc21
movl %eax, 0x4(%rsp)
movq %rbp, %rdi
callq 0x3ddb0
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x37ce8
movq %rbp, %rdi
callq 0x3d614
testl %eax, %eax
je 0x3dc21
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x180, %rdi            # imm = 0x180
callq 0xc0dfa
addq $0x18, %rbx
cmpq $0x18000, %rbx          # imm = 0x18000
jne 0x3db06
xorl %edx, %edx
leaq 0x138(%rsp), %r15
leaq 0x98(%rsp), %r12
xorl %r14d, %r14d
movl 0x4(%rsp), %ecx
movq %rbp, 0x8(%rsp)
movslq 0x34(%rbp), %rax
cmpq %rax, %rdx
jge 0x3dc09
cmpl $0xfff, %r14d           # imm = 0xFFF
jg 0x3dc09
movq 0x28(%rbp), %rax
movq %rdx, 0x10(%rsp)
movq (%rax,%rdx,8), %r13
leaq 0x40(%r13), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
movslq 0x30(%r13), %rax
cmpq %rax, %rbp
jge 0x3dbf7
movq 0x28(%r13), %rsi
movzbl 0x46(%rsi,%rbx), %eax
cmpl %eax, %ecx
jne 0x3dbea
addq %rbx, %rsi
movq %r15, %rdi
callq 0x310c8
movq %r12, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
callq 0xc0fd9
movq %r12, %rdi
callq 0xc18be
cmpl $0x1000, %r14d          # imm = 0x1000
jge 0x3dbf3
movq %r15, %rdi
leaq 0x58aa9f(%rip), %rsi    # 0x5c8660
movq %r12, %rdx
callq 0xc10c3
movslq %r14d, %rax
incl %r14d
imulq $0x18, %rax, %rax
leaq (%rsp,%rax), %rdi
addq $0x180, %rdi            # imm = 0x180
movq %r15, %rsi
callq 0xc10e6
movl 0x4(%rsp), %ecx
incq %rbp
addq $0x50, %rbx
jmp 0x3db75
movl 0x4(%rsp), %ecx
movq 0x10(%rsp), %rdx
incq %rdx
movq 0x8(%rsp), %rbp
jmp 0x3db41
movq %rbp, %rdi
callq 0x3d6e4
testl %eax, %eax
sete %al
cmpl $0x3, %r14d
setl %cl
orb %al, %cl
je 0x3dc33
addq $0x18188, %rsp          # imm = 0x18188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe0(%rsp), %rbx
leaq 0x180(%rsp), %r15
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x92678
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
movq %r15, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x92701
leaq 0xc8(%rsp), %rsi
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0xc15cc
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0xc1836
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3dca5
leaq 0x20(%rsp), %rdi
callq 0xc1912
leaq 0xf804c(%rip), %rdx     # 0x135cf8
leaq 0x80(%rsp), %r14
leaq 0x20(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xc1c6e
movq %r14, %rdi
callq 0xc18be
leaq 0x68(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xc1c6e
movq %r12, %rdi
callq 0xc18be
leaq 0x138(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0xaffa6
xorl %r13d, %r13d
leaq 0x50(%rsp), %r14
leaq 0xf8(%rsp), %r15
movslq 0x34(%rbp), %rax
cmpq %rax, %r13
jge 0x3dc21
movq 0x28(%rbp), %rax
movq (%rax,%r13,8), %r12
leaq 0x40(%r12), %rsi
movq %r14, %rdi
callq 0xc0e0c
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x41d52
movq %r12, %rdi
movq %r15, %rsi
callq 0x3e4a6
movq %r15, %rdi
callq 0x41e6a
incq %r13
jmp 0x3dd10
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x41e6a
movq %rbx, %rdi
callq 0x28170