2019-11-01 11:10:03 +00:00
|
|
|
#define NOMINMAX
|
2019-11-01 14:31:26 +00:00
|
|
|
#include "OpenVDBUtils.hpp"
|
2021-02-10 17:04:16 +00:00
|
|
|
|
|
|
|
#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>
|
2021-02-10 17:04:16 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif // _MSC_VER
|
|
|
|
|
2019-11-01 11:10:03 +00:00
|
|
|
#include <openvdb/tools/VolumeToMesh.h>
|
2020-09-10 11:39:43 +00:00
|
|
|
#include <openvdb/tools/Composite.h>
|
2019-11-08 15:51:43 +00:00
|
|
|
#include <openvdb/tools/LevelSetRebuild.h>
|
2022-04-06 13:58:59 +00:00
|
|
|
#include <openvdb/tools/FastSweeping.h>
|
2019-11-08 15:51:43 +00:00
|
|
|
|
|
|
|
//#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;
|
2020-12-17 12:38:09 +00:00
|
|
|
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
|
2020-12-17 12:38:09 +00:00
|
|
|
// 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;
|
2020-12-17 12:38:09 +00:00
|
|
|
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} {};
|
2020-12-17 12:38:09 +00:00
|
|
|
};
|
2019-11-06 12:38:43 +00:00
|
|
|
|
2021-05-25 16:23:01 +00:00
|
|
|
openvdb::FloatGrid::Ptr mesh_to_grid(const indexed_triangle_set & mesh,
|
2019-11-05 13:48:00 +00:00
|
|
|
const openvdb::math::Transform &tr,
|
2020-12-17 12:38:09 +00:00
|
|
|
float voxel_scale,
|
|
|
|
float exteriorBandWidth,
|
2022-04-04 14:37:58 +00:00
|
|
|
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
|
2019-11-05 08:43:42 +00:00
|
|
|
openvdb::initialize();
|
2020-09-10 11:39:43 +00:00
|
|
|
|
2021-05-28 12:55:25 +00:00
|
|
|
std::vector<indexed_triangle_set> meshparts = its_split(mesh);
|
2021-05-26 14:41:34 +00:00
|
|
|
|
2021-05-28 12:55:25 +00:00
|
|
|
auto it = std::remove_if(meshparts.begin(), meshparts.end(),
|
|
|
|
[](auto &m) { return its_volume(m) < EPSILON; });
|
2020-09-10 11:39:43 +00:00
|
|
|
|
|
|
|
meshparts.erase(it, meshparts.end());
|
|
|
|
|
|
|
|
openvdb::FloatGrid::Ptr grid;
|
2021-02-24 17:56:01 +00:00
|
|
|
for (auto &m : meshparts) {
|
2020-09-16 09:08:58 +00:00
|
|
|
auto subgrid = openvdb::tools::meshToVolume<openvdb::FloatGrid>(
|
2022-04-08 11:59:40 +00:00
|
|
|
TriangleMeshDataAdapter{m, voxel_scale}, tr, 1.f, 1.f);
|
2020-09-10 11:39:43 +00:00
|
|
|
|
2020-09-16 09:08:58 +00:00
|
|
|
if (grid && subgrid) openvdb::tools::csgUnion(*grid, *subgrid);
|
|
|
|
else if (subgrid) grid = std::move(subgrid);
|
2020-09-10 11:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 10:11:37 +00:00
|
|
|
if(meshparts.empty()) {
|
2020-09-16 09:08:58 +00:00
|
|
|
// Splitting failed, fall back to hollow the original mesh
|
|
|
|
grid = openvdb::tools::meshToVolume<openvdb::FloatGrid>(
|
2022-04-08 11:59:40 +00:00
|
|
|
TriangleMeshDataAdapter{mesh}, tr, 1.f, 1.f);
|
2020-09-16 09:08:58 +00:00
|
|
|
}
|
2020-09-10 11:39:43 +00:00
|
|
|
|
2022-04-06 13:58:59 +00:00
|
|
|
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);
|
|
|
|
|
2020-12-17 12:38:09 +00:00
|
|
|
grid->insertMeta("voxel_scale", openvdb::FloatMetadata(voxel_scale));
|
2019-10-31 13:36:33 +00:00
|
|
|
|
2020-12-17 12:38:09 +00:00
|
|
|
return grid;
|
2019-10-31 13:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 14:41:34 +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
|
|
|
{
|
2019-11-05 08:43:42 +00:00
|
|
|
openvdb::initialize();
|
2020-12-17 12:38:09 +00:00
|
|
|
|
2019-10-31 13:36:33 +00:00
|
|
|
std::vector<openvdb::Vec3s> points;
|
|
|
|
std::vector<openvdb::Vec3I> triangles;
|
|
|
|
std::vector<openvdb::Vec4I> quads;
|
2020-12-17 12:38:09 +00:00
|
|
|
|
2019-10-31 13:36:33 +00:00
|
|
|
openvdb::tools::volumeToMesh(grid, points, triangles, quads, isovalue,
|
|
|
|
adaptivity, relaxDisorientedTriangles);
|
2020-12-17 12:38:09 +00:00
|
|
|
|
|
|
|
float scale = 1.;
|
|
|
|
try {
|
|
|
|
scale = grid.template metaValue<float>("voxel_scale");
|
|
|
|
} catch (...) { }
|
|
|
|
|
2021-05-26 14:41:34 +00:00
|
|
|
indexed_triangle_set ret;
|
|
|
|
ret.vertices.reserve(points.size());
|
|
|
|
ret.indices.reserve(triangles.size() + quads.size() * 2);
|
2019-11-06 12:38:43 +00:00
|
|
|
|
2021-05-26 14:41:34 +00:00
|
|
|
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-11-06 12:38:43 +00:00
|
|
|
|
2019-10-31 13:36:33 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-17 12:38:09 +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
|
|
|
{
|
2020-12-17 12:38:09 +00:00
|
|
|
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;
|
2019-11-05 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 13:58:59 +00:00
|
|
|
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
|