// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl %r8d, 0xc(%rsp)
movl %ecx, %ebp
movl %edx, %r13d
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x94e60(%rip), %rax     # 0xe20e8
addq $0x10, %rax
movq %rax, (%rdi)
addq $0x8, %rdi
leaq 0x98d66(%rip), %r15     # 0xe6000
leaq 0x8(%r15), %rsi
movq %rdi, %r14
callq 0x2c5ba
movl $0x88, %esi
leaq (%r12,%rsi), %rdi
addq 0x97d2a(%rip), %rsi     # 0xe4fe0
callq 0x2c5ba
movq 0x108(%r15), %rax
movq %r12, 0x10(%rsp)
movq %rax, 0x108(%r12)
leaq 0x18(%rsp), %rdi
movq %rbx, %r15
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4ccf0
testl %r13d, %r13d
movl 0xc(%rsp), %r12d
movq 0x10(%rsp), %rbx
jne 0x4d326
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
leaq 0xd0(%rsp), %rdi
callq 0x3d4be
leaq 0xd0(%rsp), %rsi
movq %r14, %rdi
callq 0x2d6f4
andb $-0x3, 0x108(%rbx)
testl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
je 0x4d36a
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4d1a8
leaq 0x98853(%rip), %rax     # 0xe5b98
ucomisd (%rax), %xmm0
jbe 0x4d36a
movsd 0x54ce5(%rip), %xmm1    # 0xa2038
divsd %xmm0, %xmm1
movq %r14, %rdi
movapd %xmm1, %xmm0
callq 0x2d8a2
andb $-0x3, 0x108(%rbx)
testl %ebp, %ebp
sete %al
cmpl $0x2, %r12d
setne %cl
testb %cl, %al
jne 0x4d4c0
xorpd %xmm0, %xmm0
leaq 0x30(%rsp), %rcx
movapd %xmm0, (%rcx)
movq $0x0, 0x10(%rcx)
leaq 0x1e0(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq %r15, %rdx
xorl %r8d, %r8d
callq 0x4cd9c
testl %ebp, %ebp
jne 0x4d3f2
leaq 0x50(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x2af90
leaq 0xd0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x2a22e
leaq 0xd0(%rsp), %rsi
movq %rbx, %rdi
callq 0x2a3ea
leaq 0xd0(%rsp), %rdi
callq 0x2a27c
cmpl $0x2, %r12d
jne 0x4d47d
movsd 0x30(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x4d42b
movsd 0x54c24(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d79c
andb $-0x3, 0x108(%rbx)
xorpd %xmm0, %xmm0
movsd 0x38(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4d452
movsd 0x54bf9(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d7c2
andb $-0x3, 0x108(%rbx)
movsd 0x40(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x4d47d
movsd 0x54bce(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d7ea
andb $-0x3, 0x108(%rbx)
leaq 0x50(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x2af90
leaq 0xd0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x2a22e
leaq 0xd0(%rsp), %rsi
movq %rbx, %rdi
callq 0x2a3ea
leaq 0xd0(%rsp), %rdi
callq 0x2a27c
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
leaq 0x50(%rsp), %rdi
callq 0x3d4be
leaq 0xd0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x3e154
leaq 0xd0(%rsp), %rsi
movq %r14, %rdi
callq 0x2d6f4
andb $-0x3, 0x108(%rbx)
movq %rbx, %rax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4d51e
jmp 0x4d547
movq %rax, %r14
leaq 0xd0(%rsp), %rdi
callq 0x2a27c
jmp 0x4d54a
jmp 0x4d547
jmp 0x4d547
jmp 0x4d547
jmp 0x4d547
jmp 0x4d547
movq %rax, %r14
movq %r12, %rdi
callq 0x2a904
jmp 0x4d554
movq %rax, %r14
movq 0x10(%rsp), %rdi
callq 0x2a27c
movq %r14, %rdi
callq 0xe7e0