// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Align.cpp
// Function: gaps::R2AlignPoints(gaps::RNArray<gaps::R2Point*> const&, gaps::RNArray<gaps::R2Point*> const&, double const*, int, int, int)
R3Matrix
R2AlignPoints(const RNArray<R2Point *>& points1, const RNArray<R2Point *>& points2, 
  const RNScalar *weights, RNBoolean align_center, RNBoolean align_rotation, int align_scale)
{
  // 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 (note: this should be weighted)
  R2Point center1(0.0, 0.0);
  R2Point center2(0.0, 0.0);
  if (align_center){
    center1 = R2Centroid(points1, weights);
    center2 = R2Centroid(points2, weights);
  }

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

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

    // Normalize covariance matrix
    if (total_weight == 0) return R3identity_matrix;
    for (int j = 0; j < 4; j++) m[j] /= total_weight;

    // Calculate SVD of covariance matrix
    RNScalar Um[4];
    RNScalar Wm[2];
    RNScalar Vmt[4];
    RNSvdDecompose(2, 2, m, Um, Wm, Vmt);

    // https://sakai.rutgers.edu/access/content/group/7bee3f05-9013-4fc2-8743-3c5078742791/material/svd_ls_rotation.pdf
    R3Matrix Ut(Um[0], Um[2], 0, Um[1], Um[3], 0, 0, 0, 1); 
    R3Matrix V(Vmt[0], Vmt[2], 0, Vmt[1], Vmt[3], 0, 0, 0, 1); 
    R3Matrix VUt = V * Ut;
    R3Matrix D = R3identity_matrix;
    D[1][1] = R3MatrixDet2(VUt[0][0], VUt[0][1], VUt[1][0], VUt[1][1]);
    rotation = V * D * Ut;
  }

  // Compute result
  R3Matrix result = R3identity_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 $0x2d8, %rsp            # imm = 0x2D8
