Skip to content
Open
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
tests/** linguist-vendored
*.sh text eol=lf
1 change: 1 addition & 0 deletions bindings/python/src/geometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_geode_python_binding(
"intersection_detection.cpp"
"mensuration.cpp"
"nn_search.cpp"
"octree.cpp"
"perpendicular.cpp"
"point.cpp"
"points_sort.cpp"
Expand Down
7 changes: 6 additions & 1 deletion bindings/python/src/geometry/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace geode
void define_intersection_detection( pybind11::module& );
void define_mensuration( pybind11::module& );
void define_nn_search( pybind11::module& );
void define_octree( pybind11::module& );
void define_perpendicular( pybind11::module& );
void define_point( pybind11::module& );
void define_points_sort( pybind11::module& );
Expand All @@ -52,12 +53,15 @@ namespace geode

PYBIND11_MODULE( opengeode_py_geometry, module )
{
// Octree returns the AttributeManager of opengeode_py_basic
geode::import_binding_dependency( module, "opengeode_py_basic" );
pybind11::add_ostream_redirect( module );
module.doc() =
"Geometry of OpenGeode: Point, Vector, Frame, CoordinateSystem, Angle, "
"BoundingBox, basic objects (Segment, Triangle, Tetrahedron, Plane, "
"Sphere, Circle, Cylinder, InfiniteLine, Ray), nearest neighbor "
"search (NNSearch) and the geometric predicates and computations "
"search (NNSearch), adaptive octree (Octree) and the geometric "
"predicates and computations "
"on them (distance, projection, intersection, position, sign, "
"mensuration, quality, barycentric coordinates). Functions and "
"classes depending on the dimension end with 2D or 3D";
Expand All @@ -77,6 +81,7 @@ PYBIND11_MODULE( opengeode_py_geometry, module )
geode::define_intersection_detection( module );
geode::define_mensuration( module );
geode::define_nn_search( module );
geode::define_octree( module );
geode::define_perpendicular( module );
geode::define_points_sort( module );
geode::define_position( module );
Expand Down
257 changes: 257 additions & 0 deletions bindings/python/src/geometry/octree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
/*
* Copyright (c) 2019 - 2026 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.hpp"

#include <geode/basic/attribute_manager.hpp>

#include <geode/geometry/octree.hpp>

namespace
{
template < geode::index_t dimension >
void check_cell( std::string_view context,
const geode::Octree< dimension >& octree,
geode::index_t cell )
{
geode::check_index( context, "cell", cell, octree.nb_cells() );
}

template < geode::index_t dimension >
void define_octree( pybind11::module& module )
{
using Octree = geode::Octree< dimension >;
const auto name = absl::StrCat( "Octree", dimension, "D" );
pybind11::class_< Octree > octree( module, name.c_str(),
"Adaptive octree (quadtree in 2D) covering an axis-aligned box. "
"The box is split into a grid of root cells (along each axis, "
"round( side / smallest side ) cells, at least 1), then each leaf "
"can be refined into NB_CHILDREN children. Cells have stable "
"indices: roots first (axis 0 varying fastest), then children "
"appended by blocks of NB_CHILDREN each time a leaf is refined; "
"there is no coarsening. Each cell has an integer anchor (its "
"minimal corner) on the finest lattice: a cell at level L spans "
"2^(max_depth() - L) lattice units along each axis" );
octree.attr( "NB_CHILDREN" ) = Octree::NB_CHILDREN;
octree.attr( "NB_FACE_CHILDREN" ) = Octree::NB_FACE_CHILDREN;
pybind11::native_enum< typename Octree::FACE_SIDE >( octree, "FaceSide",
"enum.Enum",
"With an axis, one of the 2 * dimension faces of a cell: the face "
"of minimal (lower) or maximal (upper) coordinate along the axis" )
.value( "lower", Octree::FACE_SIDE::lower )
.value( "upper", Octree::FACE_SIDE::upper )
.finalize();
pybind11::native_enum< typename Octree::BALANCE_TYPE >( octree,
"BalanceType", "enum.Enum",
"Adjacency enforced by balance(): faces (leaves sharing a face, "
"an edge in 2D) or full (leaves sharing a face, an edge or a "
"vertex)" )
.value( "faces", Octree::BALANCE_TYPE::faces )
.value( "full", Octree::BALANCE_TYPE::full )
.finalize();
octree.def( pybind11::init<>(), "Create an empty octree (no cell)" )
.def( pybind11::init< const geode::BoundingBox< dimension >& >(),
pybind11::arg( "bbox" ),
"Create an octree covering the box with only root cells. "
"Raise an OpenGeodeDataError if a box side is not larger "
"than GLOBAL_EPSILON or if the box is too elongated (more "
"root cells than the lattice can hold)" )
.def( "max_depth", &Octree::max_depth,
"Return the maximal level a cell can have (roots are at "
"level 0)" )
.def( "nb_cells", &Octree::nb_cells,
"Return the number of cells, roots and refined cells "
"included" )
.def( "nb_root_cells", &Octree::nb_root_cells,
"Return the number of root cells along each axis" )
.def(
"is_leaf",
[]( const Octree& self, geode::index_t cell ) {
check_cell( "is_leaf", self, cell );
return self.is_leaf( cell );
},
pybind11::arg( "cell" ), "Return True if the cell is a leaf" )
.def(
"level",
[]( const Octree& self, geode::index_t cell ) {
check_cell( "level", self, cell );
return self.level( cell );
},
pybind11::arg( "cell" ),
"Return the level of the cell (roots are at level 0)" )
.def(
"parent",
[]( const Octree& self, geode::index_t cell ) {
check_cell( "parent", self, cell );
return self.parent( cell );
},
pybind11::arg( "cell" ),
"Return the parent cell, None for a root cell" )
.def(
"children",
[]( const Octree& self, geode::index_t cell ) {
check_cell( "children", self, cell );
return self.children( cell );
},
pybind11::arg( "cell" ),
"Return the index of the first child, None for a leaf. The "
"NB_CHILDREN children are contiguous; bit d of (child - "
"first child) is set when the child is in the upper half of "
"the cell along axis d" )
.def(
"cell_bounding_box",
[]( const Octree& self, geode::index_t cell ) {
check_cell( "cell_bounding_box", self, cell );
return self.cell_bounding_box( cell );
},
pybind11::arg( "cell" ),
"Return the bounding box of the cell, computed from lattice "
"values only: cells sharing a face have exactly the same face "
"coordinates" )
.def(
"cell_center",
[]( const Octree& self, geode::index_t cell ) {
check_cell( "cell_center", self, cell );
return self.cell_center( cell );
},
pybind11::arg( "cell" ), "Return the center of the cell" )
.def(
"cell_lengths",
[]( const Octree& self, geode::index_t cell ) {
check_cell( "cell_lengths", self, cell );
return self.cell_lengths( cell );
},
pybind11::arg( "cell" ),
"Return the cell length along each axis: root cell length / "
"2^level" )
.def(
"cell_anchor",
[]( const Octree& self, geode::index_t cell ) {
check_cell( "cell_anchor", self, cell );
return self.cell_anchor( cell );
},
pybind11::arg( "cell" ),
"Return the minimal corner of the cell on the finest lattice "
"(a copy)" )
.def( "cell", &Octree::cell, pybind11::arg( "level" ),
pybind11::arg( "anchor" ),
"Return the cell with the given level and anchor, None if it "
"does not exist (not created, level above max_depth(), "
"anchor outside the domain or not aligned on the cell size "
"of this level)" )
.def( "leaves",
pybind11::overload_cast<>( &Octree::leaves, pybind11::const_ ),
"Return all the leaf cells, sorted by cell index" )
.def(
"leaves",
[]( const Octree& self, geode::index_t cell ) {
check_cell( "leaves", self, cell );
return self.leaves( cell );
},
pybind11::arg( "cell" ),
"Return the leaf cells of the subtree of the cell (the cell "
"itself if it is a leaf), in depth-first order, children in "
"index order" )
.def( "domain_bounding_box", &Octree::domain_bounding_box,
"Return the domain covered by the root cells: exactly the "
"bounding box given at construction" )
.def( "leaf_containing", &Octree::leaf_containing,
pybind11::arg( "point" ),
"Return the leaf containing the point, None if the point is "
"outside domain_bounding_box() (no tolerance). Cells are "
"closed: on a face shared by several leaves, the leaf on the "
"upper side is returned, except on the domain upper border" )
.def(
"traverse",
[]( const Octree& self, const pybind11::function& filter,
const pybind11::function& action ) {
self.traverse(
[&filter]( geode::index_t cell,
const geode::BoundingBox< dimension >& box ) {
return filter( cell, box ).template cast< bool >();
},
[&action]( geode::index_t leaf ) {
action( leaf );
} );
},
pybind11::arg( "filter" ), pybind11::arg( "action" ),
"Depth-first traversal from the roots, in index order. "
"filter(cell, box) is called on each visited cell with its "
"bounding box: if it returns False, the cell and its subtree "
"are skipped. action(leaf) is called on each leaf accepted by "
"the filter" )
.def(
"refine",
[]( Octree& self, geode::index_t leaf ) {
check_cell( "refine", self, leaf );
return self.refine( leaf );
},
pybind11::arg( "leaf" ),
"Split a leaf into NB_CHILDREN children appended at the end "
"of the cells, copying all its cell attribute values to them, "
"and return the index of the first child. Raise an "
"OpenGeodeDataError if the cell is not a leaf or is at "
"max_depth()" )
.def(
"face_neighbor_leaves",
[]( const Octree& self, geode::index_t cell,
geode::local_index_t axis,
typename Octree::FACE_SIDE side ) {
check_cell( "face_neighbor_leaves", self, cell );
geode::check_direction(
"face_neighbor_leaves", axis, dimension );
const auto neighbors =
self.face_neighbor_leaves( cell, axis, side );
return std::vector< geode::index_t >(
neighbors.begin(), neighbors.end() );
},
pybind11::arg( "cell" ), pybind11::arg( "axis" ),
pybind11::arg( "side" ),
"Return the leaves outside the cell (a leaf or not) sharing a "
"part of its face given by axis and side: the same-size leaf "
"on the other side, else the coarser leaf containing it, else "
"all the finer leaves touching the face (depth-first). Empty "
"on the domain border" )
.def( "balance", &Octree::balance,
pybind11::call_guard< pybind11::gil_scoped_release >(),
pybind11::arg( "type" ),
"Refine leaves until any two adjacent leaves (see "
"BalanceType) differ by at most one level. Existing indices "
"are unchanged, new cells inherit attribute values as in "
"refine(); the result is deterministic" )
.def( "cell_attribute_manager", &Octree::cell_attribute_manager,
pybind11::return_value_policy::reference_internal,
"Return the manager of the attributes on all the cells "
"(refined cells included); refine() copies the values of a "
"leaf to its children" );
}
} // namespace

namespace geode
{
void define_octree( pybind11::module& module )
{
::define_octree< 2 >( module );
::define_octree< 3 >( module );
}
} // namespace geode
15 changes: 14 additions & 1 deletion bindings/python/src/mesh/helpers/convert_solid_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include "../../common.hpp"

#include <geode/geometry/octree.hpp>

#include <geode/mesh/core/hybrid_solid.hpp>
#include <geode/mesh/core/light_regular_grid.hpp>
#include <geode/mesh/core/regular_grid_solid.hpp>
#include <geode/mesh/core/tetrahedral_solid.hpp>
Expand All @@ -43,6 +46,16 @@ namespace geode
&convert_grid_into_tetrahedral_solid, pybind11::arg( "grid" ),
pybind11::call_guard< pybind11::gil_scoped_release >(),
"Return a tetrahedral solid obtained by splitting every cell "
"of the 3D grid into tetrahedra" );
"of the 3D grid into tetrahedra" )
.def( "convert_octree_into_hybrid_solid",
&convert_octree_into_hybrid_solid, pybind11::arg( "octree" ),
pybind11::call_guard< pybind11::gil_scoped_release >(),
"Return a hybrid solid with one hexahedron per leaf of the 3D "
"octree (in the order of Octree3D.leaves()), vertices shared "
"between leaves at the same lattice position, and the cell "
"attributes of the leaves transferred to the polyhedra. The "
"mesh is non-conforming at level jumps (no hanging vertex in "
"the coarser cells, missing adjacencies): it is meant for "
"visualization and export" );
}
} // namespace geode
13 changes: 13 additions & 0 deletions bindings/python/src/mesh/helpers/convert_surface_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "../../common.hpp"

#include <geode/geometry/octree.hpp>

#include <geode/mesh/core/light_regular_grid.hpp>
#include <geode/mesh/core/polygonal_surface.hpp>
#include <geode/mesh/core/regular_grid_surface.hpp>
Expand Down Expand Up @@ -52,6 +54,17 @@ namespace geode
pybind11::call_guard< pybind11::gil_scoped_release >(),
"Return a triangulated surface obtained by splitting every "
"cell of the 2D grid into triangles" )
.def( "convert_octree_into_polygonal_surface",
&convert_octree_into_polygonal_surface,
pybind11::arg( "octree" ),
pybind11::call_guard< pybind11::gil_scoped_release >(),
"Return a polygonal surface with one quadrangle per leaf of "
"the 2D octree (in the order of Octree2D.leaves()), vertices "
"shared between leaves at the same lattice position, and the "
"cell attributes of the leaves transferred to the polygons. "
"The mesh is non-conforming at level jumps (no hanging vertex "
"in the coarser cells, missing adjacencies): it is meant for "
"visualization and export" )
.def( "triangulate_surface_mesh2D",
static_cast< void ( * )( SurfaceMesh< 2 >& ) >(
&triangulate_surface_mesh< 2 > ),
Expand Down
6 changes: 6 additions & 0 deletions bindings/python/tests/geometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ add_geode_python_test(
DEPENDENCIES
${PROJECT_NAME}::py_geometry
)
add_geode_python_test(
SOURCE "test-py-octree.py"
DEPENDENCIES
${PROJECT_NAME}::py_geometry
${PROJECT_NAME}::py_mesh
)
add_geode_python_test(
SOURCE "test-py-perpendicular.py"
DEPENDENCIES
Expand Down
Loading
Loading