// File: 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
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x1a8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6b10
movl -0x1a8(%rbp), %ecx
movl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x1a1(%rbp)
jne 0x7081
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x1ac(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6b20
movl %eax, %ecx
movl -0x1ac(%rbp), %eax
cmpl %ecx, %eax
sete %al
movb %al, -0x1a1(%rbp)
movb -0x1a1(%rbp), %al
testb $0x1, %al
jne 0x708d
jmp 0x708f
jmp 0x70ae
leaq 0xb4c3(%rip), %rdi      # 0x12559
leaq 0xb4f3(%rip), %rsi      # 0x12590
movl $0xf, %edx
leaq 0xb4f7(%rip), %rcx      # 0x125a0
callq 0x3140
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movl (%rax), %esi
movq -0x8(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x7760
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rax
movl (%rax), %esi
movq -0x8(%rbp), %rax
movl 0x4(%rax), %edx
callq 0x7760
movq -0x8(%rbp), %rdi
addq $0xf8, %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x7800
movsd (%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
addq $0xf8, %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x7800
movsd (%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
addq $0xf8, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x7800
movsd (%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
addq $0xf8, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x7800
movsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x8(%rbp), %rdi
addq $0xf8, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x7800
movsd (%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd 0xae7e(%rip), %xmm0     # 0x12008
divsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd 0xae6c(%rip), %xmm0     # 0x12008
divsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x40(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd -0x30(%rbp), %xmm1
mulsd -0x38(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
movsd -0x50(%rbp), %xmm2
movsd -0x48(%rbp), %xmm1
divsd -0x30(%rbp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x50(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
divsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x1b0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6b20
movl %eax, %ecx
movl -0x1b0(%rbp), %eax
cmpl %ecx, %eax
jge 0x7740
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x1b4(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6b10
movl %eax, %ecx
movl -0x1b4(%rbp), %eax
cmpl %ecx, %eax
jge 0x7730
movsd -0x58(%rbp), %xmm0
cvtsi2sdl -0x80(%rbp), %xmm2
movsd -0x68(%rbp), %xmm1
cvtsi2sdl -0x7c(%rbp), %xmm3
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd -0x70(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x60(%rbp), %xmm0
cvtsi2sdl -0x7c(%rbp), %xmm2
movsd -0x78(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x10(%rbp), %rdi
movl -0x80(%rbp), %esi
movl -0x7c(%rbp), %edx
callq 0x7860
movsd (%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x88(%rbp), %xmm0
movsd -0x98(%rbp), %xmm2
mulsd %xmm2, %xmm0
movsd -0x90(%rbp), %xmm1
mulsd %xmm2, %xmm1
leaq -0xb8(%rbp), %rdi
callq 0x7890
movq -0x8(%rbp), %rsi
addq $0x158, %rsi            # imm = 0x158
leaq -0xf8(%rbp), %rdi
callq 0x7910
jmp 0x7335
movq -0x8(%rbp), %rdx
addq $0x1b8, %rdx            # imm = 0x1B8
leaq -0x118(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x7950
jmp 0x7355
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
leaq -0x118(%rbp), %rdx
callq 0x78d0
jmp 0x7371
leaq -0xd8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x79a0
jmp 0x7386
leaq -0xf0(%rbp), %rdi
callq 0x79d0
leaq -0x118(%rbp), %rdi
callq 0x7a10
leaq -0xf8(%rbp), %rdi
callq 0x7a50
movq -0x18(%rbp), %rsi
addq $0x158, %rsi            # imm = 0x158
leaq -0x160(%rbp), %rdi
leaq -0xd8(%rbp), %rdx
callq 0x7ad0
jmp 0x73ca
movq -0x18(%rbp), %rdx
addq $0x1b8, %rdx            # imm = 0x1B8
leaq -0x150(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x7a80
jmp 0x73ea
leaq -0x138(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x7b10
jmp 0x73ff
leaq -0x150(%rbp), %rdi
callq 0x7b40
leaq -0x160(%rbp), %rdi
callq 0x7b80
movq -0x18(%rbp), %rsi
addq $0xf8, %rsi
leaq -0x190(%rbp), %rdi
leaq -0x138(%rbp), %rdx
callq 0x7ad0
jmp 0x7437
leaq -0x180(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x7bc0
jmp 0x744c
leaq -0x190(%rbp), %rdi
callq 0x7b80
xorl %eax, %eax
movl %eax, %esi
leaq -0x180(%rbp), %rdi
callq 0x7bf0
movq %rax, -0x1c0(%rbp)
jmp 0x7471
movq -0x1c0(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x1d0(%rbp)
leaq -0x180(%rbp), %rdi
movl $0x2, %esi
callq 0x7bf0
movq %rax, -0x1c8(%rbp)
jmp 0x749e
movsd -0x1d0(%rbp), %xmm0
movq -0x1c8(%rbp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x198(%rbp)
leaq -0x180(%rbp), %rdi
movl $0x1, %esi
callq 0x7bf0
movq %rax, -0x1d8(%rbp)
jmp 0x74d7
movq -0x1d8(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x1e8(%rbp)
leaq -0x180(%rbp), %rdi
movl $0x2, %esi
callq 0x7bf0
movq %rax, -0x1e0(%rbp)
jmp 0x7504
movsd -0x1e8(%rbp), %xmm0
movq -0x1e0(%rbp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x1a0(%rbp)
movsd -0x198(%rbp), %xmm0
movl -0x80(%rbp), %esi
cvtsi2sd %esi, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x1f8(%rbp)
movq -0x20(%rbp), %rdi
movl -0x7c(%rbp), %edx
callq 0x7c20
movq %rax, -0x1f0(%rbp)
jmp 0x7553
movq -0x1f0(%rbp), %rax
movsd -0x1f8(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0x1a0(%rbp), %xmm0
movl -0x7c(%rbp), %edx
cvtsi2sd %edx, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x208(%rbp)
movq -0x28(%rbp), %rdi
movl -0x80(%rbp), %esi
callq 0x7c20
movq %rax, -0x200(%rbp)
jmp 0x7596
movq -0x200(%rbp), %rax
movsd -0x208(%rbp), %xmm0
movsd %xmm0, (%rax)
leaq -0x180(%rbp), %rdi
callq 0x61d0
leaq -0x138(%rbp), %rdi
callq 0x61d0
leaq -0xd8(%rbp), %rdi
callq 0x61d0
leaq -0xb8(%rbp), %rdi
callq 0x61d0
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x7263
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
jmp 0x7722
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
jmp 0x7650
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
jmp 0x7644
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x79d0
leaq -0x118(%rbp), %rdi
callq 0x7a10
leaq -0xf8(%rbp), %rdi
callq 0x7a50
jmp 0x7722
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
jmp 0x7716
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
jmp 0x76aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x7b40
leaq -0x160(%rbp), %rdi
callq 0x7b80
jmp 0x7716
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
jmp 0x770a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x7b80
jmp 0x770a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x61d0
leaq -0x138(%rbp), %rdi
callq 0x61d0
leaq -0xd8(%rbp), %rdi
callq 0x61d0
leaq -0xb8(%rbp), %rdi
callq 0x61d0
jmp 0x7749
jmp 0x7732
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x723a
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0x100(%rbp), %rdi
callq 0x33e0
nopw %cs:(%rax,%rax)
nop