Update to 2.0.0 tree from current Fremantle build
[opencv] / src / cv / cvcalcimagehomography.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "_cv.h"
43
44 /****************************************************************************************\
45
46    calculate image homography 
47    
48 \****************************************************************************************/
49
50 CV_IMPL void
51 cvCalcImageHomography( float* line, CvPoint3D32f* _center,
52                        float* _intrinsic, float* _homography )
53 {
54     CV_FUNCNAME( "cvCalcImageHomography" );
55
56     __BEGIN__;
57
58     double norm_xy, norm_xz, xy_sina, xy_cosa, xz_sina, xz_cosa, nx1, plane_dist;
59     float _ry[3], _rz[3], _r_trans[9];
60     CvMat rx = cvMat( 1, 3, CV_32F, line );
61     CvMat ry = cvMat( 1, 3, CV_32F, _ry );
62     CvMat rz = cvMat( 1, 3, CV_32F, _rz );
63     CvMat r_trans = cvMat( 3, 3, CV_32F, _r_trans );
64     CvMat center = cvMat( 3, 1, CV_32F, _center );
65
66     float _sub[9];
67     CvMat sub = cvMat( 3, 3, CV_32F, _sub );
68     float _t_trans[3];
69     CvMat t_trans = cvMat( 3, 1, CV_32F, _t_trans );
70
71     CvMat intrinsic = cvMat( 3, 3, CV_32F, _intrinsic );
72     CvMat homography = cvMat( 3, 3, CV_32F, _homography );
73
74     if( !line || !_center || !_intrinsic || !_homography )
75         CV_ERROR( CV_StsNullPtr, "" );
76
77     norm_xy = cvSqrt( line[0] * line[0] + line[1] * line[1] );
78     xy_cosa = line[0] / norm_xy;
79     xy_sina = line[1] / norm_xy;
80
81     norm_xz = cvSqrt( line[0] * line[0] + line[2] * line[2] );
82     xz_cosa = line[0] / norm_xz;
83     xz_sina = line[2] / norm_xz;
84
85     nx1 = -xz_sina;
86
87     _rz[0] = (float)(xy_cosa * nx1);
88     _rz[1] = (float)(xy_sina * nx1);
89     _rz[2] = (float)xz_cosa;
90     cvScale( &rz, &rz, 1./cvNorm(&rz,0,CV_L2) );
91
92     /*  new axe  y  */
93     cvCrossProduct( &rz, &rx, &ry );
94     cvScale( &ry, &ry, 1./cvNorm( &ry, 0, CV_L2 ) );
95
96     /*  transpone rotation matrix    */
97     memcpy( &_r_trans[0], line, 3*sizeof(float));
98     memcpy( &_r_trans[3], _ry, 3*sizeof(float));
99     memcpy( &_r_trans[6], _rz, 3*sizeof(float));
100
101     /*  calculate center distanse from arm plane  */
102     plane_dist = cvDotProduct( &center, &rz );
103
104     /* calculate (I - r_trans)*center */
105     cvSetIdentity( &sub );
106     cvSub( &sub, &r_trans, &sub );
107     cvMatMul( &sub, &center, &t_trans ); 
108
109     cvMatMul( &t_trans, &rz, &sub );
110     cvScaleAdd( &sub, cvRealScalar(1./plane_dist), &r_trans, &sub ); /* ? */
111
112     cvMatMul( &intrinsic, &sub, &r_trans );
113     cvInvert( &intrinsic, &sub, CV_SVD );
114     cvMatMul( &r_trans, &sub, &homography );
115
116     __END__;
117 }
118
119 /* End of file. */
120