Skip to content

Commit 81900f2

Browse files
authored
Merge pull request #928 from Geode-solutions/feat/gradient_computation
feat(compute_gradient): Added a method to compute the gradient of a s…
2 parents dc050cf + 97bab6a commit 81900f2

File tree

12 files changed

+568
-1
lines changed

12 files changed

+568
-1
lines changed

bindings/python/src/mesh/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ add_geode_python_binding(
6868
"helpers/crs_helper.cpp"
6969
"helpers/euclidean_distance_transform.cpp"
7070
"helpers/geometrical_operations_on_mesh.cpp"
71+
"helpers/gradient_computation.cpp"
7172
"helpers/repair_polygon_orientations.cpp"
7273
"io/edged_curve.cpp"
7374
"io/graph.cpp"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
24+
#include "../../common.h"
25+
26+
#include <geode/mesh/core/hybrid_solid.h>
27+
#include <geode/mesh/core/polygonal_surface.h>
28+
#include <geode/mesh/core/polyhedral_solid.h>
29+
#include <geode/mesh/core/regular_grid_solid.h>
30+
#include <geode/mesh/core/regular_grid_surface.h>
31+
#include <geode/mesh/core/tetrahedral_solid.h>
32+
#include <geode/mesh/core/triangulated_surface.h>
33+
#include <geode/mesh/helpers/gradient_computation.h>
34+
35+
namespace geode
36+
{
37+
void define_gradient_computation( pybind11::module& module )
38+
{
39+
module
40+
.def( "compute_surface_scalar_function_gradient2D",
41+
&compute_surface_scalar_function_gradient< 2 > )
42+
.def( "compute_surface_scalar_function_gradient3D",
43+
&compute_surface_scalar_function_gradient< 3 > )
44+
.def( "compute_solid_scalar_function_gradient3D",
45+
&compute_solid_scalar_function_gradient );
46+
}
47+
} // namespace geode

bindings/python/src/mesh/mesh.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ namespace geode
7878
void define_euclidean_distance_transform( pybind11::module& );
7979
void define_repair_polygon_orientations( pybind11::module& );
8080
void define_geometrical_operations_on_mesh( pybind11::module& );
81+
void define_gradient_computation( pybind11::module& module );
8182
void define_mesh_crs_helper( pybind11::module& );
8283

8384
void define_vertex_set_io( pybind11::module& );
@@ -159,6 +160,7 @@ PYBIND11_MODULE( opengeode_py_mesh, module )
159160
geode::define_euclidean_distance_transform( module );
160161
geode::define_repair_polygon_orientations( module );
161162
geode::define_geometrical_operations_on_mesh( module );
163+
geode::define_gradient_computation( module );
162164
geode::define_mesh_crs_helper( module );
163165

164166
geode::define_vertex_set_io( module );

bindings/python/tests/mesh/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,11 @@ add_geode_python_test(
100100
${PROJECT_NAME}::py_geometry
101101
${PROJECT_NAME}::py_mesh
102102
)
103+
add_geode_python_test(
104+
SOURCE "test-py-gradient-computation.py"
105+
DEPENDENCIES
106+
${PROJECT_NAME}::py_basic
107+
${PROJECT_NAME}::py_geometry
108+
${PROJECT_NAME}::py_mesh
109+
)
103110

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# -*- coding: utf-8 -*-
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+
import os
23+
import sys
24+
import platform
25+
if sys.version_info >= (3, 8, 0) and platform.system() == "Windows":
26+
for path in [x.strip() for x in os.environ['PATH'].split(';') if x]:
27+
os.add_dll_directory(path)
28+
29+
import opengeode_py_basic
30+
import opengeode_py_geometry as geom
31+
import opengeode_py_mesh as mesh
32+
33+
def test_gradient_grid2D():
34+
grid = mesh.RegularGrid2D.create()
35+
builder = mesh.RegularGridBuilder2D.create( grid )
36+
builder.initialize_cartesian_grid( geom.Point2D([ 0, 0 ] ), [ 3, 3 ], 1 )
37+
scalar_function_name = "scalar_function"
38+
attribute = grid.vertex_attribute_manager().find_or_create_attribute_variable_double( scalar_function_name, 0 )
39+
attribute.set_value( 1, 1 )
40+
attribute.set_value( 4, 1 )
41+
attribute.set_value( 6, 1 )
42+
attribute.set_value( 9, 1 )
43+
attribute.set_value( 2, 2 )
44+
attribute.set_value( 3, 3 )
45+
attribute.set_value( 7, 2 )
46+
attribute.set_value( 8, 2 )
47+
attribute.set_value( 10, 2 )
48+
attribute.set_value( 11, 3 )
49+
attribute.set_value( 12, 3 )
50+
attribute.set_value( 13, 2 )
51+
attribute.set_value( 14, 3 )
52+
attribute.set_value( 15, 8 )
53+
gradient_name = mesh.compute_surface_scalar_function_gradient2D( grid, scalar_function_name )
54+
print("Gradient attribute name: ", gradient_name)
55+
mesh.save_regular_grid2D( grid, "grid_with_gradient.og_rgd2d" )
56+
57+
def test_gradient_triangulated_surface2D():
58+
surface = mesh.TriangulatedSurface2D.create()
59+
builder = mesh.TriangulatedSurfaceBuilder2D.create( surface )
60+
builder.create_vertices( 10 )
61+
builder.set_point( 0, geom.Point2D([ 0, 0 ]) )
62+
builder.set_point( 1, geom.Point2D([ 1, 0 ]) )
63+
builder.set_point( 2, geom.Point2D([ 2, 0 ]) )
64+
builder.set_point( 3, geom.Point2D([ 0, 1 ]) )
65+
builder.set_point( 4, geom.Point2D([ 1, 1 ]) )
66+
builder.set_point( 5, geom.Point2D([ 0, 2 ]) )
67+
builder.set_point( 6, geom.Point2D([ 2, 2 ]) )
68+
builder.set_point( 7, geom.Point2D([ 3, 2 ]) )
69+
builder.set_point( 8, geom.Point2D([ 1, 3 ]) )
70+
builder.set_point( 9, geom.Point2D([ 2, 4 ]) )
71+
builder.create_polygon( [ 0, 1, 3 ] )
72+
builder.create_polygon( [ 1, 2, 4 ] )
73+
builder.create_polygon( [ 1, 4, 3 ] )
74+
builder.create_polygon( [ 3, 4, 5 ] )
75+
builder.create_polygon( [ 4, 6, 5 ] )
76+
builder.create_polygon( [ 2, 6, 4 ] )
77+
builder.create_polygon( [ 2, 7, 6 ] )
78+
builder.create_polygon( [ 6, 7, 9 ] )
79+
builder.create_polygon( [ 6, 9, 8 ] )
80+
builder.create_polygon( [ 5, 6, 8 ] )
81+
builder.compute_polygon_adjacencies()
82+
scalar_function_name = "scalar_function"
83+
attribute = surface.vertex_attribute_manager().find_or_create_attribute_variable_double( scalar_function_name, 0 )
84+
attribute.set_value( 1, 1 )
85+
attribute.set_value( 2, 1 )
86+
attribute.set_value( 3, 1 )
87+
attribute.set_value( 5, 1 )
88+
attribute.set_value( 6, 2 )
89+
attribute.set_value( 9, 3 )
90+
attribute.set_value( 7, 2 )
91+
attribute.set_value( 8, 2 )
92+
gradient_name = mesh.compute_surface_scalar_function_gradient2D( surface, scalar_function_name )
93+
print("Gradient attribute name: ", gradient_name)
94+
mesh.save_triangulated_surface2D( surface, "mesh_with_gradient.og_tsf2d" )
95+
96+
def test_gradient_grid3D():
97+
grid = mesh.RegularGrid3D.create()
98+
builder = mesh.RegularGridBuilder3D.create( grid )
99+
builder.initialize_cartesian_grid( geom.Point3D([ 0, 0, 0 ]), [ 2, 2, 2 ], 1 )
100+
scalar_function_name = "scalar_function"
101+
attribute = grid.vertex_attribute_manager().find_or_create_attribute_variable_double( scalar_function_name, 0 )
102+
attribute.set_value( 4, 1 )
103+
attribute.set_value( 10, 1 )
104+
attribute.set_value( 12, 1 )
105+
attribute.set_value( 14, 1 )
106+
attribute.set_value( 16, 1 )
107+
attribute.set_value( 22, 1 )
108+
attribute.set_value( 1, 2 )
109+
attribute.set_value( 3, 2 )
110+
attribute.set_value( 5, 2 )
111+
attribute.set_value( 7, 2 )
112+
attribute.set_value( 9, 2 )
113+
attribute.set_value( 11, 2 )
114+
attribute.set_value( 15, 2 )
115+
attribute.set_value( 17, 2 )
116+
attribute.set_value( 19, 2 )
117+
attribute.set_value( 21, 2 )
118+
attribute.set_value( 23, 2 )
119+
attribute.set_value( 25, 2 )
120+
attribute.set_value( 0, 3 )
121+
attribute.set_value( 2, 3 )
122+
attribute.set_value( 6, 3 )
123+
attribute.set_value( 8, 3 )
124+
attribute.set_value( 18, 3 )
125+
attribute.set_value( 20, 3 )
126+
attribute.set_value( 24, 3 )
127+
attribute.set_value( 26, 3 )
128+
gradient_name = mesh.compute_solid_scalar_function_gradient3D( grid, scalar_function_name )
129+
print("Gradient attribute name: ", gradient_name)
130+
mesh.save_regular_grid3D( grid, "grid_with_gradient.og_rgd3d" )
131+
132+
if __name__ == '__main__':
133+
mesh.OpenGeodeMeshLibrary.initialize()
134+
test_gradient_grid2D()
135+
test_gradient_triangulated_surface2D()
136+
test_gradient_grid3D()

include/geode/geometry/detail/bitsery_archive.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <geode/basic/bitsery_archive.h>
2727

2828
#include <geode/geometry/point.h>
29+
#include <geode/geometry/vector.h>
2930

3031
namespace geode
3132
{
@@ -76,6 +77,12 @@ namespace geode
7677
context, "Point2D" );
7778
AttributeManager::register_attribute_type< Point3D, Serializer >(
7879
context, "Point3D" );
80+
AttributeManager::register_attribute_type< Vector1D, Serializer >(
81+
context, "Vector1D" );
82+
AttributeManager::register_attribute_type< Vector2D, Serializer >(
83+
context, "Vector2D" );
84+
AttributeManager::register_attribute_type< Vector3D, Serializer >(
85+
context, "Vector3D" );
7986
AttributeManager::register_attribute_type<
8087
absl::InlinedVector< Point1D, 2 >, Serializer >(
8188
context, absl::StrCat( "InlinedVector_Point1D_2" ) );
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
24+
#pragma once
25+
26+
#include <geode/mesh/common.h>
27+
28+
namespace geode
29+
{
30+
FORWARD_DECLARATION_DIMENSION_CLASS( SurfaceMesh );
31+
FORWARD_DECLARATION_DIMENSION_CLASS( SolidMesh );
32+
ALIAS_2D_AND_3D( SurfaceMesh );
33+
ALIAS_3D( SolidMesh );
34+
} // namespace geode
35+
36+
namespace geode
37+
{
38+
template < index_t dimension >
39+
std::string compute_surface_scalar_function_gradient(
40+
const SurfaceMesh< dimension >& mesh,
41+
absl::string_view scalar_function_name );
42+
43+
std::string opengeode_mesh_api compute_solid_scalar_function_gradient(
44+
const SolidMesh3D& mesh, absl::string_view scalar_function_name );
45+
} // namespace geode

include/geode/mesh/helpers/hausdorff_distance.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
namespace geode
2929
{
30-
FORWARD_DECLARATION_DIMENSION_CLASS( AABBTree );
3130
FORWARD_DECLARATION_DIMENSION_CLASS( TriangulatedSurface );
3231
ALIAS_3D( TriangulatedSurface );
3332
} // namespace geode

src/geode/mesh/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ add_geode_library(
104104
"helpers/convert_solid_mesh.cpp"
105105
"helpers/create_coordinate_system.cpp"
106106
"helpers/euclidean_distance_transform.cpp"
107+
"helpers/gradient_computation.cpp"
107108
"helpers/hausdorff_distance.cpp"
108109
"helpers/rasterize.cpp"
109110
"helpers/ray_tracing.cpp"
@@ -235,6 +236,7 @@ add_geode_library(
235236
"helpers/convert_solid_mesh.h"
236237
"helpers/create_coordinate_system.h"
237238
"helpers/euclidean_distance_transform.h"
239+
"helpers/gradient_computation.h"
238240
"helpers/generic_solid_accessor.h"
239241
"helpers/generic_surface_accessor.h"
240242
"helpers/generic_edged_curve_accessor.h"

0 commit comments

Comments
 (0)