2019-11-01 11:10:03 +00:00
|
|
|
#define NOMINMAX
|
2019-11-01 14:31:26 +00:00
|
|
|
#include "OpenVDBUtils.hpp"
|
2019-11-01 11:10:03 +00:00
|
|
|
#include <openvdb/tools/MeshToVolume.h>
|
|
|
|
#include <openvdb/tools/VolumeToMesh.h>
|
2019-11-08 15:51:43 +00:00
|
|
|
#include <openvdb/tools/LevelSetRebuild.h>
|
|
|
|
|
|
|
|
//#include "MTUtils.hpp"
|
2019-11-01 11:10:03 +00:00
|
|
|
|
2019-10-31 13:36:33 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
class TriangleMeshDataAdapter {
|
|
|
|
public:
|
|
|
|
const TriangleMesh &mesh;
|
|
|
|
|
|
|
|
size_t polygonCount() const { return mesh.its.indices.size(); }
|
|
|
|
size_t pointCount() const { return mesh.its.vertices.size(); }
|
|
|
|
size_t vertexCount(size_t) const { return 3; }
|
|
|
|
|
|
|
|
// Return position pos in local grid index space for polygon n and vertex v
|
|
|
|
void getIndexSpacePoint(size_t n, size_t v, openvdb::Vec3d& pos) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Contour3DDataAdapter {
|
|
|
|
public:
|
|
|
|
const sla::Contour3D &mesh;
|
|
|
|
|
|
|
|
size_t polygonCount() const { return mesh.faces3.size() + mesh.faces4.size(); }
|
|
|
|
size_t pointCount() const { return mesh.points.size(); }
|
|
|
|
size_t vertexCount(size_t n) const { return n < mesh.faces3.size() ? 3 : 4; }
|
|
|
|
|
|
|
|
// Return position pos in local grid index space for polygon n and vertex v
|
|
|
|
void getIndexSpacePoint(size_t n, size_t v, openvdb::Vec3d& pos) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
void TriangleMeshDataAdapter::getIndexSpacePoint(size_t n,
|
|
|
|
size_t v,
|
|
|
|
openvdb::Vec3d &pos) const
|
|
|
|
{
|
|
|
|
auto vidx = size_t(mesh.its.indices[n](Eigen::Index(v)));
|
|
|
|
Slic3r::Vec3d p = mesh.its.vertices[vidx].cast<double>();
|
|
|
|
pos = {p.x(), p.y(), p.z()};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Contour3DDataAdapter::getIndexSpacePoint(size_t n,
|
|
|
|
size_t v,
|
|
|
|
openvdb::Vec3d &pos) const
|
|
|
|
{
|
|
|
|
size_t vidx = 0;
|
|
|
|
if (n < mesh.faces3.size()) vidx = size_t(mesh.faces3[n](Eigen::Index(v)));
|
|
|
|
else vidx = size_t(mesh.faces4[n - mesh.faces3.size()](Eigen::Index(v)));
|
|
|
|
|
|
|
|
Slic3r::Vec3d p = mesh.points[vidx];
|
|
|
|
pos = {p.x(), p.y(), p.z()};
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:38:43 +00:00
|
|
|
|
|
|
|
// TODO: Do I need to call initialize? Seems to work without it as well but the
|
|
|
|
// docs say it should be called ones. It does a mutex lock-unlock sequence all
|
|
|
|
// even if was called previously.
|
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
openvdb::FloatGrid::Ptr mesh_to_grid(const TriangleMesh &mesh,
|
2019-11-05 13:48:00 +00:00
|
|
|
const openvdb::math::Transform &tr,
|
2019-11-05 08:43:42 +00:00
|
|
|
float exteriorBandWidth,
|
|
|
|
float interiorBandWidth,
|
2019-11-05 13:48:00 +00:00
|
|
|
int flags)
|
2019-10-31 13:36:33 +00:00
|
|
|
{
|
2019-11-05 08:43:42 +00:00
|
|
|
openvdb::initialize();
|
2019-10-31 13:36:33 +00:00
|
|
|
return openvdb::tools::meshToVolume<openvdb::FloatGrid>(
|
2019-11-05 08:43:42 +00:00
|
|
|
TriangleMeshDataAdapter{mesh}, tr, exteriorBandWidth,
|
|
|
|
interiorBandWidth, flags);
|
2019-10-31 13:36:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
openvdb::FloatGrid::Ptr mesh_to_grid(const sla::Contour3D &mesh,
|
2019-11-05 13:48:00 +00:00
|
|
|
const openvdb::math::Transform &tr,
|
2019-11-05 08:43:42 +00:00
|
|
|
float exteriorBandWidth,
|
|
|
|
float interiorBandWidth,
|
2019-11-05 13:48:00 +00:00
|
|
|
int flags)
|
2019-10-31 13:36:33 +00:00
|
|
|
{
|
2019-11-05 08:43:42 +00:00
|
|
|
openvdb::initialize();
|
2019-10-31 13:36:33 +00:00
|
|
|
return openvdb::tools::meshToVolume<openvdb::FloatGrid>(
|
2019-11-05 08:43:42 +00:00
|
|
|
Contour3DDataAdapter{mesh}, tr, exteriorBandWidth, interiorBandWidth,
|
|
|
|
flags);
|
2019-10-31 13:36:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 08:43:42 +00:00
|
|
|
template<class Grid>
|
2019-11-08 15:51:43 +00:00
|
|
|
sla::Contour3D _volumeToMesh(const Grid &grid,
|
2019-11-06 12:38:43 +00:00
|
|
|
double isovalue,
|
|
|
|
double adaptivity,
|
|
|
|
bool relaxDisorientedTriangles)
|
2019-10-31 13:36:33 +00:00
|
|
|
{
|
2019-11-05 08:43:42 +00:00
|
|
|
openvdb::initialize();
|
|
|
|
|
2019-10-31 13:36:33 +00:00
|
|
|
std::vector<openvdb::Vec3s> points;
|
|
|
|
std::vector<openvdb::Vec3I> triangles;
|
|
|
|
std::vector<openvdb::Vec4I> quads;
|
2019-11-01 14:31:26 +00:00
|
|
|
|
2019-10-31 13:36:33 +00:00
|
|
|
openvdb::tools::volumeToMesh(grid, points, triangles, quads, isovalue,
|
|
|
|
adaptivity, relaxDisorientedTriangles);
|
|
|
|
|
|
|
|
sla::Contour3D ret;
|
|
|
|
ret.points.reserve(points.size());
|
|
|
|
ret.faces3.reserve(triangles.size());
|
|
|
|
ret.faces4.reserve(quads.size());
|
2019-11-06 12:38:43 +00:00
|
|
|
|
2019-10-31 13:36:33 +00:00
|
|
|
for (auto &v : points) ret.points.emplace_back(to_vec3d(v));
|
|
|
|
for (auto &v : triangles) ret.faces3.emplace_back(to_vec3i(v));
|
|
|
|
for (auto &v : quads) ret.faces4.emplace_back(to_vec4i(v));
|
2019-11-06 12:38:43 +00:00
|
|
|
|
2019-10-31 13:36:33 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
TriangleMesh grid_to_mesh(const openvdb::FloatGrid &grid,
|
2019-11-06 12:38:43 +00:00
|
|
|
double isovalue,
|
|
|
|
double adaptivity,
|
|
|
|
bool relaxDisorientedTriangles)
|
|
|
|
{
|
2019-11-08 15:51:43 +00:00
|
|
|
return to_triangle_mesh(
|
|
|
|
_volumeToMesh(grid, isovalue, adaptivity, relaxDisorientedTriangles));
|
2019-11-06 12:38:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
sla::Contour3D grid_to_contour3d(const openvdb::FloatGrid &grid,
|
|
|
|
double isovalue,
|
|
|
|
double adaptivity,
|
|
|
|
bool relaxDisorientedTriangles)
|
2019-11-06 12:38:43 +00:00
|
|
|
{
|
2019-11-08 15:51:43 +00:00
|
|
|
return _volumeToMesh(grid, isovalue, adaptivity,
|
|
|
|
relaxDisorientedTriangles);
|
2019-11-06 12:38:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 15:51:43 +00:00
|
|
|
openvdb::FloatGrid::Ptr redistance_grid(const openvdb::FloatGrid &grid, double iso, double er, double ir)
|
2019-11-05 08:43:42 +00:00
|
|
|
{
|
2019-11-08 15:51:43 +00:00
|
|
|
return openvdb::tools::levelSetRebuild(grid, float(iso), float(er), float(ir));
|
2019-11-05 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 13:36:33 +00:00
|
|
|
} // namespace Slic3r
|