Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#pragma once
#include <sofa/component/constraint/lagrangian/model/config.h>

#include <sofa/core/behavior/Constraint.h>
#include <sofa/core/behavior/LagrangianConstraint.h>
#include <sofa/core/behavior/ConstraintResolution.h>
#include <sofa/core/behavior/MechanicalState.h>
#include <sofa/core/behavior/OdeSolver.h>
Expand All @@ -32,18 +32,18 @@ namespace sofa::component::constraint::lagrangian::model
{

template< class DataTypes >
class FixedLagrangianConstraint : public core::behavior::Constraint<DataTypes>
class FixedLagrangianConstraint : public core::behavior::LagrangianConstraint<DataTypes>
{
public:
SOFA_CLASS(SOFA_TEMPLATE(FixedLagrangianConstraint,DataTypes), SOFA_TEMPLATE(core::behavior::Constraint,DataTypes));
SOFA_CLASS(SOFA_TEMPLATE(FixedLagrangianConstraint,DataTypes), SOFA_TEMPLATE(core::behavior::LagrangianConstraint,DataTypes));

typedef typename DataTypes::VecCoord VecCoord;
typedef typename DataTypes::VecDeriv VecDeriv;
typedef typename DataTypes::Coord Coord;
typedef typename DataTypes::Deriv Deriv;
typedef typename DataTypes::MatrixDeriv MatrixDeriv;
typedef typename core::behavior::MechanicalState<DataTypes> MechanicalState;
typedef typename core::behavior::Constraint<DataTypes> Inherit;
typedef typename core::behavior::LagrangianConstraint<DataTypes> Inherit;

typedef typename DataTypes::MatrixDeriv::RowIterator MatrixDerivRowIterator;
typedef core::objectmodel::Data<VecCoord> DataVecCoord;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#pragma once
#include <sofa/component/constraint/lagrangian/model/config.h>

#include <sofa/core/behavior/Constraint.h>
#include <sofa/core/behavior/LagrangianConstraint.h>
#include <sofa/core/behavior/ConstraintResolution.h>
#include <sofa/core/behavior/MechanicalState.h>
#include <sofa/core/behavior/OdeSolver.h>
Expand Down Expand Up @@ -65,10 +65,10 @@ class StopperLagrangianConstraintResolution1Dof : public core::behavior::Constra
};

template< class DataTypes >
class StopperLagrangianConstraint : public core::behavior::Constraint<DataTypes>
class StopperLagrangianConstraint : public core::behavior::LagrangianConstraint<DataTypes>
{
public:
SOFA_CLASS(SOFA_TEMPLATE(StopperLagrangianConstraint,DataTypes), SOFA_TEMPLATE(core::behavior::Constraint,DataTypes));
SOFA_CLASS(SOFA_TEMPLATE(StopperLagrangianConstraint,DataTypes), SOFA_TEMPLATE(core::behavior::LagrangianConstraint,DataTypes));

typedef typename DataTypes::VecCoord VecCoord;
typedef typename DataTypes::VecDeriv VecDeriv;
Expand All @@ -77,7 +77,7 @@ class StopperLagrangianConstraint : public core::behavior::Constraint<DataTypes>
typedef typename DataTypes::MatrixDeriv MatrixDeriv;
typedef typename Coord::value_type Real;
typedef typename core::behavior::MechanicalState<DataTypes> MechanicalState;
typedef typename core::behavior::Constraint<DataTypes> Inherit;
typedef typename core::behavior::LagrangianConstraint<DataTypes> Inherit;

typedef typename DataTypes::MatrixDeriv::RowIterator MatrixDerivRowIterator;
typedef core::objectmodel::Data<VecCoord> DataVecCoord;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
#pragma once
#include <sofa/component/constraint/lagrangian/model/config.h>

#include <sofa/core/behavior/Constraint.h>
#include <sofa/core/behavior/LagrangianConstraint.h>

namespace sofa::component::constraint::lagrangian::model
{

template < class DataTypes >
class UniformLagrangianConstraint : public sofa::core::behavior::Constraint< DataTypes >
class UniformLagrangianConstraint : public sofa::core::behavior::LagrangianConstraint< DataTypes >
{
public:
SOFA_CLASS(SOFA_TEMPLATE(UniformLagrangianConstraint, DataTypes), SOFA_TEMPLATE(sofa::core::behavior::Constraint, DataTypes));
SOFA_CLASS(SOFA_TEMPLATE(UniformLagrangianConstraint, DataTypes), SOFA_TEMPLATE(sofa::core::behavior::LagrangianConstraint, DataTypes));

typedef typename DataTypes::VecCoord VecCoord;
typedef typename DataTypes::VecDeriv VecDeriv;
Expand Down
10 changes: 6 additions & 4 deletions Sofa/framework/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ set(HEADER_FILES
${SRC_ROOT}/behavior/BaseProjectiveConstraintSet.h
${SRC_ROOT}/behavior/BaseRotationFinder.h
${SRC_ROOT}/behavior/BaseVectorOperations.h
${SRC_ROOT}/behavior/BlockType.h
${SRC_ROOT}/behavior/BlocMatrixWriter.h
${SRC_ROOT}/behavior/BlockType.h
${SRC_ROOT}/behavior/Constraint.h
${SRC_ROOT}/behavior/Constraint.inl
${SRC_ROOT}/behavior/ConstraintCorrection.h
Expand All @@ -80,11 +80,13 @@ set(HEADER_FILES
${SRC_ROOT}/behavior/DefaultMultiMatrixAccessor.h
${SRC_ROOT}/behavior/ForceField.h
${SRC_ROOT}/behavior/ForceField.inl
${SRC_ROOT}/behavior/LagrangianConstraint.h
${SRC_ROOT}/behavior/LagrangianConstraint.inl
${SRC_ROOT}/behavior/LinearSolver.h
${SRC_ROOT}/behavior/LinearSolverAccessor.h
${SRC_ROOT}/behavior/MatrixAPICompatibility.h
${SRC_ROOT}/behavior/Mass.h
${SRC_ROOT}/behavior/Mass.inl
${SRC_ROOT}/behavior/MatrixAPICompatibility.h
${SRC_ROOT}/behavior/MechanicalMatrix.h
${SRC_ROOT}/behavior/MechanicalState.h
${SRC_ROOT}/behavior/MechanicalState.inl
Expand All @@ -106,10 +108,10 @@ set(HEADER_FILES
${SRC_ROOT}/behavior/ProjectiveConstraintSet.h
${SRC_ROOT}/behavior/ProjectiveConstraintSet.inl
${SRC_ROOT}/behavior/RotationFinder.h
${SRC_ROOT}/behavior/StateAccessor.h
${SRC_ROOT}/behavior/SingleMatrixAccessor.h
${SRC_ROOT}/behavior/SingleStateAccessor.h
${SRC_ROOT}/behavior/SingleStateAccessor.inl
${SRC_ROOT}/behavior/StateAccessor.h
${SRC_ROOT}/behavior/fwd.h
${SRC_ROOT}/collision/BroadPhaseDetection.h
${SRC_ROOT}/collision/CollisionAlgorithm.h
Expand Down Expand Up @@ -246,12 +248,12 @@ set(SOURCE_FILES
${SRC_ROOT}/behavior/BaseMechanicalState.cpp
${SRC_ROOT}/behavior/BaseOrderingMethod.cpp
${SRC_ROOT}/behavior/BaseProjectiveConstraintSet.cpp
${SRC_ROOT}/behavior/Constraint.cpp
${SRC_ROOT}/behavior/ConstraintCorrection.cpp
${SRC_ROOT}/behavior/ConstraintResolution.cpp
${SRC_ROOT}/behavior/ConstraintSolver.cpp
${SRC_ROOT}/behavior/DefaultMultiMatrixAccessor.cpp
${SRC_ROOT}/behavior/ForceField.cpp
${SRC_ROOT}/behavior/LagrangianConstraint.cpp
${SRC_ROOT}/behavior/LinearSolver.cpp
${SRC_ROOT}/behavior/LinearSolverAccessor.cpp
${SRC_ROOT}/behavior/Mass.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace sofa::core::behavior
* given its current position and velocity.
*
* Constraints can be internal to a given body (attached to one MechanicalState,
* see the Constraint class), or link several bodies together (such as contacts,
* see the LagrangianConstraint class), or link several bodies together (such as contacts,
* see the InteractionConstraint class).
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace sofa::core::behavior
*
* This class only contains the data of the body and not any of its
* <i>active</i> computations, which are handled by the Mass, ForceField, and
* Constraint components.
* LagrangianConstraint components.
*
* Two types of vectors are used :
* \li \code VecCoord \endcode : containing positions.
Expand Down
123 changes: 4 additions & 119 deletions Sofa/framework/Core/src/sofa/core/behavior/Constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,127 +21,12 @@
******************************************************************************/
#pragma once

#include <sofa/core/config.h>
#include <sofa/core/behavior/BaseLagrangianConstraint.h>
#include <sofa/core/behavior/MechanicalState.h>
#include <sofa/core/behavior/LagrangianConstraint.h>

#include <sofa/core/behavior/SingleStateAccessor.h>
#include <sofa/core/config.h>
SOFA_HEADER_DEPRECATED("v25.12", "v26.12", "sofa/core/behavior/LagrangianConstraint.h")

namespace sofa::core::behavior
{

/**
* \brief Component computing constraints within a simulated body.
*
* This class define the abstract API common to constraints using a given type
* of DOFs.
* A Constraint computes constraints applied to one simulated body given its
* current position and velocity.
*
*/
template<class DataTypes>
class Constraint : public BaseLagrangianConstraint, public SingleStateAccessor<DataTypes>
{
public:
SOFA_CLASS2(SOFA_TEMPLATE(Constraint, DataTypes), BaseLagrangianConstraint, SOFA_TEMPLATE(SingleStateAccessor, DataTypes));

typedef typename DataTypes::Real Real;
typedef typename DataTypes::VecCoord VecCoord;
typedef typename DataTypes::VecDeriv VecDeriv;
typedef typename DataTypes::Coord Coord;
typedef typename DataTypes::Deriv Deriv;
typedef typename DataTypes::MatrixDeriv MatrixDeriv;

typedef core::objectmodel::Data<VecCoord> DataVecCoord;
typedef core::objectmodel::Data<VecDeriv> DataVecDeriv;
typedef core::objectmodel::Data<MatrixDeriv> DataMatrixDeriv;
protected:
Constraint(MechanicalState<DataTypes> *mm = nullptr);

~Constraint() override;

virtual void init() override;
public:
Data<Real> endTime; ///< The constraint stops acting after the given value. Use a negative value for infinite constraints
virtual bool isActive() const; ///< if false, the constraint does nothing

using BaseConstraintSet::getConstraintViolation;

/// Construct the Constraint violations vector of each constraint
///
/// \param v is the result vector that contains the whole constraints violations
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) override;

/// Construct the Constraint violations vector of each constraint
///
/// \param resV is the result vector that contains the whole constraints violations
/// \param x is the position vector used to compute constraint position violation
/// \param v is the velocity vector used to compute constraint velocity violation
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
///
/// This is the method that should be implemented by the component
virtual void getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *resV, const DataVecCoord &x, const DataVecDeriv &v) = 0;


/// Construct the Jacobian Matrix
///
/// \param cId is the result constraint sparse matrix Id
/// \param cIndex is the index of the next constraint equation: when building the constraint matrix, you have to use this index, and then update it
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
void buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex) override;

/// Construct the Jacobian Matrix
///
/// \param c is the result constraint sparse matrix
/// \param cIndex is the index of the next constraint equation: when building the constraint matrix, you have to use this index, and then update it
/// \param x is the position vector used for constraint equation computation
/// \param cParams defines the state vectors to use for positions and velocities. Also defines the order of the constraint (POS, VEL, ACC)
///
/// This is the method that should be implemented by the component
virtual void buildConstraintMatrix(const ConstraintParams* cParams, DataMatrixDeriv & c, unsigned int &cIndex, const DataVecCoord &x) = 0;


void storeLambda(const ConstraintParams* cParams, MultiVecDerivId res, const sofa::linearalgebra::BaseVector* lambda) override;

/// Pre-construction check method called by ObjectFactory.
/// Check that DataTypes matches the MechanicalState.
template<class T>
static bool canCreate(T*& obj, objectmodel::BaseContext* context, objectmodel::BaseObjectDescription* arg)
{
if (dynamic_cast<MechanicalState<DataTypes>*>(context->getMechanicalState()) == nullptr) {
arg->logError("No mechanical state with the datatype '" + std::string(DataTypes::Name()) + "' found in the context node.");
return false;
}

return BaseObject::canCreate(obj, context, arg);
}

virtual type::vector<std::string> getBaseConstraintIdentifiers() override final
{
type::vector<std::string> ids = getConstraintIdentifiers();
ids.push_back("Constraint");
return ids;
}

protected:

virtual type::vector<std::string> getConstraintIdentifiers(){ return {}; }


private:
void storeLambda(const ConstraintParams* cParams, Data<VecDeriv>& resId, const Data<MatrixDeriv>& jacobian, const sofa::linearalgebra::BaseVector* lambda);
};

#if !defined(SOFA_CORE_BEHAVIOR_CONSTRAINT_CPP)
extern template class SOFA_CORE_API Constraint<defaulttype::Vec6Types>;
extern template class SOFA_CORE_API Constraint<defaulttype::Vec3Types>;
extern template class SOFA_CORE_API Constraint<defaulttype::Vec2Types>;
extern template class SOFA_CORE_API Constraint<defaulttype::Vec1Types>;
extern template class SOFA_CORE_API Constraint<defaulttype::Rigid3Types>;
extern template class SOFA_CORE_API Constraint<defaulttype::Rigid2Types>;


#endif
} // namespace sofa::core::behavior
using Constraint SOFA_ATTRIBUTE_DEPRECATED("v25.12", "v26.12", "Constraint has been renamed to LagrangianConstraint") = LagrangianConstraint<DataTypes>;
}
73 changes: 2 additions & 71 deletions Sofa/framework/Core/src/sofa/core/behavior/Constraint.inl
Original file line number Diff line number Diff line change
Expand Up @@ -21,75 +21,6 @@
******************************************************************************/
#pragma once

#include <sofa/core/behavior/Constraint.h>
#include <sofa/core/ConstraintParams.h>
#include <sofa/core/behavior/LagrangianConstraint.inl>

namespace sofa::core::behavior
{

template<class DataTypes>
Constraint<DataTypes>::Constraint(MechanicalState<DataTypes> *mm)
: Inherit1(), Inherit2(mm)
, endTime( initData(&endTime,(Real)-1,"endTime","The constraint stops acting after the given value.\nUse a negative value for infinite constraints") )
{
}

template<class DataTypes>
Constraint<DataTypes>::~Constraint()
{
}


template <class DataTypes>
bool Constraint<DataTypes>::isActive() const
{
if( endTime.getValue()<0 ) return true;
return endTime.getValue()>getContext()->getTime();
}

template <class DataTypes>
void Constraint<DataTypes>::init()
{
Inherit1::init();
Inherit2::init();
}

template<class DataTypes>
void Constraint<DataTypes>::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v)
{
if (cParams)
{
getConstraintViolation(cParams, v, *cParams->readX(this->mstate.get()), *cParams->readV(this->mstate.get()));
}
}


template<class DataTypes>
void Constraint<DataTypes>::buildConstraintMatrix(const ConstraintParams* cParams, MultiMatrixDerivId cId, unsigned int &cIndex)
{
if (cParams)
{
buildConstraintMatrix(cParams, *cId[this->mstate.get()].write(), cIndex, *cParams->readX(this->mstate.get()));
}
}


template<class DataTypes>
void Constraint<DataTypes>::storeLambda(const ConstraintParams* cParams, MultiVecDerivId res, const sofa::linearalgebra::BaseVector* lambda)
{
if (cParams)
{
storeLambda(cParams, *res[this->mstate.get()].write(), *cParams->readJ(this->mstate.get()), lambda);
}
}

template<class DataTypes>
void Constraint<DataTypes>::storeLambda(const ConstraintParams*, Data<VecDeriv>& result, const Data<MatrixDeriv>& jacobian, const sofa::linearalgebra::BaseVector* lambda)
{
auto res = sofa::helper::getWriteAccessor(result);
const MatrixDeriv& j = jacobian.getValue();
j.multTransposeBaseVector(res, lambda ); // lambda is a vector of scalar value so block size is one.
}


} // namespace sofa::core::behavior
SOFA_HEADER_DEPRECATED("v25.12", "v26.12", "sofa/core/behavior/LagrangianConstraint.inl")
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ namespace sofa::core::behavior
{

using namespace sofa::defaulttype;
template class SOFA_CORE_API Constraint<Vec6Types>;
template class SOFA_CORE_API Constraint<Vec3Types>;
template class SOFA_CORE_API Constraint<Vec2Types>;
template class SOFA_CORE_API Constraint<Vec1Types>;
template class SOFA_CORE_API Constraint<Rigid3Types>;
template class SOFA_CORE_API Constraint<Rigid2Types>;
template class SOFA_CORE_API LagrangianConstraint<Vec6Types>;
template class SOFA_CORE_API LagrangianConstraint<Vec3Types>;
template class SOFA_CORE_API LagrangianConstraint<Vec2Types>;
template class SOFA_CORE_API LagrangianConstraint<Vec1Types>;
template class SOFA_CORE_API LagrangianConstraint<Rigid3Types>;
template class SOFA_CORE_API LagrangianConstraint<Rigid2Types>;


} // namespace sofa::core::behavior
Loading