From 61983e859b3211b107041e43eecc0f8e8ba08611 Mon Sep 17 00:00:00 2001 From: Frederick Roy Date: Wed, 21 Oct 2026 09:02:56 +0900 Subject: [PATCH 1/2] [Topology.Grid,Mapping.Linear] BarycentricMapperRegularGridTopology: grid resolution read once per loop GridTopology::getHexaCopy(i) read the Data d_n 9 times (once, then once per node through point()/getIndex), and BarycentricMapperRegularGridTopology calls it for each mapped point in apply, applyJ, applyJT, getJ and draw --- .../BarycentricMapperRegularGridTopology.inl | 19 +++++++++----- .../topology/container/grid/GridTopology.cpp | 26 ++++++++++++------- .../topology/container/grid/GridTopology.h | 6 +++++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/Sofa/Component/Mapping/Linear/src/sofa/component/mapping/linear/BarycentricMappers/BarycentricMapperRegularGridTopology.inl b/Sofa/Component/Mapping/Linear/src/sofa/component/mapping/linear/BarycentricMappers/BarycentricMapperRegularGridTopology.inl index 893018d352b..8ec90f28f2e 100644 --- a/Sofa/Component/Mapping/Linear/src/sofa/component/mapping/linear/BarycentricMappers/BarycentricMapperRegularGridTopology.inl +++ b/Sofa/Component/Mapping/Linear/src/sofa/component/mapping/linear/BarycentricMappers/BarycentricMapperRegularGridTopology.inl @@ -103,9 +103,11 @@ void BarycentricMapperRegularGridTopology::apply ( typename Out::VecCoor { out.resize( m_map.size() ); + // the resolution of the grid read once (getHexaCopy(Index) reads the Data) + const auto& n = this->m_fromTopology->d_n.getValue(); for ( unsigned int i=0; im_fromTopology->getHexaCopy ( this->m_map[i].in_index ); + const topology::container::grid::RegularGridTopology::Hexa cube = RegularGridTopology::getHexaCopy ( n, this->m_map[i].in_index ); const Real fx = m_map[i].baryCoords[0]; const Real fy = m_map[i].baryCoords[1]; @@ -127,9 +129,10 @@ void BarycentricMapperRegularGridTopology::applyJ ( typename Out::VecDer { out.resize( m_map.size() ); + const auto& n = this->m_fromTopology->d_n.getValue(); for( size_t index=0 ; index< out.size() ; ++index) { - const topology::container::grid::RegularGridTopology::Hexa cube = this->m_fromTopology->getHexaCopy ( this->m_map[index].in_index ); + const topology::container::grid::RegularGridTopology::Hexa cube = RegularGridTopology::getHexaCopy ( n, this->m_map[index].in_index ); const Real fx = m_map[index].baryCoords[0]; const Real fy = m_map[index].baryCoords[1]; @@ -148,10 +151,11 @@ void BarycentricMapperRegularGridTopology::applyJ ( typename Out::VecDer template void BarycentricMapperRegularGridTopology::applyJT ( typename In::VecDeriv& out, const typename Out::VecDeriv& in ) { + const auto& n = this->m_fromTopology->d_n.getValue(); for( size_t index=0 ; indexm_fromTopology->getHexaCopy ( this->m_map[index].in_index ); + const topology::container::grid::RegularGridTopology::Hexa cube = RegularGridTopology::getHexaCopy ( n, this->m_map[index].in_index ); const OutReal fx = ( OutReal ) m_map[index].baryCoords[0]; const OutReal fy = ( OutReal ) m_map[index].baryCoords[1]; @@ -185,11 +189,12 @@ const sofa::linearalgebra::BaseMatrix* BarycentricMapperRegularGridTopologyclear(); + const auto& n = this->m_fromTopology->d_n.getValue(); for ( size_t i=0; im_fromTopology->getHexaCopy ( this->m_map[i].in_index ); + const topology::container::grid::RegularGridTopology::Hexa cube = RegularGridTopology::getHexaCopy ( n, this->m_map[i].in_index ); const Real fx = ( Real ) m_map[i].baryCoords[0]; const Real fy = ( Real ) m_map[i].baryCoords[1]; @@ -219,10 +224,11 @@ void BarycentricMapperRegularGridTopology::draw (const core::visual::Vi { std::vector< Vec3 > points; + const auto& n = this->m_fromTopology->d_n.getValue(); for ( unsigned int i=0; im_fromTopology->getHexaCopy ( this->m_map[i].in_index ); + const topology::container::grid::RegularGridTopology::Hexa cube = RegularGridTopology::getHexaCopy ( n, this->m_map[i].in_index ); const Real fx = m_map[i].baryCoords[0]; const Real fy = m_map[i].baryCoords[1]; @@ -256,6 +262,7 @@ template void BarycentricMapperRegularGridTopology::applyJT ( typename In::MatrixDeriv& out, const typename Out::MatrixDeriv& in ) { typename Out::MatrixDeriv::RowConstIterator rowItEnd = in.end(); + const auto& n = this->m_fromTopology->d_n.getValue(); for (typename Out::MatrixDeriv::RowConstIterator rowIt = in.begin(); rowIt != rowItEnd; ++rowIt) { @@ -271,7 +278,7 @@ void BarycentricMapperRegularGridTopology::applyJT ( typename In::Matrix unsigned int indexIn = colIt.index(); InDeriv data = (InDeriv) Out::getDPos(colIt.val()); - const topology::container::grid::RegularGridTopology::Hexa cube = this->m_fromTopology->getHexaCopy ( this->m_map[indexIn].in_index ); + const topology::container::grid::RegularGridTopology::Hexa cube = RegularGridTopology::getHexaCopy ( n, this->m_map[indexIn].in_index ); const OutReal fx = (OutReal) m_map[indexIn].baryCoords[0]; const OutReal fy = (OutReal) m_map[indexIn].baryCoords[1]; diff --git a/Sofa/Component/Topology/Container/Grid/src/sofa/component/topology/container/grid/GridTopology.cpp b/Sofa/Component/Topology/Container/Grid/src/sofa/component/topology/container/grid/GridTopology.cpp index 2a23965f9b7..1c0593ee961 100644 --- a/Sofa/Component/Topology/Container/Grid/src/sofa/component/topology/container/grid/GridTopology.cpp +++ b/Sofa/Component/Topology/Container/Grid/src/sofa/component/topology/container/grid/GridTopology.cpp @@ -351,8 +351,7 @@ void GridTopology::computePointList() GridTopology::Index GridTopology::getIndex( int i, int j, int k ) const { - const auto& n = d_n.getValue(); - return Index(n[0]* ( n[1]*k + j ) + i); + return getIndex(d_n.getValue(), i, j, k); } @@ -380,21 +379,28 @@ sofa::type::Vec3 GridTopology::getPointInGrid(int i, int j, int k) const GridTopology::Hexa GridTopology::getHexaCopy(Index i) { - const auto& n = d_n.getValue(); + return getHexaCopy(d_n.getValue(), i); +} + +GridTopology::Hexa GridTopology::getHexahedron(int x, int y, int z) +{ + return getHexahedron(d_n.getValue(), x, y, z); +} +GridTopology::Hexa GridTopology::getHexaCopy(const type::Vec3i& n, Index i) +{ const int x = i%(n[0]-1); i/=(n[0]-1); const int y = i%(n[1]-1); i/=(n[1]-1); const int z = int(i); - return getHexahedron(x,y,z); + return getHexahedron(n, x, y, z); } -GridTopology::Hexa GridTopology::getHexahedron(int x, int y, int z) +GridTopology::Hexa GridTopology::getHexahedron(const type::Vec3i& n, int x, int y, int z) { - - return Hexa(point(x ,y ,z ),point(x+1,y ,z ), - point(x+1,y+1,z ),point(x ,y+1,z ), - point(x ,y ,z+1),point(x+1,y ,z+1), - point(x+1,y+1,z+1),point(x ,y+1,z+1)); + return Hexa(getIndex(n, x ,y ,z ),getIndex(n, x+1,y ,z ), + getIndex(n, x+1,y+1,z ),getIndex(n, x ,y+1,z ), + getIndex(n, x ,y ,z+1),getIndex(n, x+1,y ,z+1), + getIndex(n, x+1,y+1,z+1),getIndex(n, x ,y+1,z+1)); } GridTopology::Quad GridTopology::getQuadCopy(Index i) diff --git a/Sofa/Component/Topology/Container/Grid/src/sofa/component/topology/container/grid/GridTopology.h b/Sofa/Component/Topology/Container/Grid/src/sofa/component/topology/container/grid/GridTopology.h index 3873134d709..6f8cbd4f4d1 100644 --- a/Sofa/Component/Topology/Container/Grid/src/sofa/component/topology/container/grid/GridTopology.h +++ b/Sofa/Component/Topology/Container/Grid/src/sofa/component/topology/container/grid/GridTopology.h @@ -141,6 +141,8 @@ using MeshTopology::getHexahedron; /// Get the one-dimensional index of a grid point given its @param i @param j @param k indices Index getIndex( int i, int j, int k ) const; + /// Get the one-dimensional index of the point @param i @param j @param k of a grid of resolution @param n + static Index getIndex( const type::Vec3i& n, int i, int j, int k ) { return Index(n[0]* ( n[1]*k + j ) + i); } /// Overwrite from @sa MeshTopology::hasPos always @return bool true bool hasPos() const override { return true; } @@ -170,6 +172,10 @@ using MeshTopology::getHexahedron; Hexa getHexahedron(int x, int y, int z); Hexa getHexaCopy(Index i); + /// Get the hexahedron of the cell @param x @param y @param z of a grid of resolution @param n + static Hexa getHexahedron(const type::Vec3i& n, int x, int y, int z); + /// Get the hexahedron of index @param i of a grid of resolution @param n + static Hexa getHexaCopy(const type::Vec3i& n, Index i); Quad getQuadCopy(Index i); /// Get Point index in Grid, will call method @sa getIndex From f3411ecff242060528ec4fa85dbedac841d7dd72 Mon Sep 17 00:00:00 2001 From: Frederick Roy Date: Wed, 21 Oct 2026 09:20:23 +0900 Subject: [PATCH 2/2] add benchmark of the BarycentricMapping on a RegularGridTopology --- .../BarycentricMapping_regulargrid.scn | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 examples/Benchmark/Performance/BarycentricMapping_regulargrid.scn diff --git a/examples/Benchmark/Performance/BarycentricMapping_regulargrid.scn b/examples/Benchmark/Performance/BarycentricMapping_regulargrid.scn new file mode 100644 index 00000000000..c0ebab8e83f --- /dev/null +++ b/examples/Benchmark/Performance/BarycentricMapping_regulargrid.scn @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +