// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/apps/mshalign/mshalign.cpp
// Function: ICPAlignmentTransformation(gaps::R3Mesh*, gaps::R3Point const*, int, gaps::R3Mesh*, gaps::R3Point const*, int, gaps::R3Affine&, gaps::R3Affine&, int, int, int)
static int
ICPAlignmentTransformation(
  R3Mesh *mesh1, const R3Point *points1, int npoints1,
  R3Mesh *mesh2, const R3Point *points2, int npoints2, 
  R3Affine& affine12, R3Affine& affine21,
  int translation, int rotation, int scale)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Allocate arrays of correspondences
  int ncorrespondences = npoints1 + npoints2;
  R3Point *correspondences_buffer1 = new R3Point [2 * ncorrespondences];
  R3Point *correspondences_buffer2 = new R3Point [2 * ncorrespondences];
  assert(correspondences_buffer1 && correspondences_buffer2);

  // Iterate until max_iterations or converged
  RNBoolean converged = FALSE;
  const int max_iterations = 128;
  for (int iteration = 0; iteration < max_iterations; iteration++) {
    // Get arrays of correspondences
    R3Point *correspondences1 = &correspondences_buffer1[iteration%2 * ncorrespondences];
    R3Point *correspondences2 = &correspondences_buffer2[iteration%2 * ncorrespondences];

    // Update correspondences for aligning transformation
    CreatePointCorrespondences(
      mesh1, points1, npoints1, 
      mesh2, points2, npoints2, 
      affine12, affine21, 
      correspondences1, correspondences2, 
      ncorrespondences);

    // Update aligning transformation for correspondences
    R4Matrix matrix = R3AlignPoints(ncorrespondences, correspondences2, correspondences1, 
      NULL, translation, rotation, scale);
    affine12.Reset(matrix);
    affine21 = affine12.Inverse();

    // Check for convergence
    converged = FALSE;
    if (iteration > 0) {
      converged = TRUE;
      R3Point *prev_correspondences1 = &correspondences_buffer1[(1-(iteration%2)) * ncorrespondences];
      R3Point *prev_correspondences2 = &correspondences_buffer2[(1-(iteration%2)) * ncorrespondences];
      for (int i = 0; i < ncorrespondences; i++) {
        if (!R3Contains(correspondences1[i], prev_correspondences1[i])) { converged = FALSE; break; }
        if (!R3Contains(correspondences2[i], prev_correspondences2[i])) { converged = FALSE; break; }
      }
    }
    if (converged) break;
  }

  // Return whether converged
  return converged;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movl %r9d, %r15d
movq %r8, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
movl %edx, %r14d
movq %rsi, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
callq 0x55804
movq %rbx, %rdi
xorl %esi, %esi
callq 0xc5b0
movq %r14, 0x58(%rsp)
movq %r15, 0x70(%rsp)
addl %r15d, %r14d
leal (%r14,%r14), %eax
movslq %eax, %r15
movl $0x18, %ecx
movq %r15, %rax
mulq %rcx
movq $-0x1, %rbx
cmovnoq %rax, %rbx
movq %rbx, %rdi
callq 0xc030
movq %rax, 0x20(%rsp)
movq %r14, 0x10(%rsp)
testl %r14d, %r14d
je 0x115f4
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %r14
xorl %r12d, %r12d
movq 0x20(%rsp), %rax
leaq (%rax,%r12), %rdi
callq 0x424b8
addq $0x18, %r12
cmpq %r12, %r14
jne 0x115dd
movq %rbx, %rdi
callq 0xc030
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x10(%rsp)
je 0x1162a
shlq $0x3, %r15
leaq (%r15,%r15,2), %r14
xorl %r15d, %r15d
movq 0x18(%rsp), %rax
leaq (%rax,%r15), %rdi
callq 0x424b8
addq $0x18, %r15
cmpq %r15, %r14
jne 0x11613
movq 0x258(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x40(%rsp)
movl $0x88, %ecx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
leaq 0x88(%rsp), %rax
addq %rax, %rcx
movq %rcx, 0x38(%rsp)
movq 0x10(%rsp), %rbp
movl %ebp, %eax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
movb $0x1, 0xf(%rsp)
movl %r14d, %eax
andl $0x1, %eax
imull %ebp, %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
movq 0x18(%rsp), %rax
leaq (%rax,%rbx), %r12
addq 0x20(%rsp), %rbx
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq 0x68(%rsp), %r8
movq 0x70(%rsp), %r9
pushq %r12
pushq %rbx
pushq 0x268(%rsp)
movq 0x268(%rsp), %r13
pushq %r13
callq 0x11a0e
addq $0x20, %rsp
leaq 0x198(%rsp), %r15
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
movl 0x260(%rsp), %r9d
movl 0x270(%rsp), %eax
pushq %rax
movl 0x270(%rsp), %eax
pushq %rax
callq 0x22331
addq $0x10, %rsp
movq %r13, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3d0ce
movq %r13, %rdi
callq 0x3ca96
movl 0x108(%r13), %edx
andl $0x1, %edx
leaq 0x88(%rsp), %rdi
movq %rax, %rsi
callq 0x3ca2a
movq 0x40(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x4099a
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x4099a
movq 0x190(%rsp), %rax
movq 0x258(%rsp), %rcx
movq %rax, 0x108(%rcx)
leaq 0x88(%rsp), %rdi
callq 0x3ca78
testl %r14d, %r14d
je 0x117f1
movl $0x1, %eax
movb $0x1, %cl
movq 0x10(%rsp), %rbp
testl %ebp, %ebp
jle 0x117fa
movzbl 0xf(%rsp), %eax
andl $0x1, %eax
imull %ebp, %eax
cltq
shlq $0x3, %rax
leaq (%rax,%rax,2), %r13
movq 0x18(%rsp), %rax
leaq (%rax,%r13), %rbp
addq 0x20(%rsp), %r13
movq 0x28(%rsp), %r15
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1a6ef
testl %eax, %eax
je 0x117f1
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1a6ef
testl %eax, %eax
je 0x117f1
addq $0x18, %rbp
addq $0x18, %r12
addq $0x18, %r13
addq $0x18, %rbx
decq %r15
jne 0x117b0
movq 0x10(%rsp), %rbp
movl $0x1, %eax
movb $0x1, %cl
jmp 0x117fa
xorl %ecx, %ecx
xorl %eax, %eax
movq 0x10(%rsp), %rbp
testb %cl, %cl
jne 0x11812
incl %r14d
incb 0xf(%rsp)
cmpl $0x80, %r14d
jne 0x1166c
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq 0x18(%rsp), %rdi
jmp 0x11836
movq %rax, %r14
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0xc6c0
jmp 0x11850
movq %rax, %r14
leaq 0x88(%rsp), %rdi
callq 0x3ca78
movq %r14, %rdi
callq 0xc620