Austin Schuh | 3333ec7 | 2022-12-29 16:21:06 -0800 | [diff] [blame^] | 1 | /* Copyright (C) 2013-2016, The Regents of The University of Michigan. |
| 2 | All rights reserved. |
| 3 | This software was developed in the APRIL Robotics Lab under the |
| 4 | direction of Edwin Olson, ebolson@umich.edu. This software may be |
| 5 | available under alternative licensing terms; contact the address above. |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are met: |
| 8 | 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | list of conditions and the following disclaimer. |
| 10 | 2. 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. |
| 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 17 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 20 | ON 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 |
| 22 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | The views and conclusions contained in the software and documentation are those |
| 24 | of the authors and should not be interpreted as representing official policies, |
| 25 | either expressed or implied, of the Regents of The University of Michigan. |
| 26 | */ |
| 27 | |
| 28 | #pragma once |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | extern "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 | |
| 54 | typedef 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 |
| 60 | } g2d_line_t; |
| 61 | |
| 62 | // initialize a line object. |
| 63 | void 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. |
| 68 | double 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. |
| 72 | int 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. |
| 77 | typedef struct |
| 78 | { |
| 79 | g2d_line_t line; |
| 80 | double p1[2]; |
| 81 | } g2d_line_segment_t; |
| 82 | |
| 83 | void 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. |
| 87 | int g2d_line_segment_intersect_segment(const g2d_line_segment_t *sega, const g2d_line_segment_t *segb, double *p); |
| 88 | |
| 89 | void g2d_line_segment_closest_point(const g2d_line_segment_t *seg, const double *q, double *p); |
| 90 | double g2d_line_segment_closest_point_distance(const g2d_line_segment_t *seg, const double *q); |
| 91 | |
| 92 | //////////////////////////////////////////////////////////////////// |
| 93 | // Polygons |
| 94 | |
| 95 | zarray_t *g2d_polygon_create_data(double v[][2], int sz); |
| 96 | |
| 97 | zarray_t *g2d_polygon_create_zeros(int sz); |
| 98 | |
| 99 | zarray_t *g2d_polygon_create_empty(); |
| 100 | |
| 101 | void 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. |
| 105 | void g2d_polygon_make_ccw(zarray_t *poly); |
| 106 | |
| 107 | // Return 1 if point q lies within poly. |
| 108 | int 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). |
| 111 | int g2d_polygon_intersects_polygon(const zarray_t *polya, const zarray_t *polyb); |
| 112 | |
| 113 | // Does polya completely contain polyb? |
| 114 | int 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? |
| 117 | int g2d_polygon_overlaps_polygon(const zarray_t *polya, const zarray_t *polyb); |
| 118 | |
| 119 | // returns the number of points written to x. see comments. |
| 120 | int g2d_polygon_rasterize(const zarray_t *poly, double y, double *x); |
| 121 | |
| 122 | #ifdef __cplusplus |
| 123 | } |
| 124 | #endif |