• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

step/stepcore

  • sources
  • kde-4.12
  • kdeedu
  • step
  • stepcore
spring.cc
Go to the documentation of this file.
1 /* This file is part of StepCore library.
2  Copyright (C) 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
3 
4  StepCore library is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  StepCore library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with StepCore; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "spring.h"
20 #include "types.h"
21 
22 #include <algorithm>
23 #include <cmath>
24 #include <QtGlobal>
25 
26 namespace StepCore {
27 
28 STEPCORE_META_OBJECT(Spring, QT_TRANSLATE_NOOP("ObjectClass", "Spring"), QT_TR_NOOP("Massless spring which can be connected to bodies"), 0,
29  STEPCORE_SUPER_CLASS(Item) STEPCORE_SUPER_CLASS(Force),
30  STEPCORE_PROPERTY_RW(double, restLength, QT_TRANSLATE_NOOP("PropertyName", "restLength"), QT_TRANSLATE_NOOP("Units", "m"), QT_TR_NOOP("Rest length"), restLength, setRestLength)
31  STEPCORE_PROPERTY_R_D(double, length, QT_TRANSLATE_NOOP("PropertyName", "length"), QT_TRANSLATE_NOOP("Units", "m"), QT_TR_NOOP("Current length"), length)
32  STEPCORE_PROPERTY_RW(double, stiffness, QT_TRANSLATE_NOOP("PropertyName", "stiffness"), QT_TRANSLATE_NOOP("Units", "N/m"), QT_TR_NOOP("Stiffness"), stiffness, setStiffness)
33  STEPCORE_PROPERTY_RW(double, damping, QT_TRANSLATE_NOOP("PropertyName", "damping"), QT_TRANSLATE_NOOP("Units", "N s/m"), QT_TR_NOOP("Damping"), damping, setDamping)
34  STEPCORE_PROPERTY_RW(Object*, body1, QT_TRANSLATE_NOOP("PropertyName", "body1"), STEPCORE_UNITS_NULL, QT_TR_NOOP("Body1"), body1, setBody1)
35  STEPCORE_PROPERTY_RW(Object*, body2, QT_TRANSLATE_NOOP("PropertyName", "body2"), STEPCORE_UNITS_NULL, QT_TR_NOOP("Body2"), body2, setBody2)
36  STEPCORE_PROPERTY_RW(StepCore::Vector2d, localPosition1, QT_TRANSLATE_NOOP("PropertyName", "localPosition1"), QT_TRANSLATE_NOOP("Units", "m"),
37  QT_TR_NOOP("Local position 1"), localPosition1, setLocalPosition1)
38  STEPCORE_PROPERTY_RW(StepCore::Vector2d, localPosition2, QT_TRANSLATE_NOOP("PropertyName", "localPosition2"), QT_TRANSLATE_NOOP("Units", "m"),
39  QT_TR_NOOP("Local position 2"), localPosition2, setLocalPosition2)
40  STEPCORE_PROPERTY_R_D(StepCore::Vector2d, position1, QT_TRANSLATE_NOOP("PropertyName", "position1"), QT_TRANSLATE_NOOP("Units", "m"), QT_TR_NOOP("Position1"), position1)
41  STEPCORE_PROPERTY_R_D(StepCore::Vector2d, position2, QT_TRANSLATE_NOOP("PropertyName", "position2"), QT_TRANSLATE_NOOP("Units", "m"), QT_TR_NOOP("Position2"), position2)
42  STEPCORE_PROPERTY_R_D(double, force, QT_TRANSLATE_NOOP("PropertyName", "force"), QT_TRANSLATE_NOOP("Units", "N"), QT_TR_NOOP("Spring tension force"), force)
43  )
44 
45 STEPCORE_META_OBJECT(SpringErrors, QT_TRANSLATE_NOOP("ObjectClass", "SpringErrors"), QT_TR_NOOP("Errors class for Spring"), 0,
46  STEPCORE_SUPER_CLASS(ObjectErrors),
47  STEPCORE_PROPERTY_RW(double, restLengthVariance, QT_TRANSLATE_NOOP("PropertyName", "restLengthVariance"), QT_TRANSLATE_NOOP("Units", "m"),
48  QT_TR_NOOP("Rest length variance"), restLengthVariance, setRestLengthVariance)
49  STEPCORE_PROPERTY_R_D(double, lengthVariance, QT_TRANSLATE_NOOP("PropertyName", "lengthVariance"), QT_TRANSLATE_NOOP("Units", "m"),
50  QT_TR_NOOP("Current length variance"), lengthVariance)
51  STEPCORE_PROPERTY_RW(double, stiffnessVariance, QT_TRANSLATE_NOOP("PropertyName", "stiffnessVariance"), QT_TRANSLATE_NOOP("Units", "N/m"),
52  QT_TR_NOOP("Stiffness variance"), stiffnessVariance, setStiffnessVariance)
53  STEPCORE_PROPERTY_RW(double, dampingVariance, QT_TRANSLATE_NOOP("PropertyName", "dampingVariance"), QT_TRANSLATE_NOOP("Units", "N/m"),
54  QT_TR_NOOP("Damping variance"), dampingVariance, setDampingVariance)
55  STEPCORE_PROPERTY_RW(StepCore::Vector2d, localPosition1Variance, QT_TRANSLATE_NOOP("PropertyName", "localPosition1Variance"), QT_TRANSLATE_NOOP("Units", "m"),
56  QT_TR_NOOP("Local position 1 variance"), localPosition1Variance, setLocalPosition1Variance)
57  STEPCORE_PROPERTY_RW(StepCore::Vector2d, localPosition2Variance, QT_TRANSLATE_NOOP("PropertyName", "localPosition2Variance"), QT_TRANSLATE_NOOP("Units", "m"),
58  QT_TR_NOOP("Local position 2 variance"), localPosition2Variance, setLocalPosition2Variance)
59  STEPCORE_PROPERTY_R_D(StepCore::Vector2d, position1Variance, QT_TRANSLATE_NOOP("PropertyName", "position1Variance"), QT_TRANSLATE_NOOP("Units", "m"),
60  QT_TR_NOOP("Position1 variance"), position1Variance)
61  STEPCORE_PROPERTY_R_D(StepCore::Vector2d, position2Variance, QT_TRANSLATE_NOOP("PropertyName", "position2Variance"), QT_TRANSLATE_NOOP("Units", "m"),
62  QT_TR_NOOP("Position2 variance"), position2Variance)
63  STEPCORE_PROPERTY_R_D(double, forceVariance, QT_TRANSLATE_NOOP("PropertyName", "forceVariance"), QT_TRANSLATE_NOOP("Units", "N"),
64  QT_TR_NOOP("Spring tension force variance"), forceVariance)
65  )
66 
67 Spring* SpringErrors::spring() const
68 {
69  return static_cast<Spring*>(owner());
70 }
71 
72 Spring::Spring(double restLength, double stiffness, double damping, Item* body1, Item* body2)
73  : _restLength(restLength),
74  _stiffness(stiffness), _damping(damping),
75  _localPosition1(0,0), _localPosition2(0,0)
76 {
77  setColor(0xff00ff00);
78  setBody1(body1);
79  setBody2(body2);
80 }
81 
82 void Spring::calcForce(bool calcVariances)
83 {
84  if(!_body1 && !_body2) return;
85 
86  Vector2d position1 = this->position1();
87  Vector2d position2 = this->position2();
88  Vector2d r = position2 - position1;
89  Vector2d v = velocity2() - velocity1();
90 
91  double l = r.norm();
92  if(l == 0) return; // XXX: take orientation from previous step
93 
94  double dl = l - _restLength;
95  double vr = v.dot(r);
96  Vector2d force = (_stiffness*dl + _damping*vr/l) / l * r;
97 
98  if(_p1) _p1->applyForce(force);
99  else if(_r1) _r1->applyForce(force, position1);
100 
101  force = -force;
102  if(_p2) _p2->applyForce(force);
103  else if(_r2) _r2->applyForce(force, position2);
104 
105  if(calcVariances) {
106  SpringErrors* se = springErrors();
107 
108  Vector2d rV = se->position2Variance() + se->position1Variance();
109  Vector2d vV = se->velocity2Variance() + se->velocity1Variance();
110 
111  Vector2d forceV = (se->_restLengthVariance * square(_stiffness) +
112  se->_stiffnessVariance * square(dl) +
113  se->_dampingVariance * square(vr/l) +
114  vV.dot( (_damping/l*r).cwise().square() )
115  )/square(l)*r.cwise().square();
116 
117  forceV[0] += rV[0] * square(_stiffness*( 1 - _restLength/l*(1 - square(r[0]/l)) ) +
118  _damping/(l*l)*( v[0]*r[0] + vr - 2*vr*square(r[0]/l) )) +
119  rV[1] * square(_stiffness*_restLength*r[0]*r[1]/(l*l*l) +
120  _damping/(l*l)*( v[1]*r[0] - 2*vr*r[0]*r[1]/(l*l) ));
121  forceV[1] += rV[1] * square(_stiffness*( 1 - _restLength/l*(1 - square(r[1]/l)) ) +
122  _damping/(l*l)*( v[1]*r[1] + vr - 2*vr*square(r[1]/l) )) +
123  rV[0] * square(_stiffness*_restLength*r[0]*r[1]/(l*l*l) +
124  _damping/(l*l)*( v[0]*r[1] - 2*vr*r[0]*r[1]/(l*l) ));
125 
126  // TODO: position1() and force is corelated, we should take it into account
127  if(_p1) _p1->particleErrors()->applyForceVariance(forceV);
128  else if(_r1) _r1->rigidBodyErrors()->applyForceVariance(force, position1,
129  forceV, se->position1Variance() );
130 
131  if(_p2) _p2->particleErrors()->applyForceVariance(forceV);
132  else if(_r2) _r2->rigidBodyErrors()->applyForceVariance(force, position2,
133  forceV, se->position2Variance() );
134  }
135 }
136 
137 void Spring::setBody1(Object* body1)
138 {
139  if(body1) {
140  if(body1->metaObject()->inherits<Particle>()) {
141  _body1 = body1;
142  _p1 = static_cast<Particle*>(body1);
143  _r1 = NULL;
144  return;
145  } else if(body1->metaObject()->inherits<RigidBody>()) {
146  _body1 = body1;
147  _p1 = NULL;
148  _r1 = static_cast<RigidBody*>(body1);
149  return;
150  }
151  }
152  _body1 = NULL;
153  _p1 = NULL;
154  _r1 = NULL;
155 }
156 
157 void Spring::setBody2(Object* body2)
158 {
159  if(body2) {
160  if(body2->metaObject()->inherits<Particle>()) {
161  _body2 = body2;
162  _p2 = static_cast<Particle*>(body2);
163  _r2 = NULL;
164  return;
165  } else if(body2->metaObject()->inherits<RigidBody>()) {
166  _body2 = body2;
167  _p2 = NULL;
168  _r2 = static_cast<RigidBody*>(body2);
169  return;
170  }
171  }
172  _body2 = NULL;
173  _p2 = NULL;
174  _r2 = NULL;
175 }
176 
177 Vector2d Spring::position1() const
178 {
179  if(_p1) return _p1->position() + _localPosition1;
180  else if(_r1) return _r1->pointLocalToWorld(_localPosition1);
181  else return _localPosition1;
182 }
183 
184 Vector2d SpringErrors::position1Variance() const
185 {
186  if(spring()->_p1)
187  return spring()->_p1->particleErrors()->positionVariance() + _localPosition1Variance;
188  // XXX: TODO
189  //RigidBody* _r1 = dynamic_cast<RigidBody*>(_body1);
190  //if(_r1) return _r1->pointLocalToWorld(_localPosition1);
191 #ifdef __GNUC__
192 #warning variance calculation for spring connected to rigidbody is not finished !
193 #warning consider unification of some part of Particle and RigidBody
194 #endif
195  else return _localPosition1Variance;
196 }
197 
198 Vector2d Spring::position2() const
199 {
200  if(_p2) return _p2->position() + _localPosition2;
201  else if(_r2) return _r2->pointLocalToWorld(_localPosition2);
202  else return _localPosition2;
203 }
204 
205 Vector2d SpringErrors::position2Variance() const
206 {
207  if(spring()->_p2)
208  return spring()->_p2->particleErrors()->positionVariance() + _localPosition2Variance;
209  // XXX: TODO
210  //RigidBody* _r2 = dynamic_cast<RigidBody*>(_body2);
211  //if(_r2) return _r2->pointLocalToWorld(_localPosition2);
212  else return _localPosition2Variance;
213 }
214 
215 double SpringErrors::lengthVariance() const
216 {
217  Vector2d r = spring()->position2() - spring()->position1();
218  Vector2d rV = position2Variance() + position1Variance();
219  return (r[0]*r[0]*rV[0] + r[1]*r[1]*rV[1])/r.squaredNorm();
220 }
221 
222 Vector2d Spring::velocity1() const
223 {
224  if(_p1) return _p1->velocity();
225  else if(_r1) return _r1->velocityLocal(_localPosition1);
226  else return Vector2d::Zero();
227 }
228 
229 Vector2d SpringErrors::velocity1Variance() const
230 {
231  if(spring()->_p1)
232  return spring()->_p1->particleErrors()->velocityVariance();
233  // XXX: TODO
234  //RigidBody* _r1 = dynamic_cast<RigidBody*>(_body1);
235  //if(_r1) return _r1->pointLocalToWorld(_localPosition1);
236  else return Vector2d::Zero();
237 }
238 
239 Vector2d Spring::velocity2() const
240 {
241  if(_p2) return _p2->velocity();
242  else if(_r2) return _r2->velocityLocal(_localPosition2);
243  else return Vector2d::Zero();
244 }
245 
246 Vector2d SpringErrors::velocity2Variance() const
247 {
248  if(spring()->_p2)
249  return spring()->_p2->particleErrors()->velocityVariance();
250  // XXX: TODO
251  //RigidBody* _r2 = dynamic_cast<RigidBody*>(_body2);
252  //if(_r2) return _r2->pointLocalToWorld(_localPosition2);
253  else return Vector2d::Zero();
254 }
255 
256 double Spring::force() const
257 {
258  Vector2d r = position2() - position1();
259  Vector2d v = velocity2() - velocity1();
260  double l = r.norm();
261  return _stiffness * (l - _restLength) +
262  _damping * v.dot(r)/l;
263 }
264 
265 double SpringErrors::forceVariance() const
266 {
267  Spring* s = spring();
268  Vector2d r = s->position2() - s->position1();
269  Vector2d v = s->velocity2() - s->velocity1();
270  Vector2d rV = position2Variance() + position1Variance();
271  Vector2d vV = velocity2Variance() + velocity1Variance();
272  double l = r.norm();
273  double dl = l - s->restLength();
274  // XXX: CHECKME
275  return square(dl) * _stiffnessVariance +
276  square(s->stiffness()) * _restLengthVariance +
277  square(v.dot(r)/l) * _dampingVariance +
278  (s->damping()/l*r).cwise().square().dot(vV) +
279  (( s->stiffness() - s->damping()*v.dot(r) / (l*l) ) / l * r +
280  s->damping() / l * v).cwise().square().dot(rV);
281 }
282 
283 /*
284 void Spring::worldItemRemoved(Item* item)
285 {
286  if(item == NULL) return;
287  else if(item == _body1) setBody1(NULL);
288  else if(item == _body2) setBody2(NULL);
289 }
290 */
291 
292 #if 0
293 void Spring::setWorld(World* world)
294 {
295  if(world == NULL) {
296  setBody1(NULL);
297  setBody2(NULL);
298  } else if(this->world() != NULL) {
299 #ifdef __GNUC__
300 #warning Use map instead of search-by-name here !
301 #endif
302  if(_body1 != NULL) setBody1(world->item(body1()->name())); //XXX
303  if(_body2 != NULL) setBody2(world->item(body2()->name())); //XXX
304  }
305  Item::setWorld(world);
306 }
307 #endif
308 
309 } // namespace StepCore
310 
StepCore::Spring::stiffness
double stiffness() const
Get stiffness of the spring.
Definition: spring.h:149
StepCore::ItemGroup::item
Item * item(const QString &name) const
Get any descendant item by its name.
Definition: world.cc:191
StepCore::Spring::velocity2
Vector2d velocity2() const
Velocity of the second end of the spring.
Definition: spring.cc:239
types.h
Type to and from string convertion helpers.
StepCore::Vector2d
Eigen::Vector2d Vector2d
Two-dimensional vector with double components.
Definition: vector.h:29
StepCore::Object
Root of the StepCore classes hierarchy.
Definition: object.h:57
StepCore::SpringErrors::_dampingVariance
double _dampingVariance
Definition: spring.h:105
StepCore::RigidBody::position
const Vector2d & position() const
Get position of the center of mass of the body.
Definition: rigidbody.h:160
StepCore::STEPCORE_SUPER_CLASS
STEPCORE_SUPER_CLASS(CollisionSolver)
StepCore::Spring::Spring
Spring(double restLength=0, double stiffness=1, double damping=0, Item *body1=0, Item *body2=0)
Constructs Spring.
StepCore::RigidBody
Rigid body.
Definition: rigidbody.h:144
spring.h
Spring class.
StepCore::SpringErrors::_restLengthVariance
double _restLengthVariance
Definition: spring.h:103
StepCore::SpringErrors::velocity2Variance
Vector2d velocity2Variance() const
Get velocity2 variance.
Definition: spring.cc:246
StepCore::QT_TRANSLATE_NOOP
QT_TRANSLATE_NOOP("ObjectClass","GJKCollisionSolver")
StepCore::Spring::velocity1
Vector2d velocity1() const
Set position of the second end of the spring (will be ignored the end is connected) ...
Definition: spring.cc:222
StepCore::dampingVariance
lengthVariance dampingVariance
Definition: spring.cc:53
StepCore::SpringErrors::velocity1Variance
Vector2d velocity1Variance() const
Get velocity1 variance.
Definition: spring.cc:229
StepCore::Spring::position1
Vector2d position1() const
Position of the first end of the spring.
Definition: spring.cc:177
StepCore::QT_TR_NOOP
QT_TR_NOOP("Errors class for CoulombForce")
StepCore::SpringErrors
Errors object for Spring.
Definition: spring.h:43
StepCore::STEPCORE_PROPERTY_RW
STEPCORE_PROPERTY_RW(double, depth, QT_TRANSLATE_NOOP("PropertyName","depth"), QT_TRANSLATE_NOOP("Units","J"), QT_TR_NOOP("Potential depth"), depth, setDepth) STEPCORE_PROPERTY_RW(double
StepCore::SpringErrors::position1Variance
Vector2d position1Variance() const
Get position1 variance.
Definition: spring.cc:184
StepCore::Particle
Particle with mass.
Definition: particle.h:103
StepCore::Spring
Massless spring.
Definition: spring.h:129
STEPCORE_UNITS_NULL
#define STEPCORE_UNITS_NULL
Definition: object.h:365
StepCore::SpringErrors::position2Variance
Vector2d position2Variance() const
Get position2 variance.
Definition: spring.cc:205
StepCore::STEPCORE_PROPERTY_R_D
setRmin setRminVariance STEPCORE_PROPERTY_R_D(double, rectPressureVariance, QT_TRANSLATE_NOOP("PropertyName","rectPressureVariance"), QT_TRANSLATE_NOOP("Units","Pa"), QT_TR_NOOP("Variance of pressure of particles in the measureRect"), rectPressureVariance) STEPCORE_PROPERTY_R_D(double
StepCore::lengthVariance
lengthVariance
Definition: spring.cc:49
StepCore::SpringErrors::_stiffnessVariance
double _stiffnessVariance
Definition: spring.h:104
StepCore::STEPCORE_META_OBJECT
STEPCORE_META_OBJECT(CollisionSolver, QT_TRANSLATE_NOOP("ObjectClass","CollisionSolver"),"CollisionSolver", MetaObject::ABSTRACT, STEPCORE_SUPER_CLASS(Object), STEPCORE_PROPERTY_RW(double, toleranceAbs, QT_TRANSLATE_NOOP("PropertyName","toleranceAbs"), STEPCORE_UNITS_1, QT_TR_NOOP("Allowed absolute tolerance"), toleranceAbs, setToleranceAbs) STEPCORE_PROPERTY_R_D(double, localError, QT_TRANSLATE_NOOP("PropertyName","localError"), STEPCORE_UNITS_1, QT_TR_NOOP("Maximal local error during last step"), localError)) STEPCORE_META_OBJECT(GJKCollisionSolver
StepCore::World
Contains multiple Item, Solver and general properties such as time.
Definition: world.h:372
StepCore::Spring::damping
double damping() const
Get damping of the spring.
Definition: spring.h:154
StepCore::square
T square(T v)
Definition: util.h:30
StepCore::Spring::position2
Vector2d position2() const
Set position of the first end of the spring (will be ignored the end is connected) ...
Definition: spring.cc:198
StepCore::Spring::restLength
double restLength() const
Get rest length of the spring.
Definition: spring.h:141
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:43:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

step/stepcore

Skip menu "step/stepcore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal