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

[SofaGeneralEngine] Cleaning of MeshBoundaryROI #2319

Merged
merged 4 commits into from
Sep 15, 2021
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
3 changes: 2 additions & 1 deletion examples/Components/engine/MeshBoundaryROI.scn
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<VisualStyle displayFlags="showVisual showBehaviorModels" />

<MeshObjLoader name="mesh1" filename="mesh/c_open.obj" triangulate="0"/>
<RegularGridTopology name="mesh2" nx=5 ny=5 nz=1 xmin=-10 xmax=10 ymin=-10 ymax=10 zmin=-5 zmax=-5/>
<!-- computeTriangleList or computeQuadList must be set to false. Otherwise both are consider, and boundary detection cannot rely on a unique element -->
<RegularGridTopology name="mesh2" nx="5" ny="5" nz="1" xmin="-10" xmax="10" ymin="-10" ymax="10" zmin="-5" zmax="-5" computeTriangleList="false"/>
<MergeMeshes name="mesh" nbMeshes="2" position1="@mesh1.position" position2="@mesh2.position" triangles1="@mesh1.triangles" triangles2="@mesh2.triangles" quads1="@mesh1.quads" quads2="@mesh2.quads"/>

<OglModel name="visual" src="@mesh" color="0.5 0.5 1 1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,100 @@ namespace sofa::component::engine
int MeshBoundaryROIClass = core::RegisterObject("Outputs indices of boundary vertices of a triangle/quad mesh")
.add< MeshBoundaryROI >(true);

MeshBoundaryROI::MeshBoundaryROI(): Inherited()
, d_triangles(initData(&d_triangles,"triangles","input triangles"))
, d_quads(initData(&d_quads,"quads","input quads"))
, d_inputROI(initData(&d_inputROI,"inputROI","optional subset of the input mesh"))
, d_indices(initData(&d_indices,"indices","Index lists of the closing vertices"))
{
}

void MeshBoundaryROI::init()
{
addInput(&d_triangles);
addInput(&d_quads);
addInput(&d_inputROI);
addOutput(&d_indices);

setDirtyValue();
}

void MeshBoundaryROI::reinit()
{
update();
}

void MeshBoundaryROI::doUpdate()
{
const helper::ReadAccessor triangles(this->d_triangles);
const helper::ReadAccessor quads(this->d_quads);

helper::WriteOnlyAccessor<Data< SetIndex > > indices(this->d_indices);
indices.clear();

std::map<PointPair, unsigned int> edgeCount;
for(size_t i=0;i<triangles.size();i++)
{
if(inROI(triangles[i][0]) && inROI(triangles[i][1]) && inROI(triangles[i][2]))
{
for(unsigned int j=0;j<3;j++)
{
PointPair edge(triangles[i][j],triangles[i][(j==2)?0:j+1]);
// increment the number of elements (triangles) associated to the edge.
this->countEdge(edgeCount,edge);
}
}
}

for(size_t i=0;i<quads.size();i++)
{
if(inROI(quads[i][0]) && inROI(quads[i][1]) && inROI(quads[i][2]) && inROI(quads[i][3]))
{
for(unsigned int j=0;j<4;j++)
{
PointPair edge(quads[i][j],quads[i][(j==3)?0:j+1]);
// increment the number of elements (quad) associated to the edge.
this->countEdge(edgeCount,edge);
}
}
}

std::set<PointID> indexset; // enforce uniqueness since SetIndex is not a set..
for(auto it=edgeCount.begin();it!=edgeCount.end();++it)
{
// consider edge only if it is on the boundary
if(it->second==1)
{
indexset.insert(it->first.first);
indexset.insert(it->first.second);
}
}
indices.wref().insert(indices.end(), indexset.begin(), indexset.end());
}

void MeshBoundaryROI::countEdge(std::map<PointPair, unsigned>& edgeCount, PointPair& edge)
{
if(edge.first > edge.second)
{
std::swap(edge.first, edge.second);
}
const auto it = edgeCount.find(edge);
if(it != edgeCount.end())
{
it->second++;
}
else
{
edgeCount[edge]=1;
}
}

