Easy3D 2.6.1
Loading...
Searching...
No Matches
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#include <easy3d/core/box.h>
36
37
38namespace easy3d {
39
42 template<typename FT>
43 class GenericPolygon : public std::vector< Vec<2, FT> > {
44 typedef std::vector< Vec<2, FT> > BaseClass;
45
46 public:
48 GenericPolygon() = default;
49
51 explicit GenericPolygon(std::size_t size) { BaseClass::resize(size); }
52
54 template<class InputIterator>
55 GenericPolygon(InputIterator first, InputIterator last);
56
63 bool is_clockwise() const;
64
68
70 bool contains(const Vec<2, FT> &point) const;
71
73 bool contains(const GenericPolygon<FT> &plg) const;
74
77 FT signed_area() const;
78
81 FT area() const;
82
85 };
86
88
89 template<typename FT>
90 template<class InputIterator>
91 inline GenericPolygon<FT>::GenericPolygon(InputIterator first, InputIterator last) {
92 std::copy(first, last, BaseClass::end());
93 }
94
95 template<typename FT>
97 #if 0
98 return signed_area() < 0;
99 #else
100 FT sum = FT(0);
101 for (std::size_t i = 0; i + 1 < BaseClass::size(); ++i) {
102 const auto &p1 = BaseClass::at(i);
103 const auto &p2 = BaseClass::at((i + 1) % BaseClass::size());
104 sum += (p2.x - p1.x) * (p2.y + p1.y);
105 }
106 return sum < 0;
107 #endif
108 }
109
110 template<typename FT>
112 if (BaseClass::size() <= 1)
113 return;
114 auto it = BaseClass::begin();
115 std::reverse(++it, BaseClass::end());
116 }
117
118
119 template<typename FT>
120 inline bool GenericPolygon<FT>::contains(const Vec<2, FT> &p) const {
121 bool inside = false;
122 std::size_t n = BaseClass::size();
123 for (std::size_t i = 0, j = n - 1; i < n; j = i, ++i) {
124 const Vec<2, FT> &u0 = BaseClass::at(i);
125 const Vec<2, FT> &u1 = BaseClass::at(j); // current edge
126
127 if (((u0.y <= p.y) && (p.y < u1.y)) || // U1 is above the ray, U0 is on or below the ray
128 ((u1.y <= p.y) && (p.y < u0.y))) // U0 is above the ray, U1 is on or below the ray
129 {
130 // find x-intersection of current edge with the ray.
131 // Only consider edge crossings on the ray to the right of P.
132 double x = u0.x + (p.y - u0.y) * (u1.x - u0.x) / (u1.y - u0.y);
133 if (x > p.x)
134 inside = !inside;
135 }
136 }
137
138 return inside;
139 }
140
141 template<typename FT>
142 inline bool GenericPolygon<FT>::contains(const GenericPolygon<FT> &plg) const {
143 for (const auto &p : plg) {
144 if (!contains(p))
145 return false;
146 }
147 return true;
148 }
149
150 template<typename FT>
151 inline FT GenericPolygon<FT>::area() const {
152 return std::fabs(signed_area());
153 }
154
155
156 // http://astronomy.swin.edu.au/~pbourke/geometry/polyarea/
157 template<typename FT>
159 FT result = 0;
160 for (unsigned int i = 0; i < BaseClass::size(); i++) {
161 unsigned int j = (i + 1) % BaseClass::size();
162 const Vec<2, FT> &t1 = BaseClass::at(i);
163 const Vec<2, FT> &t2 = BaseClass::at(j);
164 result += t1.x * t2.y - t2.x * t1.y;
165 }
166 result /= 2.0;
167 return result;
168 }
169
170
171 template<typename FT>
174 for (auto p : *this)
175 box.grow(p);
176 return box;
177 }
178
179}
180
181
182#endif // EASY3D_CORE_POLYGON_H
GenericBox represents the bounding box of shapes.
Definition box.h:45
void grow(const Point &p)
Add a point to this box. This will compute its new extent.
Definition box.h:276
GenericPolygon()=default
Default constructor.
bool contains(const GenericPolygon< FT > &plg) const
Tests if a polygon plg lies inside this polygon (i.e., all its vertices are inside).
Definition polygon.h:142
bool is_clockwise() const
Checks if the polygon has a clockwise orientation (right-hand rule).
Definition polygon.h:96
GenericPolygon(std::size_t size)
Initializes with a know size.
Definition polygon.h:51
void reverse_orientation()
Reverses the orientation of the polygon. The first vertex (pointed to by p.begin(),...
Definition polygon.h:111
GenericBox< 2, FT > bbox() const
Returns the smallest axis-aligned bounding box containing this polygon.
Definition polygon.h:172
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:151
GenericPolygon(InputIterator first, InputIterator last)
Initializes from a known range.
Definition polygon.h:91
bool contains(const Vec< 2, FT > &point) const
Tests if a point lies inside the polygon.
Definition polygon.h:120
FT signed_area() const
Returns the signed area of the polygon. The signed area is positive for counter clockwise polygons an...
Definition polygon.h:158
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
Definition collider.cpp:182
std::vector< FT > sum(const Matrix< FT > &)
Definition matrix.h:1485