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  {
163  UT_FixedVector<T,SIZE> result;
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;
198  UT_FixedVector<T,SIZE> result;
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  {
211  UT_FixedVector<T,SIZE> result;
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  if (vec[i] < -tol || vec[i] > tol)
309  return false;
310  }
311  return true;
312  }
313  template<typename S,bool S_INSTANTIATED>
315  {
316  for (exint i = 0; i < SIZE; ++i)
317  {
318  if (vec[i] < that[i]-tol || vec[i] > that[i]+tol)
319  return false;
320  }
321  return true;
322  }
324  {
325  T v = vec[0];
326  for (exint i = 1; i < SIZE; ++i)
327  v = (vec[i] > v) ? vec[i] : v;
328  return v;
329  }
331  {
332  T v = vec[0];
333  for (exint i = 1; i < SIZE; ++i)
334  v = (vec[i] < v) ? vec[i] : v;
335  return v;
336  }
338  {
339  T v = vec[0];
340  for (exint i = 1; i < SIZE; ++i)
341  v += vec[i];
342  return v / SIZE;
343  }
344 
346  {
347  typedef typename UT_StorageAtLeast32Bit<T,T>::Type TheType;
348  TheType a0(vec[0]);
349  TheType result(a0*a0);
350  for (exint i = 1; i < SIZE; ++i)
351  {
352  TheType ai(vec[i]);
353  result += ai*ai;
354  }
355  return result;
356  }
358  {
359  typename Storage::MathFloat len2(length2());
360  return SYSsqrt(len2);
361  }
362  template<typename S,bool S_INSTANTIATED>
364  {
365  typedef typename UT_StorageAtLeast32Bit<T,S>::Type TheType;
366  TheType result(TheType(vec[0])*TheType(that[0]));
367  for (exint i = 1; i < SIZE; ++i)
368  result += TheType(vec[i])*TheType(that[i]);
369  return result;
370  }
371  template<typename S,bool S_INSTANTIATED>
373  {
374  typedef typename UT_StorageAtLeast32Bit<T,S>::Type TheType;
375  TheType v(TheType(vec[0]) - TheType(that[0]));
376  TheType result(v*v);
377  for (exint i = 1; i < SIZE; ++i)
378  {
379  v = TheType(vec[i]) - TheType(that[i]);
380  result += v*v;
381  }
382  return result;
383  }
384  template<typename S,bool S_INSTANTIATED>
386  {
387  typename UT_StorageNum<typename UT_StorageBetter<T,S>::Type>::MathFloat dist2(distance2(that));
388  return SYSsqrt(dist2);
389  }
390 
392  {
393  typename Storage::AtLeast32Bit len2 = length2();
395  return typename Storage::MathFloat(0);
396  if (len2 == typename Storage::AtLeast32Bit(1))
397  return typename Storage::MathFloat(1);
398  typename Storage::MathFloat len = SYSsqrt(typename Storage::MathFloat(len2));
399  // Check if the square root is equal 1. sqrt(1+dx) ~ 1+dx/2,
400  // so it may get rounded to 1 when it wasn't 1 before.
401  if (len != typename Storage::MathFloat(1))
402  (*this) /= len;
403  return len;
404  }
405 
406 public:
408 };
409 
410 /// NOTE: Strictly speaking, this should use UT_StorageBetter<T,S>::Type,
411 /// but in the interests of avoiding accidental precision escalation,
412 /// it uses T.
413 template<typename T,exint SIZE,bool INSTANTIATED,typename S>
415 {
416  T t(that);
417  UT_FixedVector<T,SIZE> result;
418  for (exint i = 0; i < SIZE; ++i)
419  result[i] = t * a[i];
420  return result;
421 }
422 
423 template<typename T, exint SIZE, bool INSTANTIATED, typename S, bool S_INSTANTIATED>
426 {
427  return a.dot(b);
428 }
429 
430 template<typename T>
432 {
434  typedef T DataType;
435  static const exint TupleSize = 1;
436  static const bool isVectorType = false;
437 };
438 
439 template<typename T,exint SIZE,bool INSTANTIATED>
441 {
443  typedef T DataType;
444  static const exint TupleSize = SIZE;
445  static const bool isVectorType = true;
446 };
447 
448 #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:609
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
const GLdouble * v
Definition: glcorearb.h:836
SYS_FORCE_INLINE bool isZero() const noexcept
UT_FixedVector< T, SIZE, INSTANTIATED > FixedVectorType
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()
void AtLeast32Bit
Definition: UT_Storage.h:72
SYS_FORCE_INLINE UT_FixedVector< typename UT_StorageBetter< T, S >::Type, SIZE > operator/(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
bool SYSisNan(const F f)
Definition: SYS_Math.h:173
SYS_FORCE_INLINE UT_FixedVector< T, SIZE > operator*(T that) const
static const exint TupleSize
png_uint_32 i
Definition: png.h:2877
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:73
int64 exint
Definition: SYS_Types.h:116
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:125
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 >::AtLeast32Bit dot(const UT_FixedVector< T, SIZE, INSTANTIATED > &a, const UT_FixedVector< S, SIZE, S_INSTANTIATED > &b)
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
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
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:221
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 Storage::MathFloat normalize()
#define SIZE
Definition: simple.C:40
#define SYS_FTOLERANCE
Definition: SYS_Types.h:203
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
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.
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:129
SYS_FORCE_INLINE T avgComponent() const
static const exint theSize
SYS_FORCE_INLINE void operator-=(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that)
bool SYSisFinite(fpreal16 f)
Definition: SYS_Math.h:198