WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
g2d.h
Go to the documentation of this file.
1/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2All rights reserved.
3This software was developed in the APRIL Robotics Lab under the
4direction of Edwin Olson, ebolson@umich.edu. This software may be
5available under alternative licensing terms; contact the address above.
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
81. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23The views and conclusions contained in the software and documentation are those
24of the authors and should not be interpreted as representing official policies,
25either expressed or implied, of the Regents of The University of Michigan.
26*/
27
28#pragma once
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include "zarray.h"
35
36// This library tries to avoid needless proliferation of types.
37//
38// A point is a double[2]. (Note that when passing a double[2] as an
39// argument, it is passed by pointer, not by value.)
40//
41// A polygon is a zarray_t of double[2]. (Note that in this case, the
42// zarray contains the actual vertex data, and not merely a pointer to
43// some other data. IMPORTANT: A polygon must be specified in CCW
44// order. It is implicitly closed (do not list the same point at the
45// beginning at the end.
46//
47// Where sensible, it is assumed that objects should be allocated
48// sparingly; consequently "init" style methods, rather than "create"
49// methods are used.
50
51////////////////////////////////////////////////////////////////////
52// Lines
53
54typedef struct
55{
56 // Internal representation: a point that the line goes through (p) and
57 // the direction of the line (u).
58 double p[2];
59 double u[2]; // always a unit vector
61
62// initialize a line object.
63void g2d_line_init_from_points(g2d_line_t *line, const double p0[2], const double p1[2]);
64
65// The line defines a one-dimensional coordinate system whose origin
66// is p. Where is q? (If q is not on the line, the point nearest q is
67// returned.
68double g2d_line_get_coordinate(const g2d_line_t *line, const double q[2]);
69
70// Intersect two lines. The intersection, if it exists, is written to
71// p (if not NULL), and 1 is returned. Else, zero is returned.
72int g2d_line_intersect_line(const g2d_line_t *linea, const g2d_line_t *lineb, double *p);
73
74////////////////////////////////////////////////////////////////////
75// Line Segments. line.p is always one endpoint; p1 is the other
76// endpoint.
77typedef struct
78{
80 double p1[2];
82
83void g2d_line_segment_init_from_points(g2d_line_segment_t *seg, const double p0[2], const double p1[2]);
84
85// Intersect two segments. The intersection, if it exists, is written
86// to p (if not NULL), and 1 is returned. Else, zero is returned.
88
89void g2d_line_segment_closest_point(const g2d_line_segment_t *seg, const double *q, double *p);
91
92////////////////////////////////////////////////////////////////////
93// Polygons
94
95zarray_t *g2d_polygon_create_data(double v[][2], int sz);
96
98
100
101void g2d_polygon_add(zarray_t *poly, double v[2]);
102
103// Takes a polygon in either CW or CCW and modifies it (if necessary)
104// to be CCW.
106
107// Return 1 if point q lies within poly.
108int g2d_polygon_contains_point(const zarray_t *poly, double q[2]);
109
110// Do the edges of the polygons cross? (Does not test for containment).
111int g2d_polygon_intersects_polygon(const zarray_t *polya, const zarray_t *polyb);
112
113// Does polya completely contain polyb?
114int g2d_polygon_contains_polygon(const zarray_t *polya, const zarray_t *polyb);
115
116// Is there some point which is in both polya and polyb?
117int g2d_polygon_overlaps_polygon(const zarray_t *polya, const zarray_t *polyb);
118
119// returns the number of points written to x. see comments.
120int g2d_polygon_rasterize(const zarray_t *poly, double y, double *x);
121
122#ifdef __cplusplus
123}
124#endif
int g2d_polygon_overlaps_polygon(const zarray_t *polya, const zarray_t *polyb)
void g2d_line_segment_init_from_points(g2d_line_segment_t *seg, const double p0[2], const double p1[2])
void g2d_line_init_from_points(g2d_line_t *line, const double p0[2], const double p1[2])
int g2d_polygon_rasterize(const zarray_t *poly, double y, double *x)
void g2d_line_segment_closest_point(const g2d_line_segment_t *seg, const double *q, double *p)
zarray_t * g2d_polygon_create_data(double v[][2], int sz)
int g2d_line_intersect_line(const g2d_line_t *linea, const g2d_line_t *lineb, double *p)
double g2d_line_get_coordinate(const g2d_line_t *line, const double q[2])
double g2d_line_segment_closest_point_distance(const g2d_line_segment_t *seg, const double *q)
int g2d_polygon_contains_polygon(const zarray_t *polya, const zarray_t *polyb)
int g2d_polygon_intersects_polygon(const zarray_t *polya, const zarray_t *polyb)
void g2d_polygon_add(zarray_t *poly, double v[2])
zarray_t * g2d_polygon_create_empty(void)
void g2d_polygon_make_ccw(zarray_t *poly)
zarray_t * g2d_polygon_create_zeros(int sz)
int g2d_polygon_contains_point(const zarray_t *poly, double q[2])
int g2d_line_segment_intersect_segment(const g2d_line_segment_t *sega, const g2d_line_segment_t *segb, double *p)
Definition g2d.h:78
g2d_line_t line
Definition g2d.h:79
Definition g2d.h:55
Definition zarray.h:44