// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Align.cpp
// Function: gaps::R3AlignPoints(gaps::RNArray<gaps::R3Point*> const&, gaps::RNArray<gaps::R3Point*> const&, double const*, int, int, int)
R4Matrix
R3AlignPoints(const RNArray<R3Point *>& points1, const RNArray<R3Point *>& points2, 
  const RNScalar* weights, RNBoolean align_center, RNBoolean align_rotation, int align_scale)
{
  int i,j,k;

  // Get count of points
  int count = points1.NEntries();
  if (points2.NEntries() < count) count = points2.NEntries();

  // Check number of points
  if (count < 1) align_center = 0;
  if (count < 2) align_scale = 0;
  if (count < 3) align_rotation = 0;

  // Compute centers
  R3Point center1(0.0, 0.0, 0.0);
  R3Point center2(0.0, 0.0, 0.0);
  if (align_center){
    center1 = R3Centroid(points1, weights);
    center2 = R3Centroid(points2, weights);
  }

  // Compute scales
  RNScalar s1 = 1;
  RNScalar s2 = 1;
  if (align_scale){
    s1 = R3AverageDistance(center1, points1, weights);
    s2 = R3AverageDistance(center2, points2, weights);
  }

  // Compute cross-covariance of two point sets
  R4Matrix rotation = R4identity_matrix;
  if (align_rotation) {
    R4Matrix m = R4identity_matrix;
    m[0][0] = m[1][1] = m[2][2] = 0;
    RNScalar total_weight = 0;
    for (i=0; i< count; i++){
      RNScalar weight = (weights) ? weights[i] : 1;
      total_weight += weight;
      R3Point *point1 = points1[i];
      R3Point *point2 = points2[i];
      R3Vector p1 = (*point1 - center1) / s1;
      R3Vector p2 = (*point2 - center2) / s2;
      for(j=0;j<3;j++){
        for(k=0;k<3;k++){
          m[j][k] += weight * p1[j]*p2[k];
        }
      }
    }

    // Normalize cross-covariance matrix
    if (total_weight == 0) return R4identity_matrix;
    for(j=0;j<3;j++){for(k=0;k<3;k++){ m[j][k] /= total_weight; } }

    // Make cross-covariance matrix skew-symmetric
    R4Matrix a = R4identity_matrix;
    for(j=0;j<3;j++){for(k=0;k<3;k++){a[j][k]=m[j][k]-m[k][j];}}
    
    // Compute trace of cross-covariance matrix
    RNScalar trace=m[0][0]+m[1][1]+m[2][2];
    
    // Setup symmetric matrix whose eigenvectors give quaternion terms of optimal rotation
    RNScalar M[16];
    M[0]=trace;
    M[1]=M[4]=a[1][2];
    M[2]=M[8]=a[2][0];
    M[3]=M[12]=a[0][1];
    for(j=0;j<3;j++){
      for(k=0;k<3;k++){M[4*(j+1)+(k+1)]=m[j][k]+m[k][j];}
      M[4*(j+1)+(j+1)]-=trace;
    }

    // Perform SVD to get eigenvectors (quaternion terms of optimal rotation)
    RNScalar U[16];
    RNScalar W[4];
    RNScalar Vt[16];
    RNSvdDecompose(4, 4, M, U, W, Vt);  
    
    // Look at error using all eigenvectors and keep best
    int minI=0;
    R4Matrix temp[4];
    RNScalar e[4];
    for(i=0;i<4;i++){
      RNScalar p[4];
      for(j=0;j<4;j++){p[j]=U[4*j+i];}
      if(p[0]<0){for(j=0;j<4;j++){p[j]=-p[j];}}
      temp[i] = SetQuaternion(p);
      e[i]= R3AlignError(points1, points2, temp[i], center1, center2, s1, s2, weights);
      if (e[i]<e[minI]) minI=i;
    }
    rotation = temp[minI];
  }

  // Compute result
  R4Matrix result = R4identity_matrix;
  if (align_center) result.Translate(center1.Vector());
  if (align_scale) result.Scale(s1/s2);
  if (align_rotation) result.Transform(rotation);
  if (align_center) result.Translate(-(center2.Vector()));

  // Return resulting matrix that takes points2 to points1
  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6f8, %rsp            # imm = 0x6F8
movl %r9d, %ebx
movl %r8d, %ebp
movq %rcx, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movl 0x730(%rsp), %r12d
movq %rsi, 0x30(%rsp)
movl 0xc(%rsi), %r14d
movq %rdx, 0x38(%rsp)
movl 0xc(%rdx), %eax
cmpl %r14d, %eax
cmovll %eax, %r14d
testl %r14d, %r14d
setle %r15b
leaq 0x98(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x37b4c
leaq 0x80(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x37b4c
testl %ebp, %ebp
sete %bpl
orb %r15b, %bpl
jne 0x465cc
leaq 0x370(%rsp), %r15
movq %r15, %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %r13
movq %r13, %rdx
callq 0x45a87
leaq 0x98(%rsp), %rdi
movq %r15, %rsi
callq 0x37e36
leaq 0x370(%rsp), %r15
movq %r15, %rdi
movq 0x38(%rsp), %rsi
movq %r13, %rdx
callq 0x45a87
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
callq 0x37e36
cmpl $0x2, %r14d
setl %al
testl %r12d, %r12d
sete %r12b
orb %al, %r12b
movb %bpl, 0xf(%rsp)
je 0x465f4
movsd 0x4ca1c(%rip), %xmm0    # 0x93008
movsd %xmm0, 0x28(%rsp)
jmp 0x46629
leaq 0x98(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %r15
movq %r15, %rdx
callq 0x45f10
movsd %xmm0, 0x28(%rsp)
leaq 0x80(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
callq 0x45f10
movsd %xmm0, 0x20(%rsp)
cmpl $0x3, %r14d
setl %bpl
leaq 0x4d7b2(%rip), %rsi     # 0x93df0
leaq 0x2f0(%rsp), %rdi
callq 0x2967c
testl %ebx, %ebx
sete %r15b
orb %bpl, %r15b
jne 0x46c67
movb %r15b, 0xd(%rsp)
movb %r12b, 0xe(%rsp)
leaq 0x4d785(%rip), %rsi     # 0x93df0
leaq 0x150(%rsp), %rbx
movq %rbx, %rdi
callq 0x2967c
andq $0x0, 0x50(%rbx)
andq $0x0, 0x28(%rbx)
andq $0x0, (%rbx)
movl %r14d, %r15d
xorpd %xmm2, %xmm2
xorl %ebp, %ebp
leaq 0x270(%rsp), %rbx
leaq 0x80(%rsp), %r13
movq 0x18(%rsp), %rax
cmpq %r15, %rbp
je 0x46795
movsd %xmm2, 0x60(%rsp)
movsd 0x4c94a(%rip), %xmm0    # 0x93008
testq %rax, %rax
je 0x466c8
movsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,8), %rsi
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,8), %r14
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
leaq 0x98(%rsp), %rdx
callq 0x37d29
leaq 0x370(%rsp), %rdi
movq %r12, %rsi
movsd 0x28(%rsp), %xmm0
callq 0x38d1e
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x37d29
movq %r12, %rdi
movq %rbx, %rsi
movsd 0x20(%rsp), %xmm0
callq 0x38d1e
movsd 0x78(%rsp), %xmm3
leaq 0x150(%rsp), %rax
xorl %ecx, %ecx
movsd 0x60(%rsp), %xmm2
cmpq $0x3, %rcx
je 0x46789
movsd 0x370(%rsp,%rcx,8), %xmm0
mulsd %xmm3, %xmm0
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x46780
movsd 0xb0(%rsp,%rdx,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rax,%rdx,8), %xmm1
movsd %xmm1, (%rax,%rdx,8)
incq %rdx
jmp 0x4675e
incq %rcx
addq $0x20, %rax
jmp 0x46749
addsd %xmm3, %xmm2
incq %rbp
jmp 0x466a2
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x467a5
jnp 0x46d18
leaq 0x150(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x467d9
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x467d0
movsd (%rax,%rdx,8), %xmm0
divsd %xmm2, %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
jmp 0x467b7
incq %rcx
addq $0x20, %rax
jmp 0x467af
leaq 0x4d610(%rip), %rsi     # 0x93df0
movq %rbx, %rdi
movq %rsi, %r13
callq 0x2967c
leaq 0x150(%rsp), %rax
xorl %ecx, %ecx
movq %rax, %rdx
cmpq $0x3, %rcx
je 0x4682c
xorl %esi, %esi
cmpq $0x18, %rsi
je 0x4681b
movsd (%rax,%rsi), %xmm0
subsd (%rdx,%rsi,4), %xmm0
movsd %xmm0, (%rbx,%rsi)
addq $0x8, %rsi
jmp 0x46800
incq %rcx
addq $0x8, %rdx
addq $0x20, %rbx
addq $0x20, %rax
jmp 0x467f8
leaq 0x150(%rsp), %rax
movsd (%rax), %xmm0
addsd 0x28(%rax), %xmm0
addsd 0x50(%rax), %xmm0
leaq 0x218(%rsp), %rcx
movsd %xmm0, -0x28(%rcx)
movsd 0x278(%rsp), %xmm1
movsd 0x2a0(%rsp), %xmm2
movsd %xmm2, -0x8(%rcx)
movsd %xmm2, -0x20(%rcx)
movsd 0x2b0(%rsp), %xmm2
movsd %xmm2, 0x18(%rcx)
movsd %xmm2, -0x18(%rcx)
movsd %xmm1, 0x38(%rcx)
movsd %xmm1, -0x10(%rcx)
xorl %edx, %edx
movq %rax, %rsi
cmpq $0x3, %rdx
je 0x468db
xorl %edi, %edi
cmpq $0x18, %rdi
je 0x468b0
movsd (%rax,%rdi), %xmm1
addsd (%rsi,%rdi,4), %xmm1
movsd %xmm1, (%rcx,%rdi)
addq $0x8, %rdi
jmp 0x46895
incq %rdx
imulq $0x28, %rdx, %rdi
movsd 0x1f0(%rsp,%rdi), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x1f0(%rsp,%rdi)
addq $0x8, %rsi
addq $0x20, %rax
addq $0x20, %rcx
jmp 0x4688d
pushq $0x4
popq %rdi
leaq 0x1f0(%rsp), %rdx
leaq 0x5f0(%rsp), %rbx
leaq 0x1d0(%rsp), %r8
leaq 0x570(%rsp), %r9
movl %edi, %esi
movq %rbx, %rcx
callq 0x678f6
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x370, %rdi            # imm = 0x370
callq 0x2967a
subq $-0x80, %r14
cmpq $0x200, %r14            # imm = 0x200
jne 0x4690b
xorl %r15d, %r15d
movapd 0x4c6fd(%rip), %xmm1    # 0x93030
leaq 0x678(%rsp), %rbp
xorl %r12d, %r12d
movslq %r12d, %r12
cmpq $0x4, %r15
je 0x46c41
xorl %eax, %eax
cmpq $0x20, %rax
je 0x46964
movsd (%rbx,%rax,4), %xmm0
movsd %xmm0, 0x40(%rsp,%rax)
addq $0x8, %rax
jmp 0x4694d
movsd 0x40(%rsp), %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jbe 0x46997
xorl %eax, %eax
cmpq $0x4, %rax
je 0x46991
movsd 0x40(%rsp,%rax,8), %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, 0x40(%rsp,%rax,8)
incq %rax
jmp 0x46976
movsd 0x40(%rsp), %xmm2
movapd %xmm2, 0x60(%rsp)
leaq 0xb0(%rsp), %rdi
movq %r13, %rsi
callq 0x2967c
movapd 0x60(%rsp), %xmm11
xorpd %xmm0, %xmm0
ucomisd %xmm11, %xmm0
jbe 0x469df
movhpd 0x48(%rsp), %xmm11      # xmm11 = xmm11[0],mem[0]
movapd 0x4c662(%rip), %xmm1    # 0x93030
xorpd %xmm1, %xmm11
movapd 0x50(%rsp), %xmm0
xorpd %xmm1, %xmm0
jmp 0x469ec
movapd 0x50(%rsp), %xmm0
movhpd 0x48(%rsp), %xmm11      # xmm11 = xmm11[0],mem[0]
movapd %xmm11, %xmm1
mulpd %xmm11, %xmm1
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
movapd %xmm11, %xmm2
mulsd %xmm11, %xmm2
addsd %xmm1, %xmm2
movapd %xmm0, %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
mulsd %xmm1, %xmm1
addsd %xmm3, %xmm1
movsd 0x4c7b4(%rip), %xmm2    # 0x931e0
ucomisd %xmm1, %xmm2
movq %r13, %rsi
ja 0x46bb9
sqrtsd %xmm1, %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
divpd %xmm2, %xmm11
movapd %xmm11, 0x40(%rsp)
movapd %xmm11, %xmm4
unpckhpd %xmm11, %xmm4           # xmm4 = xmm4[1],xmm11[1]
movapd %xmm4, %xmm8
mulsd %xmm4, %xmm8
movapd %xmm11, %xmm1
mulsd %xmm11, %xmm1
addsd %xmm1, %xmm8
movapd %xmm11, %xmm6
movapd 0x4c5b2(%rip), %xmm10   # 0x93030
xorpd %xmm10, %xmm6
divpd %xmm2, %xmm0
movapd %xmm0, %xmm5
shufpd $0x1, %xmm0, %xmm5      # xmm5 = xmm5[1],xmm0[0]
movlpd %xmm0, 0x50(%rsp)
movapd %xmm0, %xmm2
xorpd %xmm10, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm2, %xmm8
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
movapd %xmm3, %xmm7
xorpd %xmm10, %xmm7
mulsd %xmm3, %xmm7
addsd %xmm7, %xmm8
movsd %xmm8, 0xb0(%rsp)
movapd %xmm11, %xmm8
unpcklpd %xmm6, %xmm8            # xmm8 = xmm8[0],xmm6[0]
mulpd %xmm5, %xmm8
movapd %xmm4, %xmm9
mulpd %xmm0, %xmm9
addpd %xmm8, %xmm9
addpd %xmm9, %xmm9
movupd %xmm9, 0xb8(%rsp)
movapd %xmm3, %xmm8
mulsd %xmm11, %xmm8
movapd %xmm4, %xmm9
mulsd %xmm0, %xmm9
subsd %xmm8, %xmm9
addsd %xmm9, %xmm9
movsd %xmm9, 0xd0(%rsp)
movapd %xmm0, %xmm9
mulsd %xmm0, %xmm9
addsd %xmm1, %xmm9
movapd %xmm4, %xmm8
xorpd %xmm10, %xmm8
mulsd %xmm4, %xmm8
addsd %xmm8, %xmm9
addsd %xmm7, %xmm9
movsd %xmm9, 0xd8(%rsp)
mulsd %xmm11, %xmm4
movapd %xmm0, %xmm7
mulsd %xmm3, %xmm7
addsd %xmm4, %xmm7
addsd %xmm7, %xmm7
movsd %xmm7, 0xe0(%rsp)
movapd %xmm0, %xmm4
unpcklpd %xmm6, %xmm4            # xmm4 = xmm4[0],xmm6[0]
mulpd %xmm11, %xmm4
movhlps %xmm11, %xmm0           # xmm0 = xmm11[1],xmm0[1]
mulpd %xmm5, %xmm0
addpd %xmm4, %xmm0
addpd %xmm0, %xmm0
movupd %xmm0, 0xf0(%rsp)
mulsd %xmm3, %xmm3
addsd %xmm1, %xmm3
addsd %xmm8, %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, 0x100(%rsp)
leaq 0xb0(%rsp), %rsi
movq %rbp, %rdi
callq 0x2967c
movq %r15, %rax
shlq $0x7, %rax
leaq (%rsp,%rax), %r14
addq $0x370, %r14            # imm = 0x370
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2ad04
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %r14, %rdx
leaq 0x98(%rsp), %rcx
leaq 0x80(%rsp), %r8
movsd 0x28(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movq 0x18(%rsp), %r9
callq 0x46203
movsd %xmm0, 0x130(%rsp,%r15,8)
movsd 0x130(%rsp,%r12,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %r15d, %r12d
incq %r15
addq $0x8, %rbx
movapd 0x4c3f4(%rip), %xmm1    # 0x93030
jmp 0x4693e
shlq $0x7, %r12
leaq (%rsp,%r12), %rsi
addq $0x370, %rsi            # imm = 0x370
leaq 0x2f0(%rsp), %rdi
callq 0x2ad04
movb 0xe(%rsp), %r12b
movb 0xd(%rsp), %r15b
leaq 0x4d182(%rip), %rsi     # 0x93df0
movq 0x10(%rsp), %rdi
callq 0x2967c
movb 0xf(%rsp), %bpl
testb %bpl, %bpl
jne 0x46ca7
leaq 0x370(%rsp), %rbx
leaq 0x98(%rsp), %rsi
movq %rbx, %rdi
callq 0x212f8
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x2a5cc
testb %r12b, %r12b
jne 0x46cc2
movsd 0x28(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
movq 0x10(%rsp), %rdi
callq 0x2a6a0
testb %r15b, %r15b
jne 0x46cd9
leaq 0x2f0(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x280fe
testb %bpl, %bpl
jne 0x46d29
leaq 0xb0(%rsp), %rbx
leaq 0x80(%rsp), %rsi
movq %rbx, %rdi
callq 0x212f8
leaq 0x370(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x38c72
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x2a5cc
jmp 0x46d29
leaq 0x4d0d1(%rip), %rsi     # 0x93df0
movq 0x10(%rsp), %rdi
callq 0x2967c
movq 0x10(%rsp), %rax
addq $0x6f8, %rsp            # imm = 0x6F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq