Skip to content

Commit 596ddea

Browse files
committed
don't return different types; wrappers dont return nb types
1 parent e4914ab commit 596ddea

39 files changed

+642
-492
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.16.0)
22
project(pyigl)
33

4-
# use C++20
4+
# use C++20 (we need at least C++17 for return value optimization)
55
set(CMAKE_CXX_STANDARD 20)
66

77
if (CMAKE_VERSION VERSION_LESS 3.18)
@@ -32,7 +32,7 @@ FetchContent_MakeAvailable(nanobind)
3232
FetchContent_Declare(
3333
libigl
3434
GIT_REPOSITORY https://github.com/libigl/libigl.git
35-
GIT_TAG 0e504e1b331e6696b485258a9aac779f0f20c5b0
35+
GIT_TAG 1410ad43dff78bfaf8d8103de422fa64adfa35d5
3636
)
3737
FetchContent_MakeAvailable(libigl)
3838

missing.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
already=$(ls src/*.cpp | sed -e "s/src\/\(.*\).cpp$/\1/g")
44
skip="igl_inline
55
EPS
6+
sparse
7+
setdiff
8+
rotation_matrix_from_directions
69
verbose
710
colon
811
min_quad_with_fixed.impl

src/AABB.cpp

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace pyigl
2020
tree.init(V,Ele);
2121
}
2222
template <typename AABB>
23-
nb::object find(
23+
auto find(
2424
AABB &tree,
2525
const nb::DRef<const Eigen::MatrixXN> &V,
2626
const nb::DRef<const Eigen::MatrixXI> &Ele,
@@ -33,80 +33,61 @@ namespace pyigl
3333
}
3434
std::vector<int> vec_int = tree.find(V,Ele,q,first);
3535
std::vector<Integer> vec(vec_int.begin(),vec_int.end());
36-
if(first)
37-
{
38-
return vec.size() ? nb::cast(vec[0]) : nb::none();
39-
}else
40-
{
41-
return nb::cast(std::move(vec));
42-
}
36+
return vec;
4337
}
4438

4539
template <typename AABB>
46-
nb::object squared_distance(
40+
auto squared_distance(
4741
AABB &tree,
4842
const nb::DRef<const Eigen::MatrixXN> &V,
4943
const nb::DRef<const Eigen::MatrixXI> &Ele,
50-
const nb::DRef<const Eigen::MatrixXN> &P,
51-
const bool return_I,
52-
const bool return_C)
44+
const nb::DRef<const Eigen::MatrixXN> &P)
5345
{
5446
Eigen::VectorXN sqrD;
5547
Eigen::VectorXI I;
5648
Eigen::MatrixXN C;
5749
tree.squared_distance(V,Ele,P,sqrD,I,C);
58-
if(return_I && return_C)
59-
{
60-
return nb::make_tuple(sqrD,I,C);
61-
}else if(return_I)
62-
{
63-
return nb::make_tuple(sqrD,I);
64-
}else if(return_C)
65-
{
66-
return nb::make_tuple(sqrD,C);
67-
}
68-
return nb::cast(std::move(sqrD));
50+
return std::make_tuple(sqrD,I,C);
6951
}
7052

7153
template <typename AABB>
72-
nb::object intersect_ray(
54+
auto intersect_ray_first(
7355
AABB &tree,
7456
const nb::DRef<const Eigen::MatrixXN> &V,
7557
const nb::DRef<const Eigen::MatrixXI> &Ele,
7658
const nb::DRef<const Eigen::MatrixXN> &orig,
7759
const nb::DRef<const Eigen::MatrixXN> &dir,
78-
const Numeric min_t,
79-
const bool first)
60+
const Numeric min_t)
8061
{
81-
if(first)
82-
{
62+
Eigen::VectorXI I;
63+
Eigen::VectorXN T;
64+
Eigen::MatrixXN UV;
65+
tree.intersect_ray(V,Ele,orig,dir,min_t,I,T,UV);
66+
return std::make_tuple(I,T,UV);
67+
}
8368

84-
Eigen::VectorXI I;
85-
Eigen::VectorXN T;
86-
Eigen::MatrixXN UV;
87-
tree.intersect_ray(V,Ele,orig,dir,min_t,I,T,UV);
88-
return nb::make_tuple(I,T,UV);
89-
}else
69+
template <typename AABB>
70+
auto intersect_ray(
71+
AABB &tree,
72+
const nb::DRef<const Eigen::MatrixXN> &V,
73+
const nb::DRef<const Eigen::MatrixXI> &Ele,
74+
const nb::DRef<const Eigen::MatrixXN> &orig,
75+
const nb::DRef<const Eigen::MatrixXN> &dir)
76+
{
77+
std::vector<std::vector<igl::Hit<Numeric>>> hits;
78+
tree.intersect_ray(V,Ele,orig,dir,hits);
79+
std::vector<std::vector<std::tuple<Integer,Numeric,Numeric,Numeric>>> out;
80+
for(const auto &hit : hits)
9081
{
91-
if(std::isfinite(min_t))
92-
{
93-
throw std::runtime_error("intersect_ray: min_t only supported for first=true");
94-
}
95-
std::vector<std::vector<igl::Hit<Numeric>>> hits;
96-
tree.intersect_ray(V,Ele,orig,dir,hits);
97-
std::vector<std::vector<std::tuple<Integer,Numeric,Numeric,Numeric>>> out;
98-
for(const auto &hit : hits)
82+
std::vector<std::tuple<Integer,Numeric,Numeric,Numeric>> hit_out;
83+
for(const auto &h : hit)
9984
{
100-
std::vector<std::tuple<Integer,Numeric,Numeric,Numeric>> hit_out;
101-
for(const auto &h : hit)
102-
{
103-
hit_out.push_back(std::make_tuple(h.id,h.t,h.u,h.v));
104-
}
105-
out.push_back(hit_out);
85+
hit_out.push_back(std::make_tuple(h.id,h.t,h.u,h.v));
10686
}
107-
108-
return nb::cast(std::move(out));
87+
out.push_back(hit_out);
10988
}
89+
90+
return out;
11091
}
11192
}
11293

@@ -118,8 +99,9 @@ void bind_AABB(nb::module_ &m)
11899
.def(nb::init<>())
119100
.def("init", &pyigl::init<AABBN3>, "V"_a, "Ele"_a)
120101
.def("find", &pyigl::find<AABBN3>, "V"_a, "Ele"_a, "q"_a, "first"_a=false)
121-
.def("squared_distance", &pyigl::squared_distance<AABBN3>, "V"_a, "Ele"_a, "P"_a, "return_I"_a=false,"return_C"_a=false)
122-
.def("intersect_ray",&pyigl::intersect_ray<AABBN3>, "V"_a, "Ele"_a, "orig"_a, "dir"_a, "min_t"_a=std::numeric_limits<Numeric>::infinity(), "first"_a=false)
102+
.def("squared_distance", &pyigl::squared_distance<AABBN3>, "V"_a, "Ele"_a, "P"_a)
103+
.def("intersect_ray_first",&pyigl::intersect_ray_first<AABBN3>, "V"_a, "Ele"_a, "orig"_a, "dir"_a, "min_t"_a=std::numeric_limits<Numeric>::infinity())
104+
.def("intersect_ray",&pyigl::intersect_ray<AABBN3>, "V"_a, "Ele"_a, "orig"_a, "dir"_a)
123105
;
124106
}
125107

src/average_from_edges_onto_vertices.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ using namespace nb::literals;
99

1010
namespace pyigl
1111
{
12-
nb::object average_from_edges_onto_vertices(
12+
auto average_from_edges_onto_vertices(
1313
const nb::DRef<const Eigen::MatrixXI> &F,
1414
const nb::DRef<const Eigen::MatrixXI> &E,
1515
const nb::DRef<const Eigen::MatrixXI> &oE,
1616
const nb::DRef<const Eigen::VectorXN> &uE)
1717
{
1818
Eigen::VectorXN uV;
1919
igl::average_from_edges_onto_vertices(F, E, oE, uE, uV);
20-
return nb::cast(std::move(uV));
20+
return uV;
2121
}
2222
}
2323

src/barycenter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ using namespace nb::literals;
1010
namespace pyigl
1111
{
1212
// Wrapper for barycenter function
13-
nb::object barycenter(
13+
auto barycenter(
1414
const nb::DRef<const Eigen::MatrixXN> &V,
1515
const nb::DRef<const Eigen::MatrixXI> &F)
1616
{
1717
Eigen::MatrixXN BC;
1818
igl::barycenter(V, F, BC);
19-
return nb::cast(std::move(BC));
19+
return BC;
2020
}
2121
}
2222

src/barycentric_coordinates.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ using namespace nb::literals;
1111
namespace pyigl
1212
{
1313
// Wrapper for barycentric_coordinates function
14-
nb::object barycentric_coordinates_PABC(
14+
auto barycentric_coordinates_PABC(
1515
const nb::DRef<const Eigen::MatrixXN> &P,
1616
const nb::DRef<const Eigen::MatrixXN> &A,
1717
const nb::DRef<const Eigen::MatrixXN> &B,
1818
const nb::DRef<const Eigen::MatrixXN> &C)
1919
{
2020
Eigen::MatrixXN L;
2121
igl::barycentric_coordinates(P, A, B, C, L);
22-
return nb::cast(std::move(L));
22+
return L;
2323
}
24-
nb::object barycentric_coordinates_PABCD(
24+
auto barycentric_coordinates_PABCD(
2525
const nb::DRef<const Eigen::MatrixXN> &P,
2626
const nb::DRef<const Eigen::MatrixXN> &A,
2727
const nb::DRef<const Eigen::MatrixXN> &B,
@@ -30,7 +30,7 @@ namespace pyigl
3030
{
3131
Eigen::MatrixXN L;
3232
igl::barycentric_coordinates(P, A, B, C, D, L);
33-
return nb::cast(std::move(L));
33+
return L;
3434
}
3535
}
3636

src/bbw.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using namespace nb::literals;
1111
namespace pyigl
1212
{
1313
// Wrapper for bbw function
14-
nb::object bbw(
14+
auto bbw(
1515
const nb::DRef<const Eigen::MatrixXN> &V,
1616
const nb::DRef<const Eigen::MatrixXI> &Ele,
1717
const nb::DRef<const Eigen::VectorXI> &b,
@@ -38,7 +38,7 @@ namespace pyigl
3838
{
3939
throw std::runtime_error("bbw: failed to compute weights");
4040
}
41-
return nb::cast(std::move(W));
41+
return W;
4242
}
4343
}
4444

src/boundary_facets.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,20 @@
44
#include <nanobind/ndarray.h>
55
#include <nanobind/eigen/dense.h>
66
#include <nanobind/eigen/sparse.h>
7+
#include <nanobind/stl/tuple.h>
78

89
namespace nb = nanobind;
910
using namespace nb::literals;
1011

1112
namespace pyigl
1213
{
13-
nb::object boundary_facets(
14-
const nb::DRef<const Eigen::MatrixXI> &T,
15-
bool return_J,
16-
bool return_K)
14+
auto boundary_facets( const nb::DRef<const Eigen::MatrixXI> &T)
1715
{
1816
Eigen::MatrixXI F;
1917
Eigen::VectorXI J;
2018
Eigen::VectorXI K;
2119
igl::boundary_facets(T,F,J,K);
22-
if(return_J && return_K)
23-
{
24-
return nb::make_tuple(F,J,K);
25-
}else if(return_J)
26-
{
27-
return nb::make_tuple(F,J);
28-
}else if(return_K)
29-
{
30-
return nb::make_tuple(F,K);
31-
}
32-
return nb::cast(std::move(F));
20+
return std::make_tuple(F,J,K);
3321
}
3422
}
3523

@@ -40,8 +28,6 @@ void bind_boundary_facets(nb::module_ &m)
4028
"boundary_facets",
4129
&pyigl::boundary_facets,
4230
"T"_a,
43-
"return_J"_a=false,
44-
"return_K"_a=false,
4531
R"(Determine boundary faces (edges) of tetrahedra (triangles) stored in T
4632
(analogous to qptoolbox's `outline` and `boundary_faces`).
4733

src/circulation.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "default_types.h"
2+
#include <igl/circulation.h>
3+
#include <nanobind/nanobind.h>
4+
#include <nanobind/ndarray.h>
5+
#include <nanobind/eigen/dense.h>
6+
#include <nanobind/stl/vector.h>
7+
#include <nanobind/stl/tuple.h>
8+
9+
namespace nb = nanobind;
10+
using namespace nb::literals;
11+
12+
namespace pyigl
13+
{
14+
auto circulation(
15+
const int e,
16+
const bool ccw,
17+
const nb::DRef<const Eigen::MatrixXI> &F,
18+
const nb::DRef<const Eigen::VectorXI> &EMAP,
19+
const nb::DRef<const Eigen::MatrixXI> &EF,
20+
const nb::DRef<const Eigen::MatrixXI> &EI)
21+
{
22+
std::vector<int> Nv, Nf;
23+
igl::circulation(e, ccw, F, EMAP, EF, EI, Nv, Nf);
24+
return std::make_tuple(Nv, Nf);
25+
}
26+
}
27+
28+
// Bind the wrapper to the Python module
29+
void bind_circulation(nb::module_ &m)
30+
{
31+
m.def(
32+
"circulation",
33+
&pyigl::circulation,
34+
"e"_a,
35+
"ccw"_a,
36+
"F"_a,
37+
"EMAP"_a,
38+
"EF"_a,
39+
"EI"_a,
40+
R"(Return lists of "next" vertex indices (Nv) and face indices (Nf) for circulation.
41+
42+
@param[in] e index of edge to circulate
43+
@param[in] ccw circulate in ccw direction
44+
@param[in] F #F by 3 list of mesh faces
45+
@param[in] EMAP #F*3 list of indices mapping each directed edge to a unique edge in E
46+
@param[in] EF #E by 2 list of edge flaps
47+
@param[in] EI #E by 2 list of edge flap corners
48+
@return Tuple containing Nv (next vertex indices) and Nf (face indices))"
49+
);
50+
}

src/connected_components.cpp

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,20 @@
33
#include <nanobind/nanobind.h>
44
#include <nanobind/eigen/sparse.h>
55
#include <nanobind/ndarray.h>
6+
#include <nanobind/stl/tuple.h>
67

78
namespace nb = nanobind;
89
using namespace nb::literals;
910

1011
namespace pyigl
1112
{
1213
// Wrapper for connected_components
13-
nb::object connected_components(
14-
const Eigen::SparseMatrix<Integer> &A,
15-
const bool return_C = false,
16-
const bool return_K = false)
14+
auto connected_components( const Eigen::SparseMatrix<Integer> &A)
1715
{
18-
int num_components;
1916
Eigen::VectorXI C;
2017
Eigen::VectorXI K;
21-
22-
if (return_C && return_K)
23-
{
24-
num_components = igl::connected_components(A, C, K);
25-
return nb::make_tuple(num_components, C, K);
26-
}
27-
else if (return_C)
28-
{
29-
num_components = igl::connected_components(A, C, K);
30-
return nb::make_tuple(num_components, C);
31-
}
32-
else if (return_K)
33-
{
34-
num_components = igl::connected_components(A, C, K);
35-
return nb::make_tuple(num_components, K);
36-
}
37-
else
38-
{
39-
num_components = igl::connected_components(A, C, K);
40-
return nb::cast(num_components);
41-
}
18+
Integer num_components = (Integer)igl::connected_components(A, C, K);
19+
return std::make_tuple(num_components, C, K);
4220
}
4321
}
4422

@@ -49,8 +27,6 @@ void bind_connected_components(nb::module_ &m)
4927
"connected_components",
5028
&pyigl::connected_components,
5129
"A"_a,
52-
"return_C"_a = false,
53-
"return_K"_a = false,
5430
R"(Determine the connected components of a graph described by the input adjacency matrix.
5531
5632
@param[in] A #A by #A adjacency matrix (treated as describing a directed graph)

0 commit comments

Comments
 (0)