1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2008-2014 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#ifndef NV_NVFOUNDATION_NVMAT33_H
#define NV_NVFOUNDATION_NVMAT33_H
/** \addtogroup foundation
@{
*/
#include "NvVec3.h"
#include "NvQuat.h"
#if !NV_DOXYGEN
namespace nvidia
{
#endif
/*!
\brief 3x3 matrix class
Some clarifications, as there have been much confusion about matrix formats etc in the past.
Short:
- Matrix have base vectors in columns (vectors are column matrices, 3x1 matrices).
- Matrix is physically stored in column major format
- Matrices are concaternated from left
Long:
Given three base vectors a, b and c the matrix is stored as
|a.x b.x c.x|
|a.y b.y c.y|
|a.z b.z c.z|
Vectors are treated as columns, so the vector v is
|x|
|y|
|z|
And matrices are applied _before_ the vector (pre-multiplication)
v' = M*v
|x'| |a.x b.x c.x| |x| |a.x*x + b.x*y + c.x*z|
|y'| = |a.y b.y c.y| * |y| = |a.y*x + b.y*y + c.y*z|
|z'| |a.z b.z c.z| |z| |a.z*x + b.z*y + c.z*z|
Physical storage and indexing:
To be compatible with popular 3d rendering APIs (read D3d and OpenGL)
the physical indexing is
|0 3 6|
|1 4 7|
|2 5 8|
index = column*3 + row
which in C++ translates to M[column][row]
The mathematical indexing is M_row,column and this is what is used for _-notation
so _12 is 1st row, second column and operator(row, column)!
*/
class NvMat33
{
public:
//! Default constructor
NV_CUDA_CALLABLE NV_FORCE_INLINE NvMat33()
{
}
//! identity constructor
NV_CUDA_CALLABLE NV_INLINE NvMat33(NvIDENTITY r)
: column0(1.0f, 0.0f, 0.0f), column1(0.0f, 1.0f, 0.0f), column2(0.0f, 0.0f, 1.0f)
{
NV_UNUSED(r);
}
//! zero constructor
NV_CUDA_CALLABLE NV_INLINE NvMat33(NvZERO r) : column0(0.0f), column1(0.0f), column2(0.0f)
{
NV_UNUSED(r);
}
//! Construct from three base vectors
NV_CUDA_CALLABLE NvMat33(const NvVec3& col0, const NvVec3& col1, const NvVec3& col2)
: column0(col0), column1(col1), column2(col2)
{
}
//! constructor from a scalar, which generates a multiple of the identity matrix
explicit NV_CUDA_CALLABLE NV_INLINE NvMat33(float r)
: column0(r, 0.0f, 0.0f), column1(0.0f, r, 0.0f), column2(0.0f, 0.0f, r)
{
}
//! Construct from float[9]
explicit NV_CUDA_CALLABLE NV_INLINE NvMat33(float values[])
: column0(values[0], values[1], values[2])
, column1(values[3], values[4], values[5])
, column2(values[6], values[7], values[8])
{
}
//! Construct from a quaternion
explicit NV_CUDA_CALLABLE NV_FORCE_INLINE NvMat33(const NvQuat& q)
{
const float x = q.x;
const float y = q.y;
const float z = q.z;
const float w = q.w;
const float x2 = x + x;
const float y2 = y + y;
const float z2 = z + z;
const float xx = x2 * x;
const float yy = y2 * y;
const float zz = z2 * z;
const float xy = x2 * y;
const float xz = x2 * z;
const float xw = x2 * w;
const float yz = y2 * z;
const float yw = y2 * w;
const float zw = z2 * w;
column0 = NvVec3(1.0f - yy - zz, xy + zw, xz - yw);
column1 = NvVec3(xy - zw, 1.0f - xx - zz, yz + xw);
column2 = NvVec3(xz + yw, yz - xw, 1.0f - xx - yy);
}
//! Copy constructor
NV_CUDA_CALLABLE NV_INLINE NvMat33(const NvMat33& other)
: column0(other.column0), column1(other.column1), column2(other.column2)
{
}
//! Assignment operator
NV_CUDA_CALLABLE NV_FORCE_INLINE NvMat33& operator=(const NvMat33& other)
{
column0 = other.column0;
column1 = other.column1;
column2 = other.column2;
return *this;
}
//! Construct from diagonal, off-diagonals are zero.
NV_CUDA_CALLABLE NV_INLINE static NvMat33 createDiagonal(const NvVec3& d)
{
return NvMat33(NvVec3(d.x, 0.0f, 0.0f), NvVec3(0.0f, d.y, 0.0f), NvVec3(0.0f, 0.0f, d.z));
}
/**
\brief returns true if the two matrices are exactly equal
*/
NV_CUDA_CALLABLE NV_INLINE bool operator==(const NvMat33& m) const
{
return column0 == m.column0 && column1 == m.column1 && column2 == m.column2;
}
//! Get transposed matrix
NV_CUDA_CALLABLE NV_FORCE_INLINE NvMat33 getTranspose() const
{
const NvVec3 v0(column0.x, column1.x, column2.x);
const NvVec3 v1(column0.y, column1.y, column2.y);
const NvVec3 v2(column0.z, column1.z, column2.z);
return NvMat33(v0, v1, v2);
}
//! Get the real inverse
NV_CUDA_CALLABLE NV_INLINE NvMat33 getInverse() const
{
const float det = getDeterminant();
NvMat33 inverse;
if(det != 0)
{
const float invDet = 1.0f / det;
inverse.column0.x = invDet * (column1.y * column2.z - column2.y * column1.z);
inverse.column0.y = invDet * -(column0.y * column2.z - column2.y * column0.z);
inverse.column0.z = invDet * (column0.y * column1.z - column0.z * column1.y);
inverse.column1.x = invDet * -(column1.x * column2.z - column1.z * column2.x);
inverse.column1.y = invDet * (column0.x * column2.z - column0.z * column2.x);
inverse.column1.z = invDet * -(column0.x * column1.z - column0.z * column1.x);
inverse.column2.x = invDet * (column1.x * column2.y - column1.y * column2.x);
inverse.column2.y = invDet * -(column0.x * column2.y - column0.y * column2.x);
inverse.column2.z = invDet * (column0.x * column1.y - column1.x * column0.y);
return inverse;
}
else
{
return NvMat33(NvIdentity);
}
}
//! Get determinant
NV_CUDA_CALLABLE NV_INLINE float getDeterminant() const
{
return column0.dot(column1.cross(column2));
}
//! Unary minus
NV_CUDA_CALLABLE NV_INLINE NvMat33 operator-() const
{
return NvMat33(-column0, -column1, -column2);
}
//! Add
NV_CUDA_CALLABLE NV_INLINE NvMat33 operator+(const NvMat33& other) const
{
return NvMat33(column0 + other.column0, column1 + other.column1, column2 + other.column2);
}
//! Subtract
NV_CUDA_CALLABLE NV_INLINE NvMat33 operator-(const NvMat33& other) const
{
return NvMat33(column0 - other.column0, column1 - other.column1, column2 - other.column2);
}
//! Scalar multiplication
NV_CUDA_CALLABLE NV_INLINE NvMat33 operator*(float scalar) const
{
return NvMat33(column0 * scalar, column1 * scalar, column2 * scalar);
}
friend NvMat33 operator*(float, const NvMat33&);
//! Matrix vector multiplication (returns 'this->transform(vec)')
NV_CUDA_CALLABLE NV_INLINE NvVec3 operator*(const NvVec3& vec) const
{
return transform(vec);
}
// a <op>= b operators
//! Matrix multiplication
NV_CUDA_CALLABLE NV_FORCE_INLINE NvMat33 operator*(const NvMat33& other) const
{
// Rows from this <dot> columns from other
// column0 = transform(other.column0) etc
return NvMat33(transform(other.column0), transform(other.column1), transform(other.column2));
}
//! Equals-add
NV_CUDA_CALLABLE NV_INLINE NvMat33& operator+=(const NvMat33& other)
{
column0 += other.column0;
column1 += other.column1;
column2 += other.column2;
return *this;
}
//! Equals-sub
NV_CUDA_CALLABLE NV_INLINE NvMat33& operator-=(const NvMat33& other)
{
column0 -= other.column0;
column1 -= other.column1;
column2 -= other.column2;
return *this;
}
//! Equals scalar multiplication
NV_CUDA_CALLABLE NV_INLINE NvMat33& operator*=(float scalar)
{
column0 *= scalar;
column1 *= scalar;
column2 *= scalar;
return *this;
}
//! Equals matrix multiplication
NV_CUDA_CALLABLE NV_INLINE NvMat33& operator*=(const NvMat33& other)
{
*this = *this * other;
return *this;
}
//! Element access, mathematical way!
NV_DEPRECATED NV_CUDA_CALLABLE NV_FORCE_INLINE float operator()(unsigned int row, unsigned int col) const
{
return (*this)[col][row];
}
//! Element access, mathematical way!
NV_DEPRECATED NV_CUDA_CALLABLE NV_FORCE_INLINE float& operator()(unsigned int row, unsigned int col)
{
return (*this)[col][row];
}
// Transform etc
//! Transform vector by matrix, equal to v' = M*v
NV_CUDA_CALLABLE NV_FORCE_INLINE NvVec3 transform(const NvVec3& other) const
{
return column0 * other.x + column1 * other.y + column2 * other.z;
}
//! Transform vector by matrix transpose, v' = M^t*v
NV_CUDA_CALLABLE NV_INLINE NvVec3 transformTranspose(const NvVec3& other) const
{
return NvVec3(column0.dot(other), column1.dot(other), column2.dot(other));
}
NV_CUDA_CALLABLE NV_FORCE_INLINE const float* front() const
{
return &column0.x;
}
NV_DEPRECATED NV_CUDA_CALLABLE NV_FORCE_INLINE NvVec3& operator[](unsigned int num)
{
return (&column0)[num];
}
NV_DEPRECATED NV_CUDA_CALLABLE NV_FORCE_INLINE const NvVec3& operator[](unsigned int num) const
{
return (&column0)[num];
}
// Data, see above for format!
NvVec3 column0, column1, column2; // the three base vectors
};
// implementation from NvQuat.h
NV_CUDA_CALLABLE NV_INLINE NvQuat::NvQuat(const NvMat33& m)
{
if (m.column2.z < 0)
{
if (m.column0.x > m.column1.y)
{
float t = 1 + m.column0.x - m.column1.y - m.column2.z;
*this = NvQuat(t, m.column0.y + m.column1.x, m.column2.x + m.column0.z, m.column1.z - m.column2.y) * (0.5f / NvSqrt(t));
}
else
{
float t = 1 - m.column0.x + m.column1.y - m.column2.z;
*this = NvQuat(m.column0.y + m.column1.x, t, m.column1.z + m.column2.y, m.column2.x - m.column0.z) * (0.5f / NvSqrt(t));
}
}
else
{
if (m.column0.x < -m.column1.y)
{
float t = 1 - m.column0.x - m.column1.y + m.column2.z;
*this = NvQuat(m.column2.x + m.column0.z, m.column1.z + m.column2.y, t, m.column0.y - m.column1.x) * (0.5f / NvSqrt(t));
}
else
{
float t = 1 + m.column0.x + m.column1.y + m.column2.z;
*this = NvQuat(m.column1.z - m.column2.y, m.column2.x - m.column0.z, m.column0.y - m.column1.x, t) * (0.5f / NvSqrt(t));
}
}
}
#if !NV_DOXYGEN
} // namespace nvidia
#endif
/** @} */
#endif // #ifndef NV_NVFOUNDATION_NVMAT33_H
|