Skip to content
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
29 changes: 13 additions & 16 deletions src/BeamAdapter/component/BaseBeamInterpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
#include <sofa/core/behavior/ForceField.h>
#include <sofa/core/behavior/Mass.h>
#include <sofa/core/objectmodel/Data.h>
#include <sofa/defaulttype/SolidTypes.h>
#include <sofa/core/topology/BaseMeshTopology.h>
#include <sofa/core/behavior/SingleStateAccessor.h>

#include <sofa/type/vector.h>
#include <sofa/type/Vec.h>
#include <sofa/type/Mat.h>
#include <sofa/type/Transform.h>
#include <sofa/type/SpatialVector.h>


#include <sofa/core/objectmodel/BaseObject.h>
#include <sofa/component/statecontainer/MechanicalObject.h>


Expand All @@ -55,13 +57,13 @@ using sofa::component::statecontainer::MechanicalObject;
*
*/
template<class DataTypes>
class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
class BaseBeamInterpolation : public sofa::core::behavior::SingleStateAccessor<DataTypes>
{
public:
SOFA_CLASS(SOFA_TEMPLATE(BaseBeamInterpolation, DataTypes) ,
sofa::core::objectmodel::BaseObject);
SOFA_TEMPLATE(sofa::core::behavior::SingleStateAccessor, DataTypes));

using Inherit = sofa::core::objectmodel::BaseObject;
using Inherit = sofa::core::behavior::SingleStateAccessor<DataTypes>;

using Coord = typename DataTypes::Coord;
using VecCoord = typename DataTypes::VecCoord;
Expand All @@ -70,8 +72,8 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
using Deriv = typename DataTypes::Deriv;
using VecDeriv = typename DataTypes::VecDeriv;

using Transform = typename sofa::defaulttype::SolidTypes<Real>::Transform;
using SpatialVector = typename sofa::defaulttype::SolidTypes<Real>::SpatialVector;
using Transform = sofa::type::Transform<Real>;
using SpatialVector = sofa::type::SpatialVector<Real>;

using Vec2 = sofa::type::Vec<2, Real>;
using Vec3 = sofa::type::Vec<3, Real>;
Expand All @@ -84,7 +86,7 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
using VecEdgeID = type::vector<BaseMeshTopology::EdgeID>;
using VecEdges = type::vector<BaseMeshTopology::Edge>;

BaseBeamInterpolation(/*sofa::component::engine::WireRestShape<DataTypes> *_restShape = nullptr*/);
BaseBeamInterpolation();

virtual ~BaseBeamInterpolation() = default;

Expand Down Expand Up @@ -212,7 +214,6 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
typename MechanicalObject<StateDataTypes>::SPtr m_StateNodes;

Data< VecEdgeID > d_edgeList;
const VecEdges* m_topologyEdges{ nullptr };

///2. Vector of length of each beam. Same size as @sa d_edgeList
Data< type::vector< Real > > d_lengthList;
Expand All @@ -230,13 +231,9 @@ class BaseBeamInterpolation : public virtual sofa::core::objectmodel::BaseObject
Data< sofa::type::vector<EdgeID> > d_beamCollision;

Data<bool> d_dofsAndBeamsAligned;


/// pointer to the topology
BaseMeshTopology* m_topology{ nullptr };

/// pointer on mechanical state
MechanicalState<DataTypes>* m_mstate{ nullptr };

/// link to the (edge) topology
SingleLink<BaseBeamInterpolation<DataTypes>, BaseMeshTopology, BaseLink::FLAG_STOREPATH|BaseLink::FLAG_STRONGLINK> l_topology;
};


