Spinning Topp Logo BlackTopp Studios
inc
triangulator.h
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 /*
41  -----------------------------------------------------------------------------
42  This source file is part of ogre-procedural
43 
44  For the latest info, see http://code.google.com/p/ogre-procedural/
45 
46  Copyright (c) 2010-2013 Michael Broutin
47 
48  Permission is hereby granted, free of charge, to any person obtaining a copy
49  of this software and associated documentation files (the "Software"), to deal
50  in the Software without restriction, including without limitation the rights
51  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
52  copies of the Software, and to permit persons to whom the Software is
53  furnished to do so, subject to the following conditions:
54 
55  The above copyright notice and this permission notice shall be included in
56  all copies or substantial portions of the Software.
57 
58  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
64  THE SOFTWARE.
65  -----------------------------------------------------------------------------
66  */
67 #ifndef _graphicsproceduraltriangulator_h
68 #define _graphicsproceduraltriangulator_h
69 
70 #include "Graphics/Procedural/proceduraldatatypes.h"
71 #include "Graphics/Procedural/Mesh/meshgenerator.h"
72 
73 #include "linesegment.h"
74 #include "triangle.h"
75 #include "track.h"
76 
77 namespace Mezzanine
78 {
79  namespace Graphics
80  {
81  namespace Procedural
82  {
83  class Shape;
84  class MultiShape;
85 
86  ///////////////////////////////////////////////////////////////////////////////
87  /// @brief A segment of 2 points in a Point2DContainer.
88  /// @details
89  ///////////////////////////////////////
91  {
92  ///////////////////////////////////////////////////////////////////////////////
93  // Public Data Members
94 
95  /// @brief The first index of the segment.
96  /// @note These indexes refer to Vector2's in a Point2DContainer.
98  /// @brief The second index of the segment.
99  /// @note These indexes refer to Vector2's in a Point2DContainer.
101 
102  ///////////////////////////////////////////////////////////////////////////////
103  // Construction and Destruction
104 
105  /// @brief Class constructor.
106  /// @param First The first index of the segment.
107  /// @param Second The second index of the segment.
108  DelaunaySegment(const Integer First, const Integer Second);
109  /// @brief Class destructor.
111 
112  ///////////////////////////////////////////////////////////////////////////////
113  // Utility
114 
115  /// @brief Gets the inverse of this segment.
116  /// @return Returns a copy of this DelaunaySegment with it's indexes reversed.
117  DelaunaySegment Inverse() const;
118  /// @brief Less-than compare operator.
119  /// @param Other The other DelaunaySegment to compare with.
120  /// @return Returns true if this DelaunaySegment is less than the other DelaunaySegment, false otherwise.
121  Boole operator<(const DelaunaySegment& Other) const;
122  };//DelaunaySegment
123 
124  ///////////////////////////////////////////////////////////////////////////////
125  /// @brief A triangle formed from 3 points in a Point2DContainer.
126  /// @details
127  ///////////////////////////////////////
129  {
130  ///////////////////////////////////////////////////////////////////////////////
131  // Public Data Members and Types
132 
133  /// @brief An enum used to describe the proximity of a point to the edge of a Circumcircle generated by the Delaunay Triangulation algorithm.
135  {
136  IT_Inside = 0, ///< Returned when the specified point is inside the circumcircle.
137  IT_Outside = 1, ///< Returned when the specified point is outside the circumcircle.
138  IT_BorderlineOutside = 2 ///< Returned when the specified point is exceedingly close to being outside the circumcircle.
139  };//InsideType
140 
141  /// @brief An array of indexes forming the triangle.
142  /// @note These indexes refer to Vector2's in a Point2DContainer.
144  /// @brief An array of Vector2's that for all the triangles/segments being operated on.
146 
147  ///////////////////////////////////////////////////////////////////////////////
148  // Construction and Destruction
149 
150  /// @brief Class constructor.
151  /// @param List A pointer to the Point2DContainer this triangle resides in.
152  DelaunayTriangle(const Point2DContainer* List);
153  /// @brief Class destructor.
155 
156  ///////////////////////////////////////////////////////////////////////////////
157  // Utility
158 
159  /// @brief Sets the points that form this triangle.
160  /// @param Index0 The first point in the Point2DContainer forming this triangle.
161  /// @param Index1 The second point in the Point2DContainer forming this triangle.
162  /// @param Index2 The third point in the Point2DContainer forming this triangle.
163  void SetVertices(const Integer Index0, const Integer Index1, const Integer Index2);
164  /// @brief Will conditionally re-arrange the vertices of this triangle.
165  void MakeDirectIfNeeded();
166 
167  /// @brief Gets a point in the Point2DContainer by index.
168  /// @param Point The index of the point to retrieve.
169  /// @return Returns the point at the specified index.
170  Vector2 GetPoint(const Integer Point) const;
171  /// @brief Gets the central point of this triangle.
172  /// @return Returns a Vector2 containing the central point of this triangle.
173  Vector2 GetMidPoint() const;
174  /// @brief Gets the index of a segment within this triangle.
175  /// @exception If a valid segment index is not found then an INTERNAL_EXCEPTION will be thrown.
176  /// @param Index0 The first index of the segment to search for.
177  /// @param Index1 The second index of the segment to search for.
178  /// @return Returns 0-2 if a valid segment is found.
179  Integer FindSegNumber(const Integer Index0, const Integer Index1) const;
180  /// @brief Checks to see of the segment specified via Indexes is a part of this triangle.
181  /// @param Index0 The first point of the segment to check for.
182  /// @param Index1 The second point of the segment to check for.
183  /// @return Returns true if the specified segment is a part of this triangle, false otherwise.
184  Boole ContainsSegment(const Integer Index0, const Integer Index1) const;
185  /// @brief Checks to see if a point is inside this triangle.
186  /// @param Point The point to check.
187  /// @return Returns true if the specified point is inside this triangle, false otherwise.
188  Boole IsPointInside(const Vector2& Point) const;
189  /// @brief Checks to see if a point is inside the Circumcircle generated from this triangle.
190  /// @param Point The point to check.
191  /// @return Returns true if the the specified point is inside the Circumcircle generated by the Delaunay Triangulation algorithm from this triangle, false otherwise.
192  InsideType IsPointInsideCircumcircle(const Vector2& Point) const;
193  /// @brief Checks if this triangle is degenerate.
194  /// @return Returns true if this triangle is degenerate, false otherwise.
195  Boole IsDegenerate() const;
196  /// @brief Gets a string description of this triangle.
197  /// @return Returns a String containing a description of this triangle.
198  String GetDebugDescription() const;
199 
200  /// @brief Equality comparison operator.
201  /// @param Other The other DelaunayTriangle to compare with.
202  /// @return Returns true if ths other DelaunayTriangle buffer is equal to this one, false otherwise.
203  Boole operator==(const DelaunayTriangle& Other) const;
204  };//DelaunayTriangle
205 
206  /// @brief A container type for the storage of DelaunayTriangles.
207  typedef std::list<DelaunayTriangle> DelaunayTriangleBuffer;
208 
209  ///////////////////////////////////////////////////////////////////////////////
210  /// @brief A triangle convenience class for some comparison operations.
211  /// @details
212  ///////////////////////////////////////
214  {
215  ///////////////////////////////////////////////////////////////////////////////
216  // Public Data Members
217 
218  /// @brief The first index of the segment.
219  /// @note These indexes refer to Vector2's in a Point2DContainer.
221  /// @brief The second index of the segment.
222  /// @note These indexes refer to Vector2's in a Point2DContainer.
224  /// @brief The third index of the segment.
225  /// @note These indexes refer to Vector2's in a Point2DContainer.
227 
228  ///////////////////////////////////////////////////////////////////////////////
229  // Construction and Destruction
230 
231  /// @brief Class constructor.
232  /// @param First The first index of the triangle.
233  /// @param Second The second index of the triangle.
234  /// @param Third The third index of the triangle.
235  TouchSuperTriangle(const Integer First, const Integer Second, const Integer Third);
236  /// @brief Class destructor.
238 
239  ///////////////////////////////////////////////////////////////////////////////
240  // Utility
241 
242  /// @brief Functor operator.
243  /// @param Tri The triangle to check.
244  /// @return Returns true if this TouchSuperTriangle has any points in common with the specified DelaunayTriangle, false otherwise.
245  Boole operator()(const DelaunayTriangle& Tri);
246  };//SuperTouchTriangle
247 
248  ///////////////////////////////////////////////////////////////////////////////
249  /// @brief A generator class that implements the Delaunay Triangulation algorithm.
250  /// @details This generator works with 2D geometry to generate triangles for 3D meshes.
251  ///////////////////////////////////////
252  class MEZZ_LIB Triangulator : public MeshGenerator<Triangulator>
253  {
254  public:
255  /// @brief Convenience typedef for the storage of segments to be processed by this class.
256  typedef std::vector<LineSegment2D> SegmentContainer;
257  /// @brief Convenience typedef for the storage of delaunay segments to be processed by this class.
258  typedef std::vector<DelaunaySegment> DelaunaySegmentContainer;
259  protected:
260  /// @internal
261  /// @brief A pointer to the Shape to be triangulated.
262  /// @note If this is valid, then the MultiShape and SegmentList pointers will be NULL. Only one can be triangulated.
264  /// @internal
265  /// @brief A pointer to the MultiShape to be triangulated.
266  /// @note If this is valid, then the Shape and SegmentList pointers will be NULL. Only one can be triangulated.
268  /// @internal
269  /// @brief A pointer to a container of segments forming the shape to triangulate.
270  /// @note If this is valid, then the Shape and MultiShape pointers will be NULL. Only one can be triangulated.
271  SegmentContainer* SegmentListToTriangulate;
272  /// @internal
273  /// @brief A pointer to an optional triangle that encompasses all points to be triangulated.
274  /// @note If this is not set, the algorithm will make an educated guess. This is slower than providing one if the information is available.
276  /// @internal
277  /// @brief Stores whether or not the super triangle should be removed at the end of triangulation.
279 
280  /// @internal
281  /// @brief Performs a Delaunay Triangulation on a provided list of points in 2D space and populates a triangle buffer with the results.
282  /// @param List The container of Vector2's to perform the triangulation on.
283  /// @param TriBuf The triangle buffer storing the results of the triangulation.
284  void Delaunay(Point2DContainer& List, DelaunayTriangleBuffer& TriBuf) const;
285  /// @internal
286  /// @brief Forces specific segments to be present in the resulting triangulation.
287  /// @param TriBuf The triangule buffer storing the results of the triangulation.
288  /// @param List The container of Vector2's to perform the triangulation on.
289  /// @param SegmentListIndices A container of indexes to segments that will be forced to exist in the resulting triangulation.
290  void _AddConstraints(DelaunayTriangleBuffer& TriBuf, const Point2DContainer& List, const IndexContainer& SegmentListIndices) const;
291  /// @internal
292  /// @brief Recursively generates a Delaunay triangle from a container of points.
293  /// @param CuttingSeg The segment used to generate the previous delaunay triangle that is a part of the provided available points.
294  /// @param InputPoints The remaining available points to generate a triangle from.
295  /// @param TriBuf The triangule buffer storing the results of the triangulation.
296  /// @param List The container of Vector2's to perform the triangulation on.
297  void _RecursiveTriangulatePolygon(const DelaunaySegment& CuttingSeg, const IndexContainer& InputPoints, DelaunayTriangleBuffer& TriBuf, const Point2DContainer& List) const;
298  public:
299  /// @brief Class constructor.
300  Triangulator();
301  /// @brief Class destructor.
302  virtual ~Triangulator();
303 
304  ///////////////////////////////////////////////////////////////////////////////
305  // Utility
306 
307  /// @copydoc MeshGenerator::AddToTriangleBuffer(TriangleBuffer&) const
308  virtual void AddToTriangleBuffer(TriangleBuffer& Buffer) const;
309  /// @brief Executes the Constrained Delaunay Triangulation algorithm.
310  /// @exception An INVALID_STATE_EXCEPTION will be thrown if either a Shape, MultiShape, or Segment List is not set when this is called.
311  /// @param Indexes A vector of indexes where is outputed the resulting triangle indexes.
312  /// @param Vertices A vector of vertices where is outputed the resulting triangle vertices.
313  void Triangulate(IndexContainer& Indexes, Point2DContainer& Vertices) const;
314 
315  ///////////////////////////////////////////////////////////////////////////////
316  // Configuration
317 
318  /// @brief Sets shape to triangulate.
319  /// @note A Shape, MultiShape, or SegmentList must be available in order to triangulate and only one will be processed. If one gets set, the other two will be NULL'd out.
320  /// @param TriShape The shape that will be triangulated.
321  /// @return Returns a reference to this.
322  Triangulator& SetShapeToTriangulate(const Shape* TriShape);
323  /// @brief Sets multi shape to triangulate.
324  /// @note A Shape, MultiShape, or SegmentList must be available in order to triangulate and only one will be processed. If one gets set, the other two will be NULL'd out.
325  /// @param TriMultiShape The multi-shape that will be triangulated.
326  /// @return Returns a reference to this.
327  Triangulator& SetMultiShapeToTriangulate(const MultiShape* TriMultiShape);
328  /// @brief Sets segment list to triangulate.
329  /// @note A Shape, MultiShape, or SegmentList must be available in order to triangulate and only one will be processed. If one gets set, the other two will be NULL'd out.
330  /// @param Segments A pointer to the container of line segments that will be triangulated.
331  /// @return Returns a reference to this.
332  Triangulator& SetSegmentListToTriangulate(SegmentContainer* Segments);
333  /// @brief Sets manual super triangle.
334  /// @note If this is not set, the algorithm will make an educated guess. This is slower than providing one if the information is available.
335  /// @param Tri The super triangle to manually set for triangulation.
336  /// @return Returns a reference to this.
337  Triangulator& SetManualSuperTriangle(Triangle2D* Tri);
338  /// @brief Sets if the outside of the shape must be removed.
339  /// @param RemoveOutside Whether or not the outside of the shape should be removed during triangulation.
340  /// @return Returns a reference to this.
341  Triangulator& SetRemoveOutside(const Boole Remove);
342  };//Triangulator
343  }//Procedural
344  }//Graphics
345 }//Mezzanine
346 
347 #endif
Boole ContainsSegment(const Integer Index0, const Integer Index1) const
Checks to see of the segment specified via Indexes is a part of this triangle.
Integer Indexes[3]
An array of indexes forming the triangle.
Definition: triangulator.h:143
DelaunaySegment Inverse() const
Gets the inverse of this segment.
A triangle formed from 3 points in a Point2DContainer.
Definition: triangulator.h:128
Triangle2D * ManualSuperTriangle
A pointer to an optional triangle that encompasses all points to be triangulated. ...
Definition: triangulator.h:275
DelaunaySegment(const Integer First, const Integer Second)
Class constructor.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
std::list< DelaunayTriangle > DelaunayTriangleBuffer
A container type for the storage of DelaunayTriangles.
Definition: triangulator.h:207
std::vector< LineSegment2D > SegmentContainer
Convenience typedef for the storage of segments to be processed by this class.
Definition: triangulator.h:256
A geometry math class for expressing a triangle in 2D space.
Definition: triangle.h:53
const Shape * ShapeToTriangulate
A pointer to the Shape to be triangulated.
Definition: triangulator.h:263
A convenience buffer that stores vertices and indices of a mesh to be generated.
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
Boole IsDegenerate() const
Checks if this triangle is degenerate.
SegmentContainer * SegmentListToTriangulate
A pointer to a container of segments forming the shape to triangulate.
Definition: triangulator.h:271
Boole IsPointInside(const Vector2 &Point) const
Checks to see if a point is inside this triangle.
Integer FindSegNumber(const Integer Index0, const Integer Index1) const
Gets the index of a segment within this triangle. a valid segment index is not found then an INTERNAL...
DelaunayTriangle(const Point2DContainer *List)
Class constructor.
std::vector< DelaunaySegment > DelaunaySegmentContainer
Convenience typedef for the storage of delaunay segments to be processed by this class.
Definition: triangulator.h:258
std::vector< Integer > IndexContainer
A container of Integers used to represent the indicies of a shape.
std::vector< Vector2 > Point2DContainer
Basic container type for the storage of 2D points.
Returned when the specified point is inside the circumcircle.
Definition: triangulator.h:136
Returned when the specified point is exceedingly close to being outside the circumcircle.
Definition: triangulator.h:138
This is used to represent a point on a 2 dimentional area, such as a screen.
Definition: vector2.h:63
A segment of 2 points in a Point2DContainer.
Definition: triangulator.h:90
Integer Index1
The first index of the segment.
Definition: triangulator.h:97
Integer Index1
The second index of the segment.
Definition: triangulator.h:223
A base class containing all of the utilities needed for a mesh generator.
Definition: meshgenerator.h:87
const Point2DContainer * Points
An array of Vector2's that for all the triangles/segments being operated on.
Definition: triangulator.h:145
Boole RemoveOutside
Stores whether or not the super triangle should be removed at the end of triangulation.
Definition: triangulator.h:278
A generator class that implements the Delaunay Triangulation algorithm.
Definition: triangulator.h:252
A triangle convenience class for some comparison operations.
Definition: triangulator.h:213
TouchSuperTriangle(const Integer First, const Integer Second, const Integer Third)
Class constructor.
Integer Index0
The first index of the segment.
Definition: triangulator.h:220
String GetDebugDescription() const
Gets a string description of this triangle.
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
InsideType IsPointInsideCircumcircle(const Vector2 &Point) const
Checks to see if a point is inside the Circumcircle generated from this triangle. ...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
InsideType
An enum used to describe the proximity of a point to the edge of a Circumcircle generated by the Dela...
Definition: triangulator.h:134
Integer Index2
The third index of the segment.
Definition: triangulator.h:226
Returned when the specified point is outside the circumcircle.
Definition: triangulator.h:137
Boole operator()(const DelaunayTriangle &Tri)
Functor operator.
Boole operator<(const DelaunaySegment &Other) const
Less-than compare operator.
A collection of interconnected 2D points used to express an arbitrary 2D shape.
Definition: shape.h:95
Vector2 GetMidPoint() const
Gets the central point of this triangle.
Boole operator==(const DelaunayTriangle &Other) const
Equality comparison operator.
A grouping of individual 2D shapes used to express more elaborate shapes.
Definition: multishape.h:87
const MultiShape * MultiShapeToTriangulate
A pointer to the MultiShape to be triangulated.
Definition: triangulator.h:267
void MakeDirectIfNeeded()
Will conditionally re-arrange the vertices of this triangle.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Integer Index2
The second index of the segment.
Definition: triangulator.h:100
void SetVertices(const Integer Index0, const Integer Index1, const Integer Index2)
Sets the points that form this triangle.
Vector2 GetPoint(const Integer Point) const
Gets a point in the Point2DContainer by index.