PrusaSlicer-NonPlainar/src/libslic3r/OpenVDBUtils.cpp

158 lines
5.4 KiB
C++
Raw Normal View History

2019-11-01 11:10:03 +00:00
#define NOMINMAX
#include "OpenVDBUtils.hpp"
#ifdef _MSC_VER
// Suppress warning C4146 in OpenVDB: unary minus operator applied to unsigned type, result still unsigned
#pragma warning(push)
#pragma warning(disable : 4146)
#endif // _MSC_VER
2019-11-01 11:10:03 +00:00
#include <openvdb/tools/MeshToVolume.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
2019-11-01 11:10:03 +00:00
#include <openvdb/tools/VolumeToMesh.h>
#include <openvdb/tools/Composite.h>
#include <openvdb/tools/LevelSetRebuild.h>
#include <openvdb/tools/FastSweeping.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:
2021-05-25 16:23:01 +00:00
const indexed_triangle_set &its;
float voxel_scale;
2021-05-25 16:23:01 +00:00
size_t polygonCount() const { return its.indices.size(); }
size_t pointCount() const { return its.vertices.size(); }
2019-10-31 13:36:33 +00:00
size_t vertexCount(size_t) const { return 3; }
// Return position pos in local grid index space for polygon n and vertex v
// The actual mesh will appear to openvdb as scaled uniformly by voxel_size
// And the voxel count per unit volume can be affected this way.
void getIndexSpacePoint(size_t n, size_t v, openvdb::Vec3d& pos) const
{
2021-05-25 16:23:01 +00:00
auto vidx = size_t(its.indices[n](Eigen::Index(v)));
Slic3r::Vec3d p = its.vertices[vidx].cast<double>() * voxel_scale;
pos = {p.x(), p.y(), p.z()};
}
2019-10-31 13:36:33 +00:00
2021-05-25 16:23:01 +00:00
TriangleMeshDataAdapter(const indexed_triangle_set &m, float voxel_sc = 1.f)
: its{m}, voxel_scale{voxel_sc} {};
};
2021-05-25 16:23:01 +00:00
openvdb::FloatGrid::Ptr mesh_to_grid(const indexed_triangle_set & mesh,
const openvdb::math::Transform &tr,
float voxel_scale,
float exteriorBandWidth,
float interiorBandWidth)
2019-10-31 13:36:33 +00:00
{
2022-04-07 10:11:37 +00:00
// Might not be needed but this is now proven to be working
openvdb::initialize();
2021-05-28 12:55:25 +00:00
std::vector<indexed_triangle_set> meshparts = its_split(mesh);
2021-05-28 12:55:25 +00:00
auto it = std::remove_if(meshparts.begin(), meshparts.end(),
[](auto &m) { return its_volume(m) < EPSILON; });
meshparts.erase(it, meshparts.end());
openvdb::FloatGrid::Ptr grid;
for (auto &m : meshparts) {
auto subgrid = openvdb::tools::meshToVolume<openvdb::FloatGrid>(
TriangleMeshDataAdapter{m, voxel_scale}, tr, 1.f, 1.f);
if (grid && subgrid) openvdb::tools::csgUnion(*grid, *subgrid);
else if (subgrid) grid = std::move(subgrid);
}
if (meshparts.size() > 1) {
// This is needed to avoid various artefacts on multipart meshes.
// TODO: replace with something faster
grid = openvdb::tools::levelSetRebuild(*grid, 0., 1.f, 1.f);
}
2022-04-07 10:11:37 +00:00
if(meshparts.empty()) {
// Splitting failed, fall back to hollow the original mesh
grid = openvdb::tools::meshToVolume<openvdb::FloatGrid>(
TriangleMeshDataAdapter{mesh}, tr, 1.f, 1.f);
}
constexpr int DilateIterations = 1;
grid = openvdb::tools::dilateSdf(
*grid, interiorBandWidth, openvdb::tools::NN_FACE_EDGE,
DilateIterations,
openvdb::tools::FastSweepingDomain::SWEEP_LESS_THAN_ISOVALUE);
grid = openvdb::tools::dilateSdf(
*grid, exteriorBandWidth, openvdb::tools::NN_FACE_EDGE,
DilateIterations,
openvdb::tools::FastSweepingDomain::SWEEP_GREATER_THAN_ISOVALUE);
grid->insertMeta("voxel_scale", openvdb::FloatMetadata(voxel_scale));
2019-10-31 13:36:33 +00:00
return grid;
2019-10-31 13:36:33 +00:00
}
indexed_triangle_set grid_to_mesh(const openvdb::FloatGrid &grid,
double isovalue,
double adaptivity,
bool relaxDisorientedTriangles)
2019-10-31 13:36:33 +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-10-31 13:36:33 +00:00
openvdb::tools::volumeToMesh(grid, points, triangles, quads, isovalue,
adaptivity, relaxDisorientedTriangles);
float scale = 1.;
try {
scale = grid.template metaValue<float>("voxel_scale");
} catch (...) { }
indexed_triangle_set ret;
ret.vertices.reserve(points.size());
ret.indices.reserve(triangles.size() + quads.size() * 2);
for (auto &v : points) ret.vertices.emplace_back(to_vec3f(v) / scale);
for (auto &v : triangles) ret.indices.emplace_back(to_vec3i(v));
for (auto &quad : quads) {
ret.indices.emplace_back(quad(0), quad(1), quad(2));
ret.indices.emplace_back(quad(2), quad(3), quad(0));
}
2019-10-31 13:36:33 +00:00
return ret;
}
openvdb::FloatGrid::Ptr redistance_grid(const openvdb::FloatGrid &grid,
double iso,
double er,
double ir)
{
auto new_grid = openvdb::tools::levelSetRebuild(grid, float(iso),
float(er), float(ir));
// Copies voxel_scale metadata, if it exists.
new_grid->insertMeta(*grid.deepCopyMeta());
return new_grid;
}
openvdb::FloatGrid::Ptr redistance_grid(const openvdb::FloatGrid &grid,
double iso)
{
auto new_grid = openvdb::tools::levelSetRebuild(grid, float(iso));
// Copies voxel_scale metadata, if it exists.
new_grid->insertMeta(*grid.deepCopyMeta());
return new_grid;
}
2019-10-31 13:36:33 +00:00
} // namespace Slic3r