27#ifndef EASY3D_CORE_POLYGON_H
28#define EASY3D_CORE_POLYGON_H
34#include <easy3d/core/vec.h>
42 typedef std::vector< Vec<2, FT> > BaseClass;
52 template<
class InputIterator>
88 template<
class InputIterator>
90 std::copy(first, last, BaseClass::end());
96 return signed_area() < 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);
108 template<
typename FT>
110 if (BaseClass::size() <= 1)
112 auto it = BaseClass::begin();
113 std::reverse(++it, BaseClass::end());
117 template<
typename FT>
120 std::size_t n = BaseClass::size();
121 for (std::size_t i = 0, j = n - 1; i < n; j = i, ++i) {
125 if (((u0.y <= p.y) && (p.y < u1.y)) ||
126 ((u1.y <= p.y) && (p.y < u0.y)))
130 double x = u0.x + (p.y - u0.y) * (u1.x - u0.x) / (u1.y - u0.y);
139 template<
typename FT>
141 for (
const auto &p : plg) {
148 template<
typename FT>
150 return std::fabs(signed_area());
155 template<
typename FT>
158 for (
unsigned int i = 0; i < BaseClass::size(); i++) {
159 unsigned int j = (i + 1) % BaseClass::size();
162 result += t1.x * t2.y - t2.x * t1.y;
169 template<
typename FT>
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