• 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
constraintsolver.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 "constraintsolver.h"
20 #include "rigidbody.h"
21 #include "particle.h"
22 #include "types.h"
23 #include <unsupported/Eigen/IterativeSolvers>
24 #include <cmath>
25 
26 using namespace Eigen;
27 
28 namespace StepCore {
29 
30 STEPCORE_META_OBJECT(ConstraintSolver, QT_TRANSLATE_NOOP("ObjectClass", "ConstraintSolver"), "ConstraintSolver", MetaObject::ABSTRACT, STEPCORE_SUPER_CLASS(Object),)
31  //STEPCORE_PROPERTY_RW(double, toleranceAbs, QT_TRANSLATE_NOOP("PropertyName", "toleranceAbs"), STEPCORE_UNITS_1, "Allowed absolute tolerance", toleranceAbs, setToleranceAbs)
32  //STEPCORE_PROPERTY_R_D(double, localError, QT_TRANSLATE_NOOP("PropertyName", "localError"), STEPCORE_UNITS_1, "Maximal local error during last step", localError))
33 
34 STEPCORE_META_OBJECT(CGConstraintSolver, QT_TRANSLATE_NOOP("ObjectClass", "CGConstraintSolver"), "CGConstraintSolver", 0,
35  STEPCORE_SUPER_CLASS(ConstraintSolver),)
36 
37 
38 int CGConstraintSolver::solve(ConstraintsInfo* info)
39 {
40  int nc = info->constraintsCount + info->contactsCount;
41 
42  // XXX: make this matrixes permanent to avoid memory allocations
43  SparseRowMatrix a(nc, nc);
44  VectorXd b(nc);
45  VectorXd x(nc);
46  x.setZero();
47 
48  a = info->jacobian * (info->inverseMass.asDiagonal() * info->jacobian.transpose());
49 
50  b = info->jacobian * info->acceleration;
51  b += info->jacobianDerivative * info->velocity;
52  b = - (b + info->value + info->derivative);
53 
54  IterationController iter(2.0E-5); // XXX
55 
56  // print debug info
57  /*std::cout << "ConstraintSolver:" << endl
58  << "J=" << info->jacobian << endl
59  << "J'=" << info->jacobianDerivative << endl
60  << "C=" << info->value << endl
61  << "C'=" << info->derivative << endl
62  << "invM=" << info->inverseMass << endl
63  << "pos=" << info->position << endl
64  << "vel=" << info->velocity << endl
65  << "acc=" << info->acceleration << endl
66  << "a=" << a << endl
67  << "b=" << b << endl
68  << "l=" << l << endl
69  << "force=" << info->force << endl;*/
70 
71  // constrained_cg ?
72  // XXX: limit iterations count
73 
74  // XXX: Use sparce vectors for fmin and fmax
75  int fminCount = 0;
76  int fmaxCount = 0;
77  for(int i=0; i<nc; ++i) {
78  if(std::isinf(info->forceMin[i]) != -1) ++fminCount;
79  if(std::isinf(info->forceMax[i]) != +1) ++fmaxCount;
80  }
81 
82  DynSparseRowMatrix c(fminCount + fmaxCount, nc);
83  VectorXd f(fminCount + fmaxCount);
84 
85  int fminIndex = 0;
86  int fmaxIndex = fminCount;
87  for(int i=0; i<nc; ++i) {
88  if(std::isinf(info->forceMin[i]) != -1) {
89  c.coeffRef(fminIndex,i) = -1;
90  f[fminIndex] = -info->forceMin[i];
91  ++fminIndex;
92  }
93  if(std::isinf(info->forceMax[i]) != +1) {
94  c.coeffRef(fmaxIndex, i) = 1;
95  f[fmaxIndex] = info->forceMax[i];
96  ++fmaxIndex;
97  }
98  }
99  ei_constrained_cg(a, c, x, b, f, iter);
100 
101  info->force = info->jacobian.transpose() * x;
102 
103  // print debug info
104  /*std::cout << "Solved:" << endl
105  << "J=" << info->jacobian << endl
106  << "J'=" << info->jacobianDerivative << endl
107  << "C=" << info->value << endl
108  << "C'=" << info->derivative << endl
109  << "invM=" << info->inverseMass << endl
110  << "pos=" << info->position << endl
111  << "vel=" << info->velocity << endl
112  << "acc=" << info->acceleration << endl
113  << "a=" << a << endl
114  << "b=" << b << endl
115  << "l=" << l << endl
116  << "force=" << info->force << endl << endl << endl;*/
117  return 0;
118 }
119 
120 } // namespace StepCore
121 
rigidbody.h
RigidBody class.
types.h
Type to and from string convertion helpers.
StepCore::SparseRowMatrix
Eigen::SparseMatrix< double, Eigen::RowMajor > SparseRowMatrix
Definition: types.h:36
StepCore::QT_TRANSLATE_NOOP
QT_TRANSLATE_NOOP("ObjectClass","CGConstraintSolver")
constraintsolver.h
ConstraintSolver interface.
StepCore::CGConstraintSolver
Definition: constraintsolver.h:53
STEPCORE_SUPER_CLASS
#define STEPCORE_SUPER_CLASS(_className)
Definition: object.h:380
particle.h
Particle and ChargedParticle classes.
StepCore::DynSparseRowMatrix
Eigen::DynamicSparseMatrix< double, Eigen::RowMajor > DynSparseRowMatrix
Definition: types.h:38
StepCore::ConstraintSolver
Constraint solver interface.
Definition: constraintsolver.h:40
STEPCORE_META_OBJECT
#define STEPCORE_META_OBJECT(_className, _classNameNoop, _description, _flags, __superClasses, __properties)
Definition: object.h:370
StepCore::VectorXd
Eigen::VectorXd VectorXd
Definition: vector.h:38
StepCore::ConstraintsInfo
Constraints information structure XXX: Move it to constraintsolver.h.
Definition: world.h:216
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