Update to 2.0.0 tree from current Fremantle build
[opencv] / src / cvaux / cvsubdiv2.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 "_cvaux.h"
43
44 CV_IMPL int
45 icvSubdiv2DCheck( CvSubdiv2D* subdiv )
46 {
47     int i, j, total = subdiv->edges->total;
48     CV_Assert( subdiv != 0 );
49     
50     for( i = 0; i < total; i++ )
51     {
52         CvQuadEdge2D* edge = (CvQuadEdge2D*)cvGetSetElem(subdiv->edges,i);
53         
54         if( edge && CV_IS_SET_ELEM( edge ))
55         {
56             for( j = 0; j < 4; j++ )
57             {
58                 CvSubdiv2DEdge e = (CvSubdiv2DEdge)edge + j;
59                 CvSubdiv2DEdge o_next = cvSubdiv2DNextEdge(e);
60                 CvSubdiv2DEdge o_prev = cvSubdiv2DGetEdge(e, CV_PREV_AROUND_ORG );
61                 CvSubdiv2DEdge d_prev = cvSubdiv2DGetEdge(e, CV_PREV_AROUND_DST );
62                 CvSubdiv2DEdge d_next = cvSubdiv2DGetEdge(e, CV_NEXT_AROUND_DST );
63
64                 // check points
65                 if( cvSubdiv2DEdgeOrg(e) != cvSubdiv2DEdgeOrg(o_next))
66                     return 0;
67                 if( cvSubdiv2DEdgeOrg(e) != cvSubdiv2DEdgeOrg(o_prev))
68                     return 0;
69                 if( cvSubdiv2DEdgeDst(e) != cvSubdiv2DEdgeDst(d_next))
70                     return 0;
71                 if( cvSubdiv2DEdgeDst(e) != cvSubdiv2DEdgeDst(d_prev))
72                     return 0;
73                 if( j % 2 == 0 )
74                 {
75                     if( cvSubdiv2DEdgeDst(o_next) != cvSubdiv2DEdgeOrg(d_prev))
76                         return 0;
77                     if( cvSubdiv2DEdgeDst(o_prev) != cvSubdiv2DEdgeOrg(d_next))
78                         return 0;
79                     if( cvSubdiv2DGetEdge(cvSubdiv2DGetEdge(cvSubdiv2DGetEdge(
80                         e,CV_NEXT_AROUND_LEFT),CV_NEXT_AROUND_LEFT),CV_NEXT_AROUND_LEFT) != e )
81                         return 0;
82                     if( cvSubdiv2DGetEdge(cvSubdiv2DGetEdge(cvSubdiv2DGetEdge(
83                         e,CV_NEXT_AROUND_RIGHT),CV_NEXT_AROUND_RIGHT),CV_NEXT_AROUND_RIGHT) != e)
84                         return 0;
85                 }
86             }
87         }
88     }
89
90     return 1;
91 }
92
93
94
95 static void
96 draw_subdiv_facet( CvSubdiv2D * subdiv, IplImage * dst, IplImage * src, CvSubdiv2DEdge edge )
97 {
98     CvSubdiv2DEdge t = edge;
99     int i, count = 0;
100     CvPoint local_buf[100];
101     CvPoint *buf = local_buf;
102
103     // count number of edges in facet 
104     do
105     {
106         count++;
107         t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
108     }
109     while( t != edge && count < subdiv->quad_edges * 4 );
110
111     if( count * sizeof( buf[0] ) > sizeof( local_buf ))
112     {
113         buf = (CvPoint *) malloc( count * sizeof( buf[0] ));
114     }
115
116     // gather points
117     t = edge;
118     for( i = 0; i < count; i++ )
119     {
120         CvSubdiv2DPoint *pt = cvSubdiv2DEdgeOrg( t );
121
122         if( !pt )
123             break;
124         assert( fabs( pt->pt.x ) < 10000 && fabs( pt->pt.y ) < 10000 );
125         buf[i] = cvPoint( cvRound( pt->pt.x ), cvRound( pt->pt.y ));
126         t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
127     }
128
129     if( i == count )
130     {
131         CvSubdiv2DPoint *pt = cvSubdiv2DEdgeDst( cvSubdiv2DRotateEdge( edge, 1 ));
132         CvPoint ip = cvPoint( cvRound( pt->pt.x ), cvRound( pt->pt.y ));
133         CvScalar color = {{0,0,0,0}};
134
135         //printf("count = %d, (%d,%d)\n", ip.x, ip.y );
136
137         if( 0 <= ip.x && ip.x < src->width && 0 <= ip.y && ip.y < src->height )
138         {
139             uchar *ptr = (uchar*)(src->imageData + ip.y * src->widthStep + ip.x * 3);
140             color = CV_RGB( ptr[2], ptr[1], ptr[0] );
141         }
142
143         cvFillConvexPoly( dst, buf, count, color );
144         //draw_subdiv_point( dst, pt->pt, CV_RGB(0,0,0));
145     }
146
147     if( buf != local_buf )
148         free( buf );
149 }
150
151
152 CV_IMPL void
153 icvDrawMosaic( CvSubdiv2D * subdiv, IplImage * src, IplImage * dst )
154 {
155     int i, total = subdiv->edges->total;
156
157     cvCalcSubdivVoronoi2D( subdiv );
158
159     //icvSet( dst, 255 );
160     for( i = 0; i < total; i++ )
161     {
162         CvQuadEdge2D *edge = (CvQuadEdge2D *) cvGetSetElem( subdiv->edges, i );
163
164         if( edge && CV_IS_SET_ELEM( edge ))
165         {
166             CvSubdiv2DEdge e = (CvSubdiv2DEdge) edge;
167
168             // left
169             draw_subdiv_facet( subdiv, dst, src, cvSubdiv2DRotateEdge( e, 1 ));
170             // right
171             draw_subdiv_facet( subdiv, dst, src, cvSubdiv2DRotateEdge( e, 3 ));
172         }
173     }
174 }
175
176 /* End of file. */