BRL-CAD
polygon.h
Go to the documentation of this file.
1 /* P O L Y G O N . H
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2024 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 /*----------------------------------------------------------------------*/
22 /* @file polygon.h */
23 /** @addtogroup bg_polygon */
24 /** @{ */
25 
26 /**
27  * @brief Functions for working with polygons
28  */
29 
30 #ifndef BG_POLYGON_H
31 #define BG_POLYGON_H
32 
33 #include "common.h"
34 #include "vmath.h"
35 #include "bu/color.h"
36 #include "bn/tol.h"
37 #include "bv/defines.h"
38 #include "bg/defines.h"
39 #include "bg/polygon_types.h"
40 
41 __BEGIN_DECLS
42 
43 /* TODO - the following are operations originally from libged - ultimately need
44  * to better integrate these and the other polygon routines. For now, trying
45  * to get all the related logic in the same place so it is clearer what we do
46  * and don't have, and make what we do have easier to reuse. */
47 
48 /*
49  * Weird upper limit from clipper ---> sqrt(2^63 -1)/2
50  * Probably should be sqrt(2^63 -1)
51  */
52 #define CLIPPER_MAX 1518500249
53 
54 BG_EXPORT extern fastf_t
56  struct bg_polygon *gpoly,
57  fastf_t sf,
58  plane_t *vp,
60  );
61 
62 BG_EXPORT extern int
64  struct bg_polygon *polyA,
65  struct bg_polygon *polyB,
66  plane_t *vp,
67  const struct bn_tol *tol,
68  fastf_t iscale
69  );
70 
71 BG_EXPORT extern struct bg_polygon *
74  struct bg_polygon *subj,
75  struct bg_polygon *clip,
76  fastf_t sf,
77  plane_t *vp
78  );
79 
80 BG_EXPORT extern struct bg_polygon *
83  struct bg_polygons *subj,
84  struct bg_polygons *clip,
85  fastf_t sf,
86  plane_t *vp
87  );
88 
89 BG_EXPORT extern void bg_polygon_free(struct bg_polygon *gpp);
90 BG_EXPORT extern void bg_polygons_free(struct bg_polygons *gpp);
91 
92 BG_EXPORT extern void bg_polygon_cpy(struct bg_polygon *dest, struct bg_polygon *src);
93 
94 /**
95  * @brief
96  * Find the 2D axis aligned bounding box of a bg_polygon in view coordinates.
97  *
98  * NOTE: If the polygon's internal data is defined in the XY plane and no view
99  * projection is desired, pass MAT_IDN to model2view to get the raw data's
100  * bbox.
101  */
102 BG_EXPORT extern void
103 bg_polygon_view_bbox(point2d_t *bmin, point2d_t *bmax, struct bg_polygon *p, matp_t model2view);
104 
105 /********************************
106  * Operations on 2D point types *
107  ********************************/
108 
109 /**
110  * @brief
111  * Test whether a polygon is clockwise (CW) or counter clockwise (CCW)
112  *
113  * Determine if a set of points forming a polygon are in clockwise
114  * or counter-clockwise order (see http://stackoverflow.com/a/1165943)
115  *
116  * @param[in] npts number of points in polygon
117  * @param[in] pts array of points
118  * @param[in] pt_indices index values into pts array building a convex polygon. duplicated points
119  * aren't allowed.
120  *
121  * If pt_indices is NULL, the first npts points in pts will be checked in array order.
122  *
123  * @return BG_CCW if polygon is counter-clockwise
124  * @return BG_CW if polygon is clockwise
125  * @return 0 if the test failed
126  */
127 BG_EXPORT extern int bg_polygon_direction(size_t npts, const point2d_t *pts, const int *pt_indices);
128 
129 /**
130  * @brief
131  * test whether a point is inside a 2d polygon
132  *
133  * franklin's test for point inclusion within a polygon - see
134  * https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html
135  * for more details and the implementation file polygon.c for license info.
136  *
137  * @param[in] npts number of points pts contains
138  * @param[in] pts array of points, building a convex polygon. duplicated points
139  * aren't allowed. the points in the array will be sorted counter-clockwise.
140  * @param[in] test_pt point to test.
141  *
142  * @return 0 if point is outside polygon
143  * @return 1 if point is inside polygon
144  */
145 BG_EXPORT extern int bg_pnt_in_polygon(size_t npts, const point2d_t *pts, const point2d_t *test_pt);
146 
147 /**
148  * Triangulation is the process of finding a set of triangles that as a set cover
149  * the same total surface area as a polygon. There are many algorithms for this
150  * operation, which have various trade-offs in speed and output quality.
151  */
152 typedef enum {
153  TRI_ANY = 0,
161 
162 /**
163  * @brief
164  * Triangulate a 2D polygon with holes.
165  *
166  * The primary polygon definition must be provided as an array of
167  * counter-clockwise indices to 2D points. If interior "hole" polygons are
168  * present, they must be passed in via the holes_array and their indices be
169  * ordered clockwise.
170  *
171  * If no holes are present, caller should pass NULL for holes_array and holes_npts,
172  * and 0 for nholes, or use bg_polygon_triangulate instead.
173  *
174  * This routine deliberately uses low level data types for both input and output
175  * to maximize the reusability of this logic.
176  *
177  * @param[out] faces Set of faces in the triangulation, stored as integer indices to the pts. The first three indices are the vertices of the first face, the second three define the second face, and so forth.
178  * @param[out] num_faces Number of faces created
179  * @param[out] out_pts output points used by faces set. If an algorithm was selected that generates new points, this will be a new array.
180  * @param[out] num_outpts number of output points, if an algorithm was selected that generates new points
181  * @param[in] poly Non-hole polygon, defined as a CCW array of indices into the pts array.
182  * @param[in] poly_npts Number of points in non-hole polygon
183  * @param[in] holes_array Array of hole polygons, each defined as a CW array of indices into the pts array.
184  * @param[in] holes_npts Array of counts of points in hole polygons
185  * @param[in] nholes Number of hole polygons contained in holes_array
186  * @param[in] steiner Array of Steiner points
187  * @param[in] steiner_npts Number of Steiner points
188  * @param[in] pts Array of points defining a polygon. Duplicated points
189  * @param[in] npts Number of points pts contains
190  * @param[in] type Type of triangulation
191  *
192  * @return 0 if triangulation is successful
193  * @return 1 if triangulation is unsuccessful
194  */
195 BG_EXPORT extern int
196 bg_nested_poly_triangulate(int **faces, int *num_faces, point2d_t **out_pts, int *num_outpts,
197  const int *poly, const size_t poly_npts,
198  const int **holes_array, const size_t *holes_npts, const size_t nholes,
199  const int *steiner, const size_t steiner_npts,
200  const point2d_t *pts, const size_t npts, triangulation_t type);
201 
202 /**
203  * @brief
204  * Triangulate a 2D polygon without holes.
205  *
206  * The polygon cannot have holes and must be provided as an array of
207  * counter-clockwise 2D points.
208  *
209  * No points are added as part of this triangulation process - the result uses
210  * only those points in the original polygon, and hence only the face
211  * information is created as output.
212  *
213  * The same fundamental routines are used here as in the bg_nested_polygon_triangulate
214  * logic - this is a convenience function to simplify calling the routine when
215  * specification of hole polygons is not needed.
216  *
217  * This routine deliberately uses low level data types for both input and output
218  * to maximize the reusability of this logic.*
219  *
220  * @param[out] faces Set of faces in the triangulation, stored as integer indices to the pts. The first three indices are the vertices of the first face, the second three define the second face, and so forth.
221  * @param[out] num_faces Number of faces created
222  * @param[out] out_pts output points used by faces set, if an algorithm was selected that generates new points
223  * @param[out] num_outpts number of output points, if an algorithm was selected that generates new points
224  * @param[in] steiner Array of Steiner points
225  * @param[in] steiner_npts Number of Steiner points
226  * @param[in] pts Array of points defining a polygon. Duplicated points
227  * @param[in] npts Number of points pts contains
228  * @param[in] type Triangulation type
229  *
230  * @return 0 if triangulation is successful
231  * @return 1 if triangulation is unsuccessful
232  */
233 BG_EXPORT extern int
234 bg_poly_triangulate(int **faces, int *num_faces, point2d_t **out_pts, int *num_outpts,
235  const int *steiner, const size_t steiner_npts,
236  const point2d_t *pts, const size_t npts, triangulation_t type);
237 
238 /**
239  * @brief
240  * Triangulate a bg_polygon.
241  *
242  * @param[out] faces Set of faces in the triangulation, stored as integer indices to the pts. The first three indices are the vertices of the first face, the second three define the second face, and so forth.
243  * @param[out] num_faces Number of faces created
244  * @param[out] out_pts output points used by faces set, if an algorithm was selected that generates new points
245  * @param[out] num_outpts number of output points, if an algorithm was selected that generates new points
246  * @param[in] p bg_polygon holding the polygon contours to be triangulated
247  * @param[in] type Triangulation type
248  *
249  * @return 0 if triangulation is successful
250  * @return 1 if triangulation is unsuccessful
251  */
252 BG_EXPORT extern int
253 bg_polygon_triangulate(int **faces, int *num_faces, point_t **out_pts, int *num_outpts,
254  struct bg_polygon *p, triangulation_t type);
255 
256 
257 /* Test function - do not use */
258 BG_EXPORT extern int
259 bg_poly2tri_test(int **faces, int *num_faces, point2d_t **out_pts, int *num_outpts,
260  const int *poly, const size_t poly_pnts,
261  const int **holes_array, const size_t *holes_npts, const size_t nholes,
262  const int *steiner, const size_t steiner_npts,
263  const point2d_t *pts);
264 
265 /*********************************************************
266  Operations on 3D point types - these are assumed to be
267  polygons embedded in 3D planes in space
268 *********************************************************/
269 
270 /**
271  * @brief
272  * Calculate the interior area of a polygon in a 3D plane in space.
273  *
274  * If npts > 4, Greens Theorem is used. The polygon mustn't
275  * be self-intersecting.
276  *
277  * @param[out] area The interior area of the polygon
278  * @param[in] npts Number of point_ts, stored in pts
279  * @param[in] pts All points of the polygon, sorted counter-clockwise.
280  * The array mustn't contain duplicated or non-coplanar points.
281  *
282  * @return 0 if calculation was successful
283  * @return 1 if calculation failed, e.g. because one parameter is a NULL-pointer
284  */
285 BG_EXPORT extern int bg_3d_polygon_area(fastf_t *area, size_t npts, const point_t *pts);
286 
287 
288 /**
289  * @brief
290  * Calculate the centroid of a non self-intersecting polygon in a 3D plane in space.
291  *
292  * @param[out] cent The centroid of the polygon
293  * @param[in] npts Number of point_ts, stored in pts
294  * @param[in] pts all points of the polygon, sorted counter-clockwise.
295  * The array mustn't contain duplicated points or non-coplanar points.
296  *
297  * @return 0 if calculation was successful
298  * @return 1 if calculation failed, e.g. because one in-parameter is a NULL-pointer
299  */
300 BG_EXPORT extern int bg_3d_polygon_centroid(point_t *cent, size_t npts, const point_t *pts);
301 
302 
303 /**
304  * @brief
305  * Sort an array of point_ts, building a convex polygon, counter-clockwise
306  *
307  *@param[in] npts Number of points, pts contains
308  *@param pts Array of point_ts, building a convex polygon. Duplicated points
309  *aren't allowed. The points in the array will be sorted counter-clockwise.
310  *@param[in] cmp Plane equation of the polygon
311  *
312  *@return 0 if calculation was successful
313  *@return 1 if calculation failed, e.g. because pts is a NULL-pointer
314  */
315 BG_EXPORT extern int bg_3d_polygon_sort_ccw(size_t npts, point_t *pts, plane_t cmp);
316 
317 
318 /**
319  * @brief
320  * Calculate for an array of plane_eqs, which build a polyhedron, the
321  * point_t's for each face.
322  *
323  * @param[out] npts Array, which stores for every face the number of
324  * point_ts, added to pts. Needs to be allocated with npts[neqs] already.
325  * @param[out] pts 2D-array which stores the point_ts for every
326  * face. The array needs to be allocated with pts[neqs][neqs-1] already.
327  * @param[in] neqs Number of plane_ts, stored in eqs
328  * @param[in] eqs Array, that contains the plane equations, which
329  * build the polyhedron
330  *
331  * @return 0 if calculation was successful
332  * @return 1 if calculation failed, e.g. because one parameter is a NULL-Pointer
333  */
334 BG_EXPORT extern int bg_3d_polygon_make_pnts_planes(size_t *npts, point_t **pts, size_t neqs, const plane_t *eqs);
335 
336 
337 
338 /* Debugging functions - do not use */
339 BG_EXPORT extern void bg_polygon_plot_2d(const char *filename, const point2d_t *pnts, int npnts, int r, int g, int b);
340 BG_EXPORT extern void bg_polygon_plot(const char *filename, const point_t *pnts, int npnts, int r, int g, int b);
341 BG_EXPORT extern void bg_tri_plot_2d(const char *filename, const int *faces, int num_faces, const point2d_t *pnts, int r, int g, int b);
342 
343 __END_DECLS
344 
345 #endif /* BG_POLYGON_H */
346 /** @} */
347 /*
348  * Local Variables:
349  * mode: C
350  * tab-width: 8
351  * indent-tabs-mode: t
352  * c-file-style: "stroustrup"
353  * End:
354  * ex: shiftwidth=4 tabstop=8
355  */
Header file for the BRL-CAD common definitions.
void bg_polygon_plot(const char *filename, const point_t *pnts, int npnts, int r, int g, int b)
int bg_polygon_direction(size_t npts, const point2d_t *pts, const int *pt_indices)
Test whether a polygon is clockwise (CW) or counter clockwise (CCW)
triangulation_t
Definition: polygon.h:153
int bg_3d_polygon_centroid(point_t *cent, size_t npts, const point_t *pts)
Calculate the centroid of a non self-intersecting polygon in a 3D plane in space.
fastf_t bg_find_polygon_area(struct bg_polygon *gpoly, fastf_t sf, plane_t *vp, fastf_t size)
int bg_polygon_triangulate(int **faces, int *num_faces, point_t **out_pts, int *num_outpts, struct bg_polygon *p, triangulation_t type)
Triangulate a bg_polygon.
int bg_3d_polygon_area(fastf_t *area, size_t npts, const point_t *pts)
Calculate the interior area of a polygon in a 3D plane in space.
int bg_pnt_in_polygon(size_t npts, const point2d_t *pts, const point2d_t *test_pt)
test whether a point is inside a 2d polygon
int bg_polygons_overlap(struct bg_polygon *polyA, struct bg_polygon *polyB, plane_t *vp, const struct bn_tol *tol, fastf_t iscale)
void bg_polygon_cpy(struct bg_polygon *dest, struct bg_polygon *src)
void bg_tri_plot_2d(const char *filename, const int *faces, int num_faces, const point2d_t *pnts, int r, int g, int b)
void bg_polygons_free(struct bg_polygons *gpp)
bg_clip_t
Functions for working with polygons.
Definition: polygon_types.h:40
int bg_poly2tri_test(int **faces, int *num_faces, point2d_t **out_pts, int *num_outpts, const int *poly, const size_t poly_pnts, const int **holes_array, const size_t *holes_npts, const size_t nholes, const int *steiner, const size_t steiner_npts, const point2d_t *pts)
int bg_3d_polygon_sort_ccw(size_t npts, point_t *pts, plane_t cmp)
Sort an array of point_ts, building a convex polygon, counter-clockwise.
struct bg_polygon * bg_clip_polygons(bg_clip_t op, struct bg_polygons *subj, struct bg_polygons *clip, fastf_t sf, plane_t *vp)
int bg_nested_poly_triangulate(int **faces, int *num_faces, point2d_t **out_pts, int *num_outpts, const int *poly, const size_t poly_npts, const int **holes_array, const size_t *holes_npts, const size_t nholes, const int *steiner, const size_t steiner_npts, const point2d_t *pts, const size_t npts, triangulation_t type)
Triangulate a 2D polygon with holes.
int bg_3d_polygon_make_pnts_planes(size_t *npts, point_t **pts, size_t neqs, const plane_t *eqs)
Calculate for an array of plane_eqs, which build a polyhedron, the point_t's for each face.
void bg_polygon_plot_2d(const char *filename, const point2d_t *pnts, int npnts, int r, int g, int b)
void bg_polygon_free(struct bg_polygon *gpp)
void bg_polygon_view_bbox(point2d_t *bmin, point2d_t *bmax, struct bg_polygon *p, matp_t model2view)
Find the 2D axis aligned bounding box of a bg_polygon in view coordinates.
struct bg_polygon * bg_clip_polygon(bg_clip_t op, struct bg_polygon *subj, struct bg_polygon *clip, fastf_t sf, plane_t *vp)
int bg_poly_triangulate(int **faces, int *num_faces, point2d_t **out_pts, int *num_outpts, const int *steiner, const size_t steiner_npts, const point2d_t *pts, const size_t npts, triangulation_t type)
Triangulate a 2D polygon without holes.
@ TRI_MONOTONE
Definition: polygon.h:157
@ TRI_ANY
Definition: polygon.h:154
@ TRI_KEIL_SNOEYINK
Definition: polygon.h:159
@ TRI_DELAUNAY
Definition: polygon.h:160
@ TRI_HERTEL_MEHLHORN
Definition: polygon.h:158
@ TRI_CONSTRAINED_DELAUNAY
Definition: polygon.h:156
@ TRI_EAR_CLIPPING
Definition: polygon.h:155
void float float int int int int float * size
Definition: tig.h:132
int clip(fastf_t *, fastf_t *, fastf_t *, fastf_t *)
double fastf_t
fastest 64-bit (or larger) floating point type
Definition: vmath.h:334
fastf_t point2d_t[ELEMENTS_PER_POINT2D]
2-tuple point
Definition: vmath.h:343
fastf_t plane_t[ELEMENTS_PER_PLANE]
Definition of a plane equation.
Definition: vmath.h:397
fastf_t * matp_t
pointer to a 4x4 matrix
Definition: vmath.h:373
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition: vmath.h:355
Definition: tol.h:72
fundamental vector, matrix, quaternion math macros