aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/module/clothing/embedded/LowLevelCloth/src/SwClothData.cpp
blob: 8a121ce7d797c3734a979e73abe71e4c02019a0e (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
/*
 * Copyright (c) 2008-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.
 */

// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.

#include "SwClothData.h"
#include "SwCloth.h"
#include "SwFabric.h"
#include "Simd4f.h"
#include "PsUtilities.h"

using namespace nvidia;

cloth::SwClothData::SwClothData(SwCloth& cloth, const SwFabric& fabric)
{
	mNumParticles = uint32_t(cloth.mCurParticles.size());
	mCurParticles = array(cloth.mCurParticles.front());
	mPrevParticles = array(cloth.mPrevParticles.front());

	const float* center = array(cloth.mParticleBoundsCenter);
	const float* extent = array(cloth.mParticleBoundsHalfExtent);
	for(uint32_t i = 0; i < 3; ++i)
	{
		mCurBounds[i] = center[i] - extent[i];
		mCurBounds[i + 3] = center[i] + extent[i];
	}

	// avoid reading uninitialized data into mCurBounds, even though it's never used.
	mPrevBounds[0] = 0.0f;

	mConfigBegin = cloth.mPhaseConfigs.empty() ? 0 : &cloth.mPhaseConfigs.front();
	mConfigEnd = mConfigBegin + cloth.mPhaseConfigs.size();

	mPhases = &fabric.mPhases.front();
	mNumPhases = uint32_t(fabric.mPhases.size());

	mSets = &fabric.mSets.front();
	mNumSets = uint32_t(fabric.mSets.size());

	mRestvalues = &fabric.mRestvalues.front();
	mNumRestvalues = uint32_t(fabric.mRestvalues.size());

	mIndices = &fabric.mIndices.front();
	mNumIndices = uint32_t(fabric.mIndices.size());

	float stiffnessExponent = cloth.mStiffnessFrequency * cloth.mPrevIterDt * 0.69314718055994531f; // logf(2.0f);

	mTethers = fabric.mTethers.begin();
	mNumTethers = uint32_t(fabric.mTethers.size());
	mTetherConstraintStiffness = 1.0f - exp(stiffnessExponent * cloth.mTetherConstraintLogStiffness);
	mTetherConstraintScale = cloth.mTetherConstraintScale * fabric.mTetherLengthScale;

	mStartMotionConstraints = cloth.mMotionConstraints.mStart.size() ? array(cloth.mMotionConstraints.mStart.front()) : 0;
	mTargetMotionConstraints =
	    !cloth.mMotionConstraints.mTarget.empty() ? array(cloth.mMotionConstraints.mTarget.front()) : 0;
	mMotionConstraintStiffness = 1.0f - exp(stiffnessExponent * cloth.mMotionConstraintLogStiffness);

	mStartSeparationConstraints =
	    cloth.mSeparationConstraints.mStart.size() ? array(cloth.mSeparationConstraints.mStart.front()) : 0;
	mTargetSeparationConstraints =
	    !cloth.mSeparationConstraints.mTarget.empty() ? array(cloth.mSeparationConstraints.mTarget.front()) : 0;

	mParticleAccelerations = cloth.mParticleAccelerations.size() ? array(cloth.mParticleAccelerations.front()) : 0;

	mStartCollisionSpheres = cloth.mStartCollisionSpheres.empty() ? 0 : array(cloth.mStartCollisionSpheres.front());
	mTargetCollisionSpheres =
	    cloth.mTargetCollisionSpheres.empty() ? mStartCollisionSpheres : array(cloth.mTargetCollisionSpheres.front());
	mNumSpheres = uint32_t(cloth.mStartCollisionSpheres.size());

	mCapsuleIndices = cloth.mCapsuleIndices.empty() ? 0 : &cloth.mCapsuleIndices.front();
	mNumCapsules = uint32_t(cloth.mCapsuleIndices.size());

	mStartCollisionPlanes = cloth.mStartCollisionPlanes.empty() ? 0 : array(cloth.mStartCollisionPlanes.front());
	mTargetCollisionPlanes =
	    cloth.mTargetCollisionPlanes.empty() ? mStartCollisionPlanes : array(cloth.mTargetCollisionPlanes.front());
	mNumPlanes = uint32_t(cloth.mStartCollisionPlanes.size());

	mConvexMasks = cloth.mConvexMasks.empty() ? 0 : &cloth.mConvexMasks.front();
	mNumConvexes = uint32_t(cloth.mConvexMasks.size());

	mStartCollisionTriangles = cloth.mStartCollisionTriangles.empty() ? 0 : array(cloth.mStartCollisionTriangles.front());
	mTargetCollisionTriangles = cloth.mTargetCollisionTriangles.empty() ? mStartCollisionTriangles
	                                                                    : array(cloth.mTargetCollisionTriangles.front());
	mNumTriangles = uint32_t(cloth.mStartCollisionTriangles.size()) / 3;

	mVirtualParticlesBegin = cloth.mVirtualParticleIndices.empty() ? 0 : array(cloth.mVirtualParticleIndices.front());
	mVirtualParticlesEnd = mVirtualParticlesBegin + 4 * cloth.mVirtualParticleIndices.size();
	mVirtualParticleWeights = cloth.mVirtualParticleWeights.empty() ? 0 : array(cloth.mVirtualParticleWeights.front());
	mNumVirtualParticleWeights = uint32_t(cloth.mVirtualParticleWeights.size());

	mEnableContinuousCollision = cloth.mEnableContinuousCollision;
	mCollisionMassScale = cloth.mCollisionMassScale;
	mFrictionScale = cloth.mFriction;

	mSelfCollisionDistance = cloth.mSelfCollisionDistance;
	mSelfCollisionStiffness = 1.0f - exp(stiffnessExponent * cloth.mSelfCollisionLogStiffness);

	mSelfCollisionIndices = cloth.mSelfCollisionIndices.empty() ? 0 : cloth.mSelfCollisionIndices.begin();
	mNumSelfCollisionIndices = mSelfCollisionIndices ? cloth.mSelfCollisionIndices.size() : mNumParticles;

	mRestPositions = cloth.mRestPositions.size() ? array(cloth.mRestPositions.front()) : 0;

	mSleepPassCounter = cloth.mSleepPassCounter;
	mSleepTestCounter = cloth.mSleepTestCounter;
}

void cloth::SwClothData::reconcile(SwCloth& cloth) const
{
	cloth.setParticleBounds(mCurBounds);
	cloth.mSleepTestCounter = mSleepTestCounter;
	cloth.mSleepPassCounter = mSleepPassCounter;
}

void cloth::SwClothData::verify() const
{
	// checks needs to be run after the constructor because

	PX_ASSERT(!mNumCapsules ||
	          mNumSpheres > *nvidia::maxElement(&mCapsuleIndices->first, &(mCapsuleIndices + mNumCapsules)->first));

	PX_ASSERT(!mNumConvexes || (1u << mNumPlanes) - 1 >= *nvidia::maxElement(mConvexMasks, mConvexMasks + mNumConvexes));
}