aboutsummaryrefslogtreecommitdiff
path: root/PhysX_3.4/Source/SimulationController/src/particles/ScParticlePacketShape.cpp
blob: aace9b7f440eff3d5bbab6e9212d907cffc3d847 (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
//
// 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.  


#include "ScParticlePacketShape.h"
#if PX_USE_PARTICLE_SYSTEM_API

#include "ScParticleBodyInteraction.h"
#include "ScNPhaseCore.h"
#include "ScScene.h"
#include "PtParticleSystemSim.h"
#include "ScParticleSystemCore.h"
#include "ScSimStats.h"
#include "ScSqBoundsManager.h"
#include "ScScene.h"
#include "PxsContext.h"

using namespace physx;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Sc::ParticlePacketShape::ParticlePacketShape(ParticleSystemSim& particleSystem, PxU32 index, Pt::ParticleShape* llParticleShape) :
	ElementSim(particleSystem, ElementType::ePARTICLE_PACKET),
	mLLParticleShape(llParticleShape)
{
	// Initialize LL shape.
	PX_ASSERT(mLLParticleShape);
	mLLParticleShape->setUserDataV(this);

	setIndex(index);

	// Add particle actor element to broadphase
	createLowLevelVolume();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Sc::ParticlePacketShape::~ParticlePacketShape()
{
	getParticleSystem().unlinkParticleShape(this);		// Let the particle system remove this shape from the list.

	// Remove particle actor element from broadphase and cleanup interactions
	destroyLowLevelVolume();

	// Destroy LowLevel shape
	if (mLLParticleShape)
	{
		mLLParticleShape->destroyV();
		mLLParticleShape = 0;
	}

	PX_ASSERT(mInteractions.size()==0);
	mInteractions.releaseMem(*this);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Sc::ParticlePacketShape::computeWorldBounds(PxBounds3& b) const
{
	 b = getBounds();
	 PX_ASSERT(b.isFinite());
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Sc::ParticlePacketShape::getFilterInfo(PxFilterObjectAttributes& filterAttr, PxFilterData& filterData) const
{
	filterAttr = 0;
	if (getParticleSystem().getInternalFlags() & Pt::InternalParticleSystemFlag::eSPH)
		ElementSim::setFilterObjectAttributeType(filterAttr, PxFilterObjectType::ePARTICLE_FLUID);
	else
		ElementSim::setFilterObjectAttributeType(filterAttr, PxFilterObjectType::ePARTICLE_SYSTEM);

	filterData = getParticleSystem().getSimulationFilterData();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Sc::ParticleSystemSim& Sc::ParticlePacketShape::getParticleSystem() const
{ 
	return static_cast<ParticleSystemSim&>(getActor()); 
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Sc::ParticlePacketShape::setInteractionsDirty(InteractionDirtyFlag::Enum flag)
{
	ParticleElementRbElementInteraction** interactions = getInteractions();
	PxU32 nbInteractions = getInteractionsCount();

	while(nbInteractions--)
	{
		ParticleElementRbElementInteraction* interaction = *interactions++;

		PX_ASSERT(interaction->readInteractionFlag(InteractionFlag::eFILTERABLE));
		PX_ASSERT(interaction->readInteractionFlag(InteractionFlag::eELEMENT_ELEMENT));

		interaction->setDirty(flag);
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// PT: TODO: refactor with Sc::ActorSim::reallocInteractions
void Sc::ParticlePacketShape::reallocInteractions(Sc::ParticleElementRbElementInteraction**& mem, PxU16& capacity, PxU16 size, PxU16 requiredMinCapacity)
{
	ParticleElementRbElementInteraction** newMem;
	PxU16 newCapacity;

	if(requiredMinCapacity==0)
	{
		newCapacity = 0;
		newMem = 0;
	}
	else if(requiredMinCapacity<=INLINE_INTERACTION_CAPACITY)
	{
		newCapacity = INLINE_INTERACTION_CAPACITY;
		newMem = mInlineInteractionMem;
	}
	else
	{
		const PxU32 desiredCapacity = Ps::nextPowerOfTwo(PxU32(requiredMinCapacity-1));
		PX_ASSERT(desiredCapacity<=65536);

		const PxU32 limit = 0xffff;
		newCapacity = Ps::to16(PxMin(limit, desiredCapacity));
		newMem = reinterpret_cast<ParticleElementRbElementInteraction**>(getScene().allocatePointerBlock(newCapacity));
	}

	PX_ASSERT(newCapacity >= requiredMinCapacity && requiredMinCapacity>=size);

	PxMemCopy(newMem, mem, size*sizeof(ParticleElementRbElementInteraction*));

	if(mem && mem!=mInlineInteractionMem)
		getScene().deallocatePointerBlock(reinterpret_cast<void**>(mem), capacity);
	
	capacity = newCapacity;
	mem = newMem;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Sc::ParticlePacketShape::createLowLevelVolume()
{
	PX_ASSERT(getBounds().isFinite());

	getScene().getBoundsArray().setBounds(getBounds(), getElementID());
#ifdef BP_FILTERING_USES_TYPE_IN_GROUP
	const PxU32 group = Bp::FilterGroup::ePARTICLES;
	const PxU32 type = Bp::FilterType::DYNAMIC;
	addToAABBMgr(0, Bp::FilterGroup::Enum((group<<2)|type), false);
#else
	addToAABBMgr(0, Bp::FilterGroup::ePARTICLES, false);
#endif
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Sc::ParticlePacketShape::destroyLowLevelVolume()
{
	if (!isInBroadPhase())
		return;

	Sc::Scene& scene = getScene();
	PxsContactManagerOutputIterator outputs = scene.getLowLevelContext()->getNphaseImplementationContext()->getContactManagerOutputs();

	scene.getNPhaseCore()->onVolumeRemoved(this, 0, outputs, scene.getPublicFlags() & PxSceneFlag::eADAPTIVE_FORCE);
	removeFromAABBMgr();
}

#endif	// PX_USE_PARTICLE_SYSTEM_API