Skip to content
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
91 changes: 63 additions & 28 deletions src/geode/geometry/basic_objects/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,26 @@
#include <geode/geometry/bounding_box.hpp>
#include <geode/geometry/distance.hpp>

namespace mapbox
namespace mapbox::util
{
namespace util
template < std::size_t coord, geode::index_t dimension >
struct nth< coord, geode::Point< dimension > >
{
template < std::size_t coord, geode::index_t dimension >
struct nth< coord, geode::Point< dimension > >
static auto get( const geode::Point< dimension >& point )
{
inline static auto get( const geode::Point< dimension >& point )
{
return point.value( coord );
};
return point.value( coord );
};
};

template < std::size_t coord, geode::index_t dimension >
struct nth< coord, geode::RefPoint< dimension > >
template < std::size_t coord, geode::index_t dimension >
struct nth< coord, geode::RefPoint< dimension > >
{
static auto get( const geode::RefPoint< dimension >& point )
{
inline static auto get( const geode::RefPoint< dimension >& point )
{
return point.get().value( coord );
};
return point.get().value( coord );
};
} // namespace util
} // namespace mapbox
};
} // namespace mapbox::util

namespace
{
Expand Down Expand Up @@ -90,6 +87,44 @@
}
return polygons;
}

template < typename PointType >
void reoriente_triangulation(
const geode::GenericPolygon< PointType, 2 >& /*polygon*/,
std::vector< std::array< geode::index_t, 3 > >& /*triangles*/ )
{
}

template < typename PointType >
void reoriente_triangulation(
const geode::GenericPolygon< PointType, 3 >& polygon,
std::vector< std::array< geode::index_t, 3 > >& triangles )
{
const auto normal =
polygon.normal().value_or( geode::Vector3D{ { 0, 0, 1 } } );
geode::index_t nb_same_orientation{ 0 };
for( const auto& triangle : triangles )
{
const auto& vertices = polygon.vertices();
const auto& point0 = vertices[triangle[0]];
const auto& point1 = vertices[triangle[1]];
const auto& point2 = vertices[triangle[2]];
const auto triangle_normal =
geode::Triangle< 3 >{ point0, point1, point2 }.normal();
if( triangle_normal && triangle_normal->dot( normal ) > 0 )
{
nb_same_orientation++;
}
}
if( nb_same_orientation > triangles.size() / 2 )
{
return;
}
for( auto& triangle : triangles )
{
std::swap( triangle[1], triangle[2] );
}
}
} // namespace

