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 // TM & (c) 2019 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3 // All rights reserved. See LICENSE.txt for license.
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 uint16_t toFloat16(float value)
158  {
159  Bits v, s;
160  v.f = value;
161  uint32_t sign = (uint32_t) (v.si & signN);
162  v.si ^= sign;
163  sign >>= shiftSign; // logical shift
164  s.si = mulN;
165  s.si = (int32_t) (s.f * v.f); // correct subnormals
166  v.si ^= (s.si ^ v.si) & -(minN > v.si);
167  v.si ^= (infN ^ v.si) & -((infN > v.si) & (v.si > maxN));
168  v.si ^= (nanN ^ v.si) & -((nanN > v.si) & (v.si > infN));
169  v.ui >>= shift; // logical shift
170  v.si ^= ((v.si - maxD) ^ v.si) & -(v.si > maxC);
171  v.si ^= ((v.si - minD) ^ v.si) & -(v.si > subC);
172  return (uint16_t) (v.ui | sign);
173  }
174 
175  static float toFloat32(uint16_t value)
176  {
177  Bits v;
178  v.ui = value;
179  int32_t sign = v.si & signC;
180  v.si ^= sign;
181  sign <<= shiftSign;
182  v.si ^= ((v.si + minD) ^ v.si) & -(v.si > subC);
183  v.si ^= ((v.si + maxD) ^ v.si) & -(v.si > maxC);
184  Bits s;
185  s.si = mulC;
186  s.f *= float(v.si);
187  int32_t mask = (norC > v.si) ? -1 : 1;
188  v.si <<= shift;
189  v.si ^= (s.si ^ v.si) & mask;
190  v.si |= sign;
191  return v.f;
192  }
193 
194  private:
195  uint16_t _data;
196 };
197 
199 
200 #endif
SYS_API double cos(double x)
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
bool operator!=(Half rhs) const
Definition: Types.h:108
S getMagnitude() const
Return the magnitude of the vector.
Definition: Types.h:211
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:277
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
Color3d(double r, double g, double b)
Definition: Types.h:92
GLboolean GLboolean g
Definition: glcorearb.h:1222
Half & operator/=(Half rhs)
Definition: Types.h:122
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
Quaternion operator*(const Quaternion &q) const
Definition: Types.h:31
GLdouble l
Definition: glew.h:9164
GLint GLenum GLint x
Definition: glcorearb.h:409
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
bool operator<=(Half rhs) const
Definition: Types.h:111
GLdouble GLdouble GLdouble GLdouble q
Definition: glew.h:1419
A tag class for constructing vectors and matrices without initialization.
Definition: Types.h:45
Half & operator-=(Half rhs)
Definition: Types.h:120
Definition: Types.h:87
GLint GLuint mask
Definition: glcorearb.h:124
const GLdouble * v
Definition: glcorearb.h:837
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
Half operator-(Half rhs) const
Definition: Types.h:115
Quaternion(float x, float y, float z, float w)
Definition: Types.h:25
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
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:52
Vector3d(double x, double y, double z)
Definition: Types.h:66
IMATH_HOSTDEVICE constexpr int sign(T a) IMATH_NOEXCEPT
Definition: ImathFun.h:33
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:302
Definition: Types.h:61
bool operator==(Half rhs) const
Definition: Types.h:107
Half operator/(Half rhs) const
Definition: Types.h:117
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLfloat f
Definition: glcorearb.h:1926
Definition: core.h:1131
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
Half & operator+=(Half rhs)
Definition: Types.h:119
GLboolean r
Definition: glcorearb.h:1222
GLdouble s
Definition: glew.h:1395
Definition: Types.h:101
GLint y
Definition: glcorearb.h:103
SYS_API double sin(double x)
Half & operator*=(Half rhs)
Definition: Types.h:121
Half operator-() const
Definition: Types.h:124