HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
9 /// @file
10 /// Data types for rendering functionality
11 
12 #include <MaterialXRender/Export.h>
13 
14 #include <MaterialXCore/Types.h>
15 
17 
18 /// @class Quaternion
19 /// A quaternion vector
20 class MX_RENDER_API Quaternion : public VectorN<Vector4, float, 4>
21 {
22  public:
24  Quaternion() = default;
25  Quaternion(float x, float y, float z, float w) :
26  VectorN(Uninit{})
27  {
28  _arr = { x, y, z, w };
29  }
30 
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 
59 /// @class Vector3d
60 /// A vector of three floating-point values (double-precision)
61 class MX_RENDER_API Vector3d : public VectorN<Vector3d, double, 3>
62 {
63  public:
65  Vector3d() = default;
66  Vector3d(double x, double y, double z) : VectorN(Uninit{})
67  {
68  _arr = {x, y, z};
69  }
70 };
71 
72 /// @class Vector4d
73 /// A vector of four floating-point values (double-precision)
74 class MX_RENDER_API Vector4d : public VectorN<Vector4d, double, 4>
75 {
76  public:
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 
85 /// @class Color3d
86 /// A three-component color value (double-precision)
87 class MX_RENDER_API Color3d : public VectorN<Color3d, double, 3>
88 {
89  public:
91  Color3d() = default;
92  Color3d(double r, double g, double b) : VectorN(Uninit{})
93  {
94  _arr = {r, g, b};
95  }
96 };
97 
98 /// @class Half
99 /// A lightweight 16-bit half-precision float class. Based on the public-domain
100 /// implementation by Paul Tessier.
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 
201 
202 #endif
SYS_API double cos(double x)
Definition: SYS_FPUMath.h:69
bool operator!=(Half rhs) const
Definition: Types.h:108
S getMagnitude() const
Return the magnitude of the vector.
Definition: Types.h:214
bool operator>(Half rhs) const
Definition: Types.h:110
Definition: Types.h:74
Half(float value)
Definition: Types.h:104
std::array< S, N > _arr
Definition: Types.h:280
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
Color3d(double r, double g, double b)
Definition: Types.h:92
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLboolean GLboolean g
Definition: glcorearb.h:1222
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
Half & operator/=(Half rhs)
Definition: Types.h:122
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLint y
Definition: glcorearb.h:103
bool operator<(Half rhs) const
Definition: Types.h:109
Half operator*(Half rhs) const
Definition: Types.h:116
static Quaternion createFromAxisAngle(const Vector3 &v, float a)
Definition: Types.h:47
GLdouble GLdouble GLdouble q
Definition: glad.h:2445
Quaternion operator*(const Quaternion &q) const
Definition: Types.h:31
bool operator<=(Half rhs) const
Definition: Types.h:111
GLfloat f
Definition: glcorearb.h:1926
A tag class for constructing vectors and matrices without initialization.
Definition: Types.h:48
Half & operator-=(Half rhs)
Definition: Types.h:120
Definition: Types.h:87
Half operator-(Half rhs) const
Definition: Types.h:115
GLint GLuint mask
Definition: glcorearb.h:124
Quaternion(float x, float y, float z, float w)
Definition: Types.h:25
Half operator+(Half rhs) const
Definition: Types.h:114
static const Quaternion IDENTITY
Definition: Types.h:56
bool operator>=(Half rhs) const
Definition: Types.h:112
Definition: Types.h:55
Vector3d(double x, double y, double z)
Definition: Types.h:66
IMATH_HOSTDEVICE constexpr int sign(T a) IMATH_NOEXCEPT
Definition: ImathFun.h:33
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
Quaternion getNormalized() const
Definition: Types.h:41
Vector4d(double x, double y, double z, double w)
Definition: Types.h:79
#define MX_RENDER_API
Definition: Export.h:18
Definition: Types.h:305
Definition: Types.h:61
bool operator==(Half rhs) const
Definition: Types.h:107
Half operator/(Half rhs) const
Definition: Types.h:117
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
Definition: core.h:1131
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
Half & operator+=(Half rhs)
Definition: Types.h:119
GLboolean r
Definition: glcorearb.h:1222
Definition: Types.h:101
SYS_API double sin(double x)
Definition: SYS_FPUMath.h:71
Half & operator*=(Half rhs)
Definition: Types.h:121
Half operator-() const
Definition: Types.h:124