VTK-m  2.0
MatrixHelpers.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_rendering_MatrixHelpers_h
11 #define vtk_m_rendering_MatrixHelpers_h
12 
13 #include <vtkm/Matrix.h>
14 #include <vtkm/VectorAnalysis.h>
15 
16 namespace vtkm
17 {
18 namespace rendering
19 {
20 
22 {
24  vtkm::Float32* oglM)
25  {
26  oglM[0] = mtx[0][0];
27  oglM[1] = mtx[1][0];
28  oglM[2] = mtx[2][0];
29  oglM[3] = mtx[3][0];
30  oglM[4] = mtx[0][1];
31  oglM[5] = mtx[1][1];
32  oglM[6] = mtx[2][1];
33  oglM[7] = mtx[3][1];
34  oglM[8] = mtx[0][2];
35  oglM[9] = mtx[1][2];
36  oglM[10] = mtx[2][2];
37  oglM[11] = mtx[3][2];
38  oglM[12] = mtx[0][3];
39  oglM[13] = mtx[1][3];
40  oglM[14] = mtx[2][3];
41  oglM[15] = mtx[3][3];
42  }
43 
45  const vtkm::Vec3f_32& lookAt,
46  const vtkm::Vec3f_32& up)
47  {
48  vtkm::Vec3f_32 viewDir = position - lookAt;
49  vtkm::Vec3f_32 right = vtkm::Cross(up, viewDir);
50  vtkm::Vec3f_32 ru = vtkm::Cross(viewDir, right);
51 
52  vtkm::Normalize(viewDir);
53  vtkm::Normalize(right);
54  vtkm::Normalize(ru);
55 
57  vtkm::MatrixIdentity(matrix);
58 
59  matrix(0, 0) = right[0];
60  matrix(0, 1) = right[1];
61  matrix(0, 2) = right[2];
62  matrix(1, 0) = ru[0];
63  matrix(1, 1) = ru[1];
64  matrix(1, 2) = ru[2];
65  matrix(2, 0) = viewDir[0];
66  matrix(2, 1) = viewDir[1];
67  matrix(2, 2) = viewDir[2];
68 
69  matrix(0, 3) = -vtkm::Dot(right, position);
70  matrix(1, 3) = -vtkm::Dot(ru, position);
71  matrix(2, 3) = -vtkm::Dot(viewDir, position);
72 
73  return matrix;
74  }
75 
77  const vtkm::Vec3f_32& newx,
78  const vtkm::Vec3f_32& newy,
79  const vtkm::Vec3f_32& newz)
80  {
82  vtkm::MatrixIdentity(matrix);
83 
84  matrix(0, 0) = newx[0];
85  matrix(0, 1) = newy[0];
86  matrix(0, 2) = newz[0];
87  matrix(1, 0) = newx[1];
88  matrix(1, 1) = newy[1];
89  matrix(1, 2) = newz[1];
90  matrix(2, 0) = newx[2];
91  matrix(2, 1) = newy[2];
92  matrix(2, 2) = newz[2];
93 
94  matrix(0, 3) = neworigin[0];
95  matrix(1, 3) = neworigin[1];
96  matrix(2, 3) = neworigin[2];
97 
98  return matrix;
99  }
100 
102  const vtkm::Float32 y,
103  const vtkm::Float32 z)
104  {
106  vtkm::MatrixIdentity(matrix);
107  matrix[0][0] = x;
108  matrix[1][1] = y;
109  matrix[2][2] = z;
110 
111  return matrix;
112  }
113 
115  vtkm::Float32 p1y,
116  vtkm::Float32 p2x,
117  vtkm::Float32 p2y)
118  {
119  const vtkm::Float32 RADIUS = 0.80f; //z value lookAt x = y = 0.0
120  const vtkm::Float32 COMPRESSION = 3.5f; // multipliers for x and y.
121  const vtkm::Float32 AR3 = RADIUS * RADIUS * RADIUS;
122 
124 
125  vtkm::MatrixIdentity(matrix);
126  if (p1x == p2x && p1y == p2y)
127  {
128  return matrix;
129  }
130 
131  vtkm::Vec3f_32 p1(p1x, p1y, AR3 / ((p1x * p1x + p1y * p1y) * COMPRESSION + AR3));
132  vtkm::Vec3f_32 p2(p2x, p2y, AR3 / ((p2x * p2x + p2y * p2y) * COMPRESSION + AR3));
133  vtkm::Vec3f_32 axis = vtkm::Normal(vtkm::Cross(p2, p1));
134 
135  vtkm::Vec3f_32 p2_p1(p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2]);
136  vtkm::Float32 t = vtkm::Magnitude(p2_p1);
137  t = vtkm::Min(vtkm::Max(t, -1.0f), 1.0f);
138  vtkm::Float32 phi = static_cast<vtkm::Float32>(-2.0f * asin(t / (2.0f * RADIUS)));
139  vtkm::Float32 val = static_cast<vtkm::Float32>(sin(phi / 2.0f));
140  axis[0] *= val;
141  axis[1] *= val;
142  axis[2] *= val;
143 
144  //quaternion
145  vtkm::Float32 q[4] = { axis[0], axis[1], axis[2], static_cast<vtkm::Float32>(cos(phi / 2.0f)) };
146 
147  // normalize quaternion to unit magnitude
148  t = 1.0f /
149  static_cast<vtkm::Float32>(sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]));
150  q[0] *= t;
151  q[1] *= t;
152  q[2] *= t;
153  q[3] *= t;
154 
155  matrix(0, 0) = 1 - 2 * (q[1] * q[1] + q[2] * q[2]);
156  matrix(0, 1) = 2 * (q[0] * q[1] + q[2] * q[3]);
157  matrix(0, 2) = (2 * (q[2] * q[0] - q[1] * q[3]));
158 
159  matrix(1, 0) = 2 * (q[0] * q[1] - q[2] * q[3]);
160  matrix(1, 1) = 1 - 2 * (q[2] * q[2] + q[0] * q[0]);
161  matrix(1, 2) = (2 * (q[1] * q[2] + q[0] * q[3]));
162 
163  matrix(2, 0) = (2 * (q[2] * q[0] + q[1] * q[3]));
164  matrix(2, 1) = (2 * (q[1] * q[2] - q[0] * q[3]));
165  matrix(2, 2) = (1 - 2 * (q[1] * q[1] + q[0] * q[0]));
166 
167  return matrix;
168  }
169 };
170 }
171 } //namespace vtkm::rendering
172 
173 #endif // vtk_m_rendering_MatrixHelpers_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Normalize
VTKM_EXEC_CONT void Normalize(T &x)
Changes a vector to be normal.
Definition: VectorAnalysis.h:168
vtkm::Magnitude
VTKM_EXEC_CONT detail::FloatingPointReturnType< T >::Type Magnitude(const T &x)
Returns the magnitude of a vector.
Definition: VectorAnalysis.h:100
Matrix.h
vtkm::rendering::MatrixHelpers
Definition: MatrixHelpers.h:21
vtkm::MatrixIdentity
VTKM_EXEC_CONT vtkm::Matrix< T, Size, Size > MatrixIdentity()
Returns the identity matrix.
Definition: Matrix.h:209
vtkm::rendering::MatrixHelpers::TrackballMatrix
static VTKM_CONT vtkm::Matrix< vtkm::Float32, 4, 4 > TrackballMatrix(vtkm::Float32 p1x, vtkm::Float32 p1y, vtkm::Float32 p2x, vtkm::Float32 p2y)
Definition: MatrixHelpers.h:114
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::rendering::MatrixHelpers::WorldMatrix
static VTKM_CONT vtkm::Matrix< vtkm::Float32, 4, 4 > WorldMatrix(const vtkm::Vec3f_32 &neworigin, const vtkm::Vec3f_32 &newx, const vtkm::Vec3f_32 &newy, const vtkm::Vec3f_32 &newz)
Definition: MatrixHelpers.h:76
vtkm::Cross
VTKM_EXEC_CONT vtkm::Vec< typename detail::FloatingPointReturnType< T >::Type, 3 > Cross(const vtkm::Vec< T, 3 > &x, const vtkm::Vec< T, 3 > &y)
Find the cross product of two vectors.
Definition: VectorAnalysis.h:177
vtkm::rendering::MatrixHelpers::CreateOGLMatrix
static VTKM_CONT void CreateOGLMatrix(const vtkm::Matrix< vtkm::Float32, 4, 4 > &mtx, vtkm::Float32 *oglM)
Definition: MatrixHelpers.h:23
vtkm::rendering::MatrixHelpers::ViewMatrix
static VTKM_CONT vtkm::Matrix< vtkm::Float32, 4, 4 > ViewMatrix(const vtkm::Vec3f_32 &position, const vtkm::Vec3f_32 &lookAt, const vtkm::Vec3f_32 &up)
Definition: MatrixHelpers.h:44
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Vec< vtkm::Float32, 3 >
vtkm::Matrix< vtkm::Float32, 4, 4 >
vtkm::rendering::MatrixHelpers::CreateScale
static VTKM_CONT vtkm::Matrix< vtkm::Float32, 4, 4 > CreateScale(const vtkm::Float32 x, const vtkm::Float32 y, const vtkm::Float32 z)
Definition: MatrixHelpers.h:101
vtkm::Float32
float Float32
Definition: Types.h:154