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_MATH_TRANSFORM_H
24#define VCL_MATH_TRANSFORM_H
25
26#include "base.h"
27
28#include <vclib/concepts/space/matrix.h>
29#include <vclib/concepts/space/point.h>
30
31namespace vcl {
32
48template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType>
50 MatrixType& matrix,
51 PointType axis,
52 const ScalarType& angleRad)
53{
54 ScalarType c = std::cos(angleRad);
55 ScalarType s = std::sin(angleRad);
56 ScalarType q = 1 - c;
57 axis.normalize();
58 matrix(0, 0) = axis[0] * axis[0] * q + c;
59 matrix(0, 1) = axis[0] * axis[1] * q - axis[2] * s;
60 matrix(0, 2) = axis[0] * axis[2] * q + axis[1] * s;
61 matrix(1, 0) = axis[1] * axis[0] * q + axis[2] * s;
62 matrix(1, 1) = axis[1] * axis[1] * q + c;
63 matrix(1, 2) = axis[1] * axis[2] * q - axis[0] * s;
64 matrix(2, 0) = axis[2] * axis[0] * q - axis[1] * s;
65 matrix(2, 1) = axis[2] * axis[1] * q + axis[0] * s;
66 matrix(2, 2) = axis[2] * axis[2] * q + c;
67}
68
84template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType>
86 MatrixType& matrix,
87 PointType axis,
88 const ScalarType& angleDeg)
89{
91}
92
108template<MatrixConcept MatrixType, Point3Concept PointType>
110 MatrixType& matrix,
111 const PointType& fromVector,
112 const PointType& toVector)
113{
114 if (fromVector == toVector) {
115 matrix.block(0, 0, 3, 3).setIdentity();
116 return;
117 }
118 PointType axis = fromVector.cross(toVector);
119 auto angle = std::acos(
120 fromVector.dot(toVector) / (fromVector.norm() * toVector.norm()));
122}
123
124template<MatrixConcept MatrixType, Point3Concept PointType>
125void setTransformMatrixTranslation(
126 MatrixType& matrix,
127 const PointType& translation)
128{
129 matrix(0, 3) = translation[0];
130 matrix(1, 3) = translation[1];
131 matrix(2, 3) = translation[2];
132}
133
134template<MatrixConcept MatrixType, typename ScalarType>
135void setTransformMatrixScale(MatrixType& matrix, const ScalarType& scale)
136{
137 matrix(0, 0) = scale;
138 matrix(1, 1) = scale;
139 matrix(2, 2) = scale;
140 matrix(3, 3) = 1;
141}
142
143template<MatrixConcept MatrixType, Point3Concept PointType>
144void setTransformMatrixScale(MatrixType& matrix, const PointType& scale)
145{
146 matrix(0, 0) = scale[0];
147 matrix(1, 1) = scale[1];
148 matrix(2, 2) = scale[2];
149 matrix(3, 3) = 1;
150}
151
167template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType>
168MatrixType rotationMatrix(const PointType& axis, const ScalarType& angleRad)
169{
170 MatrixType matrix;
171 matrix.setIdentity();
173 return matrix;
174}
175
192template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType>
193MatrixType rotationMatrixDeg(const PointType& axis, const ScalarType& angleDeg)
194{
196}
197
213template<MatrixConcept MatrixType, Point3Concept PointType>
214MatrixType rotationMatrix(
215 const PointType& fromVector,
216 const PointType& toVector)
217{
218 MatrixType matrix;
219 matrix.setIdentity();
221 return matrix;
222}
223
224} // namespace vcl
225
226#endif // VCL_MATH_TRANSFORM_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
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
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:193
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:85
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:168