// File: /workspace/github_cpp_makefile_star2_O3/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 0x959ad(%rip), %rax     # 0xe20e8
addq $0x10, %rax
movq %rax, (%rdi)
addq $0x8, %rdi
leaq 0x998b3(%rip), %r15     # 0xe6000
leaq 0x8(%r15), %rsi
movq %rdi, %r14
callq 0x2c2be
movl $0x88, %esi
leaq (%r12,%rsi), %rdi
addq 0x98877(%rip), %rsi     # 0xe4fe0
callq 0x2c2be
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 0x4c1ec
testl %r13d, %r13d
movl 0xc(%rsp), %r12d
movq 0x10(%rsp), %rbx
jne 0x4c7d9
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
leaq 0xd0(%rsp), %rdi
callq 0x3cfc6
leaq 0xd0(%rsp), %rsi
movq %r14, %rdi
callq 0x2d3f8
andb $-0x3, 0x108(%rbx)
testl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
je 0x4c81d
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4c65b
leaq 0x993a0(%rip), %rax     # 0xe5b98
ucomisd (%rax), %xmm0
jbe 0x4c81d
movsd 0x55832(%rip), %xmm1    # 0xa2038
divsd %xmm0, %xmm1
movq %r14, %rdi
movapd %xmm1, %xmm0
callq 0x2d5a6
andb $-0x3, 0x108(%rbx)
testl %ebp, %ebp
sete %al
cmpl $0x2, %r12d
setne %cl
testb %cl, %al
jne 0x4c973
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 0x4c299
testl %ebp, %ebp
jne 0x4c8a5
leaq 0x50(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x2ad48
leaq 0xd0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x29fda
leaq 0xd0(%rsp), %rsi
movq %rbx, %rdi
callq 0x2a196
leaq 0xd0(%rsp), %rdi
callq 0x2a028
cmpl $0x2, %r12d
jne 0x4c930
movsd 0x30(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x4c8de
movsd 0x55771(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d4a0
andb $-0x3, 0x108(%rbx)
xorpd %xmm0, %xmm0
movsd 0x38(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4c905
movsd 0x55746(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d4c6
andb $-0x3, 0x108(%rbx)
movsd 0x40(%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x4c930
movsd 0x5571b(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movq %r14, %rdi
callq 0x2d4ee
andb $-0x3, 0x108(%rbx)
leaq 0x50(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x2ad48
leaq 0xd0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x29fda
leaq 0xd0(%rsp), %rsi
movq %rbx, %rdi
callq 0x2a196
leaq 0xd0(%rsp), %rdi
callq 0x2a028
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
leaq 0x50(%rsp), %rdi
callq 0x3cfc6
leaq 0xd0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x3dc00
leaq 0xd0(%rsp), %rsi
movq %r14, %rdi
callq 0x2d3f8
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 0x4c9d1
jmp 0x4c9fa
movq %rax, %r14
leaq 0xd0(%rsp), %rdi
callq 0x2a028
jmp 0x4c9fd
jmp 0x4c9fa
jmp 0x4c9fa
jmp 0x4c9fa
jmp 0x4c9fa
jmp 0x4c9fa
movq %rax, %r14
movq %r12, %rdi
callq 0x2a6bc
jmp 0x4ca07
movq %rax, %r14
movq 0x10(%rsp), %rdi
callq 0x2a028
movq %r14, %rdi
callq 0xe7e0