// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
cmpl $0x2, 0x14(%rsi)
jg 0x2aa2e
testq %r12, %r12
je 0x2aa04
subq $-0x80, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x9e4d6
testq %r15, %r15
je 0x2aa1a
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
movq $0x0, 0x10(%r15)
leaq 0x145567(%rip), %rsi    # 0x16ff88
movq %rbx, %rdi
callq 0x8af72
jmp 0x2ab59
movq %rsp, %rdi
callq 0xc352c
cmpl $0x0, 0x14(%r14)
jle 0x2aa66
xorl %ebp, %ebp
movq %rsp, %r13
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %rsi
addq $0x8, %rsi
movl 0xc(%rsp), %edx
movq %r13, %rdi
callq 0xc35c2
incq %rbp
movslq 0x14(%r14), %rax
cmpq %rax, %rbp
jl 0x2aa42
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
xorl %edx, %edx
callq 0x6c618
testq %r12, %r12
je 0x2aa87
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x9e4d6
leaq 0x40(%rsp), %rsi
movq %rsp, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
movq %r15, %r8
callq 0x6c6c5
movl 0x40(%r14), %eax
cmpl $0x3, %eax
je 0x2ab01
cmpl $0x2, %eax
jne 0x2ab51
addq $0xb0, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9ee8e
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2ab51
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x9f83c
leaq 0x18(%rbx), %rsi
leaq 0x10(%rsp), %rdi
callq 0x9f83c
leaq 0x30(%rbx), %rcx
leaq 0x28(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x8b01e
jmp 0x2ab51
addq $0xc8, %r14
leaq 0x30(%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x9ee8e
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2ab51
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x9f83c
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x9f83c
leaq 0x18(%rbx), %rdx
leaq 0x28(%rsp), %rsi
leaq 0x10(%rsp), %rcx
movq %rbx, %rdi
callq 0x8b01e
movq %rsp, %rdi
callq 0xc3596
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2ab75
jmp 0x2ab75
jmp 0x2ab75
jmp 0x2ab75
jmp 0x2ab75
movq %rax, %rbx
movq %rsp, %rdi
callq 0xc3596
movq %rbx, %rdi
callq 0x15ad0