Delaunator
2D Delaunay Triangulation in C++ with Python wrapper.
 All Classes Namespaces Functions
vertex.h
1 #ifndef VERTEX_H_INCLUDED
2 #define VERTEX_H_INCLUDED
3 
4 
5 
6 /*
7  * LIBRARIES
8  */
9 // LOCAL MODULES
10 #include "commons.h"
11 #include "virtualVertex.h"
12 
13 
14 
15 /*
16  * DEFINES
17  */
18 
19 
20 
21 
22 /*
23  * PREDECLARATIONS
24  */
25 class Edge;
26 class VirtualVertex;
27 
28 
29 
30 
34 struct Coordinates {
35  public:
36  // CONSTRUCTOR
37  Coordinates(const float x = 0., const float y = 0.) {
38  this->setCoord(x, y);
39  }
40  // PUBLIC METHODS
41  float squareDistanceTo(const Coordinates& othr) const {
42  return (this->_x - othr._x)*(this->_x - othr._x)+(this->_y - othr._y)*(this->_y - othr._y);
43  }
44  float distanceTo(const Coordinates& othr) const {
45  return sqrt(
46  (this->_x - othr._x)*(this->_x - othr._x)+(this->_y - othr._y)*(this->_y - othr._y)
47  );
48  }
49  // ACCESSORS
50  float x() const { return this->_x; }
51  float y() const { return this->_y; }
52  void setX(float x) { this->_x = round_float(x, EPSILON*10.); }
53  void setY(float y) { this->_y = round_float(y, EPSILON*10.); }
54  void setCoord(float x, float y) { this->setX(x); this->setY(y); }
55  // OPERATORS
56  bool operator==(const Coordinates& othr) {
57  return (fabs(othr._x - this->_x) < EPSILON) && (fabs(othr._y - this->_y) < EPSILON);
58  }
59  bool operator!=(const Coordinates& othr) {
60  return not (*this == othr);
61  }
62  Coordinates operator+(const Coordinates& c) {
63  return Coordinates(this->_x + c._x, this->_y + c._y);
64  }
65  Coordinates operator-(const Coordinates& c) {
66  return Coordinates(this->_x - c._x, this->_y - c._y);
67  }
68  Coordinates operator/(const float value) {
69  return Coordinates(this->_x / value, this->_y / value);
70  }
71  Coordinates operator+=(const Coordinates& c) {
72  return *this + c;
73  }
74  private:
75  // ATTRIBUTES
76  float _x, _y;
77 };
78 
79 
83 class Vertex : public Coordinates {
84  public:
85  // CONSTRUCTOR
86  Vertex(const float = 0., const float = 0., Edge* = NULL);
87  Vertex(const Coordinates c, Edge* = NULL);
88  ~Vertex();
89  // PUBLIC METHODS
90  unsigned int neighbourCount() const;
91  void take(VirtualVertex*, Vertex* = NULL);
93  void forget(VirtualVertex*);
94  // ACCESSORS
95  unsigned int getID() const { return this->id; }
96  unsigned int getObjectCount() const { return this->objects.size(); }
97  std::list<VirtualVertex*> getObjects(unsigned int = 0) const;
99  Edge* getEdge() const;
100  void setEdge(Edge* e);
101  // PREDICATS
102  bool isNeighbourOf(Vertex*) const;
103  bool isACorner() const;
104  bool have(VirtualVertex*) const;
105  private:
106  // ATTRIBUTES
107  Edge* edge; // edge has this Vertex as origin.
108  unsigned int id;
109  static unsigned int last_id;
110  std::list<VirtualVertex*> objects;
111 };
112 
113 
114 
115 #if !SWIG
116 
117 // EXTERNAL METHODS
118  std::ostream& operator<<(std::ostream&, Coordinates const &);
119  std::ostream& operator<<(std::ostream&, Vertex const &);
120 
121 
122 
123 // EXTERNAL TYPES
129  public:
130  // CONSTRUCTOR
131  VertexComparison(const Coordinates c) : reference(c) {}
132  // PREDICATS
133  bool operator()(const Vertex* left, const Vertex* right) {
134  // STL structures wait generally for the strict weak order (<).
135  // In a case of a priority_queue, its for found the greater object.
136  // But we want to minimize distance to reference, so we use > operator instead of <.
137  return left->squareDistanceTo(this->reference) > right->squareDistanceTo(this->reference);
138  }
139  private:
140  // ATTRIBUTES
141  const Coordinates reference;
142 };
143 
148 struct VertexHash {
149  inline size_t operator()(const Vertex* o) const { return (size_t)o; }
150 };
154 typedef std::priority_queue<const Vertex*, std::vector<Vertex*>, VertexComparison> vertex_comparator;
155 
156 
157 #endif
158 
159 
160 
161 
162 #endif