dart
HierarchicalIK.hpp
1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_DYNAMICS_HIERARCHICALIK_HPP_
34 #define DART_DYNAMICS_HIERARCHICALIK_HPP_
35 
36 #include <unordered_set>
37 
38 #include "dart/dynamics/InverseKinematics.hpp"
39 
40 namespace dart {
41 namespace dynamics {
42 
48 typedef std::vector<std::vector<std::shared_ptr<InverseKinematics> > >
49  IKHierarchy;
50 
64 {
65 public:
67  virtual ~HierarchicalIK() = default;
68 
75  DART_DEPRECATED(6.8)
76  bool solve(bool applySolution = true);
77 
83  DART_DEPRECATED(6.8)
84  bool solve(Eigen::VectorXd& positions, bool applySolution = true);
85 
94  bool findSolution(Eigen::VectorXd& positions);
95 
106  bool solveAndApply(bool allowIncompleteResult = true);
107 
121  bool solveAndApply(
122  Eigen::VectorXd& positions, bool allowIncompleteResult = true);
123 
125  virtual std::shared_ptr<HierarchicalIK> clone(
126  const SkeletonPtr& _newSkel) const = 0;
127 
134  class Function
135  {
136  public:
138  virtual optimization::FunctionPtr clone(
139  const std::shared_ptr<HierarchicalIK>& _newIK) const = 0;
140 
142  virtual ~Function() = default;
143  };
144 
146  void setObjective(const std::shared_ptr<optimization::Function>& _objective);
147 
149  const std::shared_ptr<optimization::Function>& getObjective();
150 
152  std::shared_ptr<const optimization::Function> getObjective() const;
153 
156  const std::shared_ptr<optimization::Function>& _nsObjective);
157 
159  const std::shared_ptr<optimization::Function>& getNullSpaceObjective();
160 
162  std::shared_ptr<const optimization::Function> getNullSpaceObjective() const;
163 
165  bool hasNullSpaceObjective() const;
166 
168  const std::shared_ptr<optimization::Problem>& getProblem();
169 
171  std::shared_ptr<const optimization::Problem> getProblem() const;
172 
179  void resetProblem(bool _clearSeeds = false);
180 
183  void setSolver(const std::shared_ptr<optimization::Solver>& _newSolver);
184 
186  const std::shared_ptr<optimization::Solver>& getSolver();
187 
189  std::shared_ptr<const optimization::Solver> getSolver() const;
190 
192  virtual void refreshIKHierarchy() = 0;
193 
195  const IKHierarchy& getIKHierarchy() const;
196 
198  const std::vector<Eigen::MatrixXd>& computeNullSpaces() const;
199 
202  Eigen::VectorXd getPositions() const;
203 
206  void setPositions(const Eigen::VectorXd& _q);
207 
209  SkeletonPtr getSkeleton();
210 
212  ConstSkeletonPtr getSkeleton() const;
213 
216  SkeletonPtr getAffiliation();
217 
220  ConstSkeletonPtr getAffiliation() const;
221 
224  void clearCaches();
225 
226 protected:
232  class Objective final : public Function, public optimization::Function
233  {
234  public:
236  Objective(const std::shared_ptr<HierarchicalIK>& _ik);
237 
239  virtual ~Objective() = default;
240 
241  // Documentation inherited
242  optimization::FunctionPtr clone(
243  const std::shared_ptr<HierarchicalIK>& _newIK) const override;
244 
245  // Documentation inherited
246  double eval(const Eigen::VectorXd& _x) override;
247 
248  // Documentation inherited
249  void evalGradient(
250  const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad) override;
251 
252  protected:
254  std::weak_ptr<HierarchicalIK> mIK;
255 
257  Eigen::VectorXd mGradCache;
258  };
259 
265  class Constraint final : public Function, public optimization::Function
266  {
267  public:
269  Constraint(const std::shared_ptr<HierarchicalIK>& _ik);
270 
272  virtual ~Constraint() = default;
273 
274  // Documentation inherited
275  optimization::FunctionPtr clone(
276  const std::shared_ptr<HierarchicalIK>& _newIK) const override;
277 
278  // Documentation inherited
279  double eval(const Eigen::VectorXd& _x) override;
280 
281  // Documentation inherited
282  void evalGradient(
283  const Eigen::VectorXd& _x, Eigen::Map<Eigen::VectorXd> _grad) override;
284 
285  protected:
287  std::weak_ptr<HierarchicalIK> mIK;
288 
290  Eigen::VectorXd mLevelGradCache;
291 
293  Eigen::VectorXd mTempGradCache;
294  };
295 
297  HierarchicalIK(const SkeletonPtr& _skeleton);
298 
300  void initialize(const std::shared_ptr<HierarchicalIK>& my_ptr);
301 
304  void copyOverSetup(const std::shared_ptr<HierarchicalIK>& _otherIK) const;
305 
307  WeakSkeletonPtr mSkeleton;
308 
310  IKHierarchy mHierarchy;
311 
313  std::shared_ptr<optimization::Problem> mProblem;
314 
316  std::shared_ptr<optimization::Solver> mSolver;
317 
319  optimization::FunctionPtr mObjective;
320 
322  optimization::FunctionPtr mNullSpaceObjective;
323 
325  std::weak_ptr<HierarchicalIK> mPtr;
326 
328  mutable Eigen::VectorXd mLastPositions;
329 
331  mutable std::vector<Eigen::MatrixXd> mNullSpaceCache;
332 
334  mutable Eigen::MatrixXd mPartialNullspaceCache;
335 
337  mutable Eigen::JacobiSVD<math::Jacobian> mSVDCache;
338 
340  mutable math::Jacobian mJacCache;
341 
342 public:
343  // To get byte-aligned Eigen vectors
344  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
345 };
346 
351 {
352 public:
353  typedef std::unordered_set<std::shared_ptr<InverseKinematics> > ModuleSet;
354  typedef std::unordered_set<std::shared_ptr<const InverseKinematics> >
355  ConstModuleSet;
356 
358  static std::shared_ptr<CompositeIK> create(const SkeletonPtr& _skel);
359 
360  // Documentation inherited
361  std::shared_ptr<HierarchicalIK> clone(
362  const SkeletonPtr& _newSkel) const override;
363 
365  virtual std::shared_ptr<CompositeIK> cloneCompositeIK(
366  const SkeletonPtr& _newSkel) const;
367 
371  bool addModule(const std::shared_ptr<InverseKinematics>& _ik);
372 
374  const ModuleSet& getModuleSet();
375 
377  ConstModuleSet getModuleSet() const;
378 
379  // Documentation inherited
380  void refreshIKHierarchy() override;
381 
382 protected:
384  CompositeIK(const SkeletonPtr& _skel);
385 
387  std::unordered_set<std::shared_ptr<InverseKinematics> > mModuleSet;
388 };
389 
394 {
395 public:
397  static std::shared_ptr<WholeBodyIK> create(const SkeletonPtr& _skel);
398 
399  // Documentation inherited
400  std::shared_ptr<HierarchicalIK> clone(
401  const SkeletonPtr& _newSkel) const override;
402 
404  virtual std::shared_ptr<WholeBodyIK> cloneWholeBodyIK(
405  const SkeletonPtr& _newSkel) const;
406 
407  // Documentation inherited
408  void refreshIKHierarchy() override;
409 
410 protected:
412  WholeBodyIK(const SkeletonPtr& _skel);
413 };
414 
415 } // namespace dynamics
416 } // namespace dart
417 
418 #endif // DART_DYNAMICS_HIERARCHICALIK_HPP_
const std::vector< Eigen::MatrixXd > & computeNullSpaces() const
Compute the null spaces of each level of the hierarchy.
Definition: HierarchicalIK.cpp:252
const std::shared_ptr< optimization::Problem > & getProblem()
Get the Problem that is being maintained by this HierarchicalIK module.
Definition: HierarchicalIK.cpp:196
Eigen::VectorXd mLastPositions
Cache for the last positions.
Definition: HierarchicalIK.hpp:328
The Subject class is a base class for any object that wants to report when it gets destroyed...
Definition: Subject.hpp:57
Eigen::VectorXd mLevelGradCache
Cache for the gradient of a level.
Definition: HierarchicalIK.hpp:290
void copyOverSetup(const std::shared_ptr< HierarchicalIK > &_otherIK) const
Copy the setup of this HierarchicalIK module into another HierarchicalIK module.
Definition: HierarchicalIK.cpp:621
virtual std::shared_ptr< HierarchicalIK > clone(const SkeletonPtr &_newSkel) const =0
Clone this HierarchicalIK module.
math::Jacobian mJacCache
Cache for Jacobians.
Definition: HierarchicalIK.hpp:340
Eigen::VectorXd mTempGradCache
Cache for temporary gradients.
Definition: HierarchicalIK.hpp:293
SkeletonPtr getSkeleton()
Get the Skeleton that this IK module is associated with.
Definition: HierarchicalIK.cpp:366
virtual void refreshIKHierarchy()=0
Refresh the IK hierarchy of this IK module.
void setNullSpaceObjective(const std::shared_ptr< optimization::Function > &_nsObjective)
Set the null space objective for this HierarchicalIK.
Definition: HierarchicalIK.cpp:169
optimization::FunctionPtr mNullSpaceObjective
The null space Objective of this IK module.
Definition: HierarchicalIK.hpp:322
const std::shared_ptr< optimization::Function > & getObjective()
Get the objective function for this HierarchicalIK.
Definition: HierarchicalIK.cpp:156
IKHierarchy mHierarchy
Cache for the IK hierarcy.
Definition: HierarchicalIK.hpp:310
void initialize(const std::shared_ptr< HierarchicalIK > &my_ptr)
Setup the module.
Definition: HierarchicalIK.cpp:590
Definition: Aspect.cpp:40
SkeletonPtr getAffiliation()
This is the same as getSkeleton().
Definition: HierarchicalIK.cpp:378
This class should be inherited by optimization::Function classes that have a dependency on the Hierar...
Definition: HierarchicalIK.hpp:134
void resetProblem(bool _clearSeeds=false)
Reset the Problem that is being maintained by this HierarchicalIK module.
Definition: HierarchicalIK.cpp:208
bool hasNullSpaceObjective() const
Returns true if this HierarchicalIK has a null space objective.
Definition: HierarchicalIK.cpp:190
void clearCaches()
Clear the caches of this IK module.
Definition: HierarchicalIK.cpp:390
std::weak_ptr< HierarchicalIK > mIK
Pointer to this Constraint&#39;s HierarchicalIK module.
Definition: HierarchicalIK.hpp:287
void setPositions(const Eigen::VectorXd &_q)
Set the current joint positions of the Skeleton associated with this IK module.
Definition: HierarchicalIK.cpp:358
Eigen::MatrixXd mPartialNullspaceCache
Cache for a partial null space computation.
Definition: HierarchicalIK.hpp:334
std::shared_ptr< optimization::Solver > mSolver
The Solver that this IK module will use.
Definition: HierarchicalIK.hpp:316
Eigen::VectorXd getPositions() const
Get the current joint positions of the Skeleton associated with this IK module.
Definition: HierarchicalIK.cpp:348
The HierarchicalIK::Constraint Function is simply used to merge the constraints of the InverseKinemat...
Definition: HierarchicalIK.hpp:265
virtual optimization::FunctionPtr clone(const std::shared_ptr< HierarchicalIK > &_newIK) const =0
Enable this function to be cloned to a new IK module.
Eigen::JacobiSVD< math::Jacobian > mSVDCache
Cache for the null space SVD.
Definition: HierarchicalIK.hpp:337
The WholeBodyIK class provides an interface for simultaneously solving all the IK constraints of all ...
Definition: HierarchicalIK.hpp:393
Definition: Function.hpp:45
std::shared_ptr< optimization::Problem > mProblem
The Problem that this IK module is maintaining.
Definition: HierarchicalIK.hpp:313
std::unordered_set< std::shared_ptr< InverseKinematics > > mModuleSet
The set of modules being used by this CompositeIK.
Definition: HierarchicalIK.hpp:387
The HierarchicalIK class provides a convenient way of setting up a hierarchical inverse kinematics op...
Definition: HierarchicalIK.hpp:63
bool solveAndApply(bool allowIncompleteResult=true)
Identical to findSolution(), but this function applies the solution when the solver successfully foun...
Definition: HierarchicalIK.cpp:129
bool findSolution(Eigen::VectorXd &positions)
Finds a solution of the IK problem without applying the solution.
Definition: HierarchicalIK.cpp:67
Eigen::VectorXd mGradCache
Cache for the gradient computation.
Definition: HierarchicalIK.hpp:257
const IKHierarchy & getIKHierarchy() const
Get the IK hierarchy of this IK module.
Definition: HierarchicalIK.cpp:246
void setSolver(const std::shared_ptr< optimization::Solver > &_newSolver)
Set the Solver that should be used by this IK module, and set it up with the Problem that is configur...
Definition: HierarchicalIK.cpp:223
virtual ~Function()=default
Virtual destructor.
const std::shared_ptr< optimization::Function > & getNullSpaceObjective()
Get the null space objective for this HierarchicalIK.
Definition: HierarchicalIK.cpp:177
std::vector< Eigen::MatrixXd > mNullSpaceCache
Cache for null space computations.
Definition: HierarchicalIK.hpp:331
WeakSkeletonPtr mSkeleton
Pointer to the Skeleton that this IK is tied to.
Definition: HierarchicalIK.hpp:307
The HierarchicalIK::Objective Function is simply used to merge the objective and null space objective...
Definition: HierarchicalIK.hpp:232
void setObjective(const std::shared_ptr< optimization::Function > &_objective)
Set the objective function for this HierarchicalIK.
Definition: HierarchicalIK.cpp:149
HierarchicalIK(const SkeletonPtr &_skeleton)
Constructor.
Definition: HierarchicalIK.cpp:582
bool solve(bool applySolution=true)
Solve the IK Problem.
Definition: HierarchicalIK.cpp:44
optimization::FunctionPtr mObjective
The Objective of this IK module.
Definition: HierarchicalIK.hpp:319
virtual ~HierarchicalIK()=default
Virtual destructor.
The CompositeIK class allows you to specify an arbitrary hierarchy of InverseKinematics modules for a...
Definition: HierarchicalIK.hpp:350
const std::shared_ptr< optimization::Solver > & getSolver()
Get the Solver that is being used by this IK module.
Definition: HierarchicalIK.cpp:234
std::weak_ptr< HierarchicalIK > mIK
Pointer to this Objective&#39;s HierarchicalIK module.
Definition: HierarchicalIK.hpp:254
std::weak_ptr< HierarchicalIK > mPtr
Weak pointer to self.
Definition: HierarchicalIK.hpp:325