1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
45 class CV_POSITTest : public CvTest
54 CV_POSITTest::CV_POSITTest():
55 CvTest( "posit", "cvPOSIT" )
58 support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
61 void CV_POSITTest::run( int start_from )
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 };
70 float trans[3] = { 0.0f, 0.0f, 40.02637f };
76 CvTermCriteria criteria;
77 CvPoint3D32f* obj_points;
78 CvPoint2D32f* img_points;
79 CvPOSITObject* object;
81 float angleX, angleY, angleZ;
82 CvRNG* rng = ts->get_rng();
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 );
94 const float flFocalLength = 760.f;
95 const float flEpsilon = 0.1f;
98 criteria.type = CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;
99 criteria.epsilon = flEpsilon;
100 criteria.max_iter = 10000;
102 /* Allocating source arrays; */
103 obj_points = (CvPoint3D32f*)cvAlloc( 8 * sizeof(CvPoint3D32f) );
104 img_points = (CvPoint2D32f*)cvAlloc( 8 * sizeof(CvPoint2D32f) );
106 /* Fill points arrays with values */
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;
118 /* Loop for test some random object positions */
119 for( counter = start_from; counter < test_case_count; counter++ )
121 ts->update_context( this, counter, true );
123 /* set all rotation matrix to zero */
124 cvZero( true_rotationX );
125 cvZero( true_rotationY );
126 cvZero( true_rotationZ );
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);
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];
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];
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];
151 cvMatMul( true_rotationX, true_rotationY, tmp_matrix);
152 cvMatMul( tmp_matrix, true_rotationZ, true_rotation);
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]);
159 /* calculate perspective projection */
160 for ( i = 0; i < 8; i++ )
163 CvMat Vec = cvMat( 3, 1, CV_MAT32F, vec );
164 CvMat Obj_point = cvMat( 3, 1, CV_MAT32F, &obj_points[i].x );
166 cvMatMul( true_rotation, &Obj_point, &Vec );
168 vec[0] += true_translation->data.fl[0];
169 vec[1] += true_translation->data.fl[1];
170 vec[2] += true_translation->data.fl[2];
172 img_points[i].x = flFocalLength * vec[0] / vec[2];
173 img_points[i].y = flFocalLength * vec[1] / vec[2];
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;
186 object = cvCreatePOSITObject( obj_points, 8 );
187 cvPOSIT( object, img_points, flFocalLength, criteria,
188 rotation->data.fl, translation->data.fl );
189 cvReleasePOSITObject( &object );
191 code = cvTsCmpEps2( ts, rotation, true_rotation, flEpsilon, false, "rotation matrix" );
195 code = cvTsCmpEps2( ts, translation, true_translation, flEpsilon, false, "translation vector" );
199 progress = update_progress( progress, counter, test_case_count, 0 );
204 cvFree( &obj_points );
205 cvFree( &img_points );
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 );
217 ts->set_failed_test_info( code );
220 CV_POSITTest posit_test;