// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Align.cpp
// Function: gaps::R3NormalizationTransformation(gaps::RNArray<gaps::R3Point*> const&, int, int, int)
R3Affine 
R3NormalizationTransformation(const RNArray<R3Point *>& points,  RNBoolean translate, RNBoolean rotate, int scale) 
{
  // Initialize transformation
  R3Affine affine(R3identity_affine);

  // Compute center of mass
  R3Point centroid = R3Centroid(points);

  // 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 = R3AverageDistance(centroid, points);
    if (RNIsPositive(radius)) affine.Scale(1.0 / radius);
  }

  // Rotate to align principal axes with XYZ
  if (rotate || (scale == 2)) {
    RNScalar variances[3] = { 0 };
    R3Triad triad = R3PrincipleAxes(centroid, points, 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.YScale(1.0 / variances[1]);
      if (variances[2] > 0) affine.ZScale(1.0 / variances[2]);
    }
    affine.Transform(R3Affine(triad.InverseMatrix()));
  }

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

// Assembly:
subq $0x448, %rsp            # imm = 0x448
movq %rdi, 0x18(%rsp)
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x440(%rsp)
movq %rsi, 0x438(%rsp)
movl %edx, 0x434(%rsp)
movl %ecx, 0x430(%rsp)
movl %r8d, 0x42c(%rsp)
movb $0x0, 0x42b(%rsp)
leaq 0xeb0d7(%rip), %rsi     # 0x178950
callq 0x36c20
movq 0x438(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
leaq 0x410(%rsp), %rdi
callq 0x8cd30
jmp 0x8d899
cmpl $0x0, 0x434(%rsp)
jne 0x8d8e9
leaq 0x3e8(%rsp), %rdi
leaq 0x410(%rsp), %rsi
callq 0x36d10
jmp 0x8d8ba
movq 0x18(%rsp), %rdi
leaq 0x3e8(%rsp), %rsi
callq 0x36cd0
jmp 0x8d8ce
jmp 0x8d8e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x408(%rsp)
movl %eax, 0x404(%rsp)
jmp 0x8dbda
cmpl $0x0, 0x42c(%rsp)
je 0x8d97e
cmpl $0x2, 0x42c(%rsp)
je 0x8d97e
movq 0x438(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
leaq 0x410(%rsp), %rdi
callq 0x8d700
movsd %xmm0, 0x10(%rsp)
jmp 0x8d922
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x3e0(%rsp)
movsd 0x3e0(%rsp), %xmm0
leaq 0xeab9f(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
movl %eax, 0xc(%rsp)
jmp 0x8d950
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
je 0x8d97c
movq 0x18(%rsp), %rdi
movsd 0x3e0(%rsp), %xmm1
movsd 0xa5699(%rip), %xmm0    # 0x133008
divsd %xmm1, %xmm0
callq 0x36d80
jmp 0x8d97a
jmp 0x8d97c
jmp 0x8d97e
cmpl $0x0, 0x430(%rsp)
jne 0x8d996
cmpl $0x2, 0x42c(%rsp)
jne 0x8db7b
xorps %xmm0, %xmm0
movaps %xmm0, 0x3c0(%rsp)
movq $0x0, 0x3d0(%rsp)
movq 0x438(%rsp), %rdx
xorl %eax, %eax
movl %eax, %r8d
leaq 0x378(%rsp), %rdi
leaq 0x410(%rsp), %rsi
leaq 0x3c0(%rsp), %rcx
callq 0x8ce50
jmp 0x8d9d9
cmpl $0x0, 0x430(%rsp)
jne 0x8da5c
leaq 0x1e8(%rsp), %rdi
leaq 0x378(%rsp), %rsi
callq 0x4efe0
jmp 0x8d9fa
leaq 0x268(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
xorl %edx, %edx
callq 0x4d390
jmp 0x8da13
movq 0x18(%rsp), %rdi
leaq 0x268(%rsp), %rsi
callq 0x4d700
jmp 0x8da27
leaq 0x268(%rsp), %rdi
callq 0x4d430
jmp 0x8da5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x408(%rsp)
movl %eax, 0x404(%rsp)
leaq 0x268(%rsp), %rdi
callq 0x4d430
jmp 0x8dbda
cmpl $0x2, 0x42c(%rsp)
jne 0x8db0b
movsd 0x3c0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x8da9f
movq 0x18(%rsp), %rdi
movsd 0x3c0(%rsp), %xmm1
movsd 0xa5576(%rip), %xmm0    # 0x133008
divsd %xmm1, %xmm0
callq 0x36dc0
jmp 0x8da9d
jmp 0x8da9f
movsd 0x3c8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x8dad4
movq 0x18(%rsp), %rdi
movsd 0x3c8(%rsp), %xmm1
movsd 0xa5541(%rip), %xmm0    # 0x133008
divsd %xmm1, %xmm0
callq 0x905c0
jmp 0x8dad2
jmp 0x8dad4
movsd 0x3d0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x8db09
movq 0x18(%rsp), %rdi
movsd 0x3d0(%rsp), %xmm1
movsd 0xa550c(%rip), %xmm0    # 0x133008
divsd %xmm1, %xmm0
callq 0x90600
jmp 0x8db07
jmp 0x8db09
jmp 0x8db0b
leaq 0x58(%rsp), %rdi
leaq 0x378(%rsp), %rsi
callq 0x4efe0
jmp 0x8db1f
leaq 0xd8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
xorl %edx, %edx
callq 0x4d390
jmp 0x8db35
movq 0x18(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x4d700
jmp 0x8db49
leaq 0xd8(%rsp), %rdi
callq 0x4d430
jmp 0x8db7b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x408(%rsp)
movl %eax, 0x404(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x4d430
jmp 0x8dbda
leaq 0x28(%rsp), %rdi
leaq 0x410(%rsp), %rsi
callq 0x36d10
jmp 0x8db8f
leaq 0x40(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x70fa0
jmp 0x8dba0
movq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x36cd0
jmp 0x8dbb1
movb $0x1, 0x42b(%rsp)
testb $0x1, 0x42b(%rsp)
jne 0x8dbcd
movq 0x18(%rsp), %rdi
callq 0x4d430
movq 0x20(%rsp), %rax
addq $0x448, %rsp            # imm = 0x448
retq
movq 0x18(%rsp), %rdi
callq 0x4d430
movq 0x408(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)