MaterialX 1.38.2
Types.h
Go to the documentation of this file.
1//
2// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3// All rights reserved. See LICENSE.txt for license.
4//
5
6#ifndef MATERIALX_TYPES_H
7#define MATERIALX_TYPES_H
8
11
13
14#include <MaterialXCore/Util.h>
15
16#include <array>
17#include <cmath>
18
19namespace MaterialX
20{
21
22extern MX_CORE_API const string DEFAULT_TYPE_STRING;
23extern MX_CORE_API const string FILENAME_TYPE_STRING;
24extern MX_CORE_API const string GEOMNAME_TYPE_STRING;
25extern MX_CORE_API const string STRING_TYPE_STRING;
26extern MX_CORE_API const string SURFACE_SHADER_TYPE_STRING;
27extern MX_CORE_API const string DISPLACEMENT_SHADER_TYPE_STRING;
28extern MX_CORE_API const string VOLUME_SHADER_TYPE_STRING;
29extern MX_CORE_API const string LIGHT_SHADER_TYPE_STRING;
30extern MX_CORE_API const string MATERIAL_TYPE_STRING;
31extern MX_CORE_API const string SURFACE_MATERIAL_NODE_STRING;
32extern MX_CORE_API const string VOLUME_MATERIAL_NODE_STRING;
33extern MX_CORE_API const string MULTI_OUTPUT_TYPE_STRING;
34extern MX_CORE_API const string NONE_TYPE_STRING;
35extern MX_CORE_API const string VALUE_STRING_TRUE;
36extern MX_CORE_API const string VALUE_STRING_FALSE;
37extern MX_CORE_API const string NAME_PREFIX_SEPARATOR;
38extern MX_CORE_API const string NAME_PATH_SEPARATOR;
39extern MX_CORE_API const string ARRAY_VALID_SEPARATORS;
40extern MX_CORE_API const string ARRAY_PREFERRED_SEPARATOR;
41
43class VectorBase { };
44
46class Uninit { };
47
53template <class V, class S, size_t N> class VectorN : public VectorBase
54{
55 public:
56 using Iterator = typename std::array<S, N>::iterator;
57 using ConstIterator = typename std::array<S, N>::const_iterator;
58
59 public:
60 VectorN() : _arr{} { }
61 explicit VectorN(Uninit) { }
62 explicit VectorN(S s) { _arr.fill(s); }
63 explicit VectorN(const std::array<S, N>& arr) : _arr(arr) { }
64 explicit VectorN(const vector<S>& vec) { std::copy(vec.begin(), vec.end(), _arr.begin()); }
65 explicit VectorN(const S* begin, const S* end) { std::copy(begin, end, _arr.begin()); }
66
69
71 bool operator==(const V& rhs) const { return _arr == rhs._arr; }
72
74 bool operator!=(const V& rhs) const { return _arr != rhs._arr; }
75
77 bool operator<(const V& rhs) const
78 {
79 return _arr < rhs._arr;
80 }
81
85
87 S& operator[](size_t i) { return _arr.at(i); }
88
90 const S& operator[](size_t i) const { return _arr.at(i); }
91
95
97 V operator+(const V& rhs) const
98 {
99 V res(Uninit{});
100 for (size_t i = 0; i < N; i++)
101 res[i] = _arr[i] + rhs[i];
102 return res;
103 }
104
106 VectorN& operator+=(const V& rhs)
107 {
108 for (size_t i = 0; i < N; i++)
109 _arr[i] += rhs[i];
110 return *this;
111 }
112
114 V operator-(const V& rhs) const
115 {
116 V res(Uninit{});
117 for (size_t i = 0; i < N; i++)
118 res[i] = _arr[i] - rhs[i];
119 return res;
120 }
121
123 VectorN& operator-=(const V& rhs)
124 {
125 for (size_t i = 0; i < N; i++)
126 _arr[i] -= rhs[i];
127 return *this;
128 }
129
131 V operator*(const V& rhs) const
132 {
133 V res(Uninit{});
134 for (size_t i = 0; i < N; i++)
135 res[i] = _arr[i] * rhs[i];
136 return res;
137 }
138
140 VectorN& operator*=(const V& rhs)
141 {
142 for (size_t i = 0; i < N; i++)
143 _arr[i] *= rhs[i];
144 return *this;
145 }
146
148 V operator/(const V& rhs) const
149 {
150 V res(Uninit{});
151 for (size_t i = 0; i < N; i++)
152 res[i] = _arr[i] / rhs[i];
153 return res;
154 }
155
157 VectorN& operator/=(const V& rhs)
158 {
159 for (size_t i = 0; i < N; i++)
160 _arr[i] /= rhs[i];
161 return *this;
162 }
163
165 V operator*(S s) const
166 {
167 V res(Uninit{});
168 for (size_t i = 0; i < N; i++)
169 res[i] = _arr[i] * s;
170 return res;
171 }
172
175 {
176 for (size_t i = 0; i < N; i++)
177 _arr[i] *= s;
178 return *this;
179 }
180
182 V operator/(S s) const
183 {
184 V res(Uninit{});
185 for (size_t i = 0; i < N; i++)
186 res[i] = _arr[i] / s;
187 return res;
188 }
189
192 {
193 for (size_t i = 0; i < N; i++)
194 _arr[i] /= s;
195 return *this;
196 }
197
199 V operator-() const
200 {
201 V res(Uninit{});
202 for (size_t i = 0; i < N; i++)
203 res[i] = -_arr[i];
204 return res;
205 }
206
210
212 S getMagnitude() const
213 {
214 S res{};
215 for (size_t i = 0; i < N; i++)
216 res += _arr[i] * _arr[i];
217 return std::sqrt(res);
218 }
219
222 {
223 return *this / getMagnitude();
224 }
225
227 S dot(const V& rhs) const
228 {
229 S res{};
230 for (size_t i = 0; i < N; i++)
231 res += _arr[i] * rhs[i];
232 return res;
233 }
234
238
239 Iterator begin() { return _arr.begin(); }
240 ConstIterator begin() const { return _arr.begin(); }
241
242 Iterator end() { return _arr.end(); }
243 ConstIterator end() const { return _arr.end(); }
244
248
250 S* data() { return _arr.data(); }
251
253 const S* data() const { return _arr.data(); }
254
256 class Hash
257 {
258 public:
259 size_t operator()(const V& v) const noexcept
260 {
261 size_t h = 0;
262 for (size_t i = 0; i < N; i++)
263 hashCombine(h, v[i]);
264 return h;
265 }
266 };
267
271
273 static constexpr size_t numElements() { return N; }
274
276
277 protected:
278 std::array<S, N> _arr;
279};
280
283class MX_CORE_API Vector2 : public VectorN<Vector2, float, 2>
284{
285 public:
287 Vector2() = default;
288 Vector2(float x, float y) : VectorN(Uninit{})
289 {
290 _arr = {x, y};
291 }
292
294 float cross(const Vector2& rhs) const
295 {
296 return _arr[0] * rhs[1] - _arr[1] * rhs[0];
297 }
298};
299
302class MX_CORE_API Vector3 : public VectorN<Vector3, float, 3>
303{
304 public:
306 Vector3() = default;
307 Vector3(float x, float y, float z) : VectorN(Uninit{})
308 {
309 _arr = {x, y, z};
310 }
311
313 Vector3 cross(const Vector3& rhs) const
314 {
315 return Vector3(_arr[1] * rhs[2] - _arr[2] * rhs[1],
316 _arr[2] * rhs[0] - _arr[0] * rhs[2],
317 _arr[0] * rhs[1] - _arr[1] * rhs[0]);
318 }
319};
320
323class MX_CORE_API Vector4 : public VectorN<Vector4, float, 4>
324{
325 public:
327 Vector4() = default;
328 Vector4(float x, float y, float z, float w) : VectorN(Uninit{})
329 {
330 _arr = {x, y, z, w};
331 }
332};
333
336class MX_CORE_API Quaternion : public VectorN<Vector4, float, 4>
337{
338 public:
340 Quaternion() = default;
341 Quaternion(float x, float y, float z, float w) : VectorN(Uninit{})
342 {
343 _arr = {x, y, z, w};
344 }
345
346 Quaternion operator*(const Quaternion& q) const
347 {
348 return
349 {
350 _arr[0] * q._arr[3] + _arr[3] * q._arr[0] + _arr[1] * q._arr[2] - _arr[2] * q._arr[1],
351 _arr[1] * q._arr[3] + _arr[3] * q._arr[1] + _arr[2] * q._arr[0] - _arr[0] * q._arr[2],
352 _arr[2] * q._arr[3] + _arr[3] * q._arr[2] + _arr[0] * q._arr[1] - _arr[1] * q._arr[0],
353 _arr[3] * q._arr[3] - _arr[0] * q._arr[0] - _arr[1] * q._arr[1] - _arr[2] * q._arr[2]
354 };
355 }
356
357 Quaternion getNormalized() const
358 {
359 float l = 1.f / getMagnitude() * (_arr[3] < 0 ? -1.f : 1.f); // after normalization, real part will be non-negative
360 return { _arr[0] * l, _arr[1] * l, _arr[2] * l, _arr[3] * l };
361 }
362
363 static Quaternion createFromAxisAngle(const Vector3& v, float a)
364 {
365 float s = std::sin(a * 0.5f);
366 return Quaternion(v[0] * s, v[1] * s, v[2] * s, std::cos(a * 0.5f));
367 }
368
369 public:
370 static const Quaternion IDENTITY;
371};
372
375class MX_CORE_API Color3 : public VectorN<Color3, float, 3>
376{
377 public:
379 Color3() = default;
380 Color3(float r, float g, float b) : VectorN(Uninit{})
381 {
382 _arr = {r, g, b};
383 }
384};
385
388class MX_CORE_API Color4 : public VectorN<Color4, float, 4>
389{
390 public:
392 Color4() = default;
393 Color4(float r, float g, float b, float a) : VectorN(Uninit{})
394 {
395 _arr = {r, g, b, a};
396 }
397};
398
400class MatrixBase { };
401
410template <class M, class S, size_t N> class MatrixN : public MatrixBase
411{
412 public:
413 using RowArray = typename std::array<S, N>;
414 using Iterator = typename std::array<RowArray, N>::iterator;
415 using ConstIterator = typename std::array<RowArray, N>::const_iterator;
416
417 public:
418 MatrixN() : _arr{} { }
419 explicit MatrixN(Uninit) { }
420 explicit MatrixN(S s) { std::fill_n(&_arr[0][0], N * N, s); }
421 explicit MatrixN(const S* begin, const S* end) { std::copy(begin, end, &_arr[0][0]); }
422
425
427 bool operator==(const M& rhs) const { return _arr == rhs._arr; }
428
430 bool operator!=(const M& rhs) const { return _arr != rhs._arr; }
431
434 bool isEquivalent(const M& rhs, S tolerance) const
435 {
436 for (size_t i = 0; i < N; i++)
437 {
438 for (size_t j = 0; j < N; j++)
439 {
440 if (std::abs(_arr[i][j] - rhs[i][j]) > tolerance)
441 {
442 return false;
443 }
444 }
445 }
446 return true;
447 }
448
452
454 RowArray& operator[](size_t i) { return _arr.at(i); }
455
457 const RowArray& operator[](size_t i) const { return _arr.at(i); }
458
462
464 M operator+(const M& rhs) const
465 {
466 M res(Uninit{});
467 for (size_t i = 0; i < N; i++)
468 for (size_t j = 0; j < N; j++)
469 res[i][j] = _arr[i][j] + rhs[i][j];
470 return res;
471 }
472
474 MatrixN& operator+=(const M& rhs)
475 {
476 *this = *this + rhs;
477 return *this;
478 }
479
481 M operator-(const M& rhs) const
482 {
483 M res(Uninit{});
484 for (size_t i = 0; i < N; i++)
485 for (size_t j = 0; j < N; j++)
486 res[i][j] = _arr[i][j] - rhs[i][j];
487 return res;
488 }
489
491 MatrixN& operator-=(const M& rhs)
492 {
493 *this = *this - rhs;
494 return *this;
495 }
496
498 M operator*(S s) const
499 {
500 M res(Uninit{});
501 for (size_t i = 0; i < N; i++)
502 for (size_t j = 0; j < N; j++)
503 res[i][j] = _arr[i][j] * s;
504 return res;
505 }
506
509 {
510 *this = *this * s;
511 return *this;
512 }
513
515 M operator/(S s) const
516 {
517 M res(Uninit{});
518 for (size_t i = 0; i < N; i++)
519 for (size_t j = 0; j < N; j++)
520 res[i][j] = _arr[i][j] / s;
521 return res;
522 }
523
526 {
527 *this = *this / s;
528 return *this;
529 }
530
534
536 M operator*(const M& rhs) const
537 {
538 M res;
539 for (size_t i = 0; i < N; i++)
540 for (size_t j = 0; j < N; j++)
541 for (size_t k = 0; k < N; k++)
542 res[i][j] += _arr[i][k] * rhs[k][j];
543 return res;
544 }
545
547 MatrixN& operator*=(const M& rhs)
548 {
549 *this = *this * rhs;
550 return *this;
551 }
552
555 M operator/(const M& rhs) const
556 {
557 return *this * rhs.getInverse();
558 }
559
562 MatrixN& operator/=(const M& rhs)
563 {
564 *this *= rhs.getInverse();
565 return *this;
566 }
567
569 M getTranspose() const;
570
572 S getDeterminant() const;
573
575 M getAdjugate() const;
576
578 M getInverse() const
579 {
580 return getAdjugate() / getDeterminant();
581 }
582
586
587 Iterator begin() { return _arr.begin(); }
588 ConstIterator begin() const { return _arr.begin(); }
589
590 Iterator end() { return _arr.end(); }
591 ConstIterator end() const { return _arr.end(); }
592
596
598 S* data() { return _arr.front().data(); }
599
601 const S* data() const { return _arr.front().data(); }
602
606
608 static constexpr size_t numRows() { return N; }
609
611 static constexpr size_t numColumns() { return N; }
612
614
615 protected:
616 std::array<RowArray, N> _arr;
617};
618
624class MX_CORE_API Matrix33 : public MatrixN<Matrix33, float, 3>
625{
626 public:
628 Matrix33() = default;
629 Matrix33(float m00, float m01, float m02,
630 float m10, float m11, float m12,
631 float m20, float m21, float m22) :
632 MatrixN(Uninit{})
633 {
634 _arr = {m00, m01, m02,
635 m10, m11, m12,
636 m20, m21, m22};
637 }
638
641
643 Vector3 multiply(const Vector3& v) const;
644
646 Vector2 transformPoint(const Vector2& v) const;
647
649 Vector2 transformVector(const Vector2& v) const;
650
652 Vector3 transformNormal(const Vector3& v) const;
653
655 static Matrix33 createTranslation(const Vector2& v);
656
658 static Matrix33 createScale(const Vector2& v);
659
662 static Matrix33 createRotation(float angle);
663
665
666 public:
667 static const Matrix33 IDENTITY;
668};
669
675class MX_CORE_API Matrix44 : public MatrixN<Matrix44, float, 4>
676{
677 public:
679 Matrix44() = default;
680 Matrix44(float m00, float m01, float m02, float m03,
681 float m10, float m11, float m12, float m13,
682 float m20, float m21, float m22, float m23,
683 float m30, float m31, float m32, float m33) :
684 MatrixN(Uninit{})
685 {
686 _arr = {m00, m01, m02, m03,
687 m10, m11, m12, m13,
688 m20, m21, m22, m23,
689 m30, m31, m32, m33};
690 }
691
694
696 Vector4 multiply(const Vector4& v) const;
697
699 Vector3 transformPoint(const Vector3& v) const;
700
702 Vector3 transformVector(const Vector3& v) const;
703
705 Vector3 transformNormal(const Vector3& v) const;
706
708 static Matrix44 createTranslation(const Vector3& v);
709
711 static Matrix44 createScale(const Vector3& v);
712
715 static Matrix44 createRotationX(float angle);
716
719 static Matrix44 createRotationY(float angle);
720
723 static Matrix44 createRotationZ(float angle);
724
728 static Matrix44 createRotation(const Quaternion& quaternion);
729
731
732 public:
733 static const Matrix44 IDENTITY;
734};
735
736} // namespace MaterialX
737
738#endif
Import and export declarations for the Core library.
Utility methods.
void hashCombine(size_t &seed, const T &value)
Combine the hash of a value with an existing seed.
Definition: Util.h:52
A three-component color value.
Definition: Types.h:376
A four-component color value.
Definition: Types.h:389
A 3x3 matrix of floating-point values.
Definition: Types.h:625
A 4x4 matrix of floating-point values.
Definition: Types.h:676
The base class for square matrices of scalar values.
Definition: Types.h:400
The class template for square matrices of scalar values.
Definition: Types.h:411
M operator/(const M &rhs) const
Divide the first matrix by the second (computed as the product of the first matrix and the inverse of...
Definition: Types.h:555
MatrixN & operator/=(S s)
Component-wise division of a matrix by a scalar.
Definition: Types.h:525
MatrixN & operator*=(const M &rhs)
Compute the matrix product.
Definition: Types.h:547
MatrixN & operator/=(const M &rhs)
Divide the first matrix by the second (computed as the product of the first matrix and the inverse of...
Definition: Types.h:562
M getAdjugate() const
Return the adjugate of the matrix.
static constexpr size_t numRows()
Return the number of rows in this matrix.
Definition: Types.h:608
MatrixN & operator*=(S s)
Component-wise multiplication of a matrix and a scalar.
Definition: Types.h:508
const S * data() const
Return a const pointer to the underlying data array.
Definition: Types.h:601
RowArray & operator[](size_t i)
Return the row array at the given index.
Definition: Types.h:454
const RowArray & operator[](size_t i) const
Return the const row array at the given index.
Definition: Types.h:457
M getTranspose() const
Return the transpose of the matrix.
M operator-(const M &rhs) const
Component-wise subtraction of two matrices.
Definition: Types.h:481
static constexpr size_t numColumns()
Return the number of columns in this matrix.
Definition: Types.h:611
M getInverse() const
Return the inverse of the matrix.
Definition: Types.h:578
MatrixN & operator+=(const M &rhs)
Component-wise addition of two matrices.
Definition: Types.h:474
M operator+(const M &rhs) const
Component-wise addition of two matrices.
Definition: Types.h:464
M operator/(S s) const
Component-wise division of a matrix by a scalar.
Definition: Types.h:515
bool operator==(const M &rhs) const
Return true if the given matrix is identical to this one.
Definition: Types.h:427
MatrixN & operator-=(const M &rhs)
Component-wise subtraction of two matrices.
Definition: Types.h:491
M operator*(S s) const
Component-wise multiplication of a matrix and a scalar.
Definition: Types.h:498
S getDeterminant() const
Return the determinant of the matrix.
M operator*(const M &rhs) const
Compute the matrix product.
Definition: Types.h:536
bool operator!=(const M &rhs) const
Return true if the given matrix differs from this one.
Definition: Types.h:430
S * data()
Return a pointer to the underlying data array.
Definition: Types.h:598
bool isEquivalent(const M &rhs, S tolerance) const
Return true if the given matrix is equivalent to this one within a given floating-point tolerance.
Definition: Types.h:434
A quaternion vector.
Definition: Types.h:337
A tag class for constructing vectors and matrices without initialization.
Definition: Types.h:46
A vector of two floating-point values.
Definition: Types.h:284
float cross(const Vector2 &rhs) const
Return the cross product of two vectors.
Definition: Types.h:294
A vector of three floating-point values.
Definition: Types.h:303
Vector3 cross(const Vector3 &rhs) const
Return the cross product of two vectors.
Definition: Types.h:313
A vector of four floating-point values.
Definition: Types.h:324
The base class for vectors of scalar values.
Definition: Types.h:43
Function object for hashing vectors.
Definition: Types.h:257
The class template for vectors of scalar values.
Definition: Types.h:54
VectorN & operator+=(const V &rhs)
Component-wise addition of two vectors.
Definition: Types.h:106
bool operator!=(const V &rhs) const
Return true if the given vector differs from this one.
Definition: Types.h:74
V operator*(S s) const
Component-wise multiplication of a vector by a scalar.
Definition: Types.h:165
V operator+(const V &rhs) const
Component-wise addition of two vectors.
Definition: Types.h:97
V operator-() const
Unary negation of a vector.
Definition: Types.h:199
const S * data() const
Return a const pointer to the underlying data array.
Definition: Types.h:253
VectorN & operator*=(S s)
Component-wise multiplication of a vector by a scalar.
Definition: Types.h:174
V operator*(const V &rhs) const
Component-wise multiplication of two vectors.
Definition: Types.h:131
V operator/(const V &rhs) const
Component-wise division of two vectors.
Definition: Types.h:148
const S & operator[](size_t i) const
Return the const scalar value at the given index.
Definition: Types.h:90
VectorN & operator*=(const V &rhs)
Component-wise multiplication of two vectors.
Definition: Types.h:140
V operator/(S s) const
Component-wise division of a vector by a scalar.
Definition: Types.h:182
bool operator<(const V &rhs) const
Compare two vectors lexicographically.
Definition: Types.h:77
bool operator==(const V &rhs) const
Return true if the given vector is identical to this one.
Definition: Types.h:71
VectorN & operator-=(const V &rhs)
Component-wise subtraction of two vectors.
Definition: Types.h:123
V getNormalized() const
Return a normalized vector.
Definition: Types.h:221
S dot(const V &rhs) const
Return the dot product of two vectors.
Definition: Types.h:227
V operator-(const V &rhs) const
Component-wise subtraction of two vectors.
Definition: Types.h:114
S & operator[](size_t i)
Return the scalar value at the given index.
Definition: Types.h:87
static constexpr size_t numElements()
Return the number of scalar elements for the vector.
Definition: Types.h:273
S getMagnitude() const
Return the magnitude of the vector.
Definition: Types.h:212
VectorN & operator/=(const V &rhs)
Component-wise division of two vectors.
Definition: Types.h:157
VectorN & operator/=(S s)
Component-wise division of a vector by a scalar.
Definition: Types.h:191
S * data()
Return a pointer to the underlying data array.
Definition: Types.h:250