From b6e1107f7877c31c1912afeb390d733af0736bfc Mon Sep 17 00:00:00 2001 From: panquez Date: Tue, 13 Sep 2022 12:19:10 +0200 Subject: [PATCH 1/4] fix(Rasterize): implements conservative rasterize segment in 3D --- src/geode/mesh/helpers/rasterize.cpp | 76 +++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 7 deletions(-) diff --git a/src/geode/mesh/helpers/rasterize.cpp b/src/geode/mesh/helpers/rasterize.cpp index 188d7459f..0c190b615 100644 --- a/src/geode/mesh/helpers/rasterize.cpp +++ b/src/geode/mesh/helpers/rasterize.cpp @@ -23,9 +23,12 @@ #include +#include + #include #include +#include #include #include @@ -440,17 +443,76 @@ namespace return cells; } + absl::InlinedVector< geode::GridCellIndices3D, 6 > neighbors( + const geode::RegularGrid3D& grid, const geode::GridCellIndices3D& cell ) + { + absl::InlinedVector< geode::GridCellIndices3D, 6 > neighbors; + for( const auto d : geode::LRange{ 3 } ) + { + if( const auto prev = grid.previous_cell( cell, d ) ) + { + neighbors.push_back( prev.value() ); + } + if( const auto next = grid.next_cell( cell, d ) ) + { + neighbors.push_back( next.value() ); + } + } + return neighbors; + } + std::vector< geode::GridCellIndices3D > conservative_voxelization_segment( const geode::RegularGrid3D& grid, const geode::Segment3D& segment, - const std::array< geode::GridCellsAroundVertex3D, 2 > vertex_cells ) + const std::array< geode::GridCellsAroundVertex3D, 2 > /*unused*/ ) { - std::vector< geode::GridCellIndices3D > cells; - geode_unused( grid ); - geode_unused( segment ); - geode_unused( vertex_cells ); - OPENGEODE_EXCEPTION( false, - "[conservative_voxelization_segment] Not implement yet for 3D" ); + constexpr auto cell_tested_attribute_name = "tested_cell_rasterize"; + auto cells = geode::rasterize_segment( grid, segment ); + auto tested_att = + grid.cell_attribute_manager() + .find_or_create_attribute< geode::VariableAttribute, bool >( + cell_tested_attribute_name, false ); + std::queue< geode::GridCellIndices3D > to_test; + for( const auto& cell : cells ) + { + tested_att->set_value( grid.cell_index( cell ), true ); + for( auto&& neighbor : neighbors( grid, cell ) ) + { + to_test.emplace( neighbor ); + } + } + const auto half_cell_size = + std::sqrt( grid.cell_length_in_direction( 0 ) + * grid.cell_length_in_direction( 0 ) + + grid.cell_length_in_direction( 1 ) + * grid.cell_length_in_direction( 1 ) + + grid.cell_length_in_direction( 2 ) + * grid.cell_length_in_direction( 2 ) ) + / 2.; + while( !to_test.empty() ) + { + const auto cell = to_test.front(); + to_test.pop(); + const auto cell_id = grid.cell_index( cell ); + if( tested_att->value( cell_id ) ) + { + continue; + } + tested_att->set_value( cell_id, true ); + const auto center = grid.polyhedron_barycenter( cell_id ); + if( std::get< 0 >( + geode::point_segment_distance( center, segment ) ) + <= half_cell_size ) + { + cells.push_back( cell ); + for( auto&& neighbor : neighbors( grid, cell ) ) + { + to_test.emplace( neighbor ); + } + } + } + grid.cell_attribute_manager().delete_attribute( + cell_tested_attribute_name ); return cells; } From 8b8735824be8f5832cac162617e3f1d395b9938a Mon Sep 17 00:00:00 2001 From: panquez Date: Tue, 13 Sep 2022 13:51:32 +0200 Subject: [PATCH 2/4] PR comments --- src/geode/mesh/helpers/rasterize.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/geode/mesh/helpers/rasterize.cpp b/src/geode/mesh/helpers/rasterize.cpp index 0c190b615..492577a74 100644 --- a/src/geode/mesh/helpers/rasterize.cpp +++ b/src/geode/mesh/helpers/rasterize.cpp @@ -466,16 +466,12 @@ namespace const geode::Segment3D& segment, const std::array< geode::GridCellsAroundVertex3D, 2 > /*unused*/ ) { - constexpr auto cell_tested_attribute_name = "tested_cell_rasterize"; auto cells = geode::rasterize_segment( grid, segment ); - auto tested_att = - grid.cell_attribute_manager() - .find_or_create_attribute< geode::VariableAttribute, bool >( - cell_tested_attribute_name, false ); + std::vector< geode::index_t > tested_cells( grid.nb_cells(), false ); std::queue< geode::GridCellIndices3D > to_test; for( const auto& cell : cells ) { - tested_att->set_value( grid.cell_index( cell ), true ); + tested_cells[grid.cell_index( cell )] = true; for( auto&& neighbor : neighbors( grid, cell ) ) { to_test.emplace( neighbor ); @@ -494,25 +490,23 @@ namespace const auto cell = to_test.front(); to_test.pop(); const auto cell_id = grid.cell_index( cell ); - if( tested_att->value( cell_id ) ) + if( tested_cells[cell_id] ) { continue; } - tested_att->set_value( cell_id, true ); + tested_cells[cell_id] = true; const auto center = grid.polyhedron_barycenter( cell_id ); if( std::get< 0 >( geode::point_segment_distance( center, segment ) ) <= half_cell_size ) { - cells.push_back( cell ); for( auto&& neighbor : neighbors( grid, cell ) ) { - to_test.emplace( neighbor ); + to_test.emplace( std::move( neighbor ) ); } + cells.push_back( std::move( cell ) ); } } - grid.cell_attribute_manager().delete_attribute( - cell_tested_attribute_name ); return cells; } From 0f8c3eecb0e6bcfc203ae10470852392b96c0788 Mon Sep 17 00:00:00 2001 From: Pierre Anquez <32702237+panquez@users.noreply.github.com> Date: Tue, 13 Sep 2022 14:17:46 +0200 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Arnaud Botella --- src/geode/mesh/helpers/rasterize.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/geode/mesh/helpers/rasterize.cpp b/src/geode/mesh/helpers/rasterize.cpp index 492577a74..68d6167b2 100644 --- a/src/geode/mesh/helpers/rasterize.cpp +++ b/src/geode/mesh/helpers/rasterize.cpp @@ -467,14 +467,14 @@ namespace const std::array< geode::GridCellsAroundVertex3D, 2 > /*unused*/ ) { auto cells = geode::rasterize_segment( grid, segment ); - std::vector< geode::index_t > tested_cells( grid.nb_cells(), false ); + std::vector< bool > tested_cells( grid.nb_cells(), false ); std::queue< geode::GridCellIndices3D > to_test; for( const auto& cell : cells ) { tested_cells[grid.cell_index( cell )] = true; for( auto&& neighbor : neighbors( grid, cell ) ) { - to_test.emplace( neighbor ); + to_test.emplace( std::move( neighbor ) ); } } const auto half_cell_size = From 29ed966b4cb4b581a52e8e887259d5cb51c54b75 Mon Sep 17 00:00:00 2001 From: Pierre Anquez <32702237+panquez@users.noreply.github.com> Date: Tue, 13 Sep 2022 14:17:59 +0200 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Arnaud Botella --- src/geode/mesh/helpers/rasterize.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/geode/mesh/helpers/rasterize.cpp b/src/geode/mesh/helpers/rasterize.cpp index 68d6167b2..aaa1611e6 100644 --- a/src/geode/mesh/helpers/rasterize.cpp +++ b/src/geode/mesh/helpers/rasterize.cpp @@ -504,7 +504,7 @@ namespace { to_test.emplace( std::move( neighbor ) ); } - cells.push_back( std::move( cell ) ); + cells.emplace_back( std::move( cell ) ); } } return cells;