INET Framework for OMNeT++/OMNEST
inet::Polygon Class Reference

#include <Polygon.h>

Inheritance diagram for inet::Polygon:
inet::GeometricObjectBase

Public Member Functions

 Polygon ()
 
 Polygon (const std::vector< Coord > &points)
 
const std::vector< Coord > & getPoints () const
 
virtual bool isNil () const override
 Returns true if this geometric object is the same as the unspecified singleton instance of this type. More...
 
virtual bool isUnspecified () const override
 Returns true if this geometric object is not completely specified. More...
 
virtual Coord computeSize () const
 
Coord getNormalUnitVector () const
 
Coord getNormalVector () const
 
virtual bool computeIntersection (const LineSegment &lineSegment, Coord &intersection1, Coord &intersection2, Coord &normal1, Coord &normal2) const
 
- Public Member Functions inherited from inet::GeometricObjectBase
 GeometricObjectBase ()
 
virtual ~GeometricObjectBase ()
 

Static Public Attributes

static const Polygon NIL = Polygon()
 

Protected Member Functions

Coord getEdgeOutwardNormalVector (const Coord &edgeP1, const Coord &edgeP2) const
 

Protected Attributes

std::vector< Coordpoints
 

Constructor & Destructor Documentation

◆ Polygon() [1/2]

inet::Polygon::Polygon ( )
inline
29 {}

◆ Polygon() [2/2]

inet::Polygon::Polygon ( const std::vector< Coord > &  points)
15 {
16  if (points.size() < 3)
17  throw cRuntimeError("A Euclidean polygon has at least three points");
18  this->points = points;
19 }

Member Function Documentation

◆ computeIntersection()

bool inet::Polygon::computeIntersection ( const LineSegment lineSegment,
Coord intersection1,
Coord intersection2,
Coord normal1,
Coord normal2 
) const
virtual
73 {
74  // Note: based on http://geomalgorithms.com/a13-_intersect-4.html
75  Coord p0 = lineSegment.getPoint1();
76  Coord p1 = lineSegment.getPoint2();
77  if (p0 == p1) {
78  normal1 = normal2 = Coord::NIL;
79  return false;
80  }
81  Coord segmentDirection = p1 - p0;
82  Coord polygonNormal = getNormalUnitVector();
83  // The segment is not in the polygon's plane
84  // The length of the intersection segment will be 0
85  if (polygonNormal * segmentDirection != 0 || (p0 - points[0]) * polygonNormal != 0)
86  return false;
87  double tE = 0;
88  double tL = 1;
89  unsigned int pointSize = points.size();
90  for (unsigned int i = 0; i < pointSize; i++) {
91  Coord normalVec = getEdgeOutwardNormalVector(points[i], points[(i + 1) % pointSize]);
92  double N = normalVec * (points[i] - p0);
93  double D = normalVec * segmentDirection;
94  if (D < 0) {
95  double t = N / D;
96  if (t > tE) {
97  tE = t;
98  normal1 = normalVec;
99  if (tE > tL)
100  return false;
101  }
102  }
103  else if (D > 0) {
104  double t = N / D;
105  if (t < tL) {
106  tL = t;
107  normal2 = normalVec;
108  if (tL < tE)
109  return false;
110  }
111  }
112  else {
113  if (N < 0)
114  return false;
115  }
116  }
117  if (tE == 0)
118  normal1 = Coord::NIL;
119  if (tL == 1)
120  normal2 = Coord::NIL;
121  intersection1 = p0 + segmentDirection * tE;
122  intersection2 = p0 + segmentDirection * tL;
123  if (intersection1 == intersection2) {
124  normal1 = normal2 = Coord::NIL;
125  return false;
126  }
127  return true;
128 }

◆ computeSize()

Coord inet::Polygon::computeSize ( ) const
virtual
50 {
51  Coord min;
52  Coord max;
53  for (const auto& elem : points) {
54  min = min.min(elem);
55  max = max.max(elem);
56  }
57  return max - min;
58 }