bool MeshBoundaryROI::inROI(const PointID& index) const
{
const SetIndex& ROI=this->d_inputROI.getValue();
if(ROI.size()==0) return true; // ROI empty -> use all points
if(std::find(ROI.begin(),ROI.end(),index)==ROI.end()) return false;
return true;
}

} //namespace sofa::component::engine
81 changes: 12 additions & 69 deletions modules/SofaGeneralEngine/src/SofaGeneralEngine/MeshBoundaryROI.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace sofa::component::engine

/**
* This class outputs indices of boundary vertices of a triangle/quad mesh
* The boundary is detected using the number elements associated to the edges.
* An edge is considered on the boundary if it has a unique associated element.
* @author benjamin gilles
*/
class MeshBoundaryROI : public core::DataEngine
Expand All @@ -57,83 +59,24 @@ class MeshBoundaryROI : public core::DataEngine

protected:

MeshBoundaryROI() : Inherited()
, d_triangles(initData(&d_triangles,"triangles","input triangles"))
, d_quads(initData(&d_quads,"quads","input quads"))
, d_inputROI(initData(&d_inputROI,"inputROI","optional subset of the input mesh"))
, d_indices(initData(&d_indices,"indices","Index lists of the closing vertices"))
{
}
MeshBoundaryROI();

~MeshBoundaryROI() override {}

public:
void init() override
{
addInput(&d_triangles);
addInput(&d_quads);
addInput(&d_inputROI);
addOutput(&d_indices);
void init() override;

setDirtyValue();
}
void reinit() override;

void reinit() override { update(); }
void doUpdate() override
{
helper::ReadAccessor<Data< SeqTriangles > > triangles(this->d_triangles);
helper::ReadAccessor<Data< SeqQuads > > quads(this->d_quads);
void doUpdate() override;

helper::WriteOnlyAccessor<Data< SetIndex > > indices(this->d_indices);
indices.clear();

std::map<PointPair, unsigned int> edgeCount;
for(size_t i=0;i<triangles.size();i++)
if(inROI(triangles[i][0]) && inROI(triangles[i][1]) && inROI(triangles[i][2]))
for(unsigned int j=0;j<3;j++)
{
PointPair edge(triangles[i][j],triangles[i][(j==2)?0:j+1]);
this->countEdge(edgeCount,edge);
}
for(size_t i=0;i<quads.size();i++)
if(inROI(quads[i][0]) && inROI(quads[i][1]) && inROI(quads[i][2]) && inROI(quads[i][3]))
for(unsigned int j=0;j<4;j++)
{
PointPair edge(quads[i][j],quads[i][(j==3)?0:j+1]);
this->countEdge(edgeCount,edge);
}

std::set<PointID> indexset; // enforce uniqueness since SetIndex is not a set..
for(std::map<PointPair, unsigned int>::iterator it=edgeCount.begin();it!=edgeCount.end();++it)
if(it->second==1)
{
indexset.insert(it->first.first);
indexset.insert(it->first.second);
}
indices.wref().insert(indices.end(), indexset.begin(), indexset.end());
}

void countEdge(std::map<PointPair, unsigned int>& edgeCount,PointPair& edge) const
{
if(edge.first>edge.second)
{
PointID i=edge.first;
edge.first=edge.second;
edge.second=i;
}
std::map<PointPair, unsigned int>::iterator it=edgeCount.find(edge);
if(it!=edgeCount.end()) it->second++;
else edgeCount[edge]=1;
}

inline bool inROI(const PointID& index) const
{
const SetIndex& ROI=this->d_inputROI.getValue();
if(ROI.size()==0) return true; // ROI empty -> use all points
if(std::find(ROI.begin(),ROI.end(),index)==ROI.end()) return false;
return true;
}
/// edge is used as a key to be found in the edgeCount map. If found, its value is incremented. Otherwise,
/// the value is set to 1.
static void countEdge(std::map<PointPair, unsigned int>& edgeCount, PointPair& edge);

/// Check if the point with PointID index is part of the indices defined in d_inputROI.
/// @return true if d_inputROI is empty or if index is in d_inputROI, false otherwise.
inline bool inROI(const PointID& index) const;
};

} //namespace sofa::component::engine