diff --git a/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.cpp b/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.cpp index f52609b7bbd..86f1a9bca7d 100644 --- a/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.cpp +++ b/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.cpp @@ -41,15 +41,6 @@ int FixedProjectiveConstraintClass = core::RegisterObject("Attach given particle .add< FixedProjectiveConstraint >() ; - -// methods specilizations declaration -template <> SOFA_COMPONENT_CONSTRAINT_PROJECTIVE_API -void FixedProjectiveConstraint::draw(const core::visual::VisualParams* vparams); -template <> SOFA_COMPONENT_CONSTRAINT_PROJECTIVE_API -void FixedProjectiveConstraint::draw(const core::visual::VisualParams* vparams); - - - template class SOFA_COMPONENT_CONSTRAINT_PROJECTIVE_API FixedProjectiveConstraint; template class SOFA_COMPONENT_CONSTRAINT_PROJECTIVE_API FixedProjectiveConstraint; template class SOFA_COMPONENT_CONSTRAINT_PROJECTIVE_API FixedProjectiveConstraint; @@ -57,85 +48,4 @@ template class SOFA_COMPONENT_CONSTRAINT_PROJECTIVE_API FixedProjectiveConstrain template class SOFA_COMPONENT_CONSTRAINT_PROJECTIVE_API FixedProjectiveConstraint; template class SOFA_COMPONENT_CONSTRAINT_PROJECTIVE_API FixedProjectiveConstraint; - - -// methods specilizations definition -template <> -void FixedProjectiveConstraint::draw(const core::visual::VisualParams* vparams) -{ - if (this->d_componentState.getValue() != ComponentState::Valid) return; - if (!d_showObject.getValue()) return; - if (!this->isActive()) return; - if (!vparams->displayFlags().getShowBehaviorModels()) return; - - const auto stateLifeCycle = vparams->drawTool()->makeStateLifeCycle(); - - const SetIndexArray & indices = d_indices.getValue(); - const VecCoord& x = mstate->read(core::ConstVecCoordId::position())->getValue(); - - std::vector< type::Vec3 > points; - - if (d_fixAll.getValue()) - { - for (const auto& xi : x) - points.push_back(xi.getCenter()); - } - else - { - if( x.size() < indices.size() ) - { - for (unsigned i=0; idrawTool()->drawPoints(points, 10, sofa::type::RGBAColor(1,0.5,0.5,1)); - else - vparams->drawTool()->drawSpheres(points, (float)d_drawSize.getValue(), sofa::type::RGBAColor(1.0f,0.35f,0.35f,1.0f)); - - -} - -template <> -void FixedProjectiveConstraint::draw(const core::visual::VisualParams* vparams) -{ - if (this->d_componentState.getValue() != ComponentState::Valid) return; - if (!d_showObject.getValue()) return; - if (!this->isActive()) return; - if (!vparams->displayFlags().getShowBehaviorModels()) return; - - const auto stateLifeCycle = vparams->drawTool()->makeStateLifeCycle(); - - const SetIndexArray& indices = d_indices.getValue(); - const VecCoord& x =mstate->read(core::ConstVecCoordId::position())->getValue(); - - vparams->drawTool()->setLightingEnabled(false); - constexpr sofa::type::RGBAColor color (1,0.5,0.5,1); - std::vector vertices; - - if(d_fixAll.getValue()) - { - for (const auto& xi : x) - vertices.emplace_back(xi.getCenter()[0], xi.getCenter()[1], 0.0); - } - else - { - for (const unsigned int index : indices) - vertices.emplace_back(x[index].getCenter()[0], x[index].getCenter()[1], 0.0); - } - - vparams->drawTool()->drawPoints(vertices, 10, color); - -} - } // namespace sofa::component::constraint::projective diff --git a/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.h b/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.h index e766fd9d3fb..56eba8ada4e 100644 --- a/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.h +++ b/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.h @@ -115,6 +115,7 @@ class FixedProjectiveConstraint : public core::behavior::ProjectiveConstraintSet void applyConstraint(sofa::core::behavior::ZeroDirichletCondition* matrix) override; + void computeBBox(const core::ExecParams* params, bool onlyVisible) override; void draw(const core::visual::VisualParams* vparams) override; bool fixAllDOFs() const { return d_fixAll.getValue(); } @@ -123,6 +124,8 @@ protected : /// Function check values of given indices void checkIndices(); + void computeBBoxForIndices(const type::vector& indices); + }; #if !defined(SOFA_COMPONENT_PROJECTIVECONSTRAINTSET_FIXEDPROJECTIVECONSTRAINT_CPP) diff --git a/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.inl b/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.inl index 4d50a0e6758..b31fe7335bf 100644 --- a/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.inl +++ b/Sofa/Component/Constraint/Projective/src/sofa/component/constraint/projective/FixedProjectiveConstraint.inl @@ -361,6 +361,62 @@ void FixedProjectiveConstraint::applyConstraint(sofa::core::behavior: } } +template +void FixedProjectiveConstraint::computeBBoxForIndices(const type::vector& indices) +{ + using Real = typename DataTypes::Real; + + constexpr Real max_real = std::numeric_limits::max(); + constexpr Real min_real = std::numeric_limits::lowest(); + Real maxBBox[3] = {min_real,min_real,min_real}; + Real minBBox[3] = {max_real,max_real,max_real}; + + const auto drawSize = static_cast(d_drawSize.getValue()); + + const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue(); + + for (const auto index : indices) + { + const auto x3d = DataTypes::getCPos(x[index]); + + for (unsigned int i = 0; i < x3d.size(); ++i) + { + maxBBox[i] = std::max(x3d[i] + drawSize, maxBBox[i]); + minBBox[i] = std::min(x3d[i] - drawSize, minBBox[i]); + } + } + this->f_bbox.setValue(sofa::type::TBoundingBox(minBBox,maxBBox)); +} + +template +void FixedProjectiveConstraint::computeBBox( + const core::ExecParams* params, bool onlyVisible) +{ + SOFA_UNUSED(params); + + if( onlyVisible && !d_showObject.getValue() ) + { + return; + } + + if(this->d_componentState.getValue() == ComponentState::Invalid) + { + return; + } + + const auto& indices = d_indices.getValue(); + + if (d_fixAll.getValue()) + { + const auto bbox = this->mstate->computeBBox(); //this may compute twice the mstate bbox, but there is no way to determine if the bbox has already been computed + this->f_bbox.setValue(std::move(bbox)); + } + else if (!indices.empty()) + { + computeBBoxForIndices(indices); + } +} + template void FixedProjectiveConstraint::draw(const core::visual::VisualParams* vparams) { @@ -389,9 +445,9 @@ void FixedProjectiveConstraint::draw(const core::visual::VisualParams } else { - for (SetIndexArray::const_iterator it = indices.begin(); it != indices.end(); ++it) + for (const auto index : indices) { - point = DataTypes::getCPos(x[*it]); + point = DataTypes::getCPos(x[index]); points.push_back(point); } } @@ -404,16 +460,18 @@ void FixedProjectiveConstraint::draw(const core::visual::VisualParams std::vector< sofa::type::Vec3 > points; sofa::type::Vec3 point; if( d_fixAll.getValue()==true ) + { for (unsigned i=0; i