// File: /workspace/github_cpp_makefile_star2_O1/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 0x2ad7e
testq %r12, %r12
je 0x2ad54
subq $-0x80, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x9f83c
testq %r15, %r15
je 0x2ad6a
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
movq $0x0, 0x10(%r15)
leaq 0x147227(%rip), %rsi    # 0x171f98
movq %rbx, %rdi
callq 0x8bf76
jmp 0x2aea9
movq %rsp, %rdi
callq 0xc4b50
cmpl $0x0, 0x14(%r14)
jle 0x2adb6
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 0xc4be6
incq %rbp
movslq 0x14(%r14), %rax
cmpq %rax, %rbp
jl 0x2ad92
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
xorl %edx, %edx
callq 0x6d40c
testq %r12, %r12
je 0x2add7
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x9f83c
leaq 0x40(%rsp), %rsi
movq %rsp, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
movq %r15, %r8
callq 0x6d4b8
movl 0x40(%r14), %eax
cmpl $0x3, %eax
je 0x2ae51
cmpl $0x2, %eax
jne 0x2aea1
addq $0xb0, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa01fe
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2aea1
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0xa0bfc
leaq 0x18(%rbx), %rsi
leaq 0x10(%rsp), %rdi
callq 0xa0bfc
leaq 0x30(%rbx), %rcx
leaq 0x28(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x8c022
jmp 0x2aea1
addq $0xc8, %r14
leaq 0x30(%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xa01fe
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2aea1
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0xa0bfc
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0xa0bfc
leaq 0x18(%rbx), %rdx
leaq 0x28(%rsp), %rsi
leaq 0x10(%rsp), %rcx
movq %rbx, %rdi
callq 0x8c022
movq %rsp, %rdi
callq 0xc4bba
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2aec5
jmp 0x2aec5
jmp 0x2aec5
jmp 0x2aec5
jmp 0x2aec5
movq %rax, %rbx
movq %rsp, %rdi
callq 0xc4bba
movq %rbx, %rdi
callq 0x15ae0