GteMassSpringCurve.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
11 
12 namespace gte
13 {
14 
15 template <int N, typename Real>
16 class MassSpringCurve : public ParticleSystem<N, Real>
17 {
18 public:
19  // Construction and destruction. This class represents a set of N-1
20  // springs connecting N masses that lie on a curve.
21  virtual ~MassSpringCurve();
22  MassSpringCurve(int numParticles, Real step);
23 
24  // Member access. The parameters are spring constant and spring resting
25  // length.
26  inline int GetNumSprings() const;
27  inline void SetConstant(int i, Real constant);
28  inline void SetLength(int i, Real length);
29  inline Real const& GetConstant(int i) const;
30  inline Real const& GetLength(int i) const;
31 
32  // The default external force is zero. Derive a class from this one to
33  // provide nonzero external forces such as gravity, wind, friction,
34  // and so on. This function is called by Acceleration(...) to compute
35  // the impulse F/m generated by the external force F.
36  virtual Vector<N, Real> ExternalAcceleration(int i, Real time,
37  std::vector<Vector<N, Real>> const& position,
38  std::vector<Vector<N, Real>> const& velocity);
39 
40 protected:
41  // Callback for acceleration (ODE solver uses x" = F/m) applied to
42  // particle i. The positions and velocities are not necessarily
43  // mPosition and mVelocity, because the ODE solver evaluates the
44  // impulse function at intermediate positions.
45  virtual Vector<N, Real> Acceleration(int i, Real time,
46  std::vector<Vector<N, Real>> const& position,
47  std::vector<Vector<N, Real>> const& velocity);
48 
49  std::vector<Real> mConstant, mLength;
50 };
51 
52 
53 template <int N, typename Real>
55 {
56 }
57 
58 template <int N, typename Real>
59 MassSpringCurve<N, Real>::MassSpringCurve(int numParticles, Real step)
60  :
61  ParticleSystem<N, Real>(numParticles, step),
62  mConstant(numParticles - 1),
63  mLength(numParticles - 1)
64 {
65  std::fill(mConstant.begin(), mConstant.end(), (Real)0);
66  std::fill(mLength.begin(), mLength.end(), (Real)0);
67 }
68 
69 template <int N, typename Real> inline
71 {
72  return this->mNumParticles - 1;
73 }
74 
75 template <int N, typename Real> inline
76 void MassSpringCurve<N, Real>::SetConstant(int i, Real constant)
77 {
78  mConstant[i] = constant;
79 }
80 
81 template <int N, typename Real> inline
83 {
84  mLength[i] = length;
85 }
86 
87 template <int N, typename Real> inline
88 Real const& MassSpringCurve<N, Real>::GetConstant(int i) const
89 {
90  return mConstant[i];
91 }
92 
93 template <int N, typename Real> inline
94 Real const& MassSpringCurve<N, Real>::GetLength(int i) const
95 {
96  return mLength[i];
97 }
98 
99 template <int N, typename Real>
101  std::vector<Vector<N, Real>> const&, std::vector<Vector<N, Real>> const&)
102 {
103  return Vector<N, Real>::Zero();
104 }
105 
106 template <int N, typename Real>
108  std::vector<Vector<N, Real>> const& position,
109  std::vector<Vector<N, Real>> const& velocity)
110 {
111  // Compute spring forces on position X[i]. The positions are not
112  // necessarily mPosition, because the RK4 solver in ParticleSystem
113  // evaluates the acceleration function at intermediate positions. The end
114  // points of the curve of masses must be handled separately, because each
115  // has only one spring attached to it.
116 
117  Vector<N, Real> acceleration = ExternalAcceleration(i, time, position,
118  velocity);
119 
120  Vector<N, Real> diff, force;
121  Real ratio;
122 
123  if (i > 0)
124  {
125  int iM1 = i - 1;
126  diff = position[iM1] - position[i];
127  ratio = mLength[iM1] / Length(diff);
128  force = mConstant[iM1] * ((Real)1 - ratio) * diff;
129  acceleration += this->mInvMass[i] * force;
130  }
131 
132  int iP1 = i + 1;
133  if (iP1 < this->mNumParticles)
134  {
135  diff = position[iP1] - position[i];
136  ratio = mLength[i] / Length(diff);
137  force = mConstant[i] * ((Real)1 - ratio) * diff;
138  acceleration += this->mInvMass[i] * force;
139  }
140 
141  return acceleration;
142 }
143 
144 
145 }
virtual Vector< N, Real > ExternalAcceleration(int i, Real time, std::vector< Vector< N, Real >> const &position, std::vector< Vector< N, Real >> const &velocity)
MassSpringCurve(int numParticles, Real step)
Real const & GetLength(int i) const
static Vector Zero()
Definition: GteVector.h:295
void SetLength(int i, Real length)
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:790
DualQuaternion< Real > Length(DualQuaternion< Real > const &d, bool robust=false)
virtual Vector< N, Real > Acceleration(int i, Real time, std::vector< Vector< N, Real >> const &position, std::vector< Vector< N, Real >> const &velocity)
std::vector< Real > mLength
std::vector< Real > mInvMass
Real const & GetConstant(int i) const
std::vector< Real > mConstant
void SetConstant(int i, Real constant)


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01