Files
OrcaSlicer/src/libslic3r/TextureBake/TextureBakeRelocate.hpp
T
HanifKoh 8a6377f087 Add Missing Includes Across src/libslic3r (#16068)
* 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.
2026-10-03 15:31:11 +08:00

74 lines
3.2 KiB
C++

#pragma once
// Tangential relocation: slide vertices along the surface so triangle edges land on the height map's
// own edges, before any displacement happens.
//
// Displacement moves vertices along the normal only, so a step in the height map - the wall of a
// mortar groove, the rim of an embossed shape - is reproduced wherever the triangle grid happens to
// fall, as a staircase quantised to triangle boundaries. Refining further only makes the steps
// smaller; it never straightens them, because the edge in the image still does not coincide with any
// edge in the mesh.
//
// This pass fixes the cause rather than the symptom. For a vertex sitting near a step it takes a
// Newton step onto the contour: with h the sampled height and g its tangential gradient, the move
//
// d = -(h - target) * g / |g|^2
//
// lands on the level set h = target to first order. The ring of vertices nearest each step therefore
// snaps onto it, the triangle edges between them follow the step, and the displaced result has a
// straight wall instead of a sawtooth.
//
// Vertices in flat regions have no gradient to speak of and are left alone, so the pass costs nothing
// where there is nothing to align.
#include <cstddef>
#include <cstdint>
#include <functional>
#include <vector>
#include "TextureBakeDisplace.hpp"
#include "TextureBakeIndex.hpp"
namespace Slic3r {
namespace TextureBake {
struct RelocateSettings
{
// Passes. Each is a Newton step, so a couple converge for vertices that start reasonably close;
// more mainly helps ones that begin further away.
int iterations = 3;
// How far a vertex may move in one pass, as a fraction of the mean length of its incident edges.
// Below a half it cannot pass a neighbour, which is what keeps the triangulation valid without
// needing a full topological check.
double max_move_fraction = 0.35;
// A vertex is only pulled when the height varies enough across its own footprint to mean
// something - as a fraction of the height range over the whole patch. Below this the gradient is
// noise, and chasing it would scramble flat regions.
double min_gradient_fraction = 0.05;
// The level to snap onto, as a position in the sampled height range: 0.5 is midway between the
// lowest and highest point of the relief, which is where the wall of a step is steepest.
double contour_level = 0.5;
// Sampling offset for the finite-difference gradient, as a fraction of the local edge length.
double gradient_step_fraction = 0.25;
};
struct RelocateResult
{
TriSoup geometry;
size_t moved = 0; // positions that were relocated at least once
size_t rejected = 0; // moves refused because a triangle would have inverted
};
// `locked`, when non-empty, has one entry per input triangle; a vertex touching a locked triangle is
// never moved, so an excluded region keeps its exact vertex positions.
RelocateResult relocate_to_contours(const TriSoup &geometry, const HeightSampleFn &sample,
const RelocateSettings &settings = {},
const std::vector<uint8_t> &locked = {});
} // namespace TextureBake
} // namespace Slic3r