movl %r9d, %ebp
movl %r8d, %ebx
movq %rcx, %r14
movq %rdi, 0x30(%rsp)
movl 0x310(%rsp), %r15d
movq %rsi, 0x20(%rsp)
movl 0xc(%rsi), %r13d
movq %rdx, 0x28(%rsp)
movl 0xc(%rdx), %eax
cmpl %r13d, %eax
cmovll %eax, %r13d
testl %r13d, %r13d
setle %r12b
leaq 0x48(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
callq 0x5f58c
leaq 0x38(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
callq 0x5f58c
testl %ebx, %ebx
sete %al
orb %r12b, %al
movl %eax, %r12d
jne 0x64d4e
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x6451c
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x5f6c4
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r14, %rdx
callq 0x6451c
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x5f6c4
cmpl $0x2, %r13d
setl %al
testl %r15d, %r15d
sete %cl
orb %al, %cl
movb %cl, 0xf(%rsp)
je 0x64d73
movsd 0x2e29d(%rip), %xmm0    # 0x93008
movsd %xmm0, 0x18(%rsp)
jmp 0x64d9d
leaq 0x48(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x64882
movsd %xmm0, 0x18(%rsp)
leaq 0x38(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r14, %rdx
callq 0x64882
movsd %xmm0, 0x10(%rsp)
cmpl $0x3, %r13d
setl %bl
leaq 0x35bbf(%rip), %rsi     # 0x9a970
leaq 0xc8(%rsp), %rdi
callq 0x5d834
testl %ebp, %ebp
sete %bpl
orb %bl, %bpl
jne 0x6503f
movb %bpl, 0xd(%rsp)
movb %r12b, 0xe(%rsp)
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rax
movapd %xmm0, 0x10(%rax)
movapd %xmm0, (%rax)
movl %r13d, %r15d
xorpd %xmm3, %xmm3
xorl %r12d, %r12d
leaq 0x128(%rsp), %r13
leaq 0x1b8(%rsp), %rbp
cmpq %r15, %r12
je 0x64edb
movsd %xmm3, 0x58(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r12,8), %rsi
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r12,8), %rbx
movq %r13, %rdi
leaq 0x48(%rsp), %rdx
callq 0x5f7c9
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
movsd 0x18(%rsp), %xmm0
callq 0x5fde7
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x38(%rsp), %rdx
callq 0x5f7c9
movq %r13, %rdi
movq %rbp, %rsi
movsd 0x10(%rsp), %xmm0
callq 0x5fde7
movsd 0x2e192(%rip), %xmm0    # 0x93008
testq %r14, %r14
je 0x64e81
movsd (%r14,%r12,8), %xmm0
leaq 0x60(%rsp), %rax
xorl %ecx, %ecx
movsd 0x58(%rsp), %xmm3
cmpq $0x2, %rcx
je 0x64ecf
movsd 0x80(%rsp,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
xorl %edx, %edx
cmpq $0x10, %rdx
je 0x64ec6
movsd 0x128(%rsp,%rdx), %xmm2
mulsd %xmm1, %xmm2
addsd (%rax,%rdx,2), %xmm2
movsd %xmm2, (%rax,%rdx,2)
addq $0x8, %rdx
jmp 0x64ea3
incq %rcx
addq $0x8, %rax
jmp 0x64e8e
addsd %xmm0, %xmm3
incq %r12
jmp 0x64e03
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
jne 0x64eeb
jnp 0x650e2
xorl %eax, %eax
movb 0xd(%rsp), %bpl
cmpq $0x4, %rax
je 0x64f0d
movsd 0x60(%rsp,%rax,8), %xmm0
divsd %xmm3, %xmm0
movsd %xmm0, 0x60(%rsp,%rax,8)
incq %rax
jmp 0x64ef2
pushq $0x2
popq %rdi
leaq 0x60(%rsp), %rdx
leaq 0x190(%rsp), %rbx
leaq 0x110(%rsp), %r8
leaq 0x170(%rsp), %r14
movl %edi, %esi
movq %rbx, %rcx
movq %r14, %r9
callq 0x678f6
movsd 0x10(%rbx), %xmm1
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm3
movsd 0x18(%rbx), %xmm4
movabsq $0x3ff0000000000000, %r15 # imm = 0x3FF0000000000000
movq %r15, (%rsp)
leaq 0x80(%rsp), %rbx
xorps %xmm2, %xmm2
xorps %xmm5, %xmm5
xorps %xmm6, %xmm6
xorps %xmm7, %xmm7
movq %rbx, %rdi
callq 0x5d88e
movsd 0x10(%r14), %xmm1
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm3
movsd 0x18(%r14), %xmm4
movq %r15, (%rsp)
leaq 0x128(%rsp), %r14
xorps %xmm2, %xmm2
xorps %xmm5, %xmm5
xorps %xmm6, %xmm6
xorps %xmm7, %xmm7
movq %r14, %rdi
callq 0x5d88e
leaq 0x1b8(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x5e359
leaq 0x359a5(%rip), %rsi     # 0x9a970
leaq 0x290(%rsp), %r12
movq %r12, %rdi
callq 0x5d834
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movsd 0x18(%r15), %xmm2
movsd 0x20(%r15), %xmm3
callq 0x5dd97
movsd %xmm0, 0x20(%r12)
leaq 0x200(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x5e359
leaq 0x248(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x5e359
leaq 0xc8(%rsp), %rdi
movq %r14, %rsi
callq 0x5e11c
movb 0xe(%rsp), %r12b
leaq 0x3592a(%rip), %rsi     # 0x9a970
movq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x5d834
testb %r12b, %r12b
jne 0x65078
leaq 0x80(%rsp), %rbx
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x51538
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5de7a
cmpb $0x0, 0xf(%rsp)
jne 0x65093
movsd 0x18(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %r15, %rdi
callq 0x5df40
testb %bpl, %bpl
jne 0x650a8
leaq 0xc8(%rsp), %rsi
movq %r15, %rdi
callq 0x65512
testb %r12b, %r12b
jne 0x650f6
leaq 0x128(%rsp), %rbx
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x51538
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5fd59
movq %r15, %rdi
movq %r14, %rsi
callq 0x5de7a
jmp 0x650f6
leaq 0x35887(%rip), %rsi     # 0x9a970
movq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x5d834
movq %r15, %rax
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq