r2d.h File Reference
#include <stdint.h>

Go to the source code of this file.

Classes

struct  r2d_rvec2
 Vector struct. More...
 
struct  r2d_dvec2
 Integer vector struct for grid indexing. More...
 
struct  r2d_plane
 A plane. More...
 
struct  r2d_vertex
 A doubly-linked vertex. More...
 
struct  r2d_poly
 A polygon. More...
 

Macros

#define R2D_MAX_VERTS   256
 
#define R2D_NUM_MOMENTS(order)   ((order+1)*(order+2)/2)
 Integrate a polynomial density over a polygon using simplicial decomposition. Uses the fast recursive method of Koehl (2012) to carry out the integration. More...
 

Typedefs

typedef double r2d_real
 Real type specifying the precision to be used in calculations. More...
 
typedef int32_t r2d_int
 Integer types used for indexing. More...
 
typedef int64_t r2d_long
 

Functions

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-spaces). More...
 
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. More...
 
void r2d_reduce (r2d_poly *poly, r2d_real *moments, r2d_int polyorder)
 
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 other vertices, and that there are no vertices that point to themselves. More...
 
r2d_rvec2 r2d_poly_center (r2d_poly *poly)
 Calculates a center of a polygon. More...
 
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. More...
 
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. More...
 
void r2d_print (r2d_poly *poly)
 Prints the vertices and connectivity of a polygon. For debugging. More...
 
void r2d_rotate (r2d_poly *poly, r2d_real theta)
 Rotate a polygon about the origin. More...
 
void r2d_translate (r2d_poly *poly, r2d_rvec2 shift)
 Translate a polygon. More...
 
void r2d_scale (r2d_poly *poly, r2d_real scale)
 Scale a polygon. More...
 
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]. More...
 
void r2d_affine (r2d_poly *poly, r2d_real mat[3][3])
 Apply a general affine transformation to a polygon. More...
 
void r2d_init_box (r2d_poly *poly, r2d_rvec2 *rbounds)
 Initialize a polygon as an axis-aligned box. More...
 
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 that the output is valid. More...
 
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-aligned box. More...
 
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 polygon. More...
 

Macro Definition Documentation

◆ R2D_MAX_VERTS

#define R2D_MAX_VERTS   256

◆ R2D_NUM_MOMENTS

#define R2D_NUM_MOMENTS (   order)    ((order+1)*(order+2)/2)

Integrate a polynomial density over a polygon using simplicial decomposition. Uses the fast recursive method of Koehl (2012) to carry out the integration.

Parameters
[in]polyThe polygon over which to integrate.
[in]polyorderOrder of the polynomial density field. 0 for constant (1 moment), 1 for linear (4 moments), 2 for quadratic (10 moments), etc.
[in,out]momentsArray to be filled with the integration results, up to the specified polyorder. Must be at least (polyorder+1)*(polyorder+2)/2 long. A conventient macro, R2D_NUM_MOMENTS() is provided to compute the number of moments for a given order. Order of moments is row-major, i.e. 1, x, y, x^2, x*y, y^2, x^3, x^2*y...

Typedef Documentation

◆ r2d_int

typedef int32_t r2d_int

Integer types used for indexing.

◆ r2d_long

typedef int64_t r2d_long

◆ r2d_real

typedef double r2d_real

Real type specifying the precision to be used in calculations.

Default is double. float precision is enabled by compiling with -DSINGLE_PRECISION.

Function Documentation

◆ r2d_affine()

void r2d_affine ( r2d_poly poly,
r2d_real  mat[3][3] 
)

Apply a general affine transformation to a polygon.

Parameters
[in,out]polyThe polygon to transform.
[in]matThe 3x3 homogeneous transformation matrix by which to transform the vertices of the polygon.

◆ r2d_box_faces_from_verts()

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-aligned box.

Parameters
[out]facesArray of four planes defining the faces of the box.
[in]rboundsArray of two vectors defining the bounds of the axis-aligned box

◆ r2d_clip()

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-spaces).

Parameters
[in,out]polyThe polygon to be clipped.
[in]planesAn array of planes against which to clip this polygon.
[in]nplanesThe number of planes in the input array.

◆ r2d_init_box()

void r2d_init_box ( r2d_poly poly,
r2d_rvec2 rbounds 
)

Initialize a polygon as an axis-aligned box.

Parameters
[out]polyThe polygon to initialize.
[in]rboundsAn array of two vectors, giving the lower and upper corners of the box.

◆ r2d_init_poly()

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 that the output is valid.

Parameters
[out]polyThe polygon to initialize.
[in]verticesArray of length numverts giving the vertices of the input polygon, in counterclockwise order.
[in]numvertsNumber of vertices in the input polygon.

◆ r2d_is_good()

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 other vertices, and that there are no vertices that point to themselves.

Parameters
[in]polyThe polygon to check.
Returns
1 if the polygon is good, 0 if not.

◆ r2d_orient()

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.

Parameters
[in]pa,pb,pcVertices defining a triangle from which to calculate an area.
Returns
The signed volume of the input triangle.

◆ r2d_poly_center()

r2d_rvec2 r2d_poly_center ( r2d_poly poly)

Calculates a center of a polygon.

Parameters
[in]polyThe polygon to check.
Returns
coordinates of a polygon center.

◆ r2d_poly_faces_from_verts()

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 polygon.

Parameters
[out]facesArray of planes of length numverts defining the faces of the polygon.
[in]verticesArray of length numverts giving the vertices of the input polygon, in counterclockwise order.
[in]numvertsNumber of vertices in the input polygon.

◆ r2d_print()

void r2d_print ( r2d_poly poly)

Prints the vertices and connectivity of a polygon. For debugging.

Parameters
[in]polyThe polygon to print.

◆ r2d_reduce()

void r2d_reduce ( r2d_poly poly,
r2d_real moments,
r2d_int  polyorder 
)

◆ r2d_rotate()

void r2d_rotate ( r2d_poly poly,
r2d_real  theta 
)

Rotate a polygon about the origin.

Parameters
[in,out]polyThe polygon to rotate.
[in]thetaThe angle, in radians, by which to rotate the polygon.

◆ r2d_scale()

void r2d_scale ( r2d_poly poly,
r2d_real  scale 
)

Scale a polygon.

Parameters
[in,out]polyThe polygon to scale.
[in]shiftThe factor by which to scale the polygon.

◆ r2d_shear()

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].

Parameters
[in,out]polyThe polygon to shear.
[in]shearThe factor by which to shear the polygon.
[in]axbThe axis (0 or 1 corresponding to x or y) along which to shear the polygon.
[in]axsThe axis (0 or 1 corresponding to x or y) across which to shear the polygon.

◆ r2d_shift_moments()

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.

Parameters
[in,out]momentsThe moments of the shifted polygon.
[in]polyorderOrder of the polygon.
[in]vcCoordinates of the polygon center, which are used to shift the polygon.

◆ r2d_split()

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.

Parameters
[in]inpolysArray input polyhedra to be split
[in]npolysThe number of input polygons
[in]planeThe plane about which to split the input polys
[out]out_posThe output array of fragments on the positive side of the clip plane. Must be at least npolys long. out_pos[i] and out_neg[i] correspond to inpolys[i], where out_neg[i].nverts or out.pos[i].nverts are set to zero if the poly lies entirely in the positive or negative side of the plane, respectively.
[out]out_negThe output array of fragments on the negitive side of the clip plane. Must be at least npolys long.

◆ r2d_translate()

void r2d_translate ( r2d_poly poly,
r2d_rvec2  shift 
)

Translate a polygon.

Parameters
[in,out]polyThe polygon to translate.
[in]shiftThe vector by which to translate the polygon.