Skip to content

Commit 78a77d9

Browse files
Merge pull request #1086 from Geode-solutions/fix/give_access_to_vertex_attribute_manager_in_light_regular_grid_python
fix(LightRegularGrid): Give access in python to the attribute manager…
2 parents 0cfb3d1 + b6c89b0 commit 78a77d9

File tree

3 files changed

+66
-21
lines changed

3 files changed

+66
-21
lines changed

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +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( "native_extension", \
45-
&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
4660

4761
namespace geode
4862
{
4963
void define_light_regular_grid( pybind11::module& module )
5064
{
51-
PYTHON_LIGHT_REGULAR_GRID( 2 );
52-
PYTHON_LIGHT_REGULAR_GRID( 3 );
65+
define_python_light_regular_grid< 2 >( module );
66+
define_python_light_regular_grid< 3 >( module );
5367
}
5468
} // namespace geode

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,44 @@ 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" )
246247
if attribute.value( 0 ) != -1:
247248
raise ValueError( "[Test] Wrong attribute value" )
248249
if attribute.value( 10 ) != 10:
249250
raise ValueError( "[Test] Wrong attribute value" )
250251
if attribute.value( grid.nb_cells() - 1 ) != -1:
251252
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" )
267+
if attribute.value( 0 ) != -1:
268+
raise ValueError( "[Test] Wrong attribute value" )
269+
if attribute.value( 10 ) != 10:
270+
raise ValueError( "[Test] Wrong attribute value" )
271+
if attribute.value( grid.nb_cells() - 1 ) != -1:
272+
raise ValueError( "[Test] Wrong attribute value" )
273+
attribute = grid.vertex_attribute_manager().find_or_create_attribute_variable_double( "toto_vertex", 1 )
274+
attribute.set_value( 10, 10 )
275+
if attribute.value( 0 ) != 1:
276+
raise ValueError( "[Test] Wrong attribute value" )
277+
if attribute.value( 10 ) != 10:
278+
raise ValueError( "[Test] Wrong attribute value" )
279+
if attribute.value( grid.nb_cells() - 1 ) != 1:
280+
raise ValueError( "[Test] Wrong attribute value" )
252281

253282
def test_io(grid, filename):
254283
mesh.save_light_regular_grid3D(grid, filename)
@@ -266,5 +295,6 @@ def test_io(grid, filename):
266295
test_cell_query( grid )
267296
test_boundary_box( grid )
268297
test_closest_vertex( grid )
269-
test_attribute( grid )
298+
test_attribute_3d( grid )
299+
test_attribute_2d()
270300
test_io(grid, "test." + grid.native_extension())

include/geode/basic/attribute_manager.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ namespace geode
7979
std::dynamic_pointer_cast< ReadOnlyAttribute< T > >(
8080
find_attribute_base( name ) );
8181
OPENGEODE_EXCEPTION( attribute.get(),
82-
"[AttributeManager::find_attribute] You have to create an "
83-
"attribute before using it. "
84-
"See find_or_create_attribute method and "
85-
"derived classes of ReadOnlyAttribute." );
82+
"[AttributeManager::find_attribute] Could not find attribute '",
83+
name,
84+
"'. You have to create an attribute before using it. See "
85+
"find_or_create_attribute method and derived classes of "
86+
"ReadOnlyAttribute." );
8687
return attribute;
8788
}
8889

0 commit comments

Comments
 (0)