// File: devernay[P]vlpovutils/vlpov_project.cpp
// Function: main
int main(int argc, char *argv[])
{
    if (argc != 2 && argc != 3) {
        fprintf(stderr, USAGE, argv[0]);
        exit(2);
    }
    bool do_motion = (argc == 3);
    const char *frame1 = argv[1];
    const char *frame2 = do_motion ? argv[2] : NULL;
    
    {
        VLPovCamera cam1;
        VLPovCamera cam2;
        int retval;
        
        retval = vlpov_povread(frame1, cam1);
        if(retval != 0) {
            fprintf(stderr,"Error %d while reading %s.txt\n", retval, frame1);
            exit(1);
        }
        
        //cout << "Intrinsic matrix 1(new): " << endl << cam1.K << endl;
        //cout << "Extrinsic 1(new): " << endl << cam1.R << endl << cam1.t << endl;
      
        if (do_motion) {
            retval = vlpov_povread(frame2, cam2);
            if(retval != 0) {
                fprintf(stderr,"Error %d while reading %s.txt\n", retval, frame2);
                exit(1);
            }
        }
        
        vector3 P; // position in the global reference frame
        while(scanf("%lg %lg %lg", &P(0), &P(1), &P(2)) == 3) {
              
            // reconstruct in camera 1 reference frame
            vector3 P1 = prod(cam1.R,P)+cam1.t;
            // project
            vector3 p1 = prod(cam1.K , P1);
            // compute coordinates
            double x1 = p1(0)/p1(2);
            double y1 = p1(1)/p1(2);
            if (!do_motion) {
                printf("%g %g %g\n", x1, y1, P1(2));
            } else {
                // 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);
                printf("%g %g %g %g %g\n", x1, y1, P1(2), x2-x1, y2-y1);
            }
        }
    }
    
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5e0, %rsp            # imm = 0x5E0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x248e
cmpl $0x3, -0x8(%rbp)
je 0x248e
movq 0x10b64(%rip), %rax     # 0x12fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0xab87(%rip), %rsi      # 0xd004
movb $0x0, %al
callq 0x2190
movl $0x2, %edi
callq 0x2160
cmpl $0x3, -0x8(%rbp)
sete %al
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
testb $0x1, -0x11(%rbp)
je 0x24bd
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x520(%rbp)
jmp 0x24c8
xorl %eax, %eax
movq %rax, -0x520(%rbp)
jmp 0x24c8
movq -0x520(%rbp), %rax
movq %rax, -0x28(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x2c40
leaq -0x3d8(%rbp), %rdi
callq 0x2c40
jmp 0x24ed
movq -0x20(%rbp), %rdi
leaq -0x200(%rbp), %rsi
callq 0x5060
movl %eax, -0x524(%rbp)
jmp 0x2505
movl -0x524(%rbp), %eax
movl %eax, -0x3e8(%rbp)
cmpl $0x0, -0x3e8(%rbp)
je 0x2574
movq 0x10aaf(%rip), %rax     # 0x12fd0
movq (%rax), %rdi
movl -0x3e8(%rbp), %edx
movq -0x20(%rbp), %rcx
leaq 0xacc6(%rip), %rsi      # 0xd1fb
movb $0x0, %al
callq 0x2190
movl $0x1, %edi
callq 0x2160
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
jmp 0x2c26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
jmp 0x2c1a
testb $0x1, -0x11(%rbp)
je 0x25d5
movq -0x28(%rbp), %rdi
leaq -0x3d8(%rbp), %rsi
callq 0x5060
movl %eax, -0x528(%rbp)
jmp 0x2592
movl -0x528(%rbp), %eax
movl %eax, -0x3e8(%rbp)
cmpl $0x0, -0x3e8(%rbp)
je 0x25d3
movq 0x10a22(%rip), %rax     # 0x12fd0
movq (%rax), %rdi
movl -0x3e8(%rbp), %edx
movq -0x28(%rbp), %rcx
leaq 0xac39(%rip), %rsi      # 0xd1fb
movb $0x0, %al
callq 0x2190
movl $0x1, %edi
callq 0x2160
jmp 0x25d5
leaq -0x408(%rbp), %rdi
callq 0x2e00
jmp 0x25e3
jmp 0x25e5
xorl %eax, %eax
movl %eax, %esi
leaq -0x408(%rbp), %rdi
callq 0x2e20
movq %rax, -0x530(%rbp)
jmp 0x25fe
leaq -0x408(%rbp), %rdi
movl $0x1, %esi
callq 0x2e20
movq %rax, -0x538(%rbp)
jmp 0x2618
leaq -0x408(%rbp), %rdi
movl $0x2, %esi
callq 0x2e20
movq %rax, -0x540(%rbp)
jmp 0x2632
movq -0x540(%rbp), %rcx
movq -0x538(%rbp), %rdx
movq -0x530(%rbp), %rsi
leaq 0xabcc(%rip), %rdi      # 0xd21a
xorl %eax, %eax
callq 0x2130
movl %eax, -0x544(%rbp)
jmp 0x265d
movl -0x544(%rbp), %eax
cmpl $0x3, %eax
jne 0x2bdf
leaq -0xa8(%rbp), %rsi
leaq -0x450(%rbp), %rdi
leaq -0x408(%rbp), %rdx
callq 0x2ea0
jmp 0x2688
leaq -0x48(%rbp), %rdx
leaq -0x440(%rbp), %rdi
leaq -0x450(%rbp), %rsi
callq 0x2e50
jmp 0x26a1
leaq -0x428(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x2ee0
jmp 0x26b6
leaq -0x440(%rbp), %rdi
callq 0x2f10
leaq -0x450(%rbp), %rdi
callq 0x2f50
leaq -0x108(%rbp), %rsi
leaq -0x480(%rbp), %rdi
leaq -0x428(%rbp), %rdx
callq 0x2ea0
jmp 0x26ea
leaq -0x470(%rbp), %rdi
leaq -0x480(%rbp), %rsi
callq 0x2f90
jmp 0x26ff
leaq -0x480(%rbp), %rdi
callq 0x2f50
xorl %eax, %eax
movl %eax, %esi
leaq -0x470(%rbp), %rdi
callq 0x2e20
movq %rax, -0x550(%rbp)
jmp 0x2724
movq -0x550(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x560(%rbp)
leaq -0x470(%rbp), %rdi
movl $0x2, %esi
callq 0x2e20
movq %rax, -0x558(%rbp)
jmp 0x2751
movsd -0x560(%rbp), %xmm0
movq -0x558(%rbp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x488(%rbp)
leaq -0x470(%rbp), %rdi
movl $0x1, %esi
callq 0x2e20
movq %rax, -0x568(%rbp)
jmp 0x278a
movq -0x568(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x578(%rbp)
leaq -0x470(%rbp), %rdi
movl $0x2, %esi
callq 0x2e20
movq %rax, -0x570(%rbp)
jmp 0x27b7
movq -0x570(%rbp), %rax
movsd -0x578(%rbp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, -0x490(%rbp)
testb $0x1, -0x11(%rbp)
jne 0x28f1
movsd -0x488(%rbp), %xmm0
movsd %xmm0, -0x590(%rbp)
movsd -0x490(%rbp), %xmm0
movsd %xmm0, -0x588(%rbp)
leaq -0x428(%rbp), %rdi
movl $0x2, %esi
callq 0x2e20
movq %rax, -0x580(%rbp)
jmp 0x2816
movsd -0x588(%rbp), %xmm1
movsd -0x590(%rbp), %xmm0
movq -0x580(%rbp), %rax
movsd (%rax), %xmm2
leaq 0xa9f4(%rip), %rdi      # 0xd22c
movb $0x3, %al
callq 0x2050
jmp 0x2841
jmp 0x2ba8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
jmp 0x2c0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
jmp 0x288f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
leaq -0x440(%rbp), %rdi
callq 0x2f10
leaq -0x450(%rbp), %rdi
callq 0x2f50
jmp 0x2c0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
jmp 0x2bd1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
leaq -0x480(%rbp), %rdi
callq 0x2f50
jmp 0x2bd1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
jmp 0x2bc5
leaq -0x280(%rbp), %rsi
leaq -0x4d8(%rbp), %rdi
leaq -0x408(%rbp), %rdx
callq 0x2ea0
jmp 0x290d
leaq -0x220(%rbp), %rdx
leaq -0x4c8(%rbp), %rdi
leaq -0x4d8(%rbp), %rsi
callq 0x2e50
jmp 0x2929
leaq -0x4b0(%rbp), %rdi
leaq -0x4c8(%rbp), %rsi
callq 0x2ee0
jmp 0x293e
leaq -0x4c8(%rbp), %rdi
callq 0x2f10
leaq -0x4d8(%rbp), %rdi
callq 0x2f50
leaq -0x2e0(%rbp), %rsi
leaq -0x508(%rbp), %rdi
leaq -0x4b0(%rbp), %rdx
callq 0x2ea0
jmp 0x2972
leaq -0x4f8(%rbp), %rdi
leaq -0x508(%rbp), %rsi
callq 0x2f90
jmp 0x2987
leaq -0x508(%rbp), %rdi
callq 0x2f50
xorl %eax, %eax
movl %eax, %esi
leaq -0x4f8(%rbp), %rdi
callq 0x2e20
movq %rax, -0x598(%rbp)
jmp 0x29ac
movq -0x598(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x5a8(%rbp)
leaq -0x4f8(%rbp), %rdi
movl $0x2, %esi
callq 0x2e20
movq %rax, -0x5a0(%rbp)
jmp 0x29d9
movsd -0x5a8(%rbp), %xmm0
movq -0x5a0(%rbp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x510(%rbp)
leaq -0x4f8(%rbp), %rdi
movl $0x1, %esi
callq 0x2e20
movq %rax, -0x5b0(%rbp)
jmp 0x2a12
movq -0x5b0(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x5c0(%rbp)
leaq -0x4f8(%rbp), %rdi
movl $0x2, %esi
callq 0x2e20
movq %rax, -0x5b8(%rbp)
jmp 0x2a3f
movsd -0x5c0(%rbp), %xmm0
movq -0x5b8(%rbp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x518(%rbp)
movsd -0x488(%rbp), %xmm0
movsd %xmm0, -0x5d8(%rbp)
movsd -0x490(%rbp), %xmm0
movsd %xmm0, -0x5d0(%rbp)
leaq -0x428(%rbp), %rdi
movl $0x2, %esi
callq 0x2e20
movq %rax, -0x5c8(%rbp)
jmp 0x2a98
movsd -0x5d0(%rbp), %xmm1
movsd -0x5d8(%rbp), %xmm0
movq -0x5c8(%rbp), %rax
movsd (%rax), %xmm2
movsd -0x510(%rbp), %xmm3
movsd -0x488(%rbp), %xmm4
subsd %xmm4, %xmm3
movsd -0x518(%rbp), %xmm4
movsd -0x490(%rbp), %xmm5
subsd %xmm5, %xmm4
leaq 0xa744(%rip), %rdi      # 0xd226
movb $0x5, %al
callq 0x2050
jmp 0x2aeb
leaq -0x4f8(%rbp), %rdi
callq 0x2fc0
leaq -0x4b0(%rbp), %rdi
callq 0x2fc0
jmp 0x2ba8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
jmp 0x2b3a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
leaq -0x4c8(%rbp), %rdi
callq 0x2f10
leaq -0x4d8(%rbp), %rdi
callq 0x2f50
jmp 0x2bc5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
jmp 0x2b9a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
leaq -0x508(%rbp), %rdi
callq 0x2f50
jmp 0x2b9a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3e0(%rbp)
movl %eax, -0x3e4(%rbp)
leaq -0x4f8(%rbp), %rdi
callq 0x2fc0
leaq -0x4b0(%rbp), %rdi
callq 0x2fc0
jmp 0x2bc5
leaq -0x470(%rbp), %rdi
callq 0x2fc0
leaq -0x428(%rbp), %rdi
callq 0x2fc0
jmp 0x25e5
leaq -0x470(%rbp), %rdi
callq 0x2fc0
leaq -0x428(%rbp), %rdi
callq 0x2fc0
jmp 0x2c0e
leaq -0x408(%rbp), %rdi
callq 0x2fc0
leaq -0x3d8(%rbp), %rdi
callq 0x2fe0
leaq -0x200(%rbp), %rdi
callq 0x2fe0
xorl %eax, %eax
addq $0x5e0, %rsp            # imm = 0x5E0
popq %rbp
retq
leaq -0x408(%rbp), %rdi
callq 0x2fc0
leaq -0x3d8(%rbp), %rdi
callq 0x2fe0
leaq -0x200(%rbp), %rdi
callq 0x2fe0
movq -0x3e0(%rbp), %rdi
callq 0x2240
nop