mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-10-07 07:41:06 +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.
193 lines
6.2 KiB
C++
193 lines
6.2 KiB
C++
#include <draco/core/decoder_buffer.h>
|
|
#include <draco/compression/config/compression_shared.h>
|
|
#include <draco/core/status.h>
|
|
#include <draco/attributes/point_attribute.h>
|
|
#include <draco/attributes/geometry_attribute.h>
|
|
#include <boost/log/trivial.hpp>
|
|
#include <draco/attributes/geometry_indices.h>
|
|
#include <cstdint>
|
|
#include <exception>
|
|
#include <draco/core/draco_types.h>
|
|
#include <draco/core/encoder_buffer.h>
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <cstring>
|
|
|
|
#include <boost/iostreams/device/mapped_file.hpp>
|
|
#include <boost/nowide/cstdio.hpp>
|
|
|
|
#include <draco/compression/encode.h>
|
|
#include <draco/compression/decode.h>
|
|
#include <draco/io/mesh_io.h>
|
|
#include <draco/mesh/mesh.h>
|
|
#include <vector>
|
|
using namespace draco;
|
|
|
|
#include "libslic3r/Model.hpp"
|
|
#include "libslic3r/TriangleMesh.hpp"
|
|
#include "DRC.hpp"
|
|
|
|
#ifdef _WIN32
|
|
#define DIR_SEPARATOR '\\'
|
|
#else
|
|
#define DIR_SEPARATOR '/'
|
|
#endif
|
|
|
|
namespace Slic3r {
|
|
|
|
bool load_drc(const char *path, TriangleMesh *meshptr)
|
|
{
|
|
try {
|
|
boost::iostreams::mapped_file_source file{boost::filesystem::path{path}};
|
|
|
|
DecoderBuffer buffer;
|
|
buffer.Init(file.data(), file.size());
|
|
|
|
auto geotype = Decoder::GetEncodedGeometryType(&buffer);
|
|
if ((!geotype.ok()) || geotype.value() != TRIANGULAR_MESH) {
|
|
return false;
|
|
}
|
|
|
|
Decoder decoder;
|
|
Mesh dracoMesh;
|
|
Status status = decoder.DecodeBufferToGeometry(&buffer, &dracoMesh);
|
|
if (!status.ok()) {
|
|
return false;
|
|
}
|
|
file.close();
|
|
|
|
|
|
indexed_triangle_set its;
|
|
|
|
const PointAttribute *const positions = dracoMesh.GetNamedAttribute(GeometryAttribute::POSITION);
|
|
if (positions == nullptr) {
|
|
BOOST_LOG_TRIVIAL(error) << "load_drc: the mesh has no POSITION attribute";
|
|
return false;
|
|
}
|
|
size_t num_vertices = positions->size();
|
|
its.vertices.reserve(num_vertices);
|
|
for (AttributeValueIndex i(0); i < num_vertices; ++ i) {
|
|
float pos[3];
|
|
if (!positions->ConvertValue<float>(i, 3, pos)) {
|
|
BOOST_LOG_TRIVIAL(error) << "load_drc: invalid vertex position";
|
|
return false;
|
|
}
|
|
its.vertices.emplace_back(pos[0], pos[1], pos[2]);
|
|
}
|
|
|
|
// The Draco decoder does not check face indices against the point count.
|
|
const uint32_t num_points = dracoMesh.num_points();
|
|
size_t num_faces = dracoMesh.num_faces();
|
|
its.indices.reserve(num_faces);
|
|
for (FaceIndex i(0); i < num_faces; ++ i) {
|
|
const Mesh::Face &face = dracoMesh.face(i);
|
|
stl_triangle_vertex_indices facet;
|
|
for (int k = 0; k < 3; ++ k) {
|
|
const size_t vertex_idx = face[k].value() < num_points ? positions->mapped_index(face[k]).value() : num_vertices;
|
|
if (vertex_idx >= num_vertices) {
|
|
BOOST_LOG_TRIVIAL(error) << "load_drc: invalid vertex index";
|
|
return false;
|
|
}
|
|
facet[k] = static_cast<int>(vertex_idx);
|
|
}
|
|
its.indices.emplace_back(facet);
|
|
}
|
|
|
|
*meshptr = TriangleMesh(std::move(its));
|
|
if (meshptr->volume() < 0)
|
|
meshptr->flip_triangles();
|
|
} catch (const std::exception& e) {
|
|
BOOST_LOG_TRIVIAL(error) << "load_drc: " << e.what();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool load_drc(const char *path, Model *model, const char *object_name_in)
|
|
{
|
|
TriangleMesh mesh;
|
|
|
|
bool ret = load_drc(path, &mesh);
|
|
|
|
if (ret) {
|
|
std::string object_name;
|
|
if (object_name_in == nullptr) {
|
|
const char *last_slash = strrchr(path, DIR_SEPARATOR);
|
|
object_name.assign((last_slash == nullptr) ? path : last_slash + 1);
|
|
} else
|
|
object_name.assign(object_name_in);
|
|
|
|
model->add_object(object_name.c_str(), path, std::move(mesh));
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
bool store_drc(const char *path, TriangleMesh *mesh, int bits, int speed)
|
|
{
|
|
try {
|
|
const std::vector<stl_triangle_vertex_indices>* indices = &(mesh->its.indices);
|
|
const std::vector<stl_vertex>* vertices = &(mesh->its.vertices);
|
|
|
|
Mesh dracoMesh;
|
|
|
|
dracoMesh.set_num_points(vertices->size());
|
|
|
|
GeometryAttribute gaPos;
|
|
gaPos.Init(GeometryAttribute::POSITION, nullptr, 3, DT_FLOAT32, false, sizeof(float)*3, 0);
|
|
int32_t idPos = dracoMesh.AddAttribute(gaPos, true, indices->size() * 3);
|
|
|
|
dracoMesh.attribute(idPos)->Resize(vertices->size());
|
|
|
|
for (size_t i = 0; i < vertices->size(); ++ i) {
|
|
float vertex[3];
|
|
vertex[0] = vertices->at(i)(0);
|
|
vertex[1] = vertices->at(i)(1);
|
|
vertex[2] = vertices->at(i)(2);
|
|
dracoMesh.attribute(idPos)->SetAttributeValue(AttributeValueIndex(i), vertex);
|
|
}
|
|
|
|
dracoMesh.SetNumFaces(indices->size());
|
|
for (size_t i = 0; i < indices->size(); ++ i) {
|
|
Mesh::Face face;
|
|
face[0] = PointIndex(indices->at(i)[0]);
|
|
face[1] = PointIndex(indices->at(i)[1]);
|
|
face[2] = PointIndex(indices->at(i)[2]);
|
|
dracoMesh.SetFace(FaceIndex(i), face);
|
|
}
|
|
|
|
Encoder encoder;
|
|
encoder.SetSpeedOptions(speed, speed);
|
|
encoder.SetAttributeQuantization(GeometryAttribute::POSITION, bits);
|
|
|
|
EncoderBuffer buffer;
|
|
encoder.EncodeMeshToBuffer(dracoMesh, &buffer);
|
|
|
|
FILE* fp = boost::nowide::fopen(path, "wb");
|
|
if (!fp) return false;
|
|
size_t written = fwrite(buffer.data(), 1, buffer.size(), fp);
|
|
fclose(fp);
|
|
|
|
if (written != buffer.size()) return false;
|
|
} catch (const std::exception& e) {
|
|
BOOST_LOG_TRIVIAL(error) << "store_drc: " << e.what();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool store_drc(const char *path, ModelObject *model_object, int bits, int speed)
|
|
{
|
|
TriangleMesh mesh = model_object->mesh();
|
|
return store_drc(path, &mesh, bits, speed);
|
|
}
|
|
|
|
bool store_drc(const char *path, Model *model, int bits, int speed)
|
|
{
|
|
TriangleMesh mesh = model->mesh();
|
|
return store_drc(path, &mesh, bits, speed);
|
|
}
|
|
|
|
}; // namespace Slic3r
|