Cogs.Core
|
Generic 2D concave or convex polygon. More...
#include <Polygon.h>
Public Types | |
using | List = std::vector< Polygon > |
using | PointList = std::vector< glm::vec3 > |
Public Member Functions | |
Polygon (const PointList &initialPoints) | |
Polygon (const Polygon &)=default | |
void | setPoints (const PointList &pointList) |
Assigns a new set of points to this polygon. | |
void | addPoint (const glm::vec3 &point) |
void | clearPoints () |
void | fixUp () |
Ensures winding is clockwise and attempts to unwrap self-intersecting sections. | |
const PointList & | getPoints () const |
const glm::vec3 & | getPoint (size_t idx) const |
size_t | getNoOfPoints () const |
float | calcArea () const |
bool | isPointInside (const glm::vec3 &p) const |
List | add (const Polygon &polygon) const |
Adds the specified polygon to this one. | |
List | subtract (const Polygon &polygon) const |
Subtracts the specified polygon from this one. | |
Static Private Member Functions | |
static bool | isClockwise (const PointList &pointList) |
Helper function for testing the orientation of the given point list. | |
static bool | isPointInside (const glm::vec3 &p, const PointList &pointList) |
Determines whether the specific point is inside the polygon defined by the list of points. | |
static float | calcSignedArea (const PointList::const_iterator &start, const PointList::const_iterator &end) |
Calculates the signed area of the given list of points. | |
Private Attributes | |
PointList | points |
Generic 2D concave or convex polygon.
Basically a list of 3D points with a constant Z value. Points are stored in a clockwise orientation.
using Cogs::Geometry::Polygon::PointList = std::vector<glm::vec3> |
Cogs::Geometry::Polygon::Polygon | ( | const PointList & | initialPoints | ) |
Definition at line 414 of file Polygon.cpp.
Cogs::Geometry::Polygon::List Cogs::Geometry::Polygon::add | ( | const Polygon & | polygon | ) | const |
Adds the specified polygon to this one.
The output will be a list of polygons that represent the merged area(s) after the operation. This list should contain one or two entries. *============================================================================================
Definition at line 508 of file Polygon.cpp.
void Cogs::Geometry::Polygon::addPoint | ( | const glm::vec3 & | point | ) |
Definition at line 431 of file Polygon.cpp.
|
staticprivate |
Calculates the signed area of the given list of points.
The signed area will be less than zero if the points are orientated clockwise, and greater than zero if the orientation is counter-clockwise. *============================================================================================
Definition at line 807 of file Polygon.cpp.
void Cogs::Geometry::Polygon::clearPoints | ( | ) |
Definition at line 498 of file Polygon.cpp.
void Cogs::Geometry::Polygon::fixUp | ( | ) |
Ensures winding is clockwise and attempts to unwrap self-intersecting sections.
*============================================================================================
Definition at line 438 of file Polygon.cpp.
|
inline |
|
inline |
|
inline |
|
staticprivate |
Helper function for testing the orientation of the given point list.
*============================================================================================
Definition at line 762 of file Polygon.cpp.
|
inline |
|
staticprivate |
Determines whether the specific point is inside the polygon defined by the list of points.
The test counts the number of polygon edges that intersect a line parallel to the x-axis from the start point. *============================================================================================
Definition at line 772 of file Polygon.cpp.
void Cogs::Geometry::Polygon::setPoints | ( | const PointList & | pointList | ) |
Assigns a new set of points to this polygon.
The incoming points will be changed to clockwise orientation if necessary. *============================================================================================
Definition at line 423 of file Polygon.cpp.
Cogs::Geometry::Polygon::List Cogs::Geometry::Polygon::subtract | ( | const Polygon & | polygon | ) | const |
Subtracts the specified polygon from this one.
The output will be a list of polygons that represent the resulting area(s) after the operation.
NOTE: This code is not working 100%. It seems to be fine in about 95% of cases, but it can and will fail when encountering some inputs. I just can't afford to spend anymore time on it right now. The code to handle fully contained or fully clipped polygons is working, and I think the intersection point generation is working fine (but it's possible there are still problems there). The code to handle partially intersecting polygons below is most probably at fault. Specifically it's failing to detect at least one instance of cutting a section out of a single polygon - which might be due to that intersection straddling the ends of the vector. Most probably if I need to revisit this in the future I will probably rewrite this part first. *============================================================================================
Definition at line 528 of file Polygon.cpp.
References Cogs::Geometry::findClosestVertices(), Cogs::Geometry::findMatchingVertex(), Cogs::Geometry::findNextIntersection(), Cogs::Geometry::generateIntersectionPoints(), and Cogs::Geometry::getVertexLocation().