Cogs.Foundation
Loading...
Searching...
No Matches
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 ()=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 PointListgetPoints () 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.

Member Typedef Documentation

◆ List

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

◆ PointList

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

Constructor & Destructor Documentation

◆ Polygon() [1/3]

Cogs::Geometry::Polygon::Polygon ( )
default

◆ Polygon() [2/3]

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

References setPoints().

◆ Polygon() [3/3]

Cogs::Geometry::Polygon::Polygon ( const Polygon )
default

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. *============================================================================================

◆ addPoint()

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

◆ calcArea()

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

◆ 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. *============================================================================================

◆ clearPoints()

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

◆ fixUp()

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

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

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

◆ getNoOfPoints()

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

◆ getPoint()

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

◆ getPoints()

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

◆ isClockwise()

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

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

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

◆ isPointInside() [1/2]

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

References isPointInside().

Referenced by isPointInside(), and subtract().

◆ 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. *============================================================================================

◆ 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. *============================================================================================

Referenced by Polygon().

◆ 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. *============================================================================================

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.

Member Data Documentation

◆ points

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

Referenced by subtract().


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