// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::PCANormalizationTransformation(int, int, int) const
R3Affine R3Mesh::
PCANormalizationTransformation(RNBoolean translate, RNBoolean rotate, int scale) const
{
  // Initialize transformation
  R3Affine affine(R3identity_affine);

  // Compute center of mass
  R3Point centroid = Centroid();

  // Translate center of mass back to original (if not translating)
  if (!translate) {
    affine.Translate(centroid.Vector());
  }

  // Scale by inverse of radius
  if ((scale != 0) && (scale != 2)) {
    RNScalar radius = AverageRadius(&centroid);
    if (RNIsPositive(radius)) affine.Scale(1.0 / radius);
  }

  // Rotate to align principal axes with XYZ
  if (rotate || (scale == 2)) {
    RNScalar variances[3];
    R3Triad triad = PrincipleAxes(&centroid, variances);
    if (!rotate) affine.Transform(R3Affine(triad.InverseMatrix()));
    if (scale == 2) {
      if (variances[0] > 0) affine.XScale(1.0 / variances[0]);
      if (variances[1] > 0) affine.XScale(1.0 / variances[1]);
      if (variances[2] > 0) affine.XScale(1.0 / variances[2]);
    }
    affine.Transform(R3Affine(triad.InverseMatrix()));
  }

  // Translate center of mass to origin
  affine.Translate(-(centroid.Vector()));
  
  // Return PCA normalization transformation
  return affine;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x210, %rsp            # imm = 0x210
movl %r8d, %ebp
movl %ecx, %r14d
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
leaq 0xc91c5(%rip), %rsi     # 0xdb010
callq 0x21282
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x1133a
testl %r12d, %r12d
jne 0x11e84
leaq 0xb8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x212f8
leaq 0xb8(%rsp), %rsi
movq %rbx, %rdi
callq 0x212e2
testl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
je 0x11ebe
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x11532
leaq 0xc8d08(%rip), %rax     # 0xdaba8
ucomisd (%rax), %xmm0
jbe 0x11ebe
movsd 0x8115a(%rip), %xmm1    # 0x93008
divsd %xmm0, %xmm1
movq %rbx, %rdi
movapd %xmm1, %xmm0
callq 0x21314
testl %r14d, %r14d
sete %al
cmpl $0x2, %ebp
setne %cl
testb %cl, %al
jne 0x11fe8
leaq 0x1c8(%rsp), %rdi
leaq 0x8(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r15, %rsi
callq 0x118ae
testl %r14d, %r14d
jne 0x11f34
leaq 0x38(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x28386
leaq 0xb8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0x276a0
leaq 0xb8(%rsp), %rsi
movq %rbx, %rdi
callq 0x2785a
leaq 0xb8(%rsp), %rdi
callq 0x276ee
cmpl $0x2, %ebp
jne 0x11fa5
movsd 0x20(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x11f61
movsd 0x810b7(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
movq %rbx, %rdi
callq 0x2132a
xorpd %xmm0, %xmm0
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x11f81
movsd 0x81093(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
movq %rbx, %rdi
callq 0x2132a
movsd 0x30(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x11fa5
movsd 0x8106f(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
movq %rbx, %rdi
callq 0x2132a
leaq 0x38(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x28386
leaq 0xb8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0x276a0
leaq 0xb8(%rsp), %rsi
movq %rbx, %rdi
callq 0x2785a
leaq 0xb8(%rsp), %rdi
callq 0x276ee
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x212f8
leaq 0xb8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x38c72
leaq 0xb8(%rsp), %rsi
movq %rbx, %rdi
callq 0x212e2
movq %rbx, %rax
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x12030
jmp 0x1204c
movq %rax, %r14
leaq 0xb8(%rsp), %rdi
callq 0x276ee
jmp 0x1204f
jmp 0x1204c
jmp 0x1204c
jmp 0x1204c
jmp 0x1204c
jmp 0x1204c
movq %rax, %r14
movq %rbx, %rdi
callq 0x276ee
movq %r14, %rdi
callq 0xe810
nop