aboutsummaryrefslogtreecommitdiff
path: root/sdk/extensions/authoring/source/NvBlastExtAuthoringMeshNoiser.h
blob: 4fced5ade38e34f1cbb62a71a7c5f04f2dbf52bb (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
// 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) 2016-2018 NVIDIA Corporation. All rights reserved.


#ifndef NVBLASTEXTAUTHORINGMESHNOISER_H
#define NVBLASTEXTAUTHORINGMESHNOISER_H
#include <vector>
#include <map>
#include "NvBlastExtAuthoringInternalCommon.h"

namespace Nv
{
	namespace Blast
	{
		class SimplexNoise;

		/**
		Structure used on tesselation stage. Maps edge to two neighboor triangles
		*/
		struct EdgeToTriangles
		{
			int32_t tr[2];
			int32_t c;
			EdgeToTriangles()
			{
				c = 0;
			}
			/**
			Add triangle to edge. Should not be called more than twice for one edge!!!!.
			*/
			void	add(int32_t t)
			{
				tr[c] = t;
				++c;
			}
			/**
			Replaces mapping from one triangle to another.
			*/
			void	replace(int32_t from, int32_t to)
			{
				if (tr[0] == from)
				{
					tr[0] = to;
				}
				else
				{
					if (c == 2 && tr[1] == from)
					{
						tr[1] = to;
					}
				}
			}
			/**
			Get triangle which is mapped by this edge and which index is different than provided.
			*/
			int32_t	getNot(int32_t id)
			{
				if (tr[0] != id)
				{
					return tr[0];
				}
				if (c == 2 && tr[1] != id)
				{
					return tr[1];
				}
				return -1;
			}

		};

		/**
			Tool for graphic mesh tesselation and adding noise to internal surface. Each triangle must have initialized 
			Triangle::userInfo field (0 for external surface triangles and != 0 for internal)
		*/
		class MeshNoiser
		{
		public:
			MeshNoiser()
			{
				reset();
			}

			void reset();

			/**
			Edge flags
			*/
			enum EdgeFlag { INTERNAL_EDGE, EXTERNAL_BORDER_EDGE, INTERNAL_BORDER_EDGE, EXTERNAL_EDGE, NONE };

	
			/**
				Set mesh to tesselate and apply noise
			*/
			void setMesh(const std::vector<Triangle>& mesh);

			/**
			Tesselate internal surface.
			\param[in] maxLen - maximal length of edge on internal surface.
			*/
			void							tesselateInternalSurface(float maxLen);

			/**
			Apply noise to internal surface. Must be called only after tesselation!!!
			\param[in] noise - noise generator
			\param[in] falloff - damping of noise around of external surface
			\param[in] relaxIterations - number of smoothing iterations before applying noise
			\param[in] relaxFactor - amount of smooting before applying noise.
			*/
			void							applyNoise(SimplexNoise& noise, float falloff, int32_t relaxIterations, float relaxFactor);

			std::vector<Triangle>			getMesh();

		private:
			physx::PxVec3						mOffset;
			float								mScale;
			bool								isTesselated;
			/**
				Mesh data
			*/
			std::vector<Vertex>					mVertices;
			std::vector<TriangleIndexed>		mTriangles;
			std::vector<Edge>					mEdges;
			std::map<Vertex, int32_t, VrtComp>	mVertMap;
			std::map<Edge, int32_t>				mEdgeMap;


			/**
				Final triangles.
			*/
			std::vector<Triangle>			mResultTriangles;


			int32_t							addVerticeIfNotExist(const Vertex& p);
			int32_t							addEdge(const Edge& e);
			int32_t							findEdge(const Edge& e);



			void							collapseEdge(int32_t id);
			void							divideEdge(int32_t id);
			void							updateVertEdgeInfo();
			void							updateEdgeTriangleInfo();
			void							relax(int32_t iterations, float factor, std::vector<Vertex>& vertices);
			void							recalcNoiseDirs();


			std::vector<bool>							mRestrictionFlag;
			std::vector<EdgeFlag>						mEdgeFlag;
			std::vector<EdgeToTriangles>				mTrMeshEdToTr;
			std::vector<int32_t>						mVertexValence;
			std::vector<std::vector<int32_t> >			mVertexToTriangleMap;



			std::vector<float>							mVerticesDistances;
			std::vector<physx::PxVec3>					mVerticesNormalsSmoothed;
			std::vector<uint32_t>						mPositionMappedVrt;
			std::vector<std::vector<int32_t> >			mGeometryGraph;

			void										prebuildEdgeFlagArray();
			void										computePositionedMapping();
			void										computeFalloffAndNormals();

			void										prebuildTesselatedTriangles();
		};

	} // namespace Blast
} // namespace Nv
#endif // ! NVBLASTEXTAUTHORINGMESHNOISER_H