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 template< typename T, exint D >
33 {
34 public:
35  typedef T value_type;
36  static constexpr int tuple_size = D;
37 
38  UT_FixedVector() = default;
39 
40  constexpr explicit UT_FixedVector( const T (&a)[ D ] ) noexcept :
41  vec{}
42  {
44  }
45 
46  template< typename S >
47  constexpr explicit UT_FixedVector( const UT_FixedVector< S, D >& a ) noexcept :
48  vec{}
49  {
51  }
52 
53  constexpr const T& operator[]( exint i ) const noexcept
54  {
55  UT_ASSERT_P( ( 0 <= i ) && ( i < tuple_size ) );
56 
57  return vec[ i ];
58  }
59 
60  constexpr T& operator[]( exint i ) noexcept
61  {
62  UT_ASSERT_P( ( 0 <= i ) && ( i < tuple_size ) );
63 
64  return vec[ i ];
65  }
66 
67  constexpr const T* data() const noexcept
68  {
69  return vec;
70  }
71 
72  constexpr T* data() noexcept
73  {
74  return vec;
75  }
76 
77  constexpr UT_FixedVector& operator+=( const UT_FixedVector& a ) noexcept
78  {
79  UT::FA::Add< T, tuple_size >{}( vec, a.vec );
80  return *this;
81  }
82 
83  constexpr UT_FixedVector& operator-=( const UT_FixedVector& a ) noexcept
84  {
86  return *this;
87  }
88 
89  constexpr UT_FixedVector& operator*=( const T& a ) noexcept
90  {
92  return *this;
93  }
94 
95  constexpr UT_FixedVector<T, D> operator-() const noexcept
96  {
97  UT_FixedVector t{ *this };
99  return t;
100  }
101 
102 private:
103  T vec[ D ];
104 
105  friend constexpr UT_FixedVector operator*( const T& a, const UT_FixedVector& b ) noexcept
106  {
107  UT_FixedVector r{ b };
109 
110  return r;
111  }
112 
113  friend constexpr UT_FixedVector operator/( const UT_FixedVector& a, const T& b ) noexcept
114  {
115  UT_FixedVector r{ a };
117 
118  return r;
119  }
120 
121  friend constexpr bool isZero( const UT_FixedVector& a ) noexcept
122  {
123  return UT::FA::IsUniformZero< T, tuple_size >{}( a.vec_ );
124  }
125 
126  friend constexpr T dot( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
127  {
128  return UT::FA::Dot< T, tuple_size >{}( a.vec, b.vec );
129  }
130 
131  friend constexpr T length2( const UT_FixedVector& a ) noexcept
132  {
133  return UT::FA::Length2< T, tuple_size >{}( a.vec );
134  }
135 
136  friend constexpr T distance2( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
137  {
138  return UT::FA::Distance2< T, tuple_size >{}( a.vec, b.vec );
139  }
140 
141  friend constexpr bool operator==( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
142  {
143  return UT::FA::AreEqual< T, tuple_size >{}( a.vec, b.vec );
144  }
145 
146  friend constexpr bool operator!=( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
147  {
148  return ! UT::FA::AreEqual< T, tuple_size >{}( a.vec, b.vec );
149  }
150 
151  /// Lexicographic order comparison operators
152  /// @{
153  friend constexpr bool operator<( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
154  {
155  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) < 0;
156  }
157 
158  friend constexpr bool operator<=( const UT_FixedVector& a, const UT_FixedVector& b ) noexcept
159  {
160  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) <= 0;
161  }
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 };
174 
175 template< typename T, exint D >
176 constexpr T length( const UT_FixedVector< T, D >& a ) noexcept
177 {
178  return SYSsqrt( length2( a ) );
179 }
180 
181 template< typename T, exint D >
182 constexpr T distance(
183  const UT_FixedVector< T, D >& a,
185 ) noexcept
186 {
187  return SYSsqrt( distance2( a, b ) );
188 }
189 
190 template< typename T, exint D >
191 constexpr T normalize( UT_FixedVector< T, D >& a ) noexcept
192 {
194 }
195 
196 template< typename T, exint D >
198  const UT_FixedVector< T, D >& a,
200 ) noexcept
201 {
204  return t;
205 }
206 
207 template< typename T, exint D >
209  const UT_FixedVector< T, D >& a,
211 ) noexcept
212 {
215  return t;
216 }
217 
218 // Function object that creates a uniform vector
219 template< typename T, exint D >
221 {
222  constexpr UT_FixedVector< T, D > operator()( const T& a ) noexcept
223  {
226  return t;
227  }
228 };
229 
230 // Function object that creates a zero vector
231 template< typename T, exint D >
233 {
234  constexpr UT_FixedVector< T, D > operator()() noexcept
235  {
236  return UT_FixedVectorUniform< T, D >{}( 0 );
237  }
238 };
239 
240 template< typename T, exint D >
242 {
244  r += b;
245  return r;
246 }
247 
248 template< typename T, exint D >
250 {
252  r -= b;
253  return r;
254 }
255 
256 template<typename T, exint D>
258 {
260  typedef T DataType;
261  static const exint TupleSize = D;
262  static const bool isVectorType = true;
263 };
264 // UT_FixedVectorT in the role of a fixed-size array.
265 
266 template< typename T, exint D >
267 struct SYS_IsFixedArrayNoCVRef< UT_FixedVector< T, D > > : std::integral_constant< bool, true > {};
268 
269 template< typename T, exint D >
271 
272 template< typename T, exint D >
273 struct SYS_FixedArraySizeNoCVRef< UT_FixedVector< T, D > > : std::integral_constant< std::size_t, D > {};
274 
275 // UT_FixedVectorFixed<T, D> is a function object that
276 // creates a UT_FixedVector<T, D> from a fixed array-like type TS,
277 // examples of which include T[D], UT_FixedVector<T, D> and UT_FixedArray<T, D> (AKA std::array<T, D>)
278 template<typename T, exint D>
280 {
281  template< typename TS >
282  constexpr UT_FixedVector<T, D> operator()(const TS& as) const noexcept
283  {
284  SYS_STATIC_ASSERT( SYS_IsFixedArrayOf_v< TS, T, D > );
285 
286  T vec[D]{};
287 
288  for ( exint i = 0; i != D; ++i )
289  {
290  vec[ i ] = as[ i ];
291  }
292 
293  return UT_FixedVector< T, D >( vec );
294  }
295 };
296 
297 // Convert a fixed array-like type TS into a UT_FixedVector< T >.
298 // This allows conversion to UT_FixedVector without fixing T and D.
299 // Instead, the element type of TS determines the type T and D.
300 template< typename TS >
301 constexpr UT_FixedVector< SYS_FixedArrayElement_t< TS >, SYS_FixedArraySize_v< TS > >
302 UTmakeFixedVector( const TS& as ) noexcept
303 {
305  constexpr exint D = SYS_FixedArraySize_v< TS >;
306 
307  return UT_FixedVectorFixed< T, D >{}( as );
308 }
309 
310 // UT_FromFixed<V> creates a V from a flat, fixed array-like representation
311 
312 // Primary
313 template <typename V >
315 
316 // Partial specialization for UT_FixedVector<T, D>
317 template<typename T, exint D>
319 
320 // Relocation traits
321 
322 template<typename T, exint D>
324 
325 template<typename T, exint D>
327 
328 #endif
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
#define SYS_STATIC_ASSERT(expr)
constexpr UT_FixedVector< T, D > operator-() const noexcept
friend constexpr T distance2(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
friend constexpr T dot(const UT_FixedVector &a, const UT_FixedVector &b) 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:591
constexpr const T * data() const 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
friend constexpr bool operator<=(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
friend constexpr T length2(const UT_FixedVector &a) 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
GLdouble GLdouble t
Definition: glew.h:1403
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) 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:601
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:152
static const bool isVectorType
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
constexpr T & operator[](exint i) noexcept
static constexpr int tuple_size
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
#define const
Definition: zconf.h:214
friend constexpr bool operator>=(const UT_FixedVector &a, const UT_FixedVector &b) noexcept
GLboolean r
Definition: glcorearb.h:1222
constexpr UT_FixedVector< T, D > operator()() 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