Cogs.Core
Public Types | Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
Cogs::Geometry::Polygon Class Reference

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
 

Detailed Description

Generic 2D concave or convex polygon.

Basically a list of 3D points with a constant Z value. Points are stored in a clockwise orientation.

Definition at line 16 of file Polygon.h.

Member Typedef Documentation

◆ List

using Cogs::Geometry::Polygon::List = std::vector<Polygon>

Definition at line 18 of file Polygon.h.

◆ PointList

using Cogs::Geometry::Polygon::PointList = std::vector<glm::vec3>

Definition at line 19 of file Polygon.h.

Constructor & Destructor Documentation

◆ Polygon()

Cogs::Geometry::Polygon::Polygon ( const PointList &  initialPoints)

Definition at line 414 of file Polygon.cpp.

Member Function Documentation

◆ add()

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.

◆ addPoint()

void Cogs::Geometry::Polygon::addPoint ( const glm::vec3 &  point)

Definition at line 431 of file Polygon.cpp.

◆ calcArea()

float Cogs::Geometry::Polygon::calcArea ( ) const
inline

Definition at line 33 of file Polygon.h.

◆ calcSignedArea()

float Cogs::Geometry::Polygon::calcSignedArea ( const PointList::const_iterator &  start,
const PointList::const_iterator &  end 
)
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.

◆ clearPoints()

void Cogs::Geometry::Polygon::clearPoints ( )

Definition at line 498 of file Polygon.cpp.

◆ fixUp()

void Cogs::Geometry::Polygon::fixUp ( )

Ensures winding is clockwise and attempts to unwrap self-intersecting sections.

*============================================================================================

Definition at line 438 of file Polygon.cpp.

◆ getNoOfPoints()

size_t Cogs::Geometry::Polygon::getNoOfPoints ( ) const
inline

Definition at line 32 of file Polygon.h.

◆ getPoint()

const glm::vec3 & Cogs::Geometry::Polygon::getPoint ( size_t  idx) const
inline

Definition at line 31 of file Polygon.h.

◆ getPoints()

const PointList & Cogs::Geometry::Polygon::getPoints ( ) const
inline

Definition at line 30 of file Polygon.h.

◆ isClockwise()

bool Cogs::Geometry::Polygon::isClockwise ( const PointList &  pointList)
staticprivate

Helper function for testing the orientation of the given point list.

*============================================================================================

Definition at line 762 of file Polygon.cpp.

◆ isPointInside() [1/2]

bool Cogs::Geometry::Polygon::isPointInside ( const glm::vec3 &  p) const
inline

Definition at line 34 of file Polygon.h.

◆ isPointInside() [2/2]

bool Cogs::Geometry::Polygon::isPointInside ( const glm::vec3 &  p,
const PointList &  pointList 
)
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.

◆ setPoints()

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.

◆ subtract()

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().

Member Data Documentation

◆ points

PointList Cogs::Geometry::Polygon::points
private

Definition at line 40 of file Polygon.h.


The documentation for this class was generated from the following files: