aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/shared/general/HACD/include/dgConvexHull3d.h
blob: 80a89679be081016751de46a4bbb84134b1fb2ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
* 
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* 
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef __DG_CONVEXHULL_3D__
#define __DG_CONVEXHULL_3D__

#include "dgTypes.h"
#include "dgList.h"
#include "dgArray.h"
#include "dgPlane.h"
#include "dgVector.h"
#include "dgMatrix.h"
#include "dgQuaternion.h"

class dgAABBPointTree3d;

class dgConvexHull3DFace
{
	public:
	dgConvexHull3DFace();
	int32_t m_index[3]; 
	
	void SetMark(int32_t mark) 
	{
		m_mark = mark;
	}

	int32_t GetMark() const 
	{
		return m_mark;
	}
	dgList<dgConvexHull3DFace>::dgListNode* GetTwin(int32_t index) const { return m_twin[index];}

	private:
	double Evalue (const dgBigVector* const pointArray, const dgBigVector& point) const;
	dgBigPlane GetPlaneEquation (const dgBigVector* const pointArray) const;

	int32_t m_mark;
	dgList<dgConvexHull3DFace>::dgListNode* m_twin[3];
	friend class dgConvexHull3d;
};

class dgHullVertex;

class dgConvexHull3d: public dgList<dgConvexHull3DFace>, public UANS::UserAllocated
{
	public:
	dgConvexHull3d(const dgConvexHull3d& source);
	dgConvexHull3d(const double* const vertexCloud, int32_t strideInBytes, int32_t count, double distTol, int32_t maxVertexCount = 0x7fffffff);
	virtual ~dgConvexHull3d();

	int32_t GetVertexCount() const;
	const dgBigVector* GetVertexPool() const;
	const dgBigVector& GetVertex(int32_t i) const;

	double GetDiagonal() const;
	void GetAABB (dgBigVector& boxP0, dgBigVector& boxP1) const;
	double RayCast (const dgBigVector& localP0, const dgBigVector& localP1) const;
	void CalculateVolumeAndSurfaceArea (double& volume, double& surcafeArea) const;

	protected:
	dgConvexHull3d(void);
	void BuildHull (const double* const vertexCloud, int32_t strideInBytes, int32_t count, double distTol, int32_t maxVertexCount);

	virtual dgListNode* AddFace (int32_t i0, int32_t i1, int32_t i2);
	virtual void DeleteFace (dgListNode* const node) ;
	virtual int32_t InitVertexArray(dgHullVertex* const points, const double* const vertexCloud, int32_t strideInBytes, int32_t count, void* const memoryPool, int32_t maxMemSize);

	void CalculateConvexHull (dgAABBPointTree3d* vertexTree, dgHullVertex* const points, int32_t count, double distTol, int32_t maxVertexCount);
	int32_t BuildNormalList (dgBigVector* const normalArray) const;
	int32_t SupportVertex (dgAABBPointTree3d** const tree, const dgHullVertex* const points, const dgBigVector& dir) const;
	double TetrahedrumVolume (const dgBigVector& p0, const dgBigVector& p1, const dgBigVector& p2, const dgBigVector& p3) const;
	void TessellateTriangle (int32_t level, const dgVector& p0, const dgVector& p1, const dgVector& p2, int32_t& count, dgBigVector* const ouput, int32_t& start) const;

	dgAABBPointTree3d* BuildTree (dgAABBPointTree3d* const parent, dgHullVertex* const points, int32_t count, int32_t baseIndex, int8_t** const memoryPool, int32_t& maxMemSize) const;
	static int32_t ConvexCompareVertex(const dgHullVertex* const  A, const dgHullVertex* const B, void* const context);
	bool Sanity() const;

	int32_t m_count;
	double m_diag;
	dgBigVector m_aabbP0;
	dgBigVector m_aabbP1;
	dgArray<dgBigVector> m_points;
};


inline int32_t dgConvexHull3d::GetVertexCount() const
{
	return m_count;
}

inline const dgBigVector* dgConvexHull3d::GetVertexPool() const
{
	return &m_points[0];
}

inline const dgBigVector& dgConvexHull3d::GetVertex(int32_t index) const
{
	return m_points[index];
}

inline double dgConvexHull3d::GetDiagonal() const
{
	return m_diag;
}


inline void dgConvexHull3d::GetAABB (dgBigVector& boxP0, dgBigVector& boxP1) const
{
	boxP0 = m_aabbP0;
	boxP1 = m_aabbP1;
}

#endif