OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::NormalUtils Class Reference

Collection of static function to create normals. More...

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/NormalUtils.hpp>

Public Types

enum class  VertexNormalType { AverageNeighbor , AngleWeighted , NelsonMaxWeighted }
using Normal = Vec3f
using Normals = std::vector<Normal>

Public Member Functions

 NormalUtils ()=delete

Static Public Member Functions

static Normal create_triangle_normal (const stl_triangle_vertex_indices &indices, const std::vector< stl_vertex > &vertices)
 Create normal for triangle defined by indices from vertices.
static Normals create_triangle_normals (const indexed_triangle_set &its)
 Create normals for each vertices.
static Normals create_normals (const indexed_triangle_set &its, VertexNormalType type=VertexNormalType::NelsonMaxWeighted)
 Create normals for each vertex by averaging neighbor triangles normal.
static Normals create_normals_average_neighbor (const indexed_triangle_set &its)
static Normals create_normals_angle_weighted (const indexed_triangle_set &its)
static Normals create_normals_nelson_weighted (const indexed_triangle_set &its)
static float indice_angle (int i, const Vec3i32 &indice, const std::vector< stl_vertex > &vertices)
 Calculate angle of trinagle side.

Detailed Description

Collection of static function to create normals.

Member Typedef Documentation

◆ Normal

◆ Normals

using Slic3r::NormalUtils::Normals = std::vector<Normal>

Member Enumeration Documentation

◆ VertexNormalType

Enumerator
AverageNeighbor 
AngleWeighted 
NelsonMaxWeighted 
21 {
22 AverageNeighbor,
23 AngleWeighted,
24 NelsonMaxWeighted
25 };

Constructor & Destructor Documentation

◆ NormalUtils()

Slic3r::NormalUtils::NormalUtils ( )
delete

Member Function Documentation

◆ create_normals()

std::vector< Vec3f > NormalUtils::create_normals ( const indexed_triangle_set & its,
VertexNormalType type = VertexNormalType::NelsonMaxWeighted )
static

Create normals for each vertex by averaging neighbor triangles normal.

Parameters
itsTriangle indices and vertices
typeType of calculation normals
Returns
Normal for each vertex
137{
138 switch (type) {
144 default:
146 }
147}
static Normals create_normals_angle_weighted(const indexed_triangle_set &its)
Definition NormalUtils.cpp:76
static Normals create_normals_average_neighbor(const indexed_triangle_set &its)
Definition NormalUtils.cpp:33
static Normals create_normals_nelson_weighted(const indexed_triangle_set &its)
Definition NormalUtils.cpp:101
@ AngleWeighted
Definition NormalUtils.hpp:23
@ NelsonMaxWeighted
Definition NormalUtils.hpp:24
@ AverageNeighbor
Definition NormalUtils.hpp:22

References AngleWeighted, AverageNeighbor, create_normals_angle_weighted(), create_normals_average_neighbor(), create_normals_nelson_weighted(), and NelsonMaxWeighted.

Referenced by Slic3r::its_short_edge_collpase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_normals_angle_weighted()

NormalUtils::Normals NormalUtils::create_normals_angle_weighted ( const indexed_triangle_set & its)
static
78{
79 size_t count_vertices = its.vertices.size();
80 std::vector<Vec3f> normals(count_vertices, Vec3f(.0f, .0f, .0f));
81 std::vector<float> count(count_vertices, 0.f);
82 for (const auto &indice : its.indices) {
83 Vec3f normal = create_triangle_normal(indice, its.vertices);
84 Vec3f angles(indice_angle(0, indice, its.vertices),
85 indice_angle(1, indice, its.vertices), 0.f);
86 angles[2] = (M_PI - angles[0] - angles[1]);
87 for (int i = 0; i < 3; ++i) {
88 const float &weight = angles[i];
89 normals[indice[i]] += normal * weight;
90 count[indice[i]] += weight;
91 }
92 }
93 // normalize to size 1
94 for (auto &normal : normals) {
95 size_t index = &normal - &normals.front();
96 normal /= count[index];
97 }
98 return normals;
99}
static unsigned int count
Definition AuxiliaryDataViewModel.cpp:246
#define M_PI
Definition ExtrusionSimulator.cpp:31
static Normal create_triangle_normal(const stl_triangle_vertex_indices &indices, const std::vector< stl_vertex > &vertices)
Create normal for triangle defined by indices from vertices.
Definition NormalUtils.cpp:10
static float indice_angle(int i, const Vec3i32 &indice, const std::vector< stl_vertex > &vertices)
Calculate angle of trinagle side.
Definition NormalUtils.cpp:55
std::vector< Vec2d > normals(const Contour &c, const Vec2d &p)
Definition SeamContour.cpp:63
Eigen::Matrix< float, 3, 1, Eigen::DontAlign > Vec3f
Definition Point.hpp:65
Point normal(Point pt, double scale)
Definition TreeSupport.cpp:112

