Visual Computing Library  devel
Loading...
Searching...
No Matches
transform.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_ALGORITHMS_MESH_UPDATE_TRANSFORM_H
24#define VCL_ALGORITHMS_MESH_UPDATE_TRANSFORM_H
25
26#include <vclib/mesh.h>
27#include <vclib/space/core.h>
28
29namespace vcl {
30
31template<MeshConcept MeshType, typename ScalarM>
32void applyTransformMatrix(
33 MeshType& mesh,
34 const Matrix44<ScalarM>& matrix,
35 bool updateNormals = true)
36{
37 using VertexType = MeshType::VertexType;
38
39 multiplyPointsByMatrix(mesh.vertices() | vcl::views::positions, matrix);
40
41 // TODO: automatize: for each element, check if it has normal and apply
42 // the matrix to it
43 if (updateNormals) {
44 if constexpr (HasPerVertexNormal<MeshType>) {
45 if (isPerVertexNormalAvailable(mesh)) {
46 multiplyNormalsByMatrix(
47 mesh.vertices() | vcl::views::normals, matrix);
48 }
49 }
50 if constexpr (HasPerFaceNormal<MeshType>) {
51 if (isPerFaceNormalAvailable(mesh)) {
52 multiplyNormalsByMatrix(
53 mesh.faces() | vcl::views::normals, matrix);
54 }
55 }
56 }
57}
58
59template<MeshConcept MeshType, PointConcept PointType>
60void translate(MeshType& mesh, const PointType& t)
61{
62 using VertexType = MeshType::VertexType;
63 for (VertexType& v : mesh.vertices()) {
64 v.position() += t;
65 }
66}
67
68template<MeshConcept MeshType, PointConcept PointType>
69void scale(MeshType& mesh, const PointType& s)
70{
71 using VertexType = MeshType::VertexType;
72 for (VertexType& v : mesh.vertices()) {
73 v.position()(0) *= s(0);
74 v.position()(1) *= s(1);
75 v.position()(2) *= s(2);
76 }
77}
78
79template<MeshConcept MeshType, typename Scalar = double>
80void scale(MeshType& mesh, const Scalar& s)
81{
82 using VertexType = MeshType::VertexType;
83 for (VertexType& v : mesh.vertices()) {
84 v.position() *= s;
85 }
86}
87
88template<MeshConcept MeshType, typename Scalar>
89void rotate(
90 MeshType& mesh,
91 const Matrix33<Scalar>& m,
92 bool updateNormals = true)
93{
94 for (auto& v : mesh.vertices()) {
95 v.position() = m * v.position();
96 }
97
98 if (updateNormals) {
99 if constexpr (HasPerVertexNormal<MeshType>) {
100 if (isPerVertexNormalAvailable(mesh)) {
101 for (auto& v : mesh.vertices()) {
102 v.normal() = m * v.normal();
103 }
104 }
105 }
106
107 if constexpr (HasPerFaceNormal<MeshType>) {
108 if (isPerFaceNormalAvailable(mesh)) {
109 for (auto& f : mesh.faces()) {
110 f.normal() = m * f.normal();
111 }
112 }
113 }
114 }
115}
116
117template<MeshConcept MeshType, PointConcept PointType, typename Scalar = double>
118void rotate(
119 MeshType& mesh,
120 const PointType& axis,
121 const Scalar& angleRad,
122 bool updateNormals = true)
123{
124 using ScalarType = MeshType::VertexType::PositionType::ScalarType;
125
126 Matrix33<ScalarType> m;
127 setTransformMatrixRotation(m, axis, angleRad);
128
129 rotate(mesh, m, updateNormals);
130}
131
132template<MeshConcept MeshType, PointConcept PointType, typename Scalar = double>
133void rotateDeg(
134 MeshType& mesh,
135 const PointType& axis,
136 const Scalar& angleDeg,
137 bool updateNormals = true)
138{
139 rotate(mesh, axis, toRad(angleDeg), updateNormals);
140}
141
142} // namespace vcl
143
144#endif // VCL_ALGORITHMS_MESH_UPDATE_TRANSFORM_H
void setTransformMatrixRotation(MatrixType &matrix, PointType axis, const ScalarType &angleRad)
Given an 3D axis and an angle expressed in radiants, fills the given matrix with a transform matrix t...
Definition transform.h:48
Scalar toRad(const Scalar &deg)
Converts an angle in degrees to radians.
Definition math.h:81
bool isPerFaceNormalAvailable(const MeshType &m)
Returns true if the Normal component is available (enabled) in the Face element of the input mesh m.
Definition face_requirements.h:592
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:84
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:92