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
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.1 (2010/10/01)
#include "Wm5PhysicsPCH.h"
#include "Wm5MassSpringVolume.h"
#include "Wm5Memory.h"
namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
MassSpringVolume<Real,TVector>::MassSpringVolume (int numSlices, int numRows,
int numCols, Real step)
:
ParticleSystem<Real,TVector>(numSlices*numRows*numCols, step)
{
mNumSlices = numSlices;
mNumRows = numRows;
mNumCols = numCols;
mSliceQuantity = mNumRows*mNumCols;
mNumSlicesM1 = mNumSlices - 1;
mNumRowsM1 = mNumRows - 1;
mNumColsM1 = mNumCols - 1;
mPositionGrid = new1<TVector**>(mNumSlices);
mVelocityGrid = new1<TVector**>(mNumSlices);
for (int slice = 0; slice < mNumSlices; ++slice)
{
mPositionGrid[slice] = new1<TVector*>(mNumRows);
mVelocityGrid[slice] = new1<TVector*>(mNumRows);
for (int row = 0; row < mNumRows; ++row)
{
int i = mNumCols*(row + mNumRows*slice);
mPositionGrid[slice][row] = &mPositions[i];
mVelocityGrid[slice][row] = &mVelocities[i];
}
}
mConstantsS = new3<Real>(mNumCols, mNumRows, mNumSlicesM1);
mLengthsS = new3<Real>(mNumCols, mNumRows, mNumSlicesM1);
mConstantsR = new3<Real>(mNumCols, mNumRowsM1, mNumSlices);
mLengthsR = new3<Real>(mNumCols, mNumRowsM1, mNumSlices);
mConstantsC = new3<Real>(mNumColsM1, mNumRows, mNumSlices);
mLengthsC = new3<Real>(mNumColsM1, mNumRows, mNumSlices);
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
MassSpringVolume<Real,TVector>::~MassSpringVolume ()
{
for (int slice = 0; slice < mNumSlices; ++slice)
{
delete1(mPositionGrid[slice]);
delete1(mVelocityGrid[slice]);
}
delete1(mPositionGrid);
delete1(mVelocityGrid);
delete3(mConstantsS);
delete3(mLengthsS);
delete3(mConstantsR);
delete3(mLengthsR);
delete3(mConstantsC);
delete3(mLengthsC);
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
int MassSpringVolume<Real,TVector>::GetNumSlices () const
{
return mNumSlices;
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
int MassSpringVolume<Real,TVector>::GetNumRows () const
{
return mNumRows;
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
int MassSpringVolume<Real,TVector>::GetNumCols () const
{
return mNumCols;
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
void MassSpringVolume<Real,TVector>::SetMass (int slice, int row, int col,
Real mass)
{
int i = GetIndex(slice, row, col);
ParticleSystem<Real,TVector>::SetMass(i, mass);
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
Real MassSpringVolume<Real,TVector>::GetMass (int slice, int row, int col)
const
{
int i = GetIndex(slice, row, col);
return ParticleSystem<Real,TVector>::GetMass(i);
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
TVector*** MassSpringVolume<Real,TVector>::Positions3D () const
{
return mPositionGrid;
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
TVector& MassSpringVolume<Real,TVector>::Position (int slice, int row,
int col)
{
int i = GetIndex(slice, row, col);
return ParticleSystem<Real,TVector>::Position(i);
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
TVector*** MassSpringVolume<Real,TVector>::Velocities3D () const
{
return mVelocityGrid;
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
TVector& MassSpringVolume<Real,TVector>::Velocity (int slice, int row,
int col)
{
int i = GetIndex(slice, row, col);
return ParticleSystem<Real,TVector>::Velocity(i);
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
Real& MassSpringVolume<Real,TVector>::ConstantS (int slice, int row, int col)
{
return mConstantsS[slice][row][col];
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
Real& MassSpringVolume<Real,TVector>::LengthS (int slice, int row, int col)
{
return mLengthsS[slice][row][col];
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
Real& MassSpringVolume<Real,TVector>::ConstantR (int slice, int row, int col)
{
return mConstantsR[slice][row][col];
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
Real& MassSpringVolume<Real,TVector>::LengthR (int slice, int row, int col)
{
return mLengthsR[slice][row][col];
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
Real& MassSpringVolume<Real,TVector>::ConstantC (int slice, int row, int col)
{
return mConstantsC[slice][row][col];
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
Real& MassSpringVolume<Real,TVector>::LengthC (int slice, int row, int col)
{
return mLengthsC[slice][row][col];
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
TVector MassSpringVolume<Real,TVector>::Acceleration (int i, Real time,
const TVector* positions, const TVector* velocities)
{
// Compute spring forces on position X[i]. The positions are not
// necessarily mPositions since the RK4 solver in ParticleSystem
// evaluates the acceleration function at intermediate positions. The
// face, edge, and corner points of the volume of masses must be handled
// separately since each has fewer than eight spring attached to it.
TVector acceleration = ExternalAcceleration(i, time, positions,
velocities);
TVector diff, force;
Real ratio;
int slice, row, col, prev, next;
GetCoordinates(i, slice, row, col);
if (slice > 0)
{
prev = i - mSliceQuantity; // index to previous slice-neighbor
diff = positions[prev] - positions[i];
ratio = LengthS(slice - 1, row, col)/diff.Length();
force = ConstantS(slice - 1, row, col)*((Real)1 - ratio)*diff;
acceleration += mInvMasses[i]*force;
}
if (slice < mNumSlicesM1)
{
next = i + mSliceQuantity; // index to next slice-neighbor
diff = positions[next] - positions[i];
ratio = LengthS(slice, row, col)/diff.Length();
force = ConstantS(slice, row, col)*((Real)1 - ratio)*diff;
acceleration += mInvMasses[i]*force;
}
if (row > 0)
{
prev = i - mNumCols; // index to previous row-neighbor
diff = positions[prev] - positions[i];
ratio = LengthR(slice, row - 1, col)/diff.Length();
force = ConstantR(slice, row - 1, col)*((Real)1 - ratio)*diff;
acceleration += mInvMasses[i]*force;
}
if (row < mNumRowsM1)
{
next = i + mNumCols; // index to next row-neighbor
diff = positions[next] - positions[i];
ratio = LengthR(slice, row, col)/diff.Length();
force = ConstantR(slice, row, col)*((Real)1 - ratio)*diff;
acceleration += mInvMasses[i]*force;
}
if (col > 0)
{
prev = i - 1; // index to previous col-neighbor
diff = positions[prev] - positions[i];
ratio = LengthC(slice, row, col - 1)/diff.Length();
force = ConstantC(slice, row, col - 1)*((Real)1 - ratio)*diff;
acceleration += mInvMasses[i]*force;
}
if (col < mNumColsM1)
{
next = i + 1; // index to next col-neighbor
diff = positions[next] - positions[i];
ratio = LengthC(slice, row, col)/diff.Length();
force = ConstantC(slice, row, col)*((Real)1 - ratio)*diff;
acceleration += mInvMasses[i]*force;
}
return acceleration;
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
TVector MassSpringVolume<Real,TVector>::ExternalAcceleration (int, Real,
const TVector*, const TVector*)
{
return TVector::ZERO;
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
int MassSpringVolume<Real,TVector>::GetIndex (int slice, int row, int col)
const
{
return col + mNumCols*(row + mNumRows*slice);
}
//----------------------------------------------------------------------------
template <typename Real, typename TVector>
void MassSpringVolume<Real,TVector>::GetCoordinates (int i, int& slice,
int& row, int& col) const
{
col = i % mNumCols;
i = (i - col)/mNumCols;
row = i % mNumRows;
slice = i / mNumRows;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template WM5_PHYSICS_ITEM
class MassSpringVolume<float,Vector3f>;
template WM5_PHYSICS_ITEM
class MassSpringVolume<double,Vector3d>;
//----------------------------------------------------------------------------
}
|