Easy3D 2.5.3
polygon.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_CORE_POLYGON_H
28#define EASY3D_CORE_POLYGON_H
29
30
31#include <vector>
32#include <algorithm>
33
34#include <easy3d/core/vec.h>
35
36namespace easy3d {
37
40 template<typename FT>
41 class GenericPolygon : public std::vector< Vec<2, FT> > {
42 typedef std::vector< Vec<2, FT> > BaseClass;
43
44 public:
46 GenericPolygon() = default;
47
49 explicit GenericPolygon(std::size_t size) { BaseClass::resize(size); }
50
52 template<class InputIterator>
53 GenericPolygon(InputIterator first, InputIterator last);
54
61 bool is_clockwise() const;
62
66
68 bool contains(const Vec<2, FT> &point) const;
69
71 bool contains(const GenericPolygon<FT> &plg) const;
72
75 FT signed_area() const;
76
79 FT area() const;
80
82 GenericBox<2, FT> bbox() const;
83 };
84
86
87 template<typename FT>
88 template<class InputIterator>
89 inline GenericPolygon<FT>::GenericPolygon(InputIterator first, InputIterator last) {
90 std::copy(first, last, BaseClass::end());
91 }
92
93 template<typename FT>
95 #if 0
96 return signed_area() < 0;
97 #else
98 FT sum = FT(0);
99 for (std::size_t i = 0; i + 1 < BaseClass::size(); ++i) {
100 const auto &p1 = BaseClass::at(i);
101 const auto &p2 = BaseClass::at((i + 1) % BaseClass::size());
102 sum += (p2.x - p1.x) * (p2.y + p1.y);
103 }
104 return sum < 0;
105 #endif
106 }
107
108 template<typename FT>
110 if (BaseClass::size() <= 1)
111 return;
112 auto it = BaseClass::begin();
113 std::reverse(++it, BaseClass::end());
114 }
115
116
117 template<typename FT>
118 inline bool GenericPolygon<FT>::contains(const Vec<2, FT> &p) const {
119 bool inside = false;
120 std::size_t n = BaseClass::size();
121 for (std::size_t i = 0, j = n - 1; i < n; j = i, ++i) {
122 const Vec<2, FT> &u0 = BaseClass::at(i);
123 const Vec<2, FT> &u1 = BaseClass::at(j); // current edge
124
125 if (((u0.y <= p.y) && (p.y < u1.y)) || // U1 is above the ray, U0 is on or below the ray
126 ((u1.y <= p.y) && (p.y < u0.y))) // U0 is above the ray, U1 is on or below the ray
127 {
128 // find x-intersection of current edge with the ray.
129 // Only consider edge crossings on the ray to the right of P.
130 double x = u0.x + (p.y - u0.y) * (u1.x - u0.x) / (u1.y - u0.y);
131 if (x > p.x)
132 inside = !inside;
133 }
134 }
135
136 return inside;
137 }
138
139 template<typename FT>
140 inline bool GenericPolygon<FT>::contains(const GenericPolygon<FT> &plg) const {
141 for (const auto &p : plg) {
142 if (!contains(p))
143 return false;
144 }
145 return true;
146 }
147
148 template<typename FT>
149 inline FT GenericPolygon<FT>::area() const {
150 return std::fabs(signed_area());
151 }
152
153
154 // http://astronomy.swin.edu.au/~pbourke/geometry/polyarea/
155 template<typename FT>
157 FT result = 0;
158 for (unsigned int i = 0; i < BaseClass::size(); i++) {
159 unsigned int j = (i + 1) % BaseClass::size();
160 const Vec<2, FT> &t1 = BaseClass::at(i);
161 const Vec<2, FT> &t2 = BaseClass::at(j);
162 result += t1.x * t2.y - t2.x * t1.y;
163 }
164 result /= 2.0;
165 return result;
166 }
167
168
169 template<typename FT>
172 for (auto p : *this)
173 box.grow(p);
174 return box;
175 }
176
177}
178
179
180#endif // EASY3D_CORE_POLYGON_H
GenericBox represents the bounding box of shapes.
Definition: box.h:47
void grow(const Point &p)
Definition: box.h:216
A 2D polygon representation.
Definition: polygon.h:41
GenericPolygon()=default
Default constructor.
bool is_clockwise() const
Checks if the polygon has a clockwise orientation (right-hand rule).
Definition: polygon.h:94
GenericPolygon(std::size_t size)
Initializes with a know size.
Definition: polygon.h:49
void reverse_orientation()
Reverses the orientation of the polygon. The first vertex (pointed to by p.begin(),...
Definition: polygon.h:109
GenericBox< 2, FT > bbox() const
Returns the smallest axis-aligned bounding box containing this polygon.
Definition: polygon.h:170
FT area() const
Returns the area of this polygon. This is the absolute value of the signed area, so it is always posi...
Definition: polygon.h:149
bool contains(const Vec< 2, FT > &point) const
Tests if a point lies inside the polygon.
Definition: polygon.h:118
FT signed_area() const
Returns the signed area of the polygon. The signed area is positive for counter clockwise polygons an...
Definition: polygon.h:156
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition: vec.h:34
Definition: collider.cpp:182
std::vector< FT > sum(const Matrix< FT > &)
Definition: matrix.h:1454