// File: 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:
subq $0x448, %rsp            # imm = 0x448
movq %rdi, 0x10(%rsp)
movq %rdi, %rax
movq %rax, 0x18(%rsp)
movq %rdi, 0x440(%rsp)
movq %rsi, 0x438(%rsp)
movl %edx, 0x434(%rsp)
movl %ecx, 0x430(%rsp)
movl %r8d, 0x42c(%rsp)
movq 0x438(%rsp), %rax
movq %rax, 0x20(%rsp)
movb $0x0, 0x42b(%rsp)
leaq 0x16257a(%rip), %rsi    # 0x178950
callq 0x36c20
movq 0x20(%rsp), %rsi
leaq 0x410(%rsp), %rdi
callq 0x14e10
jmp 0x163ef
cmpl $0x0, 0x434(%rsp)
jne 0x1643f
leaq 0x3e8(%rsp), %rdi
leaq 0x410(%rsp), %rsi
callq 0x36d10
jmp 0x16410
movq 0x10(%rsp), %rdi
leaq 0x3e8(%rsp), %rsi
callq 0x36cd0
jmp 0x16424
jmp 0x1643f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x408(%rsp)
movl %eax, 0x404(%rsp)
jmp 0x1670a
cmpl $0x0, 0x42c(%rsp)
je 0x164cd
cmpl $0x2, 0x42c(%rsp)
je 0x164cd
movq 0x20(%rsp), %rdi
leaq 0x410(%rsp), %rsi
callq 0x15130
movsd %xmm0, 0x8(%rsp)
jmp 0x16471
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x3e0(%rsp)
movsd 0x3e0(%rsp), %xmm0
leaq 0x162050(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
movl %eax, 0x4(%rsp)
jmp 0x1649f
movl 0x4(%rsp), %eax
cmpl $0x0, %eax
je 0x164cb
movq 0x10(%rsp), %rdi
movsd 0x3e0(%rsp), %xmm1
movsd 0x11cb4a(%rip), %xmm0   # 0x133008
divsd %xmm1, %xmm0
callq 0x36d80
jmp 0x164c9
jmp 0x164cb
jmp 0x164cd
cmpl $0x0, 0x430(%rsp)
jne 0x164e5
cmpl $0x2, 0x42c(%rsp)
jne 0x166ab
movq 0x20(%rsp), %rsi
leaq 0x378(%rsp), %rdi
leaq 0x410(%rsp), %rdx
leaq 0x3c0(%rsp), %rcx
callq 0x15820
jmp 0x16509
cmpl $0x0, 0x430(%rsp)
jne 0x1658c
leaq 0x1e8(%rsp), %rdi
leaq 0x378(%rsp), %rsi
callq 0x4efe0
jmp 0x1652a
leaq 0x268(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
xorl %edx, %edx
callq 0x4d390
jmp 0x16543
movq 0x10(%rsp), %rdi
leaq 0x268(%rsp), %rsi
callq 0x4d700
jmp 0x16557
leaq 0x268(%rsp), %rdi
callq 0x4d430
jmp 0x1658c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x408(%rsp)
movl %eax, 0x404(%rsp)
leaq 0x268(%rsp), %rdi
callq 0x4d430
jmp 0x1670a
cmpl $0x2, 0x42c(%rsp)
jne 0x1663b
movsd 0x3c0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x165cf
movq 0x10(%rsp), %rdi
movsd 0x3c0(%rsp), %xmm1
movsd 0x11ca46(%rip), %xmm0   # 0x133008
divsd %xmm1, %xmm0
callq 0x36dc0
jmp 0x165cd
jmp 0x165cf
movsd 0x3c8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x16604
movq 0x10(%rsp), %rdi
movsd 0x3c8(%rsp), %xmm1
movsd 0x11ca11(%rip), %xmm0   # 0x133008
divsd %xmm1, %xmm0
callq 0x36dc0
jmp 0x16602
jmp 0x16604
movsd 0x3d0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x16639
movq 0x10(%rsp), %rdi
movsd 0x3d0(%rsp), %xmm1
movsd 0x11c9dc(%rip), %xmm0   # 0x133008
divsd %xmm1, %xmm0
callq 0x36dc0
jmp 0x16637
jmp 0x16639
jmp 0x1663b
leaq 0x58(%rsp), %rdi
leaq 0x378(%rsp), %rsi
callq 0x4efe0
jmp 0x1664f
leaq 0xd8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
xorl %edx, %edx
callq 0x4d390
jmp 0x16665
movq 0x10(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x4d700
jmp 0x16679
leaq 0xd8(%rsp), %rdi
callq 0x4d430
jmp 0x166ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x408(%rsp)
movl %eax, 0x404(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x4d430
jmp 0x1670a
leaq 0x28(%rsp), %rdi
leaq 0x410(%rsp), %rsi
callq 0x36d10
jmp 0x166bf
leaq 0x40(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x70fa0
jmp 0x166d0
movq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x36cd0
jmp 0x166e1
movb $0x1, 0x42b(%rsp)
testb $0x1, 0x42b(%rsp)
jne 0x166fd
movq 0x10(%rsp), %rdi
callq 0x4d430
movq 0x18(%rsp), %rax
addq $0x448, %rsp            # imm = 0x448
retq
movq 0x10(%rsp), %rdi
callq 0x4d430
movq 0x408(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)