Skip to content

Commit b6c89b0

Browse files
committed
changes for cell_attribute_manager
1 parent 3065203 commit b6c89b0

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

bindings/python/src/mesh/core/light_regular_grid.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,39 @@
3030

3131
#include <geode/mesh/core/light_regular_grid.hpp>
3232

33-
#define PYTHON_LIGHT_REGULAR_GRID( dimension ) \
34-
const auto name##dimension = \
35-
"LightRegularGrid" + std::to_string( dimension ) + "D"; \
36-
pybind11::class_< LightRegularGrid##dimension##D, Grid##dimension##D, \
37-
Identifier >( module, name##dimension.c_str() ) \
38-
.def( pybind11::init< Point< dimension >, \
39-
std::array< index_t, dimension >, \
40-
std::array< double, dimension > >() ) \
41-
.def( pybind11::init< Point< dimension >, \
42-
std::array< index_t, dimension >, \
43-
std::array< Vector< dimension >, dimension > >() ) \
44-
.def( "vertex_attribute_manager", \
45-
&Grid##dimension##D::grid_vertex_attribute_manager, \
46-
pybind11::return_value_policy::reference ) \
47-
.def( "native_extension", \
48-
&LightRegularGrid##dimension##D::native_extension )
33+
namespace
34+
{
35+
template < geode::index_t dimension >
36+
void define_python_light_regular_grid( pybind11::module& module )
37+
{
38+
const auto class_name =
39+
absl::StrCat( "LightRegularGrid", dimension, "D" );
40+
pybind11::class_< geode::LightRegularGrid< dimension >,
41+
geode::Grid< dimension >, geode::Identifier >(
42+
module, class_name.c_str() )
43+
.def( pybind11::init< geode::Point< dimension >,
44+
std::array< geode::index_t, dimension >,
45+
std::array< double, dimension > >() )
46+
.def( pybind11::init< geode::Point< dimension >,
47+
std::array< geode::index_t, dimension >,
48+
std::array< geode::Vector< dimension >, dimension > >() )
49+
.def( "vertex_attribute_manager",
50+
&geode::Grid< dimension >::grid_vertex_attribute_manager,
51+
pybind11::return_value_policy::reference )
52+
.def( dimension == 2 ? "polygon_attribute_manager"
53+
: "polyhedron_attribute_manager",
54+
&geode::Grid< dimension >::cell_attribute_manager,
55+
pybind11::return_value_policy::reference )
56+
.def( "native_extension",
57+
&geode::LightRegularGrid< dimension >::native_extension );
58+
}
59+
} // namespace
4960

5061
namespace geode
5162
{
5263
void define_light_regular_grid( pybind11::module& module )
5364
{
54-
PYTHON_LIGHT_REGULAR_GRID( 2 );
55-
PYTHON_LIGHT_REGULAR_GRID( 3 );
65+
define_python_light_regular_grid< 2 >( module );
66+
define_python_light_regular_grid< 3 >( module );
5667
}
5768
} // namespace geode

bindings/python/tests/mesh/test-py-light-regular-grid.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,30 @@ def test_closest_vertex( grid ):
240240
if result != answer:
241241
raise ValueError( "[Test] Wrong result for closest vertex for query p4" )
242242

243-
def test_attribute( grid ):
243+
def test_attribute_3d( grid ):
244244
attribute = grid.cell_attribute_manager().find_or_create_attribute_variable_double( "toto", -1 )
245245
attribute.set_value( 10, 10 )
246+
attribute = grid.polyhedron_attribute_manager().find_attribute_double( "toto" )
247+
if attribute.value( 0 ) != -1:
248+
raise ValueError( "[Test] Wrong attribute value" )
249+
if attribute.value( 10 ) != 10:
250+
raise ValueError( "[Test] Wrong attribute value" )
251+
if attribute.value( grid.nb_cells() - 1 ) != -1:
252+
raise ValueError( "[Test] Wrong attribute value" )
253+
attribute = grid.vertex_attribute_manager().find_or_create_attribute_variable_double( "toto_vertex", 1 )
254+
attribute.set_value( 10, 10 )
255+
if attribute.value( 0 ) != 1:
256+
raise ValueError( "[Test] Wrong attribute value" )
257+
if attribute.value( 10 ) != 10:
258+
raise ValueError( "[Test] Wrong attribute value" )
259+
if attribute.value( grid.nb_cells() - 1 ) != 1:
260+
raise ValueError( "[Test] Wrong attribute value" )
261+
262+
def test_attribute_2d():
263+
grid = mesh.LightRegularGrid2D(geom.Point2D([1.5, 0]), [5, 10], [1., 2.])
264+
attribute = grid.cell_attribute_manager().find_or_create_attribute_variable_double( "toto", -1 )
265+
attribute.set_value( 10, 10 )
266+
attribute = grid.polygon_attribute_manager().find_attribute_double( "toto" )
246267
if attribute.value( 0 ) != -1:
247268
raise ValueError( "[Test] Wrong attribute value" )
248269
if attribute.value( 10 ) != 10:
@@ -274,5 +295,6 @@ def test_io(grid, filename):
274295
test_cell_query( grid )
275296
test_boundary_box( grid )
276297
test_closest_vertex( grid )
277-
test_attribute( grid )
298+
test_attribute_3d( grid )
299+
test_attribute_2d()
278300
test_io(grid, "test." + grid.native_extension())

0 commit comments

Comments
 (0)