// File: tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3SegmentationCluster::PrincipleAxes(gaps::R3Point*, double*) const
R3Triad R3SegmentationCluster::
PrincipleAxes(R3Point *returned_center, RNScalar *returned_variances) const
{
  // Check if there are at least three points
  if (points.NEntries() < 3) {
    if (returned_center) *returned_center = primitive.centroid;
    if (returned_variances) returned_variances[0] = returned_variances[1] = returned_variances[2] = 0;
    return R3xyz_triad;
  }

  // Fill arrays of point positions and weights
  RNArray<R3Point *> positions;
  for (int i = 0; i < points.NEntries(); i++) {
    positions.Insert(&points[i]->position);
  }

  // Compute center (should be nearly same as primitive.centroid)
  R3Point center = R3Centroid(positions);
  if (returned_center) *returned_center = center;

  // Compute principle axes
  R3Triad axes = R3PrincipleAxes(center, positions, NULL, returned_variances);

  // Check if orientation is compatible with primitive
  if (primitive.primitive_type == R3_SEGMENTATION_LINE_PRIMITIVE_TYPE) {
    if (primitive.line.Vector().Dot(axes[0]) < 0) axes.Reset(-axes[0], -axes[1], axes[2]);
  }
  else if (primitive.primitive_type == R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE) {
    if (primitive.plane.Normal().Dot(axes[2]) < 0) axes.Reset(-axes[0], axes[1], -axes[2]);
  }

  // Return principle axes
  return axes;
}

// Assembly:
subq $0x148, %rsp            # imm = 0x148
movq %rdi, 0x70(%rsp)
movq %rdi, %rax
movq %rax, 0x78(%rsp)
movq %rdi, 0x140(%rsp)
movq %rsi, 0x138(%rsp)
movq %rdx, 0x130(%rsp)
movq %rcx, 0x128(%rsp)
movq 0x138(%rsp), %rdi
movq %rdi, 0x80(%rsp)
addq $0x8, %rdi
callq 0x1ecf0
cmpl $0x3, %eax
jge 0x3f0fa
cmpq $0x0, 0x130(%rsp)
je 0x3f0aa
movq 0x80(%rsp), %rsi
addq $0x40, %rsi
addq $0x40, %rsi
movq 0x130(%rsp), %rdi
callq 0x12b3e0
cmpq $0x0, 0x128(%rsp)
je 0x3f0e4
movq 0x128(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rax)
movq 0x128(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movq 0x128(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x70(%rsp), %rdi
leaq 0x22a8b8(%rip), %rsi    # 0x2699a8
callq 0x108730
jmp 0x3f418
leaq 0x118(%rsp), %rdi
callq 0x47720
movl $0x0, 0x114(%rsp)
movq 0x80(%rsp), %rdi
movl 0x114(%rsp), %eax
movl %eax, 0x68(%rsp)
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x6c(%rsp)
jmp 0x3f134
movl 0x68(%rsp), %eax
movl 0x6c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3f1b2
movq 0x80(%rsp), %rdi
addq $0x8, %rdi
movl 0x114(%rsp), %esi
callq 0x1f8f0
movq %rax, 0x60(%rsp)
jmp 0x3f15f
movq 0x60(%rsp), %rsi
addq $0x8, %rsi
leaq 0x118(%rsp), %rdi
callq 0x47730
jmp 0x3f177
jmp 0x3f179
movl 0x114(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
jmp 0x3f112
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x108(%rsp)
movl %eax, 0x104(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x47760
jmp 0x3f425
xorl %eax, %eax
movl %eax, %edx
leaq 0xe8(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0xc66c0
jmp 0x3f1cd
cmpq $0x0, 0x130(%rsp)
je 0x3f1f1
movq 0x130(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x12b3e0
jmp 0x3f1ef
jmp 0x3f1f1
movq 0x70(%rsp), %rdi
movq 0x128(%rsp), %r8
xorl %eax, %eax
movl %eax, %ecx
leaq 0xe8(%rsp), %rsi
leaq 0x118(%rsp), %rdx
callq 0xc67e0
jmp 0x3f219
movq 0x80(%rsp), %rax
cmpl $0x2, 0x40(%rax)
jne 0x3f312
movq 0x80(%rsp), %rdi
addq $0x98, %rdi
callq 0x47560
movq %rax, 0x58(%rsp)
jmp 0x3f246
movq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x36020
movq %rax, 0x50(%rsp)
jmp 0x3f259
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdi
callq 0x12c4c0
movsd %xmm0, 0x48(%rsp)
jmp 0x3f270
movsd 0x48(%rsp), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3f30d
movq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x36020
movq %rax, 0x40(%rsp)
jmp 0x3f296
movq 0x40(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x12d1a0
jmp 0x3f2aa
movq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x36020
movq %rax, 0x38(%rsp)
jmp 0x3f2c0
movq 0x38(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x12d1a0
jmp 0x3f2d4
movq 0x70(%rsp), %rdi
movl $0x2, %esi
callq 0x36020
movq %rax, 0x30(%rsp)
jmp 0x3f2ea
movq 0x30(%rsp), %rcx
movq 0x70(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
callq 0x108850
jmp 0x3f30b
jmp 0x3f30d
jmp 0x3f40b
movq 0x80(%rsp), %rax
cmpl $0x3, 0x40(%rax)
jne 0x3f409
movq 0x80(%rsp), %rdi
addq $0xc8, %rdi
callq 0x2dca0
movq %rax, 0x28(%rsp)
jmp 0x3f33f
movq 0x70(%rsp), %rdi
movl $0x2, %esi
callq 0x36020
movq %rax, 0x20(%rsp)
jmp 0x3f355
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x12c4c0
movsd %xmm0, 0x18(%rsp)
jmp 0x3f36c
movsd 0x18(%rsp), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3f407
movq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x36020
movq %rax, 0x10(%rsp)
jmp 0x3f392
movq 0x10(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x12d1a0
jmp 0x3f3a6
movq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x36020
movq %rax, 0x8(%rsp)
jmp 0x3f3bc
movq 0x70(%rsp), %rdi
movl $0x2, %esi
callq 0x36020
movq %rax, (%rsp)
jmp 0x3f3d1
movq (%rsp), %rsi
leaq 0x88(%rsp), %rdi
callq 0x12d1a0
jmp 0x3f3e4
movq 0x8(%rsp), %rdx
movq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x88(%rsp), %rcx
callq 0x108850
jmp 0x3f405
jmp 0x3f407
jmp 0x3f409
jmp 0x3f40b
leaq 0x118(%rsp), %rdi
callq 0x47760
movq 0x78(%rsp), %rax
addq $0x148, %rsp            # imm = 0x148
retq
movq 0x108(%rsp), %rdi
callq 0x16ca0
nopw %cs:(%rax,%rax)