mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-10-08 16:21:14 +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.
148 lines
4.7 KiB
C++
148 lines
4.7 KiB
C++
#include "NormalUtils.hpp"
|
|
#include "Point.hpp"
|
|
#include <vector>
|
|
#include <cstddef>
|
|
#include <cmath>
|
|
#include <math.h>
|
|
|
|
using namespace Slic3r;
|
|
|
|
Vec3f NormalUtils::create_triangle_normal(
|
|
const stl_triangle_vertex_indices &indices,
|
|
const std::vector<stl_vertex> & vertices)
|
|
{
|
|
const stl_vertex &v0 = vertices[indices[0]];
|
|
const stl_vertex &v1 = vertices[indices[1]];
|
|
const stl_vertex &v2 = vertices[indices[2]];
|
|
Vec3f direction = (v1 - v0).cross(v2 - v0);
|
|
direction.normalize();
|
|
return direction;
|
|
}
|
|
|
|
std::vector<Vec3f> NormalUtils::create_triangle_normals(
|
|
const indexed_triangle_set &its)
|
|
{
|
|
std::vector<Vec3f> normals;
|
|
normals.reserve(its.indices.size());
|
|
for (const auto &index : its.indices) {
|
|
normals.push_back(create_triangle_normal(index, its.vertices));
|
|
}
|
|
return normals;
|
|
}
|
|
|
|
NormalUtils::Normals NormalUtils::create_normals_average_neighbor(
|
|
const indexed_triangle_set &its)
|
|
{
|
|
size_t count_vertices = its.vertices.size();
|
|
std::vector<Vec3f> normals(count_vertices, Vec3f(.0f, .0f, .0f));
|
|
std::vector<unsigned int> count(count_vertices, 0);
|
|
for (const auto &indice : its.indices) {
|
|
Vec3f normal = create_triangle_normal(indice, its.vertices);
|
|
for (int i = 0; i < 3; ++i) {
|
|
normals[indice[i]] += normal;
|
|
++count[indice[i]];
|
|
}
|
|
}
|
|
// normalize to size 1
|
|
for (auto &normal : normals) {
|
|
size_t index = &normal - &normals.front();
|
|
normal /= static_cast<float>(count[index]);
|
|
}
|
|
return normals;
|
|
}
|
|
|
|
// calc triangle angle of vertex defined by index to triangle indices
|
|
float NormalUtils::indice_angle(int i,
|
|
const Vec3i32 & indice,
|
|
const std::vector<stl_vertex> &vertices)
|
|
{
|
|
int i1 = (i == 0) ? 2 : (i - 1);
|
|
int i2 = (i == 2) ? 0 : (i + 1);
|
|
|
|
Vec3f v1 = vertices[i1] - vertices[i];
|
|
Vec3f v2 = vertices[i2] - vertices[i];
|
|
|
|
v1.normalize();
|
|
v2.normalize();
|
|
|
|
float w = v1.dot(v2);
|
|
if (w > 1.f)
|
|
w = 1.f;
|
|
else if (w < -1.f)
|
|
w = -1.f;
|
|
return acos(w);
|
|
}
|
|
|
|
NormalUtils::Normals NormalUtils::create_normals_angle_weighted(
|
|
const indexed_triangle_set &its)
|
|
{
|
|
size_t count_vertices = its.vertices.size();
|
|
std::vector<Vec3f> normals(count_vertices, Vec3f(.0f, .0f, .0f));
|
|
std::vector<float> count(count_vertices, 0.f);
|
|
for (const auto &indice : its.indices) {
|
|
Vec3f normal = create_triangle_normal(indice, its.vertices);
|
|
Vec3f angles(indice_angle(0, indice, its.vertices),
|
|
indice_angle(1, indice, its.vertices), 0.f);
|
|
angles[2] = (M_PI - angles[0] - angles[1]);
|
|
for (int i = 0; i < 3; ++i) {
|
|
const float &weight = angles[i];
|
|
normals[indice[i]] += normal * weight;
|
|
count[indice[i]] += weight;
|
|
}
|
|
}
|
|
// normalize to size 1
|
|
for (auto &normal : normals) {
|
|
size_t index = &normal - &normals.front();
|
|
normal /= count[index];
|
|
}
|
|
return normals;
|
|
}
|
|
|
|
NormalUtils::Normals NormalUtils::create_normals_nelson_weighted(
|
|
const indexed_triangle_set &its)
|
|
{
|
|
size_t count_vertices = its.vertices.size();
|
|
std::vector<Vec3f> normals(count_vertices, Vec3f(.0f, .0f, .0f));
|
|
std::vector<float> count(count_vertices, 0.f);
|
|
const std::vector<stl_vertex> &vertices = its.vertices;
|
|
for (const auto &indice : its.indices) {
|
|
Vec3f normal = create_triangle_normal(indice, vertices);
|
|
|
|
const stl_vertex &v0 = vertices[indice[0]];
|
|
const stl_vertex &v1 = vertices[indice[1]];
|
|
const stl_vertex &v2 = vertices[indice[2]];
|
|
|
|
float e0 = (v0 - v1).norm();
|
|
float e1 = (v1 - v2).norm();
|
|
float e2 = (v2 - v0).norm();
|
|
|
|
Vec3f coefs(e0 * e2, e0 * e1, e1 * e2);
|
|
for (int i = 0; i < 3; ++i) {
|
|
const float &weight = coefs[i];
|
|
normals[indice[i]] += normal * weight;
|
|
count[indice[i]] += weight;
|
|
}
|
|
}
|
|
// normalize to size 1
|
|
for (auto &normal : normals) {
|
|
size_t index = &normal - &normals.front();
|
|
normal /= count[index];
|
|
}
|
|
return normals;
|
|
}
|
|
|
|
// calculate normals by averaging normals of neghbor triangles
|
|
std::vector<Vec3f> NormalUtils::create_normals(
|
|
const indexed_triangle_set &its, VertexNormalType type)
|
|
{
|
|
switch (type) {
|
|
case VertexNormalType::AverageNeighbor:
|
|
return create_normals_average_neighbor(its);
|
|
case VertexNormalType::AngleWeighted:
|
|
return create_normals_angle_weighted(its);
|
|
case VertexNormalType::NelsonMaxWeighted:
|
|
default:
|
|
return create_normals_nelson_weighted(its);
|
|
}
|
|
}
|