HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_FixedVector.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_FixedVector.h (UT Library, C++)
7  *
8  * COMMENTS: A vector class templated on its size and data type.
9  */
10 
11 #pragma once
12 
13 #ifndef __UT_FixedVector__
14 #define __UT_FixedVector__
15 
16 #include "UT_Assert.h"
17 #include "UT_Storage.h"
18 #include <SYS/SYS_Inline.h>
19 #include <SYS/SYS_Math.h>
20 #include <SYS/SYS_Types.h>
21 #include <SYS/SYS_TypeTraits.h>
22 
23 template<typename T,exint SIZE,bool INSTANTIATED=false>
25 {
26 public:
28  typedef T value_type;
29  typedef T theType;
30  static const exint theSize = SIZE;
31 
33 
34  SYS_FORCE_INLINE UT_FixedVector() = default;
35 
36  /// Initializes every component to the same value
37  SYS_FORCE_INLINE explicit UT_FixedVector(T that) noexcept
38  {
39  for (exint i = 0; i < SIZE; ++i)
40  vec[i] = that;
41  }
42 
43  SYS_FORCE_INLINE UT_FixedVector(const ThisType &that) = default;
44  SYS_FORCE_INLINE UT_FixedVector(ThisType &&that) = default;
45 
46  /// Converts vector of S into vector of T,
47  /// or just copies if same type.
48  template<typename S,bool S_INSTANTIATED>
50  {
51  for (exint i = 0; i < SIZE; ++i)
52  vec[i] = that[i];
53  }
54 
55  template<typename S>
56  SYS_FORCE_INLINE UT_FixedVector(const S that[SIZE]) noexcept
57  {
58  for (exint i = 0; i < SIZE; ++i)
59  vec[i] = that[i];
60  }
61 
62  SYS_FORCE_INLINE const T &operator[](exint i) const noexcept
63  {
64  UT_ASSERT_P(i >= 0 && i < SIZE);
65  return vec[i];
66  }
68  {
69  UT_ASSERT_P(i >= 0 && i < SIZE);
70  return vec[i];
71  }
72 
73  SYS_FORCE_INLINE constexpr const T *data() const noexcept
74  {
75  return vec;
76  }
77  SYS_FORCE_INLINE T *data() noexcept
78  {
79  return vec;
80  }
81 
82  SYS_FORCE_INLINE ThisType &operator=(const ThisType &that) = default;
83  SYS_FORCE_INLINE ThisType &operator=(ThisType &&that) = default;
84 
85  template <typename S,bool S_INSTANTIATED>
87  {
88  for (exint i = 0; i < SIZE; ++i)
89  vec[i] = that[i];
90  return *this;
91  }
92  SYS_FORCE_INLINE const ThisType &operator=(T that) noexcept
93  {
94  for (exint i = 0; i < SIZE; ++i)
95  vec[i] = that;
96  return *this;
97  }
98  template<typename S,bool S_INSTANTIATED>
100  {
101  for (exint i = 0; i < SIZE; ++i)
102  vec[i] += that[i];
103  }
105  {
106  for (exint i = 0; i < SIZE; ++i)
107  vec[i] += that;
108  }
109  template<typename S,bool S_INSTANTIATED>
111  {
112  typedef typename UT_StorageBetter<T,S>::Type Type;
114  for (exint i = 0; i < SIZE; ++i)
115  result[i] = vec[i] + that[i];
116  return result;
117  }
118  template<typename S,bool S_INSTANTIATED>
120  {
121  for (exint i = 0; i < SIZE; ++i)
122  vec[i] -= that[i];
123  }
125  {
126  for (exint i = 0; i < SIZE; ++i)
127  vec[i] -= that;
128  }
129  template<typename S,bool S_INSTANTIATED>
131  {
132  typedef typename UT_StorageBetter<T,S>::Type Type;
134  for (exint i = 0; i < SIZE; ++i)
135  result[i] = vec[i] - that[i];
136  return result;
137  }
138  template<typename S,bool S_INSTANTIATED>
140  {
141  for (exint i = 0; i < SIZE; ++i)
142  vec[i] *= that[i];
143  }
144  template<typename S,bool S_INSTANTIATED>
146  {
147  typedef typename UT_StorageBetter<T,S>::Type Type;
149  for (exint i = 0; i < SIZE; ++i)
150  result[i] = vec[i] * that[i];
151  return result;
152  }
154  {
155  for (exint i = 0; i < SIZE; ++i)
156  vec[i] *= that;
157  }
158  /// NOTE: Strictly speaking, this should use UT_StorageBetter<T,S>::Type,
159  /// but in the interests of avoiding accidental precision escalation,
160  /// it uses T.
162  {
164  for (exint i = 0; i < SIZE; ++i)
165  result[i] = vec[i] * that;
166  return result;
167  }
168  template<typename S,bool S_INSTANTIATED>
170  {
171  for (exint i = 0; i < SIZE; ++i)
172  vec[i] /= that[i];
173  }
174  template<typename S,bool S_INSTANTIATED>
176  {
177  typedef typename UT_StorageBetter<T,S>::Type Type;
179  for (exint i = 0; i < SIZE; ++i)
180  result[i] = vec[i] / that[i];
181  return result;
182  }
183 
185  {
186  typename Storage::MathFloat t(that);
187  t = typename Storage::MathFloat(1)/t;
188  for (exint i = 0; i < SIZE; ++i)
189  vec[i] *= t;
190  }
191  /// NOTE: Strictly speaking, this should use UT_StorageBetter<T,S>::Type,
192  /// but in the interests of avoiding accidental precision escalation,
193  /// it uses T.
195  {
196  typename Storage::MathFloat t(that);
197  t = typename Storage::MathFloat(1)/t;
199  for (exint i = 0; i < SIZE; ++i)
200  result[i] = vec[i] * t;
201  return result;
202  }
204  {
205  for (exint i = 0; i < SIZE; ++i)
206  vec[i] = -vec[i];
207  }
208 
210  {
212  for (exint i = 0; i < SIZE; ++i)
213  result[i] = -vec[i];
214  return result;
215  }
216 
217  template<typename S,bool S_INSTANTIATED>
219  {
220  for (exint i = 0; i < SIZE; ++i)
221  {
222  if (vec[i] != T(that[i]))
223  return false;
224  }
225  return true;
226  }
227  template<typename S,bool S_INSTANTIATED>
229  {
230  return !(*this==that);
231  }
232  /// Lexicographic order comparison operators
233  /// @{
234  template<typename S,bool S_INSTANTIATED>
235  SYS_FORCE_INLINE bool operator<(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const noexcept
236  {
237  for (exint i = 0; i < SIZE-1; ++i)
238  {
239  if (vec[i] != that[i])
240  return (vec[i] < that[i]);
241  }
242  return (vec[SIZE-1] < that[SIZE-1]);
243  }
244  template<typename S,bool S_INSTANTIATED>
245  SYS_FORCE_INLINE bool operator<=(const UT_FixedVector<S,SIZE,S_INSTANTIATED> &that) const noexcept
246  {
247  for (exint i = 0; i < SIZE-1; ++i)
248  {
249  if (vec[i] != that[i])
250  return (vec[i] < that[i]);
251  }
252  return (vec[SIZE-1] <= that[SIZE-1]);
253  }
254  template<typename S,bool S_INSTANTIATED>
256  {
257  // NOTE: Because of NaNs, this can't just be !(*this <= that)
258  for (exint i = 0; i < SIZE-1; ++i)
259  {
260  if (vec[i] != that[i])
261  return (vec[i] > that[i]);
262  }
263  return (vec[SIZE-1] > that[SIZE-1]);
264  }
265  template<typename S,bool S_INSTANTIATED>
267  {
268  // NOTE: Because of NaNs, this can't just be !(*this < that)
269  for (exint i = 0; i < SIZE-1; ++i)
270  {
271  if (vec[i] != that[i])
272  return (vec[i] > that[i]);
273  }
274  return (vec[SIZE-1] >= that[SIZE-1]);
275  }
276  /// @}
277  SYS_FORCE_INLINE bool isNan() const
278  {
279  for (exint i = 0; i < SIZE; ++i)
280  {
281  if (SYSisNan(vec[i]))
282  return true;
283  }
284  return false;
285  }
287  {
288  for (exint i = 0; i < SIZE; ++i)
289  {
290  if (!SYSisFinite(vec[i]))
291  return false;
292  }
293  return true;
294  }
295  SYS_FORCE_INLINE bool isZero() const noexcept
296  {
297  for (exint i = 0; i < SIZE; ++i)
298  {
299  if (vec[i] != T(0))
300  return false;
301  }
302  return true;
303  }
305  {
306  for (exint i = 0; i < SIZE; ++i)
307  {
308  // NB: We return FALSE for NaN values
309  if (!(vec[i] >= -tol) || !(vec[i] <= tol))
310  return false;
311  }
312  return true;
313  }
314  template<typename S,bool S_INSTANTIATED>
316  {
317  for (exint i = 0; i < SIZE; ++i)
318  {
319  // NB: We return FALSE for NaN values
320  if (!(vec[i] >= that[i]-tol) || !(vec[i] <= that[i]+tol))
321  return false;
322  }
323  return true;
324  }
326  {
327  T v = vec[0];
328  for (exint i = 1; i < SIZE; ++i)
329  v = (vec[i] > v) ? vec[i] : v;
330  return v;
331  }
333  {
334  T v = vec[0];
335  for (exint i = 1; i < SIZE; ++i)
336  v = (vec[i] < v) ? vec[i] : v;
337  return v;
338  }
340  {
341  T v = vec[0];
342  for (exint i = 1; i < SIZE; ++i)
343  v += vec[i];
344  return v / SIZE;
345  }
346 
348  {
349  typedef typename UT_StorageAtLeast32Bit<T,T>::Type TheType;
350  TheType a0(vec[0]);
351  TheType result(a0*a0);
352  for (exint i = 1; i < SIZE; ++i)
353  {
354  TheType ai(vec[i]);
355  result += ai*ai;
356  }
357  return result;
358  }
360  {
361  typename Storage::MathFloat len2(length2());
362  return SYSsqrt(len2);
363  }
364  template<typename S,bool S_INSTANTIATED>
366  {
367  typedef typename UT_StorageAtLeast32Bit<T,S>::Type TheType;
368  TheType result(TheType(vec[0])*TheType(that[0]));
369  for (exint i = 1; i < SIZE; ++i)
370  result += TheType(vec[i])*TheType(that[i]);
371  return result;
372  }
373  template<typename S,bool S_INSTANTIATED>
375  {
376  typedef typename UT_StorageAtLeast32Bit<T,S>::Type TheType;
377  TheType v(TheType(vec[0]) - TheType(that[0]));
378  TheType result(v*v);
379  for (exint i = 1; i < SIZE; ++i)
380  {
381  v = TheType(vec[i]) - TheType(that[i]);
382  result += v*v;
383  }
384  return result;
385  }
386  template<typename S,bool S_INSTANTIATED>
388  {
389  typename UT_StorageNum<typename UT_StorageBetter<T,S>::Type>::MathFloat dist2(distance2(that));
390  return SYSsqrt(dist2);
391  }
392 
394  {
395  typename Storage::AtLeast32Bit len2 = length2();
397  return typename Storage::MathFloat(0);
398  if (len2 == typename Storage::AtLeast32Bit(1))
399  return typename Storage::MathFloat(1);
400  typename Storage::MathFloat len = SYSsqrt(typename Storage::MathFloat(len2));
401  // Check if the square root is equal 1. sqrt(1+dx) ~ 1+dx/2,
402  // so it may get rounded to 1 when it wasn't 1 before.
403  if (len != typename Storage::MathFloat(1))
404  (*this) /= len;
405  return len;
406  }
407 
408 public:
410 };
411 
412 /// NOTE: Strictly speaking, this should use UT_StorageBetter<T,S>::Type,
413 /// but in the interests of avoiding accidental precision escalation,
414 /// it uses T.
415 template<typename T,exint SIZE,bool INSTANTIATED,typename S>
417 {
418  T t(that);
420  for (exint i = 0; i < SIZE; ++i)
421  result[i] = t * a[i];
422  return result;
423 }
424 
425 template<typename T, exint SIZE, bool INSTANTIATED, typename S, bool S_INSTANTIATED>
428 {
429  return a.dot(b);
430 }
431 
432 template<typename T>
434 {
436  typedef T DataType;
437  static const exint TupleSize = 1;
438  static const bool isVectorType = false;
439 };
440 
441 template<typename T,exint SIZE,bool INSTANTIATED>
443 {
445  typedef T DataType;
446  static const exint TupleSize = SIZE;
447  static const bool isVectorType = true;
448 };
449 
450 #endif
SYS_FORCE_INLINE void operator*=(T that)
SYS_FORCE_INLINE void operator+=(T that)
SYS_FORCE_INLINE T minComponent() const
SYS_FORCE_INLINE constexpr const T * data() const noexcept
SYS_FORCE_INLINE UT_FixedVector(const S that[SIZE]) noexcept
SYS_FORCE_INLINE UT_FixedVector< T, SIZE > operator-() const
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Multiply each element of the given matrix by scalar and return the result.
Definition: Mat3.h:582
SYS_FORCE_INLINE const ThisType & operator=(T that) noexcept
SYS_FORCE_INLINE void operator*=(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that)
SYS_FORCE_INLINE bool operator>(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const noexcept
SYS_FORCE_INLINE bool isNan() const
SYS_FORCE_INLINE const T & operator[](exint i) const noexcept
SYS_FORCE_INLINE void operator/=(T that)
UT_StorageNum< T > Storage
SYS_FORCE_INLINE ThisType & operator=(const ThisType &that)=default
SYS_FORCE_INLINE bool equalZero(T tol=T(SYS_FTOLERANCE)) const
SYS_FORCE_INLINE bool isZero() const noexcept
bool SYSisFinite(fpreal64 f)
Definition: SYS_Math.h:194
UT_FixedVector< T, SIZE, INSTANTIATED > FixedVectorType
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
SYS_FORCE_INLINE UT_FixedVector< typename UT_StorageBetter< T, S >::Type, SIZE > operator+(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const
SYS_FORCE_INLINE void negate()
int64 exint
Definition: SYS_Types.h:125
void AtLeast32Bit
Definition: UT_Storage.h:88
constexpr bool SYSisNan(const F f)
Definition: SYS_Math.h:178
SYS_FORCE_INLINE UT_FixedVector< typename UT_StorageBetter< T, S >::Type, SIZE > operator/(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const
SYS_FORCE_INLINE UT_FixedVector< T, SIZE > operator*(T that) const
const GLdouble * v
Definition: glew.h:1391
static const exint TupleSize
SYS_FORCE_INLINE bool operator==(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const noexcept
SYS_FORCE_INLINE UT_FixedVector()=default
SYS_FORCE_INLINE UT_FixedVector(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) noexcept
SYS_FORCE_INLINE bool isFinite() const
SYS_FORCE_INLINE void operator+=(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that)
SYS_FORCE_INLINE void operator/=(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that)
fpreal32 MathFloat
Definition: UT_Storage.h:89
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:134
SYS_FORCE_INLINE UT_FixedVector< T, SIZE > operator/(T that) const
static const bool isVectorType
SYS_FORCE_INLINE UT_StorageAtLeast32Bit< T, S >::Type distance2(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
SYS_FORCE_INLINE bool operator>=(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const noexcept
SYS_FORCE_INLINE bool operator!=(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const noexcept
SYS_FORCE_INLINE UT_StorageAtLeast32Bit< T, T >::Type length2() const noexcept
SYS_FORCE_INLINE T maxComponent() const
SYS_FORCE_INLINE UT_StorageNum< typename UT_StorageBetter< T, S >::Type >::MathFloat distance(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const
SYS_FORCE_INLINE T * data() noexcept
UT_FixedVector< T, SIZE, INSTANTIATED > ThisType
SYS_FORCE_INLINE UT_FixedVector< typename UT_StorageBetter< T, S >::Type, SIZE > operator*(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const
SYS_FORCE_INLINE UT_StorageAtLeast32Bit< T, S >::Type dot(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
typename SYS_SelectType< typename UT_StorageNum< typename UT_StorageBetter< T0, T1 >::Type >::AtLeast32Bit, T0, UT_StorageNum< typename UT_StorageBetter< T0, T1 >::Type >::theStorage==UT_Storage::INVALID >::type Type
Definition: UT_Storage.h:254
SYS_FORCE_INLINE T & operator[](exint i) noexcept
SYS_FORCE_INLINE bool isEqual(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that, S tol=S(SYS_FTOLERANCE)) const
UT_FixedVector< T, 1 > FixedVectorType
SYS_FORCE_INLINE UT_StorageAtLeast32Bit< T, S >::Type dot(const UT_FixedVector< T, SIZE, INSTANTIATED > &a, const UT_FixedVector< S, SIZE, S_INSTANTIATED > &b)
SYS_FORCE_INLINE Storage::MathFloat normalize()
GLuint64EXT * result
Definition: glew.h:14007
#define SIZE
Definition: simple.C:40
#define SYS_FTOLERANCE
Definition: SYS_Types.h:208
SYS_FORCE_INLINE void operator-=(T that)
#define const
Definition: zconf.h:214
SYS_FORCE_INLINE ThisType & operator=(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) noexcept
vint4 min(const vint4 &a, const vint4 &b)
Definition: simd.h:4694
SYS_FORCE_INLINE UT_FixedVector< typename UT_StorageBetter< T, S >::Type, SIZE > operator-(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const
SYS_FORCE_INLINE Storage::MathFloat length() const
SYS_FORCE_INLINE UT_FixedVector(T that) noexcept
Initializes every component to the same value.
SYS_FORCE_INLINE T avgComponent() const
static const exint theSize
GLdouble GLdouble t
Definition: glew.h:1398
SYS_FORCE_INLINE void operator-=(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that)
GLenum GLsizei len
Definition: glew.h:7752