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
|
// 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_NVMAT44_H
#define NV_NVFOUNDATION_NVMAT44_H
/** \addtogroup foundation
@{
*/
#include "NvQuat.h"
#include "NvVec4.h"
#include "NvMat33.h"
#include "NvTransform.h"
#if !NV_DOXYGEN
namespace nvidia
{
#endif
/*!
\brief 4x4 matrix class
This class is layout-compatible with D3D and OpenGL matrices. More notes on layout are given in the NvMat33
@see NvMat33 NvTransform
*/
class NvMat44
{
public:
//! Default constructor
NV_CUDA_CALLABLE NV_INLINE NvMat44()
{
}
//! identity constructor
NV_CUDA_CALLABLE NV_INLINE NvMat44(NvIDENTITY r)
: column0(1.0f, 0.0f, 0.0f, 0.0f)
, column1(0.0f, 1.0f, 0.0f, 0.0f)
, column2(0.0f, 0.0f, 1.0f, 0.0f)
, column3(0.0f, 0.0f, 0.0f, 1.0f)
{
NV_UNUSED(r);
}
//! zero constructor
NV_CUDA_CALLABLE NV_INLINE NvMat44(NvZERO r) : column0(NvZero), column1(NvZero), column2(NvZero), column3(NvZero)
{
NV_UNUSED(r);
}
//! Construct from four 4-vectors
NV_CUDA_CALLABLE NvMat44(const NvVec4& col0, const NvVec4& col1, const NvVec4& col2, const NvVec4& col3)
: column0(col0), column1(col1), column2(col2), column3(col3)
{
}
//! constructor that generates a multiple of the identity matrix
explicit NV_CUDA_CALLABLE NV_INLINE NvMat44(float r)
: column0(r, 0.0f, 0.0f, 0.0f)
, column1(0.0f, r, 0.0f, 0.0f)
, column2(0.0f, 0.0f, r, 0.0f)
, column3(0.0f, 0.0f, 0.0f, r)
{
}
//! Construct from three base vectors and a translation
NV_CUDA_CALLABLE NvMat44(const NvVec3& col0, const NvVec3& col1, const NvVec3& col2, const NvVec3& col3)
: column0(col0, 0), column1(col1, 0), column2(col2, 0), column3(col3, 1.0f)
{
}
//! Construct from float[16]
explicit NV_CUDA_CALLABLE NV_INLINE NvMat44(float values[])
: column0(values[0], values[1], values[2], values[3])
, column1(values[4], values[5], values[6], values[7])
, column2(values[8], values[9], values[10], values[11])
, column3(values[12], values[13], values[14], values[15])
{
}
//! Construct from a quaternion
explicit NV_CUDA_CALLABLE NV_INLINE NvMat44(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 = NvVec4(1.0f - yy - zz, xy + zw, xz - yw, 0.0f);
column1 = NvVec4(xy - zw, 1.0f - xx - zz, yz + xw, 0.0f);
column2 = NvVec4(xz + yw, yz - xw, 1.0f - xx - yy, 0.0f);
column3 = NvVec4(0.0f, 0.0f, 0.0f, 1.0f);
}
//! Construct from a diagonal vector
explicit NV_CUDA_CALLABLE NV_INLINE NvMat44(const NvVec4& diagonal)
: column0(diagonal.x, 0.0f, 0.0f, 0.0f)
, column1(0.0f, diagonal.y, 0.0f, 0.0f)
, column2(0.0f, 0.0f, diagonal.z, 0.0f)
, column3(0.0f, 0.0f, 0.0f, diagonal.w)
{
}
//! Construct from Mat33 and a translation
NV_CUDA_CALLABLE NvMat44(const NvMat33& axes, const NvVec3& position)
: column0(axes.column0, 0.0f), column1(axes.column1, 0.0f), column2(axes.column2, 0.0f), column3(position, 1.0f)
{
}
NV_CUDA_CALLABLE NvMat44(const NvTransform& t)
{
*this = NvMat44(NvMat33(t.q), t.p);
}
/**
\brief returns true if the two matrices are exactly equal
*/
NV_CUDA_CALLABLE NV_INLINE bool operator==(const NvMat44& m) const
{
return column0 == m.column0 && column1 == m.column1 && column2 == m.column2 && column3 == m.column3;
}
//! Copy constructor
NV_CUDA_CALLABLE NV_INLINE NvMat44(const NvMat44& other)
: column0(other.column0), column1(other.column1), column2(other.column2), column3(other.column3)
{
}
//! Assignment operator
NV_CUDA_CALLABLE NV_INLINE const NvMat44& operator=(const NvMat44& other)
{
column0 = other.column0;
column1 = other.column1;
column2 = other.column2;
column3 = other.column3;
return *this;
}
//! Get transposed matrix
NV_CUDA_CALLABLE NV_INLINE NvMat44 getTranspose() const
{
return NvMat44(
NvVec4(column0.x, column1.x, column2.x, column3.x), NvVec4(column0.y, column1.y, column2.y, column3.y),
NvVec4(column0.z, column1.z, column2.z, column3.z), NvVec4(column0.w, column1.w, column2.w, column3.w));
}
//! Unary minus
NV_CUDA_CALLABLE NV_INLINE NvMat44 operator-() const
{
return NvMat44(-column0, -column1, -column2, -column3);
}
//! Add
NV_CUDA_CALLABLE NV_INLINE NvMat44 operator+(const NvMat44& other) const
{
return NvMat44(column0 + other.column0, column1 + other.column1, column2 + other.column2,
column3 + other.column3);
}
//! Subtract
NV_CUDA_CALLABLE NV_INLINE NvMat44 operator-(const NvMat44& other) const
{
return NvMat44(column0 - other.column0, column1 - other.column1, column2 - other.column2,
column3 - other.column3);
}
//! Scalar multiplication
NV_CUDA_CALLABLE NV_INLINE NvMat44 operator*(float scalar) const
{
return NvMat44(column0 * scalar, column1 * scalar, column2 * scalar, column3 * scalar);
}
friend NvMat44 operator*(float, const NvMat44&);
//! Matrix multiplication
NV_CUDA_CALLABLE NV_INLINE NvMat44 operator*(const NvMat44& other) const
{
// Rows from this <dot> columns from other
// column0 = transform(other.column0) etc
return NvMat44(transform(other.column0), transform(other.column1), transform(other.column2),
transform(other.column3));
}
// a <op>= b operators
//! Equals-add
NV_CUDA_CALLABLE NV_INLINE NvMat44& operator+=(const NvMat44& other)
{
column0 += other.column0;
column1 += other.column1;
column2 += other.column2;
column3 += other.column3;
return *this;
}
//! Equals-sub
NV_CUDA_CALLABLE NV_INLINE NvMat44& operator-=(const NvMat44& other)
{
column0 -= other.column0;
column1 -= other.column1;
column2 -= other.column2;
column3 -= other.column3;
return *this;
}
//! Equals scalar multiplication
NV_CUDA_CALLABLE NV_INLINE NvMat44& operator*=(float scalar)
{
column0 *= scalar;
column1 *= scalar;
column2 *= scalar;
column3 *= scalar;
return *this;
}
//! Equals matrix multiplication
NV_CUDA_CALLABLE NV_INLINE NvMat44& operator*=(const NvMat44& 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 vector by matrix, equal to v' = M*v
NV_CUDA_CALLABLE NV_INLINE NvVec4 transform(const NvVec4& other) const
{
return column0 * other.x + column1 * other.y + column2 * other.z + column3 * other.w;
}
//! Transform vector by matrix, equal to v' = M*v
NV_CUDA_CALLABLE NV_INLINE NvVec3 transform(const NvVec3& other) const
{
return transform(NvVec4(other, 1.0f)).getXYZ();
}
//! Rotate vector by matrix, equal to v' = M*v
NV_CUDA_CALLABLE NV_INLINE const NvVec4 rotate(const NvVec4& other) const
{
return column0 * other.x + column1 * other.y + column2 * other.z; // + column3*0;
}
//! Rotate vector by matrix, equal to v' = M*v
NV_CUDA_CALLABLE NV_INLINE const NvVec3 rotate(const NvVec3& other) const
{
return rotate(NvVec4(other, 1.0f)).getXYZ();
}
NV_CUDA_CALLABLE NV_INLINE NvVec3 getBasis(int num) const
{
NV_ASSERT(num >= 0 && num < 3);
return (&column0)[num].getXYZ();
}
NV_CUDA_CALLABLE NV_INLINE NvVec3 getPosition() const
{
return column3.getXYZ();
}
NV_CUDA_CALLABLE NV_INLINE void setPosition(const NvVec3& position)
{
column3.x = position.x;
column3.y = position.y;
column3.z = position.z;
}
NV_CUDA_CALLABLE NV_FORCE_INLINE const float* front() const
{
return &column0.x;
}
NV_DEPRECATED NV_CUDA_CALLABLE NV_FORCE_INLINE NvVec4& operator[](unsigned int num)
{
return (&column0)[num];
}
NV_DEPRECATED NV_CUDA_CALLABLE NV_FORCE_INLINE const NvVec4& operator[](unsigned int num) const
{
return (&column0)[num];
}
NV_CUDA_CALLABLE NV_INLINE void scale(const NvVec4& p)
{
column0 *= p.x;
column1 *= p.y;
column2 *= p.z;
column3 *= p.w;
}
NV_CUDA_CALLABLE NV_INLINE NvMat44 inverseRT(void) const
{
NvVec3 r0(column0.x, column1.x, column2.x), r1(column0.y, column1.y, column2.y),
r2(column0.z, column1.z, column2.z);
return NvMat44(r0, r1, r2, -(r0 * column3.x + r1 * column3.y + r2 * column3.z));
}
NV_CUDA_CALLABLE NV_INLINE bool isFinite() const
{
return column0.isFinite() && column1.isFinite() && column2.isFinite() && column3.isFinite();
}
// Data, see above for format!
NvVec4 column0, column1, column2, column3; // the four base vectors
};
// implementation from NvTransform.h
NV_CUDA_CALLABLE NV_FORCE_INLINE NvTransform::NvTransform(const NvMat44& m)
{
NvVec3 column0 = NvVec3(m.column0.x, m.column0.y, m.column0.z);
NvVec3 column1 = NvVec3(m.column1.x, m.column1.y, m.column1.z);
NvVec3 column2 = NvVec3(m.column2.x, m.column2.y, m.column2.z);
q = NvQuat(NvMat33(column0, column1, column2));
p = NvVec3(m.column3.x, m.column3.y, m.column3.z);
}
#if !NV_DOXYGEN
} // namespace nvidia
#endif
/** @} */
#endif // #ifndef NV_NVFOUNDATION_NVMAT44_H
|