// File: /workspace/github_cpp_makefile_star2_O2/devernay[P]vlpovutils/motionfield.cpp
// Function: vlpov_motionfield(VLPovCamera const&, VLPovImage<double> const&, VLPovCamera const&, VLPovImage<double>&, VLPovImage<double>&)
void vlpov_motionfield(const VLPovCamera &cam1, const VLPovImage<double> &z1, const VLPovCamera &cam2, VLPovImage<double> &m1x, VLPovImage<double> &m1y)
{
    assert(cam1.width == z1.width() && cam1.height == z1.height());
    m1x.resize(cam1.width, cam1.height);
    m1y.resize(cam1.width, cam1.height);
    
    double fx = cam1.K(0,0);
    double fy = cam1.K(1,1);
    double s = cam1.K(0,1);
    double cx = cam1.K(0,2);
    double cy = cam1.K(1,2);
    
    // compute inverse intrinsic matrix;
    double ifx = 1./fx;
    double ify = 1./fy;
    double is = -s/(fx*fy);
    double icx = is*cy -cx/fx;
    double icy = -cy/fy;
    
    for(int y1 = 0; y1 < z1.height(); ++y1) {
        for(int x1 = 0; x1 < z1.width(); ++x1) {
            // apply inverse intrisics
            double x1i = ifx*x1 + is*y1 + icx;
            double y1i = ify*y1 + icy;

            // compute the 3-D reconstruction P1 in camera 1 reference frame
            double Z1 = z1(x1,y1);
            vector3 P1 = vector3_from_coeffs(x1i * Z1, y1i * Z1, Z1);
#           ifdef DEBUG
            // project
            vector3 p1 = prod(cam1.K , P1);
            // compute coordinates
            double x1p = p1(0)/p1(2);
            double y1p = p1(1)/p1(2);
            assert(std::abs(x1p - x1) < 1e-3 && std::abs(y1p - y1) < 1e-3);
#           endif
            // reconstruct in the global reference frame
            vector3 P = prod(trans(cam1.R),P1-cam1.t);
            // reconstruct in camera 2 reference frame
            vector3 P2 = prod(cam2.R,P)+cam2.t;
            // project
            vector3 p2 = prod(cam2.K , P2);
            // compute coordinates
            double x2 = p2(0)/p2(2);
            double y2 = p2(1)/p2(2);
            // compute motion field
            m1x(x1,y1) = x2-x1;
            m1y(x1,y1) = y2-y1;
        }
    }
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %r8, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r12
movl (%rdi), %esi
cmpl (%r12), %esi
jne 0x4015
movq %rdi, %rbx
movl 0x4(%rdi), %edx
cmpl 0x4(%r12), %edx
jne 0x4015
movq 0x20(%rsp), %rdi
callq 0x4032
movl (%rbx), %esi
movl 0x4(%rbx), %edx
movq 0x28(%rsp), %rdi
callq 0x4032
movl $0xf8, %eax
leaq (%rbx,%rax), %r15
xorl %r13d, %r13d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4082
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
pushq $0x1
popq %rbp
movq %r15, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x4082
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x4082
movsd (%rax), %xmm0
movaps %xmm0, 0x30(%rsp)
pushq $0x2
popq %r14
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x4082
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x4082
movsd (%rax), %xmm1
movsd 0x42a4(%rip), %xmm0     # 0x8008
movapd %xmm0, %xmm2
movsd (%rsp), %xmm3
divsd %xmm3, %xmm2
movsd %xmm2, 0x60(%rsp)
movsd 0x18(%rsp), %xmm2
divsd %xmm2, %xmm0
movsd %xmm0, 0x40(%rsp)
movapd 0x4851(%rip), %xmm0     # 0x85e0
movapd 0x30(%rsp), %xmm5
xorpd %xmm0, %xmm5
movsd 0x10(%rsp), %xmm6
divsd %xmm3, %xmm6
mulsd %xmm2, %xmm3
movapd %xmm2, %xmm4
divsd %xmm3, %xmm5
movapd %xmm5, 0x30(%rsp)
movapd %xmm5, %xmm2
mulsd %xmm1, %xmm2
subsd %xmm6, %xmm2
movapd %xmm2, 0xa0(%rsp)
xorpd %xmm0, %xmm1
divsd %xmm4, %xmm1
movapd %xmm1, 0x90(%rsp)
movl $0x158, %eax            # imm = 0x158
leaq (%rbx,%rax), %rcx
movq %rcx, 0x50(%rsp)
movl $0x1b8, %ecx            # imm = 0x1B8
addq %rcx, %rbx
movq %rbx, 0x68(%rsp)
movq 0x8(%rsp), %rbp
addq %rbp, %rax
movq %rax, 0x58(%rsp)
addq %rbp, %rcx
movq %rcx, 0x48(%rsp)
movl $0xf8, %eax
addq %rax, %rbp
movq %rbp, 0x8(%rsp)
leaq 0xe8(%rsp), %rbp
leaq 0xc8(%rsp), %r15
cmpl 0x4(%r12), %r13d
jge 0x4003
cvtsi2sd %r13d, %xmm0
movapd 0x30(%rsp), %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, 0xb0(%rsp)
movsd 0x40(%rsp), %xmm1
movsd %xmm0, 0x78(%rsp)
mulsd %xmm0, %xmm1
addsd 0x90(%rsp), %xmm1
movsd %xmm1, 0x70(%rsp)
xorl %ebx, %ebx
movl (%r12), %eax
cmpl %eax, %ebx
jge 0x3ffb
cvtsi2sd %ebx, %xmm1
movsd %xmm1, 0x18(%rsp)
movsd 0x60(%rsp), %xmm0
mulsd %xmm1, %xmm0
addsd 0xb0(%rsp), %xmm0
addsd 0xa0(%rsp), %xmm0
movq 0x8(%r12), %rcx
imull %r13d, %eax
addl %ebx, %eax
cltq
movsd (%rcx,%rax,8), %xmm2
mulsd %xmm2, %xmm0
movsd 0x70(%rsp), %xmm1
mulsd %xmm2, %xmm1
leaq 0x128(%rsp), %r14
movq %r14, %rdi
callq 0x40a6
movq 0x50(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq %r14, 0xf0(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0xf8(%rsp)
leaq 0x108(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4618
movq 0x58(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq %r14, 0xd0(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x4a3a
movq 0x8(%rsp), %rax
movq %rax, 0x80(%rsp)
movq %rbp, 0x88(%rsp)
movq %r15, %rdi
leaq 0x80(%rsp), %rsi
callq 0x4c04
movq %r15, %rdi
xorl %esi, %esi
callq 0x4108
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %r15, %rdi
pushq $0x2
popq %r14
movq %r14, %rsi
callq 0x4108
movsd (%rsp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %r15, %rdi
pushq $0x1
popq %rsi
callq 0x4108
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x4108
movsd 0x10(%rsp), %xmm1
divsd (%rax), %xmm1
movsd (%rsp), %xmm0
subsd 0x18(%rsp), %xmm0
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rax
movslq (%rcx), %rcx
movslq %r13d, %rdx
imulq %rdx, %rcx
addq %rbx, %rcx
movsd %xmm0, (%rax,%rcx,8)
subsd 0x78(%rsp), %xmm1
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rax
movslq (%rcx), %rcx
imulq %rdx, %rcx
addq %rbx, %rcx
movsd %xmm1, (%rax,%rcx,8)
incq %rbx
jmp 0x3e6c
incl %r13d
jmp 0x3e28
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x45d4(%rip), %rdi      # 0x85f0
leaq 0x4604(%rip), %rsi      # 0x8627
leaq 0x460d(%rip), %rcx      # 0x8637
pushq $0xf
popq %rdx
callq 0x20e0