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:
9  * A vector class templated on its scalar type and dimension.
10  *
11  * All additional operations that work with UT_FixedVector should be
12  * implemented as free-standing functions!
13  * Don't add any new members or friends.
14  *
15  */
16 
17 #pragma once
18 
19 #ifndef __UT_FixedVector__
20 #define __UT_FixedVector__
21 
22 #include "UT_Assert.h"
23 #include "UT_FixedArrayMath.h"
24 #include "UT_FixedVectorTraits.h"
25 
26 #include <SYS/SYS_Math.h>
27 #include <SYS/SYS_Types.h>
28 #include <SYS/SYS_TypeTraits.h>
29 #include <SYS/SYS_TypeDecorate.h>
30 
31 #include <utility>
32 
33 template<typename T, exint D> struct UT_FixedVectorFixed;
34 
35 template< typename T, exint D >
37 {
38 public:
39  typedef T value_type;
40  static constexpr int tuple_size = D;
41 
42  UT_FixedVector() = default;
43 
44  constexpr explicit UT_FixedVector( const T (&a)[ D ] ) noexcept :
45  vec{}
46  {
48  }
49 
50  template< typename S >
51  constexpr explicit UT_FixedVector( const UT_FixedVector< S, D >& a ) noexcept :
52  vec{}
53  {
55  }
56 
57  constexpr const T& operator[]( exint i ) const noexcept
58  {
59  UT_ASSERT_P( ( 0 <= i ) && ( i < tuple_size ) );
60 
61  return vec[ i ];
62  }
63 
64  constexpr T& operator[]( exint i ) noexcept
65  {
66  UT_ASSERT_P( ( 0 <= i ) && ( i < tuple_size ) );
67 
68  return vec[ i ];
69  }
70 
71  constexpr const T* data() const noexcept
72  {
73  return vec;
74  }
75 
76  constexpr T* data() noexcept
77  {
78  return vec;
79  }
80 
81  constexpr UT_FixedVector& operator+=( const UT_FixedVector& a ) noexcept
82  {
83  UT::FA::Add< T, tuple_size >{}( vec, a.vec );
84  return *this;
85  }
86 
87  constexpr UT_FixedVector& operator-=( const UT_FixedVector& a ) noexcept
88  {
90  return *this;
91  }
92 
93  constexpr UT_FixedVector& operator*=( const T& a ) noexcept
94  {
96  return *this;
97  }
98 
99  constexpr UT_FixedVector<T, D> operator-() const noexcept
100  {
101  UT_FixedVector t{ *this };
103  return t;
104  }
105 
106 private:
107  T vec[ D ];
108 
109  template< typename AS, std::size_t ... Is >
110  constexpr explicit UT_FixedVector( const AS& as, const std::index_sequence< Is ... > ) noexcept :
111  vec{ as[ Is ] ... }
112  {
113  }
114 
115  friend constexpr UT_FixedVector operator*( const T& a, const UT_FixedVector& b ) noexcept
116  {
117  UT_FixedVector r{ b };
119 
120  return r;
121  }
122 
123  friend constexpr UT_FixedVector operator/( const UT_FixedVector& a, const T& b ) noexcept
124  {
125  UT_FixedVector r{ a };
127 
128  return r;
129  }
130 
131  friend constexpr bool isZero( const UT_FixedVector& a ) noexcept
132  {
133  return UT::FA::IsUniformZero< T, tuple_size >{}( a.vec_ );
134  }
135 
136  friend constexpr auto dot( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
137  {
138  return UT::FA::Dot< T, tuple_size >{}( a.vec, b.vec );
139  }
140 
141  friend constexpr auto length2( const UT_FixedVector& a ) noexcept
142  {
143  return UT::FA::Length2< T, tuple_size >{}( a.vec );
144  }
145 
146  friend constexpr auto distance2( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
147  {
148  return UT::FA::Distance2< T, tuple_size >{}( a.vec, b.vec );
149  }
150 
151  friend constexpr bool operator==( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
152  {
153  return UT::FA::AreEqual< T, tuple_size >{}( a.vec, b.vec );
154  }
155 
156  friend constexpr bool operator!=( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
157  {
158  return ! UT::FA::AreEqual< T, tuple_size >{}( a.vec, b.vec );
159  }
160 
161  /// Lexicographic order comparison operators
162  /// @{
163  friend constexpr bool operator<( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
164  {
165  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) < 0;
166  }
167 
168  friend constexpr bool operator<=( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
169  {
170  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) <= 0;
171  }
172 
173  friend constexpr bool operator>( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
174  {
175  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) > 0;
176  }
177 
178  friend constexpr bool operator>=( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
179  {
180  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) >= 0;
181  }
182  /// @}
183 
184  friend struct UT_FixedVectorFixed< T, D >;
185 };
186 
187 template< typename T, exint D >
188 constexpr T length( const UT_FixedVector< T, D >& a ) noexcept
189 {
190  return SYSsqrt( length2( a ) );
191 }
192 
193 template< typename T, exint D >
194 constexpr T distance(
195  const UT_FixedVector< T, D >& a,
197 ) noexcept
198 {
199  return SYSsqrt( distance2( a, b ) );
200 }
201 
202 template< typename T, exint D >
203 constexpr T normalize( UT_FixedVector< T, D >& a ) noexcept
204 {
206 }
207 
208 template< typename T, exint D >
210  const UT_FixedVector< T, D >& a,
212 ) noexcept
213 {
216  return t;
217 }
218 
219 template< typename T, exint D >
221  const UT_FixedVector< T, D >& a,
223 ) noexcept
224 {
227  return t;
228 }
229 
230 // Function object that creates a uniform vector
231 template< typename T, exint D >
233 {
234  constexpr UT_FixedVector< T, D > operator()( const T& a ) const noexcept
235  {
238  return t;
239  }
240 };
241 
242 // Function object that creates a zero vector
243 template< typename T, exint D >
245 {
247  {
248  return UT_FixedVectorUniform< T, D >{}( 0 );
249  }
250 };
251 
252 template< typename T, exint D >
254 {
256  r += b;
257  return r;
258 }
259 
260 template< typename T, exint D >
262 {
264  r -= b;
265  return r;
266 }
267 
268 template<typename T, exint D>
270 {
272  typedef T DataType;
273  static const exint TupleSize = D;
274  static const bool isVectorType = true;
275 };
276 // UT_FixedVectorT in the role of a fixed-size array.
277 
278 template< typename T, exint D >
279 struct SYS_IsFixedArrayNoCVRef< UT_FixedVector< T, D > > : std::integral_constant< bool, true > {};
280 
281 template< typename T, exint D >
283 
284 template< typename T, exint D >
285 struct SYS_FixedArraySizeNoCVRef< UT_FixedVector< T, D > > : std::integral_constant< std::size_t, D > {};
286 
287 // UT_FixedVectorFixed<T, D> is a function object that
288 // creates a UT_FixedVector<T, D> from a fixed array-like type TS,
289 // examples of which include T[D], UT_FixedVector<T, D> and UT_FixedArray<T, D> (AKA std::array<T, D>)
290 template<typename T, exint D>
291 struct UT_FixedVectorFixed
292 {
293  template< typename TS >
294  constexpr UT_FixedVector<T, D> operator()(const TS& as) const noexcept
295  {
296  SYS_STATIC_ASSERT( SYS_IsFixedArrayOf_v< TS, T, D > );
297 
298  return UT_FixedVector< T, D >( as, std::make_index_sequence< D >{} );
299  }
300 };
301 
302 // Convert a fixed array-like type TS into a UT_FixedVector< T >.
303 // This allows conversion to UT_FixedVector without fixing T and D.
304 // Instead, the element type of TS determines the type T and D.
305 template< typename TS >
306 constexpr UT_FixedVector< SYS_FixedArrayElement_t< TS >, SYS_FixedArraySize_v< TS > >
307 UTmakeFixedVector( const TS& as ) noexcept
308 {
310  constexpr exint D = SYS_FixedArraySize_v< TS >;
311 
312  return UT_FixedVectorFixed< T, D >{}( as );
313 }
314 
315 // UT_FromFixed<V> creates a V from a flat, fixed array-like representation
316 
317 // Primary
318 template <typename V >
320 
321 // Partial specialization for UT_FixedVector<T, D>
322 template<typename T, exint D>
324 
325 // Relocation traits
326 
327 template<typename T, exint D>
328 struct SafeTrivialRelocationNoCV< UT_FixedVector< T, D > > : SafeTrivialRelocationNoCV< SYS_RemoveCV_t< T > > {};
329 
330 template<typename T, exint D>
331 struct UnsafeTrivialRelocationNoCV< UT_FixedVector< T, D > > : UnsafeTrivialRelocationNoCV< SYS_RemoveCV_t< T > > {};
332 
333 #endif
#define SYS_STATIC_ASSERT(expr)
constexpr UT_FixedVector< T, D > operator-() const noexcept
UT_FixedVector()=default
Mat3< typename promote< T0, T1 >::type > operator+(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Add corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:577
constexpr const T * data() const noexcept
friend constexpr auto distance2(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
constexpr UT_FixedVector< SYS_FixedArrayElement_t< TS >, SYS_FixedArraySize_v< TS > > UTmakeFixedVector(const TS &as) noexcept
typename SYS_FixedArrayElement< T >::type SYS_FixedArrayElement_t
friend constexpr bool operator>(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
fpreal64 distance2(const UT_VectorD &v1, const UT_VectorD &v2)
Distance squared (L2) aka quadrance.
Definition: UT_Vector.h:399
int64 exint
Definition: SYS_Types.h:125
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
friend constexpr bool operator<=(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
constexpr const T & operator[](exint i) const noexcept
constexpr UT_FixedVector(const UT_FixedVector< S, D > &a) noexcept
static const exint TupleSize
friend constexpr bool operator<(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
constexpr UT_FixedVector & operator*=(const T &a) noexcept
constexpr UT_FixedVector< T, D > productComponentwise(const UT_FixedVector< T, D > &a, const UT_FixedVector< T, D > &b) noexcept
friend constexpr UT_FixedVector operator/(const UT_FixedVector &a, const T &b) noexcept
constexpr UT_FixedVector< T, D > operator()(const T &a) const noexcept
constexpr UT_FixedVector< T, D > operator()() const noexcept
friend constexpr bool isZero(const UT_FixedVector &a) noexcept
constexpr UT_FixedVector & operator+=(const UT_FixedVector &a) noexcept
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Subtract corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:587
constexpr UT_FixedVector< T, D > quotientComponentwise(const UT_FixedVector< T, D > &a, const UT_FixedVector< T, D > &b) noexcept
friend constexpr bool operator==(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
static const bool isVectorType
constexpr T & operator[](exint i) noexcept
static constexpr int tuple_size
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLdouble t
Definition: glad.h:2397
constexpr UT_FixedVector & operator-=(const UT_FixedVector &a) noexcept
constexpr T distance(const UT_FixedVector< T, D > &a, const UT_FixedVector< T, D > &b) noexcept
constexpr UT_FixedVector< T, D > operator()(const TS &as) const noexcept
friend constexpr bool operator!=(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
friend constexpr auto dot(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
GLboolean r
Definition: glcorearb.h:1222
#define const
Definition: zconf.h:214
friend constexpr bool operator>=(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
friend constexpr auto length2(const UT_FixedVector &a) noexcept
friend constexpr UT_FixedVector operator*(const T &a, const UT_FixedVector &b) noexcept
constexpr T * data() noexcept
constexpr T normalize(UT_FixedVector< T, D > &a) noexcept
constexpr T length(const UT_FixedVector< T, D > &a) noexcept
constexpr UT_FixedVector(const T(&a)[D]) noexcept