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

inet::Polygon::Polygon ( )
inline
39 {}
inet::Polygon::Polygon ( const std::vector< Coord > &  points)
25 {
26  if (points.size() < 3)
27  throw cRuntimeError("A Euclidean polygon has at least three points");
28  this->points = points;
29 }
std::vector< Coord > points
Definition: Polygon.h:33

Member Function Documentation

bool inet::Polygon::computeIntersection ( const LineSegment lineSegment,
Coord intersection1,
Coord intersection2,
Coord normal1,
Coord normal2 
) const
virtual
84 {
85  // Note: based on http://geomalgorithms.com/a13-_intersect-4.html
86  Coord p0 = lineSegment.getPoint1();
87  Coord p1 = lineSegment.getPoint2();
88  if (p0 == p1)
89  {
90  normal1 = normal2 = Coord::NIL;
91  return false;
92  }
93  Coord segmentDirection = p1 - p0;
94  Coord polygonNormal = getNormalUnitVector();
95  // The segment is not in the polygon's plane
96  // The length of the intersection segment will be 0
97  if (polygonNormal * segmentDirection != 0 || (p0 - points[0]) * polygonNormal != 0)
98  return false;
99  double tE = 0;
100  double tL = 1;
101  unsigned int pointSize = points.size();
102  for (unsigned int i = 0; i < pointSize; i++)
103  {
104  Coord normalVec = getEdgeOutwardNormalVector(points[i], points[(i+1) % pointSize]);
105  double N = normalVec * (points[i] - p0);
106  double D = normalVec * segmentDirection;
107  if (D < 0)
108  {
109  double t = N / D;
110  if (t > tE)
111  {
112  tE = t;
113  normal1 = normalVec;
114  if (tE > tL)
115  return false;
116  }
117  }
118  else if (D > 0)
119  {
120  double t = N / D;
121  if (t < tL)
122  {
123  tL = t;
124  normal2 = normalVec;
125  if (tL < tE)
126  return false;
127  }
128  }
129  else
130  {
131  if (N < 0)
132  return false;
133  }
134  }
135  if (tE == 0)
136  normal1 = Coord::NIL;
137  if (tL == 1)
138  normal2 = Coord::NIL;
139  intersection1 = p0 + segmentDirection * tE;
140  intersection2 = p0 + segmentDirection * tL;
141  if (intersection1 == intersection2)
142  {
143  normal1 = normal2 = Coord::NIL;
144  return false;
145  }
146  return true;
147 }
static const Coord NIL
Constant with all values set to 0.
Definition: Coord.h:40
Coord getEdgeOutwardNormalVector(const Coord &edgeP1, const Coord &edgeP2) const
Definition: Polygon.cc:71
compose< m, compose< kg, pow< s,-2 > > > N
Definition: Units.h:767
Coord getNormalUnitVector() const
Definition: Polygon.cc:40
std::vector< Coord > points
Definition: Polygon.h:33
Coord inet::Polygon::computeSize ( ) const
virtual
61 {
62  Coord min;
63  Coord max;
64  for (const auto & elem : points) {
65  min = min.min(elem);
66  max = max.max(elem);
67  }
68  return max - min;
69 }
double min(const double a, const double b)
Returns the minimum of a and b.
Definition: SCTPAssociation.h:270
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
std::vector< Coord > points
Definition: Polygon.h:33
Coord inet::Polygon::getEdgeOutwardNormalVector ( const Coord edgeP1,
const Coord edgeP2 
) const
protected

Referenced by computeIntersection().

72 {
73  Coord polygonNormal = getNormalUnitVector();
74  Coord vectorA = edgeP1 - polygonNormal;
75  Coord vectorB = edgeP2 - polygonNormal;
76  Coord vectorC(vectorA.y * vectorB.z - vectorA.z * vectorB.y,
77  vectorA.z * vectorB.x - vectorA.x * vectorB.z,
78  vectorA.x * vectorB.y - vectorA.y * vectorB.x);
79  // The projection of a vector image v onto a plane with unit normal vector n is: p = v - (v*n)*n.
80  return vectorC - polygonNormal * (vectorC * polygonNormal);
81 }
Coord getNormalUnitVector() const
Definition: Polygon.cc:40
Coord inet::Polygon::getNormalUnitVector ( ) const

Referenced by computeIntersection(), inet::Prism::genereateFaces(), and getEdgeOutwardNormalVector().

41 {
42  Coord normalVec = getNormalVector();
43  return normalVec / normalVec.length();
44 }
Coord getNormalVector() const
Definition: Polygon.cc:47
Coord inet::Polygon::getNormalVector ( ) const

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

48 {
49  Coord point1 = points[0];
50  Coord point2 = points[1];
51  Coord point3 = points[2];
52  Coord vectorA = point2 - point1;
53  Coord vectorB = point3 - point1;
54  Coord vectorC(vectorA.y * vectorB.z - vectorA.z * vectorB.y,
55  vectorA.z * vectorB.x - vectorA.x * vectorB.z,
56  vectorA.x * vectorB.y - vectorA.y * vectorB.x);
57  return vectorC;
58 }
std::vector< Coord > points
Definition: Polygon.h:33
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.

44 { return this == &NIL; }
static const Polygon NIL
Definition: Polygon.h:30
bool inet::Polygon::isUnspecified ( ) const
overridevirtual

Returns true if this geometric object is not completely specified.

Implements inet::GeometricObjectBase.

32 {
33  for (const auto & elem : points) {
34  if ((elem).isUnspecified())
35  return true;
36  }
37  return false;
38 }
std::vector< Coord > points
Definition: Polygon.h:33
virtual bool isUnspecified() const override
Returns true if this geometric object is not completely specified.
Definition: Polygon.cc:31

Member Data Documentation

const Polygon inet::Polygon::NIL = Polygon()
static
std::vector<Coord> inet::Polygon::points
protected

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