◆ getEdgeOutwardNormalVector()

Coord inet::Polygon::getEdgeOutwardNormalVector ( const Coord edgeP1,
const Coord edgeP2 
) const
protected
61 {
62  Coord polygonNormal = getNormalUnitVector();
63  Coord vectorA = edgeP1 - polygonNormal;
64  Coord vectorB = edgeP2 - polygonNormal;
65  Coord vectorC(vectorA.y * vectorB.z - vectorA.z * vectorB.y,
66  vectorA.z * vectorB.x - vectorA.x * vectorB.z,
67  vectorA.x * vectorB.y - vectorA.y * vectorB.x);
68  // The projection of a vector image v onto a plane with unit normal vector n is: p = v - (v*n)*n.
69  return vectorC - polygonNormal * (vectorC * polygonNormal);
70 }

Referenced by computeIntersection().

◆ getNormalUnitVector()

Coord inet::Polygon::getNormalUnitVector ( ) const
31 {
32  Coord normalVec = getNormalVector();
33  return normalVec / normalVec.length();
34 }

Referenced by computeIntersection(), and getEdgeOutwardNormalVector().

◆ getNormalVector()

Coord inet::Polygon::getNormalVector ( ) const
37 {
38  Coord point1 = points[0];
39  Coord point2 = points[1];
40  Coord point3 = points[2];
41  Coord vectorA = point2 - point1;
42  Coord vectorB = point3 - point1;
43  Coord vectorC(vectorA.y * vectorB.z - vectorA.z * vectorB.y,
44  vectorA.z * vectorB.x - vectorA.x * vectorB.z,
45  vectorA.x * vectorB.y - vectorA.y * vectorB.x);
46  return vectorC;
47 }

Referenced by inet::Prism::computeOutwardNormalVector(), and getNormalUnitVector().

◆ getPoints()

◆ isNil()

virtual bool inet::Polygon::isNil ( ) const
inlineoverridevirtual

Returns true if this geometric object is the same as the unspecified singleton instance of this type.

Implements inet::GeometricObjectBase.

34 { return this == &NIL; }

◆ isUnspecified()

bool inet::Polygon::isUnspecified ( ) const
overridevirtual

Returns true if this geometric object is not completely specified.

Implements inet::GeometricObjectBase.

22 {
23  for (const auto& elem : points) {
24  if ((elem).isUnspecified())
25  return true;
26  }
27  return false;
28 }

Member Data Documentation

◆ NIL

const Polygon inet::Polygon::NIL = Polygon()
static

◆ points

std::vector<Coord> inet::Polygon::points
protected

The documentation for this class was generated from the following files:
inet::Polygon::NIL
static const Polygon NIL
Definition: Polygon.h:20
inet::sctp::min
double min(const double a, const double b)
Returns the minimum of a and b.
Definition: SctpAssociation.h:261
inet::units::units::N
compose< m, compose< kg, pow< s, -2 > > > N
Definition: Units.h:936
inet::Polygon::points
std::vector< Coord > points
Definition: Polygon.h:23
inet::Polygon::isUnspecified
virtual bool isUnspecified() const override
Returns true if this geometric object is not completely specified.
Definition: Polygon.cc:21
inet::Polygon::getEdgeOutwardNormalVector
Coord getEdgeOutwardNormalVector(const Coord &edgeP1, const Coord &edgeP2) const
Definition: Polygon.cc:60
inet::Polygon::getNormalVector
Coord getNormalVector() const
Definition: Polygon.cc:36
inet::sctp::max
double max(const double a, const double b)
Returns the maximum of a and b.
Definition: SctpAssociation.h:266
inet::Coord::NIL
static const Coord NIL
Constant with all values set to 0.
Definition: Coord.h:26
inet::Polygon::getNormalUnitVector
Coord getNormalUnitVector() const
Definition: Polygon.cc:30