Cogs.Foundation
|
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 ()=default | |
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::List = std::vector<Polygon> |
using Cogs::Geometry::Polygon::PointList = std::vector<glm::vec3> |
|
default |
Cogs::Geometry::Polygon::Polygon | ( | const PointList & | initialPoints | ) |
References setPoints().
|
default |
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. *============================================================================================
void Cogs::Geometry::Polygon::addPoint | ( | const glm::vec3 & | point | ) |
|
inline |
|
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. *============================================================================================
void Cogs::Geometry::Polygon::clearPoints | ( | ) |
void Cogs::Geometry::Polygon::fixUp | ( | ) |
Ensures winding is clockwise and attempts to unwrap self-intersecting sections.
*============================================================================================
|
inline |
|
inline |
|
inline |
|
staticprivate |
Helper function for testing the orientation of the given point list.
*============================================================================================
|
inline |
References isPointInside().
Referenced by isPointInside(), and subtract().
|
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. *============================================================================================
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. *============================================================================================
Referenced by Polygon().
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. *============================================================================================
References Cogs::Geometry::findClosestVertices(), Cogs::Geometry::findMatchingVertex(), Cogs::Geometry::findNextIntersection(), Cogs::Geometry::generateIntersectionPoints(), Cogs::Geometry::getVertexLocation(), Cogs::Geometry::Inside, isPointInside(), Cogs::Geometry::Vertex::None, Cogs::Geometry::Outside, and points.
|
private |
Referenced by subtract().