2 #pragma package <opencv>
5 #define CV_NO_BACKWARD_COMPATIBILITY
13 /* the script demostrates iterative construction of
14 delaunay triangulation and voronoi tesselation */
16 CvSubdiv2D* init_delaunay( CvMemStorage* storage,
21 subdiv = cvCreateSubdiv2D( CV_SEQ_KIND_SUBDIV2D, sizeof(*subdiv),
22 sizeof(CvSubdiv2DPoint),
25 cvInitSubdivDelaunay2D( subdiv, rect );
31 void draw_subdiv_point( IplImage* img, CvPoint2D32f fp, CvScalar color )
33 cvCircle( img, cvPoint(cvRound(fp.x), cvRound(fp.y)), 3, color, CV_FILLED, 8, 0 );
37 void draw_subdiv_edge( IplImage* img, CvSubdiv2DEdge edge, CvScalar color )
39 CvSubdiv2DPoint* org_pt;
40 CvSubdiv2DPoint* dst_pt;
45 org_pt = cvSubdiv2DEdgeOrg(edge);
46 dst_pt = cvSubdiv2DEdgeDst(edge);
48 if( org_pt && dst_pt )
53 iorg = cvPoint( cvRound( org.x ), cvRound( org.y ));
54 idst = cvPoint( cvRound( dst.x ), cvRound( dst.y ));
56 cvLine( img, iorg, idst, color, 1, CV_AA, 0 );
61 void draw_subdiv( IplImage* img, CvSubdiv2D* subdiv,
62 CvScalar delaunay_color, CvScalar voronoi_color )
65 int i, total = subdiv->edges->total;
66 int elem_size = subdiv->edges->elem_size;
68 cvStartReadSeq( (CvSeq*)(subdiv->edges), &reader, 0 );
70 for( i = 0; i < total; i++ )
72 CvQuadEdge2D* edge = (CvQuadEdge2D*)(reader.ptr);
74 if( CV_IS_SET_ELEM( edge ))
76 draw_subdiv_edge( img, (CvSubdiv2DEdge)edge + 1, voronoi_color );
77 draw_subdiv_edge( img, (CvSubdiv2DEdge)edge, delaunay_color );
80 CV_NEXT_SEQ_ELEM( elem_size, reader );
85 void locate_point( CvSubdiv2D* subdiv, CvPoint2D32f fp, IplImage* img,
86 CvScalar active_color )
89 CvSubdiv2DEdge e0 = 0;
90 CvSubdiv2DPoint* p = 0;
92 cvSubdiv2DLocate( subdiv, fp, &e0, &p );
99 draw_subdiv_edge( img, e, active_color );
100 e = cvSubdiv2DGetEdge(e,CV_NEXT_AROUND_LEFT);
105 draw_subdiv_point( img, fp, active_color );
109 void draw_subdiv_facet( IplImage* img, CvSubdiv2DEdge edge )
111 CvSubdiv2DEdge t = edge;
115 // count number of edges in facet
119 t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
120 } while (t != edge );
122 buf = (CvPoint*)malloc( count * sizeof(buf[0]));
126 for( i = 0; i < count; i++ )
128 CvSubdiv2DPoint* pt = cvSubdiv2DEdgeOrg( t );
130 buf[i] = cvPoint( cvRound(pt->pt.x), cvRound(pt->pt.y));
131 t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
136 CvSubdiv2DPoint* pt = cvSubdiv2DEdgeDst( cvSubdiv2DRotateEdge( edge, 1 ));
137 cvFillConvexPoly( img, buf, count, CV_RGB(rand()&255,rand()&255,rand()&255), CV_AA, 0 );
138 cvPolyLine( img, &buf, &count, 1, 1, CV_RGB(0,0,0), 1, CV_AA, 0);
139 draw_subdiv_point( img, pt->pt, CV_RGB(0,0,0));
144 void paint_voronoi( CvSubdiv2D* subdiv, IplImage* img )
147 int i, total = subdiv->edges->total;
148 int elem_size = subdiv->edges->elem_size;
150 cvCalcSubdivVoronoi2D( subdiv );
152 cvStartReadSeq( (CvSeq*)(subdiv->edges), &reader, 0 );
154 for( i = 0; i < total; i++ )
156 CvQuadEdge2D* edge = (CvQuadEdge2D*)(reader.ptr);
158 if( CV_IS_SET_ELEM( edge ))
160 CvSubdiv2DEdge e = (CvSubdiv2DEdge)edge;
162 draw_subdiv_facet( img, cvSubdiv2DRotateEdge( e, 1 ));
165 draw_subdiv_facet( img, cvSubdiv2DRotateEdge( e, 3 ));
168 CV_NEXT_SEQ_ELEM( elem_size, reader );
175 char win[] = "source";
177 CvRect rect = { 0, 0, 600, 600 };
178 CvMemStorage* storage;
181 CvScalar active_facet_color, delaunay_color, voronoi_color, bkgnd_color;
183 active_facet_color = CV_RGB( 255, 0, 0 );
184 delaunay_color = CV_RGB( 0,0,0);
185 voronoi_color = CV_RGB(0, 180, 0);
186 bkgnd_color = CV_RGB(255,255,255);
188 img = cvCreateImage( cvSize(rect.width,rect.height), 8, 3 );
189 cvSet( img, bkgnd_color, 0 );
191 cvNamedWindow( win, 1 );
193 storage = cvCreateMemStorage(0);
194 subdiv = init_delaunay( storage, rect );
196 printf("Delaunay triangulation will be build now interactively.\n"
197 "To stop the process, press any key\n\n");
199 for( i = 0; i < 200; i++ )
201 CvPoint2D32f fp = cvPoint2D32f( (float)(rand()%(rect.width-10)+5),
202 (float)(rand()%(rect.height-10)+5));
204 locate_point( subdiv, fp, img, active_facet_color );
205 cvShowImage( win, img );
207 if( cvWaitKey( 100 ) >= 0 )
210 cvSubdivDelaunay2DInsert( subdiv, fp );
211 cvCalcSubdivVoronoi2D( subdiv );
212 cvSet( img, bkgnd_color, 0 );
213 draw_subdiv( img, subdiv, delaunay_color, voronoi_color );
214 cvShowImage( win, img );
216 if( cvWaitKey( 100 ) >= 0 )
220 cvSet( img, bkgnd_color, 0 );
221 paint_voronoi( subdiv, img );
222 cvShowImage( win, img );
226 cvReleaseMemStorage( &storage );
227 cvReleaseImage(&img);
228 cvDestroyWindow( win );
231 int main( int argc, char** argv )
241 main( 1, "delaunay.c" );