// File: /workspace/github_cpp_makefile_star2_O2/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 0x28b5b
testq %r12, %r12
je 0x28b36
subq $-0x80, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x9067a
testq %r15, %r15
je 0x28b47
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
leaq 0x13445a(%rip), %rsi    # 0x15cfa8
movq %rbx, %rdi
callq 0x7f4b4
jmp 0x28c7d
movq %rsp, %r13
movq %r13, %rdi
callq 0xb1eda
xorl %ebp, %ebp
movslq 0x14(%r14), %rax
cmpq %rax, %rbp
jge 0x28b8a
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %rsi
addq $0x8, %rsi
movq %r13, %rdi
callq 0x1a04c
incq %rbp
jmp 0x28b68
leaq 0x40(%rsp), %rdi
movq %rsp, %rsi
xorl %edx, %edx
callq 0x63717
testq %r12, %r12
je 0x28bab
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x9067a
leaq 0x40(%rsp), %rsi
movq %rsp, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
movq %r15, %r8
callq 0x637bf
movl 0x40(%r14), %eax
cmpl $0x3, %eax
je 0x28c25
cmpl $0x2, %eax
jne 0x28c75
addq $0xb0, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x90dca
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x28c75
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x914b6
leaq 0x18(%rbx), %rsi
leaq 0x10(%rsp), %rdi
callq 0x914b6
leaq 0x30(%rbx), %rcx
leaq 0x28(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x7f54c
jmp 0x28c75
addq $0xc8, %r14
leaq 0x30(%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x90dca
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x28c75
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x914b6
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x914b6
leaq 0x18(%rbx), %rdx
leaq 0x28(%rsp), %rsi
leaq 0x10(%rsp), %rcx
movq %rbx, %rdi
callq 0x7f54c
movq %rsp, %rdi
callq 0xb1f48
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x28c99
jmp 0x28c99
jmp 0x28c99
jmp 0x28c99
jmp 0x28c99
movq %rax, %rbx
movq %rsp, %rdi
callq 0xb1f48
movq %rbx, %rdi
callq 0x15af0