Expand Down
77 changes: 37 additions & 40 deletions src/BeamAdapter/component/BaseBeamInterpolation.inl
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ void BaseBeamInterpolation<DataTypes>::RotateFrameForAlignNormalizedX(const Quat


template <class DataTypes>
BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation(/*sofa::component::engine::WireRestShape<DataTypes> *_restShape*/)
: m_StateNodes(sofa::core::objectmodel::New< sofa::component::statecontainer::MechanicalObject<StateDataTypes> >())
BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation()
: Inherit()
, m_StateNodes(sofa::core::objectmodel::New< sofa::component::statecontainer::MechanicalObject<StateDataTypes> >())
, d_edgeList(initData(&d_edgeList, "edgeList", "list of the edge in the topology that are concerned by the Interpolation"))
, d_lengthList(initData(&d_lengthList, "lengthList", "list of the length of each beam"))
, d_DOF0TransformNode0(initData(&d_DOF0TransformNode0, "DOF0TransformNode0", "Optional rigid transformation between the degree of Freedom and the first node of the beam"))
Expand All @@ -106,49 +107,43 @@ BaseBeamInterpolation<DataTypes>::BaseBeamInterpolation(/*sofa::component::engin
, d_beamCollision(initData(&d_beamCollision, "beamCollision", "list of beam (in edgeList) that needs to be considered for collision"))
, d_dofsAndBeamsAligned(initData(&d_dofsAndBeamsAligned, true, "dofsAndBeamsAligned",
"if false, a transformation for each beam is computed between the DOF and the beam nodes"))
, m_topology(nullptr)
, m_mstate(nullptr)
, l_topology(initLink("topology", "link to the topology (must contain edges)"))
{


m_StateNodes->setName("bezierNodes");
addSlave(m_StateNodes);
this->addSlave(m_StateNodes);
}


template<class DataTypes>
void BaseBeamInterpolation<DataTypes>::init()
{
Inherit::init();

BaseContext* context = getContext();

m_mstate = dynamic_cast<sofa::core::behavior::MechanicalState<DataTypes> *> (context->getMechanicalState());
if (m_mstate == nullptr)

if (!l_topology)
{
msg_error() << "No MechanicalState found. Component is de-activated.";
d_componentState.setValue(ComponentState::Invalid);
return;
l_topology.set(this->getMState()->getContext()->getMeshTopologyLink());
}

/// Get the topology from context and check if it is valid.
m_topology = context->getMeshTopology();
if (!m_topology)
if (l_topology)
{
msg_info() << "Found topology named "<< l_topology->getName() ;
}
else
{
msg_error() << "No Topology found. Component is de-activated.";
d_componentState.setValue(ComponentState::Invalid);
msg_error() << "Cannot find a topology container. Please specify the link to the topology or insert one in the same node.";
this->d_componentState.setValue(ComponentState::Invalid);
return;
}

/// Check the topology properties
if (m_topology->getNbEdges() == 0)

if(l_topology->getNbEdges() == 0)
{
msg_error() << "Found a topology but it is empty. Component is de-activated";
d_componentState.setValue(ComponentState::Invalid);
msg_error() << "Found a topology but it is empty (no edges). Component is de-activated";
this->d_componentState.setValue(ComponentState::Invalid);
return;
}

m_topologyEdges = &m_topology->getEdges();
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid);
}


Expand Down Expand Up @@ -266,7 +261,7 @@ void BaseBeamInterpolation<DataTypes>::getBeamAtCurvAbs(const Real x_input, unsi
/// LTotal = length sum of the beams that are "out"
Real LTotal = 0.0;

const unsigned int edgeListSize = this->d_edgeList.getValue().size();
const sofa::Size edgeListSize = static_cast<sofa::Size>(this->d_edgeList.getValue().size());

/// we find the length of the beam that is "out"
for (unsigned int e = start; e < edgeListSize; e++)
Expand Down Expand Up @@ -360,7 +355,7 @@ void BaseBeamInterpolation<DataTypes>::getDOFtoLocalTransformInGlobalFrame(const
template <class DataTypes>
void BaseBeamInterpolation<DataTypes>::setTransformBetweenDofAndNode(const sofa::Index beam, const Transform& DOF_H_Node, unsigned int zeroORone)
{
if (beam > int(d_DOF0TransformNode0.getValue().size() - 1) || beam > int(d_DOF1TransformNode1.getValue().size() - 1))
if (beam > (d_DOF0TransformNode0.getValue().size() - 1) || beam > (d_DOF1TransformNode1.getValue().size() - 1))
{
msg_error() << "WARNING setTransformBetweenDofAndNode on non existing beam";
return;
Expand Down Expand Up @@ -402,15 +397,15 @@ int BaseBeamInterpolation<DataTypes>::getNodeIndices(const EdgeID edgeInList,
unsigned int& node0Idx,
unsigned int& node1Idx)
{
if (m_topologyEdges == nullptr)
if(!this->isComponentStateValid())
{
msg_error() << "This object does not have edge topology defined (computation halted). ";
msg_error() << "(getNodeIndices) This component is invalid, check the other error messages. ";
return -1;
}

/// 1. Get the indices of element and nodes
const EdgeID& e = d_edgeList.getValue()[edgeInList];
const BaseMeshTopology::Edge& edge = (*m_topologyEdges)[e];
const BaseMeshTopology::Edge& edge = l_topology->getEdge(e);
node0Idx = edge[0];
node1Idx = edge[1];

Expand All @@ -437,14 +432,14 @@ void BaseBeamInterpolation<DataTypes>::getSplinePoints(const EdgeID edgeInList,
template<class DataTypes>
unsigned int BaseBeamInterpolation<DataTypes>::getStateSize() const
{
if (m_mstate == nullptr)
if(!this->isComponentStateValid())
{
msg_error() << "No _mstate found (Aborting)";
msg_error() << "(getStateSize) This component is invalid, check the other error messages. ";
return 0;
}
else
{
return m_mstate->getSize();
return this->getMState()->getSize();
}
}

Expand Down Expand Up @@ -582,22 +577,24 @@ void BaseBeamInterpolation<DataTypes>::computeStrechAndTwist(const EdgeID edgeIn

///vId_Out provides the id of the multiVecId which stores the position of the Bezier Points
template<class DataTypes>
void BaseBeamInterpolation<DataTypes>::updateBezierPoints(const VecCoord& x, sofa::core::VecCoordId& vId_Out) {
void BaseBeamInterpolation<DataTypes>::updateBezierPoints(const VecCoord& x, sofa::core::VecCoordId& vId_Out)
{
const sofa::Size edgeListSize = static_cast<sofa::Size>(d_edgeList.getValue().size());
///Mechanical Object to stock Bezier points.
m_StateNodes->resize(d_edgeList.getValue().size() * 4);
m_StateNodes->resize(edgeListSize * 4);
auto bezierPosVec = sofa::helper::getWriteOnlyAccessor(*m_StateNodes->write(vId_Out));
bezierPosVec.resize(d_edgeList.getValue().size() * 4);
bezierPosVec.resize(edgeListSize * 4);

for (unsigned int i = 0; i < d_edgeList.getValue().size(); i++) {
for (unsigned int i = 0; i < edgeListSize; i++) {
updateBezierPoints(x, i, bezierPosVec.wref());

}
}

template<class DataTypes>
void BaseBeamInterpolation<DataTypes>::updateBezierPoints(const VecCoord& x, unsigned int index,
VectorVec3& bezierPosVec) {
/// <<" interpolatePointUsingSpline : "<< edgeInList<<" xbary="<<baryCoord<<" localPos"<<localPos<<std::endl;
VectorVec3& bezierPosVec)
{
const Real _L = d_lengthList.getValue()[index];

/// \todo remove call to
Expand Down
2 changes: 2 additions & 0 deletions src/BeamAdapter/component/BeamInterpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class BeamInterpolation : public BaseBeamInterpolation<DataTypes>
{
public:
SOFA_CLASS( SOFA_TEMPLATE(BeamInterpolation, DataTypes) , SOFA_TEMPLATE(BaseBeamInterpolation, DataTypes));

using Inherit = BaseBeamInterpolation<DataTypes>;

using Coord = typename DataTypes::Coord;
using VecCoord = typename DataTypes::VecCoord;
Expand Down
38 changes: 20 additions & 18 deletions src/BeamAdapter/component/BeamInterpolation.inl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ using sofa::helper::ReadAccessor ;

//////////////////////////////////// BREAMINTERPOLATION ////////////////////////////////////////////
template <class DataTypes>
BeamInterpolation<DataTypes>::BeamInterpolation() :
crossSectionShape(initData(&crossSectionShape,
BeamInterpolation<DataTypes>::BeamInterpolation()
: Inherit()
, crossSectionShape(initData(&crossSectionShape,
{"circular","elliptic (not available)","rectangular"},
"crossSectionShape",
"shape of the cross-section. Can be: circular, elliptic, square, rectangular. Default is circular" ))
Expand Down Expand Up @@ -139,7 +140,7 @@ void BeamInterpolation<DataTypes>::computeCrossSectionInertiaMatrix()
}
else
{
Size nbEdges = this->m_topology->getNbEdges();
Size nbEdges = this->l_topology->getNbEdges();
m_section.resize(nbEdges);
if ( crossSectionShape.getValue().getSelectedItem() == "elliptic")
{
Expand Down Expand Up @@ -192,12 +193,12 @@ template <class DataTypes>
void BeamInterpolation<DataTypes>::init()
{
this->d_componentState.setValue(ComponentState::Loading);
BaseBeamInterpolation<DataTypes>::init();
Inherit::init();

if (this->d_componentState.getValue() == ComponentState::Invalid)
return;

Size nbEdges = this->m_topology->getNbEdges();
Size nbEdges = this->l_topology->getNbEdges();
checkDataSize(m_defaultRadius, d_radius, nbEdges);
checkDataSize(m_defaultInnerRadius, d_innerRadius, nbEdges);
checkDataSize(m_defaultLengthY, d_lengthY, nbEdges);
Expand All @@ -211,7 +212,7 @@ void BeamInterpolation<DataTypes>::init()
auto edgeList = sofa::helper::getWriteOnlyAccessor(this->d_edgeList);
edgeList.clear();

for (unsigned int i=0; i<this->m_topology->getNbEdges(); i++)
for (unsigned int i=0; i<this->l_topology->getNbEdges(); i++)
{
edgeList.push_back(i);
}
Expand All @@ -225,7 +226,7 @@ void BeamInterpolation<DataTypes>::init()
DOF1TransformNode1.resize(edgeList.size());
}

ReadAccessor<Data<VecCoord> > statePos = this->m_mstate->read(sofa::core::vec_id::read_access::position) ;
ReadAccessor<Data<VecCoord> > statePos = this->getMState()->read(sofa::core::vec_id::read_access::position) ;

auto lengthList = sofa::helper::getWriteOnlyAccessor(this->d_lengthList);
lengthList.clear();
Expand Down Expand Up @@ -335,17 +336,18 @@ bool BeamInterpolation<DataTypes>::interpolationIsAlreadyInitialized()
template<class DataTypes>
bool BeamInterpolation<DataTypes>::verifyTopology()
{
const auto nbEdges = this->l_topology->getNbEdges();

//TODO(dmarchal) This contains "code" specific slang that cannot be understood by user.
dmsg_info() << "The vector _topologyEdges is now set with " << this->m_topologyEdges->size() << " edges" ;

dmsg_info() << "The vector _topologyEdges is now set with " << nbEdges << " edges" ;

const VecElementID &edgeList = this->d_edgeList.getValue();
for (unsigned int j = 0; j < edgeList.size(); j++)
{
if(edgeList[j] > this->m_topologyEdges->size())
if(edgeList[j] > nbEdges)
{
msg_error() << "The provided edge index '" << edgeList[j] << "'is larger than '"
<< this->m_topologyEdges->size() << "' the amount of edges in the topology. " ;
<< nbEdges << "' the amount of edges in the topology. " ;
return false;
}
}
Expand Down Expand Up @@ -420,7 +422,7 @@ void BeamInterpolation<DataTypes>::getMechanicalSampling(Real& dx, const Real x_
{
SOFA_UNUSED(x_localcurv_abs);

const auto numLines = this->m_topologyEdges->size();
const auto numLines = this->l_topology->getNbEdges();
dx = getRestTotalLength()/numLines;
}

Expand All @@ -429,14 +431,14 @@ void BeamInterpolation<DataTypes>::getCollisionSampling(Real &dx, const Real x_l
{
SOFA_UNUSED(x_localcurv_abs);

const auto numLines = this->m_topologyEdges->size();
const auto numLines = this->l_topology->getNbEdges();
dx = getRestTotalLength()/numLines;
}

template <class DataTypes>
void BeamInterpolation<DataTypes>::getNumberOfCollisionSegment(Real &dx, unsigned int &numLines)
{
numLines = static_cast<unsigned int>(this->m_topologyEdges->size());
numLines = static_cast<unsigned int>(this->l_topology->getNbEdges());
dx = getRestTotalLength()/numLines;
}

Expand Down Expand Up @@ -599,16 +601,16 @@ void BeamInterpolation<DataTypes>::updateInterpolation(){
if(d_vecID.getValue().getSelectedItem() == "current")
{
dmsg_info() <<" position " << msgendl
<< " ="<< this->m_mstate->read( sofa::core::vec_id::read_access::position )->getValue( ) ;
x=this->m_mstate->read( sofa::core::vec_id::read_access::position );
<< " ="<< this->getMState()->read( sofa::core::vec_id::read_access::position )->getValue( ) ;
x=this->getMState()->read( sofa::core::vec_id::read_access::position );
}
else if(d_vecID.getValue().getSelectedItem() == "free")
{
x=this->m_mstate->read( sofa::core::vec_id::read_access::freePosition ) ;
x=this->getMState()->read( sofa::core::vec_id::read_access::freePosition ) ;
}
else /// rest position
{
x=this->m_mstate->read( sofa::core::vec_id::read_access::restPosition ) ;
x=this->getMState()->read( sofa::core::vec_id::read_access::restPosition ) ;
computeVel = false;
}

Expand Down
Loading
Loading