VTK-m  2.0
Transform3D.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_Transform3D_h
11 #define vtk_m_Transform3D_h
12 
13 // This header file contains a collection of math functions useful in the
14 // linear transformation of homogeneous points for rendering in 3D.
15 
16 #include <vtkm/Matrix.h>
17 #include <vtkm/VectorAnalysis.h>
18 
19 namespace vtkm
20 {
21 
33 template <typename T>
35  const vtkm::Vec<T, 3>& point)
36 {
37  vtkm::Vec<T, 4> homogeneousPoint(point[0], point[1], point[2], T(1));
38  return vtkm::Vec<T, 3>(vtkm::Dot(vtkm::MatrixGetRow(matrix, 0), homogeneousPoint),
39  vtkm::Dot(vtkm::MatrixGetRow(matrix, 1), homogeneousPoint),
40  vtkm::Dot(vtkm::MatrixGetRow(matrix, 2), homogeneousPoint));
41 }
42 
52 template <typename T>
54  const vtkm::Vec<T, 3>& point)
55 {
56  vtkm::Vec<T, 4> homogeneousPoint(point[0], point[1], point[2], T(1));
57  T inverseW = 1 / vtkm::Dot(vtkm::MatrixGetRow(matrix, 3), homogeneousPoint);
58  return vtkm::Vec<T, 3>(vtkm::Dot(vtkm::MatrixGetRow(matrix, 0), homogeneousPoint) * inverseW,
59  vtkm::Dot(vtkm::MatrixGetRow(matrix, 1), homogeneousPoint) * inverseW,
60  vtkm::Dot(vtkm::MatrixGetRow(matrix, 2), homogeneousPoint) * inverseW);
61 }
62 
69 template <typename T>
71  const vtkm::Vec<T, 3>& vector)
72 {
73  vtkm::Vec<T, 4> homogeneousVector(vector[0], vector[1], vector[2], T(0));
74  homogeneousVector = vtkm::MatrixMultiply(matrix, homogeneousVector);
75  return vtkm::Vec<T, 3>(homogeneousVector[0], homogeneousVector[1], homogeneousVector[2]);
76 }
77 
83 template <typename T>
85  const T& scaleY,
86  const T& scaleZ)
87 {
88  vtkm::Matrix<T, 4, 4> scaleMatrix(T(0));
89  scaleMatrix(0, 0) = scaleX;
90  scaleMatrix(1, 1) = scaleY;
91  scaleMatrix(2, 2) = scaleZ;
92  scaleMatrix(3, 3) = T(1);
93  return scaleMatrix;
94 }
95 
101 template <typename T>
103 {
104  return vtkm::Transform3DScale(scaleVec[0], scaleVec[1], scaleVec[2]);
105 }
106 
112 template <typename T>
114 {
115  return vtkm::Transform3DScale(scale, scale, scale);
116 }
117 
120 template <typename T>
121 VTKM_EXEC_CONT vtkm::Matrix<T, 4, 4> Transform3DTranslate(const T& x, const T& y, const T& z)
122 {
123  vtkm::Matrix<T, 4, 4> translateMatrix;
124  vtkm::MatrixIdentity(translateMatrix);
125  translateMatrix(0, 3) = x;
126  translateMatrix(1, 3) = y;
127  translateMatrix(2, 3) = z;
128  return translateMatrix;
129 }
130 template <typename T>
132 {
133  return vtkm::Transform3DTranslate(v[0], v[1], v[2]);
134 }
135 
143 template <typename T>
145  const vtkm::Vec<T, 3>& axisOfRotation)
146 {
147  T angleRadians = vtkm::Pi_180<T>() * angleDegrees;
148  const vtkm::Vec<T, 3> normAxis = vtkm::Normal(axisOfRotation);
149  T sinAngle = vtkm::Sin(angleRadians);
150  T cosAngle = vtkm::Cos(angleRadians);
151 
152  vtkm::Matrix<T, 4, 4> matrix;
153 
154  matrix(0, 0) = normAxis[0] * normAxis[0] * (1 - cosAngle) + cosAngle;
155  matrix(0, 1) = normAxis[0] * normAxis[1] * (1 - cosAngle) - normAxis[2] * sinAngle;
156  matrix(0, 2) = normAxis[0] * normAxis[2] * (1 - cosAngle) + normAxis[1] * sinAngle;
157  matrix(0, 3) = T(0);
158 
159  matrix(1, 0) = normAxis[1] * normAxis[0] * (1 - cosAngle) + normAxis[2] * sinAngle;
160  matrix(1, 1) = normAxis[1] * normAxis[1] * (1 - cosAngle) + cosAngle;
161  matrix(1, 2) = normAxis[1] * normAxis[2] * (1 - cosAngle) - normAxis[0] * sinAngle;
162  matrix(1, 3) = T(0);
163 
164  matrix(2, 0) = normAxis[2] * normAxis[0] * (1 - cosAngle) - normAxis[1] * sinAngle;
165  matrix(2, 1) = normAxis[2] * normAxis[1] * (1 - cosAngle) + normAxis[0] * sinAngle;
166  matrix(2, 2) = normAxis[2] * normAxis[2] * (1 - cosAngle) + cosAngle;
167  matrix(2, 3) = T(0);
168 
169  matrix(3, 0) = T(0);
170  matrix(3, 1) = T(0);
171  matrix(3, 2) = T(0);
172  matrix(3, 3) = T(1);
173 
174  return matrix;
175 }
176 template <typename T>
178 {
179  return vtkm::Transform3DRotate(angleDegrees, vtkm::Vec<T, 3>(x, y, z));
180 }
181 
186 template <typename T>
188 {
189  return vtkm::Transform3DRotate(angleDegrees, T(1), T(0), T(0));
190 }
191 
196 template <typename T>
198 {
199  return vtkm::Transform3DRotate(angleDegrees, T(0), T(1), T(0));
200 }
201 
206 template <typename T>
208 {
209  return vtkm::Transform3DRotate(angleDegrees, T(0), T(0), T(1));
210 }
211 
212 } // namespace vtkm
213 
214 #endif //vtk_m_Transform3D_h
vtkm::Transform3DRotateY
VTKM_EXEC_CONT vtkm::Matrix< T, 4, 4 > Transform3DRotateY(T angleDegrees)
Returns a rotation matrix.
Definition: Transform3D.h:197
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Cos
VTKM_EXEC_CONT vtkm::Float32 Cos(vtkm::Float32 x)
Compute the cosine of x.
Definition: Math.h:269
vtkm::Transform3DRotate
VTKM_EXEC_CONT vtkm::Matrix< T, 4, 4 > Transform3DRotate(T angleDegrees, const vtkm::Vec< T, 3 > &axisOfRotation)
Returns a rotation matrix.
Definition: Transform3D.h:144
vtkm::Transform3DPointPerspective
VTKM_EXEC_CONT vtkm::Vec< T, 3 > Transform3DPointPerspective(const vtkm::Matrix< T, 4, 4 > &matrix, const vtkm::Vec< T, 3 > &point)
Transform a 3D point by a transformation matrix with perspective.
Definition: Transform3D.h:53
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Transform3DPoint
VTKM_EXEC_CONT vtkm::Vec< T, 3 > Transform3DPoint(const vtkm::Matrix< T, 4, 4 > &matrix, const vtkm::Vec< T, 3 > &point)
Transform a 3D point by a transformation matrix.
Definition: Transform3D.h:34
Matrix.h
vtkm::Transform3DVector
VTKM_EXEC_CONT vtkm::Vec< T, 3 > Transform3DVector(const vtkm::Matrix< T, 4, 4 > &matrix, const vtkm::Vec< T, 3 > &vector)
Transform a 3D vector by a transformation matrix.
Definition: Transform3D.h:70
vtkm::MatrixIdentity
VTKM_EXEC_CONT vtkm::Matrix< T, Size, Size > MatrixIdentity()
Returns the identity matrix.
Definition: Matrix.h:209
vtkm::Sin
VTKM_EXEC_CONT vtkm::Float32 Sin(vtkm::Float32 x)
Compute the sine of x.
Definition: Math.h:210
vtkm::Transform3DTranslate
VTKM_EXEC_CONT vtkm::Matrix< T, 4, 4 > Transform3DTranslate(const T &x, const T &y, const T &z)
Returns a translation matrix.
Definition: Transform3D.h:121
vtkm::Normal
VTKM_EXEC_CONT T Normal(const T &x)
Returns a normalized version of the given vector.
Definition: VectorAnalysis.h:157
VectorAnalysis.h
vtkm::Vec< T, 4 >
Definition: Types.h:1093
vtkm::MatrixMultiply
VTKM_EXEC_CONT vtkm::Matrix< T, NumRow, NumCol > MatrixMultiply(const vtkm::Matrix< T, NumRow, NumInternal > &leftFactor, const vtkm::Matrix< T, NumInternal, NumCol > &rightFactor)
Standard matrix multiplication.
Definition: Matrix.h:156
vtkm::MatrixGetRow
const VTKM_EXEC_CONT vtkm::Vec< T, NumCol > & MatrixGetRow(const vtkm::Matrix< T, NumRow, NumCol > &matrix, vtkm::IdComponent rowIndex)
Returns a tuple containing the given row (indexed from 0) of the given matrix.
Definition: Matrix.h:105
vtkm::Vec< T, 3 >
Definition: Types.h:975
vtkm::Matrix
Basic Matrix type.
Definition: Matrix.h:33
vtkm::Transform3DRotateZ
VTKM_EXEC_CONT vtkm::Matrix< T, 4, 4 > Transform3DRotateZ(T angleDegrees)
Returns a rotation matrix.
Definition: Transform3D.h:207
vtkm::Transform3DRotateX
VTKM_EXEC_CONT vtkm::Matrix< T, 4, 4 > Transform3DRotateX(T angleDegrees)
Returns a rotation matrix.
Definition: Transform3D.h:187
vtkm::Transform3DScale
VTKM_EXEC_CONT vtkm::Matrix< T, 4, 4 > Transform3DScale(const T &scaleX, const T &scaleY, const T &scaleZ)
Returns a scale matrix.
Definition: Transform3D.h:84