1 /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
2  All rights reserved.
3 
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 
9  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 
11  3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  */
15 #pragma once
16 #ifndef HACD_ICHULL_H
17 #define HACD_ICHULL_H
18 #include "hacdVersion.h"
19 #include "hacdManifoldMesh.h"
20 #include "hacdVector.h"
21 #include <vector>
22 #include <map>
23 namespace HACD
24 {
25 class DPoint;
26 class HACD;
27 //!	Incremental Convex Hull algorithm (cf. http://maven.smith.edu/~orourke/books/ftp.html ).
28 enum ICHullError
29 {
30 	ICHullErrorOK = 0,
31 	ICHullErrorCoplanarPoints,
32 	ICHullErrorNoVolume,
33 	ICHullErrorInconsistent,
34 	ICHullErrorNotEnoughPoints
35 };
36 class ICHull
37 {
38 public:
39 	//!
IsFlat()40 	bool IsFlat() { return m_isFlat; }
41 	//!
GetDistPoints()42 	std::map<long, DPoint> *GetDistPoints() const { return m_distPoints; }
43 	//!
SetDistPoints(std::map<long,DPoint> * distPoints)44 	void SetDistPoints(std::map<long, DPoint> *distPoints) { m_distPoints = distPoints; }
45 	//! Returns the computed mesh
GetMesh()46 	TMMesh &GetMesh() { return m_mesh; }
47 	//!	Add one point to the convex-hull
AddPoint(const Vec3<Real> & point)48 	bool AddPoint(const Vec3<Real> &point) { return AddPoints(&point, 1); }
49 	//!	Add one point to the convex-hull
50 	bool AddPoint(const Vec3<Real> &point, long id);
51 	//!	Add points to the convex-hull
52 	bool AddPoints(const Vec3<Real> *points, size_t nPoints);
53 	bool AddPoints(std::vector<Vec3<Real> > points);
54 	//!
55 	ICHullError Process();
56 	//!
57 	ICHullError Process(unsigned long nPointsCH);
58 	//!
59 	double ComputeVolume();
60 	//!
61 	bool IsInside(const Vec3<Real> &pt0);
62 	//!
63 	double ComputeDistance(long name, const Vec3<Real> &pt, const Vec3<Real> &normal, bool &insideHull, bool updateIncidentPoints);
64 	//!
65 	const ICHull &operator=(ICHull &rhs);
66 
67 	//!	Constructor
68 	ICHull(void);
69 	//! Destructor
~ICHull(void)70 	virtual ~ICHull(void){};
71 
72 private:
73 	//!	DoubleTriangle builds the initial double triangle.  It first finds 3 noncollinear points and makes two faces out of them, in opposite order. It then finds a fourth point that is not coplanar with that face.  The vertices are stored in the face structure in counterclockwise order so that the volume between the face and the point is negative. Lastly, the 3 newfaces to the fourth point are constructed and the data structures are cleaned up.
74 	ICHullError DoubleTriangle();
75 	//!	MakeFace creates a new face structure from three vertices (in ccw order).  It returns a pointer to the face.
76 	CircularListElement<TMMTriangle> *MakeFace(CircularListElement<TMMVertex> *v0,
77 											   CircularListElement<TMMVertex> *v1,
78 											   CircularListElement<TMMVertex> *v2,
79 											   CircularListElement<TMMTriangle> *fold);
80 	//!
81 	CircularListElement<TMMTriangle> *MakeConeFace(CircularListElement<TMMEdge> *e, CircularListElement<TMMVertex> *v);
82 	//!
83 	bool ProcessPoint();
84 	//!
85 	bool ComputePointVolume(double &totalVolume, bool markVisibleFaces);
86 	//!
87 	bool FindMaxVolumePoint();
88 	//!
89 	bool CleanEdges();
90 	//!
91 	bool CleanVertices(unsigned long &addedPoints);
92 	//!
93 	bool CleanTriangles();
94 	//!
95 	bool CleanUp(unsigned long &addedPoints);
96 	//!
97 	bool MakeCCW(CircularListElement<TMMTriangle> *f,
98 				 CircularListElement<TMMEdge> *e,
99 				 CircularListElement<TMMVertex> *v);
100 	void Clear();
101 
102 private:
103 	static const long sc_dummyIndex;
104 	static const double sc_distMin;
105 	TMMesh m_mesh;
106 	std::vector<CircularListElement<TMMEdge> *> m_edgesToDelete;
107 	std::vector<CircularListElement<TMMEdge> *> m_edgesToUpdate;
108 	std::vector<CircularListElement<TMMTriangle> *> m_trianglesToDelete;
109 	std::map<long, DPoint> *m_distPoints;
110 	CircularListElement<TMMVertex> *m_dummyVertex;
111 	Vec3<Real> m_normal;
112 	bool m_isFlat;
113 
114 	ICHull(const ICHull &rhs);
115 
116 	friend class HACD;
117 };
118 
119 }  // namespace HACD
120 #endif
121