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_CORE_TRANSFORM_H
24#define VCL_ALGORITHMS_CORE_TRANSFORM_H
25
26#include <vclib/space/core.h>
27
28#include <cmath>
29
30namespace vcl {
31
47template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType>
49 MatrixType& matrix,
50 PointType axis,
51 const ScalarType& angleRad)
52{
53 ScalarType c = std::cos(angleRad);
54 ScalarType s = std::sin(angleRad);
55 ScalarType q = 1 - c;
56 axis.normalize();
57 matrix(0, 0) = axis[0] * axis[0] * q + c;
58 matrix(0, 1) = axis[0] * axis[1] * q - axis[2] * s;
59 matrix(0, 2) = axis[0] * axis[2] * q + axis[1] * s;
60 matrix(1, 0) = axis[1] * axis[0] * q + axis[2] * s;
61 matrix(1, 1) = axis[1] * axis[1] * q + c;
62 matrix(1, 2) = axis[1] * axis[2] * q - axis[0] * s;
63 matrix(2, 0) = axis[2] * axis[0] * q - axis[1] * s;
64 matrix(2, 1) = axis[2] * axis[1] * q + axis[0] * s;
65 matrix(2, 2) = axis[2] * axis[2] * q + c;
66}
67
83template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType>
85 MatrixType& matrix,
86 PointType axis,
87 const ScalarType& angleDeg)
88{
90}
91
107template<MatrixConcept MatrixType, Point3Concept PointType>
109 MatrixType& matrix,
110 const PointType& fromVector,
111 const PointType& toVector)
112{
113 if (fromVector == toVector) {
114 matrix.block(0, 0, 3, 3).setIdentity();
115 return;
116 }
117 PointType axis = fromVector.cross(toVector);
118 auto angle = std::acos(
119 fromVector.dot(toVector) / (fromVector.norm() * toVector.norm()));
121}
122
123template<MatrixConcept MatrixType, Point3Concept PointType>
124void setTransformMatrixTranslation(
125 MatrixType& matrix,
126 const PointType& translation)
127{
128 matrix(0, 3) = translation[0];
129 matrix(1, 3) = translation[1];
130 matrix(2, 3) = translation[2];
131}
132
133template<MatrixConcept MatrixType, typename ScalarType>
134void setTransformMatrixScale(MatrixType& matrix, const ScalarType& scale)
135{
136 matrix(0, 0) = scale;
137 matrix(1, 1) = scale;
138 matrix(2, 2) = scale;
139 matrix(3, 3) = 1;
140}
141
142template<MatrixConcept MatrixType, Point3Concept PointType>
143void setTransformMatrixScale(MatrixType& matrix, const PointType& scale)
144{
145 matrix(0, 0) = scale[0];
146 matrix(1, 1) = scale[1];
147 matrix(2, 2) = scale[2];
148 matrix(3, 3) = 1;
149}
150
166template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType>
167MatrixType rotationMatrix(const PointType& axis, const ScalarType& angleRad)
168{
169 MatrixType matrix;
170 matrix.setIdentity();
172 return matrix;
173}
174
191template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType>
192MatrixType rotationMatrixDeg(const PointType& axis, const ScalarType& angleDeg)
193{
195}
196
212template<MatrixConcept MatrixType, Point3Concept PointType>
213MatrixType rotationMatrix(
214 const PointType& fromVector,
215 const PointType& toVector)
216{
217 MatrixType matrix;
218 matrix.setIdentity();
220 return matrix;
221}
222
237template<Matrix33Or44Concept MatrixType>
239{
240 using ScalarType = typename MatrixType::Scalar;
241 ScalarType scaleX = std::sqrt(
242 matrix(0, 0) * matrix(0, 0) + matrix(0, 1) * matrix(0, 1) +
243 matrix(0, 2) * matrix(0, 2));
244 ScalarType scaleY = std::sqrt(
245 matrix(1, 0) * matrix(1, 0) + matrix(1, 1) * matrix(1, 1) +
246 matrix(1, 2) * matrix(1, 2));
247 ScalarType scaleZ = std::sqrt(
248 matrix(2, 0) * matrix(2, 0) + matrix(2, 1) * matrix(2, 1) +
249 matrix(2, 2) * matrix(2, 2));
250 for (int i = 0; i < 3; ++i) {
251 matrix(0, i) /= scaleX;
252 matrix(1, i) /= scaleY;
253 matrix(2, i) /= scaleZ;
254 }
255}
256
273template<Matrix33Or44Concept MatrixType>
274MatrixType removeScalingFromMatrix(const MatrixType& matrix)
275{
276 MatrixType result = matrix;
278 return result;
279}
280
281template<Point3Concept PointType, Matrix33Concept MatrixType>
282PointType multiplyNormalByMatrix(
283 const PointType& normal,
284 MatrixType mat,
285 bool removeScalingFromMatrix = true)
286{
287 using ScalarType = typename PointType::Scalar;
290 }
291 return mat.template cast<ScalarType>() * normal;
292}
293
294template<Point3Concept PointType, Matrix44Concept MatrixType>
295PointType multiplyNormalByMatrix(
296 const PointType& normal,
297 MatrixType mat,
298 bool removeScalingFromMatrix = true)
299{
300 using ScalarType = typename PointType::Scalar;
301
302 Matrix33<ScalarType> m33 =
303 mat.template cast<ScalarType>().block(0, 0, 3, 3);
306 }
307 return mat * normal;
308}
309
310template<Range R, Matrix44Concept MatrixType>
311void multiplyPointsByMatrix(R&& points, const MatrixType& mat)
312 requires Point3Concept<std::ranges::range_value_t<R>>
313{
314 using PointType = std::ranges::range_value_t<R>;
315 using ScalarType = PointType::ScalarType;
316
317 Matrix44<ScalarType> m44 = mat.template cast<ScalarType>();
318
319 parallelFor(points, [&](auto& point) {
320 point *= m44;
321 });
322}
323
324template<Range R, Matrix33Concept MatrixType>
325void multiplyNormalsByMatrix(
326 R&& normals,
327 const MatrixType& mat,
328 bool removeScalingFromMatrix = true)
329 requires Point3Concept<std::ranges::range_value_t<R>>
330{
331 using PointType = std::ranges::range_value_t<R>;
332 using ScalarType = PointType::ScalarType;
333
334 Matrix33<ScalarType> m33 = mat.template cast<ScalarType>();
335
338 }
339
340 parallelFor(normals, [&](auto& normal) {
341 normal = m33 * normal;
342 });
343}
344
345template<Range R, Matrix44Concept MatrixType>
346void multiplyNormalsByMatrix(
347 R&& normals,
348 const MatrixType& mat,
349 bool removeScalingFromMatrix = true)
350 requires Point3Concept<std::ranges::range_value_t<R>>
351{
352 using PointType = std::ranges::range_value_t<R>;
353 using ScalarType = PointType::ScalarType;
354
355 Matrix33<ScalarType> m33 =
356 mat.template cast<ScalarType>().block(0, 0, 3, 3);
357
358 multiplyNormalsByMatrix(normals, m33, removeScalingFromMatrix);
359}
360
361} // namespace vcl
362
363#endif // VCL_ALGORITHMS_CORE_TRANSFORM_H
A class representing a box in N-dimensional space.
Definition box.h:46
void removeScalingFromMatrixInPlace(MatrixType &matrix)
Removes the scaling factors from the matrix in place.
Definition transform.h:238
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
MatrixType removeScalingFromMatrix(const MatrixType &matrix)
Returns a matrix with the scaling factors removed.
Definition transform.h:274
MatrixType rotationMatrixDeg(const PointType &axis, const ScalarType &angleDeg)
Given an 3D axis and an angle expressed in degrees, fills the given matrix with a transform matrix th...
Definition transform.h:192
void setTransformMatrixRotationDeg(MatrixType &matrix, PointType axis, const ScalarType &angleDeg)
Given an 3D axis and an angle expressed in degrees, fills the given matrix with a transform matrix th...
Definition transform.h:84
MatrixType rotationMatrix(const PointType &axis, const ScalarType &angleRad)
Given an 3D axis and an angle expressed in radiants, returns a transform matrix that represents the r...
Definition transform.h:167
Scalar toRad(const Scalar &deg)
Converts an angle in degrees to radians.
Definition math.h:81