1 #include <opencv2/imgproc/imgproc_c.h>
2 #include <opencv2/highgui/highgui.hpp>
6 /* the script demostrates iterative construction of
7 delaunay triangulation and voronoi tesselation */
9 CvSubdiv2D* init_delaunay( CvMemStorage* storage,
14 subdiv = cvCreateSubdiv2D( CV_SEQ_KIND_SUBDIV2D, sizeof(*subdiv),
15 sizeof(CvSubdiv2DPoint),
18 cvInitSubdivDelaunay2D( subdiv, rect );
24 void draw_subdiv_point( IplImage* img, CvPoint2D32f fp, CvScalar color )
26 cvCircle( img, cvPoint(cvRound(fp.x), cvRound(fp.y)), 3, color, CV_FILLED, 8, 0 );
30 void draw_subdiv_edge( IplImage* img, CvSubdiv2DEdge edge, CvScalar color )
32 CvSubdiv2DPoint* org_pt;
33 CvSubdiv2DPoint* dst_pt;
38 org_pt = cvSubdiv2DEdgeOrg(edge);
39 dst_pt = cvSubdiv2DEdgeDst(edge);
41 if( org_pt && dst_pt )
46 iorg = cvPoint( cvRound( org.x ), cvRound( org.y ));
47 idst = cvPoint( cvRound( dst.x ), cvRound( dst.y ));
49 cvLine( img, iorg, idst, color, 1, CV_AA, 0 );
54 void draw_subdiv( IplImage* img, CvSubdiv2D* subdiv,
55 CvScalar delaunay_color, CvScalar voronoi_color )
58 int i, total = subdiv->edges->total;
59 int elem_size = subdiv->edges->elem_size;
61 cvStartReadSeq( (CvSeq*)(subdiv->edges), &reader, 0 );
63 for( i = 0; i < total; i++ )
65 CvQuadEdge2D* edge = (CvQuadEdge2D*)(reader.ptr);
67 if( CV_IS_SET_ELEM( edge ))
69 draw_subdiv_edge( img, (CvSubdiv2DEdge)edge + 1, voronoi_color );
70 draw_subdiv_edge( img, (CvSubdiv2DEdge)edge, delaunay_color );
73 CV_NEXT_SEQ_ELEM( elem_size, reader );
78 void locate_point( CvSubdiv2D* subdiv, CvPoint2D32f fp, IplImage* img,
79 CvScalar active_color )
82 CvSubdiv2DEdge e0 = 0;
83 CvSubdiv2DPoint* p = 0;
85 cvSubdiv2DLocate( subdiv, fp, &e0, &p );
92 draw_subdiv_edge( img, e, active_color );
93 e = cvSubdiv2DGetEdge(e,CV_NEXT_AROUND_LEFT);
98 draw_subdiv_point( img, fp, active_color );
102 void draw_subdiv_facet( IplImage* img, CvSubdiv2DEdge edge )
104 CvSubdiv2DEdge t = edge;
108 // count number of edges in facet
112 t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
113 } while (t != edge );
115 buf = (CvPoint*)malloc( count * sizeof(buf[0]));
119 for( i = 0; i < count; i++ )
121 CvSubdiv2DPoint* pt = cvSubdiv2DEdgeOrg( t );
123 buf[i] = cvPoint( cvRound(pt->pt.x), cvRound(pt->pt.y));
124 t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
129 CvSubdiv2DPoint* pt = cvSubdiv2DEdgeDst( cvSubdiv2DRotateEdge( edge, 1 ));
130 cvFillConvexPoly( img, buf, count, CV_RGB(rand()&255,rand()&255,rand()&255), CV_AA, 0 );
131 cvPolyLine( img, &buf, &count, 1, 1, CV_RGB(0,0,0), 1, CV_AA, 0);
132 draw_subdiv_point( img, pt->pt, CV_RGB(0,0,0));
137 void paint_voronoi( CvSubdiv2D* subdiv, IplImage* img )
140 int i, total = subdiv->edges->total;
141 int elem_size = subdiv->edges->elem_size;
143 cvCalcSubdivVoronoi2D( subdiv );
145 cvStartReadSeq( (CvSeq*)(subdiv->edges), &reader, 0 );
147 for( i = 0; i < total; i++ )
149 CvQuadEdge2D* edge = (CvQuadEdge2D*)(reader.ptr);
151 if( CV_IS_SET_ELEM( edge ))
153 CvSubdiv2DEdge e = (CvSubdiv2DEdge)edge;
155 draw_subdiv_facet( img, cvSubdiv2DRotateEdge( e, 1 ));
158 draw_subdiv_facet( img, cvSubdiv2DRotateEdge( e, 3 ));
161 CV_NEXT_SEQ_ELEM( elem_size, reader );
168 char win[] = "source";
170 CvRect rect = { 0, 0, 600, 600 };
171 CvMemStorage* storage;
174 CvScalar active_facet_color, delaunay_color, voronoi_color, bkgnd_color;
176 active_facet_color = CV_RGB( 255, 0, 0 );
177 delaunay_color = CV_RGB( 0,0,0);
178 voronoi_color = CV_RGB(0, 180, 0);
179 bkgnd_color = CV_RGB(255,255,255);
181 img = cvCreateImage( cvSize(rect.width,rect.height), 8, 3 );
182 cvSet( img, bkgnd_color, 0 );
184 cvNamedWindow( win, 1 );
186 storage = cvCreateMemStorage(0);
187 subdiv = init_delaunay( storage, rect );
189 printf("Delaunay triangulation will be build now interactively.\n"
190 "To stop the process, press any key\n\n");
192 for( i = 0; i < 200; i++ )
194 CvPoint2D32f fp = cvPoint2D32f( (float)(rand()%(rect.width-10)+5),
195 (float)(rand()%(rect.height-10)+5));
197 locate_point( subdiv, fp, img, active_facet_color );
198 cvShowImage( win, img );
200 if( cvWaitKey( 100 ) >= 0 )
203 cvSubdivDelaunay2DInsert( subdiv, fp );
204 cvCalcSubdivVoronoi2D( subdiv );
205 cvSet( img, bkgnd_color, 0 );
206 draw_subdiv( img, subdiv, delaunay_color, voronoi_color );
207 cvShowImage( win, img );
209 if( cvWaitKey( 100 ) >= 0 )
213 cvSet( img, bkgnd_color, 0 );
214 paint_voronoi( subdiv, img );
215 cvShowImage( win, img );
219 cvReleaseMemStorage( &storage );
220 cvReleaseImage(&img);
221 cvDestroyWindow( win );
224 int main( int argc, char** argv )
234 main( 1, "delaunay.c" );