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" // IWYU pragma: export
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);
91 template <typename T>
92 inline bool SYSisEqual(const UT_Vector2T<T> &a, const UT_Vector2T<T> &b)
93 { return SYSisEqual(a, b, SYS_FTOLERANCE); }
94 /// Componentwise integer test
95 template <typename T>
96 inline bool SYSisInteger(const UT_Vector2T<T> &v1)
97 { return SYSisInteger(v1.x()) && SYSisInteger(v1.y()); }
98 /// Componentwise linear interpolation
99 template <typename T,typename S>
100 inline UT_Vector2T<T> SYSlerp(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2, S t);
101 
102 /// Componentwise inverse linear interpolation
103 template <typename T>
105 
106 /// Bilinear interpolation
107 template <typename T,typename S>
108 inline UT_Vector2T<T> SYSbilerp(const UT_Vector2T<T> &u0v0, const UT_Vector2T<T> &u1v0,
109  const UT_Vector2T<T> &u0v1, const UT_Vector2T<T> &u1v1, S u, S v)
110 { return SYSlerp(SYSlerp(u0v0, u0v1, v), SYSlerp(u1v0, u1v1, v), u); }
111 
112 /// Barycentric interpolation
113 template <typename T, typename S>
115  const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2, S u, S v)
116 { return v0 * (1 - u - v) + v1 * u + v2 *v; }
117 
118 /// The orthogonal projection of a vector u onto a vector v
119 template <typename T>
120 inline UT_Vector2T<T> project (const UT_Vector2T<T> &u, const UT_Vector2T<T> &v);
121 
122 /// Multiplication of a row or column vector by a matrix (ie. right vs. left
123 /// multiplication respectively). The operator*() declared above is an alias
124 /// for rowVecMult().
125 // @{
126 //
127 // Notes on optimisation of matrix/vector multiplies:
128 // - multiply(dest, mat) functions have been deprecated in favour of
129 // rowVecMult/colVecMult routines, which produce temporaries. For these to
130 // offer comparable performance, the compiler has to optimize away the
131 // temporary, but most modern compilers can do this. Performance tests with
132 // gcc3.3 indicate that this is a realistic expectation for modern
133 // compilers.
134 // - since matrix/vector multiplies cannot be done without temporary data,
135 // the "primary" functions are the global matrix/vector
136 // rowVecMult/colVecMult routines, rather than the member functions.
137 
138 template <typename T, typename S>
140  const UT_Matrix2T<S> &m);
141 template <typename T, typename S>
142 inline UT_Vector2T<T> colVecMult(const UT_Matrix2T<S> &m,
143  const UT_Vector2T<T> &v);
144 // @}
145 
146 template <typename T>
147 inline T distance2d(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2);
148 
149 /// Given a 2D position, input, and a 2D quad, (p0, p0+du, p0+du+dv+duv, p0+dv),
150 /// finds the 0, 1, or 2 locations in the parameter space of that quad that correspond
151 /// with the input position. Only parameter locations approximately between 0 and 1
152 /// are accepted. The return value is the number of accepted parameter locations.
153 template <typename T>
154 int UTinverseBilerp(const UT_Vector2T<T> &input,
155  const UT_Vector2T<T> &p0,
156  const UT_Vector2T<T> &du, const UT_Vector2T<T> &dv,
157  const UT_Vector2T<T> &duv,
158  UT_Vector2T<T> outputs[2]);
159 
160 /// 2D Vector class.
161 template <typename T>
163 {
164 public:
165  typedef T value_type;
166  static constexpr int tuple_size = 2;
167 
168  /// Default constructor.
169  /// No data is initialized! Use it for extra speed.
170  constexpr SYS_FORCE_INLINE UT_Vector2T() = default;
171 
172  constexpr SYS_FORCE_INLINE UT_Vector2T(const UT_Vector2T<T> &that) = default;
173  constexpr SYS_FORCE_INLINE UT_Vector2T(UT_Vector2T<T> &&that) = default;
174 
175  constexpr SYS_FORCE_INLINE UT_Vector2T(const T vx, const T vy) noexcept :
176  vec{ vx, vy }
177  {}
178 
179  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(T v) noexcept :
180  UT_Vector2T( v, v )
181  {}
182 
183  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const fpreal16 v[]) noexcept :
184  UT_Vector2T( v[0], v[1] )
185  {}
186  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const fpreal32 v[]) noexcept :
187  UT_Vector2T( v[0], v[1] )
188  {}
189  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const fpreal64 v[]) noexcept :
190  UT_Vector2T( v[0], v[1] )
191  {}
192  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const int32 v[]) noexcept :
193  UT_Vector2T( v[0], v[1] )
194  {}
195  constexpr explicit SYS_FORCE_INLINE UT_Vector2T(const int64 v[]) noexcept :
196  UT_Vector2T( v[0], v[1] )
197  {}
198 
199  constexpr explicit UT_Vector2T(const UT_Vector3T<T> &v) noexcept :
200  UT_Vector2T( v.x(), v.y() )
201  {}
202 
203  constexpr explicit UT_Vector2T(const UT_Vector4T<T> &v) noexcept :
204  UT_Vector2T( v.x(), v.y() )
205  {}
206 
207  /// Our own type of any given value_type.
208  template <typename S>
209  constexpr SYS_FORCE_INLINE UT_Vector2T(const UT_Vector2T<S>& 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.
354  /// Assignment operator that truncates a V4 to a V2.
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 auto 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  constexpr SYS_FORCE_INLINE T &x() noexcept { return vec[0]; }
427  constexpr SYS_FORCE_INLINE T x() const noexcept { return vec[0]; }
428  constexpr SYS_FORCE_INLINE T &y() noexcept { return vec[1]; }
429  constexpr SYS_FORCE_INLINE T y() const noexcept { return vec[1]; }
430 
431  T &operator()(unsigned i)
432  {
433  UT_ASSERT_P(i < tuple_size);
434  return vec[i];
435  }
436  T operator()(unsigned i) const
437  {
438  UT_ASSERT_P(i < tuple_size);
439  return vec[i];
440  }
441  // @}
442 
443  /// Compute a hash
444  unsigned hash() const { return SYSvector_hash(data(), tuple_size); }
445 
446  // TODO: eliminate these methods. They're redundant, given good inline
447  // constructors.
448  /// Set the values of the vector components
449  void assign(T xx = 0.0f, T yy = 0.0f)
450  {
451  vec[0] = xx; vec[1] = yy;
452  }
453  /// Set the values of the vector components
454  void assign(const T *v) {vec[0]=v[0]; vec[1]=v[1];}
455 
456  /// Express the point in homogeneous coordinates or vice-versa
457  // @{
458  void homogenize () { vec[0] *= vec[1]; }
459  void dehomogenize() { if (vec[1] != 0) vec[0] /= vec[1]; }
460  // @}
461 
462  /// Protected I/O methods
463  // @{
464  void save(std::ostream &os, int binary = 0) const;
465  bool load(UT_IStream &is);
466  // @}
467 
468  /// @{
469  /// Methods to serialize to a JSON stream. The vector is stored as an
470  /// array of 2 reals.
471  bool save(UT_JSONWriter &w) const;
472  bool save(UT_JSONValue &v) const;
473  bool load(UT_JSONParser &p);
474  /// @}
475 
476  /// Returns the vector size
477  static int entries() { return tuple_size; }
478 
479  T vec[tuple_size];
480 
481 private:
482 
483  friend constexpr bool isZero( const UT_Vector2T& a ) noexcept
484  {
486  }
487 
488  friend constexpr auto length2( const UT_Vector2T& a ) noexcept
489  {
490  return UT::FA::Length2< T, tuple_size >{}( a.vec );
491  }
492 
493  friend constexpr auto distance2( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
494  {
495  return UT::FA::Distance2< T, tuple_size >{}( a.vec, b.vec );
496  }
497 
498  friend constexpr bool operator==( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
499  {
500  return UT::FA::AreEqual< T, tuple_size >{}( a.vec, b.vec );
501  }
502 
503  friend constexpr bool operator!=( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
504  {
505  return ! UT::FA::AreEqual< T, tuple_size >{}( a.vec, b.vec );
506  }
507 
508  /// Lexicographic order comparison operators
509  /// @{
510  friend constexpr bool operator<( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
511  {
512  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) < 0;
513  }
514 
515  friend constexpr bool operator<=( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
516  {
517  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) <= 0;
518  }
519 
520  friend constexpr bool operator>( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
521  {
522  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) > 0;
523  }
524 
525  friend constexpr bool operator>=( const UT_Vector2T& a, const UT_Vector2T& b ) noexcept
526  {
527  return UT::FA::TernaryOrder< T, tuple_size >{}( a.vec, b.vec ) >= 0;
528  }
529  /// @}
530 
531  /// I/O friends
532  // @{
533  friend std::ostream &operator<<(std::ostream &os, const UT_Vector2T<T> &v)
534  {
535  v.save(os);
536  return os;
537  }
538  // @}
539 };
540 
541 template <typename T>
543 {
544  for ( int i = 0; i != tuple_size; ++i )
545  {
546  vec[i] = a;
547  }
548 
549  return *this;
550 }
551 
552 #include "UT_Matrix2.h"
553 
554 // Free floating functions:
555 template <typename T>
556 constexpr UT_Vector2T<T> operator+(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
557 {
558  return UT_Vector2T<T>(v1.x()+v2.x(), v1.y()+v2.y());
559 }
560 template <typename T>
561 constexpr UT_Vector2T<T> operator-(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
562 {
563  return UT_Vector2T<T>(v1.x()-v2.x(), v1.y()-v2.y());
564 }
565 template <typename T>
567 {
568  return UT_Vector2T<T>(v1.x()*v2.x(), v1.y()*v2.y());
569 }
570 template <typename T>
572 {
573  return UT_Vector2T<T>(v1.x()/v2.x(), v1.y()/v2.y());
574 }
575 template <typename T>
576 constexpr bool operator<(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
577 {
578  return ((v1.x() < v2.x()) || (v1.x() == v2.x() && v1.y() < v2.y()));
579 }
580 template <typename T>
581 constexpr bool operator<=(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
582 {
583  return (v1 < v2) || (v1 == v2);
584 }
585 template <typename T>
586 constexpr bool operator>(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
587 {
588  return v2 < v1;
589 }
590 template <typename T>
591 constexpr bool operator>=(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2) noexcept
592 {
593  return v2 <= v1;
594 }
595 template <typename T, typename S>
596 inline UT_Vector2T<T> operator+(const UT_Vector2T<T> &v, S scalar)
597 {
598  return UT_Vector2T<T>(v.x()+scalar, v.y()+scalar);
599 }
600 template <typename T, typename S>
601 inline UT_Vector2T<T> operator+(S scalar, const UT_Vector2T<T> &v)
602 {
603  return UT_Vector2T<T>(v.x()+scalar, v.y()+scalar);
604 }
605 template <typename T, typename S>
606 inline UT_Vector2T<T> operator-(const UT_Vector2T<T> &v, S scalar)
607 {
608  return UT_Vector2T<T>(v.x()-scalar, v.y()-scalar);
609 }
610 template <typename T, typename S>
611 inline UT_Vector2T<T> operator-(S scalar, const UT_Vector2T<T> &v)
612 {
613  return UT_Vector2T<T>(scalar-v.x(), scalar-v.y());
614 }
615 template <typename T, typename S>
616 inline UT_Vector2T<T> operator*(const UT_Vector2T<T> &v, S scalar)
617 {
618  return UT_Vector2T<T>(v.x()*scalar, v.y()*scalar);
619 }
620 template <typename T, typename S>
621 constexpr UT_Vector2T<T> operator*(S scalar, const UT_Vector2T<T> &v) noexcept
622 {
623  return UT_Vector2T<T>(v.x()*scalar, v.y()*scalar);
624 }
625 template <typename T, typename S>
626 inline UT_Vector2T<T> operator/(const UT_Vector2T<T> &v, S scalar)
627 {
628  return UT_Vector2T<T>(v.x()/scalar, v.y()/scalar);
629 }
630 template <typename T, typename S>
631 inline UT_Vector2T<T> operator/(S scalar, const UT_Vector2T<T> &v)
632 {
633  return UT_Vector2T<T>(scalar/v.x(), scalar/v.y());
634 }
635 
636 template <typename T>
637 constexpr auto dot( const UT_Vector2T< T >& a, const UT_Vector2T< T >& b ) noexcept
638 {
639  return UT::FA::Dot< T, 2 >{}( a.vec, b.vec );
640 }
641 
642 template <typename T>
643 inline T cross(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
644 {
645  return v1.x() * v2.y() - v1.y() * v2.x();
646 }
647 template <typename T>
648 inline
650 {
651  return UT_Vector2T<T>(SYSabs(v.x()), SYSabs(v.y()));
652 }
653 
654 
655 template <typename T>
656 inline
658 {
659  return UT_Vector2T<T>(
660  SYSmin(v1.x(), v2.x()),
661  SYSmin(v1.y(), v2.y())
662  );
663 }
664 
665 template <typename T>
666 inline
668 {
669  return UT_Vector2T<T>(
670  SYSmax(v1.x(), v2.x()),
671  SYSmax(v1.y(), v2.y())
672  );
673 }
674 
675 template <typename T, typename S>
676 inline bool
678 {
679  return a.isEqual(b, tol);
680 }
681 
682 template <typename T,typename S>
683 inline
685 {
686  return UT_Vector2T<T>(
687  SYSlerp(v1.x(), v2.x(), t),
688  SYSlerp(v1.y(), v2.y(), t));
689 }
690 
691 template <typename T>
692 inline
694  const UT_Vector2T<T> &v1,
695  const UT_Vector2T<T> &v2)
696 {
697  return UT_Vector2T<T>(
698  SYSinvlerp(a.x(), v1.x(), v2.x()),
699  SYSinvlerp(a.y(), v1.y(), v2.y()));
700 }
701 
702 template <typename T>
704 {
705  return dot(u, v) / v.length2() * v;
706 }
707 template <typename T, typename S>
709 {
710  return UT_Vector2T<T>(v.x()*m(0,0) + v.y()*m(1,0),
711  v.x()*m(0,1) + v.y()*m(1,1));
712 }
713 template <>
715 {
716  const v4uf l(v.x(), v.x(), v.y(), v.y());
717  const v4uf r(m.data());
718  const v4uf p = l * r;
719 
720  return UT_Vector2T<float>(p[0] + p[2], p[1] + p[3]);
721 }
722 template <typename T, typename S>
724 {
725  return UT_Vector2T<T>(m(0,0)*v.x() + m(0,1)*v.y(),
726  m(1,0)*v.x() + m(1,1)*v.y());
727 }
728 template <>
730 {
731  const v4uf l(m.data());
732  const v4uf r(v.x(), v.y(), v.x(), v.y());
733  const v4uf p = l * r;
734  return UT_Vector2T<float>(p[0] + p[1], p[2] + p[3]);
735 }
736 template <typename T, typename S>
738 {
739  return rowVecMult(v, m);
740 }
741 template <typename T>
742 inline T distance2d(const UT_Vector2T<T> &v1, const UT_Vector2T<T> &v2)
743 {
744  return (v1 - v2).length();
745 }
746 
747 template <typename T>
748 inline size_t hash_value(const UT_Vector2T<T> &val)
749 {
750  return val.hash();
751 }
752 
753 // Overload for custom formatting of UT_Vector2T<T> with UTformat.
754 template <typename T>
755 UT_API size_t
756 UTformatBuffer(char *buffer, size_t buffer_size, const UT_Vector2T<T> &v);
757 
758 template< typename T, exint D >
759 class UT_FixedVector;
760 
761 template<typename T>
763 {
765  typedef T DataType;
766  static const exint TupleSize = 2;
767  static const bool isVectorType = true;
768 };
769 
770 // UT_Vector2T in the role of a fixed array-like type.
771 
772 template< typename T >
774 
775 template< typename T >
777 
778 template< typename T >
779 struct SYS_FixedArraySizeNoCVRef< UT_Vector2T< T > > : std::integral_constant< std::size_t, 2 > {};
780 
781 
782 // UT_Vector2TFromUnbounded<T> is a function object that
783 // creates a UT_Vector2T<T> from an unbounded array-like type 'as'.
784 // 'as' must have at size at least 2.
785 template <typename T>
787 {
788  template< typename TS >
789  constexpr SYS_FORCE_INLINE UT_Vector2T<T> operator()(const TS& as) const noexcept
790  {
791  return UT_Vector2T<T>( as[0], as[1] );
792  }
793 };
794 
795 // UT_FromUnbounded<V> creates a V from an unbounded array-like type
796 
797 // Primary
798 template <typename V >
799 struct UT_FromUnbounded;
800 
801 // Partial specialization for UT_Vector2T
802 template <typename T>
804 
805 
806 // UT_Vector2TFromFixed<T> is a function object that
807 // creates a UT_Vector2T<T> from a fixed array-like type TS,
808 // examples of which include T[2], UT_FixedVector<T,2> and UT_FixedArray<T,2> (AKA std::array<T,2>)
809 // This version is safer than UT_Vector2TFromUnbounded, but it can be used
810 // only if the size of 'as' can be deduced at compile time.
811 template <typename T>
813 {
814  template< typename TS >
815  constexpr SYS_FORCE_INLINE UT_Vector2T<T> operator()(const TS& as) const noexcept
816  {
817  SYS_STATIC_ASSERT( SYS_IsFixedArrayOf_v< TS, T, 2 > );
818 
819  return UT_Vector2TFromUnbounded< T >{}( as );
820  }
821 };
822 
823 // Convert a fixed array-like type TS into a UT_Vector2T< T >.
824 // This allows conversion to UT_Vector2T without fixing T.
825 // Instead, the element type of TS determines the type T.
826 template< typename TS >
828 UTmakeVector2T( const TS& as ) noexcept
829 {
831 
832  return UT_Vector2TFromFixed< T >{}( as );
833 }
834 
835 // UT_FromFixed<V> creates a V from a flat, fixed array-like representation
836 
837 // Primary
838 template <typename V >
839 struct UT_FromFixed;
840 
841 // Partial specialization for UT_Vector2T
842 template <typename T>
844 
845 // Relocation traits for UT_Vector2T are defined in UT_VectorTypes.h
846 
847 #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:209
T vec[tuple_size]
Definition: UT_Vector2.h:479
constexpr SYS_FORCE_INLINE UT_Vector2T & operator*=(const T &a) noexcept
Definition: UT_Vector2.h:268
bool SYSisEqual(const UT_Vector2T< T > &a, const UT_Vector2T< T > &b, S tol)
Componentwise equality.
Definition: UT_Vector2.h:677
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:649
constexpr SYS_FORCE_INLINE UT_Vector2T(const fpreal64 v[]) noexcept
Definition: UT_Vector2.h:189
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:185
class UT_API UT_Vector2T
constexpr SYS_FORCE_INLINE T x() const noexcept
Definition: UT_Vector2.h:427
#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:503
friend constexpr bool operator<=(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:515
UT_FixedVector< T, 2 > FixedVectorType
Definition: UT_Vector2.h:764
T & operator()(unsigned i)
Definition: UT_Vector2.h:431
UT_FromUnbounded creates a V from an unbounded array-like type.
Definition: UT_Matrix2.h:733
UT_Vector2T< T > rowVecMult(const UT_Vector2T< T > &v, const UT_Matrix2T< S > &m)
Definition: UT_Vector2.h:708
constexpr SYS_FORCE_INLINE T & operator[](exint i) noexcept
Definition: UT_Vector2.h:237
static int entries()
Returns the vector size.
Definition: UT_Vector2.h:477
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:386
T whichSide(const UT_Vector2T< T > &e1, const UT_Vector2T< T > &e2) const
Definition: UT_Vector2.h:374
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:259
UT_Vector2T< T > operator/(const UT_Vector2T< T > &v, S scalar)
Definition: UT_Vector2.h:626
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:96
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:242
void assign(const T *v)
Set the values of the vector components.
Definition: UT_Vector2.h:454
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:789
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:39
constexpr SYS_FORCE_INLINE auto dot(const UT_Vector2T &b) const noexcept
Definition: UT_Vector2.h:402
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:483
3D Vector class.
4D Vector class.
Definition: UT_Vector4.h:176
2D Vector class.
Definition: UT_Vector2.h:162
float fpreal32
Definition: SYS_Types.h:200
GLuint buffer
Definition: glcorearb.h:660
constexpr UT_Vector2T(const UT_Vector4T< T > &v) noexcept
Definition: UT_Vector2.h:203
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:643
UT_Vector2T< T > & operator*=(const UT_Matrix2T< S > &mat)
Definition: UT_Vector2.h:381
double fpreal64
Definition: SYS_Types.h:201
constexpr SYS_FORCE_INLINE T & x() noexcept
Definition: UT_Vector2.h:426
constexpr SYS_FORCE_INLINE UT_Vector2T< T > & operator=(const UT_Vector2T< S > &v)
Definition: UT_Vector2.h:217
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:684
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
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:114
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:587
typename UT_StorageAtLeast32Bit< T0, T1 >::type UT_StorageAtLeast32Bit_t
Definition: UT_Storage.h:285
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:164
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:444
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:703
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:319
constexpr SYS_FORCE_INLINE const T * data() const noexcept
Definition: UT_Vector2.h:220
friend constexpr auto length2(const UT_Vector2T &a) noexcept
Definition: UT_Vector2.h:488
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:510
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:693
friend constexpr bool operator==(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:498
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:108
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:591
GLdouble t
Definition: glad.h:2397
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:186
GLfloat v0
Definition: glcorearb.h:816
SYS_FORCE_INLINE UT_Vector2T< T > operator-() const
Definition: UT_Vector2.h:359
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
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:667
size_t hash_value(const UT_Vector2T< T > &val)
Definition: UT_Vector2.h:748
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
UT_API size_t UTformatBuffer(char *buffer, size_t buffer_size, const UT_Vector2T< T > &v)
constexpr UT_Vector2T(const UT_Vector3T< T > &v) noexcept
Definition: UT_Vector2.h:199
constexpr SYS_FORCE_INLINE T maxComponent() const noexcept
Definition: UT_Vector2.h:344
T operator()(unsigned i) const
Definition: UT_Vector2.h:436
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:175
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:520
friend constexpr bool operator>=(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:525
constexpr SYS_FORCE_INLINE UT_Vector2T(const int64 v[]) noexcept
Definition: UT_Vector2.h:195
#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:449
UT_Vector2T< T > SYSmin(const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2)
Componentwise min and maximum.
Definition: UT_Vector2.h:657
constexpr SYS_FORCE_INLINE UT_Vector2T(T v) noexcept
Definition: UT_Vector2.h:179
constexpr SYS_FORCE_INLINE UT_Vector2T(const int32 v[]) noexcept
Definition: UT_Vector2.h:192
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:230
constexpr bool operator>(const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2) noexcept
Definition: UT_Vector2.h:586
constexpr SYS_FORCE_INLINE UT_Vector2T< T > operator()(const TS &as) const noexcept
Definition: UT_Vector2.h:815
constexpr UT_Vector2T< SYS_FixedArrayElement_t< TS > > UTmakeVector2T(const TS &as) noexcept
Definition: UT_Vector2.h:828
UT_Vector2T< T > colVecMult(const UT_Matrix2T< S > &m, const UT_Vector2T< T > &v)
Definition: UT_Vector2.h:723
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 bool isEqual(const UT_Vector2T &b, const T tolerance=SYS_FTOLERANCE) const noexcept
Definition: UT_Vector2.h:339
friend constexpr auto distance2(const UT_Vector2T &a, const UT_Vector2T &b) noexcept
Definition: UT_Vector2.h:493
void dehomogenize()
Express the point in homogeneous coordinates or vice-versa.
Definition: UT_Vector2.h:459
constexpr SYS_FORCE_INLINE T & y() noexcept
Definition: UT_Vector2.h:428
T distance2d(const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2)
Definition: UT_Vector2.h:742
constexpr auto dot(const UT_Vector2T< T > &a, const UT_Vector2T< T > &b) noexcept
The dot product.
Definition: UT_Vector2.h:637
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:429
constexpr SYS_FORCE_INLINE UT_Vector2T(const fpreal16 v[]) noexcept
Definition: UT_Vector2.h:183
void homogenize()
Express the point in homogeneous coordinates or vice-versa.
Definition: UT_Vector2.h:458