aboutsummaryrefslogtreecommitdiff
path: root/NvBlast/sdk/extensions/physx/include/NvBlastExtStressSolver.h
blob: 2fd389d2e4e8f3e2bf9ff060dae9bb58c9abc382 (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
/*
* Copyright (c) 2016-2017, NVIDIA CORPORATION.  All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, 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.
*/

#ifndef NVBLASTEXTSTRESSSOLVER_H
#define NVBLASTEXTSTRESSSOLVER_H

#include "common/PxRenderBuffer.h"
#include <vector>
#include "NvPreprocessor.h"


namespace Nv
{
namespace Blast
{

// forward declarations
class ExtPxFamily;
class ExtPxActor;

/**
Stress Solver Settings

Stress on every bond is calculated as 
stress = bond.linearStress * stressLinearFactor + bond.angularStress * stressAngularFactor
where:
bond.linearStress - is linear stress force on particular bond
bond.angularStress - is angular stress force on particular bond
stressLinearFactor, stressAngularFactor - are a multiplier parameter set by this struct

Support graph reduction:
2 ^ reduction level = max node count to be aggregated during graph reduction, so 0 is 2 % 0 = 1, basically use support graph.
So N nodes graph will be simplified to contain ~ N / (2 ^ reduction level)
*/
struct ExtStressSolverSettings
{
	float		stressLinearFactor;			//!<	linear stress on bond multiplier
	float		stressAngularFactor;		//!<	angular stress on bond multiplier
	uint32_t	bondIterationsPerFrame;		//!<	number of bond iterations to perform per frame, @see getIterationsPerFrame() below
	uint32_t	graphReductionLevel;		//!<	graph reduction level

	ExtStressSolverSettings() :
		stressLinearFactor(0.00004f),
		stressAngularFactor(0.00007f),
		bondIterationsPerFrame(18000),
		graphReductionLevel(3)
	{}
};


/**
Stress Solver.

Uses ExtPxFamily, allocates and prepares it's graph once when it's created. Then it's being quickly updated on every 
actor split.
Works on both dynamic and static actor's within family.
For static actors it applies gravity.
For dynamic actors it applies centrifugal force.
Additionally applyImpulse() method can be used to apply external impulse (like impact damage).
*/
class NV_DLL_EXPORT ExtStressSolver
{
public:
	//////// creation ////////

	/**
	Create a new ExtStressSolver.

	\param[in]	family			The ExtPxFamily instance to calculate stress on.
	\param[in]	settings		The settings to be set on ExtStressSolver.

	\return the new ExtStressSolver if successful, NULL otherwise.
	*/
	static ExtStressSolver*					create(ExtPxFamily& family, ExtStressSolverSettings settings = ExtStressSolverSettings());


	//////// interface ////////

	/**
	Release this stress solver.
	*/
	virtual void							release() = 0;

	/**
	Set stress solver settings.
	Changing graph reduction level will lead to graph being rebuilt (which is fast, but still not recommended).
	All other settings are applied instantly and can be changed every frame.

	\param[in]	settings		The settings to be set on ExtStressSolver.
	*/
	virtual void							setSettings(const ExtStressSolverSettings& settings) = 0;

	/**
	Get stress solver settings.

	\return the pointer to stress solver settings currently set.
	*/
	virtual const ExtStressSolverSettings&	getSettings() const = 0;

	/**
	Apply external impulse on particular actor of family

	\param[in]	actor			The ExtPxActor to apply impulse on.
	\param[in]	position		Local position in actor's coordinates to apply impulse on.
	\param[in]	force			Impulse to apply (kg * m / s).
	*/
	virtual void							applyImpulse(ExtPxActor& actor, physx::PxVec3 position, physx::PxVec3 force) = 0;

	/**
	Update stress solver.

	Calculate stress and optionally apply damage.

	\param[in]	doDamage		If 'true' damage will be applied after stress solver.
	*/
	virtual void							update(bool doDamage = true) = 0;

	/**
	Reset stress solver.

	Stress solver uses warm start internally, calling this function will flush all previous data calculated and also zeros frame count.
	This function is to be used for debug purposes. 
	*/
	virtual void							reset() = 0;

	/**
	Debug Render Mode
	*/
	enum DebugRenderMode
	{
		STRESS_GRAPH = 0,					//!<	render only stress graph
		STRESS_GRAPH_NODES_IMPULSES = 1,	//!<	render stress graph + nodes impulses after solving stress
		STRESS_GRAPH_BONDS_IMPULSES = 2		//!<	render stress graph + bonds impulses after solving stress
	};

	/**
	Fill debug render for passed array of support graph nodes.

	\param[in]	nodes			Node indices of support graph to debug render for.
	\param[out]	lines			Lines array to fill.
	\param[in]	mode			Debug render mode.
	\param[in]	scale			Scale to be applied on impulses.
	*/
	virtual void							fillDebugRender(const std::vector<uint32_t>& nodes, std::vector<physx::PxDebugLine>& lines, DebugRenderMode mode, float scale = 1.0f) = 0;

	/**
	Get stress solver linear error.

	\return the total linear error of stress calculation.
	*/
	virtual float							getStressErrorLinear() const = 0;

	/**
	Get stress solver angular error.

	\return the total angular error of stress calculation.
	*/
	virtual float							getStressErrorAngular() const = 0;

	/**
	Get stress solver total iterations count since it was created (or reset).

	\return the iterations count.
	*/
	virtual uint32_t						getIterationCount() const = 0;

	/**
	Get stress solver total frames count (update() calls) since it was created (or reset).

	\return the frames count.
	*/
	virtual uint32_t						getFrameCount() const = 0;

	/**
	Get stress solver bonds count, after graph reduction was applied.

	\return the bonds count.
	*/
	virtual uint32_t						getBondCount() const = 0;


	//////// helpers ////////

	/**
	Get iteration per frame (update() call).

	Helper method to know how many solver iterations are made per frame.

	\return the iterations per frame count.
	*/
	uint32_t								getIterationsPerFrame() const
	{
		uint32_t perFrame = getSettings().bondIterationsPerFrame / (getBondCount() + 1);
		return perFrame > 0 ? perFrame : 1;
	}
};

} // namespace Blast
} // namespace Nv


#endif // ifndef NVBLASTEXTSTRESSSOLVER_H