Viskores  1.0
Transform3D.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_Transform3D_h
19 #define viskores_Transform3D_h
20 
21 // This header file contains a collection of math functions useful in the
22 // linear transformation of homogeneous points for rendering in 3D.
23 
24 #include <viskores/Matrix.h>
26 
27 namespace viskores
28 {
29 
41 template <typename T>
43  const viskores::Vec<T, 3>& point)
44 {
45  viskores::Vec<T, 4> homogeneousPoint(point[0], point[1], point[2], T(1));
46  return viskores::Vec<T, 3>(viskores::Dot(viskores::MatrixGetRow(matrix, 0), homogeneousPoint),
47  viskores::Dot(viskores::MatrixGetRow(matrix, 1), homogeneousPoint),
48  viskores::Dot(viskores::MatrixGetRow(matrix, 2), homogeneousPoint));
49 }
50 
60 template <typename T>
62  const viskores::Matrix<T, 4, 4>& matrix,
63  const viskores::Vec<T, 3>& point)
64 {
65  viskores::Vec<T, 4> homogeneousPoint(point[0], point[1], point[2], T(1));
66  T inverseW = 1 / viskores::Dot(viskores::MatrixGetRow(matrix, 3), homogeneousPoint);
67  return viskores::Vec<T, 3>(
68  viskores::Dot(viskores::MatrixGetRow(matrix, 0), homogeneousPoint) * inverseW,
69  viskores::Dot(viskores::MatrixGetRow(matrix, 1), homogeneousPoint) * inverseW,
70  viskores::Dot(viskores::MatrixGetRow(matrix, 2), homogeneousPoint) * inverseW);
71 }
72 
79 template <typename T>
81  const viskores::Vec<T, 3>& vector)
82 {
83  viskores::Vec<T, 4> homogeneousVector(vector[0], vector[1], vector[2], T(0));
84  homogeneousVector = viskores::MatrixMultiply(matrix, homogeneousVector);
85  return viskores::Vec<T, 3>(homogeneousVector[0], homogeneousVector[1], homogeneousVector[2]);
86 }
87 
93 template <typename T>
95  const T& scaleY,
96  const T& scaleZ)
97 {
98  viskores::Matrix<T, 4, 4> scaleMatrix(T(0));
99  scaleMatrix(0, 0) = scaleX;
100  scaleMatrix(1, 1) = scaleY;
101  scaleMatrix(2, 2) = scaleZ;
102  scaleMatrix(3, 3) = T(1);
103  return scaleMatrix;
104 }
105 
111 template <typename T>
113 {
114  return viskores::Transform3DScale(scaleVec[0], scaleVec[1], scaleVec[2]);
115 }
116 
122 template <typename T>
124 {
125  return viskores::Transform3DScale(scale, scale, scale);
126 }
127 
130 template <typename T>
132  const T& y,
133  const T& z)
134 {
135  viskores::Matrix<T, 4, 4> translateMatrix;
136  viskores::MatrixIdentity(translateMatrix);
137  translateMatrix(0, 3) = x;
138  translateMatrix(1, 3) = y;
139  translateMatrix(2, 3) = z;
140  return translateMatrix;
141 }
142 template <typename T>
144 {
145  return viskores::Transform3DTranslate(v[0], v[1], v[2]);
146 }
147 
155 template <typename T>
157  T angleDegrees,
158  const viskores::Vec<T, 3>& axisOfRotation)
159 {
160  T angleRadians = viskores::Pi_180<T>() * angleDegrees;
161  const viskores::Vec<T, 3> normAxis = viskores::Normal(axisOfRotation);
162  T sinAngle = viskores::Sin(angleRadians);
163  T cosAngle = viskores::Cos(angleRadians);
164 
166 
167  matrix(0, 0) = normAxis[0] * normAxis[0] * (1 - cosAngle) + cosAngle;
168  matrix(0, 1) = normAxis[0] * normAxis[1] * (1 - cosAngle) - normAxis[2] * sinAngle;
169  matrix(0, 2) = normAxis[0] * normAxis[2] * (1 - cosAngle) + normAxis[1] * sinAngle;
170  matrix(0, 3) = T(0);
171 
172  matrix(1, 0) = normAxis[1] * normAxis[0] * (1 - cosAngle) + normAxis[2] * sinAngle;
173  matrix(1, 1) = normAxis[1] * normAxis[1] * (1 - cosAngle) + cosAngle;
174  matrix(1, 2) = normAxis[1] * normAxis[2] * (1 - cosAngle) - normAxis[0] * sinAngle;
175  matrix(1, 3) = T(0);
176 
177  matrix(2, 0) = normAxis[2] * normAxis[0] * (1 - cosAngle) - normAxis[1] * sinAngle;
178  matrix(2, 1) = normAxis[2] * normAxis[1] * (1 - cosAngle) + normAxis[0] * sinAngle;
179  matrix(2, 2) = normAxis[2] * normAxis[2] * (1 - cosAngle) + cosAngle;
180  matrix(2, 3) = T(0);
181 
182  matrix(3, 0) = T(0);
183  matrix(3, 1) = T(0);
184  matrix(3, 2) = T(0);
185  matrix(3, 3) = T(1);
186 
187  return matrix;
188 }
189 template <typename T>
191 {
192  return viskores::Transform3DRotate(angleDegrees, viskores::Vec<T, 3>(x, y, z));
193 }
194 
199 template <typename T>
201 {
202  return viskores::Transform3DRotate(angleDegrees, T(1), T(0), T(0));
203 }
204 
209 template <typename T>
211 {
212  return viskores::Transform3DRotate(angleDegrees, T(0), T(1), T(0));
213 }
214 
219 template <typename T>
221 {
222  return viskores::Transform3DRotate(angleDegrees, T(0), T(0), T(1));
223 }
224 
225 } // namespace viskores
226 
227 #endif //viskores_Transform3D_h
viskores::MatrixGetRow
const viskores::Vec< T, NumCol > & MatrixGetRow(const viskores::Matrix< T, NumRow, NumCol > &matrix, viskores::IdComponent rowIndex)
Returns a tuple containing the given row (indexed from 0) of the given matrix.
Definition: Matrix.h:116
viskores::Normal
T Normal(const T &x)
Returns a normalized version of the given vector.
Definition: VectorAnalysis.h:166
viskores::Transform3DRotateY
viskores::Matrix< T, 4, 4 > Transform3DRotateY(T angleDegrees)
Returns a rotation matrix.
Definition: Transform3D.h:210
Matrix.h
viskores::Sin
viskores::Float32 Sin(viskores::Float32 x)
Definition: Math.h:174
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::Vec< T, 3 >
Definition: Types.h:1025
viskores::Transform3DTranslate
viskores::Matrix< T, 4, 4 > Transform3DTranslate(const T &x, const T &y, const T &z)
Returns a translation matrix.
Definition: Transform3D.h:131
VectorAnalysis.h
viskores::Transform3DVector
viskores::Vec< T, 3 > Transform3DVector(const viskores::Matrix< T, 4, 4 > &matrix, const viskores::Vec< T, 3 > &vector)
Transform a 3D vector by a transformation matrix.
Definition: Transform3D.h:80
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::Transform3DRotate
viskores::Matrix< T, 4, 4 > Transform3DRotate(T angleDegrees, const viskores::Vec< T, 3 > &axisOfRotation)
Returns a rotation matrix.
Definition: Transform3D.h:156
viskores::Transform3DRotateZ
viskores::Matrix< T, 4, 4 > Transform3DRotateZ(T angleDegrees)
Returns a rotation matrix.
Definition: Transform3D.h:220
viskores::Matrix
Basic Matrix type.
Definition: Matrix.h:41
viskores::Transform3DPoint
viskores::Vec< T, 3 > Transform3DPoint(const viskores::Matrix< T, 4, 4 > &matrix, const viskores::Vec< T, 3 > &point)
Transform a 3D point by a transformation matrix.
Definition: Transform3D.h:42
viskores::MatrixIdentity
viskores::Matrix< T, Size, Size > MatrixIdentity()
Returns the identity matrix.
Definition: Matrix.h:221
viskores::Cos
viskores::Float32 Cos(viskores::Float32 x)
Definition: Math.h:235
viskores::Vec< T, 4 >
Definition: Types.h:1143
viskores::MatrixMultiply
viskores::Matrix< T, NumRow, NumCol > MatrixMultiply(const viskores::Matrix< T, NumRow, NumInternal > &leftFactor, const viskores::Matrix< T, NumInternal, NumCol > &rightFactor)
Standard matrix multiplication.
Definition: Matrix.h:168
viskores::Transform3DPointPerspective
viskores::Vec< T, 3 > Transform3DPointPerspective(const viskores::Matrix< T, 4, 4 > &matrix, const viskores::Vec< T, 3 > &point)
Transform a 3D point by a transformation matrix with perspective.
Definition: Transform3D.h:61
viskores::Transform3DScale
viskores::Matrix< T, 4, 4 > Transform3DScale(const T &scaleX, const T &scaleY, const T &scaleZ)
Returns a scale matrix.
Definition: Transform3D.h:94
viskores::Transform3DRotateX
viskores::Matrix< T, 4, 4 > Transform3DRotateX(T angleDegrees)
Returns a rotation matrix.
Definition: Transform3D.h:200