From 0cca5fe4c0eb02a4a586273c66efca9b618b1ab7 Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Wed, 4 Feb 2026 11:15:02 +0100 Subject: [PATCH] [Constraint] Improve initialization and state validation in StopperLagrangianConstraint --- .../model/StopperLagrangianConstraint.inl | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/StopperLagrangianConstraint.inl b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/StopperLagrangianConstraint.inl index ec48235ed5e..89041bfaa34 100644 --- a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/StopperLagrangianConstraint.inl +++ b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/StopperLagrangianConstraint.inl @@ -40,15 +40,19 @@ StopperLagrangianConstraint::StopperLagrangianConstraint(MechanicalSt template void StopperLagrangianConstraint::init() { - this->mstate = dynamic_cast(this->getContext()->getMechanicalState()); - assert(this->mstate); + core::behavior::SingleStateAccessor::init(); - helper::WriteAccessor > xData = *this->mstate->write(core::vec_id::write_access::position); - VecCoord& x = xData.wref(); - if (x[d_index.getValue()].x() < d_min.getValue()) - x[d_index.getValue()].x() = (Real) d_min.getValue(); - if (x[d_index.getValue()].x() > d_max.getValue()) - x[d_index.getValue()].x() = (Real) d_max.getValue(); + if (!this->isComponentStateInvalid()) + { + helper::WriteAccessor > xData = *this->mstate->write(core::vec_id::write_access::position); + VecCoord& x = xData.wref(); + if (x[d_index.getValue()].x() < d_min.getValue()) + x[d_index.getValue()].x() = (Real) d_min.getValue(); + if (x[d_index.getValue()].x() > d_max.getValue()) + x[d_index.getValue()].x() = (Real) d_max.getValue(); + + this->d_componentState.setValue(core::objectmodel::ComponentState::Valid); + } } template