-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy_api.cpp
71 lines (58 loc) · 2.35 KB
/
py_api.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <vector>
#include <array>
#include <tuple>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "marchingcubes.h"
#include <pybind11/eigen.h>
#include <Eigen/Dense>
namespace py = pybind11;
using namespace std;
using namespace Eigen;
tuple<MatrixXd, MatrixXi, vector<uint>, vector<uint>, vector<bool>>
marching_cubes(const py::EigenDRef<VectorXd> arr_flatten, const array<int, 3>& shape, double isovalue)
{
// here we use EigenDRef as it does not make a copy caues it's just a refernce
// pybind11 seems not support pass by reference for std::vector
array<int, 3> lower{0, 0, 0};
array<int, 3> upper{shape[0]-1, shape[1]-1, shape[2]-1};
int numx = upper[0] - lower[0] + 1;
int numy = upper[1] - lower[1] + 1;
int numz = upper[2] - lower[2] + 1;
auto access_3d_arr = [&](int i, int j, int k){
auto idx = i * shape[1] * shape[2] + j * shape[2] + k;
return arr_flatten(idx);
};
auto tm = TableManager(numx, numy, numz);
tm.reset();
return mc::marching_cubes(lower, upper, numx, numy, numz, access_3d_arr, isovalue, tm);
}
class MarchingCubeStaticSize
{
public:
TableManager tm;
array<int, 3> shape;
MarchingCubeStaticSize(const array<int, 3>& shape_)
: tm(TableManager(shape_[0], shape_[1], shape_[2])), shape(shape_) {}
tuple<MatrixXd, MatrixXi, vector<uint>, vector<uint>, vector<bool>>
compute(const py::EigenDRef<VectorXd> arr_flatten, double isovalue){
array<int, 3> lower{0, 0, 0};
array<int, 3> upper{shape[0]-1, shape[1]-1, shape[2]-1};
int numx = upper[0] - lower[0] + 1;
int numy = upper[1] - lower[1] + 1;
int numz = upper[2] - lower[2] + 1;
auto access_3d_arr = [&](int i, int j, int k){
auto idx = i * shape[1] * shape[2] + j * shape[2] + k;
return arr_flatten(idx);
};
tm.reset();
return mc::marching_cubes(lower, upper, numx, numy, numz, access_3d_arr, isovalue, tm);
}
};
PYBIND11_MODULE(mcube, m) {
m.doc() = "python wrapper";
m.def("marching_cube", &marching_cubes, "marching cube method");
py::class_<MarchingCubeStaticSize>(m, "MarchingCubeStaticSize")
.def(py::init<std::array<int, 3>&>())
.def("compute", &MarchingCubeStaticSize::compute);
}