Skip to content

Commit c83d021

Browse files
committed
I would propose these changes
1 parent 5cd66a5 commit c83d021

File tree

3 files changed

+87
-11
lines changed

3 files changed

+87
-11
lines changed

src/geode/mesh/helpers/build_grid.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
namespace geode
2929
{
30-
3130
template < index_t dimension >
3231
LightRegularGrid< dimension >
3332
build_grid_from_bbox_target_length_and_maximum_cell_number(
@@ -41,22 +40,17 @@ namespace geode
4140
{
4241
numerator *= diagonal.value( d );
4342
}
44-
const auto min_length =
45-
std::pow( numerator / ( max_nb_cells - 1 ), 1. / dimension );
46-
auto min_cell_length = min_length;
47-
for( const auto d : LRange{ dimension } )
48-
{
49-
min_cell_length = std::max( ( diagonal.value( d ) + 2 * min_length )
50-
/ diagonal.value( d ) * min_length,
51-
min_cell_length );
52-
}
43+
const auto min_cell_length =
44+
std::pow( numerator / max_nb_cells, 1. / dimension );
5345
const auto cell_length =
5446
std::max( min_cell_length, target_cell_length );
5547
std::array< index_t, dimension > cell_numbers;
5648
std::array< double, dimension > cell_lengths;
5749
for( const auto d : LRange{ dimension } )
5850
{
59-
cell_numbers[d] = std::ceil( diagonal.value( d ) / cell_length );
51+
cell_numbers[d] = std::max( static_cast< index_t >( 1 ),
52+
static_cast< index_t >(
53+
std::floor( diagonal.value( d ) / cell_length ) ) );
6054
cell_lengths[d] = diagonal.value( d ) / cell_numbers[d];
6155
}
6256
return { bbox.min(), std::move( cell_numbers ),

tests/mesh/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,10 @@ add_geode_test(
275275
${PROJECT_NAME}::geometry
276276
${PROJECT_NAME}::mesh
277277
)
278+
add_geode_test(
279+
SOURCE "test-build-grid.cpp"
280+
DEPENDENCIES
281+
${PROJECT_NAME}::basic
282+
${PROJECT_NAME}::geometry
283+
${PROJECT_NAME}::mesh
284+
)

tests/mesh/test-build-grid.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2019 - 2024 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
#include <geode/basic/logger.h>
24+
25+
#include <geode/geometry/bounding_box.h>
26+
#include <geode/geometry/point.h>
27+
28+
#include <geode/mesh/helpers/build_grid.h>
29+
30+
#include <geode/tests/common.h>
31+
32+
void test()
33+
{
34+
geode::OpenGeodeMeshLibrary::initialize();
35+
const geode::index_t max_nb_cells{ 100 };
36+
geode::BoundingBox2D bbox;
37+
bbox.add_point( { { 0, 0 } } );
38+
bbox.add_point( { { 1, 1 } } );
39+
const auto grid =
40+
geode::build_grid_from_bbox_target_length_and_maximum_cell_number(
41+
bbox, 0.01, max_nb_cells );
42+
DEBUG( grid.nb_cells() );
43+
DEBUG( grid.nb_cells_in_direction( 0 ) );
44+
DEBUG( grid.nb_cells_in_direction( 1 ) );
45+
DEBUG( grid.cell_length_in_direction( 0 ) );
46+
DEBUG( grid.cell_length_in_direction( 1 ) );
47+
OPENGEODE_EXCEPTION( grid.nb_cells() <= max_nb_cells,
48+
"[Test] Too much cells in built grid" );
49+
50+
bbox.add_point( { { 1, 10 } } );
51+
const auto grid2 =
52+
geode::build_grid_from_bbox_target_length_and_maximum_cell_number(
53+
bbox, 0.01, max_nb_cells );
54+
DEBUG( grid2.nb_cells() );
55+
DEBUG( grid2.nb_cells_in_direction( 0 ) );
56+
DEBUG( grid2.nb_cells_in_direction( 1 ) );
57+
DEBUG( grid2.cell_length_in_direction( 0 ) );
58+
DEBUG( grid2.cell_length_in_direction( 1 ) );
59+
OPENGEODE_EXCEPTION( grid.nb_cells() <= max_nb_cells,
60+
"[Test] Too much cells in built grid2" );
61+
62+
bbox.add_point( { { 3, 10 } } );
63+
const auto grid3 =
64+
geode::build_grid_from_bbox_target_length_and_maximum_cell_number(
65+
bbox, 0.01, max_nb_cells );
66+
DEBUG( grid3.nb_cells() );
67+
DEBUG( grid3.nb_cells_in_direction( 0 ) );
68+
DEBUG( grid3.nb_cells_in_direction( 1 ) );
69+
DEBUG( grid3.cell_length_in_direction( 0 ) );
70+
DEBUG( grid3.cell_length_in_direction( 1 ) );
71+
OPENGEODE_EXCEPTION( grid.nb_cells() <= max_nb_cells,
72+
"[Test] Too much cells in built grid3" );
73+
}
74+
75+
OPENGEODE_TEST( "graph" )

0 commit comments

Comments
 (0)