-
Notifications
You must be signed in to change notification settings - Fork 348
[Mapping] Introduce SubsetTopologicalMultiMapping with multiple topology inputs and one output #5952
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
Open
th-skam
wants to merge
6
commits into
sofa-framework:master
Choose a base branch
from
th-skam:th-topomultimap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[Mapping] Introduce SubsetTopologicalMultiMapping with multiple topology inputs and one output #5952
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b6b6808
[Mapping] Create a SubsetTopologicalMultiMapping (points, edges, tria…
th-skam 9a53724
[Mapping] Add support for tetrahedra
th-skam a2b94fa
[Mapping] Add quads (allow flip normals) and hexahedra support
th-skam 86763bf
[Mapping] Simplify code and format with clang file
th-skam 9b0d249
[scene] Add an example for SubsetTopologicalMultiMapping
th-skam 24ded1f
[Mapping] Rely on a link to the indexPairs to couple mechanical - top…
th-skam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
...nt/Topology/Mapping/src/sofa/component/topology/mapping/SubsetTopologicalMultiMapping.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| /****************************************************************************** | ||
| * SOFA, Simulation Open-Framework Architecture * | ||
| * (c) 2006 INRIA, USTL, UJF, CNRS, MGH * | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify it * | ||
| * under the terms of the GNU Lesser General Public License as published by * | ||
| * the Free Software Foundation; either version 2.1 of the License, or (at * | ||
| * your option) any later version. * | ||
| * * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT * | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * | ||
| * for more details. * | ||
| * * | ||
| * You should have received a copy of the GNU Lesser General Public License * | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
| ******************************************************************************* | ||
| * Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
| * * | ||
| * Contact information: contact@sofa-framework.org * | ||
| ******************************************************************************/ | ||
| #include <sofa/component/topology/mapping/SubsetTopologicalMultiMapping.h> | ||
| #include <sofa/core/ObjectFactory.h> | ||
| #include <sofa/helper/accessor.h> | ||
|
|
||
| namespace sofa::component::topology::mapping | ||
| { | ||
|
|
||
| void registerSubsetTopologicalMultiMapping(sofa::core::ObjectFactory* factory) | ||
| { | ||
| factory->registerObjects( | ||
| core::ObjectRegistrationData("Merges multiple input topologies (points, edges, triangles, " | ||
| "quads, tetrahedra, hexahedra) " | ||
| "into a single output topology with index remapping. " | ||
| "Exposes indexPairs Data for linking to SubsetMultiMapping.") | ||
| .add<SubsetTopologicalMultiMapping>()); | ||
| } | ||
|
|
||
| SubsetTopologicalMultiMapping::SubsetTopologicalMultiMapping() | ||
| : l_inputs(initLink("input", "Input topology sources to merge")), | ||
| l_output(initLink("output", "Output merged topology")), | ||
| d_flipNormals( | ||
| initData(&d_flipNormals, sofa::type::vector<bool>(), "flipNormals", | ||
| "Per-source boolean flags to reverse triangle and quad winding order")), | ||
| d_indexPairs(initData(&d_indexPairs, sofa::type::vector<unsigned>(), "indexPairs", | ||
| "Output: flat array of (source_index, coord_in_source) pairs")) | ||
| { | ||
| d_indexPairs.setReadOnly(true); | ||
| } | ||
|
|
||
| SubsetTopologicalMultiMapping::~SubsetTopologicalMultiMapping() = default; | ||
|
|
||
| void SubsetTopologicalMultiMapping::init() | ||
| { | ||
| BaseObject::init(); | ||
|
|
||
| if (l_inputs.size()) | ||
| { | ||
| for (const auto inputTopology : l_inputs) | ||
| { | ||
| if (!inputTopology.get()) | ||
| { | ||
| msg_error() << "Input topology '" << inputTopology.path << "' could not be found."; | ||
| d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| msg_error() << "No input topologies found to be linked. Set the 'input' Data with " | ||
| "paths to the topologies considered as inputs for the mapping."; | ||
| d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); | ||
| return; | ||
| } | ||
|
|
||
| if (!l_output.get()) | ||
| { | ||
| msg_error() | ||
| << "Null output topology. Set the 'output' Data with the path to the output topology."; | ||
| d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); | ||
| return; | ||
| } | ||
|
|
||
| mapTopologies(); | ||
|
|
||
| d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid); | ||
| } | ||
|
|
||
| void SubsetTopologicalMultiMapping::reinit() | ||
| { | ||
| if (d_componentState.getValue() == sofa::core::objectmodel::ComponentState::Invalid) return; | ||
|
|
||
| mapTopologies(); | ||
| } | ||
|
|
||
| void SubsetTopologicalMultiMapping::mapTopologies() | ||
| { | ||
| const auto numInputs = l_inputs.size(); | ||
|
|
||
| l_output->clear(); | ||
| m_pointOffsets.clear(); | ||
|
|
||
| // Compute per-source point offsets | ||
| sofa::Size totalPoints{0}; | ||
| for (const auto input : l_inputs) | ||
| { | ||
| m_pointOffsets.push_back(totalPoints); | ||
| totalPoints += input->getNbPoints(); | ||
| } | ||
| l_output->setNbPoints(totalPoints); | ||
|
|
||
| // Build indexPairs necessary to attach mechanical mapping through SubsetMultiMapping link | ||
| { | ||
| auto indexPairs = sofa::helper::getWriteOnlyAccessor(d_indexPairs); | ||
| indexPairs.clear(); | ||
| indexPairs.reserve(totalPoints * 2); | ||
|
|
||
| for (sofa::Size srcIdx = 0; srcIdx < numInputs; ++srcIdx) | ||
| { | ||
| const auto nbPts = l_inputs.get(srcIdx)->getNbPoints(); | ||
| for (sofa::Size p = 0; p < nbPts; ++p) | ||
| { | ||
| indexPairs.push_back(srcIdx); | ||
| indexPairs.push_back(p); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Pre-normalize flipNormals to exactly numInputs entries | ||
| auto flipVec = sofa::helper::getWriteAccessor(d_flipNormals); | ||
| if (flipVec.size() > numInputs) | ||
| { | ||
| msg_warning() << "flipNormals has " << flipVec.size() << " entries but there are only " | ||
| << numInputs << " input topologies. Extra entries will be discarded."; | ||
| flipVec.resize(numInputs); | ||
| } | ||
| else if (flipVec.size() < numInputs) | ||
| { | ||
| flipVec.resize(numInputs, false); | ||
| } | ||
|
|
||
| // Concatenate edges from sources with offset remapping | ||
| for (sofa::Size srcIdx = 0; srcIdx < numInputs; ++srcIdx) | ||
| { | ||
| const sofa::Size offset = m_pointOffsets[srcIdx]; | ||
|
|
||
| for (const auto& edge : l_inputs.get(srcIdx)->getEdges()) | ||
| l_output->addEdge(edge[0] + offset, edge[1] + offset); | ||
| } | ||
|
|
||
| // Concatenate triangles with offset remapping; optionally flip normals | ||
| for (sofa::Size srcIdx = 0; srcIdx < numInputs; ++srcIdx) | ||
| { | ||
| const sofa::Size offset = m_pointOffsets[srcIdx]; | ||
| const bool flip = flipVec[srcIdx]; | ||
|
|
||
| for (const auto& tri : l_inputs.get(srcIdx)->getTriangles()) | ||
| { | ||
| if (flip) | ||
| l_output->addTriangle(tri[0] + offset, tri[2] + offset, tri[1] + offset); | ||
| else | ||
| l_output->addTriangle(tri[0] + offset, tri[1] + offset, tri[2] + offset); | ||
| } | ||
| } | ||
|
|
||
| // Concatenate quads with offset remapping; optionally flip normals | ||
| for (sofa::Size srcIdx = 0; srcIdx < numInputs; ++srcIdx) | ||
| { | ||
| const sofa::Size offset = m_pointOffsets[srcIdx]; | ||
| const bool flip = flipVec[srcIdx]; | ||
|
|
||
| for (auto& quad : l_inputs.get(srcIdx)->getQuads()) | ||
| { | ||
| if (flip) | ||
| l_output->addQuad(quad[0] + offset, quad[3] + offset, quad[2] + offset, | ||
| quad[1] + offset); | ||
| else | ||
| l_output->addQuad(quad[0] + offset, quad[1] + offset, quad[2] + offset, | ||
| quad[3] + offset); | ||
| } | ||
| } | ||
|
|
||
| // Concatenate tetrahedra with offset remapping | ||
| for (sofa::Size srcIdx = 0; srcIdx < numInputs; ++srcIdx) | ||
| { | ||
| const sofa::Size offset = m_pointOffsets[srcIdx]; | ||
|
|
||
| for (const auto& tet : l_inputs.get(srcIdx)->getTetrahedra()) | ||
| l_output->addTetra(tet[0] + offset, tet[1] + offset, tet[2] + offset, tet[3] + offset); | ||
| } | ||
|
|
||
| // Concatenate hexahedra with offset remapping | ||
| for (sofa::Size srcIdx = 0; srcIdx < numInputs; ++srcIdx) | ||
| { | ||
| const sofa::Size offset = m_pointOffsets[srcIdx]; | ||
|
|
||
| for (const auto& hex : l_inputs.get(srcIdx)->getHexahedra()) | ||
| { | ||
| l_output->addHexa(hex[0] + offset, hex[1] + offset, hex[2] + offset, hex[3] + offset, | ||
| hex[4] + offset, hex[5] + offset, hex[6] + offset, hex[7] + offset); | ||
| } | ||
| } | ||
|
|
||
| msg_info() << "Merged " << numInputs << " topologies: " << totalPoints << " points, " | ||
| << l_output->getNbEdges() << " edges, " << l_output->getNbTriangles() | ||
| << " triangles, " << l_output->getNbQuads() << " quads, " | ||
| << l_output->getNbTetrahedra() << " tetrahedra, " << l_output->getNbHexahedra() | ||
| << " hexahedra."; | ||
| } | ||
|
|
||
| } // namespace sofa::component::topology::mapping | ||
84 changes: 84 additions & 0 deletions
84
...nent/Topology/Mapping/src/sofa/component/topology/mapping/SubsetTopologicalMultiMapping.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /****************************************************************************** | ||
| * SOFA, Simulation Open-Framework Architecture * | ||
| * (c) 2006 INRIA, USTL, UJF, CNRS, MGH * | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify it * | ||
| * under the terms of the GNU Lesser General Public License as published by * | ||
| * the Free Software Foundation; either version 2.1 of the License, or (at * | ||
| * your option) any later version. * | ||
| * * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT * | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * | ||
| * for more details. * | ||
| * * | ||
| * You should have received a copy of the GNU Lesser General Public License * | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
| ******************************************************************************* | ||
| * Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
| * * | ||
| * Contact information: contact@sofa-framework.org * | ||
| ******************************************************************************/ | ||
| #pragma once | ||
| #include <sofa/component/topology/mapping/config.h> | ||
| #include <sofa/core/objectmodel/BaseObject.h> | ||
| #include <sofa/core/topology/BaseMeshTopology.h> | ||
|
|
||
| namespace sofa::component::topology::mapping | ||
| { | ||
|
|
||
| /** | ||
| * @class SubsetTopologicalMultiMapping | ||
| * @brief Merges multiple input topologies into a single output topology. | ||
| * | ||
| * For each input topology, points are concatenated with index offsets, and geometric primitives | ||
| * (edges/triangles/quads/tetrahedra/hexahedra are remapped accordingly. Triangle/quad winding | ||
| * order can be reversed per source via flipNormals. | ||
| * | ||
| * This is the topological counterpart to SubsetMultiMapping. | ||
| * The indexPairs output can be linked to a SubsetMultiMapping in scenes. | ||
| * | ||
| * Merging is performed once during init(). Dynamic topology changes are not supported. | ||
| */ | ||
| class SOFA_COMPONENT_TOPOLOGY_MAPPING_API SubsetTopologicalMultiMapping | ||
| : public sofa::core::objectmodel::BaseObject | ||
| { | ||
| public: | ||
| SOFA_CLASS(SubsetTopologicalMultiMapping, sofa::core::objectmodel::BaseObject); | ||
|
|
||
| using Index = sofa::Index; | ||
| using BaseMeshTopology = sofa::core::topology::BaseMeshTopology; | ||
|
|
||
| void init() override; | ||
| void reinit() override; | ||
|
|
||
| /// N input topologies | ||
| MultiLink<SubsetTopologicalMultiMapping, BaseMeshTopology, | ||
| sofa::core::objectmodel::BaseLink::FLAG_STOREPATH | | ||
| sofa::core::objectmodel::BaseLink::FLAG_STRONGLINK> | ||
| l_inputs; | ||
|
|
||
| /// Single output topology | ||
| SingleLink<SubsetTopologicalMultiMapping, BaseMeshTopology, | ||
| sofa::core::objectmodel::BaseLink::FLAG_STOREPATH | | ||
| sofa::core::objectmodel::BaseLink::FLAG_STRONGLINK> | ||
| l_output; | ||
|
|
||
| Data<sofa::type::vector<bool>> | ||
| d_flipNormals; ///< Per-source flags to reverse triangle and/or quad winding order | ||
| Data<sofa::type::vector<unsigned>> | ||
| d_indexPairs; ///< Output: flat array of (source_index, coord_in_source) pairs, | ||
| ///< linkable to SubsetMultiMapping | ||
|
|
||
| protected: | ||
| SubsetTopologicalMultiMapping(); | ||
| ~SubsetTopologicalMultiMapping() override; | ||
|
|
||
| private: | ||
| void mapTopologies(); | ||
|
|
||
| /// Per-source cumulative point offsets. m_pointOffsets[i] = total points from sources 0..i-1. | ||
| sofa::type::vector<Index> m_pointOffsets; | ||
| }; | ||
|
|
||
| } // namespace sofa::component::topology::mapping |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can eventually add
d_indexPairsto the group "Outputs"