namespace geode
Expand Down Expand Up @@ -137,13 +172,13 @@
GenericPolygon< PointType, dimension >::normal() const
{
Vector3D normal;
const auto& p0 = vertices_[0];
const auto& point0 = vertices_[0];
for( const auto v : Range{ 2, nb_vertices() } )
{
const auto& p1 = vertices_[v - 1];
const auto& p2 = vertices_[v];
const auto& point1 = vertices_[v - 1];
const auto& point2 = vertices_[v];
if( const auto triangle_normal =
Triangle< T >{ p0, p1, p2 }.normal() )
Triangle< T >{ point0, point1, point2 }.normal() )
{
normal += triangle_normal.value();
}
Expand Down Expand Up @@ -220,23 +255,23 @@
std::vector< std::array< index_t, 3 > >
GenericPolygon< PointType, dimension >::triangulate() const
{
const std::array polygons{ vertices_ };
const auto new_triangles =
mapbox::earcut< index_t >( polygon_points( *this ) );
const auto nb_new_triangles = new_triangles.size() / 3;
std::vector< std::array< index_t, 3 > > result;
result.reserve( nb_new_triangles );
for( const auto trgl : Range{ nb_new_triangles } )
{
result.emplace_back( std::array< index_t, 3 >{
new_triangles[3 * trgl], new_triangles[3 * trgl + 1],
new_triangles[3 * trgl + 2] } );
const auto cur_trgl = 3 * trgl;
result.emplace_back( std::array{ new_triangles[cur_trgl],
new_triangles[cur_trgl + 1], new_triangles[cur_trgl + 2] } );
}
reoriente_triangulation( *this, result );
return result;
}

template < typename PointType, index_t dimension >
double GenericPolygon< PointType, dimension >::minimum_height() const

Check warning on line 274 in src/geode/geometry/basic_objects/polygon.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

src/geode/geometry/basic_objects/polygon.cpp:274:52 [readability-function-cognitive-complexity]

function 'minimum_height' has cognitive complexity of 11 (threshold 10)
{
const auto nb_vertices = vertices_.size();
double max_length{ 0. };
Expand Down Expand Up @@ -264,7 +299,7 @@
geode::Segment< dimension >{
vertices_[max_length_edge], vertices_[next] }
};
auto opposite_vertex = 0;
index_t opposite_vertex{ 0 };
for( const auto vertex : geode::Range{ nb_vertices } )
{
if( vertex == max_length_edge || vertex == next )
Expand All @@ -287,12 +322,12 @@
std::string GenericPolygon< PointType, dimension >::string() const
{
std::string result{ "[" };
auto sep = "";
for( const Point< dimension >& point : vertices_ )
{
absl::StrAppend( &result, sep, point.string() );
sep = ", ";
absl::StrAppend( &result, point.string(), ", " );
}
result.pop_back();
result.pop_back();
result += "]";
return result;
}
Expand Down
13 changes: 10 additions & 3 deletions src/geode/model/helpers/component_mesh_edges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
const std::array< geode::index_t, 2 >& edge_unique_vertices,
const geode::ComponentType& type )
{
if( edge_unique_vertices[0] == edge_unique_vertices[1] )
{
return {};
}
return geode::component_mesh_vertex_pairs(
model.component_mesh_vertices( edge_unique_vertices[0] ),
model.component_mesh_vertices( edge_unique_vertices[1] ), type );
Expand All @@ -62,6 +66,10 @@
const std::array< geode::index_t, 2 >& edge_unique_vertices,
const geode::ComponentID& component )
{
if( edge_unique_vertices[0] == edge_unique_vertices[1] )
{
return {};
}
return geode::component_mesh_vertex_pairs(
model.component_mesh_vertices( edge_unique_vertices[0] ),
model.component_mesh_vertices( edge_unique_vertices[1] ),
Expand Down Expand Up @@ -116,7 +124,7 @@
namespace detail
{
template < typename Model >
geode::ModelComponentMeshEdges::LineEdges line_component_mesh_edges(

Check warning on line 127 in src/geode/model/helpers/component_mesh_edges.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

src/geode/model/helpers/component_mesh_edges.cpp:127:51 [readability-function-cognitive-complexity]

function 'line_component_mesh_edges' has cognitive complexity of 13 (threshold 10)
const Model& model,
const std::array< geode::index_t, 2 >& edge_unique_vertices )
{
Expand Down Expand Up @@ -194,7 +202,7 @@

template < typename Model >
geode::ModelComponentMeshEdges::SurfaceEdges
surface_component_mesh_edges( const Model& model,

Check warning on line 205 in src/geode/model/helpers/component_mesh_edges.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

src/geode/model/helpers/component_mesh_edges.cpp:205:13 [readability-function-cognitive-complexity]

function 'surface_component_mesh_edges' has cognitive complexity of 12 (threshold 10)
const std::array< geode::index_t, 2 >& edge_unique_vertices )
{
if( edge_unique_vertices[0] == NO_ID
Expand Down Expand Up @@ -299,8 +307,7 @@
if( auto edge = mesh.polyhedron_facet_edge_from_vertices(
{ pair[0], pair[1] } ) )
{
edges[block.id()].emplace_back(
std::move( edge.value() ) );
edges[block.id()].emplace_back( edge.value() );
}
}
}
Expand Down Expand Up @@ -331,7 +338,7 @@
if( auto edge = mesh.polyhedron_facet_edge_from_vertices(
{ pair[0], pair[1] } ) )
{
edges.emplace_back( std::move( edge.value() ) );
edges.emplace_back( edge.value() );
}
}
geode::sort_unique( edges );
Expand Down
Loading