References count, create_triangle_normal(), indice_angle(), M_PI, and Slic3r::normal().

Referenced by create_normals().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_normals_average_neighbor()

NormalUtils::Normals NormalUtils::create_normals_average_neighbor ( const indexed_triangle_set & its)
static
35{
36 size_t count_vertices = its.vertices.size();
37 std::vector<Vec3f> normals(count_vertices, Vec3f(.0f, .0f, .0f));
38 std::vector<unsigned int> count(count_vertices, 0);
39 for (const auto &indice : its.indices) {
40 Vec3f normal = create_triangle_normal(indice, its.vertices);
41 for (int i = 0; i < 3; ++i) {
42 normals[indice[i]] += normal;
43 ++count[indice[i]];
44 }
45 }
46 // normalize to size 1
47 for (auto &normal : normals) {
48 size_t index = &normal - &normals.front();
49 normal /= static_cast<float>(count[index]);
50 }
51 return normals;
52}

References count, create_triangle_normal(), and Slic3r::normal().

Referenced by create_normals().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_normals_nelson_weighted()

NormalUtils::Normals NormalUtils::create_normals_nelson_weighted ( const indexed_triangle_set & its)
static
103{
104 size_t count_vertices = its.vertices.size();
105 std::vector<Vec3f> normals(count_vertices, Vec3f(.0f, .0f, .0f));
106 std::vector<float> count(count_vertices, 0.f);
107 const std::vector<stl_vertex> &vertices = its.vertices;
108 for (const auto &indice : its.indices) {
109 Vec3f normal = create_triangle_normal(indice, vertices);
110
111 const stl_vertex &v0 = vertices[indice[0]];
112 const stl_vertex &v1 = vertices[indice[1]];
113 const stl_vertex &v2 = vertices[indice[2]];
114
115 float e0 = (v0 - v1).norm();
116 float e1 = (v1 - v2).norm();
117 float e2 = (v2 - v0).norm();
118
119 Vec3f coefs(e0 * e2, e0 * e1, e1 * e2);
120 for (int i = 0; i < 3; ++i) {
121 const float &weight = coefs[i];
122 normals[indice[i]] += normal * weight;
123 count[indice[i]] += weight;
124 }
125 }
126 // normalize to size 1
127 for (auto &normal : normals) {
128 size_t index = &normal - &normals.front();
129 normal /= count[index];
130 }
131 return normals;
132}

References count, create_triangle_normal(), and Slic3r::normal().

Referenced by create_normals().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_triangle_normal()

Vec3f NormalUtils::create_triangle_normal ( const stl_triangle_vertex_indices & indices,
const std::vector< stl_vertex > & vertices )
static

Create normal for triangle defined by indices from vertices.

Parameters
indicesindex into vertices
verticesvector of vertices
Returns
normal to triangle(normalized to size 1)
13{
14 const stl_vertex &v0 = vertices[indices[0]];
15 const stl_vertex &v1 = vertices[indices[1]];
16 const stl_vertex &v2 = vertices[indices[2]];
17 Vec3f direction = (v1 - v0).cross(v2 - v0);
18 direction.normalize();
19 return direction;
20}
T cross(const boost::geometry::model::d2::point_xy< T > &v1, const boost::geometry::model::d2::point_xy< T > &v2)
Definition ExtrusionSimulator.cpp:168

References Slic3r::cross().

Referenced by create_normals_angle_weighted(), create_normals_average_neighbor(), create_normals_nelson_weighted(), and create_triangle_normals().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_triangle_normals()

std::vector< Vec3f > NormalUtils::create_triangle_normals ( const indexed_triangle_set & its)
static

Create normals for each vertices.

Parameters
itsindices and vertices
Returns
Vector of normals
24{
25 std::vector<Vec3f> normals;
26 normals.reserve(its.indices.size());
27 for (const auto &index : its.indices) {
28 normals.push_back(create_triangle_normal(index, its.vertices));
29 }
30 return normals;
31}

References create_triangle_normal().

Here is the call graph for this function:

◆ indice_angle()

float NormalUtils::indice_angle ( int i,
const Vec3i32 & indice,
const std::vector< stl_vertex > & vertices )
static

Calculate angle of trinagle side.

Parameters
iindex to indices, define angle point
indiceaddress to vertices
verticesvertices data
Returns
Angle [in radian]
58{
59 int i1 = (i == 0) ? 2 : (i - 1);
60 int i2 = (i == 2) ? 0 : (i + 1);
61
62 Vec3f v1 = vertices[i1] - vertices[i];
63 Vec3f v2 = vertices[i2] - vertices[i];
64
65 v1.normalize();
66 v2.normalize();
67
68 float w = v1.dot(v2);
69 if (w > 1.f)
70 w = 1.f;
71 else if (w < -1.f)
72 w = -1.f;
73 return acos(w);
74}

Referenced by create_normals_angle_weighted().

Here is the caller graph for this function:

The documentation for this class was generated from the following files: