HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Vector2.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_Vector2.h (C++)
7  *
8  *
9  * COMMENTS:
10  * This class handles fpreal vectors of dimension 2.
11  *
12  * WARNING:
13  * This class should NOT contain any virtual methods, nor should it
14  * define more member data. The size of UT_VectorF2 must always be
15  * 8 bytes (2 floats).
16  */
17 
18 #pragma once
19 
20 #ifndef __UT_Vector2_h__
21 #define __UT_Vector2_h__
22 
23 #include "UT_API.h"
24 #include "UT_Assert.h"
25 #include "UT_FixedVectorTraits.h"
26 #include "UT_FixedVector.h"
27 #include "UT_Storage.h"
28 #include "UT_FixedArrayMath.h"
29 #include "UT_VectorTypes.h"
30 #include <SYS/SYS_Deprecated.h>
31 #include <SYS/SYS_Math.h>
32 #include <SYS/SYS_Inline.h>
33 #include <VM/VM_SIMD.h>
34 #include <iosfwd>
35 #include <limits>
36 
37 class UT_IStream;
38 class UT_JSONWriter;
39 class UT_JSONValue;
40 class UT_JSONParser;
41 
42 // Free floating functions:
43 
44 // Operators that involve a UT_Vector2 object:
45 template <typename T>
46 constexpr UT_Vector2T<T> operator+(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept;
47 template <typename T>
48 constexpr UT_Vector2T<T> operator-(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept;
49 template <typename T>
50 constexpr bool operator<(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept;
51 template <typename T>
52 constexpr bool operator<=(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept;
53 template <typename T>
54 constexpr bool operator>(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept;
55 template <typename T>
56 constexpr bool operator>=(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept;
57 template <typename T, typename S>
58 inline UT_Vector2T<T> operator+(const UT_Vector2T<T> &v, S scalar);
59 template <typename T, typename S>
60 inline UT_Vector2T<T> operator-(const UT_Vector2T<T> &v, S scalar);
61 template <typename T, typename S>
62 inline UT_Vector2T<T> operator*(const UT_Vector2T<T> &v, S scalar);
63 template <typename T, typename S>
64 inline UT_Vector2T<T> operator/(const UT_Vector2T<T> &v, S scalar);
65 template <typename T, typename S>
66 inline UT_Vector2T<T> operator+(S scalar, const UT_Vector2T<T> &v);
67 template <typename T, typename S>
68 inline UT_Vector2T<T> operator-(S scalar, const UT_Vector2T<T> &v);
69 template <typename T, typename S>
70 constexpr UT_Vector2T<T> operator*(S scalar, const UT_Vector2T<T> &v) noexcept;
71 template <typename T, typename S>
72 inline UT_Vector2T<T> operator/(S scalar, const UT_Vector2T<T> &v);
73 template <typename T, typename S>
75  const UT_Matrix2T<S> &mat);
76 
77 /// The dot product
78 template <typename T>
79 constexpr T dot ( const UT_Vector2T< T >& a, const UT_Vector2T< T >& b ) noexcept;
80 /// Cross product, which for 2d vectors results in a fpreal.
81 template <typename T>
82 inline T cross (const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
83 
84 /// Componentwise min and maximum
85 template <typename T>
86 inline UT_Vector2T<T> SYSmin (const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
87 template <typename T>
88 inline UT_Vector2T<T> SYSmax (const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
89 /// Componentwise integer test
90 template <typename T>
91 inline bool SYSisInteger(const UT_Vector2T<T> &v1)
92 { return SYSisInteger(v1.x()) && SYSisInteger(v1.y()); }
93 /// Componentwise linear interpolation
94 template <typename T,typename S>
95 inline UT_Vector2T<T> SYSlerp(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2, S t);
96 
97 /// Componentwise inverse linear interpolation
98 template <typename T>
100 
101 /// Bilinear interpolation
102 template <typename T,typename S>
103 inline UT_Vector2T<T> SYSbilerp(const UT_Vector2T<T> &u0v0, const UT_Vector2T<T> &u1v0,
104  const UT_Vector2T<T> &u0v1, const UT_Vector2T<T> &u1v1, S u, S v)
105 { return SYSlerp(SYSlerp(u0v0, u0v1, v), SYSlerp(u1v0, u1v1, v), u); }
106 
107 /// Barycentric interpolation
108 template <typename T, typename S>
110  const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2, S u, S v)
111 { return v0 * (1 - u - v) + v1 * u + v2 *v; }
112 
113 /// The orthogonal projection of a vector u onto a vector v
114 template <typename T>
115 inline UT_Vector2T<T> project (const UT_Vector2T<T> &u, const UT_Vector2T<T> &v);
116 
117 /// Multiplication of a row or column vector by a matrix (ie. right vs. left
118 /// multiplication respectively). The operator*() declared above is an alias
119 /// for rowVecMult().
120 // @{
121 //
122 // Notes on optimisation of matrix/vector multiplies:
123 // - multiply(dest, mat) functions have been deprecated in favour of
124 // rowVecMult/colVecMult routines, which produce temporaries. For these to
125 // offer comparable performance, the compiler has to optimize away the
126 // temporary, but most modern compilers can do this. Performance tests with
127 // gcc3.3 indicate that this is a realistic expectation for modern
128 // compilers.
129 // - since matrix/vector multiplies cannot be done without temporary data,
130 // the "primary" functions are the global matrix/vector
131 // rowVecMult/colVecMult routines, rather than the member functions.
132 
133 template <typename T, typename S>
135  const UT_Matrix2T<S> &m);
136 template <typename T, typename S>
138  const UT_Vector2T<T> &v);
139 // @}
140 
141 template <typename T>
142 inline T distance2d(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
143 
144 /// Given a 2D position, input, and a 2D quad, (p0, p0+du, p0+du+dv+duv, p0+dv),
145 /// finds the 0, 1, or 2 locations in the parameter space of that quad that correspond
146 /// with the input position. Only parameter locations approximately between 0 and 1
147 /// are accepted. The return value is the number of accepted parameter locations.
148 template <typename T>
150  const UT_Vector2T<T> &p0,
151  const UT_Vector2T<T> &du, const UT_Vector2T<T> &dv,
152  const UT_Vector2T<T> &duv,
153  UT_Vector2T<T> outputs[2]);
154 
155 /// 2D Vector class.
156 template <typename T>
158 {
159 public:
160  typedef T value_type;
161  static constexpr int tuple_size = 2;
162 
163  /// Default constructor.
164  /// No data is initialized! Use it for extra speed.
165  constexpr SYS_FORCE_INLINE UT_Vector2T() = default;
166 
167  constexpr SYS_FORCE_INLINE UT_Vector2T(const UT_Vector2T<T> &that) = default;
168  constexpr SYS_FORCE_INLINE UT_Vector2T(UT_Vector2T<T> &&that) = default;
169 
170  constexpr SYS_FORCE_INLINE UT_Vector2T(const T vx, const T vy) noexcept :
171  vec{ vx, vy }
172  {}
173 
174  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(T v) noexcept :
175  UT_Vector2T( v, v )
176  {}
177 
178  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const fpreal16 v[]) noexcept :
179  UT_Vector2T( v[0], v[1] )
180  {}
181  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const fpreal32 v[]) noexcept :
182  UT_Vector2T( v[0], v[1] )
183  {}
184  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const fpreal64 v[]) noexcept :
185  UT_Vector2T( v[0], v[1] )
186  {}
187  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const int32 v[]) noexcept :
188  UT_Vector2T( v[0], v[1] )
189  {}
190  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const int64 v[]) noexcept :
191  UT_Vector2T( v[0], v[1] )
192  {}
193 
194  constexpr UT_Vector2T(const UT_Vector3T<T> &v) noexcept :
195  UT_Vector2T( v.x(), v.y() )
196  {}
197 
198  constexpr UT_Vector2T(const UT_Vector4T<T> &v) noexcept :
199  UT_Vector2T( v.x(), v.y() )
200  {}
201 
202  /// Our own type of any given value_type.
203  template <typename S>
204  constexpr SYS_FORCE_INLINE UT_Vector2T(const UT_Vector2T<S>& v) noexcept :
205  UT_Vector2T( v[0], v[1] )
206  {}
207 
208  /// UT_FixedVector of the same size
209  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const UT_FixedVector<T,tuple_size>& v) noexcept :
210  UT_Vector2T( v[0], v[1] )
211  {}
212 
213  constexpr SYS_FORCE_INLINE UT_Vector2T<T> &operator=(const UT_Vector2T<T> &that) = default;
214  constexpr SYS_FORCE_INLINE UT_Vector2T<T> &operator=(UT_Vector2T<T> &&that) = default;
215 
216  template <typename S>
218  { vec[0] = v.x(); vec[1] = v.y(); return *this; }
219 
220  constexpr SYS_FORCE_INLINE const T* data() const noexcept
221  {
222  return vec;
223  }
224 
225  constexpr SYS_FORCE_INLINE T* data() noexcept
226  {
227  return vec;
228  }
229 
230  constexpr SYS_FORCE_INLINE const T& operator[]( exint i ) const noexcept
231  {
232  UT_ASSERT_P( ( 0 <= i ) && ( i < tuple_size ) );
233 
234  return vec[ i ];
235  }
236 
237  constexpr SYS_FORCE_INLINE T& operator[]( exint i ) noexcept
238  {
239  UT_ASSERT_P( ( 0 <= i ) && ( i < tuple_size ) );
240 
241  return vec[ i ];
242  }
243 
244  constexpr SYS_FORCE_INLINE UT_Vector2T& operator+=( const UT_Vector2T& a ) noexcept
245  {
246  UT::FA::Add< T, tuple_size >{}( vec, a.vec );
247  return *this;
248  }
249 
250  constexpr SYS_FORCE_INLINE UT_Vector2T& operator-=( const UT_Vector2T& a ) noexcept
251  {
252  UT::FA::Subtract< T, tuple_size >{}( vec, a.vec );
253  return *this;
254  }
255 
256  constexpr SYS_FORCE_INLINE UT_Vector2T& operator+=( const T& a ) noexcept
257  {
259  return *this;
260  }
261 
262  constexpr SYS_FORCE_INLINE UT_Vector2T& operator-=( const T& a ) noexcept
263  {
265  return *this;
266  }
267 
268  constexpr SYS_FORCE_INLINE UT_Vector2T& operator*=( const T& a ) noexcept
269  {
271  return *this;
272  }
273 
274  constexpr SYS_FORCE_INLINE UT_Vector2T& operator/=( const T& a ) noexcept
275  {
276  using MF = UT_StorageMathFloat_t< T >;
277  UT::FA::Scale< T, tuple_size, MF >{}( vec, MF{1} / a );
278  return *this;
279  }
280 
281  constexpr SYS_FORCE_INLINE UT_Vector2T& operator*=( const UT_Vector2T& a ) noexcept
282  {
284  return *this;
285  }
286 
287  constexpr SYS_FORCE_INLINE void negate() noexcept
288  {
290  }
291 
292  constexpr SYS_FORCE_INLINE T length2() const noexcept
293  {
294  return UT::FA::Length2< T, tuple_size >{}( vec );
295  }
296 
297  constexpr SYS_FORCE_INLINE T length() const noexcept
298  {
299  return SYSsqrt( length2() );
300  }
301 
302  constexpr SYS_FORCE_INLINE T distance2( const UT_Vector2T& b ) const noexcept
303  {
304  return UT::FA::Distance2< T, tuple_size >{}( vec, b.vec );
305  }
306 
307  constexpr SYS_FORCE_INLINE T distance( const UT_Vector2T& b ) const noexcept
308  {
309  return SYSsqrt( distance2( b ) );
310  }
311 
313  {
314  using MF = UT_StorageMathFloat_t< T >;
317  }
318 
319  constexpr SYS_FORCE_INLINE bool isNan() const noexcept
320  {
321  return UT::FA::AnyOf< T, tuple_size >{}( vec, [ & ]( const T& a ) { return SYSisNan( a ); } );
322  }
323 
324  constexpr SYS_FORCE_INLINE bool isFinite() const noexcept
325  {
326  return UT::FA::AllOf< T, tuple_size >{}( vec, [ & ]( const T& a ) { return SYSisFinite( a ); } );
327  }
328 
329  constexpr SYS_FORCE_INLINE bool isZero() const noexcept
330  {
332  }
333 
334  constexpr SYS_FORCE_INLINE bool equalZero( const T tolerance = SYS_FTOLERANCE ) const noexcept
335  {
336  return UT::FA::MaxNormIsLEQ< T, tuple_size >{}( vec, tolerance );
337  }
338 
339  constexpr SYS_FORCE_INLINE bool isEqual( const UT_Vector2T& b, const T tolerance = SYS_FTOLERANCE ) const noexcept
340  {
341  return UT::FA::MaxMetricIsLEQ< T, tuple_size >{}( vec, b.vec, tolerance );
342  }
343 
344  constexpr SYS_FORCE_INLINE T maxComponent() const noexcept
345  {
346  return UT::FA::Max< T, tuple_size >{}( vec );
347  }
348 
349  // TODO: remove these. They should require an explicit UT_Vector2()
350  // construction, since they're unsafe.
351 
352  /// Assignment operator that truncates a V3 to a V2.
353  UT_Vector2T<T> &operator=(const UT_Vector3T<T> &v);
354  /// Assignment operator that truncates a V4 to a V2.
355  UT_Vector2T<T> &operator=(const UT_Vector4T<T> &v);
356 
357  constexpr SYS_FORCE_INLINE UT_Vector2T& operator=( const T a ) noexcept;
358 
360  {
361  return UT_Vector2T<T>(-vec[0], -vec[1]);
362  }
364  {
365  vec[0] *= v.vec[0];
366  vec[1] *= v.vec[1];
367  }
368 
369  /// Given an oriented line from e1 passing through e2, determine on which
370  /// side of the line the point p lies. Alternatively, determine in which
371  /// half plane, positive or negative, the point lies. If the segment
372  /// degenerates to a point, then the point is always on it.
373  // (Moret and Shapiro 1991)
374  T whichSide(const UT_Vector2T<T> &e1, const UT_Vector2T<T> &e2) const
375  {
376  return (vec[0] - e1.vec[0]) * (e2.vec[1] - e1.vec[1]) -
377  (vec[1] - e1.vec[1]) * (e2.vec[0] - e1.vec[0]);
378  }
379 
380  template <typename S>
382  { return operator=(*this * mat); }
383 
384  /// These allow you to find out what indices to use for different axes
385  // @{
386  int findMinAbsAxis() const
387  {
388  if (SYSabs(x()) < SYSabs(y()))
389  return 0;
390  else
391  return 1;
392  }
393  int findMaxAbsAxis() const
394  {
395  if (SYSabs(x()) >= SYSabs(y()))
396  return 0;
397  else
398  return 1;
399  }
400  // @}
401 
402  constexpr SYS_FORCE_INLINE T dot( const UT_Vector2T& b ) const noexcept
403  {
404  return UT::FA::Dot< T, tuple_size >{}( vec, b.vec );
405  }
406 
407  /// Calculates the orthogonal projection of a vector u on the *this vector
408  UT_Vector2T<T> project(const UT_Vector2T<T> &u) const;
409 
410 
411  /// Vector p (representing a point in 2-space) and vector v define
412  /// a line. This member returns the projection of "this" onto the
413  /// line (the point on the line that is closest to this point).
414  UT_Vector2T<T> projection(const UT_Vector2T<T> &p, const UT_Vector2T<T> &v) const;
415 
416  /// Compute (homogeneous) barycentric co-ordinates of this point
417  /// relative to the triangle defined by t0, t1 and t2. (The point is
418  /// projected into the triangle's plane.)
419  UT_Vector3T<T> getBary(const UT_Vector2T<T> &t0, const UT_Vector2T<T> &t1,
420  const UT_Vector2T<T> &t2, bool *degen = NULL) const;
421 
422 
423  /// Return the components of the vector. The () operator does NOT check
424  /// for the boundary condition.
425  // @{
426  T &x() { return vec[0]; }
427  constexpr T x() const noexcept { return vec[0]; }
428  T &y() { return vec[1]; }
429  constexpr T y() const noexcept { return vec[1]; }
430  T &operator()(unsigned i)
431  {
432  UT_ASSERT_P(i < tuple_size);
433  return vec[i];
434  }
435  T operator()(unsigned i) const
436  {
437  UT_ASSERT_P(i < tuple_size);
438  return vec[i];
439  }
440  // @}
441 
442  /// Compute a hash
443  unsigned hash() const { return SYSvector_hash(data(), tuple_size); }
444 
445  // TODO: eliminate these methods. They're redundant, given good inline
446  // constructors.
447  /// Set the values of the vector components
448  void assign(T xx = 0.0f, T yy = 0.0f)
449  {
450  vec[0] = xx; vec[1] = yy;
451  }
452  /// Set the values of the vector components
453  void assign(const T *v) {vec[0]=v[0]; vec[1]=v[1];}
454 
455  /// Express the point in homogeneous coordinates or vice-versa
456  // @{
457  void homogenize () { vec[0] *= vec[1]; }
458  void dehomogenize() { if (vec[1] != 0) vec[0] /= vec[1]; }
459  // @}
460 
461  /// Protected I/O methods
462  // @{
463  void save(std::ostream &os, int binary = 0) const;
464  bool load(UT_IStream &is);
465  // @}
466 
467  /// @{
468  /// Methods to serialize to a JSON stream. The vector is stored as an
469  /// array of 2 reals.
470  bool save(UT_JSONWriter &w) const;
471  bool save(UT_JSONValue &v) const;
472  bool load(UT_JSONParser &p);
473  /// @}
474 
475  /// Returns the vector size
476  static int entries() { return tuple_size; }
477 
478  T vec[tuple_size];
479 
480 private:
481 
482  friend constexpr bool isZero( const UT_Vector2T& a ) noexcept
483  {
485  }
486 
487  friend constexpr T length2( const UT_Vector2T& a ) noexcept
488  {
489  return UT::FA::Length2< T, tuple_size >{}( a.vec );
490  }
491 
492  friend constexpr T distance2( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
493  {
494  return UT::FA::Distance2< T, tuple_size >{}( a.vec, b.vec );
495  }
496 
497  friend constexpr bool operator==( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
498  {
499  return UT::FA::AreEqual< T, tuple_size >{}( a.vec, b.vec );
500  }
501 
502  friend constexpr bool operator!=( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
503  {
504  return ! UT::FA::AreEqual< T, tuple_size >{}( a.vec, b.vec );
505  }
506 
507  /// Lexicographic order comparison operators
508  /// @{
509  friend constexpr bool operator<( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
510  {
511  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) < 0;
512  }
513 
514  friend constexpr bool operator<=( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
515  {
516  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) <= 0;
517  }
518 
519  friend constexpr bool operator>( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
520  {
521  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) > 0;
522  }
523 
524  friend constexpr bool operator>=( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
525  {
526  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) >= 0;
527  }
528  /// @}
529 
530  /// I/O friends
531  // @{
532  friend std::ostream &operator<<(std::ostream &os, const UT_Vector2T<T> &v)
533  {
534  v.save(os);
535  return os;
536  }
537  // @}
538 };
539 
540 template <typename T>
542 {
543  for ( int i = 0; i != tuple_size; ++i )
544  {
545  vec[i] = a;
546  }
547 
548  return *this;
549 }
550 
551 #include "UT_Matrix2.h"
552 
553 // Free floating functions:
554 template <typename T>
555 constexpr UT_Vector2T<T> operator+(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
556 {
557  return UT_Vector2T<T>(v1.x()+v2.x(), v1.y()+v2.y());
558 }
559 template <typename T>
560 constexpr UT_Vector2T<T> operator-(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
561 {
562  return UT_Vector2T<T>(v1.x()-v2.x(), v1.y()-v2.y());
563 }
564 template <typename T>
566 {
567  return UT_Vector2T<T>(v1.x()*v2.x(), v1.y()*v2.y());
568 }
569 template <typename T>
571 {
572  return UT_Vector2T<T>(v1.x()/v2.x(), v1.y()/v2.y());
573 }
574 template <typename T>
575 constexpr bool operator<(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
576 {
577  return ((v1.x() < v2.x()) || (v1.x() == v2.x() && v1.y() < v2.y()));
578 }
579 template <typename T>
580 constexpr bool operator<=(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
581 {
582  return (v1 < v2) || (v1 == v2);
583 }
584 template <typename T>
585 constexpr bool operator>(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
586 {
587  return v2 < v1;
588 }
589 template <typename T>
590 constexpr bool operator>=(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
591 {
592  return v2 <= v1;
593 }
594 template <typename T, typename S>
595 inline UT_Vector2T<T> operator+(const UT_Vector2T<T> &v, S scalar)
596 {
597  return UT_Vector2T<T>(v.x()+scalar, v.y()+scalar);
598 }
599 template <typename T, typename S>
600 inline UT_Vector2T<T> operator+(S scalar, const UT_Vector2T<T> &v)
601 {
602  return UT_Vector2T<T>(v.x()+scalar, v.y()+scalar);
603 }
604 template <typename T, typename S>
605 inline UT_Vector2T<T> operator-(const UT_Vector2T<T> &v, S scalar)
606 {
607  return UT_Vector2T<T>(v.x()-scalar, v.y()-scalar);
608 }
609 template <typename T, typename S>
610 inline UT_Vector2T<T> operator-(S scalar, const UT_Vector2T<T> &v)
611 {
612  return UT_Vector2T<T>(scalar-v.x(), scalar-v.y());
613 }
614 template <typename T, typename S>
615 inline UT_Vector2T<T> operator*(const UT_Vector2T<T> &v, S scalar)
616 {
617  return UT_Vector2T<T>(v.x()*scalar, v.y()*scalar);
618 }
619 template <typename T, typename S>
620 constexpr UT_Vector2T<T> operator*(S scalar, const UT_Vector2T<T> &v) noexcept
621 {
622  return UT_Vector2T<T>(v.x()*scalar, v.y()*scalar);
623 }
624 template <typename T, typename S>
625 inline UT_Vector2T<T> operator/(const UT_Vector2T<T> &v, S scalar)
626 {
627  return UT_Vector2T<T>(v.x()/scalar, v.y()/scalar);
628 }
629 template <typename T, typename S>
630 inline UT_Vector2T<T> operator/(S scalar, const UT_Vector2T<T> &v)
631 {
632  return UT_Vector2T<T>(scalar/v.x(), scalar/v.y());
633 }
634 
635 template <typename T>
636 constexpr T dot( const UT_Vector2T< T >& a, const UT_Vector2T< T >& b ) noexcept
637 {
638  return UT::FA::Dot< T, 2 >{}( a.vec, b.vec );
639 }
640 
641 template <typename T>
642 inline T cross(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
643 {
644  return v1.x() * v2.y() - v1.y() * v2.x();
645 }
646 template <typename T>
647 inline
649 {
650  return UT_Vector2T<T>(SYSabs(v.x()), SYSabs(v.y()));
651 }
652 
653 
654 template <typename T>
655 inline
657 {
658  return UT_Vector2T<T>(
659  SYSmin(v1.x(), v2.x()),
660  SYSmin(v1.y(), v2.y())
661  );
662 }
663 
664 template <typename T>
665 inline
667 {
668  return UT_Vector2T<T>(
669  SYSmax(v1.x(), v2.x()),
670  SYSmax(v1.y(), v2.y())
671  );
672 }
673 
674 template <typename T,typename S>
675 inline
677 {
678  return UT_Vector2T<T>(
679  SYSlerp(v1.x(), v2.x(), t),
680  SYSlerp(v1.y(), v2.y(), t));
681 }
682 
683 template <typename T>
684 inline
686  const UT_Vector2T<T> &v1,
687  const UT_Vector2T<T> &v2)
688 {
689  return UT_Vector2T<T>(
690  SYSinvlerp(a.x(), v1.x(), v2.x()),
691  SYSinvlerp(a.y(), v1.y(), v2.y()));
692 }
693 
694 template <typename T>
696 {
697  return dot(u, v) / v.length2() * v;
698 }
699 template <typename T, typename S>
701 {
702  return UT_Vector2T<T>(v.x()*m(0,0) + v.y()*m(1,0),
703  v.x()*m(0,1) + v.y()*m(1,1));
704 }
705 template <>
707 {
708  const v4uf l(v.x(), v.x(), v.y(), v.y());
709  const v4uf r(m.data());
710  const v4uf p = l * r;
711 
712  return UT_Vector2T<float>(p[0] + p[2], p[1] + p[3]);
713 }
714 template <typename T, typename S>
716 {
717  return UT_Vector2T<T>(m(0,0)*v.x() + m(0,1)*v.y(),
718  m(1,0)*v.x() + m(1,1)*v.y());
719 }
720 template <>
722 {
723  const v4uf l(m.data());
724  const v4uf r(v.x(), v.y(), v.x(), v.y());
725  const v4uf p = l * r;
726  return UT_Vector2T<float>(p[0] + p[1], p[2] + p[3]);
727 }
728 template <typename T, typename S>
730 {
731  return rowVecMult(v, m);
732 }
733 template <typename T>
734 inline T distance2d(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
735 {
736  return (v1 - v2).length();
737 }
738 
739 template <typename T>
740 inline size_t hash_value(const UT_Vector2T<T> &val)
741 {
742  return val.hash();
743 }
744 
745 // Overload for custom formatting of UT_Vector2T<T> with UTformat.
746 template<typename T>
747 UT_API size_t format(char *buffer, size_t buffer_size, const UT_Vector2T<T> &v);
748 
749 template< typename T, exint D >
750 class UT_FixedVector;
751 
752 template<typename T>
754 {
756  typedef T DataType;
757  static const exint TupleSize = 2;
758  static const bool isVectorType = true;
759 };
760 
761 // UT_Vector2T in the role of a fixed array-like type.
762 
763 template< typename T >
765 
766 template< typename T >
768 
769 template< typename T >
770 struct SYS_FixedArraySizeNoCVRef< UT_Vector2T< T > > : std::integral_constant< std::size_t, 2 > {};
771 
772 // UT_Vector2TFromFixed<T> is a function object that
773 // creates a UT_Vector2T<T> from a fixed array-like type TS,
774 // examples of which include T[2], UT_FixedVector<T,2> and UT_FixedArray<T,2> (AKA std::array<T,2>)
775 template <typename T>
777 {
778  template< typename TS >
779  constexpr SYS_FORCE_INLINE UT_Vector2T<T> operator()(const TS& as) const noexcept
780  {
781  SYS_STATIC_ASSERT( SYS_IsFixedArrayOf_v< TS, T, 2 > );
782 
783  return UT_Vector2T<T>{as[0], as[1]};
784  }
785 };
786 
787 // Convert a fixed array-like type TS into a UT_Vector2T< T >.
788 // This allows conversion to UT_Vector2T without fixing T.
789 // Instead, the element type of TS determines the type T.
790 template< typename TS >
792 UTmakeVector2T( const TS& as ) noexcept
793 {
795 
796  return UT_Vector2TFromFixed< T >{}( as );
797 }
798 
799 // UT_FromFixed<V> creates a V from a flat, fixed array-like representation
800 
801 // Primary
802 template <typename V >
803 struct UT_FromFixed;
804 
805 // Partial specialization for UT_Vector2T
806 template <typename T>
808 
809 // Relocation traits for UT_Vector2T are defined in UT_VectorTypes.h
810 
811 #endif
constexpr SYS_FORCE_INLINE UT_Vector2T(const UT_Vector2T< S > &v) noexcept
Our own type of any given value_type.
Definition: UT_Vector2.h:204
T vec[tuple_size]
Definition: UT_Vector2.h:478
constexpr SYS_FORCE_INLINE UT_Vector2T & operator*=(const T &a) noexcept
Definition: UT_Vector2.h:268
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:575
UT_Vector2T< T > SYSabs(const UT_Vector2T< T > &v)
Definition: UT_Vector2.h:648
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
constexpr SYS_FORCE_INLINE UT_Vector2T(const fpreal64 v[]) noexcept
Definition: UT_Vector2.h:184
constexpr SYS_FORCE_INLINE UT_Vector2T & operator-=(const UT_Vector2T &a) noexcept
Definition: UT_Vector2.h:250
typename UT_StorageNum< T >::MathFloat UT_StorageMathFloat_t
Definition: UT_Storage.h:176
class UT_API UT_Vector2T
#define SYS_STATIC_ASSERT(expr)
constexpr T x() const noexcept
Definition: UT_Vector2.h:427
int int32
Definition: SYS_Types.h:39
friend constexpr bool operator!=(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:502
friend constexpr bool operator<=(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:514
UT_FixedVector< T, 2 > FixedVectorType
Definition: UT_Vector2.h:755
T & operator()(unsigned i)
Definition: UT_Vector2.h:430
UT_Vector2T< T > rowVecMult(const UT_Vector2T< T > &v, const UT_Matrix2T< S > &m)
Definition: UT_Vector2.h:700
constexpr SYS_FORCE_INLINE T & operator[](exint i) noexcept
Definition: UT_Vector2.h:237
constexpr T dot(const UT_Vector2T< T > &a, const UT_Vector2T< T > &b) noexcept
The dot product.
Definition: UT_Vector2.h:636
static int entries()
Returns the vector size.
Definition: UT_Vector2.h:476
int findMinAbsAxis() const
These allow you to find out what indices to use for different axes.
Definition: UT_Vector2.h:386
T whichSide(const UT_Vector2T< T > &e1, const UT_Vector2T< T > &e2) const
Definition: UT_Vector2.h:374
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
bool SYSisFinite(fpreal64 f)
Definition: SYS_Math.h:194
UT_Vector2T< T > operator/(const UT_Vector2T< T > &v, S scalar)
Definition: UT_Vector2.h:625
constexpr SYS_FORCE_INLINE UT_Vector2T & operator*=(const UT_Vector2T &a) noexcept
Definition: UT_Vector2.h:281
const GLuint GLenum const void * binary
Definition: glcorearb.h:1924
bool SYSisInteger(const UT_Vector2T< T > &v1)
Componentwise integer test.
Definition: UT_Vector2.h:91
GLenum GLenum GLenum input
Definition: glew.h:14162
constexpr SYS_FORCE_INLINE T dot(const UT_Vector2T &b) const noexcept
Definition: UT_Vector2.h:402
typename SYS_FixedArrayElement< T >::type SYS_FixedArrayElement_t
fpreal64 distance2(const UT_VectorD &v1, const UT_VectorD &v2)
Distance squared (L2) aka quadrance.
Definition: UT_Vector.h:399
constexpr SYS_FORCE_INLINE UT_Vector2T & operator/=(const T &a) noexcept
Definition: UT_Vector2.h:274
int64 exint
Definition: SYS_Types.h:125
constexpr bool SYSisNan(const F f)
Definition: SYS_Math.h:178
void assign(const T *v)
Set the values of the vector components.
Definition: UT_Vector2.h:453
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:88
#define UT_API
Definition: UT_API.h:14
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition: glew.h:12900
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:35
static const exint TupleSize
friend constexpr T distance2(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:492
friend constexpr bool isZero(const UT_Vector2T &a) noexcept
Definition: UT_Vector2.h:482
3D Vector class.
4D Vector class.
Definition: UT_Vector4.h:172
2D Vector class.
Definition: UT_Vector2.h:157
float fpreal32
Definition: SYS_Types.h:200
GLdouble GLdouble t
Definition: glew.h:1403
constexpr UT_Vector2T(const UT_Vector4T< T > &v) noexcept
Definition: UT_Vector2.h:198
T cross(const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2)
Cross product, which for 2d vectors results in a fpreal.
Definition: UT_Vector2.h:642
GLdouble l
Definition: glew.h:9164
UT_Vector2T< T > & operator*=(const UT_Matrix2T< S > &mat)
Definition: UT_Vector2.h:381
GLint GLenum GLint x
Definition: glcorearb.h:409
friend constexpr T length2(const UT_Vector2T &a) noexcept
Definition: UT_Vector2.h:487
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
double fpreal64
Definition: SYS_Types.h:201
constexpr SYS_FORCE_INLINE UT_Vector2T< T > & operator=(const UT_Vector2T< S > &v)
Definition: UT_Vector2.h:217
UT_Vector2T< T > SYSlerp(const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2, S t)
Componentwise linear interpolation.
Definition: UT_Vector2.h:676
constexpr SYS_FORCE_INLINE T length() const noexcept
Definition: UT_Vector2.h:297
constexpr SYS_FORCE_INLINE UT_Vector2T & operator-=(const T &a) noexcept
Definition: UT_Vector2.h:262
Definition: core.h:760
UT_Vector2T< T > SYSbarycentric(const UT_Vector2T< T > &v0, const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2, S u, S v)
Barycentric interpolation.
Definition: UT_Vector2.h:109
int findMaxAbsAxis() const
These allow you to find out what indices to use for different axes.
Definition: UT_Vector2.h:393
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
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
typename UT_StorageAtLeast32Bit< T0, T1 >::type UT_StorageAtLeast32Bit_t
Definition: UT_Storage.h:265
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:152
int UTinverseBilerp(const UT_Vector2T< T > &input, const UT_Vector2T< T > &p0, const UT_Vector2T< T > &du, const UT_Vector2T< T > &dv, const UT_Vector2T< T > &duv, UT_Vector2T< T > outputs[2])
unsigned hash() const
Compute a hash.
Definition: UT_Vector2.h:443
const GLdouble * v
Definition: glcorearb.h:837
UT_Vector2T< T > project(const UT_Vector2T< T > &u, const UT_Vector2T< T > &v)
The orthogonal projection of a vector u onto a vector v.
Definition: UT_Vector2.h:695
static const bool isVectorType
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
Definition: VM_SIMD.h:188
constexpr SYS_FORCE_INLINE bool isNan() const noexcept
Definition: UT_Vector2.h:319
constexpr SYS_FORCE_INLINE const T * data() const noexcept
Definition: UT_Vector2.h:220
long long int64
Definition: SYS_Types.h:116
GLfloat GLfloat p
Definition: glew.h:16656
friend constexpr bool operator<(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:509
constexpr SYS_FORCE_INLINE T distance(const UT_Vector2T &b) const noexcept
Definition: UT_Vector2.h:307
UT_Vector2T< T > SYSinvlerp(const UT_Vector2T< T > &a, const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2)
Componentwise inverse linear interpolation.
Definition: UT_Vector2.h:685
friend constexpr bool operator==(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:497
UT_Vector2T< T > SYSbilerp(const UT_Vector2T< T > &u0v0, const UT_Vector2T< T > &u1v0, const UT_Vector2T< T > &u0v1, const UT_Vector2T< T > &u1v1, S u, S v)
Bilinear interpolation.
Definition: UT_Vector2.h:103
constexpr bool operator>=(const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2) noexcept
Definition: UT_Vector2.h:590
GLfloat v0
Definition: glcorearb.h:816
UT_API size_t format(char *buffer, size_t buffer_size, const UT_Vector2T< T > &v)
constexpr SYS_FORCE_INLINE T * data() noexcept
Definition: UT_Vector2.h:225
constexpr SYS_FORCE_INLINE UT_Vector2T(const fpreal32 v[]) noexcept
Definition: UT_Vector2.h:181
SYS_FORCE_INLINE UT_Vector2T< T > operator-() const
Definition: UT_Vector2.h:359
GLboolean * data
Definition: glcorearb.h:131
void multiplyComponents(const UT_Vector2T< T > &v)
Definition: UT_Vector2.h:363
constexpr SYS_FORCE_INLINE T distance2(const UT_Vector2T &b) const noexcept
Definition: UT_Vector2.h:302
constexpr SYS_FORCE_INLINE UT_Vector2T & operator+=(const UT_Vector2T &a) noexcept
Definition: UT_Vector2.h:244
GLuint GLfloat * val
Definition: glcorearb.h:1608
constexpr SYS_FORCE_INLINE T length2() const noexcept
Definition: UT_Vector2.h:292
UT_Vector2T< T > SYSmax(const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2)
Definition: UT_Vector2.h:666
size_t hash_value(const UT_Vector2T< T > &val)
Definition: UT_Vector2.h:740
constexpr SYS_FORCE_INLINE UT_Vector2T & operator+=(const T &a) noexcept
Definition: UT_Vector2.h:256
constexpr SYS_FORCE_INLINE bool isZero() const noexcept
Definition: UT_Vector2.h:329
constexpr SYS_FORCE_INLINE bool equalZero(const T tolerance=SYS_FTOLERANCE) const noexcept
Definition: UT_Vector2.h:334
constexpr UT_Vector2T(const UT_Vector3T< T > &v) noexcept
Definition: UT_Vector2.h:194
constexpr SYS_FORCE_INLINE T maxComponent() const noexcept
Definition: UT_Vector2.h:344
T operator()(unsigned i) const
Definition: UT_Vector2.h:435
constexpr SYS_FORCE_INLINE void negate() noexcept
Definition: UT_Vector2.h:287
constexpr SYS_FORCE_INLINE UT_Vector2T(const T vx, const T vy) noexcept
Definition: UT_Vector2.h:170
constexpr T y() const noexcept
Definition: UT_Vector2.h:429
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
Definition: glew.h:12900
const GLdouble * m
Definition: glew.h:9166
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:77
GLfloat f
Definition: glcorearb.h:1926
friend constexpr bool operator>(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:519
friend constexpr bool operator>=(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:524
constexpr SYS_FORCE_INLINE UT_Vector2T(const int64 v[]) noexcept
Definition: UT_Vector2.h:190
#define SYS_FTOLERANCE
Definition: SYS_Types.h:208
void assign(T xx=0.0f, T yy=0.0f)
Set the values of the vector components.
Definition: UT_Vector2.h:448
UT_Vector2T< T > SYSmin(const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2)
Componentwise min and maximum.
Definition: UT_Vector2.h:656
constexpr SYS_FORCE_INLINE UT_Vector2T(T v) noexcept
Definition: UT_Vector2.h:174
constexpr SYS_FORCE_INLINE UT_Vector2T(const int32 v[]) noexcept
Definition: UT_Vector2.h:187
#define const
Definition: zconf.h:214
GLfloat GLfloat v1
Definition: glcorearb.h:817
const T * data() const
Return the raw matrix data.
Definition: UT_Matrix2.h:423
constexpr SYS_FORCE_INLINE const T & operator[](exint i) const noexcept
Definition: UT_Vector2.h:230
constexpr bool operator>(const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2) noexcept
Definition: UT_Vector2.h:585
constexpr SYS_FORCE_INLINE UT_Vector2T< T > operator()(const TS &as) const noexcept
Definition: UT_Vector2.h:779
constexpr UT_Vector2T< SYS_FixedArrayElement_t< TS > > UTmakeVector2T(const TS &as) noexcept
Definition: UT_Vector2.h:792
GLboolean r
Definition: glcorearb.h:1222
UT_Vector2T< T > colVecMult(const UT_Matrix2T< S > &m, const UT_Vector2T< T > &v)
Definition: UT_Vector2.h:715
constexpr SYS_FORCE_INLINE bool isFinite() const noexcept
Definition: UT_Vector2.h:324
SYS_FORCE_INLINE UT_StorageMathFloat_t< T > normalize() noexcept
Definition: UT_Vector2.h:312
constexpr SYS_FORCE_INLINE UT_Vector2T(const UT_FixedVector< T, tuple_size > &v) noexcept
UT_FixedVector of the same size.
Definition: UT_Vector2.h:209
constexpr SYS_FORCE_INLINE bool isEqual(const UT_Vector2T &b, const T tolerance=SYS_FTOLERANCE) const noexcept
Definition: UT_Vector2.h:339
GLint y
Definition: glcorearb.h:103
void dehomogenize()
Express the point in homogeneous coordinates or vice-versa.
Definition: UT_Vector2.h:458
T distance2d(const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2)
Definition: UT_Vector2.h:734
constexpr SYS_FORCE_INLINE UT_Vector2T< T > & operator=(const UT_Vector2T< T > &that)=default
constexpr SYS_FORCE_INLINE UT_Vector2T(const fpreal16 v[]) noexcept
Definition: UT_Vector2.h:178
void homogenize()
Express the point in homogeneous coordinates or vice-versa.
Definition: UT_Vector2.h:457