diff --git a/src/marching_cubes.cpp b/src/marching_cubes.cpp index 9457737..f845023 100644 --- a/src/marching_cubes.cpp +++ b/src/marching_cubes.cpp @@ -312,6 +312,15 @@ Mesh::Mesh(size_t vertexCount, Point* vertices, Point* normals, size_t faceCount Mesh::Mesh() {} +Mesh::~Mesh() +{ + if (!vertices) + delete[] vertices; + if (!normals) + delete[] normals; + if (!faces) + delete[] faces; +} template Mesh march(const T* volume, size_t xDim, size_t yDim, size_t zDim, T isoLevel) diff --git a/src/marching_cubes.h b/src/marching_cubes.h index ff2b296..6337562 100644 --- a/src/marching_cubes.h +++ b/src/marching_cubes.h @@ -37,6 +37,7 @@ struct Mesh Mesh(size_t, Point*, Point*, size_t, size_t*); Mesh(); + ~Mesh(); }; /** @@ -48,4 +49,4 @@ struct Mesh */ template Mesh march(const T* volume, size_t xDim, size_t yDim, size_t zDim, T isoLevel); -#endif \ No newline at end of file +#endif diff --git a/src/python_wrapper.cpp b/src/python_wrapper.cpp index cd1eb19..f62013b 100644 --- a/src/python_wrapper.cpp +++ b/src/python_wrapper.cpp @@ -56,18 +56,42 @@ namespace marching_cubes throw py::value_error("vertex or face count are zero: terminating marching cubes"); } - std::string vert_format = py::format_descriptor::value; - auto vert_info = py::buffer_info(mesh.vertices, sizeof(float), vert_format, 2, { vc, 3 }, { sizeof(float) * 3, sizeof(float) }); - auto vert_array = py::array(vert_info); - - std::string norm_format = py::format_descriptor::value; - auto norm_info = py::buffer_info(mesh.normals, sizeof(float), norm_format, 2, { vc, 3 }, { sizeof(float) * 3, sizeof(float) }); - auto norm_array = py::array(norm_info); - - std::string face_format = py::format_descriptor::value; - auto face_info = py::buffer_info(mesh.faces, sizeof(size_t), face_format, 2, { fc, 3 }, { sizeof(size_t) * 3, sizeof(size_t) }); - auto face_array = py::array(face_info); - + //std::string vert_format = py::format_descriptor::value; + //auto vert_info = py::buffer_info(mesh.vertices, sizeof(float), vert_format, 2, { vc, 3 }, { sizeof(float) * 3, sizeof(float) }); + //auto vert_array = py::array(vert_info); + + //std::string norm_format = py::format_descriptor::value; + //auto norm_info = py::buffer_info(mesh.normals, sizeof(float), norm_format, 2, { vc, 3 }, { sizeof(float) * 3, sizeof(float) }); + //auto norm_array = py::array(norm_info); + + //std::string face_format = py::format_descriptor::value; + //auto face_info = py::buffer_info(mesh.faces, sizeof(size_t), face_format, 2, { fc, 3 }, { sizeof(size_t) * 3, sizeof(size_t) }); + //auto face_array = py::array(face_info); + + py::capsule free_vertices_when_done(mesh.vertices, [](void *f) { + float* foo = reinterpret_cast(f); + delete[] foo; + }); + py::capsule free_normals_when_done(mesh.normals, [](void *f) { + float* foo = reinterpret_cast(f); + delete[] foo; + }); + py::capsule free_faces_when_done(mesh.faces, [](void *f) { + size_t* foo = reinterpret_cast(f); + delete[] foo; + }); + auto vert_array = py::array_t({vc,3}, + {sizeof(float)*3,sizeof(float)}, + (float*)mesh.vertices, + free_vertices_when_done); + auto norm_array = py::array_t({vc,3}, + {sizeof(float)*3,sizeof(float)}, + (float*)mesh.normals, + free_normals_when_done); + auto face_array = py::array_t({fc,3}, + {sizeof(size_t)*3,sizeof(size_t)}, + (size_t*)mesh.faces, + free_faces_when_done); return std::make_tuple(vert_array, norm_array, face_array); }