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