// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movl %r8d, 0x4(%rsp)
movl %ecx, %ebp
movl %edx, %r13d
movq %rsi, %r15
movq %rdi, %rbx
leaq 0xcfa1c(%rip), %rax     # 0xe20e8
addq $0x10, %rax
movq %rax, (%rdi)
addq $0x8, %rdi
leaq 0xd3922(%rip), %r12     # 0xe6000
leaq 0x8(%r12), %rsi
movq %rdi, %r14
callq 0x2c2be
movl $0x88, %esi
leaq (%rbx,%rsi), %rdi
addq 0xd28e5(%rip), %rsi     # 0xe4fe0
callq 0x2c2be
movq 0x108(%r12), %rax
movq %rax, 0x108(%rbx)
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x11ac4
testl %r13d, %r13d
movl 0x4(%rsp), %r12d
jne 0x1275c
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
leaq 0xc0(%rsp), %rdi
callq 0x3cfc6
leaq 0xc0(%rsp), %rsi
movq %r14, %rdi
callq 0x2d3f8
andb $-0x3, 0x108(%rbx)
testl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
je 0x1279e
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x11cf2
leaq 0xd341f(%rip), %rax     # 0xe5b98
ucomisd (%rax), %xmm0
jbe 0x1279e
movsd 0x8f8b1(%rip), %xmm1    # 0xa2038
divsd %xmm0, %xmm1
movq %r14, %rdi
movapd %xmm1, %xmm0
callq 0x2d5a6
andb $-0x3, 0x108(%rbx)
testl %ebp, %ebp
sete %al
cmpl $0x2, %r12d
setne %cl
testb %cl, %al
jne 0x128e1
leaq 0x1d0(%rsp), %rdi
leaq 0x8(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r15, %rsi
callq 0x120f6
testl %ebp, %ebp
jne 0x12813
leaq 0x40(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x2ad48
leaq 0xc0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
xorl %edx, %edx
callq 0x29fda
leaq 0xc0(%rsp), %rsi
movq %rbx, %rdi
callq 0x2a196
leaq 0xc0(%rsp), %rdi
callq 0x2a028
cmpl $0x2, %r12d
jne 0x1289e
movsd 0x20(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x1284c
movsd 0x8f803(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d4a0
andb $-0x3, 0x108(%rbx)
xorpd %xmm0, %xmm0
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x12873
movsd 0x8f7d8(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d4a0
andb $-0x3, 0x108(%rbx)
movsd 0x30(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x1289e
movsd 0x8f7ad(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d4a0
andb $-0x3, 0x108(%rbx)
leaq 0x40(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x2ad48
leaq 0xc0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
xorl %edx, %edx
callq 0x29fda
leaq 0xc0(%rsp), %rsi
movq %rbx, %rdi
callq 0x2a196
leaq 0xc0(%rsp), %rdi
callq 0x2a028
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
leaq 0x40(%rsp), %rdi
callq 0x3cfc6
leaq 0xc0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3dc00
leaq 0xc0(%rsp), %rsi
movq %r14, %rdi
callq 0x2d3f8
andb $-0x3, 0x108(%rbx)
movq %rbx, %rax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1293f
jmp 0x12968
movq %rax, %r14
leaq 0xc0(%rsp), %rdi
callq 0x2a028
jmp 0x1296b
jmp 0x12968
jmp 0x12968
jmp 0x12968
jmp 0x12968
jmp 0x12968
movq %rax, %r14
movq %rbx, %rdi
callq 0x2a6bc
jmp 0x12973
movq %rax, %r14
movq %rbx, %rdi
callq 0x2a028
movq %r14, %rdi
callq 0xe7e0
nop