// File: /workspace/github_cpp_makefile_star2_O3/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 $0x6e8, %rsp            # imm = 0x6E8
movl %r9d, %ebx
movl %r8d, %ebp
movq %rcx, %r13
movq %rdi, 0x28(%rsp)
movl 0x720(%rsp), %r12d
movq %rsi, 0x18(%rsp)
movl 0xc(%rsi), %r15d
movq %rdx, 0x20(%rsp)
movl 0xc(%rdx), %eax
cmpl %r15d, %eax
cmovll %eax, %r15d
testl %r15d, %r15d
setle %r14b
leaq 0x68(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x3c532
leaq 0x50(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x3c532
testl %ebp, %ebp
sete %bpl
orb %r14b, %bpl
jne 0x4cd9c
leaq 0x360(%rsp), %r14
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
callq 0x4c1ec
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq 0x3c89a
leaq 0x360(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
callq 0x4c1ec
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0x3c89a
cmpl $0x2, %r15d
setl %al
testl %r12d, %r12d
sete %r14b
orb %al, %r14b
movb %bpl, 0x7(%rsp)
je 0x4cdc4
movsd 0x5527c(%rip), %xmm0    # 0xa2038
movsd %xmm0, 0x10(%rsp)
jmp 0x4cdee
leaq 0x68(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
callq 0x4c65b
movsd %xmm0, 0x10(%rsp)
leaq 0x50(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
callq 0x4c65b
movsd %xmm0, 0x8(%rsp)
cmpl $0x3, %r15d
setl %bpl
leaq 0x5603d(%rip), %rsi     # 0xa2e40
leaq 0x2e0(%rsp), %rdi
callq 0x2c2be
testl %ebx, %ebx
sete %r12b
orb %bpl, %r12b
jne 0x4d420
movb %r12b, 0x5(%rsp)
movb %r14b, 0x6(%rsp)
leaq 0x56010(%rip), %rsi     # 0xa2e40
leaq 0x140(%rsp), %rbx
movq %rbx, %rdi
callq 0x2c2be
xorl %ebp, %ebp
movq %rbp, 0x50(%rbx)
movq %rbp, 0x28(%rbx)
movq %rbp, (%rbx)
movl %r15d, %r15d
xorpd %xmm1, %xmm1
leaq 0x260(%rsp), %rbx
movsd %xmm1, 0x80(%rsp)
movsd 0x551cb(%rip), %xmm0    # 0xa2038
testq %r13, %r13
je 0x4ce79
movsd (%r13,%rbp,8), %xmm0
movsd %xmm0, 0x98(%rsp)
movq %r13, %r12
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,8), %rsi
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,8), %r13
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
leaq 0x68(%rsp), %rdx
callq 0x3c78e
leaq 0x360(%rsp), %rdi
movq %r14, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x3dcac
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x50(%rsp), %rdx
callq 0x3c78e
movq %r14, %rdi
movq %rbx, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x3dcac
movsd 0x98(%rsp), %xmm2
leaq 0x140(%rsp), %rax
xorl %ecx, %ecx
movsd 0x360(%rsp,%rcx,8), %xmm0
mulsd %xmm2, %xmm0
xorl %edx, %edx
movsd 0xa0(%rsp,%rdx,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rax,%rdx,8), %xmm1
movsd %xmm1, (%rax,%rdx,8)
incq %rdx
cmpq $0x3, %rdx
jne 0x4cf0b
incq %rcx
addq $0x20, %rax
cmpq $0x3, %rcx
jne 0x4cefc
movsd 0x80(%rsp), %xmm1
addsd %xmm2, %xmm1
incq %rbp
cmpq %r15, %rbp
movq %r12, %r13
jne 0x4ce5c
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x4cf64
jnp 0x4d4f6
leaq 0x140(%rsp), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movsd (%rax,%rdx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
cmpq $0x3, %rdx
jne 0x4cf70
incq %rcx
addq $0x20, %rax
cmpq $0x3, %rcx
jne 0x4cf6e
leaq 0x55ea5(%rip), %rsi     # 0xa2e40
movq %rbx, %rdi
callq 0x2c2be
leaq 0x140(%rsp), %rax
xorl %ecx, %ecx
movq %rax, %rdx
xorl %esi, %esi
movsd (%rdx,%rsi), %xmm0
subsd (%rax,%rsi,4), %xmm0
movsd %xmm0, (%rbx,%rsi)
addq $0x8, %rsi
cmpq $0x18, %rsi
jne 0x4cfb2
incq %rcx
addq $0x20, %rbx
addq $0x20, %rdx
addq $0x8, %rax
cmpq $0x3, %rcx
jne 0x4cfb0
leaq 0x140(%rsp), %rax
movsd (%rax), %xmm0
addsd 0x28(%rax), %xmm0
addsd 0x50(%rax), %xmm0
leaq 0x208(%rsp), %rcx
movsd %xmm0, -0x28(%rcx)
movsd 0x268(%rsp), %xmm1
movsd 0x290(%rsp), %xmm2
movsd %xmm2, -0x8(%rcx)
movsd %xmm2, -0x20(%rcx)
movsd 0x2a0(%rsp), %xmm2
movsd %xmm2, 0x18(%rcx)
movsd %xmm2, -0x18(%rcx)
movsd %xmm1, 0x38(%rcx)
movsd %xmm1, -0x10(%rcx)
xorl %edx, %edx
movq %rax, %rsi
xorl %edi, %edi
movsd (%rsi,%rdi), %xmm1
addsd (%rax,%rdi,4), %xmm1
movsd %xmm1, (%rcx,%rdi)
addq $0x8, %rdi
cmpq $0x18, %rdi
jne 0x4d043
incq %rdx
leaq (%rdx,%rdx,4), %rdi
movsd 0x1e0(%rsp,%rdi,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x1e0(%rsp,%rdi,8)
addq $0x20, %rsi
addq $0x8, %rax
addq $0x20, %rcx
cmpq $0x3, %rdx
jne 0x4d041
leaq 0x1e0(%rsp), %rdx
leaq 0x5e0(%rsp), %rbx
leaq 0x1c0(%rsp), %r8
leaq 0x560(%rsp), %r9
movl $0x4, %edi
movl $0x4, %esi
movq %rbx, %rcx
callq 0x710b6
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x360, %rdi            # imm = 0x360
callq 0x2c2bc
subq $-0x80, %r14
cmpq $0x200, %r14            # imm = 0x200
jne 0x4d0c0
xorl %r15d, %r15d
movapd 0x54f38(%rip), %xmm1    # 0xa2020
leaq 0x668(%rsp), %rbp
xorl %r12d, %r12d
xorl %eax, %eax
movsd (%rbx,%rax,4), %xmm0
movsd %xmm0, 0x30(%rsp,%rax)
addq $0x8, %rax
cmpq $0x20, %rax
jne 0x4d0f5
movsd 0x30(%rsp), %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jbe 0x4d13c
xorl %eax, %eax
movapd 0x30(%rsp,%rax,8), %xmm0
xorpd %xmm1, %xmm0
movapd %xmm0, 0x30(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x4, %rax
jne 0x4d11c
movsd 0x30(%rsp), %xmm2
movapd %xmm2, 0x80(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x55cec(%rip), %rsi     # 0xa2e40
callq 0x2c2be
movapd 0x80(%rsp), %xmm11
xorpd %xmm0, %xmm0
ucomisd %xmm11, %xmm0
jbe 0x4d18e
movhpd 0x38(%rsp), %xmm11      # xmm11 = xmm11[0],mem[0]
movapd 0x54ea3(%rip), %xmm1    # 0xa2020
xorpd %xmm1, %xmm11
movapd 0x40(%rsp), %xmm0
xorpd %xmm1, %xmm0
jmp 0x4d19b
movapd 0x40(%rsp), %xmm0
movhpd 0x38(%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 0x55005(%rip), %xmm2    # 0xa21e0
ucomisd %xmm1, %xmm2
leaq 0x55c5a(%rip), %rsi     # 0xa2e40
ja 0x4d36f
xorps %xmm2, %xmm2
sqrtsd %xmm1, %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
divpd %xmm2, %xmm11
movapd %xmm11, 0x30(%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 0x54dec(%rip), %xmm10   # 0xa2020
xorpd %xmm10, %xmm6
divpd %xmm2, %xmm0
movapd %xmm0, %xmm5
shufpd $0x1, %xmm0, %xmm5      # xmm5 = xmm5[1],xmm0[0]
movlpd %xmm0, 0x40(%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, 0xa0(%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, 0xa8(%rsp)
movapd %xmm3, %xmm8
mulsd %xmm11, %xmm8
movapd %xmm4, %xmm9
mulsd %xmm0, %xmm9
subsd %xmm8, %xmm9
addsd %xmm9, %xmm9
movsd %xmm9, 0xc0(%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, 0xc8(%rsp)
mulsd %xmm11, %xmm4
movapd %xmm0, %xmm7
mulsd %xmm3, %xmm7
addsd %xmm4, %xmm7
addsd %xmm7, %xmm7
movsd %xmm7, 0xd0(%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, 0xe0(%rsp)
mulsd %xmm3, %xmm3
addsd %xmm1, %xmm3
addsd %xmm8, %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, 0xf0(%rsp)
leaq 0xa0(%rsp), %rsi
movq %rbp, %rdi
callq 0x2c2be
movq %r15, %rax
shlq $0x7, %rax
leaq (%rsp,%rax), %r14
addq $0x360, %r14            # imm = 0x360
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2dea2
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
leaq 0x68(%rsp), %rcx
leaq 0x50(%rsp), %r8
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq %r13, %r9
callq 0x4ca0f
movsd %xmm0, 0x120(%rsp,%r15,8)
movslq %r12d, %r12
movsd 0x120(%rsp,%r12,8), %xmm1
ucomisd %xmm0, %xmm1
cmoval %r15d, %r12d
incq %r15
addq $0x8, %rbx
cmpq $0x4, %r15
movapd 0x54c2f(%rip), %xmm1    # 0xa2020
jne 0x4d0f3
movslq %r12d, %rax
shlq $0x7, %rax
leaq (%rsp,%rax), %rsi
addq $0x360, %rsi            # imm = 0x360
leaq 0x2e0(%rsp), %rdi
callq 0x2dea2
movb 0x6(%rsp), %r14b
movb 0x5(%rsp), %r12b
leaq 0x55a19(%rip), %rsi     # 0xa2e40
movq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x2c2be
movb 0x7(%rsp), %bpl
testb %bpl, %bpl
jne 0x4d46b
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
leaq 0x360(%rsp), %rbx
movq %rbx, %rdi
callq 0x3cfc6
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2d3f8
testb %r14b, %r14b
jne 0x4d484
movsd 0x10(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
movq %r15, %rdi
callq 0x2d5a6
testb %r12b, %r12b
jne 0x4d4af
leaq 0x360(%rsp), %rbx
leaq 0x2e0(%rsp), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2e2af
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2dea2
testb %bpl, %bpl
jne 0x4d50a
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
callq 0x3cfc6
leaq 0x360(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3dc00
movq %r15, %rdi
movq %r14, %rsi
callq 0x2d3f8
jmp 0x4d50a
leaq 0x55943(%rip), %rsi     # 0xa2e40
movq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x2c2be
movq %r15, %rax
addq $0x6e8, %rsp            # imm = 0x6E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq