File size: 10,642 Bytes
0c51b93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2013-2017 NVIDIA Corporation. All rights reserved.

#include "../include/NvFlexExt.h"

#include "../core/cloth.h"

namespace
{
	struct Key
	{
		Key(int i, float d) : index(i), depth(d) {}

		int index;
		float depth;
		
		bool operator < (const Key& rhs) const { return depth < rhs.depth; }
	};
}

int NvFlexExtCreateWeldedMeshIndices(const float* vertices, int numVertices, int* uniqueIndices, int* originalToUniqueMap, float threshold)
{
	memset(originalToUniqueMap, -1, numVertices*sizeof(int));

	const Vec3* positions = (const Vec3*)vertices;

	// use a sweep and prune style search to accelerate neighbor finding
	std::vector<Key> keys;
	for (int i=0; i < numVertices; i++)
		keys.push_back(Key(i, positions[i].z));

	std::sort(keys.begin(), keys.end());

	int uniqueCount = 0;

	// sweep keys to find matching verts
	for (int i=0; i < numVertices; ++i)
	{
		// we are a duplicate, skip
		if (originalToUniqueMap[keys[i].index] != -1)
			continue;

		// scan forward until no vertex can be closer than threshold
		for (int j=i+1; j < numVertices && (keys[j].depth-keys[i].depth) <= threshold; ++j)
		{
			float distance = Length(Vector3(positions[keys[i].index])-Vector3(positions[keys[j].index]));

			if (distance <= threshold)
				originalToUniqueMap[keys[j].index] = uniqueCount;
		}

		originalToUniqueMap[keys[i].index] = uniqueCount;

		uniqueIndices[uniqueCount++] = keys[i].index;
	}

	return uniqueCount;
}

NvFlexExtAsset* NvFlexExtCreateClothFromMesh(const float* particles, int numVertices, const int* indices, int numTriangles, float stretchStiffness, float bendStiffness, float tetherStiffness, float tetherGive, float pressure)
{
	NvFlexExtAsset* asset = new NvFlexExtAsset();
	memset(asset, 0, sizeof(*asset));

	asset->particles = new float[numVertices*4];
	memcpy(asset->particles, particles, numVertices*sizeof(float)*4);	

	asset->triangleIndices = new int[numTriangles*3];
	memcpy(asset->triangleIndices, indices, numTriangles*3*sizeof(int));

	asset->numParticles = numVertices;
	asset->maxParticles = numVertices;

	asset->numTriangles = numTriangles;

	// create cloth mesh
	ClothMesh cloth((Vec4*)particles, numVertices, indices, numTriangles*3, stretchStiffness, bendStiffness, true);

	if (cloth.mValid)
	{
		// create tethers
		if (tetherStiffness > 0.0f)
		{
			std::vector<int> anchors;
			anchors.reserve(numVertices);

			// find anchors
			for (int i=0; i < numVertices; ++i)
			{
				Vec4& particle = ((Vec4*)particles)[i];

				if (particle.w == 0.0f)
					anchors.push_back(i);
			}

			if (anchors.size())
			{
				// create tethers
				for (int i=0; i < numVertices; ++i)
				{
					Vec4& particle = ((Vec4*)particles)[i];
					if (particle.w == 0.0f)
						continue;

					float minSqrDist = FLT_MAX;
					int minIndex = -1;

					// find the closest attachment point
					for (int a=0; a < int(anchors.size()); ++a)
					{
						Vec4& attachment = ((Vec4*)particles)[anchors[a]];

						float distSqr = LengthSq(Vec3(particle)-Vec3(attachment));
						if (distSqr < minSqrDist)
						{
							minSqrDist = distSqr;
							minIndex = anchors[a];
						}
					}

					// add a tether
					if (minIndex != -1)
					{						
						cloth.mConstraintIndices.push_back(i);
						cloth.mConstraintIndices.push_back(minIndex);
						cloth.mConstraintRestLengths.push_back(sqrtf(minSqrDist)*(1.0f + tetherGive));						
						
						// negative stiffness indicates tether (unilateral constraint)
						cloth.mConstraintCoefficients.push_back(-tetherStiffness);
					}
				}
			}
		}

		const int numSprings = int(cloth.mConstraintCoefficients.size());

		asset->springIndices = new int[numSprings*2];
		asset->springCoefficients = new float[numSprings];
		asset->springRestLengths = new float[numSprings];
		asset->numSprings = numSprings;

		for (int i=0; i < numSprings; ++i)
		{
			asset->springIndices[i*2+0] = cloth.mConstraintIndices[i*2+0];
			asset->springIndices[i*2+1] = cloth.mConstraintIndices[i*2+1];			
			asset->springRestLengths[i] = cloth.mConstraintRestLengths[i];
			asset->springCoefficients[i] = cloth.mConstraintCoefficients[i];
		}

		if (pressure > 0.0f)
		{
			asset->inflatable = true;
			asset->inflatableVolume = cloth.mRestVolume;
			asset->inflatableStiffness = cloth.mConstraintScale;
			asset->inflatablePressure = pressure;
		}
	}
	else
	{
		NvFlexExtDestroyAsset(asset);
		return NULL;
	}

	return asset;
}

struct FlexExtTearingClothAsset : public NvFlexExtAsset
{
	ClothMesh* mMesh;
};

NvFlexExtAsset* NvFlexExtCreateTearingClothFromMesh(const float* particles, int numParticles, int maxParticles, const int* indices, int numTriangles, float stretchStiffness, float bendStiffness, float pressure)
{
	FlexExtTearingClothAsset* asset = new FlexExtTearingClothAsset();
	memset(asset, 0, sizeof(*asset));

	asset->particles = new float[maxParticles*4];
	memcpy(asset->particles, particles, numParticles*sizeof(float)*4);	

	asset->triangleIndices = new int[numTriangles*3];
	memcpy(asset->triangleIndices, indices, numTriangles*3*sizeof(int));

	asset->numParticles = numParticles;
	asset->maxParticles = maxParticles;

	asset->numTriangles = numTriangles;
	
	// create and store cloth mesh
	asset->mMesh = new ClothMesh((Vec4*)particles, numParticles, indices, numTriangles*3, stretchStiffness, bendStiffness, true);
	
	ClothMesh& cloth = *asset->mMesh;

	if (cloth.mValid)
	{
		const int numSprings = int(cloth.mConstraintCoefficients.size());

		// asset references cloth mesh memory directly
		asset->springIndices = &cloth.mConstraintIndices[0];
		asset->springCoefficients = &cloth.mConstraintCoefficients[0];
		asset->springRestLengths = &cloth.mConstraintRestLengths[0];
		asset->numSprings = numSprings;

		if (pressure > 0.0f)
		{
			asset->inflatable = true;
			asset->inflatableVolume = cloth.mRestVolume;
			asset->inflatableStiffness = cloth.mConstraintScale;
			asset->inflatablePressure = pressure;
		}
	}
	else
	{
		NvFlexExtDestroyAsset(asset);
		return NULL;
	}

	return asset;

}

void NvFlexExtDestroyTearingCloth(NvFlexExtAsset* asset)
{
	FlexExtTearingClothAsset* tearable = (FlexExtTearingClothAsset*)asset;

	delete[] asset->particles;
	delete[] asset->triangleIndices;

	delete tearable->mMesh;
	delete tearable;
}

void NvFlexExtTearClothMesh(NvFlexExtAsset* asset, float maxStrain, int maxSplits, NvFlexExtTearingParticleClone* particleCopies, int* numParticleCopies,  int maxCopies, NvFlexExtTearingMeshEdit* triangleEdits, int* numTriangleEdits, int maxEdits) 
{
	FlexExtTearingClothAsset* tearable = (FlexExtTearingClothAsset*)asset;
	
	std::vector<ClothMesh::TriangleUpdate> edits;
	std::vector<ClothMesh::VertexCopy> copies;

	int splits = 0;

	maxCopies = Min(maxCopies, tearable->maxParticles-tearable->numParticles);

	// iterate over all edges and tear if beyond maximum strain
	for (int i=0; i < tearable->numSprings && int(copies.size()) < maxCopies && splits < maxSplits; ++i)
	{
		int a = tearable->springIndices[i*2+0];
		int b = tearable->springIndices[i*2+1];

		Vec3 p = Vec3(&tearable->particles[a*4]);
		Vec3 q = Vec3(&tearable->particles[b*4]);

		// check strain and break if greater than max threshold
		if (Length(p-q) > tearable->springRestLengths[i]*maxStrain)
		{
			// skip fixed particles
			if (Vec4(&tearable->particles[a*4]).w == 0.0f)
				continue;

			if (Vec4(&tearable->particles[b*4]).w == 0.0f)
				continue;

			// choose vertex of edge to split
			const int splitIndex = Randf() > 0.5f ? a : b;
			const Vec3 splitPlane = Normalize(p-q);	// todo: use plane perpendicular to normal and edge..

			std::vector<int> adjacentTriangles;
			std::vector<int> adjacentVertices;

			const int newIndex = tearable->mMesh->SplitVertex((Vec4*)tearable->particles, splitIndex, splitPlane, adjacentTriangles, adjacentVertices, edits, copies, maxCopies-int(copies.size()));

			if (newIndex != -1)
			{
				++splits;

				// separate each adjacent vertex if it is now singular
				for (int s=0; s < int(adjacentVertices.size()); ++s)
				{
					const int adjacentVertex = adjacentVertices[s];

					tearable->mMesh->SeparateVertex(adjacentVertex, edits, copies, maxCopies-int(copies.size()));
				}

				// also test the new vertex which can become singular
				tearable->mMesh->SeparateVertex(newIndex, edits, copies, maxCopies-int(copies.size()));
			}
		}
	}

	// update asset particle count
	tearable->numParticles = tearable->mMesh->mNumVertices;

	// output copies
	for (int c=0; c < int(copies.size()); ++c)
	{
		NvFlexExtTearingParticleClone clone;
		clone.srcIndex = copies[c].srcIndex;
		clone.destIndex = copies[c].destIndex;

		particleCopies[c] = clone;
	}

	// output mesh edits, note that some edits will not be reported if edit buffer is not big enough
	const int numEdits = Min(int(edits.size()), maxEdits);

	for (int u=0; u < numEdits; ++u)
	{
		NvFlexExtTearingMeshEdit update;
		update.triIndex = edits[u].triangle;
		update.newParticleIndex = edits[u].vertex;

		tearable->triangleIndices[update.triIndex] = update.newParticleIndex;

		triangleEdits[u] = update;
	}


	*numTriangleEdits = numEdits;
	*numParticleCopies = int(copies.size());
}