// File: /workspace/github_cpp_makefile_star2_O2/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 $0x208, %rsp            # imm = 0x208
movl %r9d, %r14d
movq %r8, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movl %edx, %r15d
movq %rsi, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x4dc92
movq %rbx, %rdi
callq 0x10fda
movq %r14, 0x58(%rsp)
movq %r15, 0x40(%rsp)
addl %r15d, %r14d
leal (%r14,%r14), %eax
movslq %eax, %r12
pushq $0x18
popq %rcx
movq %r12, %rax
mulq %rcx
pushq $-0x1
popq %rbx
cmovnoq %rax, %rbx
movq %rbx, %rdi
callq 0xc030
movq %rax, %r13
movq %r14, 0x8(%rsp)
testl %r14d, %r14d
je 0x107d8
imulq $0x18, %r12, %r14
xorl %r15d, %r15d
leaq (%r15,%r13), %rdi
callq 0x3c81e
addq $0x18, %r15
cmpq %r15, %r14
jne 0x107c6
movq %rbx, %rdi
callq 0xc030
movq %rax, %rbp
cmpl $0x0, 0x8(%rsp)
je 0x10803
imulq $0x18, %r12, %r14
xorl %r15d, %r15d
leaq (%r15,%rbp), %rdi
callq 0x3c81e
addq $0x18, %r15
cmpq %r15, %r14
jne 0x107f1
movq 0x8(%rsp), %rax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
incq %rcx
movq %rcx, 0x28(%rsp)
movb $0x1, 0x7(%rsp)
xorl %r15d, %r15d
movq %r13, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
cmpl $0x80, %r15d
je 0x109a6
movl %r15d, %eax
andl $0x1, %eax
imull 0x8(%rsp), %eax
cltq
imulq $0x18, %rax, %r12
leaq (%r12,%r13), %rbx
leaq (%r12,%rbp), %r14
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x58(%rsp), %r9
pushq %r14
pushq %rbx
movq 0x258(%rsp), %r13
pushq %r13
movq 0x258(%rsp), %rbp
pushq %rbp
callq 0x10b70
addq $0x20, %rsp
leaq 0x78(%rsp), %r13
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
movl 0x250(%rsp), %r9d
movl 0x260(%rsp), %eax
pushq %rax
movl 0x260(%rsp), %eax
pushq %rax
callq 0x1f80b
popq %rax
popq %rcx
movq %rbp, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x37dfe
leaq 0xf8(%rsp), %rdi
movq %rbp, %rsi
movq %rdi, %rbx
callq 0x1108a
movq 0x248(%rsp), %rdi
movq %rbx, %rsi
callq 0x110bc
movq %rbx, %rdi
callq 0x37802
movl %r15d, 0x14(%rsp)
testl %r15d, %r15d
je 0x10976
movzbl 0x7(%rsp), %eax
andl $0x1, %eax
imull 0x8(%rsp), %eax
cltq
imulq $0x18, %rax, %rbx
movq 0x18(%rsp), %rax
leaq (%rax,%rbx), %rbp
leaq (%rax,%r12), %r14
movq 0x20(%rsp), %rax
addq %rax, %rbx
addq %rax, %r12
movq 0x28(%rsp), %r15
decq %r15
je 0x10991
movq %r12, %rdi
movq %rbx, %rsi
callq 0x18f23
testl %eax, %eax
je 0x10976
leaq 0x18(%rbp), %rax
movq %rax, 0x60(%rsp)
leaq 0x18(%r14), %r13
addq $0x18, %rbx
addq $0x18, %r12
movq %r14, %rdi
movq %rbp, %rsi
callq 0x18f23
movq %r13, %r14
movq 0x60(%rsp), %rbp
testl %eax, %eax
jne 0x10936
movl 0x14(%rsp), %r15d
incl %r15d
incb 0x7(%rsp)
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %rbp
jmp 0x1082c
pushq $0x1
popq %rax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x10994
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x37802
jmp 0x109d2
movq %rax, %r14
movq %rbp, %rdi
jmp 0x109ca
movq %rax, %r14
movq %r13, %rdi
movq %rbx, %rsi
callq 0xc6f0
movq %r14, %rdi
callq 0xc650