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

[constraint] PositionModel: add features #282

Merged
merged 1 commit into from
May 2, 2024
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
2 changes: 1 addition & 1 deletion src/SoftRobots/component/constraint/PositionConstraint.inl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void PositionConstraint<DataTypes>::getConstraintViolation(const ConstraintParam
for(sofa::Size j=0; j<DataTypes::Deriv::total_size; j++)
if(useDirections[j])
{
Real dfree = Jdx->element(index) + d*directions[j]*weight;
Real dfree = Jdx->element(index) + d*directions[j]*weight[j];
resV->set(m_constraintId+index, dfree);
index++;
}
Expand Down
8 changes: 7 additions & 1 deletion src/SoftRobots/component/constraint/model/PositionModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ void PositionModel<Rigid3Types>::normalizeDirections()
d_directions.setValue(directions);
}


template<>
void PositionModel<Vec1Types>::drawPoints(const VisualParams* vparams, const std::vector<Coord> &points, float size, const RGBAColor& color) {
}

template<>
void PositionModel<Vec3Types>::drawPoints(const VisualParams* vparams, const std::vector<Coord> &points, float size, const RGBAColor& color) {
vparams->drawTool()->drawPoints(points, size, color);
Expand All @@ -75,8 +80,9 @@ void PositionModel<Rigid3Types>::drawPoints(const VisualParams* vparams, const s
}

using namespace sofa::defaulttype;
template class SOFA_SOFTROBOTS_API PositionModel<Vec1Types>;
template class SOFA_SOFTROBOTS_API PositionModel<Vec2Types>;
template class SOFA_SOFTROBOTS_API PositionModel<Vec3Types>;
template class SOFA_SOFTROBOTS_API PositionModel<Rigid3Types>;
template class SOFA_SOFTROBOTS_API PositionModel<Vec2Types>;

} // namespace
5 changes: 3 additions & 2 deletions src/SoftRobots/component/constraint/model/PositionModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class PositionModel : virtual public SoftRobotsConstraint<DataTypes>

protected:
sofa::Data<sofa::type::vector<unsigned int> > d_indices;
sofa::Data<Real> d_weight;
sofa::Data<sofa::type::vector<Real>> d_weight;
sofa::Data<VecDeriv> d_directions;
sofa::Data<Vec<Deriv::total_size, bool>> d_useDirections;
sofa::Data<sofa::type::vector<Real>> d_delta;
Expand Down Expand Up @@ -125,8 +125,9 @@ template<> SOFA_SOFTROBOTS_API
void PositionModel<sofa::defaulttype::Rigid3Types>::drawPoints(const VisualParams* vparams, const std::vector<Coord> &points, float size, const sofa::type::RGBAColor& color);

#if !defined(SOFTROBOTS_POSITIONMODEL_CPP)
extern template class SOFA_SOFTROBOTS_API PositionModel<sofa::defaulttype::Vec3Types>;
extern template class SOFA_SOFTROBOTS_API PositionModel<sofa::defaulttype::Vec1Types>;
extern template class SOFA_SOFTROBOTS_API PositionModel<sofa::defaulttype::Vec2Types>;
extern template class SOFA_SOFTROBOTS_API PositionModel<sofa::defaulttype::Vec3Types>;
extern template class SOFA_SOFTROBOTS_API PositionModel<sofa::defaulttype::Rigid3Types>;
#endif

Expand Down
19 changes: 16 additions & 3 deletions src/SoftRobots/component/constraint/model/PositionModel.inl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ PositionModel<DataTypes>::PositionModel(MechanicalState* object)
"If indices size is lower than target size, \n"
"some target will not be considered"))

, d_weight(initData(&d_weight, 1., "weight",
, d_weight(initData(&d_weight, sofa::type::vector<Real>(Deriv::total_size, 1.), "weight",
"The parameter sets a weight to the minimization."))

, d_directions(initData(&d_directions,"directions",
Expand All @@ -67,8 +67,21 @@ PositionModel<DataTypes>::PositionModel(MechanicalState* object)
, d_delta(initData(&d_delta, "delta","Distance to target"))
{
d_delta.setReadOnly(true);
}

this->addUpdateCallback("updateWeight", {&d_weight}, [this](const sofa::core::DataTracker& t)
{
SOFA_UNUSED(t);
auto weight = sofa::helper::getWriteAccessor(d_weight);
if (weight.size() != Deriv::total_size)
{
msg_info() << "Wrong size for the data field weight, " << weight.size() <<
" instead of " << Deriv::total_size << ". Resizing, with weight[0] as the default value.";
Real w = weight.empty()? 1.: weight[0];
d_weight.setValue(sofa::type::vector<Real>(Deriv::total_size, w));
}
return sofa::core::objectmodel::ComponentState::Valid;
}, {});
}


template<class DataTypes>
Expand Down Expand Up @@ -220,7 +233,7 @@ void PositionModel<DataTypes>::buildConstraintMatrix(const ConstraintParams* cPa
if(useDirections[j])
{
MatrixDerivRowIterator rowIterator = column.writeLine(m_constraintId+index);
rowIterator.setCol(indices[i], directions[j]*weight);
rowIterator.setCol(indices[i], directions[j]*weight[j]);
index++;
}
}
Expand Down
Loading