aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/common/include/ApexIsoMesh.h
blob: 86ba81698a25fe8f0a9aa0b0292510cc5d8f7f8a (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * 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.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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 OWNER 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.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.



#ifndef APEX_ISO_MESH_H
#define APEX_ISO_MESH_H

#include "ApexUsingNamespace.h"
#include "ApexUsingNamespace.h"
#include "PsUserAllocated.h"
#include "PsArray.h"
#include "PxBounds3.h"

namespace nvidia
{
namespace apex
{

class IProgressListener;

class ApexIsoMesh : public UserAllocated
{
public:
	ApexIsoMesh(uint32_t isoGridSubdivision, uint32_t keepNBiggestMeshes, bool discardInnerMeshes);
	~ApexIsoMesh();

	void setBound(const PxBounds3& bound);
	void clear();
	void clearTemp();
	void addTriangle(const PxVec3& v0, const PxVec3& v1, const PxVec3& v2);
	bool update(IProgressListener* progress);


	uint32_t getNumVertices() const
	{
		return mIsoVertices.size();
	}
	const PxVec3& getVertex(uint32_t index) const
	{
		PX_ASSERT(index < mIsoVertices.size());
		return mIsoVertices[index];
	}

	uint32_t getNumTriangles() const
	{
		return mIsoTriangles.size();
	}
	void getTriangle(uint32_t index, uint32_t& v0, uint32_t& v1, uint32_t& v2) const;
private:
	// settable parameters
	uint32_t mIsoGridSubdivision;
	uint32_t mKeepNBiggestMeshes;
	bool  mDiscardInnerMeshes;
	PxBounds3 mBound;

	bool generateMesh(IProgressListener* progress);
	bool interpolate(float d0, float d1, const PxVec3& pos0, const PxVec3& pos1, PxVec3& pos);
	bool findNeighbors(IProgressListener* progress);
	void removeLayers();
	uint32_t floodFill(uint32_t triangleNr, uint32_t groupNr);

	void removeTrisAndVerts();

	// non-settable parameters (deducted from the ones you can set)
	float mCellSize;
	float mThickness;
	PxVec3 mOrigin;
	int32_t mNumX, mNumY, mNumZ;
	const float mIsoValue;


	struct IsoCell
	{
		void init()
		{
			density = 0.0f;
			vertNrX = -1;
			vertNrY = -1;
			vertNrZ = -1;
			firstTriangle = -1;
			numTriangles = 0;
		}
		float density;
		int32_t vertNrX;
		int32_t vertNrY;
		int32_t vertNrZ;
		int32_t firstTriangle;
		int32_t numTriangles;
	};
	physx::Array<IsoCell> mGrid;
	inline IsoCell& cellAt(int xi, int yi, int zi)
	{
		uint32_t index = (uint32_t)(((xi * mNumY) + yi) * mNumZ + zi);
		PX_ASSERT(index < mGrid.size());
		return mGrid[index];
	}


	struct IsoTriangle
	{
		void init()
		{
			vertexNr[0] = -1;
			vertexNr[1] = -1;
			vertexNr[2] = -1;
			adjTriangles[0] = -1;
			adjTriangles[1] = -1;
			adjTriangles[2] = -1;
			groupNr = -1;
			deleted = false;
		}
		void set(int32_t v0, int32_t v1, int32_t v2, int32_t cubeX, int32_t cubeY, int32_t cubeZ)
		{
			init();
			vertexNr[0] = v0;
			vertexNr[1] = v1;
			vertexNr[2] = v2;
			this->cubeX = cubeX;
			this->cubeY = cubeY;
			this->cubeZ = cubeZ;
		}
		void addNeighbor(int32_t triangleNr)
		{
			if (adjTriangles[0] == -1)
			{
				adjTriangles[0] = triangleNr;
			}
			else if (adjTriangles[1] == -1)
			{
				adjTriangles[1] = triangleNr;
			}
			else if (adjTriangles[2] == -1)
			{
				adjTriangles[2] = triangleNr;
			}
		}

		int32_t vertexNr[3];
		int32_t cubeX, cubeY, cubeZ;
		int32_t adjTriangles[3];
		int32_t groupNr;
		bool deleted;
	};

	struct IsoEdge
	{
		void set(int newV0, int newV1, int newTriangle)
		{
			if (newV0 < newV1)
			{
				v0 = newV0;
				v1 = newV1;
			}
			else
			{
				v0 = newV1;
				v1 = newV0;
			}
			triangleNr = newTriangle;
		}
		PX_INLINE bool operator < (const IsoEdge& e) const
		{
			if (v0 < e.v0)
			{
				return true;
			}
			if (v0 > e.v0)
			{
				return false;
			}
			return (v1 < e.v1);
		}

		PX_INLINE bool operator()(const IsoEdge& e1, const IsoEdge& e2) const
		{
			return e1 < e2;
		}

		PX_INLINE bool operator == (const IsoEdge& e) const
		{
			return v0 == e.v0 && v1 == e.v1;
		}

		int v0, v1;
		int triangleNr;
	};

	physx::Array<PxVec3> mIsoVertices;
	physx::Array<IsoTriangle> mIsoTriangles;
	physx::Array<IsoEdge> mIsoEdges;

	// evil, should not be used
	ApexIsoMesh& operator=(const ApexIsoMesh&);
};

}
} // end namespace nvidia::apex

#endif // APEX_ISO_MESH_H