mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-10-09 00:31:19 +00:00
* Add Missing Includes Across src/libslic3r Every libslic3r source and header now directly includes the headers declaring what it uses, rather than relying on the precompiled header or transitive includes. Generated with clang-tidy misc-include-cleaner, with libslic3r headers spelled libslic3r/... so they resolve outside the library's private include paths. MultiMaterialSegmentation.hpp, Support/SupportParameters.hpp and Format/STEP.hpp are made self-contained by hand. * Make the libslic3r Headers Compile on Their Own Each now includes, or forward-declares, what it uses instead of relying on what its includers happened to include first. Left out: I18N.hpp, which errors on purpose when included from GUI code, and VoxelizeCSGMesh.hpp and SLA/bicubic.h, which nothing includes and which no longer compile at all. * Add the Includes Missing From the Hand-Fixed libslic3r Headers clang-tidy would not edit these headers while they failed to compile on their own, so the first pass skipped them. With the headers now self-contained, a second pass adds the rest. * Keep Windows Setup Ahead of the Added libslic3r Includes Print.cpp and Thread.cpp open with a _WIN32 block that has to come first; without the precompiled header, Print.cpp otherwise reaches windows.h through OCCT with NONLS defined and boost/regex fails. OpenVDBUtils.cpp and SLA/SupportTreeBuilder.cpp had includes inside #ifndef NOMINMAX, which libslic3r defines on Windows, so those were skipped there. .clang-tidy also ignores the MSVC STL and UCRT internals, Boost.Multiprecision's fwd.hpp and CPython's Windows include directory. * Re-Add libslic3r Includes After the Clipper2 2.0.1 Migration Rebasing onto main took main's version of the files the Clipper2 migration rewrote, so their added includes are restored here, along with includes for main's new code. Clipper2's individual headers are now ignored by clang-tidy: they only build the Z variant through clipper2_z.hpp, which defines USINGZ first, so including clipper.core.h and the like directly broke ClipperZUtils.cpp.
152 lines
5.1 KiB
C++
152 lines
5.1 KiB
C++
#ifndef NOMINMAX
|
|
#define NOMINMAX
|
|
#endif
|
|
|
|
#include <cstddef>
|
|
#include <openvdb/Types.h>
|
|
#include <Eigen/Core>
|
|
#include "Point.hpp"
|
|
#include <openvdb/openvdb.h>
|
|
#include <openvdb/math/Transform.h>
|
|
#include <vector>
|
|
#include "TriangleMesh.hpp"
|
|
#include <algorithm>
|
|
#include "libslic3r.h"
|
|
#include <utility>
|
|
#include <openvdb/Metadata.h>
|
|
#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
|
|
#include <openvdb/tools/MeshToVolume.h>
|
|
#ifdef _MSC_VER
|
|
#pragma warning(pop)
|
|
#endif // _MSC_VER
|
|
|
|
#include <openvdb/tools/VolumeToMesh.h>
|
|
#include <openvdb/tools/Composite.h>
|
|
#include <openvdb/tools/LevelSetRebuild.h>
|
|
|
|
//#include "MTUtils.hpp"
|
|
|
|
namespace Slic3r {
|
|
|
|
class TriangleMeshDataAdapter {
|
|
public:
|
|
const indexed_triangle_set &its;
|
|
float voxel_scale;
|
|
|
|
size_t polygonCount() const { return its.indices.size(); }
|
|
size_t pointCount() const { return 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
|
|
// 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
|
|
{
|
|
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()};
|
|
}
|
|
|
|
TriangleMeshDataAdapter(const indexed_triangle_set &m, float voxel_sc = 1.f)
|
|
: its{m}, voxel_scale{voxel_sc} {};
|
|
};
|
|
|
|
// 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.
|
|
openvdb::FloatGrid::Ptr mesh_to_grid(const indexed_triangle_set & mesh,
|
|
const openvdb::math::Transform &tr,
|
|
float voxel_scale,
|
|
float exteriorBandWidth,
|
|
float interiorBandWidth,
|
|
int flags)
|
|
{
|
|
openvdb::initialize();
|
|
|
|
std::vector<indexed_triangle_set> meshparts = its_split(mesh);
|
|
|
|
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, exteriorBandWidth,
|
|
interiorBandWidth, flags);
|
|
|
|
if (grid && subgrid) openvdb::tools::csgUnion(*grid, *subgrid);
|
|
else if (subgrid) grid = std::move(subgrid);
|
|
}
|
|
|
|
if (grid) {
|
|
grid = openvdb::tools::levelSetRebuild(*grid, 0., exteriorBandWidth,
|
|
interiorBandWidth);
|
|
} else if(meshparts.empty()) {
|
|
// Splitting failed, fall back to hollow the original mesh
|
|
grid = openvdb::tools::meshToVolume<openvdb::FloatGrid>(
|
|
TriangleMeshDataAdapter{mesh}, tr, exteriorBandWidth,
|
|
interiorBandWidth, flags);
|
|
}
|
|
|
|
grid->insertMeta("voxel_scale", openvdb::FloatMetadata(voxel_scale));
|
|
|
|
return grid;
|
|
}
|
|
|
|
indexed_triangle_set grid_to_mesh(const openvdb::FloatGrid &grid,
|
|
double isovalue,
|
|
double adaptivity,
|
|
bool relaxDisorientedTriangles)
|
|
{
|
|
openvdb::initialize();
|
|
|
|
std::vector<openvdb::Vec3s> points;
|
|
std::vector<openvdb::Vec3I> triangles;
|
|
std::vector<openvdb::Vec4I> quads;
|
|
|
|
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));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
} // namespace Slic3r
|