r2d.h
Go to the documentation of this file.
1 /*
2  *
3  * r2d.h
4  *
5  * Routines for fast, geometrically robust clipping operations
6  * and analytic area/moment computations over polygons in 2D.
7  *
8  * Devon Powell
9  * 31 August 2015
10  *
11  * This program was prepared by Los Alamos National Security, LLC at Los Alamos National
12  * Laboratory (LANL) under contract No. DE-AC52-06NA25396 with the U.S. Department of Energy (DOE).
13  * All rights in the program are reserved by the DOE and Los Alamos National Security, LLC.
14  * Permission is granted to the public to copy and use this software without charge, provided that
15  * this Notice and any statement of authorship are reproduced on all copies. Neither the U.S.
16  * Government nor LANS makes any warranty, express or implied, or assumes any liability
17  * or responsibility for the use of this software.
18  *
19  */
20 
21 #ifndef _R2D_H_
22 #define _R2D_H_
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include <stdint.h>
29 
43 #ifdef SINGLE_PRECISION
44 typedef float r2d_real;
45 #else
46 typedef double r2d_real;
47 #endif
48 
52 typedef int32_t r2d_int;
53 typedef int64_t r2d_long;
54 
58 typedef union {
59  struct {
60  r2d_real x,
61  y;
62  };
63  r2d_real xy[2];
64 } r2d_rvec2;
65 
69 typedef union {
70  struct {
71  r2d_int i,
72  j;
73  };
74  r2d_int ij[2];
75 } r2d_dvec2;
76 
80 typedef struct {
81  r2d_rvec2 n;
82  r2d_real d;
83 } r2d_plane;
84 
88 typedef struct {
89  r2d_int pnbrs[2];
90  r2d_rvec2 pos;
91 } r2d_vertex;
92 
96 typedef struct {
97 #define R2D_MAX_VERTS 256
98  r2d_vertex verts[R2D_MAX_VERTS];
99  r2d_int nverts;
100 } r2d_poly;
101 
115 void r2d_clip(r2d_poly* poly, r2d_plane* planes, r2d_int nplanes);
116 
140 void r2d_split(r2d_poly* inpolys, r2d_int npolys, r2d_plane plane, r2d_poly* out_pos, r2d_poly* out_neg);
141 
160 #define R2D_NUM_MOMENTS(order) ((order+1)*(order+2)/2)
161 void r2d_reduce(r2d_poly* poly, r2d_real* moments, r2d_int polyorder);
162 
174 r2d_int r2d_is_good(r2d_poly* poly);
175 
187 
201 void r2d_shift_moments(r2d_real* moments, r2d_int polyorder, r2d_rvec2 vc);
202 
213 r2d_real r2d_orient(r2d_rvec2 pa, r2d_rvec2 pb, r2d_rvec2 pc);
214 
222 void r2d_print(r2d_poly* poly);
223 
234 void r2d_rotate(r2d_poly* poly, r2d_real theta);
235 
246 void r2d_translate(r2d_poly* poly, r2d_rvec2 shift);
247 
258 void r2d_scale(r2d_poly* poly, r2d_real scale);
259 
277 void r2d_shear(r2d_poly* poly, r2d_real shear, r2d_int axb, r2d_int axs);
278 
290 void r2d_affine(r2d_poly* poly, r2d_real mat[3][3]);
291 
302 void r2d_init_box(r2d_poly* poly, r2d_rvec2* rbounds);
303 
318 void r2d_init_poly(r2d_poly* poly, r2d_rvec2* vertices, r2d_int numverts);
319 
331 void r2d_box_faces_from_verts(r2d_plane* faces, r2d_rvec2* rbounds);
332 
347 void r2d_poly_faces_from_verts(r2d_plane* faces, r2d_rvec2* vertices, r2d_int numverts);
348 
349 #ifdef __cplusplus
350 }
351 #endif
352 
353 #endif // _R2D_H_
r2d_rvec2 r2d_poly_center(r2d_poly *poly)
Calculates a center of a polygon.
void r2d_print(r2d_poly *poly)
Prints the vertices and connectivity of a polygon. For debugging.
void r2d_poly_faces_from_verts(r2d_plane *faces, r2d_rvec2 *vertices, r2d_int numverts)
Get all faces (unit normals and distances to the origin) from a general boundary description of a pol...
int64_t r2d_long
Long integer type used for grid indexing.
Definition: r2d.h:51
void r2d_affine(r2d_poly *poly, r2d_real mat[3][3])
Apply a general affine transformation to a polygon.
A polygon.
Definition: r2d.h:97
void r2d_clip(r2d_poly *poly, r2d_plane *planes, r2d_int nplanes)
Clip a polygon against an arbitrary number of clip planes (find its intersection with a set of half-s...
#define R2D_MAX_VERTS
Definition: r2d.h:97
r2d_real r2d_orient(r2d_rvec2 pa, r2d_rvec2 pb, r2d_rvec2 pc)
Get the signed volume of the triangle defined by the input vertices.
void r2d_init_box(r2d_poly *poly, r2d_rvec2 *rbounds)
Initialize a polygon as an axis-aligned box.
void r2d_translate(r2d_poly *poly, r2d_rvec2 shift)
Translate a polygon.
void r2d_reduce(r2d_poly *poly, r2d_int polyorder, r2d_real *moments)
Integrate a polynomial density over a polygon using simplicial decomposition.
r2d_int r2d_is_good(r2d_poly *poly)
Checks a polygon to see if all vertices have two valid edges, that all vertices are pointed to by two...
void r2d_scale(r2d_poly *poly, r2d_real scale)
Scale a polygon.
Integer vector struct for grid indexing.
Definition: r2d.h:64
void r2d_shear(r2d_poly *poly, r2d_real shear, r2d_int axb, r2d_int axs)
Shear a polygon. Each vertex undergoes the transformation pos.xy[axb] += shear*pos.xy[axs].
void r2d_shift_moments(r2d_real *moments, r2d_int polyorder, r2d_rvec2 vc)
Adjust moments according to the shift of polygon vertices to the origin.
A plane.
Definition: r2d.h:72
void r2d_split(r2d_poly *inpolys, r2d_int npolys, r2d_plane plane, r2d_poly *out_pos, r2d_poly *out_neg)
Splits a list of polygons across a single plane.
Vector struct.
Definition: r2d.h:56
void r2d_init_poly(r2d_poly *poly, r2d_rvec2 *vertices, r2d_int numverts)
Initialize a (simply-connected) general polygon from a list of vertices. Can use r2d_is_good to check...
A doubly-linked vertex.
Definition: r2d.h:88
double r2d_real
Real type specifying the precision to be used in calculations.
Definition: r2d.h:40
void r2d_box_faces_from_verts(r2d_plane *faces, r2d_rvec2 *rbounds)
Get four faces (unit normals and distances to the origin) from a two-vertex description of an axis-al...
int32_t r2d_int
Integer type used for grid indexing and bit flags.
Definition: r2d.h:46
void r2d_rotate(r2d_poly *poly, r2d_real theta)
Rotate a polygon about the origin.