// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x210, %rsp            # imm = 0x210
movl %r8d, %ebp
movl %ecx, %r14d
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x95035(%rip), %rsi     # 0xdb010
callq 0x21282
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x45a87
testl %r12d, %r12d
jne 0x46016
leaq 0xb8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x212f8
leaq 0xb8(%rsp), %rsi
movq %rbx, %rdi
callq 0x212e2
testl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
je 0x46052
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x45f10
leaq 0x94b74(%rip), %rax     # 0xdaba8
ucomisd (%rax), %xmm0
jbe 0x46052
movsd 0x4cfc6(%rip), %xmm1    # 0x93008
divsd %xmm0, %xmm1
movq %rbx, %rdi
movapd %xmm1, %xmm0
callq 0x21314
testl %r14d, %r14d
sete %al
cmpl $0x2, %ebp
setne %cl
testb %cl, %al
jne 0x4618c
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rcx
movapd %xmm0, (%rcx)
andq $0x0, 0x10(%rcx)
leaq 0x1c8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %r15, %rdx
xorl %r8d, %r8d
callq 0x45b2f
testl %r14d, %r14d
jne 0x460d8
leaq 0x38(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x28386
leaq 0xb8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0x276a0
leaq 0xb8(%rsp), %rsi
movq %rbx, %rdi
callq 0x2785a
leaq 0xb8(%rsp), %rdi
callq 0x276ee
cmpl $0x2, %ebp
jne 0x46149
movsd 0x20(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x46105
movsd 0x4cf13(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
movq %rbx, %rdi
callq 0x2132a
xorpd %xmm0, %xmm0
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x46125
movsd 0x4ceef(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
movq %rbx, %rdi
callq 0x4779e
movsd 0x30(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x46149
movsd 0x4cecb(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
movq %rbx, %rdi
callq 0x477b4
leaq 0x38(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x28386
leaq 0xb8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
xorl %edx, %edx
callq 0x276a0
leaq 0xb8(%rsp), %rsi
movq %rbx, %rdi
callq 0x2785a
leaq 0xb8(%rsp), %rdi
callq 0x276ee
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x212f8
leaq 0xb8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x38c72
leaq 0xb8(%rsp), %rsi
movq %rbx, %rdi
callq 0x212e2
movq %rbx, %rax
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x461d4
jmp 0x461f0
movq %rax, %r14
leaq 0xb8(%rsp), %rdi
callq 0x276ee
jmp 0x461f3
jmp 0x461f0
jmp 0x461f0
jmp 0x461f0
jmp 0x461f0
jmp 0x461f0
movq %rax, %r14
movq %rbx, %rdi
callq 0x276ee
movq %r14, %rdi
callq 0xe810