aboutsummaryrefslogtreecommitdiff
path: root/PhysX_3.4/Source/PhysX/src/buffering/ScbArticulation.h
blob: e01a7ca6564050597dcc1b0605c8e7be6cd6afc5 (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
//
// 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) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.  

#ifndef PX_PHYSICS_SCB_ARTICULATION
#define PX_PHYSICS_SCB_ARTICULATION

#include "ScArticulationCore.h"
#include "ScbBase.h"
#include "ScbDefs.h"

namespace physx
{

namespace Scb
{

struct ArticulationBuffer
{
	template <PxU32 I, PxU32 Dummy> struct Fns {};
	typedef Sc::ArticulationCore Core;
	typedef ArticulationBuffer Buf;

	SCB_REGULAR_ATTRIBUTE(0, PxU32,		InternalDriveIterations)
	SCB_REGULAR_ATTRIBUTE(1, PxU32,		ExternalDriveIterations)
	SCB_REGULAR_ATTRIBUTE(2, PxU32,		MaxProjectionIterations)
	SCB_REGULAR_ATTRIBUTE(3, PxReal,	SeparationTolerance)
	SCB_REGULAR_ATTRIBUTE(4, PxReal,	SleepThreshold)
	SCB_REGULAR_ATTRIBUTE(5, PxU16,		SolverIterationCounts)
	SCB_REGULAR_ATTRIBUTE(6, PxReal,	FreezeThreshold)

	enum { BF_WakeCounter	= 1<<7 };
	enum { BF_PutToSleep	= 1<<8 };
	enum { BF_WakeUp		= 1<<9 };
};

class Articulation : public Base
{
//= ATTENTION! =====================================================================================
// Changing the data layout of this class breaks the binary serialization format.  See comments for 
// PX_BINARY_SERIAL_VERSION.  If a modification is required, please adjust the getBinaryMetaData 
// function.  If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
// accordingly.
//==================================================================================================

	typedef ArticulationBuffer Buf;
	typedef Sc::ArticulationCore Core;

// PX_SERIALIZATION
public:
												Articulation(const PxEMPTY) : Base(PxEmpty), mArticulation(PxEmpty), mBufferedIsSleeping(1) {}
	static			void						getBinaryMetaData(PxOutputStream& stream);
//~PX_SERIALIZATION

private:

public:
	PX_INLINE									Articulation();
	PX_INLINE									~Articulation() {}

	//---------------------------------------------------------------------------------
	// Wrapper for Sc::Articulation interface
	//---------------------------------------------------------------------------------

	PX_INLINE		PxU32		getInternalDriveIterations() const				{ return read<Buf::BF_InternalDriveIterations>();	}	
	PX_INLINE		void		setInternalDriveIterations(const PxU32 v)		{ write<Buf::BF_InternalDriveIterations>(v);		}

	PX_INLINE		PxU32		getExternalDriveIterations() const				{ return read<Buf::BF_ExternalDriveIterations>();	}	
	PX_INLINE		void		setExternalDriveIterations(const PxU32 v)		{ write<Buf::BF_ExternalDriveIterations>(v);		}

	PX_INLINE		PxU32		getMaxProjectionIterations() const				{ return read<Buf::BF_MaxProjectionIterations>();	}
	PX_INLINE		void		setMaxProjectionIterations(const PxU32 v)		{ write<Buf::BF_MaxProjectionIterations>(v);		}

	PX_INLINE		PxU16		getSolverIterationCounts() const				{ return read<Buf::BF_SolverIterationCounts>();		}
	PX_INLINE		void		setSolverIterationCounts(PxU16 v)				{ write<Buf::BF_SolverIterationCounts>(v);			}

	PX_INLINE		PxReal		getSeparationTolerance() const					{ return read<Buf::BF_SeparationTolerance>();		}
	PX_INLINE		void		setSeparationTolerance(const PxReal v)			{ write<Buf::BF_SeparationTolerance>(v);			}

	PX_INLINE		PxReal		getSleepThreshold() const						{ return read<Buf::BF_SleepThreshold>();			}
	PX_INLINE		void		setSleepThreshold(const PxReal v)				{ write<Buf::BF_SleepThreshold>(v);					}

	PX_INLINE		PxReal		getFreezeThreshold() const						{ return read<Buf::BF_FreezeThreshold>();			}
	PX_INLINE		void		setFreezeThreshold(const PxReal v)				{ write<Buf::BF_FreezeThreshold>(v);				}

	PX_INLINE		PxReal		getWakeCounter() const							{ return mBufferedWakeCounter;						}
	PX_INLINE		void		setWakeCounter(const PxReal v);

	PX_FORCE_INLINE	void		wakeUp();
	PX_FORCE_INLINE	void		putToSleep();
	PX_FORCE_INLINE	bool		isSleeping() const								{ return (mBufferedIsSleeping != 0); }

	//---------------------------------------------------------------------------------
	// Data synchronization
	//---------------------------------------------------------------------------------
	PX_INLINE		void						syncState();

	PX_FORCE_INLINE const Sc::ArticulationCore&	getScArticulation()			const	{ return mArticulation; }  // Only use if you know what you're doing!
	PX_FORCE_INLINE Sc::ArticulationCore&		getScArticulation()					{ return mArticulation; }  // Only use if you know what you're doing!

	PX_FORCE_INLINE static Articulation&		fromSc(Core &a)					{ return *reinterpret_cast<Articulation*>(reinterpret_cast<PxU8*>(&a)-getScOffset()); }
	PX_FORCE_INLINE static const Articulation&	fromSc(const Core &a)			{ return *reinterpret_cast<const Articulation*>(reinterpret_cast<const PxU8*>(&a)-getScOffset()); }

	static size_t getScOffset()	{ return reinterpret_cast<size_t>(&reinterpret_cast<Articulation*>(0)->mArticulation);	}

	PX_FORCE_INLINE	void		wakeUpInternal(PxReal wakeCounter);

	PX_FORCE_INLINE	void		initBufferedState();
	PX_FORCE_INLINE	void		clearBufferedState();
	PX_FORCE_INLINE	void		clearBufferedSleepStateChange();

private:
	Sc::ArticulationCore		mArticulation;
	PxReal						mBufferedWakeCounter;
	PxU8						mBufferedIsSleeping;
	
	PX_FORCE_INLINE	const Buf*	getArticulationBuffer()	const	{ return reinterpret_cast<const Buf*>(getStream()); }
	PX_FORCE_INLINE	Buf*		getArticulationBuffer()			{ return reinterpret_cast<Buf*>(getStream()); }

	//---------------------------------------------------------------------------------
	// Infrastructure for regular attributes
	//---------------------------------------------------------------------------------

	struct Access: public BufferedAccess<Buf, Core, Articulation> {};
	template<PxU32 f> PX_FORCE_INLINE typename Buf::Fns<f,0>::Arg read() const		{	return Access::read<Buf::Fns<f,0> >(*this, mArticulation);	}
	template<PxU32 f> PX_FORCE_INLINE void write(typename Buf::Fns<f,0>::Arg v)		{	Access::write<Buf::Fns<f,0> >(*this, mArticulation, v);		}
	template<PxU32 f> PX_FORCE_INLINE void flush(const Buf& buf)					{	Access::flush<Buf::Fns<f,0> >(*this, mArticulation, buf);	}
};

Articulation::Articulation()
{
	setScbType(ScbType::eARTICULATION);
	mBufferedWakeCounter = mArticulation.getWakeCounter();
	mBufferedIsSleeping = 1;  // this is the specified value for free standing objects
}

PX_INLINE void Articulation::setWakeCounter(PxReal counter)
{
	mBufferedWakeCounter = counter;

	if(!isBuffering())
	{
		if(getScbScene() && (counter > 0.0f))
			mBufferedIsSleeping = 0;

		mArticulation.setWakeCounter(counter);
		UPDATE_PVD_PROPERTIES_OBJECT()
	}
	else
	{
		if(counter > 0.0f)
		{
			mBufferedIsSleeping = 0;
			markUpdated(Buf::BF_WakeUp | Buf::BF_WakeCounter);
			resetBufferFlag(Buf::BF_PutToSleep);
		}
		else
			markUpdated(Buf::BF_WakeCounter);
	}
}

PX_FORCE_INLINE void Articulation::wakeUp()
{
	Scene* scene = getScbScene();
	PX_ASSERT(scene);  // only allowed for an object in a scene

	wakeUpInternal(scene->getWakeCounterResetValue());
}

PX_FORCE_INLINE void Articulation::wakeUpInternal(PxReal wakeCounter)
{
	PX_ASSERT(getScbScene());

	mBufferedWakeCounter = wakeCounter;

	mBufferedIsSleeping = 0;
	if(!isBuffering())
	{
		mArticulation.wakeUp(wakeCounter);
	}
	else
	{
		markUpdated(Buf::BF_WakeUp | Buf::BF_WakeCounter);
		resetBufferFlag(Buf::BF_PutToSleep);
	}
}

PX_FORCE_INLINE void Articulation::putToSleep()
{
	mBufferedWakeCounter = 0.0f;

	mBufferedIsSleeping = 1;
	if(!isBuffering())
	{
		mArticulation.putToSleep();
	}
	else
	{
		markUpdated(Buf::BF_PutToSleep | Buf::BF_WakeCounter);
		resetBufferFlag(Buf::BF_WakeUp);
	}
}

PX_FORCE_INLINE	void Articulation::initBufferedState()
{
	PX_ASSERT(mBufferedIsSleeping);  // this method is only meant to get called when an object is added to the scene
	
	if(getWakeCounter() == 0.0f)
		mBufferedIsSleeping = 1;
	else
		mBufferedIsSleeping = 0;
	// note: to really know whether the articulation is awake/asleep on insertion, we need to go through every link and check whether any of them has
	//       a parameter setup that keeps it alive. However, the links get added after the articulation, so we do not test those here. After the links
	//       are added, an additional check will wake the articulation up if necessary.
}

PX_FORCE_INLINE	void Articulation::clearBufferedState()
{
	mBufferedIsSleeping = 1;  // the expected state when an object gets removed from the scene
}

PX_FORCE_INLINE	void Articulation::clearBufferedSleepStateChange()
{
	resetBufferFlag(Buf::BF_WakeUp | Buf::BF_PutToSleep);
}

//--------------------------------------------------------------
//
// Data synchronization
//
//--------------------------------------------------------------

PX_INLINE void Articulation::syncState()
{
	// see comments in Body::syncState
	PX_ASSERT(	(getControlState() != ControlState::eREMOVE_PENDING) || 
				(mBufferedIsSleeping && (!isBuffered(Buf::BF_WakeUp | Buf::BF_PutToSleep))) );

	const PxU32 flags = getBufferFlags();

	//----

	if((flags & Buf::BF_WakeCounter) == 0)
		mBufferedWakeCounter = mArticulation.getWakeCounter();
	else if (!(flags & (Buf::BF_WakeUp | Buf::BF_PutToSleep)))	// if there has been at least one buffered sleep state transition, then there is no use in adjusting the wake counter separately because it will
																// get done in the sleep state update.
	{
		PX_ASSERT(mBufferedWakeCounter == 0.0f);  // a wake counter change is always connected to a sleep state change, except one case: if setWakeCounter(0.0f) was called

		mArticulation.setWakeCounter(mBufferedWakeCounter);
	}

	//----

	if((flags & (Buf::BF_WakeUp | Buf::BF_PutToSleep)) == 0)
	{
		const bool isSimObjectSleeping = mArticulation.isSleeping();
		if(getControlState() != ControlState::eREMOVE_PENDING)  // we do not want the simulation sleep state to take effect if the object was removed (free standing objects have buffered state sleeping)
			mBufferedIsSleeping = PxU8(isSimObjectSleeping);
		else
			PX_ASSERT(mBufferedIsSleeping);  // this must get set immediately at remove
	}
	else
	{
		PX_ASSERT(flags & Buf::BF_WakeCounter);  // sleep state transitions always mark the wake counter dirty
		PX_ASSERT(getControlState() != ControlState::eREMOVE_PENDING);  // removing an object should clear pending wakeUp/putToSleep operations since the state for a free standing object gets set according to specification.

		if(flags & Buf::BF_PutToSleep)
		{
			PX_ASSERT(mBufferedIsSleeping);
			PX_ASSERT(!(flags & Buf::BF_WakeUp));
			PX_ASSERT(mBufferedWakeCounter == 0.0f);
			mArticulation.putToSleep();
		}
		else
		{
			PX_ASSERT(!mBufferedIsSleeping);
			PX_ASSERT(flags & Buf::BF_WakeUp);

			mArticulation.wakeUp(mBufferedWakeCounter);
		}
	}

	//----

	if(flags&~(Buf::BF_WakeCounter|Buf::BF_WakeUp|Buf::BF_PutToSleep))  // Optimization to avoid all the if-statements below if possible
	{
		const Buf* PX_RESTRICT buffer =  getArticulationBuffer();

		flush<Buf::BF_ExternalDriveIterations>(*buffer);
		flush<Buf::BF_InternalDriveIterations>(*buffer);
		flush<Buf::BF_MaxProjectionIterations>(*buffer);
		flush<Buf::BF_SeparationTolerance>(*buffer);
		flush<Buf::BF_SleepThreshold>(*buffer);
		flush<Buf::BF_SolverIterationCounts>(*buffer);
		flush<Buf::BF_FreezeThreshold>(*buffer);
	}

	// --------------
	// postconditions
	//
	PX_ASSERT((getControlState() != ControlState::eREMOVE_PENDING) || mBufferedIsSleeping);  // nothing in this method should change this
	// 
	// postconditions
	// --------------

	postSyncState();
}

}  // namespace Scb

}

#endif