// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xf0, %rsp
movl %r8d, %ebp
movl %ecx, %r14d
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x47861b(%rip), %rsi    # 0x4dcf68
callq 0x5ad70
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x6451c
testl %r12d, %r12d
jne 0x64983
leaq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x51538
leaq 0x8(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0x5de7a
testl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
je 0x649c0
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x64882
leaq 0x76207(%rip), %rax     # 0xdaba8
ucomisd (%rax), %xmm0
jbe 0x649c0
movsd 0x2e659(%rip), %xmm1    # 0x93008
divsd %xmm0, %xmm1
leaq 0x8(%rbx), %rdi
movapd %xmm1, %xmm0
callq 0x5df40
testl %r14d, %r14d
sete %al
cmpl $0x2, %ebp
setne %cl
testb %cl, %al
jne 0x64ad2
xorpd %xmm0, %xmm0
leaq 0x70(%rsp), %rcx
movapd %xmm0, (%rcx)
andq $0x0, 0x10(%rcx)
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %r15, %rdx
xorl %r8d, %r8d
callq 0x645bf
testl %r14d, %r14d
jne 0x64a43
leaq 0xa8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x5d734
leaq 0x18(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
xorl %edx, %edx
callq 0x5d334
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x5d43a
leaq 0x18(%rsp), %rdi
callq 0x5d61e
cmpl $0x2, %ebp
jne 0x64a92
movsd 0x70(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x64a71
movsd 0x2e5a8(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
leaq 0x8(%rbx), %rdi
callq 0x5dec6
xorpd %xmm0, %xmm0
movsd 0x78(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x64a92
movsd 0x2e583(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
leaq 0x8(%rbx), %rdi
callq 0x5dede
leaq 0xa8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x5d734
leaq 0x18(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
xorl %edx, %edx
callq 0x5d334
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x5d43a
leaq 0x18(%rsp), %rdi
callq 0x5d61e
leaq 0xa8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x51538
leaq 0x18(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x5fd59
leaq 0x8(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0x5de7a
movq %rbx, %rax
addq $0xf0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x64b1b
jmp 0x64b34
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x5d61e
jmp 0x64b37
jmp 0x64b34
jmp 0x64b34
jmp 0x64b34
jmp 0x64b34
jmp 0x64b34
movq %rax, %r14
movq %rbx, %rdi
callq 0x5d61e
movq %r14, %rdi
callq 0xe810