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

[GUI.Component] Add virtualization for attachment performer #4638

12 changes: 6 additions & 6 deletions Sofa/GUI/Common/src/sofa/gui/common/MouseOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ class SOFA_GUI_COMMON_API AttachOperation : public Operation
{}
~AttachOperation() override {}

void setStiffness(double s) {setting->stiffness.setValue(s);}
double getStiffness() const { return setting->stiffness.getValue();}
void setArrowSize(double s) {setting->arrowSize.setValue(s);}
double getArrowSize() const { return setting->arrowSize.getValue();}
void setShowFactorSize(double s) { setting->showFactorSize.setValue(s); }
double getShowFactorSize() const { return setting->showFactorSize.getValue(); }
void setStiffness(double s) {setting->d_stiffness.setValue(s);}
double getStiffness() const { return setting->d_stiffness.getValue();}
void setArrowSize(double s) {setting->d_arrowSize.setValue(s);}
double getArrowSize() const { return setting->d_arrowSize.getValue();}
void setShowFactorSize(double s) { setting->d_showFactorSize.setValue(s); }
double getShowFactorSize() const { return setting->d_showFactorSize.getValue(); }

static std::string getDescription() {return "Attach an object to the Mouse using a spring force field";}

Expand Down
2 changes: 2 additions & 0 deletions Sofa/GUI/Component/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ set(HEADER_FILES
${SOFAGUICOMPONENT_SOURCE_DIR}/performer/AddRecordedCameraPerformer.h
${SOFAGUICOMPONENT_SOURCE_DIR}/performer/AttachBodyPerformer.h
${SOFAGUICOMPONENT_SOURCE_DIR}/performer/AttachBodyPerformer.inl
${SOFAGUICOMPONENT_SOURCE_DIR}/performer/BaseAttachBodyPerformer.h
${SOFAGUICOMPONENT_SOURCE_DIR}/performer/BaseAttachBodyPerformer.inl
${SOFAGUICOMPONENT_SOURCE_DIR}/performer/ComponentMouseInteraction.h
${SOFAGUICOMPONENT_SOURCE_DIR}/performer/ComponentMouseInteraction.inl
${SOFAGUICOMPONENT_SOURCE_DIR}/performer/ConstraintAttachBodyPerformer.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ int AttachBodyButtonSettingClass = core::RegisterObject("Attach Body Button conf
;

AttachBodyButtonSetting::AttachBodyButtonSetting():
stiffness(initData(&stiffness, 1000.0_sreal, "stiffness", "Stiffness of the spring to attach a particule"))
, arrowSize(initData(&arrowSize, 0.0_sreal, "arrowSize", "Size of the drawn spring: if >0 an arrow will be drawn"))
, showFactorSize(initData(&showFactorSize, 1.0_sreal, "showFactorSize", "Show factor size of the JointSpringForcefield when interacting with rigids"))
d_stiffness(initData(&d_stiffness, 1000.0_sreal, "stiffness", "Stiffness of the spring to attach a particule"))
, d_arrowSize(initData(&d_arrowSize, 0.0_sreal, "arrowSize", "Size of the drawn spring: if >0 an arrow will be drawn"))
, d_showFactorSize(initData(&d_showFactorSize, 1.0_sreal, "showFactorSize", "Show factor size of the JointSpringForcefield when interacting with rigids"))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ class SOFA_GUI_COMPONENT_API AttachBodyButtonSetting: public sofa::component::se
AttachBodyButtonSetting();
public:
std::string getOperationType() override {return "Attach";}
Data<SReal> stiffness; ///< Stiffness of the spring to attach a particule
Data<SReal> arrowSize; ///< Size of the drawn spring: if >0 an arrow will be drawn
Data<SReal> showFactorSize; ///< Show factor size of the JointSpringForcefield when interacting with rigids
Data<SReal> d_stiffness; ///< Stiffness of the spring to attach a particule
Data<SReal> d_arrowSize; ///< Size of the drawn spring: if >0 an arrow will be drawn
Data<SReal> d_showFactorSize; ///< Show factor size of the JointSpringForcefield when interacting with rigids

SOFA_ATTRIBUTE_DISABLED__NAMING("v24.06", "v24.06", stiffness, d_stiffness);
SOFA_ATTRIBUTE_DISABLED__NAMING("v24.06", "v24.06", arrowSize, d_arrowSize);
SOFA_ATTRIBUTE_DISABLED__NAMING("v24.06", "v24.06", showFactorSize, d_showFactorSize);
};

} // namespace sofa::gui::component
7 changes: 7 additions & 0 deletions Sofa/GUI/Component/src/sofa/gui/component/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
# define SOFA_GUI_COMPONENT_API SOFA_IMPORT_DYNAMIC_LIBRARY
#endif

#define SOFA_ATTRIBUTE_DISABLED__NAMING( deprecationDate, disableDate, oldName, newName) \
SOFA_ATTRIBUTE_DISABLED( \
deprecationDate, disableDate, \
"The attribute " sofa_tostring(oldName) " has been renamed to \'" sofa_tostring(newName) "\' to fit naming policy ") \
DeprecatedAndRemoved oldName


