Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Slic3r::NormalUtils Class Reference

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

#include <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 Vec3crd &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 

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
132{
133 switch (type) {
139 default:
141 }
142}
static Normals create_normals_angle_weighted(const indexed_triangle_set &its)
Definition NormalUtils.cpp:71
static Normals create_normals_average_neighbor(const indexed_triangle_set &its)
Definition NormalUtils.cpp:28
static Normals create_normals_nelson_weighted(const indexed_triangle_set &its)
Definition NormalUtils.cpp:96

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
73{
74 size_t count_vertices = its.vertices.size();
75 std::vector<Vec3f> normals(count_vertices, Vec3f(.0f, .0f, .0f));
76 std::vector<float> count(count_vertices, 0.f);
77 for (const Vec3crd &indice : its.indices) {
79 Vec3f angles(indice_angle(0, indice, its.vertices),
80 indice_angle(1, indice, its.vertices), 0.f);
81 angles[2] = (M_PI - angles[0] - angles[1]);
82 for (int i = 0; i < 3; ++i) {
83 const float &weight = angles[i];
84 normals[indice[i]] += normal * weight;
85 count[indice[i]] += weight;
86 }
87 }
88 // normalize to size 1
89 for (auto &normal : normals) {
90 size_t index = &normal - &normals.front();
91 normal /= count[index];
92 }
93 return normals;
94}
#define M_PI
Definition ExtrusionSimulator.cpp:20
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:5
static float indice_angle(int i, const Vec3crd &indice, const std::vector< stl_vertex > &vertices)
Calculate angle of trinagle side.
Definition NormalUtils.cpp:50
Vec< 3, T > normal(const std::array< Vec< 3, T >, 3 > &tri)
Definition Rotfinder.cpp:43
Eigen::MatrixXd normals(Ex ex_policy, const PointSet &points, const AABBMesh &mesh, double eps, std::function< void()> thr, const std::vector< unsigned > &pt_indices)
Definition MeshNormals.cpp:116
Eigen::Matrix< float, 3, 1, Eigen::DontAlign > Vec3f
Definition Point.hpp:49
IGL_INLINE void count(const Eigen::SparseMatrix< XType > &X, const int dim, Eigen::SparseVector< SType > &S)
Definition count.cpp:12
std::vector< stl_vertex > vertices
Definition stl.h:165

References create_triangle_normal(), indice_angle(), indexed_triangle_set::indices, M_PI, Slic3r::normals(), and indexed_triangle_set::vertices.

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
30{
31 size_t count_vertices = its.vertices.size();
32 std::vector<Vec3f> normals(count_vertices, Vec3f(.0f, .0f, .0f));
33 std::vector<unsigned int> count(count_vertices, 0);
34 for (const Vec3crd &indice : its.indices) {
36 for (int i = 0; i < 3; ++i) {
37 normals[indice[i]] += normal;
38 ++count[indice[i]];
39 }
40 }
41 // normalize to size 1
42 for (auto &normal : normals) {
43 size_t index = &normal - &normals.front();
44 normal /= static_cast<float>(count[index]);
45 }
46 return normals;
47}

References create_triangle_normal(), indexed_triangle_set::indices, Slic3r::normals(), and indexed_triangle_set::vertices.

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
98{
99 size_t count_vertices = its.vertices.size();
100 std::vector<Vec3f> normals(count_vertices, Vec3f(.0f, .0f, .0f));
101 std::vector<float> count(count_vertices, 0.f);
102 const std::vector<stl_vertex> &vertices = its.vertices;
103 for (const Vec3crd &indice : its.indices) {
104 Vec3f normal = create_triangle_normal(indice, vertices);
105
106 const stl_vertex &v0 = vertices[indice[0]];
107 const stl_vertex &v1 = vertices[indice[1]];
108 const stl_vertex &v2 = vertices[indice[2]];
109
110 float e0 = (v0 - v1).norm();
111 float e1 = (v1 - v2).norm();
112 float e2 = (v2 - v0).norm();
113
114 Vec3f coefs(e0 * e2, e0 * e1, e1 * e2);
115 for (int i = 0; i < 3; ++i) {
116 const float &weight = coefs[i];
117 normals[indice[i]] += normal * weight;
118 count[indice[i]] += weight;
119 }
120 }
121 // normalize to size 1
122 for (auto &normal : normals) {
123 size_t index = &normal - &normals.front();
124 normal /= count[index];
125 }
126 return normals;
127}

References create_triangle_normal(), indexed_triangle_set::indices, Slic3r::normals(), and indexed_triangle_set::vertices.

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)
8{
9 const stl_vertex &v0 = vertices[indices[0]];
10 const stl_vertex &v1 = vertices[indices[1]];
11 const stl_vertex &v2 = vertices[indices[2]];
12 Vec3f direction = (v1 - v0).cross(v2 - v0);
13 direction.normalize();
14 return direction;
15}
T cross(const boost::geometry::model::d2::point_xy< T > &v1, const boost::geometry::model::d2::point_xy< T > &v2)
Definition ExtrusionSimulator.cpp:157

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
19{
20 std::vector<Vec3f> normals;
21 normals.reserve(its.indices.size());
22 for (const Vec3crd &index : its.indices) {
23 normals.push_back(create_triangle_normal(index, its.vertices));
24 }
25 return normals;
26}
std::vector< stl_triangle_vertex_indices > indices
Definition stl.h:164

References create_triangle_normal(), indexed_triangle_set::indices, Slic3r::normals(), and indexed_triangle_set::vertices.

+ Here is the call graph for this function:

◆ indice_angle()

float NormalUtils::indice_angle ( int  i,
const Vec3crd 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]
53{
54 int i1 = (i == 0) ? 2 : (i - 1);
55 int i2 = (i == 2) ? 0 : (i + 1);
56
57 Vec3f v1 = vertices[i1] - vertices[i];
58 Vec3f v2 = vertices[i2] - vertices[i];
59
60 v1.normalize();
61 v2.normalize();
62
63 float w = v1.dot(v2);
64 if (w > 1.f)
65 w = 1.f;
66 else if (w < -1.f)
67 w = -1.f;
68 return acos(w);
69}
EIGEN_DEVICE_FUNC const AcosReturnType acos() const
Definition ArrayCwiseUnaryOps.h:262

References acos().

Referenced by create_normals_angle_weighted().

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

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