Developer Documentation
Triangulator.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 #ifndef ACG_TRIANGULATOR_HH
43 #define ACG_TRIANGULATOR_HH
44 
45 
46 
47 #include <ACG/Math/VectorT.hh>
48 #include <ACG/Config/ACGDefines.hh>
49 
50 #include <vector>
51 #include <list>
52 
53 
54 namespace ACG{
55 
56 
57 
58 class ACGDLLEXPORT Triangulator
59 {
60 public:
65  explicit Triangulator(const std::vector<Vec3f>& _pos);
66 
69  virtual ~Triangulator();
70 
74  size_t numTriangles() const { return numTris_; }
75 
81  int index(int _i) const { return tris_[_i]; }
82 
87  const std::vector<int>& indices() const { return tris_; }
88 
93  bool convex() const { return convex_; }
94 
100  size_t numReflexVertices() const { return numReflexVertices_; }
101 
108  bool isReflexVertex(int _i) const;
109 
114  bool success() const { return ok_; }
115 
116 
117 private:
118 
119  void initVertexList();
120 
121  // ear clipping algorithm in O(n^2)
122  int earClippingN2();
123 
124  // ear clipping algorithm in O(n^3)
125  int earClippingN3();
126 
127 
128  // disable implicit assignment operator
129  Triangulator& operator=(const Triangulator&) = delete;
130 
131 
132  float triangleAreaSign(const Vec2f& v0, const Vec2f& v1, const Vec2f& v2) const;
133  float distancePointToSegmentSq(const Vec2f& v0, const Vec2f& v1, const Vec2f& pt) const;
134 
135  // test if vertex v1 is reflex in triangle v0-v1-v2 (inner angle at v1 is greater than 180 deg)
136  bool isReflexVertex(const Vec2f& v0, const Vec2f& v1, const Vec2f& v2) const;
137 
138 
139 
140  bool pointInTriangle(const Vec2f& v0, const Vec2f& v1, const Vec2f& v2, const Vec2f& pt) const;
141 
142 
143  struct RingVertex
144  {
145  RingVertex() {}
146 
147  RingVertex(int i, bool r, const Vec2f& x, RingVertex* p, RingVertex* n)
148  : id(i), reflex(r), pos(x), prev(p), next(n) { }
149 
150  int id;
151  bool reflex;
152  Vec2f pos;
153 
154  RingVertex* prev;
155  RingVertex* next;
156  };
157 
158 
159  bool updateReflexVertex(RingVertex* v);
160  void addEar(RingVertex* _earTip);
161 
162 
163  const size_t polySize_;
164  size_t numRemaningVertices_;
165  size_t numTris_;
166  size_t numReflexVertices_;
167 
168  bool ok_;
169  bool convex_;
170 
171 
172  std::vector<Vec2f> pos_;
173 
174  std::vector<RingVertex> vertices_;
175  std::list<RingVertex*> reflexVertices_;
176 
177  std::vector<int> tris_;
178 
179 };
180 
181 
182 
183 
184 //=============================================================================
185 }
186 
187 
188 //=============================================================================
189 #endif // ACG_TRIANGULATOR_HH defined
190 //=============================================================================
size_t numReflexVertices() const
Get number of reflex vertices.
Namespace providing different geometric functions concerning angles.
bool success() const
Check if the triangulation was successful.
int index(int _i) const
Get local vertex index.
Definition: Triangulator.hh:81
bool convex() const
Is the polygon convex?
Definition: Triangulator.hh:93
const std::vector< int > & indices() const
Get local index buffer.
Definition: Triangulator.hh:87
size_t numTriangles() const
Get number of triangles.
Definition: Triangulator.hh:74