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 cvMaxRect( const CvRect* rect1, const CvRect* rect2 )
52 max_rect.x = a = rect1->x;
57 max_rect.width = a += rect1->width;
60 if( max_rect.width < b )
62 max_rect.width -= max_rect.x;
64 max_rect.y = a = rect1->y;
69 max_rect.height = a += rect1->height;
72 if( max_rect.height < b )
74 max_rect.height -= max_rect.y;
82 return cvRect(0,0,0,0);
87 cvBoxPoints( CvBox2D box, CvPoint2D32f pt[4] )
89 CV_FUNCNAME( "cvBoxPoints" );
93 double angle = box.angle*CV_PI/180.;
94 float a = (float)cos(angle)*0.5f;
95 float b = (float)sin(angle)*0.5f;
98 CV_ERROR( CV_StsNullPtr, "NULL vertex array pointer" );
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;
114 icvIntersectLines( double x1, double dx1, double y1, double dy1,
115 double x2, double dx2, double y2, double dy2, double *t2 )
117 double d = dx1 * dy2 - dx2 * dy1;
122 *t2 = ((x2 - x1) * dy1 - (y2 - y1) * dx1) / d;
130 icvCreateCenterNormalLine( CvSubdiv2DEdge edge, double *_a, double *_b, double *_c )
132 CvPoint2D32f org = cvSubdiv2DEdgeOrg( edge )->pt;
133 CvPoint2D32f dst = cvSubdiv2DEdgeDst( edge )->pt;
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));
146 icvIntersectLines3( double *a0, double *b0, double *c0,
147 double *a1, double *b1, double *c1, CvPoint2D32f * point )
149 double det = a0[0] * b1[0] - a1[0] * b0[0];
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);
159 point->x = point->y = FLT_MAX;
165 cvPointPolygonTest( const CvArr* _contour, CvPoint2D32f pt, int measure_dist )
168 CV_FUNCNAME( "cvCheckPointPolygon" );
174 CvSeq* contour = (CvSeq*)_contour;
176 int i, total, counter = 0;
178 double min_dist_num = FLT_MAX, min_dist_denom = 1;
181 if( !CV_IS_SEQ(contour) )
183 CV_CALL( contour = cvPointSeqFromMat( CV_SEQ_KIND_CURVE + CV_SEQ_FLAG_CLOSED,
184 _contour, &header, &block ));
186 else if( CV_IS_SEQ_POLYGON(contour) )
188 if( contour->header_size == sizeof(CvContour) && !measure_dist )
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 )
196 else if( CV_IS_SEQ_CHAIN(contour) )
198 CV_ERROR( CV_StsBadArg,
199 "Chains are not supported. Convert them to polygonal representation using cvApproxChains()" );
202 CV_ERROR( CV_StsBadArg, "Input contour is neither a valid sequence nor a matrix" );
204 total = contour->total;
205 is_float = CV_SEQ_ELTYPE(contour) == CV_32FC2;
206 cvStartReadSeq( contour, &reader, -1 );
208 if( !is_float && !measure_dist && (ip.x = cvRound(pt.x)) == pt.x && (ip.y = cvRound(pt.y)) == pt.y )
210 // the fastest "pure integer" branch
212 CV_READ_SEQ_ELEM( v, reader );
214 for( i = 0; i < total; i++ )
218 CV_READ_SEQ_ELEM( v, reader );
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 )
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)) )
230 dist = (ip.y - v0.y)*(v.x - v0.x) - (ip.x - v0.x)*(v.y - v0.y);
238 result = counter % 2 == 0 ? -100 : 100;
247 CV_READ_SEQ_ELEM( v, reader );
251 CV_READ_SEQ_ELEM( iv, reader );
252 v = cvPointTo32f( iv );
257 for( i = 0; i < total; i++ )
263 CV_READ_SEQ_ELEM( v, reader );
267 CV_READ_SEQ_ELEM( iv, reader );
268 v = cvPointTo32f( iv );
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 )
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)) )
281 dist = (double)(pt.y - v0.y)*(v.x - v0.x) - (double)(pt.x - v0.x)*(v.y - v0.y);
289 result = counter % 2 == 0 ? -100 : 100;
293 for( i = 0; i < total; i++ )
295 double dx, dy, dx1, dy1, dx2, dy2, dist_num, dist_denom = 1;
300 CV_READ_SEQ_ELEM( v, reader );
304 CV_READ_SEQ_ELEM( iv, reader );
305 v = cvPointTo32f( iv );
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;
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;
318 dist_num = (dy1*dx - dx1*dy);
319 dist_num *= dist_num;
320 dist_denom = dx*dx + dy*dy;
323 if( dist_num*min_dist_denom < min_dist_num*dist_denom )
325 min_dist_num = dist_num;
326 min_dist_denom = dist_denom;
327 if( min_dist_num == 0 )
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 )
336 dist_num = dy1*dx - dx1*dy;
338 dist_num = -dist_num;
339 counter += dist_num > 0;
342 result = sqrt(min_dist_num/min_dist_denom);
343 if( counter % 2 == 0 )