Visual Computing Library
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 "normal.h"
27
28#include <vclib/math/transform.h>
29#include <vclib/mesh/requirements.h>
30
31namespace vcl {
32
33template<MeshConcept MeshType, typename ScalarM>
34void applyTransformMatrix(
35 MeshType& mesh,
36 const Matrix44<ScalarM>& matrix,
37 bool updateNormals = true)
38{
39 using VertexType = MeshType::VertexType;
40 for (VertexType& v : mesh.vertices()) {
41 v.coord() *= matrix;
42 }
43 if (updateNormals) {
44 if constexpr (HasPerVertexNormal<MeshType>) {
45 if (isPerVertexNormalAvailable(mesh)) {
46 multiplyPerVertexNormalsByMatrix(mesh, matrix);
47 }
48 }
49 if constexpr (HasPerFaceNormal<MeshType>) {
50 if (isPerFaceNormalAvailable(mesh)) {
51 multiplyPerFaceNormalsByMatrix(mesh, matrix);
52 }
53 }
54 }
55}
56
57template<MeshConcept MeshType, PointConcept PointType>
58void translate(MeshType& mesh, const PointType& t)
59{
60 using VertexType = MeshType::VertexType;
61 for (VertexType& v : mesh.vertices()) {
62 v.coord() += t;
63 }
64}
65
66template<MeshConcept MeshType, PointConcept PointType>
67void scale(MeshType& mesh, const PointType& s)
68{
69 using VertexType = MeshType::VertexType;
70 for (VertexType& v : mesh.vertices()) {
71 v.coord()(0) *= s(0);
72 v.coord()(1) *= s(1);
73 v.coord()(2) *= s(2);
74 }
75}
76
77template<MeshConcept MeshType, typename Scalar = double>
78void scale(MeshType& mesh, const Scalar& s)
79{
80 using VertexType = MeshType::VertexType;
81 for (VertexType& v : mesh.vertices()) {
82 v.coord() *= s;
83 }
84}
85
86template<MeshConcept MeshType, typename Scalar>
87void rotate(
88 MeshType& mesh,
89 const Matrix33<Scalar>& m,
90 bool updateNormals = true)
91{
92 for (auto& v : mesh.vertices()) {
93 v.coord() = m * v.coord();
94 }
95
96 if (updateNormals) {
97 if constexpr (HasPerVertexNormal<MeshType>) {
98 if (isPerVertexNormalAvailable(mesh)) {
99 for (auto& v : mesh.vertices()) {
100 v.normal() = m * v.normal();
101 }
102 }
103 }
104
105 if constexpr (HasPerFaceNormal<MeshType>) {
106 if (isPerFaceNormalAvailable(mesh)) {
107 for (auto& f : mesh.faces()) {
108 f.normal() = m * f.normal();
109 }
110 }
111 }
112 }
113}
114
115template<MeshConcept MeshType, PointConcept PointType, typename Scalar = double>
116void rotate(
117 MeshType& mesh,
118 const PointType& axis,
119 const Scalar& angleRad,
120 bool updateNormals = true)
121{
122 using ScalarType = MeshType::VertexType::CoordType::ScalarType;
123
124 Matrix33<ScalarType> m;
125 setTransformMatrixRotation(m, axis, angleRad);
126
127 rotate(mesh, m, updateNormals);
128}
129
130template<MeshConcept MeshType, PointConcept PointType, typename Scalar = double>
131void rotateDeg(
132 MeshType& mesh,
133 const PointType& axis,
134 const Scalar& angleDeg,
135 bool updateNormals = true)
136{
137 rotate(mesh, axis, toRad(angleDeg), updateNormals);
138}
139
140} // namespace vcl
141
142#endif // VCL_ALGORITHMS_MESH_UPDATE_TRANSFORM_H
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:330
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:49
Scalar toRad(const Scalar &deg)
Converts an angle in degrees to radians.
Definition base.h:83
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:52
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:60