Move the sources to trunk
[opencv] / tests / cv / src / aposit.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 "cvtest.h"
43
44
45 class CV_POSITTest : public CvTest
46 {
47 public:
48     CV_POSITTest();
49 protected:
50     void run(int);
51 };
52
53
54 CV_POSITTest::CV_POSITTest():
55     CvTest( "posit", "cvPOSIT" )
56 {
57     test_case_count = 20;
58     support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
59 }
60
61 void CV_POSITTest::run( int start_from )
62 {
63     int code = CvTS::OK;
64
65     /* fixed parameters output */
66     /*float rot[3][3]={  0.49010f,  0.85057f, 0.19063f,
67                       -0.56948f,  0.14671f, 0.80880f,
68                        0.65997f, -0.50495f, 0.55629f };
69
70     float trans[3] = { 0.0f, 0.0f, 40.02637f };
71     */
72
73     /* Some variables */
74     int i, counter;
75
76     CvTermCriteria criteria;
77     CvPoint3D32f* obj_points;
78     CvPoint2D32f* img_points;
79     CvPOSITObject* object;
80
81     float angleX, angleY, angleZ;
82     CvRNG* rng = ts->get_rng();
83     int progress = 0;
84
85     CvMat* true_rotationX = cvCreateMat( 3, 3, CV_32F );
86     CvMat* true_rotationY = cvCreateMat( 3, 3, CV_32F );
87     CvMat* true_rotationZ = cvCreateMat( 3, 3, CV_32F );
88     CvMat* tmp_matrix = cvCreateMat( 3, 3, CV_32F );
89     CvMat* true_rotation = cvCreateMat( 3, 3, CV_32F );
90     CvMat* rotation = cvCreateMat( 3, 3, CV_32F );
91     CvMat* translation = cvCreateMat( 3, 1, CV_32F );
92     CvMat* true_translation = cvCreateMat( 3, 1, CV_32F );
93
94     const float flFocalLength = 760.f;
95     const float flEpsilon = 0.1f;
96
97     /* Initilization */
98     criteria.type = CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;
99     criteria.epsilon = flEpsilon;
100     criteria.max_iter = 10000;
101
102     /* Allocating source arrays; */
103     obj_points = (CvPoint3D32f*)cvAlloc( 8 * sizeof(CvPoint3D32f) );
104     img_points = (CvPoint2D32f*)cvAlloc( 8 * sizeof(CvPoint2D32f) );
105
106     /* Fill points arrays with values */
107
108     /* cube model with edge size 10 */
109     obj_points[0].x = 0;  obj_points[0].y = 0;  obj_points[0].z = 0;
110     obj_points[1].x = 10; obj_points[1].y = 0;  obj_points[1].z = 0;
111     obj_points[2].x = 10; obj_points[2].y = 10; obj_points[2].z = 0;
112     obj_points[3].x = 0;  obj_points[3].y = 10; obj_points[3].z = 0;
113     obj_points[4].x = 0;  obj_points[4].y = 0;  obj_points[4].z = 10;
114     obj_points[5].x = 10; obj_points[5].y = 0;  obj_points[5].z = 10;
115     obj_points[6].x = 10; obj_points[6].y = 10; obj_points[6].z = 10;
116     obj_points[7].x = 0;  obj_points[7].y = 10; obj_points[7].z = 10;
117
118     /* Loop for test some random object positions */
119     for( counter = start_from; counter < test_case_count; counter++ )
120     {
121         ts->update_context( this, counter, true );
122         
123         /* set all rotation matrix to zero */
124         cvZero( true_rotationX );
125         cvZero( true_rotationY );
126         cvZero( true_rotationZ );
127         
128         /* fill random rotation matrix */
129         angleX = (float)(cvTsRandReal(rng)*2*CV_PI);
130         angleY = (float)(cvTsRandReal(rng)*2*CV_PI);
131         angleZ = (float)(cvTsRandReal(rng)*2*CV_PI);
132
133         true_rotationX->data.fl[0 *3+ 0] = 1;
134         true_rotationX->data.fl[1 *3+ 1] = (float)cos(angleX);
135         true_rotationX->data.fl[2 *3+ 2] = true_rotationX->data.fl[1 *3+ 1];
136         true_rotationX->data.fl[1 *3+ 2] = -(float)sin(angleX);
137         true_rotationX->data.fl[2 *3+ 1] = -true_rotationX->data.fl[1 *3+ 2];
138
139         true_rotationY->data.fl[1 *3+ 1] = 1;
140         true_rotationY->data.fl[0 *3+ 0] = (float)cos(angleY);
141         true_rotationY->data.fl[2 *3+ 2] = true_rotationY->data.fl[0 *3+ 0];
142         true_rotationY->data.fl[0 *3+ 2] = -(float)sin(angleY);
143         true_rotationY->data.fl[2 *3+ 0] = -true_rotationY->data.fl[0 *3+ 2];
144
145         true_rotationZ->data.fl[2 *3+ 2] = 1;
146         true_rotationZ->data.fl[0 *3+ 0] = (float)cos(angleZ);
147         true_rotationZ->data.fl[1 *3+ 1] = true_rotationZ->data.fl[0 *3+ 0];
148         true_rotationZ->data.fl[0 *3+ 1] = -(float)sin(angleZ);
149         true_rotationZ->data.fl[1 *3+ 0] = -true_rotationZ->data.fl[0 *3+ 1];
150
151         cvMatMul( true_rotationX, true_rotationY, tmp_matrix);
152         cvMatMul( tmp_matrix, true_rotationZ, true_rotation);
153
154         /* fill translation vector */
155         true_translation->data.fl[2] = (float)(cvRandReal(rng)*(2*flFocalLength-40) + 40);
156         true_translation->data.fl[0] = (float)((cvRandReal(rng)*2-1)*true_translation->data.fl[2]);
157         true_translation->data.fl[1] = (float)((cvRandReal(rng)*2-1)*true_translation->data.fl[2]);
158
159         /* calculate perspective projection */
160         for ( i = 0; i < 8; i++ )
161         {
162             float vec[3];
163             CvMat Vec = cvMat( 3, 1, CV_MAT32F, vec );
164             CvMat Obj_point = cvMat( 3, 1, CV_MAT32F, &obj_points[i].x );
165
166             cvMatMul( true_rotation, &Obj_point, &Vec );
167
168             vec[0] += true_translation->data.fl[0];
169             vec[1] += true_translation->data.fl[1];
170             vec[2] += true_translation->data.fl[2];
171
172             img_points[i].x = flFocalLength * vec[0] / vec[2];
173             img_points[i].y = flFocalLength * vec[1] / vec[2];
174         }
175
176         /*img_points[0].x = 0 ; img_points[0].y =   0;
177         img_points[1].x = 80; img_points[1].y = -93;
178         img_points[2].x = 245;img_points[2].y =  -77;
179         img_points[3].x = 185;img_points[3].y =  32;
180         img_points[4].x = 32; img_points[4].y = 135;
181         img_points[5].x = 99; img_points[5].y = 35;
182         img_points[6].x = 247; img_points[6].y = 62;
183         img_points[7].x = 195; img_points[7].y = 179;
184         */
185
186         object = cvCreatePOSITObject( obj_points, 8 );
187         cvPOSIT( object, img_points, flFocalLength, criteria,
188                  rotation->data.fl, translation->data.fl );
189         cvReleasePOSITObject( &object );
190
191         code = cvTsCmpEps2( ts, rotation, true_rotation, flEpsilon, false, "rotation matrix" );
192         if( code < 0 )
193             goto _exit_;
194
195         code = cvTsCmpEps2( ts, translation, true_translation, flEpsilon, false, "translation vector" );
196         if( code < 0 )
197             goto _exit_;
198
199         progress = update_progress( progress, counter, test_case_count, 0 );
200     }
201
202 _exit_:
203
204     cvFree( &obj_points );
205     cvFree( &img_points );
206
207     cvReleaseMat( &true_rotationX );
208     cvReleaseMat( &true_rotationY );
209     cvReleaseMat( &true_rotationZ );
210     cvReleaseMat( &tmp_matrix );
211     cvReleaseMat( &true_rotation );
212     cvReleaseMat( &rotation );
213     cvReleaseMat( &translation );
214     cvReleaseMat( &true_translation );
215
216     if( code < 0 )
217         ts->set_failed_test_info( code );
218 }
219
220 CV_POSITTest posit_test;
221
222 /* End of file. */