File indexing completed on 2024-10-13 06:32:54

0001 /*
0002     SPDX-FileCopyrightText: 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
0003     SPDX-FileCopyrightText: 2014 Inge Wallin <inge@lysator.liu.se>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 /** \file joint.h
0009  *  \brief Contains the Joint object.
0010  */
0011 
0012 #ifndef STEPCORE_JOINT_H
0013 #define STEPCORE_JOINT_H
0014 
0015 
0016 // stdc++
0017 #include <vector> // XXX: Replace if Qt is enabled.
0018 
0019 // Stepcore
0020 #include "types.h"
0021 #include "item.h"
0022 
0023 
0024 namespace StepCore
0025 {
0026 
0027 
0028 /** \ingroup joints
0029  *  Constraints information structure
0030  *  XXX: Move it to constraintsolver.h
0031  */
0032 struct ConstraintsInfo
0033 {
0034     int                variablesCount;      ///< Number of dynamic variables
0035     int                constraintsCount;    ///< Number of constraints equations
0036     int                contactsCount;       ///< Number of additional constrains 
0037                                             ///< equations due to contacts
0038 
0039     VectorXd           value;               ///< Current constraints values (amount of brokenness)
0040     VectorXd           derivative;          ///< Time-derivative of constraints values
0041     DynSparseRowMatrix jacobian;            ///< Position-derivative of constraints values
0042     DynSparseRowMatrix jacobianDerivative;  ///< Time-derivative of constraintsJacobian
0043     VectorXd           inverseMass;         ///< Diagonal coefficients of the inverse mass matrix of the system
0044 
0045     MappedVector       position;            ///< Positions of the bodies
0046     MappedVector       velocity;            ///< Velocities of the bodies
0047     MappedVector       acceleration;        ///< Accelerations of the bodies before applying constraints
0048 
0049     VectorXd           forceMin;            ///< Constraints force lower limit
0050     VectorXd           forceMax;            ///< Constraints force upper limit
0051 
0052     VectorXd           force;               ///< Resulting constraints force
0053 
0054     bool               collisionFlag;       ///< True if there is a collision to be resolved
0055 
0056     ConstraintsInfo(): variablesCount(0), constraintsCount(0), contactsCount(0),
0057                        position(nullptr,0), velocity(nullptr,0), acceleration(nullptr,0) {}
0058 
0059     /** Set variablesCount, constraintsCount and reset contactsCount,
0060      *  resize all arrays appropriately */
0061     void setDimension(int newVariablesCount, int newConstraintsCount, int newContactsCount = 0);
0062 
0063     /** Clear the structure */
0064     void clear();
0065 
0066 private:
0067     ConstraintsInfo(const ConstraintsInfo&);
0068     ConstraintsInfo& operator=(const ConstraintsInfo&);
0069 };
0070 
0071 
0072 /** \ingroup joints
0073  *  \brief Interface for joints
0074  */
0075 class Joint : public Item
0076 {
0077     STEPCORE_OBJECT(Joint)
0078 
0079 public:
0080     virtual ~Joint() {}
0081 
0082     /** Get count of constraints */
0083     virtual int constraintsCount() = 0;
0084 
0085     /** Fill the part of constraints information structure starting at offset */
0086     virtual void getConstraintsInfo(ConstraintsInfo* info, int offset) = 0;
0087 
0088 #if 0
0089     /** Get current constraints value (amount of brokenness) and its derivative */
0090     virtual void getConstraints(double* value, double* derivative) = 0;
0091 
0092     /** Get force limits, default is no limits at all */
0093     virtual void getForceLimits(double* forceMin STEPCORE_UNUSED, double* forceMax STEPCORE_UNUSED) {}
0094 
0095     /** Get constraints jacobian (space-derivatives of constraint value),
0096      *  its derivative and product of inverse mass matrix by transposed jacobian (wjt) */
0097     virtual void getJacobian(GmmSparseRowMatrix* value, GmmSparseRowMatrix* derivative, int offset) = 0;
0098 #endif
0099 };
0100 
0101 
0102 /** List of pointers to Joint */
0103 typedef std::vector<Joint*> JointList;
0104 
0105 
0106 } // namespace StepCore
0107 
0108 
0109 #endif