Skip to content

Commit 73b9cda

Browse files
alxbilgerhugtalbotfredroy
authored
[Topology.Mapping] Secure pointers access, remove macro and cache Data (#5788)
* [Topology.Mapping] Secure pointers access, remove macro and cache Data * Apply suggestions from code review Co-authored-by: Frederick Roy <fredroy@users.noreply.github.com> --------- Co-authored-by: Hugo <hugo.talbot@sofa-framework.org> Co-authored-by: Frederick Roy <fredroy@users.noreply.github.com>
1 parent df92006 commit 73b9cda

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

Sofa/Component/Topology/Mapping/src/sofa/component/topology/mapping/Hexa2TetraTopologicalMapping.cpp

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ void Hexa2TetraTopologicalMapping::init()
6666
{
6767
using namespace container::dynamic;
6868

69+
Inherit1::init();
70+
6971
if (!this->checkTopologyInputTypes()) // method will display error message if false
7072
{
7173
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
7274
return;
7375
}
7476

75-
TetrahedronSetTopologyModifier* to_tstm;
77+
TetrahedronSetTopologyModifier* to_tstm { nullptr };
7678
toModel->getContext()->get(to_tstm);
7779
if (!to_tstm)
7880
{
@@ -83,8 +85,15 @@ void Hexa2TetraTopologicalMapping::init()
8385

8486
// INITIALISATION of TETRAHEDRAL mesh from HEXAHEDRAL mesh :
8587

86-
TetrahedronSetTopologyContainer *to_tstc;
88+
TetrahedronSetTopologyContainer *to_tstc { nullptr };
8789
toModel->getContext()->get(to_tstc);
90+
if (!to_tstc)
91+
{
92+
msg_error() << "No TetrahedronSetTopologyContainer found in the Tetrahedron topology Node.";
93+
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
94+
return;
95+
}
96+
8897
// Clear output topology
8998
to_tstc->clear();
9099

@@ -111,45 +120,49 @@ void Hexa2TetraTopologicalMapping::init()
111120
}
112121
}
113122

123+
static constexpr int numberTetraInHexa = 6;
124+
Loc2GlobVec.reserve(nbcubes*numberTetraInHexa);
125+
126+
const bool swapping = d_swapping.getValue();
127+
114128
// Tessellation of each cube into 6 tetrahedra
115-
for (size_t i=0; i<nbcubes; i++)
129+
for (size_t i = 0; i < nbcubes; ++i)
116130
{
117131
core::topology::BaseMeshTopology::Hexa c = fromModel->getHexahedron(i);
118-
#define swap(a,b) { int t = a; a = b; b = t; }
119-
// TODO : swap indexes where needed (currently crash in TriangleSetContainer)
132+
120133
bool swapped = false;
121134

122-
if(d_swapping.getValue())
135+
if(swapping)
123136
{
124137
if (!((i%nx)&1))
125138
{
126139
// swap all points on the X edges
127-
swap(c[0],c[1]);
128-
swap(c[3],c[2]);
129-
swap(c[4],c[5]);
130-
swap(c[7],c[6]);
140+
std::swap(c[0],c[1]);
141+
std::swap(c[3],c[2]);
142+
std::swap(c[4],c[5]);
143+
std::swap(c[7],c[6]);
131144
swapped = !swapped;
132145
}
133146
if (((i/nx)%ny)&1)
134147
{
135148
// swap all points on the Y edges
136-
swap(c[0],c[3]);
137-
swap(c[1],c[2]);
138-
swap(c[4],c[7]);
139-
swap(c[5],c[6]);
149+
std::swap(c[0],c[3]);
150+
std::swap(c[1],c[2]);
151+
std::swap(c[4],c[7]);
152+
std::swap(c[5],c[6]);
140153
swapped = !swapped;
141154
}
142155
if ((i/(nx*ny))&1)
143156
{
144157
// swap all points on the Z edges
145-
swap(c[0],c[4]);
146-
swap(c[1],c[5]);
147-
swap(c[2],c[6]);
148-
swap(c[3],c[7]);
158+
std::swap(c[0],c[4]);
159+
std::swap(c[1],c[5]);
160+
std::swap(c[2],c[6]);
161+
std::swap(c[3],c[7]);
149162
swapped = !swapped;
150163
}
151164
}
152-
#undef swap
165+
153166
if(!swapped)
154167
{
155168
to_tstc->addTetra(c[0],c[5],c[1],c[6]);
@@ -168,9 +181,11 @@ void Hexa2TetraTopologicalMapping::init()
168181
to_tstc->addTetra(c[6],c[7],c[5],c[0]);
169182
to_tstc->addTetra(c[7],c[5],c[0],c[4]);
170183
}
171-
for(int j=0; j<6; j++)
184+
for (int j = 0; j < numberTetraInHexa; j++)
185+
{
172186
Loc2GlobVec.push_back(i);
173-
Glob2LocMap[i] = (unsigned int)Loc2GlobVec.size()-1;
187+
}
188+
Glob2LocMap[i] = static_cast<unsigned int>(Loc2GlobVec.size()) -1;
174189
}
175190

176191
// Need to fully init the target topology
@@ -181,7 +196,6 @@ void Hexa2TetraTopologicalMapping::init()
181196

182197
Index Hexa2TetraTopologicalMapping::getFromIndex(Index /*ind*/)
183198
{
184-
185199
return sofa::InvalidID;
186200
}
187201

0 commit comments

Comments
 (0)