// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Align.cpp
// Function: gaps::R2NormalizationTransformation(gaps::RNArray<gaps::R2Point*> const&, int, int, int)
R2Affine 
R2NormalizationTransformation(const RNArray<R2Point *>& points,  RNBoolean translate, RNBoolean rotate, int scale) 
{
  // Initialize transformation
  R2Affine affine(R2identity_affine);

  // Compute center of mass
  R2Point centroid = R2Centroid(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 = R2AverageDistance(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 };
    R2Diad diad = R2PrincipleAxes(centroid, points, variances);
    if (!rotate) affine.Transform(R2Affine(diad.InverseMatrix()));
    if (scale == 2) {
      if (variances[0] > 0) affine.XScale(1.0 / variances[0]);
      if (variances[1] > 0) affine.YScale(1.0 / variances[1]);
    }
    affine.Transform(R2Affine(diad.InverseMatrix()));
  }

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

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0x10(%rsp)
movq %rdi, %rax
movq %rax, 0x18(%rsp)
movq %rdi, 0x210(%rsp)
movq %rsi, 0x208(%rsp)
movl %edx, 0x204(%rsp)
movl %ecx, 0x200(%rsp)
movl %r8d, 0x1fc(%rsp)
movb $0x0, 0x1fb(%rsp)
leaq 0x4ac1bf(%rip), %rsi    # 0x57aa68
callq 0xb9740
movq 0x208(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
leaq 0x1e8(%rsp), %rdi
callq 0xcdf80
jmp 0xce8c9
cmpl $0x0, 0x204(%rsp)
jne 0xce919
leaq 0x1c8(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0xa5b80
jmp 0xce8ea
movq 0x10(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0xb92e0
jmp 0xce8fe
jmp 0xce919
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
jmp 0xcebd1
cmpl $0x0, 0x1fc(%rsp)
je 0xce9ae
cmpl $0x2, 0x1fc(%rsp)
je 0xce9ae
movq 0x208(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
leaq 0x1e8(%rsp), %rdi
callq 0xce730
movsd %xmm0, 0x8(%rsp)
jmp 0xce952
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x1c0(%rsp)
movsd 0x1c0(%rsp), %xmm0
leaq 0xa9b6f(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
movl %eax, 0x4(%rsp)
jmp 0xce980
movl 0x4(%rsp), %eax
cmpl $0x0, %eax
je 0xce9ac
movq 0x10(%rsp), %rdi
movsd 0x1c0(%rsp), %xmm1
movsd 0x64669(%rip), %xmm0    # 0x133008
divsd %xmm1, %xmm0
callq 0xb97c0
jmp 0xce9aa
jmp 0xce9ac
jmp 0xce9ae
cmpl $0x0, 0x200(%rsp)
jne 0xce9c6
cmpl $0x2, 0x1fc(%rsp)
jne 0xceb72
xorps %xmm0, %xmm0
movaps %xmm0, 0x1a0(%rsp)
movq $0x0, 0x1b0(%rsp)
movq 0x208(%rsp), %rdx
xorl %eax, %eax
movl %eax, %r8d
leaq 0x180(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
leaq 0x1a0(%rsp), %rcx
callq 0xce0a0
jmp 0xcea09
cmpl $0x0, 0x200(%rsp)
jne 0xcea8c
leaq 0xe0(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0xbf520
jmp 0xcea2a
leaq 0x128(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
xorl %edx, %edx
callq 0xbed60
jmp 0xcea43
movq 0x10(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xbef40
jmp 0xcea57
leaq 0x128(%rsp), %rdi
callq 0xb9250
jmp 0xcea8c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
leaq 0x128(%rsp), %rdi
callq 0xb9250
jmp 0xcebd1
cmpl $0x2, 0x1fc(%rsp)
jne 0xceb02
movsd 0x1a0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xceacb
movq 0x10(%rsp), %rdi
movsd 0x1a0(%rsp), %xmm1
movsd 0x6454a(%rip), %xmm0    # 0x133008
divsd %xmm1, %xmm0
callq 0xcfab0
jmp 0xceac9
jmp 0xceacb
movsd 0x1a8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xceb00
movq 0x10(%rsp), %rdi
movsd 0x1a8(%rsp), %xmm1
movsd 0x64515(%rip), %xmm0    # 0x133008
divsd %xmm1, %xmm0
callq 0xcfae0
jmp 0xceafe
jmp 0xceb00
jmp 0xceb02
leaq 0x40(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0xbf520
jmp 0xceb16
leaq 0x88(%rsp), %rdi
leaq 0x40(%rsp), %rsi
xorl %edx, %edx
callq 0xbed60
jmp 0xceb2c
movq 0x10(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0xbef40
jmp 0xceb40
leaq 0x88(%rsp), %rdi
callq 0xb9250
jmp 0xceb72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
leaq 0x88(%rsp), %rdi
callq 0xb9250
jmp 0xcebd1
leaq 0x20(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0xa5b80
jmp 0xceb86
leaq 0x30(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xc4700
jmp 0xceb97
movq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xb92e0
jmp 0xceba8
movb $0x1, 0x1fb(%rsp)
testb $0x1, 0x1fb(%rsp)
jne 0xcebc4
movq 0x10(%rsp), %rdi
callq 0xb9250
movq 0x18(%rsp), %rax
addq $0x218, %rsp            # imm = 0x218
retq
movq 0x10(%rsp), %rdi
callq 0xb9250
movq 0x1e0(%rsp), %rdi
callq 0xf890
nopl (%rax,%rax)