// File: /workspace/github_cpp_makefile_star2_O1/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 0xcfab0(%rip), %rax     # 0xe20e8
addq $0x10, %rax
movq %rax, (%rdi)
addq $0x8, %rdi
leaq 0xd39b6(%rip), %r12     # 0xe6000
leaq 0x8(%r12), %rsi
movq %rdi, %r14
callq 0x2c5ba
movl $0x88, %esi
leaq (%rbx,%rsi), %rdi
addq 0xd2979(%rip), %rsi     # 0xe4fe0
callq 0x2c5ba
movq 0x108(%r12), %rax
movq %rax, 0x108(%rbx)
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x11b04
testl %r13d, %r13d
movl 0x4(%rsp), %r12d
jne 0x126c8
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
leaq 0xc0(%rsp), %rdi
callq 0x3d4be
leaq 0xc0(%rsp), %rsi
movq %r14, %rdi
callq 0x2d6f4
andb $-0x3, 0x108(%rbx)
testl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
je 0x1270a
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x11d32
leaq 0xd34b3(%rip), %rax     # 0xe5b98
ucomisd (%rax), %xmm0
jbe 0x1270a
movsd 0x8f945(%rip), %xmm1    # 0xa2038
divsd %xmm0, %xmm1
movq %r14, %rdi
movapd %xmm1, %xmm0
callq 0x2d8a2
andb $-0x3, 0x108(%rbx)
testl %ebp, %ebp
sete %al
cmpl $0x2, %r12d
setne %cl
testb %cl, %al
jne 0x1284d
leaq 0x1d0(%rsp), %rdi
leaq 0x8(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r15, %rsi
callq 0x12148
testl %ebp, %ebp
jne 0x1277f
leaq 0x40(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x2af90
leaq 0xc0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
xorl %edx, %edx
callq 0x2a22e
leaq 0xc0(%rsp), %rsi
movq %rbx, %rdi
callq 0x2a3ea
leaq 0xc0(%rsp), %rdi
callq 0x2a27c
cmpl $0x2, %r12d
jne 0x1280a
movsd 0x20(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x127b8
movsd 0x8f897(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d79c
andb $-0x3, 0x108(%rbx)
xorpd %xmm0, %xmm0
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x127df
movsd 0x8f86c(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d79c
andb $-0x3, 0x108(%rbx)
movsd 0x30(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x1280a
movsd 0x8f841(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d79c
andb $-0x3, 0x108(%rbx)
leaq 0x40(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x2af90
leaq 0xc0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
xorl %edx, %edx
callq 0x2a22e
leaq 0xc0(%rsp), %rsi
movq %rbx, %rdi
callq 0x2a3ea
leaq 0xc0(%rsp), %rdi
callq 0x2a27c
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
leaq 0x40(%rsp), %rdi
callq 0x3d4be
leaq 0xc0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3e154
leaq 0xc0(%rsp), %rsi
movq %r14, %rdi
callq 0x2d6f4
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 0x128ab
jmp 0x128d4
movq %rax, %r14
leaq 0xc0(%rsp), %rdi
callq 0x2a27c
jmp 0x128d7
jmp 0x128d4
jmp 0x128d4
jmp 0x128d4
jmp 0x128d4
jmp 0x128d4
movq %rax, %r14
movq %rbx, %rdi
callq 0x2a904
jmp 0x128df
movq %rax, %r14
movq %rbx, %rdi
callq 0x2a27c
movq %r14, %rdi
callq 0xe7e0
nop