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