]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/doc/cv_planar_subdivisions.tex
4a4ea3374c0e9b87f93ec549f339dd0046281c04
[opencv.git] / opencv / doc / cv_planar_subdivisions.tex
1 \section{Planar Subdivisions}
2
3 \ifCPy
4
5 \cvfunc{CvSubdiv2D}\label{CvSubdiv2D}
6
7 Planar subdivision.
8
9 \begin{lstlisting}
10 #define CV_SUBDIV2D_FIELDS()    \
11     CV_GRAPH_FIELDS()           \
12     int  quad_edges;            \
13     int  is_geometry_valid;     \
14     CvSubdiv2DEdge recent_edge; \
15     CvPoint2D32f  topleft;      \
16     CvPoint2D32f  bottomright;
17
18 typedef struct CvSubdiv2D
19 {
20     CV_SUBDIV2D_FIELDS()
21 }
22 CvSubdiv2D;
23 \end{lstlisting}
24
25 Planar subdivision is the subdivision of a plane into a set of
26 non-overlapped regions (facets) that cover the whole plane. The above
27 structure describes a subdivision built on a 2d point set, where the points
28 are linked together and form a planar graph, which, together with a few
29 edges connecting the exterior subdivision points (namely, convex hull points)
30 with infinity, subdivides a plane into facets by its edges.
31
32 For every subdivision there exists a dual subdivision in which facets and
33 points (subdivision vertices) swap their roles, that is, a facet is
34 treated as a vertex (called a virtual point below) of the dual subdivision and
35 the original subdivision vertices become facets. On the picture below
36 original subdivision is marked with solid lines and dual subdivision
37 with dotted lines.
38
39 \includegraphics[width=0.5\textwidth]{pics/subdiv.png}
40
41 OpenCV subdivides a plane into triangles using Delaunay's
42 algorithm. Subdivision is built iteratively starting from a dummy
43 triangle that includes all the subdivision points for sure. In this
44 case the dual subdivision is a Voronoi diagram of the input 2d point set. The
45 subdivisions can be used for the 3d piece-wise transformation of a plane,
46 morphing, fast location of points on the plane, building special graphs
47 (such as NNG,RNG) and so forth.
48
49 \cvfunc{CvQuadEdge2D}\label{CvQuadEdge2D}
50
51 Quad-edge of planar subdivision.
52
53 \begin{lstlisting}
54 /* one of edges within quad-edge, lower 2 bits is index (0..3)
55    and upper bits are quad-edge pointer */
56 typedef long CvSubdiv2DEdge;
57
58 /* quad-edge structure fields */
59 #define CV_QUADEDGE2D_FIELDS()     \
60     int flags;                     \
61     struct CvSubdiv2DPoint* pt[4]; \
62     CvSubdiv2DEdge  next[4];
63
64 typedef struct CvQuadEdge2D
65 {
66     CV_QUADEDGE2D_FIELDS()
67 }
68 CvQuadEdge2D;
69
70 \end{lstlisting}
71
72 Quad-edge is a basic element of subdivision containing four edges (e, eRot, reversed e and reversed eRot):
73
74 \includegraphics[width=0.5\textwidth]{pics/quadedge.png}
75
76 \cvfunc{CvSubdiv2DPoint}\label{CvSubdiv2DPoint}
77
78 Point of original or dual subdivision.
79
80 \begin{lstlisting}
81 #define CV_SUBDIV2D_POINT_FIELDS()\
82     int            flags;      \
83     CvSubdiv2DEdge first;      \
84     CvPoint2D32f   pt;         \
85     int id;
86
87 #define CV_SUBDIV2D_VIRTUAL_POINT_FLAG (1 << 30)
88
89 typedef struct CvSubdiv2DPoint
90 {
91     CV_SUBDIV2D_POINT_FIELDS()
92 }
93 CvSubdiv2DPoint;
94 \end{lstlisting}
95
96 \begin{itemize}
97 \item[id] This integer can be used to index auxillary data associated with each vertex of the planar subdivision
98 \end{itemize}
99
100 \cvCPyFunc{CalcSubdivVoronoi2D}
101 Calculates the coordinates of Voronoi diagram cells.
102
103 \cvdefC{
104 void cvCalcSubdivVoronoi2D( \par CvSubdiv2D* subdiv );
105 }
106 \cvdefPy{CalcSubdivVoronoi2D(subdiv)-> None}
107
108 \begin{description}
109 \cvarg{subdiv}{Delaunay subdivision, in which all the points are already added}
110 \end{description}
111
112 The function calculates the coordinates
113 of virtual points. All virtual points corresponding to some vertex of the
114 original subdivision form (when connected together) a boundary of the Voronoi
115 cell at that point.
116
117 \cvCPyFunc{ClearSubdivVoronoi2D}
118 Removes all virtual points.
119
120 \cvdefC{
121 void cvClearSubdivVoronoi2D( CvSubdiv2D* subdiv );
122 }\cvdefPy{ClearSubdivVoronoi2D(subdiv)-> None}
123
124 \begin{description}
125 \cvarg{subdiv}{Delaunay subdivision}
126 \end{description}
127
128 The function removes all of the virtual points. It
129 is called internally in \cvCPyCross{CalcSubdivVoronoi2D} if the subdivision
130 was modified after previous call to the function.
131
132
133 \cvCPyFunc{CreateSubdivDelaunay2D}
134 Creates an empty Delaunay triangulation.
135
136 \cvdefC{
137 CvSubdiv2D* cvCreateSubdivDelaunay2D( \par CvRect rect,\par CvMemStorage* storage );
138 }\cvdefPy{CreateSubdivDelaunay2D(rect,storage)-> delaunay\_triangulation}
139
140 \begin{description}
141 \cvarg{rect}{Rectangle that includes all of the 2d points that are to be added to the subdivision}
142 \cvarg{storage}{Container for subdivision}
143 \end{description}
144
145 The function creates an empty Delaunay
146 subdivision, where 2d points can be added using the function
147 \cvCPyCross{SubdivDelaunay2DInsert}. All of the points to be added must be within
148 the specified rectangle, otherwise a runtime error will be raised.
149
150 Note that the triangulation is a single large triangle that covers the given rectangle.  Hence the three vertices of this triangle are outside the rectangle \texttt{rect}.
151
152 \cvCPyFunc{FindNearestPoint2D}
153 Finds the closest subdivision vertex to the given point.
154
155 \cvdefC{
156 CvSubdiv2DPoint* cvFindNearestPoint2D( \par CvSubdiv2D* subdiv,\par CvPoint2D32f pt );
157 }\cvdefPy{FindNearestPoint2D(subdiv,pt)-> point}
158
159 \begin{description}
160 \cvarg{subdiv}{Delaunay or another subdivision}
161 \cvarg{pt}{Input point}
162 \end{description}
163
164 The function is another function that
165 locates the input point within the subdivision. It finds the subdivision vertex that
166 is the closest to the input point. It is not necessarily one of vertices
167 of the facet containing the input point, though the facet (located using
168 \cvCPyCross{Subdiv2DLocate}) is used as a starting
169 point. The function returns a pointer to the found subdivision vertex.
170
171 \cvCPyFunc{Subdiv2DEdgeDst}
172 Returns the edge destination.
173
174 \cvdefC{
175 CvSubdiv2DPoint* cvSubdiv2DEdgeDst( \par CvSubdiv2DEdge edge );
176 }
177 \cvdefPy{Subdiv2DEdgeDst(edge)-> point}
178
179 \begin{description}
180 \cvarg{edge}{Subdivision edge (not a quad-edge)}
181 \end{description}
182
183 The function returns the edge destination. The
184 returned pointer may be NULL if the edge is from dual subdivision and
185 the virtual point coordinates are not calculated yet. The virtual points
186 can be calculated using the function \cvCPyCross{CalcSubdivVoronoi2D}.
187
188 \cvCPyFunc{Subdiv2DEdgeOrg}
189 Returns the edge origin.
190
191 \cvdefC{
192 CvSubdiv2DPoint* cvSubdiv2DEdgeOrg( \par CvSubdiv2DEdge edge );
193 }\cvdefPy{Subdiv2DEdgeOrg(edge)-> point}
194
195 \begin{description}
196 \cvarg{edge}{Subdivision edge (not a quad-edge)}
197 \end{description}
198
199 The function returns the edge
200 origin. The returned pointer may be NULL if the edge is from dual
201 subdivision and the virtual point coordinates are not calculated
202 yet. The virtual points can be calculated using the function
203 \cvCPyCross{CalcSubdivVoronoi2D}.
204
205 \cvCPyFunc{Subdiv2DGetEdge}
206 Returns one of the edges related to the given edge.
207
208 \cvdefC{
209 CvSubdiv2DEdge  cvSubdiv2DGetEdge( CvSubdiv2DEdge edge, CvNextEdgeType type );
210
211
212 }\cvdefPy{Subdiv2DGetEdge(edge,type)-> CvSubdiv2DEdge}
213 \begin{lstlisting}
214 #define cvSubdiv2DNextEdge( edge ) cvSubdiv2DGetEdge( edge, CV_NEXT_AROUND_ORG )
215 \end{lstlisting}
216
217 \begin{description}
218 \cvarg{edge}{Subdivision edge (not a quad-edge)}
219 \cvarg{type}{Specifies which of the related edges to return, one of the following:}
220 \begin{description}
221   \cvarg{CV\_NEXT\_AROUND\_ORG}{next around the edge origin (\texttt{eOnext} on the picture above if \texttt{e} is the input edge)}
222   \cvarg{CV\_NEXT\_AROUND\_DST}{next around the edge vertex (\texttt{eDnext})}
223   \cvarg{CV\_PREV\_AROUND\_ORG}{previous around the edge origin (reversed \texttt{eRnext})}
224   \cvarg{CV\_PREV\_AROUND\_DST}{previous around the edge destination (reversed \texttt{eLnext})}
225   \cvarg{CV\_NEXT\_AROUND\_LEFT}{next around the left facet (\texttt{eLnext})}
226   \cvarg{CV\_NEXT\_AROUND\_RIGHT}{next around the right facet (\texttt{eRnext})}
227   \cvarg{CV\_PREV\_AROUND\_LEFT}{previous around the left facet (reversed \texttt{eOnext})}
228   \cvarg{CV\_PREV\_AROUND\_RIGHT}{previous around the right facet (reversed \texttt{eDnext})}
229 \end{description}
230 \end{description}
231
232 The function returns one of the edges related to the input edge.
233
234 \cvCPyFunc{Subdiv2DLocate}
235 Returns the location of a point within a Delaunay triangulation.
236
237 \cvdefC{
238 CvSubdiv2DPointLocation  cvSubdiv2DLocate( \par CvSubdiv2D* subdiv,\par CvPoint2D32f pt,\par CvSubdiv2DEdge* edge,\par CvSubdiv2DPoint** vertex=NULL );
239 }\cvdefPy{Subdiv2DLocate(subdiv, pt) -> (loc, where)}
240
241 \begin{description}
242 \cvarg{subdiv}{Delaunay or another subdivision}
243 \cvarg{pt}{The point to locate}
244 \cvC{\cvarg{edge}{The output edge the point falls onto or right to}}
245 \cvC{\cvarg{vertex}{Optional output vertex double pointer the input point coinsides with}}
246 \cvPy{\cvarg{loc}{The location of the point within the triangulation}}
247 \cvPy{\cvarg{where}{The edge or vertex.  See below.}}
248 \end{description}
249
250 The function locates the input point within the subdivision. There are 5 cases:
251
252 \ifC
253 \begin{itemize}
254  \item The point falls into some facet. The function returns \texttt{CV\_PTLOC\_INSIDE} and \texttt{*edge} will contain one of edges of the facet.
255  \item The point falls onto the edge. The function returns \texttt{CV\_PTLOC\_ON\_EDGE} and \texttt{*edge} will contain this edge.
256  \item The point coincides with one of the subdivision vertices. The function returns \texttt{CV\_PTLOC\_VERTEX} and \texttt{*vertex} will contain a pointer to the vertex.
257  \item The point is outside the subdivsion reference rectangle. The function returns \texttt{CV\_PTLOC\_OUTSIDE\_RECT} and no pointers are filled.
258  \item One of input arguments is invalid. A runtime error is raised or, if silent or "parent" error processing mode is selected, \\texttt{CV\_PTLOC\_ERROR} is returnd.
259 \end{itemize}
260 \fi
261
262 \ifPy
263 \begin{itemize}
264  \item The point falls into some facet.                          \texttt{loc} is \texttt{CV\_PTLOC\_INSIDE} and \texttt{where} is one of edges of the facet.
265  \item The point falls onto the edge.                            \texttt{loc} is \texttt{CV\_PTLOC\_ON\_EDGE} and \texttt{where} is the edge.
266  \item The point coincides with one of the subdivision vertices. \texttt{loc} is \texttt{CV\_PTLOC\_VERTEX} and \texttt{where} is the vertex.
267  \item The point is outside the subdivsion reference rectangle.  \texttt{loc} is \texttt{CV\_PTLOC\_OUTSIDE\_RECT} and \texttt{where} is None.
268  \item One of input arguments is invalid. The function raises an exception.
269 \end{itemize}
270 \fi
271
272 \cvCPyFunc{Subdiv2DRotateEdge}
273 Returns another edge of the same quad-edge.
274
275 \cvdefC{
276 CvSubdiv2DEdge  cvSubdiv2DRotateEdge( \par CvSubdiv2DEdge edge,\par int rotate );
277 }\cvdefPy{Subdiv2DRotateEdge(edge,rotate)-> CvSubdiv2DEdge}
278
279 \begin{description}
280 \cvarg{edge}{Subdivision edge (not a quad-edge)}
281 \cvarg{rotate}{Specifies which of the edges of the same quad-edge as the input one to return, one of the following:
282 \begin{description}
283   \cvarg{0}{the input edge (\texttt{e} on the picture above if \texttt{e} is the input edge)}
284   \cvarg{1}{the rotated edge (\texttt{eRot})}
285   \cvarg{2}{the reversed edge (reversed \texttt{e} (in green))}
286   \cvarg{3}{the reversed rotated edge (reversed \texttt{eRot} (in green))}
287 \end{description}}
288 \end{description}
289
290 The function returns one of the edges of the same quad-edge as the input edge.
291
292 \cvCPyFunc{SubdivDelaunay2DInsert}
293 Inserts a single point into a Delaunay triangulation.
294
295 \cvdefC{
296 CvSubdiv2DPoint*  cvSubdivDelaunay2DInsert( \par CvSubdiv2D* subdiv,\par CvPoint2D32f pt);
297 }\cvdefPy{SubdivDelaunay2DInsert(subdiv,pt)-> point}
298
299 \begin{description}
300 \cvarg{subdiv}{Delaunay subdivision created by the function \cvCPyCross{CreateSubdivDelaunay2D}}
301 \cvarg{pt}{Inserted point}
302 \end{description}
303
304 The function inserts a single point into a subdivision and modifies the subdivision topology appropriately. If a point with the same coordinates exists already, no new point is added. The function returns a pointer to the allocated point. No virtual point coordinates are calculated at this stage.
305
306 \fi