Skip to content

Commit

Permalink
Merge pull request #112 from Geode-solutions/fix/bindings_horizon_sta…
Browse files Browse the repository at this point in the history
…cks_creation

fix(Python): Fixed bindings with the HorizonsStack.
  • Loading branch information
panquez authored Apr 18, 2024
2 parents 25d5b83 + fa2cc95 commit e7c9af2
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 44 deletions.
2 changes: 1 addition & 1 deletion bindings/python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile bindings/python/requirements.in
# pip-compile --pre bindings/python/requirements.in
#
opengeode-core==14.*,>=14.19.0
# via -r bindings/python/requirements.in
87 changes: 87 additions & 0 deletions bindings/python/src/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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 <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <absl/container/fixed_array.h>
#include <absl/container/inlined_vector.h>
#include <absl/strings/string_view.h>
#include <absl/types/optional.h>
#include <absl/types/span.h>

namespace pybind11
{
namespace detail
{
template < typename Type >
struct type_caster< absl::FixedArray< Type > >
: list_caster< absl::FixedArray< Type >, Type >
{
};

template < typename Type, size_t dimension >
struct type_caster< absl::InlinedVector< Type, dimension > >
: list_caster< absl::InlinedVector< Type, dimension >, Type >
{
};

template < typename Type >
struct type_caster< absl::Span< Type > >
: list_caster< absl::Span< Type >, Type >
{
using value_conv = make_caster< Type >;

bool load( handle src, bool convert )
{
cpp_.clear();
auto s = reinterpret_borrow< sequence >( src );
cpp_.reserve( s.size() );
for( auto it : s )
{
value_conv conv;
if( !conv.load( it, convert ) )
return false;
cpp_.push_back( cast_op< Type&& >( std::move( conv ) ) );
}
this->value = absl::MakeConstSpan( cpp_ );
return true;
}

std::vector< typename std::remove_const< Type >::type > cpp_;
};

template <>
struct type_caster< absl::string_view >
: string_caster< absl::string_view, true >
{
};

template < typename T >
struct type_caster< absl::optional< T > >
: public optional_caster< absl::optional< T > >
{
};
} // namespace detail
} // namespace pybind11
20 changes: 2 additions & 18 deletions bindings/python/src/explicit/explicit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*
*/

#include "../common.h"

#include <pybind11/iostream.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand Down Expand Up @@ -51,24 +53,6 @@
#include "representation/io/cross_section.h"
#include "representation/io/structural_model.h"

namespace pybind11
{
namespace detail
{
template <>
struct type_caster< absl::string_view >
: string_caster< absl::string_view, true >
{
};

template < typename Type >
struct type_caster< absl::FixedArray< Type > >
: list_caster< absl::FixedArray< Type >, Type >
{
};
} // namespace detail
} // namespace pybind11

PYBIND11_MODULE( opengeode_geosciences_py_explicit, module )
{
module.doc() = "OpenGeode-Geosciences Python binding for explicit library";
Expand Down
26 changes: 2 additions & 24 deletions bindings/python/src/implicit/implicit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*
*/

#include "../common.h"

#include <pybind11/iostream.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand All @@ -45,30 +47,6 @@
#include "representation/io/stratigraphic_model.h"
#include "representation/io/stratigraphic_section.h"

namespace pybind11
{
namespace detail
{
template <>
struct type_caster< absl::string_view >
: string_caster< absl::string_view, true >
{
};

template < typename Type, size_t dimension >
struct type_caster< absl::InlinedVector< Type, dimension > >
: list_caster< absl::InlinedVector< Type, dimension >, Type >
{
};

template < typename T >
struct type_caster< absl::optional< T > >
: public optional_caster< absl::optional< T > >
{
};
} // namespace detail
} // namespace pybind11

PYBIND11_MODULE( opengeode_geosciences_py_implicit, module )
{
module.doc() = "OpenGeode-Geosciences Python binding for implicit";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
Identifier >( module, name##dimension.c_str() ) \
.def( pybind11::init<>() ) \
.def( "native_extension", \
&HorizonsStack##dimension##D::native_extension )
&HorizonsStack##dimension##D::native_extension ) \
.def( "top_horizon", &HorizonsStack##dimension##D::top_horizon ) \
.def( "bottom_horizon", &HorizonsStack##dimension##D::bottom_horizon ) \
.def( "is_eroded_by", &HorizonsStack##dimension##D::is_eroded_by ) \
.def( "is_baselap_of", &HorizonsStack##dimension##D::is_baselap_of )

namespace geode
{
Expand Down
2 changes: 2 additions & 0 deletions bindings/python/tests/implicit/test-py-horizons-stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def test_horizons_stack():
geode_imp.save_horizons_stack3D( horizons_stack, stack_path )
reloaded_stack = geode_imp.load_horizons_stack3D( stack_path )

stack_from_name_lists = geode_imp.horizons_stack_from_name_list_3d(["hor1","hor2"],["su1","su2","su3"])

if __name__ == '__main__':
print("Starting test")
geode_imp.GeosciencesImplicitLibrary.initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ namespace geode
/*!
* Creates a HorizonsStack from a list of names of Horizons and
* StratigraphicUnits.
* The bottom horizon and stratigraphic unit will be the first in the
* lists, respectively.
*/
template < index_t dimension >
HorizonsStack< dimension > horizons_stack_from_name_list(
Expand Down

0 comments on commit e7c9af2

Please sign in to comment.