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