Skip to content

Commit 04a4a91

Browse files
committed
perf(SolidMesh): better return type of polyhedron_facets_vertices and polyhedron_edges_vertices
WARNING: there is a minor breaking change due to the return type modification. Please prefer using auto or absl::Span for better generic code
1 parent 65296c3 commit 04a4a91

File tree

6 files changed

+90
-80
lines changed

6 files changed

+90
-80
lines changed

include/geode/mesh/core/geode_hybrid_solid.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ namespace geode
138138
absl::optional< index_t > get_polyhedron_adjacent(
139139
const PolyhedronFacet& polyhedron_facet ) const override;
140140

141-
std::vector< std::array< index_t, 2 > > polyhedron_edges_vertices(
141+
PolyhedronEdgesVertices polyhedron_edges_vertices(
142142
index_t polyhedron ) const final;
143143

144-
std::vector< PolyhedronFacetVertices > polyhedron_facets_vertices(
144+
PolyhedronFacetsVertices polyhedron_facets_vertices(
145145
index_t polyhedron ) const final;
146146

147147
typename HybridSolid< dimension >::Type polyhedron_type(

include/geode/mesh/core/solid_mesh.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,14 @@ namespace geode
197197
local_index_t edge_id{ NO_LID };
198198
};
199199

200+
using PolyhedronEdgesVertices =
201+
absl::InlinedVector< std::array< index_t, 2 >, 6 >;
202+
200203
using PolyhedronFacetVertices = absl::InlinedVector< index_t, 3 >;
201204

205+
using PolyhedronFacetsVertices =
206+
absl::InlinedVector< PolyhedronFacetVertices, 4 >;
207+
202208
using PolyhedronVertices = absl::InlinedVector< index_t, 4 >;
203209

204210
using PolyhedronFacets = absl::InlinedVector< PolyhedronFacet, 4 >;
@@ -314,17 +320,17 @@ namespace geode
314320
const std::array< index_t, 2 >& edge_vertices,
315321
index_t polyhedron_id ) const;
316322

317-
virtual std::vector< std::array< index_t, 2 > >
318-
polyhedron_edges_vertices( index_t polyhedron ) const;
323+
virtual PolyhedronEdgesVertices polyhedron_edges_vertices(
324+
index_t polyhedron ) const;
319325

320326
absl::optional< PolyhedronFacet > polyhedron_facet_from_vertices(
321327
PolyhedronFacetVertices polyhedron_facet_vertices ) const;
322328

323329
PolyhedronFacetVertices polyhedron_facet_vertices(
324330
const PolyhedronFacet& polyhedron_facet ) const;
325331

326-
virtual std::vector< PolyhedronFacetVertices >
327-
polyhedron_facets_vertices( index_t polyhedron ) const;
332+
virtual PolyhedronFacetsVertices polyhedron_facets_vertices(
333+
index_t polyhedron ) const;
328334

329335
virtual PolyhedronFacets polyhedron_vertex_facets(
330336
const PolyhedronVertex& polyhedron_vertex ) const;

include/geode/mesh/core/tetrahedral_solid.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ namespace geode
6363

6464
std::unique_ptr< TetrahedralSolid< dimension > > clone() const;
6565

66-
std::vector< std::array< index_t, 2 > > polyhedron_edges_vertices(
66+
PolyhedronEdgesVertices polyhedron_edges_vertices(
6767
index_t polyhedron ) const final;
6868

69-
std::vector< PolyhedronFacetVertices > polyhedron_facets_vertices(
69+
PolyhedronFacetsVertices polyhedron_facets_vertices(
7070
index_t polyhedron ) const final;
7171

7272
PolyhedraAroundEdge polyhedra_around_edge(

src/geode/mesh/core/geode_hybrid_solid.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ namespace
5151
};
5252

5353
template < typename Data >
54-
std::vector< std::array< geode::index_t, 2 > > polyhedron_edges_vertices(
54+
geode::PolyhedronEdgesVertices polyhedron_edges_vertices(
5555
const geode::HybridSolid3D& mesh,
5656
geode::index_t polyhedron,
5757
const Data& data )
5858
{
59-
std::vector< std::array< geode::index_t, 2 > > result;
59+
geode::PolyhedronEdgesVertices result;
6060
result.reserve( data.size() );
6161
for( const auto& edge : data )
6262
{
@@ -68,12 +68,12 @@ namespace
6868
}
6969

7070
template < typename Data >
71-
std::vector< geode::PolyhedronFacetVertices > polyhedron_facets_vertices(
71+
geode::PolyhedronFacetsVertices polyhedron_facets_vertices(
7272
const geode::HybridSolid3D& mesh,
7373
geode::index_t polyhedron,
7474
const Data& data )
7575
{
76-
std::vector< geode::PolyhedronFacetVertices > result;
76+
geode::PolyhedronFacetsVertices result;
7777
result.reserve( data.size() );
7878
for( const auto& facet : data )
7979
{
@@ -293,7 +293,7 @@ namespace geode
293293
polyhedron_adjacents_.end() );
294294
}
295295

296-
std::vector< std::array< index_t, 2 > > polyhedron_edges_vertices(
296+
PolyhedronEdgesVertices polyhedron_edges_vertices(
297297
const HybridSolid< dimension >& solid, index_t polyhedron ) const
298298
{
299299
switch( polyhedron_type( polyhedron ) )
@@ -317,7 +317,7 @@ namespace geode
317317
return {};
318318
}
319319

320-
std::vector< PolyhedronFacetVertices > polyhedron_facets_vertices(
320+
PolyhedronFacetsVertices polyhedron_facets_vertices(
321321
const HybridSolid< dimension >& solid, index_t polyhedron ) const
322322
{
323323
switch( polyhedron_type( polyhedron ) )
@@ -604,15 +604,15 @@ namespace geode
604604
}
605605

606606
template < index_t dimension >
607-
std::vector< std::array< index_t, 2 > >
607+
PolyhedronEdgesVertices
608608
OpenGeodeHybridSolid< dimension >::polyhedron_edges_vertices(
609609
index_t polyhedron ) const
610610
{
611611
return impl_->polyhedron_edges_vertices( *this, polyhedron );
612612
}
613613

614614
template < index_t dimension >
615-
std::vector< PolyhedronFacetVertices >
615+
PolyhedronFacetsVertices
616616
OpenGeodeHybridSolid< dimension >::polyhedron_facets_vertices(
617617
index_t polyhedron ) const
618618
{

src/geode/mesh/core/solid_mesh.cpp

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,12 +1002,10 @@ namespace geode
10021002
}
10031003

10041004
template < index_t dimension >
1005-
std::vector< std::array< index_t, 2 > >
1006-
SolidMesh< dimension >::polyhedron_edges_vertices(
1007-
index_t polyhedron ) const
1005+
PolyhedronEdgesVertices SolidMesh< dimension >::polyhedron_edges_vertices(
1006+
index_t polyhedron ) const
10081007
{
1009-
std::vector< std::array< index_t, 2 > > edge_vertices;
1010-
edge_vertices.reserve( 3 * nb_polyhedron_facets( polyhedron ) );
1008+
PolyhedronEdgesVertices edge_vertices;
10111009
for( const auto f : LRange{ nb_polyhedron_facets( polyhedron ) } )
10121010
{
10131011
const PolyhedronFacet facet{ polyhedron, f };
@@ -1025,18 +1023,25 @@ namespace geode
10251023
}
10261024

10271025
template < index_t dimension >
1028-
std::vector< PolyhedronFacetVertices >
1029-
SolidMesh< dimension >::polyhedron_facets_vertices(
1030-
index_t polyhedron ) const
1026+
PolyhedronFacetsVertices SolidMesh< dimension >::polyhedron_facets_vertices(
1027+
index_t polyhedron ) const
10311028
{
1032-
std::vector< PolyhedronFacetVertices > facet_vertices;
1033-
facet_vertices.reserve( 3 * nb_polyhedron_facets( polyhedron ) );
1029+
PolyhedronFacetsVertices facets_vertices;
1030+
facets_vertices.reserve( nb_polyhedron_facets( polyhedron ) );
1031+
const auto vertices = polyhedron_vertices( polyhedron );
10341032
for( const auto f : LRange{ nb_polyhedron_facets( polyhedron ) } )
10351033
{
1036-
facet_vertices.emplace_back(
1037-
polyhedron_facet_vertices( { polyhedron, f } ) );
1034+
const PolyhedronFacet facet{ polyhedron, f };
1035+
auto& facet_vertices = facets_vertices.emplace_back();
1036+
for( const auto v :
1037+
LRange{ nb_polyhedron_facet_vertices( facet ) } )
1038+
{
1039+
facet_vertices.push_back(
1040+
vertices[polyhedron_facet_vertex_id( { facet, v } )
1041+
.vertex_id] );
1042+
}
10381043
}
1039-
return facet_vertices;
1044+
return facets_vertices;
10401045
}
10411046

10421047
template < index_t dimension >
@@ -1184,47 +1189,46 @@ namespace geode
11841189
SolidMesh< dimension >::polyhedron_adjacent_facet(
11851190
const PolyhedronFacet& polyhedron_facet ) const
11861191
{
1187-
if( !is_polyhedron_facet_on_border( polyhedron_facet ) )
1192+
const auto opt_polyhedron_adj = polyhedron_adjacent( polyhedron_facet );
1193+
if( !opt_polyhedron_adj )
11881194
{
1189-
absl::FixedArray< index_t > vertices(
1190-
nb_polyhedron_facet_vertices( polyhedron_facet ) );
1191-
for( const auto v : LIndices{ vertices } )
1195+
return absl::nullopt;
1196+
}
1197+
absl::FixedArray< index_t > vertices(
1198+
nb_polyhedron_facet_vertices( polyhedron_facet ) );
1199+
for( const auto v : LIndices{ vertices } )
1200+
{
1201+
vertices[v] = polyhedron_facet_vertex( { polyhedron_facet, v } );
1202+
}
1203+
const auto polyhedron_adj = opt_polyhedron_adj.value();
1204+
for( const auto f : LRange{ nb_polyhedron_facets( polyhedron_adj ) } )
1205+
{
1206+
if( polyhedron_adjacent( { polyhedron_adj, f } )
1207+
!= polyhedron_facet.polyhedron_id )
11921208
{
1193-
vertices[v] =
1194-
polyhedron_facet_vertex( { polyhedron_facet, v } );
1209+
continue;
11951210
}
1196-
const auto polyhedron_adj =
1197-
polyhedron_adjacent( polyhedron_facet ).value();
1198-
for( const auto f :
1199-
LRange{ nb_polyhedron_facets( polyhedron_adj ) } )
1211+
bool all_contained{ true };
1212+
for( const auto v : LRange{
1213+
nb_polyhedron_facet_vertices( { polyhedron_adj, f } ) } )
12001214
{
1201-
const auto polyhedron =
1202-
polyhedron_adjacent( { polyhedron_adj, f } );
1203-
if( polyhedron == polyhedron_facet.polyhedron_id )
1215+
if( absl::c_find( vertices, polyhedron_facet_vertex(
1216+
{ { polyhedron_adj, f }, v } ) )
1217+
== vertices.end() )
12041218
{
1205-
bool all_contained{ true };
1206-
for( const auto v : LRange{ nb_polyhedron_facet_vertices(
1207-
{ polyhedron_adj, f } ) } )
1208-
{
1209-
if( absl::c_find(
1210-
vertices, polyhedron_facet_vertex(
1211-
{ { polyhedron_adj, f }, v } ) )
1212-
== vertices.end() )
1213-
{
1214-
all_contained = false;
1215-
break;
1216-
}
1217-
}
1218-
if( all_contained )
1219-
{
1220-
return PolyhedronFacet{ polyhedron_adj, f };
1221-
}
1219+
all_contained = false;
1220+
break;
12221221
}
12231222
}
1224-
throw OpenGeodeException{ "[SolidMesh::polyhedron_adjacent_"
1225-
"facet] Wrong adjacency with polyhedra: ",
1226-
polyhedron_facet.polyhedron_id, " and ", polyhedron_adj };
1223+
if( all_contained )
1224+
{
1225+
return absl::optional< PolyhedronFacet >{ absl::in_place,
1226+
polyhedron_adj, f };
1227+
}
12271228
}
1229+
throw OpenGeodeException{ "[SolidMesh::polyhedron_adjacent_"
1230+
"facet] Wrong adjacency with polyhedra: ",
1231+
polyhedron_facet.polyhedron_id, " and ", polyhedron_adj };
12281232
return absl::nullopt;
12291233
}
12301234

src/geode/mesh/core/tetrahedral_solid.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,23 +227,25 @@ namespace geode
227227
index_t first_polyhedron ) const
228228
{
229229
PolyhedraAroundEdge result{ first_polyhedron };
230-
const auto v0 =
231-
this->vertex_in_polyhedron( first_polyhedron, vertices[0] );
232-
OPENGEODE_ASSERT( v0, "[TetrahedralSolid::polyhedra_around_edge] First "
233-
"vertex is not in given tetrahedron" );
234-
const auto v1 =
235-
this->vertex_in_polyhedron( first_polyhedron, vertices[1] );
236-
OPENGEODE_ASSERT( v1,
237-
"[TetrahedralSolid::polyhedra_around_edge] Second "
238-
"vertex is not in given tetrahedron" );
230+
std::array< index_t, 2 > excluded_facets{ NO_ID, NO_ID };
231+
local_index_t count{ 0 };
232+
const auto polyhedron_vertices =
233+
this->polyhedron_vertices( first_polyhedron );
234+
for( const auto v : LRange{ 4 } )
235+
{
236+
if( polyhedron_vertices[v] == vertices[0]
237+
|| polyhedron_vertices[v] == vertices[1] )
238+
{
239+
excluded_facets[count++] = v;
240+
}
241+
}
239242
for( const auto f : LRange{ 4 } )
240243
{
241-
if( f == v0 || f == v1 )
244+
if( f == excluded_facets[0] || f == excluded_facets[1] )
242245
{
243246
continue;
244247
}
245-
const auto vertex_id =
246-
this->polyhedron_vertex( { first_polyhedron, f } );
248+
const auto vertex_id = polyhedron_vertices[f];
247249
if( vertex_id == vertices[0] || vertex_id == vertices[1] )
248250
{
249251
continue;
@@ -273,7 +275,7 @@ namespace geode
273275
}
274276

275277
template < index_t dimension >
276-
std::vector< std::array< index_t, 2 > >
278+
PolyhedronEdgesVertices
277279
TetrahedralSolid< dimension >::polyhedron_edges_vertices(
278280
index_t polyhedron ) const
279281
{
@@ -282,8 +284,7 @@ namespace geode
282284
this->polyhedron_vertex( { polyhedron, 1 } ),
283285
this->polyhedron_vertex( { polyhedron, 2 } ),
284286
this->polyhedron_vertex( { polyhedron, 3 } ) };
285-
std::vector< std::array< index_t, 2 > > result;
286-
result.reserve( detail::tetrahedron_edge_vertices.size() );
287+
PolyhedronEdgesVertices result;
287288
for( const auto& edge : detail::tetrahedron_edge_vertices )
288289
{
289290
result.emplace_back( std::array< index_t, 2 >{
@@ -306,7 +307,7 @@ namespace geode
306307
}
307308

308309
template < index_t dimension >
309-
std::vector< PolyhedronFacetVertices >
310+
PolyhedronFacetsVertices
310311
TetrahedralSolid< dimension >::polyhedron_facets_vertices(
311312
index_t polyhedron ) const
312313
{
@@ -315,8 +316,7 @@ namespace geode
315316
this->polyhedron_vertex( { polyhedron, 1 } ),
316317
this->polyhedron_vertex( { polyhedron, 2 } ),
317318
this->polyhedron_vertex( { polyhedron, 3 } ) };
318-
std::vector< PolyhedronFacetVertices > result;
319-
result.reserve( detail::tetrahedron_facet_vertices.size() );
319+
PolyhedronFacetsVertices result;
320320
for( const auto& facet : detail::tetrahedron_facet_vertices )
321321
{
322322
result.emplace_back( PolyhedronFacetVertices{

0 commit comments

Comments
 (0)