MaterialX 1.38.9
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_RENDER_TYPES_H
7#define MATERIALX_RENDER_TYPES_H
8
11
13
14#include <MaterialXCore/Types.h>
15
16MATERIALX_NAMESPACE_BEGIN
17
20class MX_RENDER_API Quaternion : public VectorN<Vector4, float, 4>
21{
22 public:
23 using VectorN<Vector4, float, 4>::VectorN;
24 Quaternion() = default;
25 Quaternion(float x, float y, float z, float w) :
27 {
28 _arr = { x, y, z, w };
29 }
30
31 Quaternion operator*(const Quaternion& q) const
32 {
33 return {
34 _arr[0] * q._arr[3] + _arr[3] * q._arr[0] + _arr[1] * q._arr[2] - _arr[2] * q._arr[1],
35 _arr[1] * q._arr[3] + _arr[3] * q._arr[1] + _arr[2] * q._arr[0] - _arr[0] * q._arr[2],
36 _arr[2] * q._arr[3] + _arr[3] * q._arr[2] + _arr[0] * q._arr[1] - _arr[1] * q._arr[0],
37 _arr[3] * q._arr[3] - _arr[0] * q._arr[0] - _arr[1] * q._arr[1] - _arr[2] * q._arr[2]
38 };
39 }
40
42 {
43 float l = 1.f / getMagnitude() * (_arr[3] < 0 ? -1.f : 1.f); // after normalization, real part will be non-negative
44 return { _arr[0] * l, _arr[1] * l, _arr[2] * l, _arr[3] * l };
45 }
46
47 static Quaternion createFromAxisAngle(const Vector3& v, float a)
48 {
49 float s = std::sin(a * 0.5f);
50 return Quaternion(v[0] * s, v[1] * s, v[2] * s, std::cos(a * 0.5f));
51 }
52
53 Matrix44 toMatrix() const;
54
55 public:
56 static const Quaternion IDENTITY;
57};
58
61class MX_RENDER_API Vector3d : public VectorN<Vector3d, double, 3>
62{
63 public:
64 using VectorN<Vector3d, double, 3>::VectorN;
65 Vector3d() = default;
66 Vector3d(double x, double y, double z) : VectorN(Uninit{})
67 {
68 _arr = { x, y, z };
69 }
70};
71
74class MX_RENDER_API Vector4d : public VectorN<Vector4d, double, 4>
75{
76 public:
77 using VectorN<Vector4d, double, 4>::VectorN;
78 Vector4d() = default;
79 Vector4d(double x, double y, double z, double w) : VectorN(Uninit{})
80 {
81 _arr = { x, y, z, w };
82 }
83};
84
87class MX_RENDER_API Color3d : public VectorN<Color3d, double, 3>
88{
89 public:
90 using VectorN<Color3d, double, 3>::VectorN;
91 Color3d() = default;
92 Color3d(double r, double g, double b) : VectorN(Uninit{})
93 {
94 _arr = { r, g, b };
95 }
96};
97
101class MX_RENDER_API Half
102{
103 public:
104 explicit Half(float value) : _data(toFloat16(value)) { }
105 operator float() const { return toFloat32(_data); }
106
107 bool operator==(Half rhs) const { return float(*this) == float(rhs); }
108 bool operator!=(Half rhs) const { return float(*this) != float(rhs); }
109 bool operator<(Half rhs) const { return float(*this) < float(rhs); }
110 bool operator>(Half rhs) const { return float(*this) > float(rhs); }
111 bool operator<=(Half rhs) const { return float(*this) <= float(rhs); }
112 bool operator>=(Half rhs) const { return float(*this) >= float(rhs); }
113
114 Half operator+(Half rhs) const { return Half(float(*this) + float(rhs)); }
115 Half operator-(Half rhs) const { return Half(float(*this) - float(rhs)); }
116 Half operator*(Half rhs) const { return Half(float(*this) * float(rhs)); }
117 Half operator/(Half rhs) const { return Half(float(*this) / float(rhs)); }
118
119 Half& operator+=(Half rhs) { return operator=(*this + rhs); }
120 Half& operator-=(Half rhs) { return operator=(*this - rhs); }
121 Half& operator*=(Half rhs) { return operator=(*this * rhs); }
122 Half& operator/=(Half rhs) { return operator=(*this / rhs); }
123
124 Half operator-() const { return Half(-float(*this)); }
125
126 private:
127 union Bits
128 {
129 float f;
130 int32_t si;
131 uint32_t ui;
132 };
133
134 static constexpr int const shift = 13;
135 static constexpr int const shiftSign = 16;
136
137 static constexpr int32_t const infN = 0x7F800000; // flt32 infinity
138 static constexpr int32_t const maxN = 0x477FE000; // max flt16 normal as a flt32
139 static constexpr int32_t const minN = 0x38800000; // min flt16 normal as a flt32
140 static constexpr int32_t const signN = (int32_t) 0x80000000; // flt32 sign bit
141
142 static constexpr int32_t const infC = infN >> shift;
143 static constexpr int32_t const nanN = (infC + 1) << shift; // minimum flt16 nan as a flt32
144 static constexpr int32_t const maxC = maxN >> shift;
145 static constexpr int32_t const minC = minN >> shift;
146 static constexpr int32_t const signC = (int32_t) 0x00008000; // flt16 sign bit
147
148 static constexpr int32_t const mulN = 0x52000000; // (1 << 23) / minN
149 static constexpr int32_t const mulC = 0x33800000; // minN / (1 << (23 - shift))
150
151 static constexpr int32_t const subC = 0x003FF; // max flt32 subnormal down shifted
152 static constexpr int32_t const norC = 0x00400; // min flt32 normal down shifted
153
154 static constexpr int32_t const maxD = infC - maxC - 1;
155 static constexpr int32_t const minD = minC - subC - 1;
156
157 static constexpr int32_t const maxF = 0x7FFFFFBF; // max int32 expressible as a flt32
158
159 static uint16_t toFloat16(float value)
160 {
161 Bits v, s;
162 v.f = value;
163 uint32_t sign = (uint32_t) (v.si & signN);
164 v.si ^= sign;
165 sign >>= shiftSign; // logical shift
166 s.si = mulN;
167 int32_t subN = (int32_t) std::min(s.f * v.f, (float) maxF); // correct subnormals
168 v.si ^= (subN ^ v.si) & -(minN > v.si);
169 v.si ^= (infN ^ v.si) & -((infN > v.si) & (v.si > maxN));
170 v.si ^= (nanN ^ v.si) & -((nanN > v.si) & (v.si > infN));
171 v.ui >>= shift; // logical shift
172 v.si ^= ((v.si - maxD) ^ v.si) & -(v.si > maxC);
173 v.si ^= ((v.si - minD) ^ v.si) & -(v.si > subC);
174 return (uint16_t) (v.ui | sign);
175 }
176
177 static float toFloat32(uint16_t value)
178 {
179 Bits v;
180 v.ui = value;
181 int32_t sign = v.si & signC;
182 v.si ^= sign;
183 sign <<= shiftSign;
184 v.si ^= ((v.si + minD) ^ v.si) & -(v.si > subC);
185 v.si ^= ((v.si + maxD) ^ v.si) & -(v.si > maxC);
186 Bits s;
187 s.si = mulC;
188 s.f *= float(v.si);
189 int32_t mask = (norC > v.si) ? -1 : 1;
190 v.si <<= shift;
191 v.si ^= (s.si ^ v.si) & mask;
192 v.si |= sign;
193 return v.f;
194 }
195
196 private:
197 uint16_t _data;
198};
199
200MATERIALX_NAMESPACE_END
201
202#endif
Data type classes.
Macros for declaring imported and exported symbols.
A three-component color value (double-precision)
Definition: Types.h:88
A lightweight 16-bit half-precision float class.
Definition: Types.h:102
A 4x4 matrix of floating-point values.
Definition: Types.h:656
A quaternion vector.
Definition: Types.h:21
A tag class for constructing vectors and matrices without initialization.
Definition: Types.h:48
A vector of three floating-point values.
Definition: Types.h:306
A vector of three floating-point values (double-precision)
Definition: Types.h:62
A vector of four floating-point values.
Definition: Types.h:328
A vector of four floating-point values (double-precision)
Definition: Types.h:75
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+(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
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
VectorN & operator*=(const V &rhs)
Component-wise multiplication of two vectors.
Definition: Types.h:142
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 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