Skip to content

Commit

Permalink
Update FTetWildWrapper.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
akaszynski authored Feb 29, 2024
1 parent a518948 commit 09fd105
Showing 1 changed file with 79 additions and 14 deletions.
93 changes: 79 additions & 14 deletions src/FTetWildWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,40 @@

#include <Eigen/Dense>
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
namespace py = pybind11;

#include <vector>
#include <floattetwild/Mesh.hpp>

std::pair<std::vector<std::array<float, 3>>, std::vector<std::array<int, 4>>> extractMeshData(const floatTetWild::Mesh& mesh) {
std::vector<std::array<float, 3>> vertices;
std::vector<std::array<int, 4>> tetrahedra;

// Extract vertices
for (const auto& vertex : mesh.tet_vertices) {
if (!vertex.is_removed) {
vertices.push_back({{static_cast<float>(vertex.pos.x()), static_cast<float>(vertex.pos.y()), static_cast<float>(vertex.pos.z())}});
}
}

// Extract tetrahedra
for (const auto& tet : mesh.tets) {
if (!tet.is_removed) {
tetrahedra.push_back({{tet.indices[0], tet.indices[1], tet.indices[2], tet.indices[3]}});
}
}

return {vertices, tetrahedra};
}

// You must include other necessary headers and namespaces used in your project

void tetrahedralizeAndWriteMesh(const std::vector<Eigen::Vector3d>& vertices,
const std::vector<Eigen::Vector3i>& faces,
const std::string& outputPath) {
std::pair<std::vector<Eigen::Vector3d>, std::vector<Eigen::Vector4i>> tetrahedralizeAndWriteMesh(
const std::vector<Eigen::Vector3d>& vertices,
const std::vector<Eigen::Vector3i>& faces) {
using namespace floatTetWild;
using namespace Eigen;

Expand Down Expand Up @@ -42,18 +70,55 @@ void tetrahedralizeAndWriteMesh(const std::vector<Eigen::Vector3d>& vertices,
// Perform tetrahedralization
FloatTetDelaunay::tetrahedralize(input_vertices, input_faces, tree, mesh, is_face_inserted);

// Write the tetrahedralized mesh to a file
std::vector<Scalar> colors; // Optional: For visualizing quality or other metrics
MeshIO::write_mesh(outputPath, mesh, false, colors);
return extractMeshData(mesh);
}

// Example usage:
int main() {
std::vector<Eigen::Vector3d> vertices = {/* Fill with your vertices */};
std::vector<Eigen::Vector3i> faces = {/* Fill with your faces */};
std::string outputPath = "output_mesh.msh";
PYBIND11_MODULE(_wrapper, m) {
m.doc() = "Pybind11 plugin for FloatTetWild mesh tetrahedralization";

tetrahedralizeAndWriteMesh(vertices, faces, outputPath);
m.def("tetrahedralize_mesh", [](py::array_t<double> vertices, py::array_t<int> faces) {
// Convert numpy arrays to Eigen matrices for vertices and faces
auto verts = vertices.unchecked<2>(); // Access numpy array data without bounds checking
auto fcs = faces.unchecked<2>();

return 0;
std::vector<Eigen::Vector3d> vert_vector(verts.shape(0));
std::vector<Eigen::Vector3i> face_vector(fcs.shape(0));

for (py::ssize_t i = 0; i < verts.shape(0); ++i) {
vert_vector[i] = Eigen::Vector3d(verts(i, 0), verts(i, 1), verts(i, 2));
}

for (py::ssize_t i = 0; i < fcs.shape(0); ++i) {
face_vector[i] = Eigen::Vector3i(fcs(i, 0), fcs(i, 1), fcs(i, 2));
}

// Call the tetrahedralization function
auto [vertices_result, tetrahedra_result] = tetrahedralizeAndWriteMesh(vert_vector, face_vector);

// Convert results back to numpy arrays
size_t num_vertices = vertices_result.size();
size_t num_tetrahedra = tetrahedra_result.size();

// Prepare numpy array (points)
py::array_t<float> np_vertices({num_vertices, 3});
auto np_verts_access = np_vertices.mutable_unchecked<2>();
for (size_t i = 0; i < num_vertices; ++i) {
for (size_t j = 0; j < 3; ++j) {
np_verts_access(i, j) = vertices_result[i][j];
}
}

// Prepare numpy array (tetrahedra)
py::array_t<int> np_tetrahedra({num_tetrahedra, 4});
auto np_tets_access = np_tetrahedra.mutable_unchecked<2>();
for (size_t i = 0; i < num_tetrahedra; ++i) {
for (size_t j = 0; j < 4; ++j) {
np_tets_access(i, j) = tetrahedra_result[i][j];
}
}

return std::make_pair(np_vertices, np_tetrahedra);
}, "Tetrahedralizes a mesh given vertices and faces arrays, returning numpy arrays of tetrahedra and points.");
}


0 comments on commit 09fd105

Please sign in to comment.