APM:Libraries
polygon.cpp
Go to the documentation of this file.
1 /*
2  * polygon.cpp
3  * Copyright (C) Andrew Tridgell 2011
4  *
5  * This file is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This file is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "AP_Math.h"
20 
21 /*
22  * The point in polygon algorithm is based on:
23  * http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
24  */
25 
26 
27 /*
28  * Polygon_outside(): test for a point in a polygon
29  * Input: P = a point,
30  * V[] = vertex points of a polygon V[n+1] with V[n]=V[0]
31  * Return: true if P is outside the polygon
32  *
33  * This does not take account of the curvature of the earth, but we
34  * expect that to be very small over the distances involved in the
35  * fence boundary
36  */
37 template <typename T>
38 bool Polygon_outside(const Vector2<T> &P, const Vector2<T> *V, unsigned n)
39 {
40  unsigned i, j;
41  bool outside = true;
42  for (i = 0, j = n-1; i < n; j = i++) {
43  if ((V[i].y > P.y) == (V[j].y > P.y)) {
44  continue;
45  }
46  const int32_t dx1 = P.x - V[i].x;
47  const int32_t dx2 = V[j].x - V[i].x;
48  const int32_t dy1 = P.y - V[i].y;
49  const int32_t dy2 = V[j].y - V[i].y;
50  const int8_t dx1s = (dx1 < 0) ? -1 : 1;
51  const int8_t dx2s = (dx2 < 0) ? -1 : 1;
52  const int8_t dy1s = (dy1 < 0) ? -1 : 1;
53  const int8_t dy2s = (dy2 < 0) ? -1 : 1;
54  const int8_t m1 = dx1s * dy2s;
55  const int8_t m2 = dx2s * dy1s;
56  // we avoid the 64 bit multiplies if we can based on sign checks.
57  if (dy2 < 0) {
58  if (m1 > m2) {
59  outside = !outside;
60  } else if (m1 < m2) {
61  continue;
62  } else if ( dx1 * (int64_t)dy2 > dx2 * (int64_t)dy1 ) {
63  outside = !outside;
64  }
65  } else {
66  if (m1 < m2) {
67  outside = !outside;
68  } else if (m1 > m2) {
69  continue;
70  } else if ( dx1 * (int64_t)dy2 < dx2 * (int64_t)dy1 ) {
71  outside = !outside;
72  }
73  }
74  }
75  return outside;
76 }
77 
78 /*
79  * check if a polygon is complete.
80  *
81  * We consider a polygon to be complete if we have at least 4 points,
82  * and the first point is the same as the last point. That is the
83  * minimum requirement for the Polygon_outside function to work
84  */
85 template <typename T>
86 bool Polygon_complete(const Vector2<T> *V, unsigned n)
87 {
88  return (n >= 4 && V[n-1] == V[0]);
89 }
90 
91 // Necessary to avoid linker errors
92 template bool Polygon_outside<int32_t>(const Vector2l &P, const Vector2l *V, unsigned n);
93 template bool Polygon_complete<int32_t>(const Vector2l *V, unsigned n);
94 template bool Polygon_outside<float>(const Vector2f &P, const Vector2f *V, unsigned n);
95 template bool Polygon_complete<float>(const Vector2f *V, unsigned n);
template bool Polygon_complete< int32_t >(const Vector2l *V, unsigned n)
bool Polygon_complete(const Vector2< T > *V, unsigned n)
Definition: polygon.cpp:86
T y
Definition: vector2.h:37
template bool Polygon_outside< float >(const Vector2f &P, const Vector2f *V, unsigned n)
template bool Polygon_complete< float >(const Vector2f *V, unsigned n)
bool outside
Definition: polygon.cpp:35
T x
Definition: vector2.h:37
template bool Polygon_outside< int32_t >(const Vector2l &P, const Vector2l *V, unsigned n)
bool Polygon_outside(const Vector2< T > &P, const Vector2< T > *V, unsigned n)
Definition: polygon.cpp:38