Skip to content

Commit

Permalink
Merge pull request #928 from Geode-solutions/feat/gradient_computation
Browse files Browse the repository at this point in the history
feat(compute_gradient): Added a method to compute the gradient of a s…
  • Loading branch information
panquez authored May 28, 2024
2 parents dc050cf + 97bab6a commit 81900f2
Show file tree
Hide file tree
Showing 12 changed files with 568 additions and 1 deletion.
1 change: 1 addition & 0 deletions bindings/python/src/mesh/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ add_geode_python_binding(
"helpers/crs_helper.cpp"
"helpers/euclidean_distance_transform.cpp"
"helpers/geometrical_operations_on_mesh.cpp"
"helpers/gradient_computation.cpp"
"helpers/repair_polygon_orientations.cpp"
"io/edged_curve.cpp"
"io/graph.cpp"
Expand Down
47 changes: 47 additions & 0 deletions bindings/python/src/mesh/helpers/gradient_computation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2019 - 2024 Geode-solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#include "../../common.h"

#include <geode/mesh/core/hybrid_solid.h>
#include <geode/mesh/core/polygonal_surface.h>
#include <geode/mesh/core/polyhedral_solid.h>
#include <geode/mesh/core/regular_grid_solid.h>
#include <geode/mesh/core/regular_grid_surface.h>
#include <geode/mesh/core/tetrahedral_solid.h>
#include <geode/mesh/core/triangulated_surface.h>
#include <geode/mesh/helpers/gradient_computation.h>

namespace geode
{
void define_gradient_computation( pybind11::module& module )
{
module
.def( "compute_surface_scalar_function_gradient2D",
&compute_surface_scalar_function_gradient< 2 > )
.def( "compute_surface_scalar_function_gradient3D",
&compute_surface_scalar_function_gradient< 3 > )
.def( "compute_solid_scalar_function_gradient3D",
&compute_solid_scalar_function_gradient );
}
} // namespace geode
2 changes: 2 additions & 0 deletions bindings/python/src/mesh/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace geode
void define_euclidean_distance_transform( pybind11::module& );
void define_repair_polygon_orientations( pybind11::module& );
void define_geometrical_operations_on_mesh( pybind11::module& );
void define_gradient_computation( pybind11::module& module );
void define_mesh_crs_helper( pybind11::module& );

void define_vertex_set_io( pybind11::module& );
Expand Down Expand Up @@ -159,6 +160,7 @@ PYBIND11_MODULE( opengeode_py_mesh, module )
geode::define_euclidean_distance_transform( module );
geode::define_repair_polygon_orientations( module );
geode::define_geometrical_operations_on_mesh( module );
geode::define_gradient_computation( module );
geode::define_mesh_crs_helper( module );

geode::define_vertex_set_io( module );
Expand Down
7 changes: 7 additions & 0 deletions bindings/python/tests/mesh/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ add_geode_python_test(
${PROJECT_NAME}::py_geometry
${PROJECT_NAME}::py_mesh
)
add_geode_python_test(
SOURCE "test-py-gradient-computation.py"
DEPENDENCIES
${PROJECT_NAME}::py_basic
${PROJECT_NAME}::py_geometry
${PROJECT_NAME}::py_mesh
)

136 changes: 136 additions & 0 deletions bindings/python/tests/mesh/test-py-gradient-computation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2019 - 2024 Geode-solutions
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import sys
import platform
if sys.version_info >= (3, 8, 0) and platform.system() == "Windows":
for path in [x.strip() for x in os.environ['PATH'].split(';') if x]:
os.add_dll_directory(path)

import opengeode_py_basic
import opengeode_py_geometry as geom
import opengeode_py_mesh as mesh

def test_gradient_grid2D():
grid = mesh.RegularGrid2D.create()
builder = mesh.RegularGridBuilder2D.create( grid )
builder.initialize_cartesian_grid( geom.Point2D([ 0, 0 ] ), [ 3, 3 ], 1 )
scalar_function_name = "scalar_function"
attribute = grid.vertex_attribute_manager().find_or_create_attribute_variable_double( scalar_function_name, 0 )
attribute.set_value( 1, 1 )
attribute.set_value( 4, 1 )
attribute.set_value( 6, 1 )
attribute.set_value( 9, 1 )
attribute.set_value( 2, 2 )
attribute.set_value( 3, 3 )
attribute.set_value( 7, 2 )
attribute.set_value( 8, 2 )
attribute.set_value( 10, 2 )
attribute.set_value( 11, 3 )
attribute.set_value( 12, 3 )
attribute.set_value( 13, 2 )
attribute.set_value( 14, 3 )
attribute.set_value( 15, 8 )
gradient_name = mesh.compute_surface_scalar_function_gradient2D( grid, scalar_function_name )
print("Gradient attribute name: ", gradient_name)
mesh.save_regular_grid2D( grid, "grid_with_gradient.og_rgd2d" )

def test_gradient_triangulated_surface2D():
surface = mesh.TriangulatedSurface2D.create()
builder = mesh.TriangulatedSurfaceBuilder2D.create( surface )
builder.create_vertices( 10 )
builder.set_point( 0, geom.Point2D([ 0, 0 ]) )
builder.set_point( 1, geom.Point2D([ 1, 0 ]) )
builder.set_point( 2, geom.Point2D([ 2, 0 ]) )
builder.set_point( 3, geom.Point2D([ 0, 1 ]) )
builder.set_point( 4, geom.Point2D([ 1, 1 ]) )
builder.set_point( 5, geom.Point2D([ 0, 2 ]) )
builder.set_point( 6, geom.Point2D([ 2, 2 ]) )
builder.set_point( 7, geom.Point2D([ 3, 2 ]) )
builder.set_point( 8, geom.Point2D([ 1, 3 ]) )
builder.set_point( 9, geom.Point2D([ 2, 4 ]) )
builder.create_polygon( [ 0, 1, 3 ] )
builder.create_polygon( [ 1, 2, 4 ] )
builder.create_polygon( [ 1, 4, 3 ] )
builder.create_polygon( [ 3, 4, 5 ] )
builder.create_polygon( [ 4, 6, 5 ] )
builder.create_polygon( [ 2, 6, 4 ] )
builder.create_polygon( [ 2, 7, 6 ] )
builder.create_polygon( [ 6, 7, 9 ] )
builder.create_polygon( [ 6, 9, 8 ] )
builder.create_polygon( [ 5, 6, 8 ] )
builder.compute_polygon_adjacencies()
scalar_function_name = "scalar_function"
attribute = surface.vertex_attribute_manager().find_or_create_attribute_variable_double( scalar_function_name, 0 )
attribute.set_value( 1, 1 )
attribute.set_value( 2, 1 )
attribute.set_value( 3, 1 )
attribute.set_value( 5, 1 )
attribute.set_value( 6, 2 )
attribute.set_value( 9, 3 )
attribute.set_value( 7, 2 )
attribute.set_value( 8, 2 )
gradient_name = mesh.compute_surface_scalar_function_gradient2D( surface, scalar_function_name )
print("Gradient attribute name: ", gradient_name)
mesh.save_triangulated_surface2D( surface, "mesh_with_gradient.og_tsf2d" )

def test_gradient_grid3D():
grid = mesh.RegularGrid3D.create()
builder = mesh.RegularGridBuilder3D.create( grid )
builder.initialize_cartesian_grid( geom.Point3D([ 0, 0, 0 ]), [ 2, 2, 2 ], 1 )
scalar_function_name = "scalar_function"
attribute = grid.vertex_attribute_manager().find_or_create_attribute_variable_double( scalar_function_name, 0 )
attribute.set_value( 4, 1 )
attribute.set_value( 10, 1 )
attribute.set_value( 12, 1 )
attribute.set_value( 14, 1 )
attribute.set_value( 16, 1 )
attribute.set_value( 22, 1 )
attribute.set_value( 1, 2 )
attribute.set_value( 3, 2 )
attribute.set_value( 5, 2 )
attribute.set_value( 7, 2 )
attribute.set_value( 9, 2 )
attribute.set_value( 11, 2 )
attribute.set_value( 15, 2 )
attribute.set_value( 17, 2 )
attribute.set_value( 19, 2 )
attribute.set_value( 21, 2 )
attribute.set_value( 23, 2 )
attribute.set_value( 25, 2 )
attribute.set_value( 0, 3 )
attribute.set_value( 2, 3 )
attribute.set_value( 6, 3 )
attribute.set_value( 8, 3 )
attribute.set_value( 18, 3 )
attribute.set_value( 20, 3 )
attribute.set_value( 24, 3 )
attribute.set_value( 26, 3 )
gradient_name = mesh.compute_solid_scalar_function_gradient3D( grid, scalar_function_name )
print("Gradient attribute name: ", gradient_name)
mesh.save_regular_grid3D( grid, "grid_with_gradient.og_rgd3d" )

if __name__ == '__main__':
mesh.OpenGeodeMeshLibrary.initialize()
test_gradient_grid2D()
test_gradient_triangulated_surface2D()
test_gradient_grid3D()
7 changes: 7 additions & 0 deletions include/geode/geometry/detail/bitsery_archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <geode/basic/bitsery_archive.h>

#include <geode/geometry/point.h>
#include <geode/geometry/vector.h>

namespace geode
{
Expand Down Expand Up @@ -76,6 +77,12 @@ namespace geode
context, "Point2D" );
AttributeManager::register_attribute_type< Point3D, Serializer >(
context, "Point3D" );
AttributeManager::register_attribute_type< Vector1D, Serializer >(
context, "Vector1D" );
AttributeManager::register_attribute_type< Vector2D, Serializer >(
context, "Vector2D" );
AttributeManager::register_attribute_type< Vector3D, Serializer >(
context, "Vector3D" );
AttributeManager::register_attribute_type<
absl::InlinedVector< Point1D, 2 >, Serializer >(
context, absl::StrCat( "InlinedVector_Point1D_2" ) );
Expand Down
45 changes: 45 additions & 0 deletions include/geode/mesh/helpers/gradient_computation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2019 - 2024 Geode-solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#pragma once

#include <geode/mesh/common.h>

namespace geode
{
FORWARD_DECLARATION_DIMENSION_CLASS( SurfaceMesh );
FORWARD_DECLARATION_DIMENSION_CLASS( SolidMesh );
ALIAS_2D_AND_3D( SurfaceMesh );
ALIAS_3D( SolidMesh );
} // namespace geode

namespace geode
{
template < index_t dimension >
std::string compute_surface_scalar_function_gradient(
const SurfaceMesh< dimension >& mesh,
absl::string_view scalar_function_name );

std::string opengeode_mesh_api compute_solid_scalar_function_gradient(
const SolidMesh3D& mesh, absl::string_view scalar_function_name );
} // namespace geode
1 change: 0 additions & 1 deletion include/geode/mesh/helpers/hausdorff_distance.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

namespace geode
{
FORWARD_DECLARATION_DIMENSION_CLASS( AABBTree );
FORWARD_DECLARATION_DIMENSION_CLASS( TriangulatedSurface );
ALIAS_3D( TriangulatedSurface );
} // namespace geode
Expand Down
2 changes: 2 additions & 0 deletions src/geode/mesh/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ add_geode_library(
"helpers/convert_solid_mesh.cpp"
"helpers/create_coordinate_system.cpp"
"helpers/euclidean_distance_transform.cpp"
"helpers/gradient_computation.cpp"
"helpers/hausdorff_distance.cpp"
"helpers/rasterize.cpp"
"helpers/ray_tracing.cpp"
Expand Down Expand Up @@ -235,6 +236,7 @@ add_geode_library(
"helpers/convert_solid_mesh.h"
"helpers/create_coordinate_system.h"
"helpers/euclidean_distance_transform.h"
"helpers/gradient_computation.h"
"helpers/generic_solid_accessor.h"
"helpers/generic_surface_accessor.h"
"helpers/generic_edged_curve_accessor.h"
Expand Down
Loading

0 comments on commit 81900f2

Please sign in to comment.