aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/shared/internal/include/authoring/ApexCSG.h
blob: 983f5f3095297dc70919d62b740bc0004d9ea05d (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//
// 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_CSG_H
#define APEX_CSG_H


#include "ApexUsingNamespace.h"
#include "RenderMeshAsset.h"

#ifndef WITHOUT_APEX_AUTHORING

namespace ApexCSG
{

class UserRandom
{
public:
	virtual	uint32_t	getInt() = 0;
	virtual float	getReal(float min, float max) = 0;
};


struct BSPBuildParameters
{
	/*
		Used for searching splitting planes.
		If NULL, a default random # generator will be used.
	 */
	UserRandom*		rnd;

	/*
		Mesh pre-processing.  The mesh is initially scaled to fit in a unit cube, then (if gridSize is not
		zero), the vertices of the scaled mesh are snapped to a grid of size 1/gridSize.
		A power of two is recommended.
		Default value = 65536.
	*/
	uint32_t	snapGridSize;

	/*
		At each step in the tree building process, the surface with maximum triangle area is compared
		to the other surface triangle areas.  If the maximum area surface is far from the "typical" set of
		surface areas, then that surface is chosen as the next splitting plane.  Otherwise, a random
		test set is chosen and a winner determined based upon the weightings below.
		The value logAreaSigmaThreshold determines how "atypical" the maximum area surface must be to
		be chosen in this manner.
		Default value = 2.0.
	 */
	float	logAreaSigmaThreshold;

	/*
		Larger values of testSetSize may find better BSP trees, but will take more time to create.
		testSetSize = 0 is treated as infinity (all surfaces will be tested for each branch).
	 */
	uint32_t	testSetSize;

	/*
		How much to weigh the relative number of triangle splits when searching for a BSP surface.
	 */
	float	splitWeight;

	/*
		How much to weigh the relative triangle imbalance when searching for a BSP surface.
	 */
	float	imbalanceWeight;

	/*
		The BSP representation of the mesh will be transformed from the space of the mesh input into IApexBSP::fromMesh
		using this transform.  By default, this is the identity transformation.  If the user wishes to use a
		different transformation, it may be set using internalTransform.  However, note that when combining
		BSPs using the IApexBSP::combine function, the two BSPs should use the same internal transform.  If they don't,
		the resulting behavior is not specified.  When a mesh is created using IApexBSP::toMesh, the inverse
		of the internal transform is applied to put the mesh back into the original space.

		A special value for internalTransform is the zero 4x4 matrix.  If this is used, an internal transform
		will be calculated in the IApexBSP::fromMesh function.  This may be read using IApexBSP::getInternalTransform(),
		and applied when creating other BSPs which are to be used in combine operations.
	 */
	physx::PxMat44	internalTransform;

	/*
		If false, the triangles associated with this BSP will not be kept.  The BSP may be used for CSG, but will
		not provide any mesh data.

		Default = true
	 */
	bool			keepTriangles;

	BSPBuildParameters()
	{
		setToDefault();
	}

	void	setToDefault()
	{
		rnd = NULL;
		snapGridSize = 65536;
		logAreaSigmaThreshold = (float)2.0;
		testSetSize = 10;
		splitWeight = (float)0.5;
		imbalanceWeight = 0;
		internalTransform = physx::PxMat44(physx::PxIdentity);
		keepTriangles = true;
	}
};

struct BSPTolerances
{
	/*
		A unitless value (relative to mesh size) used to determine mesh triangle coplanarity during BSP building.
		Default value = 1.0e-6.
	 */
	float	linear;

	/*
		A threshold angle (in radians) used to determine mesh triangle coplanarity during BSP building.
		Default value = 1.0e-5.
	 */
	float	angular;

	/*
		A unitless value (relative to mesh size) used to determine triangle splitting during BSP building.
		Default value = 1.0e-9.
	 */
	float	base;

	/*
		A unitless value (relative to mesh size) used to determine a skin width for mesh clipping against BSP
		nodes during mesh creation from the BSP.
		Default value = 1.0e-13.
	 */
	float	clip;

	/*
		Mesh postprocessing.  A unitless value (relative to mesh size) used to determine merge tolerances for
		mesh clean-up after triangles have been clipped to BSP leaves.  A value of 0.0 disables this feature.
		Default value = 1.0e-6.
	 */
	float	cleaning;

	BSPTolerances()
	{
		setToDefault();
	}

	void	setToDefault()
	{
		linear = (float)1.0e-6;
		angular = (float)1.0e-5;
		base = (float)1.0e-9;
		clip = (float)1.0e-13;
		cleaning = (float)1.0e-6;
	}
};

extern BSPTolerances gDefaultTolerances;

struct Operation
{
	enum Enum
	{
		Empty_Set				= 0x0,	// constant
		All_Space				= 0x1,	// constant
		Set_A					= 0x2,	// unary
		Set_A_Complement		= 0x3,	// unary
		Set_B					= 0x4,	// unary
		Set_B_Complement		= 0x5,	// unary
		Exclusive_Or			= 0x6,
		Equivalent				= 0x7,
		Intersection			= 0x8,
		Intersection_Complement	= 0x9,
		A_Minus_B				= 0xA,
		A_Implies_B				= 0xB,
		B_Minus_A				= 0xC,
		B_Implies_A				= 0xD,
		Union					= 0xE,
		Union_Complement		= 0xF,

		NOP						= 0x80000000	// no op
	};
};


struct BSPVisualizationFlags
{
	enum Enum
	{
		OutsideRegions	= (1 << 0),
		InsideRegions	= (1 << 1),

		SingleRegion	= (1 << 16)
	};
};


struct BSPType
{
	enum Enum
	{
		Empty_Set,	// BSP has a single node, which is an outside leaf.  Therefore the inside region is the empty set.
		All_Space,	// BSP has a single node, which is an inside leaf.  Therefore the inside region is all of space.
		Nontrivial,	// BSP has more than a single node.
		Combined,	// BSP is the combination of two BSPs, ready for a CSG operation to define a single BSP.

		BSPTypeCount
	};
};


/*
	Memory cache for BSP construction.  Not global, so that concurrent calculations can use different pools.
 */
class IApexBSPMemCache
{
public:

	/*
		Deallocate all memory buffers.
	 */
	virtual void	clearAll() = 0;

	/*
		Deallocate only temporary data buffers.
	 */
	virtual void	clearTemp() = 0;

	/*
		Clean up.
	 */
	virtual	void	release() = 0;

protected:

	IApexBSPMemCache()	{}
	virtual			~IApexBSPMemCache()	{}
};


/*
	BSP interface.

	Convert a mesh into a BSP, perform boolean operations between BSPs, and extract the resulting mesh.
 */

class IApexBSP
{
public:
	/*
		Set the tolerances used for various aspects of BSP creation, merging, mesh creation, etc.
		Default values are those in BSPTolerances.
	*/
	virtual void			setTolerances(const BSPTolerances& tolerances) = 0;

	/*
		Construct a BSP from the given mesh, using the given parameters.
	 */
	virtual bool			fromMesh(const nvidia::ExplicitRenderTriangle* mesh, uint32_t meshSize, const BSPBuildParameters& params, nvidia::IProgressListener* progressListener = NULL, volatile bool* cancel = NULL) = 0;

	/*
		Construct a BSP from a convex polyhedron defined by a list of planes.
		See the definition of internalTransform in BSPBuildParameters.  The same meaning applies here.
		The mesh array is optional.  If included, the single internal leaf created will be associated with these triangles.
	 */
	virtual bool			fromConvexPolyhedron(const physx::PxPlane* poly, uint32_t polySize, const physx::PxMat44& internalTransform = physx::PxMat44(physx::PxIdentity), const nvidia::ExplicitRenderTriangle* mesh = NULL, uint32_t meshSize = 0) = 0;

	/*
		Build a combination of two BSPs (this and the passed-in bsp), upon which boolean operations of the two can be performed.
	 */
	virtual bool			combine(const IApexBSP& bsp) = 0;

	/*
		Build a BSP resulting from a boolean operation upon a combination.
		Note: you may do this "in place," i.e.
			bsp.op( bsp, operation );
		... in this case, bsp will no longer be a combined BSP.
	 */
	virtual bool			op(const IApexBSP& combinedBSP, Operation::Enum operation) = 0;

	/*
		This BSP is changed to its complement (inside <-> outside)
	 */
	virtual bool			complement() = 0;

	/*
		The transform from mesh space to BSP space.  This may be used in the BSPBuildParameters passed into fromMesh,
		in order to match the transform used for a combining mesh.
	 */
	virtual physx::PxMat44	getInternalTransform() const = 0;

	/*
		Returns an enum characterizing the BSP.  See BSPType.
	 */
	virtual BSPType::Enum	getType() const = 0;

	/*
		Returns the total surface area and volume of the regions designated to be on the given side.
		If this is a combined BSP, then you must provide a merge operation.  In this case,
		the BSP will not actually be merged, but the resulting area will be that of the
		merged BSP you would get if you did perform the merge with the op() function.
		If this is not a combined BSP and you provide a merge operation, it will be ignored.

		If there the volume or area of one of the leaves in consideration is infinite, then this function returns false.  Otherwise it returns true.
	 */
	virtual bool			getSurfaceAreaAndVolume(float& area, float& volume, bool inside, Operation::Enum operation = Operation::NOP) const = 0;

	/*
		Determines if given point is in an outside or inside leaf.
		If this is a combined BSP, then you must provide a merge operation.  In this case,
		the BSP will not actually be merged, but the resulting area will be that of the
		merged BSP you would get if you did perform the merge with the op() function.
		If this is not a combined BSP and you provide a merge operation, it will be ignored.
	*/
	virtual bool			pointInside(const physx::PxVec3& point, Operation::Enum operation = Operation::NOP) const = 0;

	/*
		Construct a mesh from the current BSP.
	 */
	virtual bool			toMesh(physx::Array<nvidia::ExplicitRenderTriangle>& mesh) const = 0;

	/*
		Deep copy of given bsp.
		Input bsp may be the same as *this.
		The transform tm will be applied.
		If the internalTransform given is not zero, it will become the new internal transform.  The mesh will be scaled internally appropriately with the given tm.
		A combined BSP may be copied.
	 */
	virtual	void			copy(const IApexBSP& bsp, const physx::PxMat44& tm = physx::PxMat44(physx::PxIdentity), const physx::PxMat44& internalTransform = physx::PxMat44(physx::PxZero)) = 0;

	/*
		Decompose into disjoint islands.
		This BSP is not affected.
		The BSP is split into a set of BSPs, each representing one connected island.
		The set of BSPs is returned as the first BSP in the list, with access
		to the remainder of the list  through the getNext() and getPrev() functions.
		The BSP must be not be a combined BSP (getType() != BSPType::Combined).
		Returns this if the BSP is already an island.
		Returns NULL if the operation fails (e.g. this is a combined BSP).
	*/
	virtual IApexBSP*		decomposeIntoIslands() const = 0;

	/**
		Utility to replace the submesh on a set of interior triangles.
	*/
	virtual	void			replaceInteriorSubmeshes(uint32_t frameCount, uint32_t* frameIndices, uint32_t submeshIndex) = 0;

	/*
		Deletes the triangles associated with this BSP.  The BSP may be used for CSG, but will not provide any mesh data.
	*/
	virtual void			deleteTriangles() = 0;

	/*
		If a BSP has been decomposed into islands, getNext() and getPrev() will iterate through the
		BSPs in the decomposition.  NULL is returned if an attempt is made to iterate past
		the beginning or end of the list.
	*/
	virtual IApexBSP*		getNext() const = 0;
	virtual IApexBSP*		getPrev() const = 0;

	/*
		Serialization.
	 */
	virtual void			serialize(physx::PxFileBuf& stream) const = 0;
	virtual void			deserialize(physx::PxFileBuf& stream) = 0;

	/*
		Visualization.
		Set flags to bits from BSPVisualizationFlags::Enum.
	 */
	virtual void			visualize(nvidia::RenderDebugInterface& debugRender, uint32_t flags, uint32_t index = 0) const = 0;

	/*
		Clean up.
	 */
	virtual	void			release() = 0;

protected:

	IApexBSP()	{}
	virtual					~IApexBSP()	{}
};


// CSG Tools API

// Create a BSP memory cache to share among several BSPs
IApexBSPMemCache*
createBSPMemCache();

// Instantiate a BSP.  If cache = NULL, the BSP will create and own its own cache.
IApexBSP*
createBSP(IApexBSPMemCache* memCache = NULL, const physx::PxMat44& internalTransform = physx::PxMat44(physx::PxIdentity));

};	// namespace ApexCSG

#endif

#endif // #ifndef APEX_CSG_H