Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / cv / src / cvgeometry.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 #include "_cv.h"
42
43
44 CV_IMPL CvRect
45 cvMaxRect( const CvRect* rect1, const CvRect* rect2 )
46 {
47     if( rect1 && rect2 )
48     {
49         CvRect max_rect;
50         int a, b;
51
52         max_rect.x = a = rect1->x;
53         b = rect2->x;
54         if( max_rect.x > b )
55             max_rect.x = b;
56
57         max_rect.width = a += rect1->width;
58         b += rect2->width;
59
60         if( max_rect.width < b )
61             max_rect.width = b;
62         max_rect.width -= max_rect.x;
63
64         max_rect.y = a = rect1->y;
65         b = rect2->y;
66         if( max_rect.y > b )
67             max_rect.y = b;
68
69         max_rect.height = a += rect1->height;
70         b += rect2->height;
71
72         if( max_rect.height < b )
73             max_rect.height = b;
74         max_rect.height -= max_rect.y;
75         return max_rect;
76     }
77     else if( rect1 )
78         return *rect1;
79     else if( rect2 )
80         return *rect2;
81     else
82         return cvRect(0,0,0,0);
83 }
84
85
86 CV_IMPL void
87 cvBoxPoints( CvBox2D box, CvPoint2D32f pt[4] )
88 {
89     CV_FUNCNAME( "cvBoxPoints" );
90
91     __BEGIN__;
92     
93     double angle = box.angle*CV_PI/180.;
94     float a = (float)cos(angle)*0.5f;
95     float b = (float)sin(angle)*0.5f;
96
97     if( !pt )
98         CV_ERROR( CV_StsNullPtr, "NULL vertex array pointer" );
99
100     pt[0].x = box.center.x - a*box.size.height - b*box.size.width;
101     pt[0].y = box.center.y + b*box.size.height - a*box.size.width;
102     pt[1].x = box.center.x + a*box.size.height - b*box.size.width;
103     pt[1].y = box.center.y - b*box.size.height - a*box.size.width;
104     pt[2].x = 2*box.center.x - pt[0].x;
105     pt[2].y = 2*box.center.y - pt[0].y;
106     pt[3].x = 2*box.center.x - pt[1].x;
107     pt[3].y = 2*box.center.y - pt[1].y;
108
109     __END__;
110 }
111
112
113 int
114 icvIntersectLines( double x1, double dx1, double y1, double dy1,
115                    double x2, double dx2, double y2, double dy2, double *t2 )
116 {
117     double d = dx1 * dy2 - dx2 * dy1;
118     int result = -1;
119
120     if( d != 0 )
121     {
122         *t2 = ((x2 - x1) * dy1 - (y2 - y1) * dx1) / d;
123         result = 0;
124     }
125     return result;
126 }
127
128
129 void
130 icvCreateCenterNormalLine( CvSubdiv2DEdge edge, double *_a, double *_b, double *_c )
131 {
132     CvPoint2D32f org = cvSubdiv2DEdgeOrg( edge )->pt;
133     CvPoint2D32f dst = cvSubdiv2DEdgeDst( edge )->pt;
134
135     double a = dst.x - org.x;
136     double b = dst.y - org.y;
137     double c = -(a * (dst.x + org.x) + b * (dst.y + org.y));
138
139     *_a = a + a;
140     *_b = b + b;
141     *_c = c;
142 }
143
144
145 void
146 icvIntersectLines3( double *a0, double *b0, double *c0,
147                     double *a1, double *b1, double *c1, CvPoint2D32f * point )
148 {
149     double det = a0[0] * b1[0] - a1[0] * b0[0];
150
151     if( det != 0 )
152     {
153         det = 1. / det;
154         point->x = (float) ((b0[0] * c1[0] - b1[0] * c0[0]) * det);
155         point->y = (float) ((a1[0] * c0[0] - a0[0] * c1[0]) * det);
156     }
157     else
158     {
159         point->x = point->y = FLT_MAX;
160     }
161 }
162
163
164 CV_IMPL double
165 cvPointPolygonTest( const CvArr* _contour, CvPoint2D32f pt, int measure_dist )
166 {
167     double result = 0;
168     CV_FUNCNAME( "cvCheckPointPolygon" );
169
170     __BEGIN__;
171     
172     CvSeqBlock block;
173     CvContour header;
174     CvSeq* contour = (CvSeq*)_contour;
175     CvSeqReader reader;
176     int i, total, counter = 0;
177     int is_float;
178     double min_dist_num = FLT_MAX, min_dist_denom = 1;
179     CvPoint ip = {0,0};
180
181     if( !CV_IS_SEQ(contour) )
182     {
183         CV_CALL( contour = cvPointSeqFromMat( CV_SEQ_KIND_CURVE + CV_SEQ_FLAG_CLOSED,
184                                               _contour, &header, &block ));
185     }
186     else if( CV_IS_SEQ_POLYGON(contour) )
187     {
188         if( contour->header_size == sizeof(CvContour) && !measure_dist )
189         {
190             CvRect r = ((CvContour*)contour)->rect;
191             if( pt.x < r.x || pt.y < r.y ||
192                 pt.x >= r.x + r.width || pt.y >= r.y + r.height )
193                 return -100;
194         }
195     }
196     else if( CV_IS_SEQ_CHAIN(contour) )
197     {
198         CV_ERROR( CV_StsBadArg,
199             "Chains are not supported. Convert them to polygonal representation using cvApproxChains()" );
200     }
201     else
202         CV_ERROR( CV_StsBadArg, "Input contour is neither a valid sequence nor a matrix" );
203
204     total = contour->total;
205     is_float = CV_SEQ_ELTYPE(contour) == CV_32FC2;
206     cvStartReadSeq( contour, &reader, -1 );
207
208     if( !is_float && !measure_dist && (ip.x = cvRound(pt.x)) == pt.x && (ip.y = cvRound(pt.y)) == pt.y )
209     {
210         // the fastest "pure integer" branch
211         CvPoint v0, v;
212         CV_READ_SEQ_ELEM( v, reader );
213
214         for( i = 0; i < total; i++ )
215         {
216             int dist;
217             v0 = v;
218             CV_READ_SEQ_ELEM( v, reader );
219
220             if( v0.y <= ip.y && v.y <= ip.y ||
221                 v0.y > ip.y && v.y > ip.y ||
222                 v0.x < ip.x && v.x < ip.x )
223             {
224                 if( ip.y == v.y && (ip.x == v.x || ip.y == v0.y &&
225                     (v0.x <= ip.x && ip.x <= v.x || v.x <= ip.x && ip.x <= v0.x)) )
226                     EXIT;
227                 continue;
228             }
229
230             dist = (ip.y - v0.y)*(v.x - v0.x) - (ip.x - v0.x)*(v.y - v0.y);
231             if( dist == 0 )
232                 EXIT;
233             if( v.y < v0.y )
234                 dist = -dist;
235             counter += dist > 0;
236         }
237
238         result = counter % 2 == 0 ? -100 : 100;
239     }
240     else
241     {
242         CvPoint2D32f v0, v;
243         CvPoint iv;
244
245         if( is_float )
246         {
247             CV_READ_SEQ_ELEM( v, reader );
248         }
249         else
250         {
251             CV_READ_SEQ_ELEM( iv, reader );
252             v = cvPointTo32f( iv );
253         }
254
255         if( !measure_dist )
256         {
257             for( i = 0; i < total; i++ )
258             {
259                 double dist;
260                 v0 = v;
261                 if( is_float )
262                 {
263                     CV_READ_SEQ_ELEM( v, reader );
264                 }
265                 else
266                 {
267                     CV_READ_SEQ_ELEM( iv, reader );
268                     v = cvPointTo32f( iv );
269                 }
270
271                 if( v0.y <= pt.y && v.y <= pt.y ||
272                     v0.y > pt.y && v.y > pt.y ||
273                     v0.x < pt.x && v.x < pt.x )
274                 {
275                     if( pt.y == v.y && (pt.x == v.x || pt.y == v0.y &&
276                         (v0.x <= pt.x && pt.x <= v.x || v.x <= pt.x && pt.x <= v0.x)) )
277                         EXIT;
278                     continue;
279                 }
280
281                 dist = (double)(pt.y - v0.y)*(v.x - v0.x) - (double)(pt.x - v0.x)*(v.y - v0.y);
282                 if( dist == 0 )
283                     EXIT;
284                 if( v.y < v0.y )
285                     dist = -dist;
286                 counter += dist > 0;
287             }
288             
289             result = counter % 2 == 0 ? -100 : 100;
290         }
291         else
292         {
293             for( i = 0; i < total; i++ )
294             {
295                 double dx, dy, dx1, dy1, dx2, dy2, dist_num, dist_denom = 1;
296         
297                 v0 = v;
298                 if( is_float )
299                 {
300                     CV_READ_SEQ_ELEM( v, reader );
301                 }
302                 else
303                 {
304                     CV_READ_SEQ_ELEM( iv, reader );
305                     v = cvPointTo32f( iv );
306                 }
307         
308                 dx = v.x - v0.x; dy = v.y - v0.y;
309                 dx1 = pt.x - v0.x; dy1 = pt.y - v0.y;
310                 dx2 = pt.x - v.x; dy2 = pt.y - v.y;
311         
312                 if( dx1*dx + dy1*dy <= 0 )
313                     dist_num = dx1*dx1 + dy1*dy1;
314                 else if( dx2*dx + dy2*dy >= 0 )
315                     dist_num = dx2*dx2 + dy2*dy2;
316                 else
317                 {
318                     dist_num = (dy1*dx - dx1*dy);
319                     dist_num *= dist_num;
320                     dist_denom = dx*dx + dy*dy;
321                 }
322
323                 if( dist_num*min_dist_denom < min_dist_num*dist_denom )
324                 {
325                     min_dist_num = dist_num;
326                     min_dist_denom = dist_denom;
327                     if( min_dist_num == 0 )
328                         break;
329                 }
330
331                 if( v0.y <= pt.y && v.y <= pt.y ||
332                     v0.y > pt.y && v.y > pt.y ||
333                     v0.x < pt.x && v.x < pt.x )
334                     continue;
335
336                 dist_num = dy1*dx - dx1*dy;
337                 if( dy < 0 )
338                     dist_num = -dist_num;
339                 counter += dist_num > 0;
340             }
341
342             result = sqrt(min_dist_num/min_dist_denom);
343             if( counter % 2 == 0 )
344                 result = -result;
345         }
346     }
347
348     __END__;
349
350     return result;
351 }
352
353
354 #define CV_VERYSMALLDOUBLE 1.0e-10
355
356 CV_IMPL void
357 cvRQDecomp3x3( const CvMat *matrixM, CvMat *matrixR, CvMat *matrixQ,
358                CvMat *matrixQx, CvMat *matrixQy, CvMat *matrixQz,
359                CvPoint3D64f *eulerAngles)
360 {
361         CvMat *tmpMatrix1 = 0;
362         CvMat *tmpMatrix2 = 0;
363         CvMat *tmpMatrixM = 0;
364         CvMat *tmpMatrixR = 0;
365         CvMat *tmpMatrixQ = 0;
366         CvMat *tmpMatrixQx = 0;
367         CvMat *tmpMatrixQy = 0;
368         CvMat *tmpMatrixQz = 0;
369         double tmpEulerAngleX, tmpEulerAngleY, tmpEulerAngleZ;
370         
371         CV_FUNCNAME("cvRQDecomp3x3");
372     __BEGIN__;
373         
374         /* Validate parameters. */
375         if(matrixM == 0 || matrixR == 0 || matrixQ == 0)
376                 CV_ERROR(CV_StsNullPtr, "Some of parameters is a NULL pointer!");
377         
378         if(!CV_IS_MAT(matrixM) || !CV_IS_MAT(matrixR) || !CV_IS_MAT(matrixQ))
379                 CV_ERROR(CV_StsUnsupportedFormat, "Input parameters must be a matrices!");
380         
381         if(matrixM->cols != 3 || matrixM->rows != 3 || matrixR->cols != 3 || matrixR->rows != 3 || matrixQ->cols != 3 || matrixQ->rows != 3)
382                 CV_ERROR(CV_StsUnmatchedSizes, "Size of matrices must be 3x3!");
383         
384         CV_CALL(tmpMatrix1 = cvCreateMat(3, 3, CV_64F));
385         CV_CALL(tmpMatrix2 = cvCreateMat(3, 3, CV_64F));
386         CV_CALL(tmpMatrixM = cvCreateMat(3, 3, CV_64F));
387         CV_CALL(tmpMatrixR = cvCreateMat(3, 3, CV_64F));
388         CV_CALL(tmpMatrixQ = cvCreateMat(3, 3, CV_64F));
389         CV_CALL(tmpMatrixQx = cvCreateMat(3, 3, CV_64F));
390         CV_CALL(tmpMatrixQy = cvCreateMat(3, 3, CV_64F));
391         CV_CALL(tmpMatrixQz = cvCreateMat(3, 3, CV_64F));
392         
393         cvCopy(matrixM, tmpMatrixM);
394         
395         /* Find Givens rotation Q_x for x axis. */
396         /*
397               ( 1  0  0 )
398          Qx = ( 0  c -s ), cos = -m33/sqrt(m32^2 + m33^2), cos = m32/sqrt(m32^2 + m33^2)
399                   ( 0  s  c )
400          */
401         
402         double x, y, z, c, s;
403         x = cvmGet(tmpMatrixM, 2, 1);
404         y = cvmGet(tmpMatrixM, 2, 2);
405         z = x * x + y * y;
406         assert(z != 0); // Prevent division by zero.
407         c = -y / sqrt(z);
408         s = x / sqrt(z);
409         
410         cvSetIdentity(tmpMatrixQx);
411         cvmSet(tmpMatrixQx, 1, 1, c);
412         cvmSet(tmpMatrixQx, 1, 2, -s);
413         cvmSet(tmpMatrixQx, 2, 1, s);
414         cvmSet(tmpMatrixQx, 2, 2, c);
415         
416         tmpEulerAngleX = acos(c) * 180.0 / CV_PI;
417         
418         /* Multiply M on the right by Q_x. */
419         
420         cvMatMul(tmpMatrixM, tmpMatrixQx, tmpMatrixR);
421         cvCopy(tmpMatrixR, tmpMatrixM);
422         
423         assert(cvmGet(tmpMatrixM, 2, 1) < CV_VERYSMALLDOUBLE && cvmGet(tmpMatrixM, 2, 1) > -CV_VERYSMALLDOUBLE); // Should actually be zero.
424         
425         if(cvmGet(tmpMatrixM, 2, 1) != 0.0)
426                 cvmSet(tmpMatrixM, 2, 1, 0.0); // Rectify arithmetic precision error.
427         
428         /* Find Givens rotation for y axis. */
429         /*
430               ( c  0  s )
431          Qy = ( 0  1  0 ), cos = m33/sqrt(m31^2 + m33^2), cos = m31/sqrt(m31^2 + m33^2)
432               (-s  0  c )
433          */
434         
435         x = cvmGet(tmpMatrixM, 2, 0);
436         y = cvmGet(tmpMatrixM, 2, 2);
437         z = x * x + y * y;
438         assert(z != 0); // Prevent division by zero.
439         c = y / sqrt(z);
440         s = x / sqrt(z);
441         
442         cvSetIdentity(tmpMatrixQy);
443         cvmSet(tmpMatrixQy, 0, 0, c);
444         cvmSet(tmpMatrixQy, 0, 2, s);
445         cvmSet(tmpMatrixQy, 2, 0, -s);
446         cvmSet(tmpMatrixQy, 2, 2, c);
447         
448         tmpEulerAngleY = acos(c) * 180.0 / CV_PI;
449         
450         /* Multiply M*Q_x on the right by Q_y. */
451         
452         cvMatMul(tmpMatrixM, tmpMatrixQy, tmpMatrixR);
453         cvCopy(tmpMatrixR, tmpMatrixM);
454         
455         assert(cvmGet(tmpMatrixM, 2, 0) < CV_VERYSMALLDOUBLE && cvmGet(tmpMatrixM, 2, 0) > -CV_VERYSMALLDOUBLE); // Should actually be zero.
456         
457         if(cvmGet(tmpMatrixM, 2, 0) != 0.0)
458                 cvmSet(tmpMatrixM, 2, 0, 0.0); // Rectify arithmetic precision error.
459         
460         /* Find Givens rotation for z axis. */
461         /*
462               ( c -s  0 )
463          Qz = ( s  c  0 ), cos = -m22/sqrt(m21^2 + m22^2), cos = m21/sqrt(m21^2 + m22^2)
464               ( 0  0  1 )
465          */
466         
467         x = cvmGet(tmpMatrixM, 1, 0);
468         y = cvmGet(tmpMatrixM, 1, 1);
469         z = x * x + y * y;
470         assert(z != 0); // Prevent division by zero.
471         c = -y / sqrt(z);
472         s = x / sqrt(z);
473         
474         cvSetIdentity(tmpMatrixQz);
475         cvmSet(tmpMatrixQz, 0, 0, c);
476         cvmSet(tmpMatrixQz, 0, 1, -s);
477         cvmSet(tmpMatrixQz, 1, 0, s);
478         cvmSet(tmpMatrixQz, 1, 1, c);
479         
480         tmpEulerAngleZ = acos(c) * 180.0 / CV_PI;
481         
482         /* Multiply M*Q_x*Q_y on the right by Q_z. */
483         
484         cvMatMul(tmpMatrixM, tmpMatrixQz, tmpMatrixR);
485         
486         assert(cvmGet(tmpMatrixR, 1, 0) < CV_VERYSMALLDOUBLE && cvmGet(tmpMatrixR, 1, 0) > -CV_VERYSMALLDOUBLE); // Should actually be zero.
487         
488         if(cvmGet(tmpMatrixR, 1, 0) != 0.0)
489                 cvmSet(tmpMatrixR, 1, 0, 0.0); // Rectify arithmetic precision error.
490         
491         /* Calulate orthogonal matrix. */
492         /*
493          Q = QzT * QyT * QxT
494          */
495         
496         cvTranspose(tmpMatrixQz, tmpMatrix1);
497         cvTranspose(tmpMatrixQy, tmpMatrix2);
498         cvMatMul(tmpMatrix1, tmpMatrix2, tmpMatrixQ);
499         cvCopy(tmpMatrixQ, tmpMatrix1);
500         cvTranspose(tmpMatrixQx, tmpMatrix2);
501         cvMatMul(tmpMatrix1, tmpMatrix2, tmpMatrixQ);
502         
503         /* Solve decomposition ambiguity. */
504         /*
505          Diagonal entries of R should be positive, so swap signs if necessary.
506          */
507         
508         if(cvmGet(tmpMatrixR, 0, 0) < 0.0) {
509                 cvmSet(tmpMatrixR, 0, 0, -1.0 * cvmGet(tmpMatrixR, 0, 0));
510                 cvmSet(tmpMatrixQ, 0, 0, -1.0 * cvmGet(tmpMatrixQ, 0, 0));
511                 cvmSet(tmpMatrixQ, 0, 1, -1.0 * cvmGet(tmpMatrixQ, 0, 1));
512                 cvmSet(tmpMatrixQ, 0, 2, -1.0 * cvmGet(tmpMatrixQ, 0, 2));
513         }
514         if(cvmGet(tmpMatrixR, 1, 1) < 0.0) {
515                 cvmSet(tmpMatrixR, 0, 1, -1.0 * cvmGet(tmpMatrixR, 0, 1));
516                 cvmSet(tmpMatrixR, 1, 1, -1.0 * cvmGet(tmpMatrixR, 1, 1));
517                 cvmSet(tmpMatrixQ, 1, 0, -1.0 * cvmGet(tmpMatrixQ, 1, 0));
518                 cvmSet(tmpMatrixQ, 1, 1, -1.0 * cvmGet(tmpMatrixQ, 1, 1));
519                 cvmSet(tmpMatrixQ, 1, 2, -1.0 * cvmGet(tmpMatrixQ, 1, 2));
520         }
521         
522         /* Save R and Q matrices. */
523         
524         cvCopy(tmpMatrixR, matrixR);
525         cvCopy(tmpMatrixQ, matrixQ);
526         
527         if(matrixQx && CV_IS_MAT(matrixQx) && matrixQx->cols == 3 || matrixQx->rows == 3)
528                 cvCopy(tmpMatrixQx, matrixQx);
529         if(matrixQy && CV_IS_MAT(matrixQy) && matrixQy->cols == 3 || matrixQy->rows == 3)
530                 cvCopy(tmpMatrixQy, matrixQy);
531         if(matrixQz && CV_IS_MAT(matrixQz) && matrixQz->cols == 3 || matrixQz->rows == 3)
532                 cvCopy(tmpMatrixQz, matrixQz);
533         
534         /* Save Euler angles. */
535         
536         if(eulerAngles)
537                 *eulerAngles = cvPoint3D64f(tmpEulerAngleX, tmpEulerAngleY, tmpEulerAngleZ);
538         
539         __END__;
540         
541         cvReleaseMat(&tmpMatrix1);
542         cvReleaseMat(&tmpMatrix2);
543         cvReleaseMat(&tmpMatrixM);
544         cvReleaseMat(&tmpMatrixR);
545         cvReleaseMat(&tmpMatrixQ);
546         cvReleaseMat(&tmpMatrixQx);
547         cvReleaseMat(&tmpMatrixQy);
548         cvReleaseMat(&tmpMatrixQz);
549
550 }
551
552
553 CV_IMPL void
554 cvDecomposeProjectionMatrix( const CvMat *projMatr, CvMat *calibMatr,
555                              CvMat *rotMatr, CvMat *posVect,
556                              CvMat *rotMatrX, CvMat *rotMatrY,
557                              CvMat *rotMatrZ, CvPoint3D64f *eulerAngles)
558 {
559         CvMat *tmpProjMatr = 0;
560         CvMat *tmpMatrixD = 0;
561         CvMat *tmpMatrixV = 0;
562         CvMat *tmpMatrixM = 0;
563         
564         CV_FUNCNAME("cvDecomposeProjectionMatrix");
565     __BEGIN__;
566         
567         /* Validate parameters. */
568         if(projMatr == 0 || calibMatr == 0 || rotMatr == 0 || posVect == 0)
569                 CV_ERROR(CV_StsNullPtr, "Some of parameters is a NULL pointer!");
570         
571         if(!CV_IS_MAT(projMatr) || !CV_IS_MAT(calibMatr) || !CV_IS_MAT(rotMatr) || !CV_IS_MAT(posVect))
572                 CV_ERROR(CV_StsUnsupportedFormat, "Input parameters must be a matrices!");
573         
574         if(projMatr->cols != 4 || projMatr->rows != 3)
575                 CV_ERROR(CV_StsUnmatchedSizes, "Size of projection matrix must be 3x4!");
576         
577         if(calibMatr->cols != 3 || calibMatr->rows != 3 || rotMatr->cols != 3 || rotMatr->rows != 3)
578                 CV_ERROR(CV_StsUnmatchedSizes, "Size of calibration and rotation matrices must be 3x3!");
579         
580         if(posVect->cols != 1 || posVect->rows != 4)
581                 CV_ERROR(CV_StsUnmatchedSizes, "Size of position vector must be 4x1!");
582         
583         CV_CALL(tmpProjMatr = cvCreateMat(4, 4, CV_64F));
584         CV_CALL(tmpMatrixD = cvCreateMat(4, 4, CV_64F));
585         CV_CALL(tmpMatrixV = cvCreateMat(4, 4, CV_64F));
586         CV_CALL(tmpMatrixM = cvCreateMat(3, 3, CV_64F));
587         
588         /* Compute position vector. */
589         
590         cvSetZero(tmpProjMatr); // Add zero row to make matrix square.
591         int i, k;
592         for(i = 0; i < 3; i++)
593                 for(k = 0; k < 4; k++)
594                         cvmSet(tmpProjMatr, i, k, cvmGet(projMatr, i, k));
595         
596         CV_CALL(cvSVD(tmpProjMatr, tmpMatrixD, NULL, tmpMatrixV, CV_SVD_MODIFY_A + CV_SVD_V_T));
597         
598         /* Save position vector. */
599         
600         for(i = 0; i < 4; i++)
601                 cvmSet(posVect, i, 0, cvmGet(tmpMatrixV, 3, i)); // Solution is last row of V.
602         
603         /* Compute calibration and rotation matrices via RQ decomposition. */
604         
605         cvGetCols(projMatr, tmpMatrixM, 0, 3); // M is first square matrix of P.
606         
607         assert(cvDet(tmpMatrixM) != 0.0); // So far only finite cameras could be decomposed, so M has to be nonsingular [det(M) != 0].
608         
609         CV_CALL(cvRQDecomp3x3(tmpMatrixM, calibMatr, rotMatr, rotMatrX, rotMatrY, rotMatrZ, eulerAngles));
610         
611         __END__;
612         
613         cvReleaseMat(&tmpProjMatr);
614         cvReleaseMat(&tmpMatrixD);
615         cvReleaseMat(&tmpMatrixV);
616         cvReleaseMat(&tmpMatrixM);
617         
618 }
619
620 /* End of file. */