namespace sofa::gui::component
{
constexpr const char* MODULE_NAME = "@PROJECT_NAME@";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace sofa::gui::component::performer

void AddRecordedCameraPerformer::start()
{
const sofa::simulation::Node::SPtr root = down_cast<sofa::simulation::Node>( interactor->getContext()->getRootContext() );
const sofa::simulation::Node::SPtr root = down_cast<sofa::simulation::Node>( m_interactor->getContext()->getRootContext() );
if(root)
{
sofa::component::visual::RecordedCamera* currentCamera = root->getNodeObject<sofa::component::visual::RecordedCamera>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
******************************************************************************/
#define SOFA_COMPONENT_COLLISION_ATTACHBODYPERFORMER_CPP

#include <sofa/gui/component/performer/BaseAttachBodyPerformer.inl>
#include <sofa/gui/component/performer/AttachBodyPerformer.inl>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/defaulttype/RigidTypes.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
#pragma once
#include <sofa/gui/component/config.h>

#include <sofa/gui/component/performer/InteractionPerformer.h>
#include <sofa/gui/component/performer/BaseAttachBodyPerformer.h>
#include <sofa/gui/component/AttachBodyButtonSetting.h>
#include <sofa/component/collision/response/mapper/BaseContactMapper.h>
#include <sofa/core/behavior/BaseForceField.h>
#include <sofa/core/visual/DisplayFlags.h>

Expand All @@ -34,52 +33,40 @@ namespace sofa::gui::component::performer
struct BodyPicked;

template <class DataTypes>
class AttachBodyPerformer: public TInteractionPerformer<DataTypes>
class AttachBodyPerformer: public BaseAttachBodyPerformer<DataTypes>
{
public:

typedef sofa::component::collision::response::mapper::BaseContactMapper< DataTypes > MouseContactMapper;
typedef sofa::core::behavior::MechanicalState< DataTypes > MouseContainer;
typedef sofa::core::behavior::BaseForceField MouseForceField;

AttachBodyPerformer(BaseMouseInteractor *i);
virtual ~AttachBodyPerformer();
virtual ~AttachBodyPerformer() = default;

void start();
void execute();
void draw(const core::visual::VisualParams* vparams);
void clear();
virtual bool startPartial(const BodyPicked& picked) override;
/*
initialise MouseForceField according to template.
StiffSpringForceField for Vec3
JointSpringForceField for Rigid3
*/

void setStiffness(SReal s) {stiffness=s;}
void setArrowSize(float s) {size=s;}
void setShowFactorSize(float s) {showFactorSize = s;}
void setStiffness(SReal s) {m_stiffness=s;}
void setArrowSize(float s) {m_size=s;}

virtual void configure(sofa::component::setting::MouseButtonSetting* setting)
{
const auto* s = dynamic_cast<sofa::gui::component::AttachBodyButtonSetting*>(setting);
if (s)
{
setStiffness(s->stiffness.getValue());
setArrowSize((float)s->arrowSize.getValue());
setShowFactorSize((float)s->showFactorSize.getValue());
setStiffness(s->d_stiffness.getValue());
setArrowSize((float)s->d_arrowSize.getValue());
}
}

protected:
SReal stiffness;
SReal size;
SReal showFactorSize;

virtual bool start_partial(const BodyPicked& picked);
/*
initialise MouseForceField according to template.
StiffSpringForceField for Vec3
JointSpringForceField for Rigid3
*/

MouseContactMapper *mapper;
MouseForceField::SPtr m_forcefield;

core::visual::DisplayFlags flags;
SReal m_stiffness;
SReal m_size;
};

#if !defined(SOFA_COMPONENT_COLLISION_ATTACHBODYPERFORMER_CPP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,126 +22,45 @@
#pragma once

#include <sofa/gui/component/performer/AttachBodyPerformer.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/gui/component/performer/MouseInteractor.h>
#include <sofa/component/solidmechanics/spring/StiffSpringForceField.h>
#include <sofa/core/BaseMapping.h>
#include <sofa/simulation/Node.h>

namespace sofa::gui::component::performer
{

template <class DataTypes>
void AttachBodyPerformer<DataTypes>::start()
{
if (m_forcefield)
{
clear();
return;
}
const BodyPicked picked=this->interactor->getBodyPicked();
if (!picked.body && !picked.mstate) return;

if (!start_partial(picked)) return; //template specialized code is here

double distanceFromMouse=picked.rayLength;
this->interactor->setDistanceFromMouse(distanceFromMouse);
sofa::component::collision::geometry::Ray ray = this->interactor->getMouseRayModel()->getRay(0);
ray.setOrigin(ray.origin() + ray.direction()*distanceFromMouse);
sofa::core::BaseMapping *mapping;
this->interactor->getContext()->get(mapping); assert(mapping);
mapping->apply(core::mechanicalparams::defaultInstance());
mapping->applyJ(core::mechanicalparams::defaultInstance());
m_forcefield->init();
this->interactor->setMouseAttached(true);
}



template <class DataTypes>
void AttachBodyPerformer<DataTypes>::execute()
{
sofa::core::BaseMapping *mapping;
this->interactor->getContext()->get(mapping); assert(mapping);
mapping->apply(core::mechanicalparams::defaultInstance());
mapping->applyJ(core::mechanicalparams::defaultInstance());
this->interactor->setMouseAttached(true);
}

template <class DataTypes>
void AttachBodyPerformer<DataTypes>::draw(const core::visual::VisualParams* vparams)
{
if (m_forcefield)
{
core::visual::VisualParams* vp = const_cast<core::visual::VisualParams*>(vparams);
const core::visual::DisplayFlags backup = vp->displayFlags();
vp->displayFlags() = flags;
m_forcefield->draw(vp);
vp->displayFlags() = backup;
}
}

template <class DataTypes>
AttachBodyPerformer<DataTypes>::AttachBodyPerformer(BaseMouseInteractor *i):
TInteractionPerformer<DataTypes>(i),
mapper(nullptr)
{
flags.setShowVisualModels(false);
flags.setShowInteractionForceFields(true);
}
BaseAttachBodyPerformer<DataTypes>(i)
{}

template <class DataTypes>
void AttachBodyPerformer<DataTypes>::clear()
{
if (m_forcefield)
{
m_forcefield->cleanup();
m_forcefield->getContext()->removeObject(m_forcefield);
m_forcefield.reset();
}

if (mapper)
{
mapper->cleanup();
delete mapper; mapper=nullptr;
}

this->interactor->setDistanceFromMouse(0);
this->interactor->setMouseAttached(false);
}


template <class DataTypes>
AttachBodyPerformer<DataTypes>::~AttachBodyPerformer()
{
clear();
}

template <class DataTypes>
bool AttachBodyPerformer<DataTypes>::start_partial(const BodyPicked& picked)
bool AttachBodyPerformer<DataTypes>::startPartial(const BodyPicked& picked)
{

core::behavior::MechanicalState<DataTypes>* mstateCollision=nullptr;
int index;
if (picked.body)
{
mapper = MouseContactMapper::Create(picked.body);
if (!mapper)
this->m_mapper = MouseContactMapper::Create(picked.body);
if (!this->m_mapper)
{
msg_warning(this->interactor) << "Problem with Mouse Mapper creation " ;
msg_warning(this->m_interactor) << "Problem with Mouse Mapper creation " ;
return false;
}
const std::string name = "contactMouse";
mstateCollision = mapper->createMapping(name.c_str());
mapper->resize(1);
mstateCollision = this->m_mapper->createMapping(name.c_str());
this->m_mapper->resize(1);

const unsigned int idx=picked.indexCollisionElement;
typename DataTypes::CPos pointPicked=(typename DataTypes::CPos)picked.point;
typename DataTypes::Real r=0.0;
typename DataTypes::Coord dofPicked;
DataTypes::setCPos(dofPicked, pointPicked);
index = mapper->addPointB(dofPicked, idx, r);
mapper->update();
index = this->m_mapper->addPointB(dofPicked, idx, r);
this->m_mapper->update();

if (mstateCollision->getContext() != picked.body->getContext())
{
Expand All @@ -164,21 +83,21 @@ bool AttachBodyPerformer<DataTypes>::start_partial(const BodyPicked& picked)
index = picked.indexCollisionElement;
if (!mstateCollision)
{
msg_warning(this->interactor) << "incompatible MState during Mouse Interaction " ;
msg_warning(this->m_interactor) << "incompatible MState during Mouse Interaction " ;
return false;
}
}

using sofa::component::solidmechanics::spring::StiffSpringForceField;

m_forcefield = sofa::core::objectmodel::New< StiffSpringForceField<DataTypes> >(dynamic_cast<MouseContainer*>(this->interactor->getMouseContainer()), mstateCollision);
StiffSpringForceField< DataTypes >* stiffspringforcefield = static_cast< StiffSpringForceField< DataTypes >* >(m_forcefield.get());
this->m_interactionObject = sofa::core::objectmodel::New< StiffSpringForceField<DataTypes> >(dynamic_cast<MouseContainer*>(this->m_interactor->getMouseContainer()), mstateCollision);
auto* stiffspringforcefield = dynamic_cast< StiffSpringForceField< DataTypes >* >(this->m_interactionObject.get());
stiffspringforcefield->setName("Spring-Mouse-Contact");
stiffspringforcefield->setArrowSize((float)this->size);
stiffspringforcefield->setArrowSize((float)this->m_size);
stiffspringforcefield->setDrawMode(2); //Arrow mode if size > 0


stiffspringforcefield->addSpring(0,index, stiffness, 0.0, picked.dist);
stiffspringforcefield->addSpring(0,index, m_stiffness, 0.0, picked.dist);
const core::objectmodel::TagSet &tags=mstateCollision->getTags();
for (core::objectmodel::TagSet::const_iterator it=tags.begin(); it!=tags.end(); ++it)
stiffspringforcefield->addTag(*it);
Expand Down
Loading
Loading