MaterialX 1.38.10
Loading...
Searching...
No Matches
Types.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
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
19MATERIALX_NAMESPACE_BEGIN
20
21extern MX_CORE_API const string DEFAULT_TYPE_STRING;
22extern MX_CORE_API const string FILENAME_TYPE_STRING;
23extern MX_CORE_API const string GEOMNAME_TYPE_STRING;
24extern MX_CORE_API const string STRING_TYPE_STRING;
25extern MX_CORE_API const string BSDF_TYPE_STRING;
26extern MX_CORE_API const string EDF_TYPE_STRING;
27extern MX_CORE_API const string VDF_TYPE_STRING;
28extern MX_CORE_API const string SURFACE_SHADER_TYPE_STRING;
29extern MX_CORE_API const string DISPLACEMENT_SHADER_TYPE_STRING;
30extern MX_CORE_API const string VOLUME_SHADER_TYPE_STRING;
31extern MX_CORE_API const string LIGHT_SHADER_TYPE_STRING;
32extern MX_CORE_API const string MATERIAL_TYPE_STRING;
33extern MX_CORE_API const string SURFACE_MATERIAL_NODE_STRING;
34extern MX_CORE_API const string VOLUME_MATERIAL_NODE_STRING;
35extern MX_CORE_API const string MULTI_OUTPUT_TYPE_STRING;
36extern MX_CORE_API const string NONE_TYPE_STRING;
37extern MX_CORE_API const string VALUE_STRING_TRUE;
38extern MX_CORE_API const string VALUE_STRING_FALSE;
39extern MX_CORE_API const string NAME_PREFIX_SEPARATOR;
40extern MX_CORE_API const string NAME_PATH_SEPARATOR;
41extern MX_CORE_API const string ARRAY_VALID_SEPARATORS;
42extern MX_CORE_API const string ARRAY_PREFERRED_SEPARATOR;
43
45class VectorBase { };
46
48class Uninit { };
49
55template <class V, class S, size_t N> class VectorN : public VectorBase
56{
57 public:
58 using Iterator = typename std::array<S, N>::iterator;
59 using ConstIterator = typename std::array<S, N>::const_iterator;
60
61 public:
62 VectorN() : _arr{} { }
63 explicit VectorN(Uninit) { }
64 explicit VectorN(S s) { _arr.fill(s); }
65 explicit VectorN(const std::array<S, N>& arr) : _arr(arr) { }
66 explicit VectorN(const vector<S>& vec) { std::copy(vec.begin(), vec.end(), _arr.begin()); }
67 explicit VectorN(const S* begin, const S* end) { std::copy(begin, end, _arr.begin()); }
68
71
73 bool operator==(const V& rhs) const { return _arr == rhs._arr; }
74
76 bool operator!=(const V& rhs) const { return _arr != rhs._arr; }
77
79 bool operator<(const V& rhs) const
80 {
81 return _arr < rhs._arr;
82 }
83
87
89 S& operator[](size_t i) { return _arr.at(i); }
90
92 const S& operator[](size_t i) const { return _arr.at(i); }
93
97
99 V operator+(const V& rhs) const
100 {
101 V res(Uninit{});
102 for (size_t i = 0; i < N; i++)
103 res[i] = _arr[i] + rhs[i];
104 return res;
105 }
106
108 VectorN& operator+=(const V& rhs)
109 {
110 for (size_t i = 0; i < N; i++)
111 _arr[i] += rhs[i];
112 return *this;
113 }
114
116 V operator-(const V& rhs) const
117 {
118 V res(Uninit{});
119 for (size_t i = 0; i < N; i++)
120 res[i] = _arr[i] - rhs[i];
121 return res;
122 }
123
125 VectorN& operator-=(const V& rhs)
126 {
127 for (size_t i = 0; i < N; i++)
128 _arr[i] -= rhs[i];
129 return *this;
130 }
131
133 V operator*(const V& rhs) const
134 {
135 V res(Uninit{});
136 for (size_t i = 0; i < N; i++)
137 res[i] = _arr[i] * rhs[i];
138 return res;
139 }
140
142 VectorN& operator*=(const V& rhs)
143 {
144 for (size_t i = 0; i < N; i++)
145 _arr[i] *= rhs[i];
146 return *this;
147 }
148
150 V operator/(const V& rhs) const
151 {
152 V res(Uninit{});
153 for (size_t i = 0; i < N; i++)
154 res[i] = _arr[i] / rhs[i];
155 return res;
156 }
157
159 VectorN& operator/=(const V& rhs)
160 {
161 for (size_t i = 0; i < N; i++)
162 _arr[i] /= rhs[i];
163 return *this;
164 }
165
167 V operator*(S s) const
168 {
169 V res(Uninit{});
170 for (size_t i = 0; i < N; i++)
171 res[i] = _arr[i] * s;
172 return res;
173 }
174
177 {
178 for (size_t i = 0; i < N; i++)
179 _arr[i] *= s;
180 return *this;
181 }
182
184 V operator/(S s) const
185 {
186 V res(Uninit{});
187 for (size_t i = 0; i < N; i++)
188 res[i] = _arr[i] / s;
189 return res;
190 }
191
194 {
195 for (size_t i = 0; i < N; i++)
196 _arr[i] /= s;
197 return *this;
198 }
199
201 V operator-() const
202 {
203 V res(Uninit{});
204 for (size_t i = 0; i < N; i++)
205 res[i] = -_arr[i];
206 return res;
207 }
208
212
214 S getMagnitude() const
215 {
216 S res{};
217 for (size_t i = 0; i < N; i++)
218 res += _arr[i] * _arr[i];
219 return std::sqrt(res);
220 }
221
224 {
225 return *this / getMagnitude();
226 }
227
229 S dot(const V& rhs) const
230 {
231 S res{};
232 for (size_t i = 0; i < N; i++)
233 res += _arr[i] * rhs[i];
234 return res;
235 }
236
240
241 Iterator begin() { return _arr.begin(); }
242 ConstIterator begin() const { return _arr.begin(); }
243
244 Iterator end() { return _arr.end(); }
245 ConstIterator end() const { return _arr.end(); }
246
250
252 S* data() { return _arr.data(); }
253
255 const S* data() const { return _arr.data(); }
256
258 class Hash
259 {
260 public:
261 size_t operator()(const V& v) const noexcept
262 {
263 size_t h = 0;
264 for (size_t i = 0; i < N; i++)
265 hashCombine(h, v[i]);
266 return h;
267 }
268 };
269
273
275 static constexpr size_t numElements() { return N; }
276
278
279 protected:
280 std::array<S, N> _arr;
281};
282
285class MX_CORE_API Vector2 : public VectorN<Vector2, float, 2>
286{
287 public:
288 using VectorN<Vector2, float, 2>::VectorN;
289 Vector2() = default;
290 Vector2(float x, float y) :
291 VectorN(Uninit{})
292 {
293 _arr = { x, y };
294 }
295
297 float cross(const Vector2& rhs) const
298 {
299 return _arr[0] * rhs[1] - _arr[1] * rhs[0];
300 }
301};
302
305class MX_CORE_API Vector3 : public VectorN<Vector3, float, 3>
306{
307 public:
308 using VectorN<Vector3, float, 3>::VectorN;
309 Vector3() = default;
310 Vector3(float x, float y, float z) :
311 VectorN(Uninit{})
312 {
313 _arr = { x, y, z };
314 }
315
317 Vector3 cross(const Vector3& rhs) const
318 {
319 return Vector3(_arr[1] * rhs[2] - _arr[2] * rhs[1],
320 _arr[2] * rhs[0] - _arr[0] * rhs[2],
321 _arr[0] * rhs[1] - _arr[1] * rhs[0]);
322 }
323};
324
327class MX_CORE_API Vector4 : public VectorN<Vector4, float, 4>
328{
329 public:
330 using VectorN<Vector4, float, 4>::VectorN;
331 Vector4() = default;
332 Vector4(float x, float y, float z, float w) :
333 VectorN(Uninit{})
334 {
335 _arr = { x, y, z, w };
336 }
337};
338
341class MX_CORE_API Color3 : public VectorN<Color3, float, 3>
342{
343 public:
344 using VectorN<Color3, float, 3>::VectorN;
345 Color3() = default;
346 Color3(float r, float g, float b) :
347 VectorN(Uninit{})
348 {
349 _arr = { r, g, b };
350 }
351
354 Color3 linearToSrgb() const;
355
358 Color3 srgbToLinear() const;
359};
360
363class MX_CORE_API Color4 : public VectorN<Color4, float, 4>
364{
365 public:
366 using VectorN<Color4, float, 4>::VectorN;
367 Color4() = default;
368 Color4(float r, float g, float b, float a) :
369 VectorN(Uninit{})
370 {
371 _arr = { r, g, b, a };
372 }
373};
374
376class MatrixBase { };
377
386template <class M, class S, size_t N> class MatrixN : public MatrixBase
387{
388 public:
389 using RowArray = typename std::array<S, N>;
390 using Iterator = typename std::array<RowArray, N>::iterator;
391 using ConstIterator = typename std::array<RowArray, N>::const_iterator;
392
393 public:
394 MatrixN() : _arr{} { }
395 explicit MatrixN(Uninit) { }
396 explicit MatrixN(S s) { std::fill_n(&_arr[0][0], N * N, s); }
397 explicit MatrixN(const S* begin, const S* end) { std::copy(begin, end, &_arr[0][0]); }
398
401
403 bool operator==(const M& rhs) const { return _arr == rhs._arr; }
404
406 bool operator!=(const M& rhs) const { return _arr != rhs._arr; }
407
410 bool isEquivalent(const M& rhs, S tolerance) const
411 {
412 for (size_t i = 0; i < N; i++)
413 {
414 for (size_t j = 0; j < N; j++)
415 {
416 if (std::abs(_arr[i][j] - rhs[i][j]) > tolerance)
417 {
418 return false;
419 }
420 }
421 }
422 return true;
423 }
424
428
430 RowArray& operator[](size_t i) { return _arr.at(i); }
431
433 const RowArray& operator[](size_t i) const { return _arr.at(i); }
434
438
440 M operator+(const M& rhs) const
441 {
442 M res(Uninit{});
443 for (size_t i = 0; i < N; i++)
444 for (size_t j = 0; j < N; j++)
445 res[i][j] = _arr[i][j] + rhs[i][j];
446 return res;
447 }
448
450 MatrixN& operator+=(const M& rhs)
451 {
452 *this = *this + rhs;
453 return *this;
454 }
455
457 M operator-(const M& rhs) const
458 {
459 M res(Uninit{});
460 for (size_t i = 0; i < N; i++)
461 for (size_t j = 0; j < N; j++)
462 res[i][j] = _arr[i][j] - rhs[i][j];
463 return res;
464 }
465
467 MatrixN& operator-=(const M& rhs)
468 {
469 *this = *this - rhs;
470 return *this;
471 }
472
474 M operator*(S s) const
475 {
476 M res(Uninit{});
477 for (size_t i = 0; i < N; i++)
478 for (size_t j = 0; j < N; j++)
479 res[i][j] = _arr[i][j] * s;
480 return res;
481 }
482
485 {
486 *this = *this * s;
487 return *this;
488 }
489
491 M operator/(S s) const
492 {
493 M res(Uninit{});
494 for (size_t i = 0; i < N; i++)
495 for (size_t j = 0; j < N; j++)
496 res[i][j] = _arr[i][j] / s;
497 return res;
498 }
499
502 {
503 *this = *this / s;
504 return *this;
505 }
506
510
512 M operator*(const M& rhs) const
513 {
514 M res;
515 for (size_t i = 0; i < N; i++)
516 for (size_t j = 0; j < N; j++)
517 for (size_t k = 0; k < N; k++)
518 res[i][j] += _arr[i][k] * rhs[k][j];
519 return res;
520 }
521
523 MatrixN& operator*=(const M& rhs)
524 {
525 *this = *this * rhs;
526 return *this;
527 }
528
531 M operator/(const M& rhs) const
532 {
533 return *this * rhs.getInverse();
534 }
535
538 MatrixN& operator/=(const M& rhs)
539 {
540 *this *= rhs.getInverse();
541 return *this;
542 }
543
547
548 Iterator begin() { return _arr.begin(); }
549 ConstIterator begin() const { return _arr.begin(); }
550
551 Iterator end() { return _arr.end(); }
552 ConstIterator end() const { return _arr.end(); }
553
557
559 S* data() { return _arr.front().data(); }
560
562 const S* data() const { return _arr.front().data(); }
563
567
569 static constexpr size_t numRows() { return N; }
570
572 static constexpr size_t numColumns() { return N; }
573
575
576 protected:
577 std::array<RowArray, N> _arr;
578};
579
585class MX_CORE_API Matrix33 : public MatrixN<Matrix33, float, 3>
586{
587 public:
588 using MatrixN<Matrix33, float, 3>::MatrixN;
589 Matrix33() = default;
590 Matrix33(float m00, float m01, float m02,
591 float m10, float m11, float m12,
592 float m20, float m21, float m22) :
593 MatrixN(Uninit{})
594 {
595 _arr = { RowArray{ m00, m01, m02 },
596 RowArray{ m10, m11, m12 },
597 RowArray{ m20, m21, m22 } };
598 }
599
602
604 Matrix33 getTranspose() const;
605
607 float getDeterminant() const;
608
610 Matrix33 getAdjugate() const;
611
614 {
615 return getAdjugate() / getDeterminant();
616 }
617
621
623 Vector3 multiply(const Vector3& v) const;
624
626 Vector2 transformPoint(const Vector2& v) const;
627
629 Vector2 transformVector(const Vector2& v) const;
630
632 Vector3 transformNormal(const Vector3& v) const;
633
635 static Matrix33 createTranslation(const Vector2& v);
636
638 static Matrix33 createScale(const Vector2& v);
639
642 static Matrix33 createRotation(float angle);
643
645
646 public:
647 static const Matrix33 IDENTITY;
648};
649
655class MX_CORE_API Matrix44 : public MatrixN<Matrix44, float, 4>
656{
657 public:
658 using MatrixN<Matrix44, float, 4>::MatrixN;
659 Matrix44() = default;
660 Matrix44(float m00, float m01, float m02, float m03,
661 float m10, float m11, float m12, float m13,
662 float m20, float m21, float m22, float m23,
663 float m30, float m31, float m32, float m33) :
664 MatrixN(Uninit{})
665 {
666 _arr = { RowArray{ m00, m01, m02, m03 },
667 RowArray{ m10, m11, m12, m13 },
668 RowArray{ m20, m21, m22, m23 },
669 RowArray{ m30, m31, m32, m33 } };
670 }
671
674
676 Matrix44 getTranspose() const;
677
679 float getDeterminant() const;
680
682 Matrix44 getAdjugate() const;
683
686 {
687 return getAdjugate() / getDeterminant();
688 }
689
693
695 Vector4 multiply(const Vector4& v) const;
696
698 Vector3 transformPoint(const Vector3& v) const;
699
701 Vector3 transformVector(const Vector3& v) const;
702
704 Vector3 transformNormal(const Vector3& v) const;
705
707 static Matrix44 createTranslation(const Vector3& v);
708
710 static Matrix44 createScale(const Vector3& v);
711
714 static Matrix44 createRotationX(float angle);
715
718 static Matrix44 createRotationY(float angle);
719
722 static Matrix44 createRotationZ(float angle);
723
725
726 public:
727 static const Matrix44 IDENTITY;
728};
729
730MATERIALX_NAMESPACE_END
731
732#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:58
A three-component color value.
Definition: Types.h:342
A four-component color value.
Definition: Types.h:364
A 3x3 matrix of floating-point values.
Definition: Types.h:586
Matrix33 getInverse() const
Return the inverse of the matrix.
Definition: Types.h:613
A 4x4 matrix of floating-point values.
Definition: Types.h:656
Matrix44 getInverse() const
Return the inverse of the matrix.
Definition: Types.h:685
The base class for square matrices of scalar values.
Definition: Types.h:376
The class template for square matrices of scalar values.
Definition: Types.h:387
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:531
MatrixN & operator/=(S s)
Component-wise division of a matrix by a scalar.
Definition: Types.h:501
MatrixN & operator*=(const M &rhs)
Compute the matrix product.
Definition: Types.h:523
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:538
static constexpr size_t numRows()
Return the number of rows in this matrix.
Definition: Types.h:569
MatrixN & operator*=(S s)
Component-wise multiplication of a matrix and a scalar.
Definition: Types.h:484
const S * data() const
Return a const pointer to the underlying data array.
Definition: Types.h:562
RowArray & operator[](size_t i)
Return the row array at the given index.
Definition: Types.h:430
const RowArray & operator[](size_t i) const
Return the const row array at the given index.
Definition: Types.h:433
M operator-(const M &rhs) const
Component-wise subtraction of two matrices.
Definition: Types.h:457
static constexpr size_t numColumns()
Return the number of columns in this matrix.
Definition: Types.h:572
MatrixN & operator+=(const M &rhs)
Component-wise addition of two matrices.
Definition: Types.h:450
M operator+(const M &rhs) const
Component-wise addition of two matrices.
Definition: Types.h:440
M operator/(S s) const
Component-wise division of a matrix by a scalar.
Definition: Types.h:491
bool operator==(const M &rhs) const
Return true if the given matrix is identical to this one.
Definition: Types.h:403
MatrixN & operator-=(const M &rhs)
Component-wise subtraction of two matrices.
Definition: Types.h:467
M operator*(S s) const
Component-wise multiplication of a matrix and a scalar.
Definition: Types.h:474
M operator*(const M &rhs) const
Compute the matrix product.
Definition: Types.h:512
bool operator!=(const M &rhs) const
Return true if the given matrix differs from this one.
Definition: Types.h:406
S * data()
Return a pointer to the underlying data array.
Definition: Types.h:559
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:410
A tag class for constructing vectors and matrices without initialization.
Definition: Types.h:48
A vector of two floating-point values.
Definition: Types.h:286
float cross(const Vector2 &rhs) const
Return the cross product of two vectors.
Definition: Types.h:297
A vector of three floating-point values.
Definition: Types.h:306
Vector3 cross(const Vector3 &rhs) const
Return the cross product of two vectors.
Definition: Types.h:317
A vector of four floating-point values.
Definition: Types.h:328
The base class for vectors of scalar values.
Definition: Types.h:45
Function object for hashing vectors.
Definition: Types.h:259
The class template for vectors of scalar values.
Definition: Types.h:56
VectorN & operator+=(const V &rhs)
Component-wise addition of two vectors.
Definition: Types.h:108
bool operator!=(const V &rhs) const
Return true if the given vector differs from this one.
Definition: Types.h:76
V operator*(S s) const
Component-wise multiplication of a vector by a scalar.
Definition: Types.h:167
V operator+(const V &rhs) const
Component-wise addition of two vectors.
Definition: Types.h:99
V operator-() const
Unary negation of a vector.
Definition: Types.h:201
const S * data() const
Return a const pointer to the underlying data array.
Definition: Types.h:255
VectorN & operator*=(S s)
Component-wise multiplication of a vector by a scalar.
Definition: Types.h:176
V operator*(const V &rhs) const
Component-wise multiplication of two vectors.
Definition: Types.h:133
V operator/(const V &rhs) const
Component-wise division of two vectors.
Definition: Types.h:150
const S & operator[](size_t i) const
Return the const scalar value at the given index.
Definition: Types.h:92
VectorN & operator*=(const V &rhs)
Component-wise multiplication of two vectors.
Definition: Types.h:142
V operator/(S s) const
Component-wise division of a vector by a scalar.
Definition: Types.h:184
bool operator<(const V &rhs) const
Compare two vectors lexicographically.
Definition: Types.h:79
bool operator==(const V &rhs) const
Return true if the given vector is identical to this one.
Definition: Types.h:73
VectorN & operator-=(const V &rhs)
Component-wise subtraction of two vectors.
Definition: Types.h:125
V getNormalized() const
Return a normalized vector.
Definition: Types.h:223
S dot(const V &rhs) const
Return the dot product of two vectors.
Definition: Types.h:229
V operator-(const V &rhs) const
Component-wise subtraction of two vectors.
Definition: Types.h:116
S & operator[](size_t i)
Return the scalar value at the given index.
Definition: Types.h:89
static constexpr size_t numElements()
Return the number of scalar elements for the vector.
Definition: Types.h:275
S getMagnitude() const
Return the magnitude of the vector.
Definition: Types.h:214
VectorN & operator/=(const V &rhs)
Component-wise division of two vectors.
Definition: Types.h:159
VectorN & operator/=(S s)
Component-wise division of a vector by a scalar.
Definition: Types.h:193
S * data()
Return a pointer to the underlying data array.
Definition: Types.h:252