HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Vector4.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: Utility Library (C++)
7  *
8  * COMMENTS:
9  * This class handles fpreal vectors of dimension 4.
10  *
11  * WARNING:
12  * This class should NOT contain any virtual methods, nor should it
13  * define more member data. The size of UT_Vector4 must always be
14  * 16 bytes (4 floats).
15  *
16  */
17 
18 #pragma once
19 
20 #ifndef __UT_Vector4_h__
21 #define __UT_Vector4_h__
22 
23 #include "UT_API.h"
24 #include "UT_Assert.h"
25 #include "UT_FixedVector.h"
26 #include "UT_VectorTypes.h"
27 #include <SYS/SYS_Deprecated.h>
28 #include <SYS/SYS_Inline.h>
29 #include <SYS/SYS_Math.h>
30 #include <iosfwd>
31 #include <limits>
32 
33 #ifndef UT_DISABLE_VECTORIZE_MATRIX
34 #include <VM/VM_SIMD.h>
35 #endif
36 
37 class UT_IStream;
38 class UT_JSONWriter;
39 class UT_JSONValue;
40 class UT_JSONParser;
41 
42 // Free floating functions:
43 
44 // Right-multiply operators (M*v) have been removed. They had previously
45 // been defined to return v*M, which was too counterintuitive. Once HDK
46 // etc. users have a chance to update their code (post 7.0) we could
47 // reintroduce a right-multiply operator that does a colVecMult.
48 
49 template <typename T, typename S>
50 inline UT_Vector4T<T> operator*(const UT_Vector4T<T> &v, const UT_Matrix4T<S> &m);
51 template <typename T>
53 template <typename T>
55 template <typename T, typename S>
56 inline UT_Vector4T<T> operator+(const UT_Vector4T<T> &v, S scalar);
57 template <typename T, typename S>
58 inline UT_Vector4T<T> operator-(const UT_Vector4T<T> &v, S scalar);
59 template <typename T, typename S>
60 inline UT_Vector4T<T> operator*(const UT_Vector4T<T> &v, S scalar);
61 template <typename T, typename S>
62 inline UT_Vector4T<T> operator/(const UT_Vector4T<T> &v, S scalar);
63 template <typename T, typename S>
64 inline UT_Vector4T<T> operator+(S scalar, const UT_Vector4T<T> &v);
65 template <typename T, typename S>
66 inline UT_Vector4T<T> operator-(S scalar, const UT_Vector4T<T> &v);
67 template <typename T, typename S>
68 inline UT_Vector4T<T> operator*(S scalar, const UT_Vector4T<T> &v);
69 template <typename T, typename S>
70 inline UT_Vector4T<T> operator/(S scalar, const UT_Vector4T<T> &v);
71 
72 /// Although the cross product is undefined for 4D vectors, we believe it's
73 /// useful in practice to define a function that takes two 4D vectors and
74 /// computes the cross-product of their first 3 components
75 template <typename T>
77 template <typename T>
79 template <typename T>
81 
82 /// The dot product between two vectors
83 // @{
84 template <typename T>
85 inline T dot(const UT_Vector4T<T> &v1, const UT_Vector4T<T> &v2);
86 template <typename T>
87 inline T dot(const UT_Vector4T<T> &v1, const UT_Vector3T<T> &v2);
88 template <typename T>
89 inline T dot(const UT_Vector3T<T> &v1, const UT_Vector4T<T> &v2);
90 // @}
91 
92 /// Componentwise min and maximum
93 template <typename T>
94 inline UT_Vector4T<T> SYSmin (const UT_Vector4T<T> &v1, const UT_Vector4T<T> &v2);
95 template <typename T>
96 inline UT_Vector4T<T> SYSmax (const UT_Vector4T<T> &v1, const UT_Vector4T<T> &v2);
97 
98 /// Componentwise linear interpolation
99 template <typename T,typename S>
100 inline UT_Vector4T<T> SYSlerp(const UT_Vector4T<T> &v1, const UT_Vector4T<T> &v2, S t);
101 template <typename T,typename S>
102 inline UT_Vector4T<T> SYSbilerp(const UT_Vector4T<T> &u0v0, const UT_Vector4T<T> &u1v0,
103  const UT_Vector4T<T> &u0v1, const UT_Vector4T<T> &u1v1,
104  S u, S v)
105 { return SYSlerp(SYSlerp(u0v0, u0v1, v), SYSlerp(u1v0, u1v1, v), u); }
106 
107 
108 /// Multiplication of a row or column vector by a matrix (ie. right vs. left
109 /// multiplication respectively). The operator*() declared above is an alias
110 /// for rowVecMult().
111 // @{
112 //
113 // Notes on optimisation of matrix/vector multiplies:
114 // - multiply(dest, mat) functions have been deprecated in favour of
115 // rowVecMult/colVecMult routines, which produce temporaries. For these to
116 // offer comparable performance, the compiler has to optimize away the
117 // temporary, but most modern compilers can do this. Performance tests with
118 // gcc3.3 indicate that this is a realistic expectation for modern
119 // compilers.
120 // - since matrix/vector multiplies cannot be done without temporary data,
121 // the "primary" functions are the global matrix/vector
122 // rowVecMult/colVecMult routines, rather than the member functions.
123 // - inlining is explicitly requested only for functions involving the
124 // native types (UT_Vector4 and UT_Matrix4)
125 template <typename T, typename S>
127 template <typename T, typename S>
129 
130 template <typename T, typename S>
132 template <typename T, typename S>
134 // @}
135 
136 /// Compute the distance between two points
137 // @{
138 template <typename T>
139 inline T distance4(const UT_Vector4T<T> &p1, const UT_Vector4T<T> &p2);
140 template <typename T>
141 inline T distance3(const UT_Vector4T<T> &p1, const UT_Vector4T<T> &p2);
142 template <typename T>
143 inline T distance3d(const UT_Vector4T<T> &p1, const UT_Vector4T<T> &p2)
144 { return distance3(p1, p2); }
145 template <typename T>
146 inline T distance3d(const UT_Vector3T<T> &p1, const UT_Vector4T<T> &p2)
147 { return distance3d(p1, UT_Vector3T<T>(p2)); }
148 template <typename T>
149 inline T distance3d(const UT_Vector4T<T> &p1, const UT_Vector3T<T> &p2)
150 { return distance3d(UT_Vector3T<T>(p1), p2); }
151 // @}
152 
153 /// 4D Vector class.
154 template <typename T>
155 class UT_API UT_Vector4T : public UT_FixedVector<T,4,true>
156 {
157 public:
159 
160  // These "using" statements are needed for operator= and operator*=
161  // so that the ones in UT_FixedVector aren't hidden by the additional
162  // ones here.
163  using Base::operator=;
164  using Base::operator*=;
165 
166  // These "using" statements are needed for GCC and Clang, because
167  // otherwise, they ignore all members of UT_FixedVector when
168  // checking validity of code in functions in this class.
169 private:
170  using Base::vec;
171 public:
172  using Base::data;
173  typedef T value_type;
174  static const int tuple_size = 4;
175 
176  /// Default constructor.
177  /// No data is initialized! Use it for extra speed.
178  SYS_FORCE_INLINE UT_Vector4T() = default;
179 
180  SYS_FORCE_INLINE UT_Vector4T(const UT_Vector4T<T> &that) = default;
181  SYS_FORCE_INLINE UT_Vector4T(UT_Vector4T<T> &&that) = default;
182 
183  SYS_FORCE_INLINE UT_Vector4T(T vx, T vy, T vz, T vw = 1.0f)
184  {
185  vec[0] = vx;
186  vec[1] = vy;
187  vec[2] = vz;
188  vec[3] = vw;
189  }
191  : Base(v)
192  {}
194  : Base(v)
195  {}
196  SYS_FORCE_INLINE UT_Vector4T(const int32 v[tuple_size])
197  : Base(v)
198  {}
199  SYS_FORCE_INLINE UT_Vector4T(const int64 v[tuple_size])
200  : Base(v)
201  {}
202 
203  // Initialises the vector as [x,y,0,1]
205  explicit UT_Vector4T(const UT_Vector2T<T> &v);
207  explicit UT_Vector4T(const UT_Vector3T<T> &v, T w = 1.f);
208 
209  /// Our own type of any given value_type.
210  template <typename S>
212  : Base(v)
213  {}
214 
215  /// Arbitrary UT_FixedVector of the same size
216  template <typename S,bool S_INSTANTIATED>
218  : Base(v)
219  {}
220 
223 
224  template <typename S>
226  { vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2]; vec[3] = v[3];
227  return *this; }
228 
229 
230  // TODO: We could remove this. It's not as error-prone as some other
231  // conversions, but it might still be better to force the user to do
232  // an explicit cast i.e., v4 = UT_Vector4(v3)
233 
234  /// Assignment operator that creates a V4 from a V3 by adding a '1'
235  /// element.
236  SYS_DEPRECATED_HDK_REPLACE(16.0,explicit UT_Vector4 constructor to avoid implicit conversion from UT_Vector3)
238 
239  int equalZero3(T tol = 0.00001f) const
240  {
241  return (vec[0] >= -tol && vec[0] <= tol) &&
242  (vec[1] >= -tol && vec[1] <= tol) &&
243  (vec[2] >= -tol && vec[2] <= tol);
244  }
245 
246  void clampZero(T tol = 0.00001f)
247  {
248  if (vec[0] >= -tol && vec[0] <= tol) vec[0] = 0;
249  if (vec[1] >= -tol && vec[1] <= tol) vec[1] = 0;
250  if (vec[2] >= -tol && vec[2] <= tol) vec[2] = 0;
251  if (vec[3] >= -tol && vec[3] <= tol) vec[3] = 0;
252  }
253 
254  void clampZero3(T tol = 0.00001f)
255  {
256  if (vec[0] >= -tol && vec[0] <= tol) vec[0] = 0;
257  if (vec[1] >= -tol && vec[1] <= tol) vec[1] = 0;
258  if (vec[2] >= -tol && vec[2] <= tol) vec[2] = 0;
259  }
260 
261  void negate3()
262  { vec[0]= -vec[0]; vec[1]= -vec[1]; vec[2]= -vec[2]; }
263 
265  {
266  vec[0] *= v.vec[0];
267  vec[1] *= v.vec[1];
268  vec[2] *= v.vec[2];
269  vec[3] *= v.vec[3];
270  }
271 
272  using Base::isEqual;
273 
274  SYS_DEPRECATED_HDK_REPLACE(16.0,explicit conversion to UT_Vector3 followed by isEqual)
275  inline int isEqual(const UT_Vector3T<T> &vect, T tol = 0.00001f) const;
276 
277  /// If you need a multiplication operator that left multiplies the vector
278  /// by a matrix (M * v), use the following colVecMult() functions. If
279  /// you'd rather not use operator*=() for right-multiplications (v * M),
280  /// use the following rowVecMult() functions.
281  // @{
282  template <typename S>
283  inline void rowVecMult(const UT_Matrix4T<S> &m)
284  { operator=(::rowVecMult(*this, m)); }
285  template <typename S>
286  inline void colVecMult(const UT_Matrix4T<S> &m)
287  { operator=(::colVecMult(m, *this)); }
288  // @}
289 
290  /// This multiply will ignore the 4th component both in the vector an in
291  /// the matrix. This helps when you want to avoid affecting the 'w'
292  /// component. This in turns annihilates the translation components (row 4)
293  /// in mat, so be careful.
294  // @{
295  template <typename S>
297  { operator=(::rowVecMult3(*this, m)); }
298  // @}
299 
300  // The *= and multiply3 routines are provided for
301  // legacy reasons. They all assume that *this is a row vector. Generally,
302  // the rowVecMult and colVecMult methods are preferred, since they're
303  // more explicit about the row vector assumption.
304  // @{
305  template <typename S>
306  inline
308  { rowVecMult(mat); return *this; }
309 
310  template <typename S>
311  inline void multiply3(const UT_Matrix4T<S> &mat)
312  { rowVecMult3(mat); }
313  template <typename S>
314  inline void multiply3(UT_Vector4T<T> &dest, const UT_Matrix4T<S> &mat) const
315  { dest = ::rowVecMult3(*this, mat); }
316  // @}
317 
318  /// These allow you to find out what indices to use for different axes
319  // @{
320  int findMinAbsAxis() const
321  {
322  if (SYSabs(x()) < SYSabs(y()))
323  if (SYSabs(z()) < SYSabs(x()))
324  if (SYSabs(w()) < SYSabs(z()))
325  return 3;
326  else
327  return 2;
328  else
329  if (SYSabs(w()) < SYSabs(x()))
330  return 3;
331  else
332  return 0;
333  else
334  if (SYSabs(z()) < SYSabs(y()))
335  if (SYSabs(w()) < SYSabs(z()))
336  return 3;
337  else
338  return 2;
339  else
340  if (SYSabs(w()) < SYSabs(y()))
341  return 3;
342  else
343  return 1;
344  }
345  int findMaxAbsAxis() const
346  {
347  if (SYSabs(x()) >= SYSabs(y()))
348  if (SYSabs(z()) >= SYSabs(x()))
349  if (SYSabs(w()) >= SYSabs(z()))
350  return 3;
351  else
352  return 2;
353  else
354  if (SYSabs(w()) >= SYSabs(x()))
355  return 3;
356  else
357  return 0;
358  else
359  if (SYSabs(z()) >= SYSabs(y()))
360  if (SYSabs(w()) >= SYSabs(z()))
361  return 3;
362  else
363  return 2;
364  else
365  if (SYSabs(w()) >= SYSabs(y()))
366  return 3;
367  else
368  return 1;
369  }
370  // @}
371 
372  /// Return the components of the vector. The () operator does NOT check
373  /// for the boundary condition.
374  // @{
375  inline T &x(void) { return vec[0]; }
376  inline T x(void) const { return vec[0]; }
377  inline T &y(void) { return vec[1]; }
378  inline T y(void) const { return vec[1]; }
379  inline T &z(void) { return vec[2]; }
380  inline T z(void) const { return vec[2]; }
381  inline T &w(void) { return vec[3]; }
382  inline T w(void) const { return vec[3]; }
383  inline T &operator()(unsigned i)
384  {
385  UT_ASSERT_P(i < tuple_size);
386  return vec[i];
387  }
388  inline T operator()(unsigned i) const
389  {
390  UT_ASSERT_P(i < tuple_size);
391  return vec[i];
392  }
393  // @}
394 
395  /// Compute a hash
396  unsigned hash() const { return SYSvector_hash(data(), tuple_size); }
397 
398  // TODO: eliminate these methods. They're redundant, given good inline
399  // constructors.
400  /// Set the values of the vector components
401  void assign(T xx = 0.0f, T yy = 0.0f, T zz = 0.0f,
402  T ww = 1.0f)
403  {
404  vec[0] = xx; vec[1] = yy; vec[2] = zz; vec[3] = ww;
405  }
406  /// Set the values of the vector components
407  void assign(const T *v, int size = tuple_size)
408  {
409  vec[0] = v[0];
410  vec[1] = v[1];
411  vec[2] = v[2];
412  if (size == tuple_size) vec[3] = v[3];
413  }
414 
415  /// Express the point in homogeneous coordinates or vice-versa
416  // @{
417  void homogenize(void)
418  {
419  vec[0] *= vec[3];
420  vec[1] *= vec[3];
421  vec[2] *= vec[3];
422  }
423  void dehomogenize(void)
424  {
425  if (vec[3] != 0)
426  {
427  T denom = 1.0f / vec[3];
428  vec[0] *= denom;
429  vec[1] *= denom;
430  vec[2] *= denom;
431  }
432  }
433  // @}
434 
435  void save(std::ostream &os, int binary=0) const;
436  bool load(UT_IStream &is);
437 
438  /// @{
439  /// Methods to serialize to a JSON stream. The vector is stored as an
440  /// array of 4 reals.
441  bool save(UT_JSONWriter &w) const;
442  bool save(UT_JSONValue &v) const;
443  bool load(UT_JSONParser &p);
444  /// @}
445 
446  /// Returns the vector size
447  static int entries() { return tuple_size; }
448 
449 private:
450  /// I/O friends
451  // @{
452  friend std::ostream &operator<<(std::ostream &os, const UT_Vector4T<T> &v)
453  {
454  v.save(os);
455  return os;
456  }
457  // @}
458 
459  /// The negate operator is not provided, because of potentially
460  /// unintuitive behaviour: you very rarely actually want to negate the
461  /// w component.
462  UT_Vector4T<T> operator-() const
463  {
464  UT_ASSERT(0);
465  UT_Vector4T a(*this);
466  a.negate();
467  return a;
468  }
469 };
470 
471 #include "UT_Vector2.h"
472 #include "UT_Vector3.h"
473 
474 template <typename T>
476 {
477  vec[0] = v.x();
478  vec[1] = v.y();
479  vec[2] = T(0);
480  vec[3] = T(1);
481 }
482 template <typename T>
484 {
485  vec[0] = v.x();
486  vec[1] = v.y();
487  vec[2] = v.z();
488  vec[3] = vw;
489 }
490 
491 #ifndef UT_DISABLE_VECTORIZE_MATRIX
492 template <> inline void
494 {
495  v4uf l(this->data());
496  const v4uf r(v.data());
497  l *= r;
498 
499  vm_store(this->data(), l.vector);
500 }
501 #endif
502 
503 template <typename T>
504 inline int
505 UT_Vector4T<T>::isEqual(const UT_Vector3T<T> &vect, T tol) const
506 {
507  return ((vec[0]>=vect.x()-tol) && (vec[0]<=vect.x()+tol) &&
508  (vec[1]>=vect.y()-tol) && (vec[1]<=vect.y()+tol) &&
509  (vec[2]>=vect.z()-tol) && (vec[2]<=vect.z()+tol));
510 }
511 
512 // Free floating functions:
513 template <typename T>
515 {
516  return UT_Vector4T<T>(v1.x()+v2.x(), v1.y()+v2.y(),
517  v1.z()+v2.z(), v1.w()+v2.w());
518 }
519 template <typename T>
521 {
522  return UT_Vector3T<T>(v1.x()+v2.x(), v1.y()+v2.y(),
523  v1.z()+v2.z());
524 }
525 template <typename T>
527 {
528  return UT_Vector3T<T>(v1.x()+v2.x(), v1.y()+v2.y(),
529  v1.z()+v2.z());
530 }
531 template <typename T>
533 {
534  return UT_Vector4T<T>(v1.x()-v2.x(), v1.y()-v2.y(),
535  v1.z()-v2.z(), v1.w()-v2.w());
536 }
537 template <typename T>
539 {
540  return UT_Vector3T<T>(v1.x()-v2.x(), v1.y()-v2.y(),
541  v1.z()-v2.z());
542 }
543 template <typename T>
545 {
546  return UT_Vector3T<T>(v1.x()-v2.x(), v1.y()-v2.y(),
547  v1.z()-v2.z());
548 }
549 template <typename T>
551 {
552  return UT_Vector4T<T>(v1.x()*v2.x(), v1.y()*v2.y(), v1.z()*v2.z(), v1.w()*v2.w());
553 }
554 
555 template <typename T>
557 {
558  return UT_Vector4T<T>(v1.x()/v2.x(), v1.y()/v2.y(), v1.z()/v2.z(), v1.w()/v2.w());
559 }
560 #ifndef UT_DISABLE_VECTORIZE_MATRIX
561 template <>
563 {
564  const v4uf l(v1.data());
565  const v4uf r(v2.data());
566  const v4uf result = l * r;
567  return UT_Vector4T<float>((float*) &result);
568 }
569 template <>
571 {
572  const v4uf l(v1.data());
573  const v4uf r(v2.data());
574  const v4uf result = l / r;
575  return UT_Vector4T<float>((float*) &result);
576 }
577 #endif
578 
579 template <typename T, typename S>
580 inline UT_Vector4T<T> operator+(const UT_Vector4T<T> &v, S scalar)
581 {
582  return UT_Vector4T<T>(v.x()+scalar, v.y()+scalar, v.z()+scalar, v.w()+scalar);
583 }
584 template <typename T, typename S>
585 inline UT_Vector4T<T> operator+(S scalar, const UT_Vector4T<T> &v)
586 {
587  return UT_Vector4T<T>(v.x()+scalar, v.y()+scalar, v.z()+scalar, v.w()+scalar);
588 }
589 template <typename T, typename S>
590 inline UT_Vector4T<T> operator-(const UT_Vector4T<T> &v, S scalar)
591 {
592  return UT_Vector4T<T>(v.x()-scalar, v.y()-scalar, v.z()-scalar, v.w()-scalar);
593 }
594 template <typename T, typename S>
595 inline UT_Vector4T<T> operator-(S scalar, const UT_Vector4T<T> &v)
596 {
597  return UT_Vector4T<T>(scalar-v.x(), scalar-v.y(), scalar-v.z(), v.w()-scalar);
598 }
599 template <typename T, typename S>
600 inline UT_Vector4T<T> operator*(const UT_Vector4T<T> &v, S scalar)
601 {
602  return UT_Vector4T<T>(v.x()*scalar, v.y()*scalar, v.z()*scalar, v.w()*scalar);
603 }
604 template <typename T, typename S>
605 inline UT_Vector4T<T> operator*(S scalar, const UT_Vector4T<T> &v)
606 {
607  return UT_Vector4T<T>(v.x()*scalar, v.y()*scalar, v.z()*scalar, v.w()*scalar);
608 }
609 template <typename T, typename S>
610 inline UT_Vector4T<T> operator/(const UT_Vector4T<T> &v, S scalar)
611 {
612  // This has to be T because S may be int for "v = v/2" code
613  // For the same reason we must cast the 1
614  T inv = ((T)1) / scalar;
615  return UT_Vector4T<T>(v.x()*inv, v.y()*inv, v.z()*inv, v.w()*inv);
616 }
617 template <typename T, typename S>
618 inline UT_Vector4T<T> operator/(S scalar, const UT_Vector4T<T> &v)
619 {
620  return UT_Vector4T<T>(scalar/v.x(), scalar/v.y(), scalar/v.z(), scalar/v.w());
621 }
622 template <typename T>
623 inline T dot(const UT_Vector4T<T> &v1, const UT_Vector4T<T> &v2)
624 {
625  return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z() + v1.w()*v2.w();
626 }
627 #ifndef UT_DISABLE_VECTORIZE_MATRIX
628 template <>
629 inline float dot(const UT_Vector4T<float> &v1, const UT_Vector4T<float> &v2)
630 {
631  return dot4(v4uf(v1.data()), v4uf(v2.data()));
632 }
633 #endif
634 
635 template <typename T>
636 inline T dot(const UT_Vector4T<T> &v1, const UT_Vector3T<T> &v2)
637 {
638  return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z();
639 }
640 template <typename T>
641 inline T dot(const UT_Vector3T<T> &v1, const UT_Vector4T<T> &v2)
642 {
643  return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z();
644 }
645 template <typename T>
646 inline
648 {
649  return UT_Vector4T<T>(
650  SYSmin(v1.x(), v2.x()),
651  SYSmin(v1.y(), v2.y()),
652  SYSmin(v1.z(), v2.z()),
653  SYSmin(v1.w(), v2.w())
654  );
655 }
656 
657 template <typename T>
658 inline
660 {
661  return UT_Vector4T<T>(
662  SYSmax(v1.x(), v2.x()),
663  SYSmax(v1.y(), v2.y()),
664  SYSmax(v1.z(), v2.z()),
665  SYSmax(v1.w(), v2.w())
666  );
667 }
668 
669 template <typename T,typename S>
670 inline
672 {
673  return UT_Vector4T<T>(
674  SYSlerp(v1.x(), v2.x(), t),
675  SYSlerp(v1.y(), v2.y(), t),
676  SYSlerp(v1.z(), v2.z(), t),
677  SYSlerp(v1.w(), v2.w(), t));
678 }
679 #ifndef UT_DISABLE_VECTORIZE_MATRIX
680 template <>
681 inline
683 {
684  const v4uf l(v1.data());
685  const v4uf r(v2.data());
686  const v4uf result = SYSlerp(l, r, t);
687  return UT_Vector4T<float>((float*) &result);
688 }
689 #endif
690 
691 
692 template <typename T, typename S>
694 {
695  return rowVecMult(v, m);
696 }
697 template <typename T>
698 inline T distance(const UT_Vector4T<T> &v1, const UT_Vector4T<T> &v2)
699 {
700  T x = v1.x()-v2.x();
701  T y = v1.y()-v2.y();
702  T z = v1.z()-v2.z();
703  T w = v1.w()-v2.w();
704  return SYSsqrt(x*x + y*y + z*z + w*w);
705 }
706 template <typename T>
708 {
709  T x = v1.x()-v2.x();
710  T y = v1.y()-v2.y();
711  T z = v1.z()-v2.z();
712  return SYSsqrt(x*x + y*y + z*z);
713 }
714 
715 #ifndef UT_DISABLE_VECTORIZE_MATRIX
716 template <>
717 inline float distance(const UT_Vector4T<float> &v1, const UT_Vector4T<float> &v2)
718 {
719  const v4uf l(v1.data());
720  const v4uf r(v2.data());
721  v4uf result = l - r;
722  result *= result;
723 
724  return SYSsqrt(result[0] + result[1] + result[2] + result[3]);
725 }
726 template <>
728 {
729  const v4uf l(v1.data());
730  const v4uf r(v2.data());
731  v4uf result = l - r;
732  result *= result;
733 
734  return SYSsqrt(result[0] + result[1] + result[2]);
735 }
736 #endif
737 
738 template <typename T>
739 inline size_t hash_value(const UT_Vector4T<T> &val)
740 {
741  return val.hash();
742 }
743 
744 // Overload for custom formatting of UT_Vector4T<T> with UTformat.
745 template<typename T>
746 UT_API size_t format(char *buffer, size_t buffer_size, const UT_Vector4T<T> &v);
747 
748 
749 template<typename T>
751 {
753  typedef T DataType;
754  static const exint TupleSize = 4;
755  static const bool isVectorType = true;
756 };
757 
758 #endif
SYS_FORCE_INLINE constexpr const T * data() const noexcept
SYS_FORCE_INLINE UT_FixedVector< T, SIZE > operator-() const
UT_API size_t format(char *buffer, size_t buffer_size, const UT_Vector4T< T > &v)
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:609
UT_Vector4T< T > operator/(const UT_Vector4T< T > &v, S scalar)
Definition: UT_Vector4.h:610
class UT_API UT_Vector4T
T & z(void)
Definition: UT_Vector4.h:379
UT_Vector4T< T > SYSlerp(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2, S t)
Componentwise linear interpolation.
Definition: UT_Vector4.h:671
UT_Vector4T< T > SYSmin(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2)
Componentwise min and maximum.
Definition: UT_Vector4.h:647
unsigned hash() const
Compute a hash.
Definition: UT_Vector4.h:396
T operator()(unsigned i) const
Definition: UT_Vector4.h:388
SYS_FORCE_INLINE UT_Vector4T(const UT_FixedVector< S, tuple_size, S_INSTANTIATED > &v)
Arbitrary UT_FixedVector of the same size.
Definition: UT_Vector4.h:217
void multiplyComponents(const UT_Vector4T< T > &v)
Definition: UT_Vector4.h:264
SYS_FORCE_INLINE ThisType & operator=(const ThisType &that)=default
const GLdouble * v
Definition: glcorearb.h:836
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:625
SYS_FORCE_INLINE UT_Vector4T(const UT_Vector4T< S > &v)
Our own type of any given value_type.
Definition: UT_Vector4.h:211
#define SYS_DEPRECATED_HDK_REPLACE(__V__, __R__)
const GLuint GLenum const void * binary
Definition: glcorearb.h:1923
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
void colVecMult(const UT_Matrix4T< S > &m)
Definition: UT_Vector4.h:286
T & x(void)
Definition: UT_Vector2.h:285
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
#define SYSabs(a)
Definition: SYS_Math.h:1369
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:75
#define UT_API
Definition: UT_API.h:13
GLint y
Definition: glcorearb.h:102
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
int isEqual(const UT_Vector3T< T > &vect, T tol=0.00001f) const
Definition: UT_Vector4.h:505
static const exint TupleSize
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:817
SYS_FORCE_INLINE T & x(void)
Definition: UT_Vector3.h:498
size_t hash_value(const UT_Vector4T< T > &val)
Definition: UT_Vector4.h:739
3D Vector class.
4D Vector class.
Definition: UT_Vector4.h:155
2D Vector class.
Definition: UT_Vector2.h:138
GLuint buffer
Definition: glcorearb.h:659
png_uint_32 i
Definition: png.h:2877
SYS_FORCE_INLINE UT_Vector4T(const fpreal64 v[tuple_size])
Definition: UT_Vector4.h:193
GLsizeiptr size
Definition: glcorearb.h:663
SYS_FORCE_INLINE UT_Vector4T()=default
void negate3()
Definition: UT_Vector4.h:261
UT_Vector4T< T > SYSmax(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2)
Definition: UT_Vector4.h:659
SYS_FORCE_INLINE T & z(void)
Definition: UT_Vector3.h:502
long long int64
Definition: SYS_Types.h:107
GLfloat f
Definition: glcorearb.h:1925
UT_API UT_Vector4T< T > colVecMult3(const UT_Matrix4T< S > &m, const UT_Vector4T< T > &v)
UT_API UT_Vector4T< T > rowVecMult3(const UT_Vector4T< T > &v, const UT_Matrix4T< S > &m)
void clampZero(T tol=0.00001f)
Definition: UT_Vector4.h:246
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:635
T distance(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2)
Definition: UT_Vector4.h:698
int64 exint
Definition: SYS_Types.h:116
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:125
double fpreal64
Definition: SYS_Types.h:192
static const bool isVectorType
void multiply3(UT_Vector4T< T > &dest, const UT_Matrix4T< S > &mat) const
Definition: UT_Vector4.h:314
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
Definition: VM_SIMD.h:180
SYS_FORCE_INLINE UT_Vector4T(const int64 v[tuple_size])
Definition: UT_Vector4.h:199
UT_API UT_Vector3T< T > cross(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2)
UT_API UT_Vector4T< T > colVecMult(const UT_Matrix4T< S > &m, const UT_Vector4T< T > &v)
T & operator()(unsigned i)
Definition: UT_Vector4.h:383
GLboolean * data
Definition: glcorearb.h:130
T z(void) const
Definition: UT_Vector4.h:380
int int32
Definition: SYS_Types.h:35
T & y(void)
Definition: UT_Vector4.h:377
int findMinAbsAxis() const
These allow you to find out what indices to use for different axes.
Definition: UT_Vector4.h:320
UT_Vector4T< T > & operator*=(const UT_Matrix4T< S > &mat)
Definition: UT_Vector4.h:307
T distance4(const UT_Vector4T< T > &p1, const UT_Vector4T< T > &p2)
Compute the distance between two points.
SYS_FORCE_INLINE UT_Vector4T< T > & operator=(const UT_Vector4T< S > &v)
Definition: UT_Vector4.h:225
void assign(T xx=0.0f, T yy=0.0f, T zz=0.0f, T ww=1.0f)
Set the values of the vector components.
Definition: UT_Vector4.h:401
SYS_FORCE_INLINE T & y(void)
Definition: UT_Vector3.h:500
int findMaxAbsAxis() const
These allow you to find out what indices to use for different axes.
Definition: UT_Vector4.h:345
UT_Vector4T< T > SYSbilerp(const UT_Vector4T< T > &u0v0, const UT_Vector4T< T > &u1v0, const UT_Vector4T< T > &u0v1, const UT_Vector4T< T > &u1v1, S u, S v)
Definition: UT_Vector4.h:102
UT_FixedVector< T, 4 > FixedVectorType
Definition: UT_Vector4.h:752
void rowVecMult(const UT_Matrix4T< S > &m)
Definition: UT_Vector4.h:283
SYS_FORCE_INLINE bool isEqual(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that, S tol=S(SYS_FTOLERANCE)) const
static int entries()
Returns the vector size.
Definition: UT_Vector4.h:447
T w(void) const
Definition: UT_Vector4.h:382
T y(void) const
Definition: UT_Vector4.h:378
png_infop png_bytep png_size_t buffer_size
Definition: png.h:2124
UT_API UT_Vector4T< T > rowVecMult(const UT_Vector4T< T > &v, const UT_Matrix4T< S > &m)
GLint GLenum GLint x
Definition: glcorearb.h:408
GLfloat GLfloat v1
Definition: glcorearb.h:816
GLuint GLfloat * val
Definition: glcorearb.h:1607
void assign(const T *v, int size=tuple_size)
Set the values of the vector components.
Definition: UT_Vector4.h:407
void homogenize(void)
Express the point in homogeneous coordinates or vice-versa.
Definition: UT_Vector4.h:417
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:75
void multiply3(const UT_Matrix4T< S > &mat)
Definition: UT_Vector4.h:311
T & x(void)
Definition: UT_Vector4.h:375
SYS_FORCE_INLINE UT_Vector4T(const fpreal32 v[tuple_size])
Definition: UT_Vector4.h:190
T & y(void)
Definition: UT_Vector2.h:287
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
T distance3d(const UT_Vector4T< T > &p1, const UT_Vector4T< T > &p2)
Compute the distance between two points.
Definition: UT_Vector4.h:143
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
T dot(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2)
The dot product between two vectors.
Definition: UT_Vector4.h:623
SYS_FORCE_INLINE UT_Vector4T(const int32 v[tuple_size])
Definition: UT_Vector4.h:196
GLboolean r
Definition: glcorearb.h:1221
void clampZero3(T tol=0.00001f)
Definition: UT_Vector4.h:254
T & w(void)
Definition: UT_Vector4.h:381
int equalZero3(T tol=0.00001f) const
Definition: UT_Vector4.h:239
T x(void) const
Definition: UT_Vector4.h:376
SYS_FORCE_INLINE UT_Vector4T(T vx, T vy, T vz, T vw=1.0f)
Definition: UT_Vector4.h:183
void rowVecMult3(const UT_Matrix4T< S > &m)
Definition: UT_Vector4.h:296
float fpreal32
Definition: SYS_Types.h:191
void dehomogenize(void)
Express the point in homogeneous coordinates or vice-versa.
Definition: UT_Vector4.h:423
T distance3(const UT_Vector4T< T > &p1, const UT_Vector4T< T > &p2)
Compute the distance between two points.
Definition: UT_Vector4.h:707
UT_FixedVector< T, 4, true > Base
Definition: UT_Vector4.h:158