Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spring] Add fixed weak constraint and examples #4750

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7ec50b2
Add fixed weak constraint and examples
bakpaul May 21, 2024
01f9faf
Remove nasty using
bakpaul May 21, 2024
bc1916b
WIP
bakpaul Jun 5, 2024
066c42e
Merge branch 'master' into add_fixed_weak_constraint
bakpaul Jul 26, 2024
01dab0f
Fix compilation
bakpaul Jul 26, 2024
f6bb00b
Added base virtualization for restShape spring forcefield. Still need…
bakpaul Jul 26, 2024
19b33f6
Merge branch 'master' into add_fixed_weak_constraint
bakpaul Dec 2, 2024
5daad92
Merge branch 'master' into add_fixed_weak_constraint
bakpaul Dec 9, 2024
06090ff
WIP: SQtill need to unify inint, reinit, bwdinint, enables fixAll and…
bakpaul Dec 9, 2024
909c760
Factorise every method in the FixedWeakConstraint object. Need to spe…
bakpaul Dec 10, 2024
937acc8
Apply changes to RestShapeSpringForceField
bakpaul Dec 10, 2024
0754eb8
apply changes to scenes
bakpaul Dec 10, 2024
a3c9866
Merge branch 'master' into add_fixed_weak_constraint
bakpaul Dec 10, 2024
a13b3ca
Remove duplication of the topology link in RestShapeSpringForceField
bakpaul Dec 11, 2024
885e980
Squashed commit of the following:
bakpaul Jan 23, 2025
aba4fb2
Merge branch 'master' into add_fixed_weak_constraint
bakpaul Jan 23, 2025
21a6198
Merge branch 'master' into add_fixed_weak_constraint
bakpaul Jan 23, 2025
5e455e4
Fix compilation
bakpaul Jan 23, 2025
1985599
Try to fix windows compilation
bakpaul Jan 23, 2025
b4e8d43
Avoid using an intermediate expression and directly use the right term
bakpaul Jan 23, 2025
eada37f
Squashed commit of the following:
bakpaul Feb 28, 2025
bb78caf
Fix compilaiton
bakpaul Feb 28, 2025
d00d06c
Merge branch 'master' into add_fixed_weak_constraint
bakpaul Feb 28, 2025
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
3 changes: 3 additions & 0 deletions Sofa/Component/SolidMechanics/Spring/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ set(HEADER_FILES
# ${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/BoxStiffSpringForceField.inl
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/FastTriangularBendingSprings.h
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/FastTriangularBendingSprings.inl
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/FixedWeakConstraint.h
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/FixedWeakConstraint.inl
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/FrameSpringForceField.h
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/FrameSpringForceField.inl
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/GearSpringForceField.h
Expand Down Expand Up @@ -58,6 +60,7 @@ set(SOURCE_FILES
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/AngularSpringForceField.cpp
# ${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/BoxStiffSpringForceField.cpp
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/FastTriangularBendingSprings.cpp
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/FixedWeakConstraint.cpp
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/FrameSpringForceField.cpp
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/GearSpringForceField.cpp
${SOFACOMPONENTSOLIDMECHANICSSPRING_SOURCE_DIR}/JointSpring.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#define SOFA_COMPONENT_FORCEFIELD_FixedWeakConstraint_CPP

#include <sofa/component/solidmechanics/spring/FixedWeakConstraint.inl>

#include <sofa/helper/visual/DrawTool.h>
#include <sofa/core/ObjectFactory.h>
#include <sofa/core/behavior/MultiMatrixAccessor.h>


namespace sofa::component::solidmechanics::spring
{

using namespace sofa::defaulttype;

void registerFixedWeakConstraint(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(core::ObjectRegistrationData("Elastic springs generating forces on degrees of freedom between their current and rest shape position.")
.add< FixedWeakConstraint<Vec3Types> >()
.add< FixedWeakConstraint<Vec1Types> >()
.add< FixedWeakConstraint<Rigid3Types> >());
}

template class SOFA_COMPONENT_SOLIDMECHANICS_SPRING_API FixedWeakConstraint<Vec3Types>;
template class SOFA_COMPONENT_SOLIDMECHANICS_SPRING_API FixedWeakConstraint<Vec1Types>;
template class SOFA_COMPONENT_SOLIDMECHANICS_SPRING_API FixedWeakConstraint<Rigid3Types>;

} // namespace sofa::component::solidmechanics::spring
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once
#include <sofa/component/solidmechanics/spring/config.h>

#include <sofa/type/RGBAColor.h>

#include <sofa/core/behavior/ForceField.h>
#include <sofa/core/objectmodel/Data.h>
#include <sofa/core/topology/TopologySubsetIndices.h>
#include <sofa/type/vector.h>
#include <sofa/linearalgebra/EigenSparseMatrix.h>
#include <sofa/core/objectmodel/lifecycle/RenamedData.h>


namespace sofa::core::behavior
{

template< class T > class MechanicalState;

} // namespace sofa::core::behavior

namespace sofa::component::solidmechanics::spring
{

/**
* @brief This class describes a simple elastic springs ForceField between DOFs positions and rest positions.
*
* Springs are applied to given degrees of freedom between their current positions and their rest shape positions.
* An external MechanicalState reference can also be passed to the ForceField as rest shape position.
*/
template<class DataTypes>
class FixedWeakConstraint : public core::behavior::ForceField<DataTypes>
{
public:
SOFA_CLASS(SOFA_TEMPLATE(FixedWeakConstraint, DataTypes), SOFA_TEMPLATE(core::behavior::ForceField, DataTypes));

typedef core::behavior::ForceField<DataTypes> Inherit;
typedef typename DataTypes::VecCoord VecCoord;
typedef typename DataTypes::VecDeriv VecDeriv;
typedef typename DataTypes::Coord Coord;
typedef typename DataTypes::CPos CPos;
typedef typename DataTypes::Deriv Deriv;
typedef typename DataTypes::Real Real;
typedef type::vector< sofa::Index > VecIndex;
typedef sofa::core::topology::TopologySubsetIndices DataSubsetIndex;
typedef type::vector< Real > VecReal;

static constexpr sofa::Size spatial_dimensions = Coord::spatial_dimensions;
static constexpr sofa::Size coord_total_size = Coord::total_size;

typedef core::objectmodel::Data<VecCoord> DataVecCoord;
typedef core::objectmodel::Data<VecDeriv> DataVecDeriv;

DataSubsetIndex d_indices; ///< points controlled by the rest shape springs
core::objectmodel::lifecycle::RemovedData d_points{this,"v24.12","v25.06","points","This data has been replaced by \'indices\'. Please update your scene."};

Data<bool> d_fixAll; ///< points controlled by the rest shape springs
Data< VecReal > d_stiffness; ///< stiffness values between the actual position and the rest shape position
Data< VecReal > d_angularStiffness; ///< angularStiffness assigned when controlling the rotation of the points
Data< bool > d_drawSpring; ///< draw Spring
Data< sofa::type::RGBAColor > d_springColor; ///< spring color. (default=[0.0,1.0,0.0,1.0])

/// Link to be set to the topology container in the component graph.
SingleLink<FixedWeakConstraint<DataTypes>, sofa::core::topology::BaseMeshTopology, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_topology;


/// BaseObject initialization method.
void bwdInit() override ;
void reinit() override ;

/// Add the forces.
void addForce(const core::MechanicalParams* mparams, DataVecDeriv& f, const DataVecCoord& x, const DataVecDeriv& v) override;

void addDForce(const core::MechanicalParams* mparams, DataVecDeriv& df, const DataVecDeriv& dx) override;
SReal getPotentialEnergy(const core::MechanicalParams* mparams, const DataVecCoord& x) const override;

/// Brings ForceField contribution to the global system stiffness matrix.
void addKToMatrix(const core::MechanicalParams* mparams, const sofa::core::behavior::MultiMatrixAccessor* matrix ) override;
void buildStiffnessMatrix(core::behavior::StiffnessMatrix* matrix) override;
void buildDampingMatrix(core::behavior::DampingMatrix* matrix) override;

void draw(const core::visual::VisualParams* vparams) override;


protected :
FixedWeakConstraint();
static constexpr type::fixed_array<bool, FixedWeakConstraint<DataTypes>::coord_total_size> s_defaultActiveDirections = sofa::type::makeHomogeneousArray<bool, FixedWeakConstraint<DataTypes>::coord_total_size>(true);

virtual const DataVecCoord* getExtPosition() const;
virtual const VecIndex& getIndices() const;
virtual const VecIndex& getExtIndices() const;
virtual const type::fixed_array<bool, FixedWeakConstraint<DataTypes>::coord_total_size>& getActiveDirections() const;
virtual const bool checkState();

virtual bool checkOutOfBoundsIndices();

bool checkOutOfBoundsIndices(const VecIndex &indices, const sofa::Size dimension);
};

#if !defined(SOFA_COMPONENT_FORCEFIELD_FixedWeakConstraint_CPP)
extern template class SOFA_COMPONENT_SOLIDMECHANICS_SPRING_API FixedWeakConstraint<sofa::defaulttype::Vec3Types>;
extern template class SOFA_COMPONENT_SOLIDMECHANICS_SPRING_API FixedWeakConstraint<sofa::defaulttype::Vec1Types>;
extern template class SOFA_COMPONENT_SOLIDMECHANICS_SPRING_API FixedWeakConstraint<sofa::defaulttype::Rigid3Types>;
#endif

} // namespace sofa::component::solidmechanics::spring
Loading
Loading