HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Vector3.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 3.
10  *
11  * WARNING:
12  * This class should NOT contain any virtual methods, nor should it
13  * define more member data. The size of UT_Vector3 must always be
14  * 12 bytes (3 floats).
15  */
16 
17 #pragma once
18 
19 #ifndef __UT_Vector3_h__
20 #define __UT_Vector3_h__
21 
22 #include "UT_API.h"
23 #include "UT_Assert.h"
24 #include "UT_FixedVector.h"
25 #include "UT_VectorTypes.h"
26 #include "UT_Vector2.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 class UT_IStream;
34 class UT_JSONWriter;
35 class UT_JSONValue;
36 class UT_JSONParser;
37 
38 // Free floating functions:
39 
40 // Right-multiply operators (M*v) have been removed. They had previously
41 // been defined to return v*M, which was too counterintuitive. Once HDK
42 // etc. users have a chance to update their code (post 7.0) we could
43 // reintroduce a right-multiply operator that does a colVecMult.
44 
45 template <typename T, typename S>
46 inline UT_Vector3T<T> operator*(const UT_Vector3T<T> &v, const UT_Matrix3T<S> &m);
47 template <typename T, typename S>
48 inline UT_Vector3T<T> operator*(const UT_Vector3T<T> &v, const UT_Matrix4T<S> &m);
49 template <typename T>
51 template <typename T>
53 template <typename T, typename S>
54 inline UT_Vector3T<T> operator+(const UT_Vector3T<T> &v, S scalar);
55 template <typename T, typename S>
56 inline UT_Vector3T<T> operator-(const UT_Vector3T<T> &v, S scalar);
57 template <typename T, typename S>
58 inline UT_Vector3T<T> operator*(const UT_Vector3T<T> &v, S scalar);
59 template <typename T, typename S>
60 inline UT_Vector3T<T> operator/(const UT_Vector3T<T> &v, S scalar);
61 template <typename T, typename S>
62 inline UT_Vector3T<T> operator+(S scalar, const UT_Vector3T<T> &v);
63 template <typename T, typename S>
64 inline UT_Vector3T<T> operator-(S scalar, const UT_Vector3T<T> &v);
65 template <typename T, typename S>
66 inline UT_Vector3T<T> operator*(S scalar, const UT_Vector3T<T> &v);
67 template <typename T, typename S>
68 inline UT_Vector3T<T> operator/(S scalar, const UT_Vector3T<T> &v);
69 
70 /// The dot and cross products between two vectors (see operator*() too)
71 // @{
72 template <typename T>
73 inline T dot(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2);
74 template <typename T>
75 inline UT_Vector3T<T> cross(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2);
76 // @}
77 
78 /// The angle between two vectors in radians
79 // @{
80 template <typename T>
82 // @}
83 
84 /// Componentwise min and maximum
85 template <typename T>
87 template <typename T>
89 
90 /// Componentwise linear interpolation
91 template <typename T,typename S>
92 inline UT_Vector3T<T> SYSlerp(const UT_Vector3T<T> &v1, const UT_Vector3T<T> &v2, S t);
93 template <typename T,typename S>
94 inline UT_Vector3T<T> SYSbilerp(const UT_Vector3T<T> &u0v0, const UT_Vector3T<T> &u1v0,
95  const UT_Vector3T<T> &u0v1, const UT_Vector3T<T> &u1v1,
96  S u, S v)
97 { return SYSlerp(SYSlerp(u0v0, u0v1, v), SYSlerp(u1v0, u1v1, v), u); }
98 
99 /// Trilinear hat function over kernel widths in s.
100 template <typename T>
101 inline T SYStrihat(const UT_Vector3T<T> &v, const UT_Vector3T<T> &s);
102 
103 /// Gradient of trilinear hat function over kernel widths in s.
104 template <typename T>
106 
107 /// The orthogonal projection of a vector u onto a vector v
108 template <typename T>
109 inline UT_Vector3T<T> project(const UT_Vector3T<T> &u, const UT_Vector3T<T> &v);
110 
111 // TODO: make UT_Vector4 versions of these:
112 
113 /// Compute the distance between two points
114 template <typename T>
115 inline T distance3d(const UT_Vector3T<T> &p1, const UT_Vector3T<T> &p2);
116 /// Compute the distance squared
117 template <typename T>
118 inline T distance2(const UT_Vector3T<T> &p1, const UT_Vector3T<T> &p2);
119 template <typename T>
120 inline T segmentPointDist2(const UT_Vector3T<T> &pos,
121  const UT_Vector3T<T> &pt1,
122  const UT_Vector3T<T> &pt2 );
123 
124 /// Intersect the lines p1 + v1 * t1 and p2 + v2 * t2.
125 /// t1 and t2 are set so that the lines intersect when
126 /// projected to the plane defined by the two lines.
127 /// This function returns a value which indicates how close
128 /// to being parallel the lines are. Closer to zero means
129 /// more parallel. This is done so that the user of this
130 /// function can decide what epsilon they want to use.
131 template <typename T>
132 UT_API double intersectLines(const UT_Vector3T<T> &p1,
133  const UT_Vector3T<T> &v1,
134  const UT_Vector3T<T> &p2,
135  const UT_Vector3T<T> &v2,
136  T &t1, T &t2);
137 
138 /// Returns true if the segments from p0 to p1 and from a to b intersect, and
139 /// t will contain the parametric value of the intersection on the segment a-b.
140 /// Otherwise returns false. Parallel segments will return false. T is
141 /// guaranteed to be between 0.0 and 1.0 if this function returns true.
142 template <typename T>
144  const UT_Vector3T<T> &p1,
145  const UT_Vector3T<T> &a,
146  const UT_Vector3T<T> &b, T &t);
147 
148 /// Returns the U coordinates of the closest points on each of the two
149 /// parallel line segments
150 template <typename T>
152  const UT_Vector3T<T> &p1,
153  const UT_Vector3T<T> &a,
154  const UT_Vector3T<T> &b);
155 /// Returns the U coordinates of the closest points on each of the two
156 /// line segments
157 template <typename T>
159  const UT_Vector3T<T> &p1,
160  const UT_Vector3T<T> &a,
161  const UT_Vector3T<T> &b);
162 /// Returns the U coordinate of the point on line segment p0->p1
163 /// that is closest to a.
164 template <typename T>
166  const UT_Vector3T<T> &p0,
167  const UT_Vector3T<T> &p1,
168  const UT_Vector3T<T> &a);
169 
170 /// Returns the squared distance between two line segments: p0-p1 and a-b
171 template <typename T>
172 inline T segmentDistance2(const UT_Vector3T<T> &p0,
173  const UT_Vector3T<T> &p1,
174  const UT_Vector3T<T> &a,
175  const UT_Vector3T<T> &b);
176 /// Returns the distance between two line segments: p0-p1 and a-b
177 template <typename T>
178 inline T segmentDistance(const UT_Vector3T<T> &p0,
179  const UT_Vector3T<T> &p1,
180  const UT_Vector3T<T> &a,
181  const UT_Vector3T<T> &b);
182 /// 3D Vector class.
183 template <typename T>
184 class UT_API UT_Vector3T : public UT_FixedVector<T,3,true>
185 {
186 public:
188 
189  // These "using" statements are needed for operator=, operator*=, and
190  // distance, so that the ones in UT_FixedVector aren't hidden by the
191  // additional ones here.
192  using Base::operator=;
193  using Base::operator*=;
194  using Base::distance;
195 
196  // These "using" statements are needed for GCC and Clang, because
197  // otherwise, they ignore all members of UT_FixedVector when
198  // checking validity of code in functions in this class.
199  using Base::vec;
200  using Base::data;
201  using Base::normalize;
202  using Base::length2;
203  typedef T value_type;
204  static const int tuple_size = 3;
205 
206  /// Default constructor.
207  /// No data is initialized! Use it for extra speed.
208  SYS_FORCE_INLINE UT_Vector3T() = default;
209 
210  SYS_FORCE_INLINE UT_Vector3T(const UT_Vector3T<T> &that) = default;
211  SYS_FORCE_INLINE UT_Vector3T(UT_Vector3T<T> &&that) = default;
212 
214  {
215  vec[0] = v;
216  vec[1] = v;
217  vec[2] = v;
218  }
219 
221  {
222  vec[0] = vx;
223  vec[1] = vy;
224  vec[2] = vz;
225  }
227  : Base(v)
228  {}
230  : Base(v)
231  {}
233  : Base(v)
234  {}
235  SYS_FORCE_INLINE UT_Vector3T(const int32 v[tuple_size])
236  : Base(v)
237  {}
238  SYS_FORCE_INLINE UT_Vector3T(const int64 v[tuple_size])
239  : Base(v)
240  {}
241 
242  SYS_FORCE_INLINE explicit UT_Vector3T(const UT_Vector2T<T> &v);
243  SYS_FORCE_INLINE explicit UT_Vector3T(const UT_Vector4T<T> &v);
244 
245  /// Our own type of any given value_type.
246  template <typename S>
248  : Base(v)
249  {}
250 
251  /// Arbitrary UT_FixedVector of the same size
252  template <typename S,bool S_INSTANTIATED>
254  : Base(v)
255  {}
256 
259 
260  template <typename S>
262  { vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2]; return *this; }
263 
264  /// Assignment operator that truncates a V4 to a V3.
265  /// TODO: remove this. This should require an explicit UT_Vector3()
266  /// construction, since it's unsafe.
267  SYS_DEPRECATED_HDK_REPLACE(16.0,explicit UT_Vector3 constructor to avoid implicit conversion from UT_Vector4)
269 
270  /// It's unclear why this is needed, given UT_FixedVector::operator-(),
271  /// but the compiler seems not to accept not having it.
273  {
274  return UT_Vector3T<T>(-vec[0], -vec[1], -vec[2]);
275  }
276 
277  void clampZero(T tol = T(0.00001f))
278  {
279  if (vec[0] >= -tol && vec[0] <= tol) vec[0] = 0;
280  if (vec[1] >= -tol && vec[1] <= tol) vec[1] = 0;
281  if (vec[2] >= -tol && vec[2] <= tol) vec[2] = 0;
282  }
283 
286  {
287  vec[0] *= v.vec[0];
288  vec[1] *= v.vec[1];
289  vec[2] *= v.vec[2];
290  }
291 
292  /// If you need a multiplication operator that left multiplies the vector
293  /// by a matrix (M * v), use the following colVecMult() functions. If
294  /// you'd rather not use operator*=() for right-multiplications (v * M),
295  /// use the following rowVecMult() functions. The methods that take a 4x4
296  /// matrix first extend this vector to 4D by adding an element equal to 1.0.
297  /// @internal These are implemented in UT_Matrix3.h and UT_Matrix4.h
298  // @{
299  SYS_FORCE_INLINE void rowVecMult(const UT_Matrix3F &m);
300  SYS_FORCE_INLINE void rowVecMult(const UT_Matrix4F &m);
301  SYS_FORCE_INLINE void rowVecMult(const UT_Matrix3D &m);
302  SYS_FORCE_INLINE void rowVecMult(const UT_Matrix4D &m);
303  SYS_FORCE_INLINE void colVecMult(const UT_Matrix3F &m);
304  SYS_FORCE_INLINE void colVecMult(const UT_Matrix4F &m);
305  SYS_FORCE_INLINE void colVecMult(const UT_Matrix3D &m);
306  SYS_FORCE_INLINE void colVecMult(const UT_Matrix4D &m);
307  // @}
308 
309 
310  /// This multiply will not extend the vector by adding a fourth element.
311  /// Instead, it converts the Matrix4 to a Matrix3. This means that
312  /// the translate component of the matrix is not applied to the vector
313  /// @internal These are implemented in UT_Matrix4.h
314  // @{
319  // @}
320 
321 
322 
323  /// The *=, multiply, multiply3 and multiplyT routines are provided for
324  /// legacy reasons. They all assume that *this is a row vector. Generally,
325  /// the rowVecMult and colVecMult methods are preferred, since they're
326  /// more explicit about the row vector assumption.
327  // @{
328  template <typename S>
330  template <typename S>
332 
333  template <typename S>
334  SYS_FORCE_INLINE void multiply3(const UT_Matrix4T<S> &mat);
335  // @}
336 
337  /// This multiply will multiply the (row) vector by the transpose of the
338  /// matrix instead of the matrix itself. This is faster than
339  /// transposing the matrix, then multiplying (as well there's potentially
340  /// less storage requirements).
341  // @{
342  template <typename S>
343  SYS_FORCE_INLINE void multiplyT(const UT_Matrix3T<S> &mat);
344  template <typename S>
345  SYS_FORCE_INLINE void multiply3T(const UT_Matrix4T<S> &mat);
346  // @}
347 
348  /// The following methods implement multiplies (row) vector by a matrix,
349  /// however, the resulting vector is specified by the dest parameter
350  /// These operations are safe even if "dest" is the same as "this".
351  // @{
352  template <typename S>
353  SYS_FORCE_INLINE void multiply3(UT_Vector3T<T> &dest,
354  const UT_Matrix4T<S> &mat) const;
355  template <typename S>
356  SYS_FORCE_INLINE void multiplyT(UT_Vector3T<T> &dest,
357  const UT_Matrix3T<S> &mat) const;
358  template <typename S>
359  SYS_FORCE_INLINE void multiply3T(UT_Vector3T<T> &dest,
360  const UT_Matrix4T<S> &mat) const;
361  template <typename S>
362  SYS_FORCE_INLINE void multiply(UT_Vector3T<T> &dest,
363  const UT_Matrix4T<S> &mat) const;
364  template <typename S>
365  SYS_FORCE_INLINE void multiply(UT_Vector3T<T> &dest,
366  const UT_Matrix3T<S> &mat) const;
367  // @}
368 
370  {
371  operator=(::cross(*this, v));
372  }
373 
375  {
376  vec[0] += (va.vec[2]+vb.vec[2])*(vb.vec[1]-va.vec[1]);
377  vec[1] += (va.vec[0]+vb.vec[0])*(vb.vec[2]-va.vec[2]);
378  vec[2] += (va.vec[1]+vb.vec[1])*(vb.vec[0]-va.vec[0]);
379  }
380 
381  /// Finds an arbitrary perpendicular to v, and sets this to it.
382  void arbitraryPerp(const UT_Vector3T<T> &v);
383  /// Makes this orthogonal to the given vector. If they are colinear,
384  /// does an arbitrary perp
385  void makeOrthonormal(const UT_Vector3T<T> &v);
386 
387  /// These allow you to find out what indices to use for different axes
388  // @{
389  int findMinAbsAxis() const
390  {
391  T ax = SYSabs(x()), ay = SYSabs(y());
392  if (ax < ay)
393  return (SYSabs(z()) < ax) ? 2 : 0;
394  else
395  return (SYSabs(z()) < ay) ? 2 : 1;
396  }
397  int findMaxAbsAxis() const
398  {
399  T ax = SYSabs(x()), ay = SYSabs(y());
400  if (ax >= ay)
401  return (SYSabs(z()) >= ax) ? 2 : 0;
402  else
403  return (SYSabs(z()) >= ay) ? 2 : 1;
404  }
405  // @}
406 
407  /// Given this vector as the z-axis, get a frame of reference such that the
408  /// X and Y vectors are orthonormal to the vector. This vector should be
409  /// normalized.
411  {
412  if (SYSabs(x()) < 0.6F) Y = UT_Vector3T<T>(1, 0, 0);
413  else if (SYSabs(z()) < 0.6F) Y = UT_Vector3T<T>(0, 1, 0);
414  else Y = UT_Vector3T<T>(0, 0, 1);
415  X = ::cross(Y, *this);
416  X.normalize();
417  Y = ::cross(*this, X);
418  }
419 
420  /// Calculates the orthogonal projection of a vector u on the *this vector
421  UT_Vector3T<T> project(const UT_Vector3T<T> &u) const;
422 
423  /// Create a matrix of projection onto this vector: the matrix transforms
424  /// a vector v into its projection on the direction of (*this) vector,
425  /// ie. dot(*this, v) * this->normalize();
426  /// If we need to be normalized, set norm to non-false.
427  template <typename S>
428  UT_Matrix3T<S> project(bool norm=true);
429 
430  /// Vector p (representing a point in 3-space) and vector v define
431  /// a line. This member returns the projection of "this" onto the
432  /// line (the point on the line that is closest to this point).
433  UT_Vector3T<T> projection(const UT_Vector3T<T> &p,
434  const UT_Vector3T<T> &v) const;
435 
436  /// Projects this onto the line segement [a,b]. The returned point
437  /// will lie between a and b.
438  UT_Vector3T<T> projectOnSegment(const UT_Vector3T<T> &va,
439  const UT_Vector3T<T> &vb) const;
440  /// Projects this onto the line segment [a, b]. The fpreal t is set
441  /// to the parametric position of intersection, a being 0 and b being 1.
442  UT_Vector3T<T> projectOnSegment(const UT_Vector3T<T> &va, const UT_Vector3T<T> &vb,
443  T &t) const;
444 
445  /// Create a matrix of symmetry around this vector: the matrix transforms
446  /// a vector v into its symmetry around (*this), ie. two times the
447  /// projection of v onto (*this) minus v.
448  /// If we need to be normalized, set norm to non-false.
449  UT_Matrix3 symmetry(bool norm=true);
450 
451  /// This method stores in (*this) the intersection between two 3D lines,
452  /// p1+t*v1 and p2+u*v2. If the two lines do not actually intersect, we
453  /// shift the 2nd line along the perpendicular on both lines (along the
454  /// line of min distance) and return the shifted intersection point; this
455  /// point thus lies on the 1st line.
456  /// If we find an intersection point (shifted or not) we return 0; if
457  /// the two lines are parallel we return -1; and if they intersect
458  /// behind our back we return -2. When we return -2 there still is a
459  /// valid intersection point in (*this).
460  int lineIntersect(const UT_Vector3T<T> &p1, const UT_Vector3T<T> &v1,
461  const UT_Vector3T<T> &p2, const UT_Vector3T<T> &v2);
462 
463  /// Compute the intersection of vector p2+t*v2 and the line segment between
464  /// points pa and pb. If the two lines do not intersect we shift the
465  /// (p2, v2) line along the line of min distance and return the point
466  /// where it intersects the segment. If we find an intersection point
467  /// along the stretch between pa and pb, we return 0. If the lines are
468  /// parallel we return -1. If they intersect before pa we return -2, and
469  /// if after pb, we return -3. The intersection point is valid with
470  /// return codes 0,-2,-3.
471  int segLineIntersect(const UT_Vector3T<T> &pa, const UT_Vector3T<T> &pb,
472  const UT_Vector3T<T> &p2, const UT_Vector3T<T> &v2);
473 
474  /// Determines whether or not the points p0, p1 and "this" are collinear.
475  /// If they are t contains the parametric value of where "this" is found
476  /// on the segment from p0 to p1 and returns true. Otherwise returns
477  /// false. If p0 and p1 are equal, t is set to
478  /// std::numeric_limits<T>::max() and true is returned.
479  bool areCollinear(const UT_Vector3T<T> &p0, const UT_Vector3T<T> &p1,
480  T *t = 0, T tol = 1e-5) const;
481 
482  /// Compute (homogeneous) barycentric co-ordinates of this point
483  /// relative to the triangle defined by t0, t1 and t2. (The point is
484  /// projected into the triangle's plane.)
485  UT_Vector3T<T> getBary(const UT_Vector3T<T> &t0, const UT_Vector3T<T> &t1,
486  const UT_Vector3T<T> &t2, bool *degen = NULL) const;
487 
488 
489  /// Compute the signed distance from us to a line.
490  T distance(const UT_Vector3T<T> &p1, const UT_Vector3T<T> &v1) const;
491  /// Compute the signed distance between two lines.
492  T distance(const UT_Vector3T<T> &p1, const UT_Vector3T<T> &v1,
493  const UT_Vector3T<T> &p2, const UT_Vector3T<T> &v2) const;
494 
495  /// Return the components of the vector. The () operator does NOT check
496  /// for the boundary condition.
497  /// @{
498  SYS_FORCE_INLINE T &x(void) { return vec[0]; }
499  SYS_FORCE_INLINE T x(void) const { return vec[0]; }
500  SYS_FORCE_INLINE T &y(void) { return vec[1]; }
501  SYS_FORCE_INLINE T y(void) const { return vec[1]; }
502  SYS_FORCE_INLINE T &z(void) { return vec[2]; }
503  SYS_FORCE_INLINE T z(void) const { return vec[2]; }
504  SYS_FORCE_INLINE T &r(void) { return vec[0]; }
505  SYS_FORCE_INLINE T r(void) const { return vec[0]; }
506  SYS_FORCE_INLINE T &g(void) { return vec[1]; }
507  SYS_FORCE_INLINE T g(void) const { return vec[1]; }
508  SYS_FORCE_INLINE T &b(void) { return vec[2]; }
509  SYS_FORCE_INLINE T b(void) const { return vec[2]; }
511  {
512  UT_ASSERT_P(i < tuple_size);
513  return vec[i];
514  }
515  SYS_FORCE_INLINE T operator()(unsigned i) const
516  {
517  UT_ASSERT_P(i < tuple_size);
518  return vec[i];
519  }
520  /// @}
521 
522  /// Compute a hash
523  unsigned hash() const { return SYSvector_hash(data(), tuple_size); }
524 
525  // TODO: eliminate these methods. They're redundant, given good inline
526  // constructors.
527  /// Set the values of the vector components
528  void assign(T xx = 0.0f, T yy = 0.0f, T zz = 0.0f)
529  {
530  vec[0] = xx; vec[1] = yy; vec[2] = zz;
531  }
532  /// Set the values of the vector components
533  void assign(const T *v)
534  {
535  vec[0]=v[0]; vec[1]=v[1]; vec[2]=v[2];
536  }
537 
538  /// Express the point in homogeneous coordinates or vice-versa
539  // @{
540  void homogenize(void)
541  {
542  vec[0] *= vec[2];
543  vec[1] *= vec[2];
544  }
545  void dehomogenize(void)
546  {
547  if (vec[2] != 0)
548  {
549  T denom = 1.0f / vec[2];
550  vec[0] *= denom;
551  vec[1] *= denom;
552  }
553  }
554  // @}
555 
556  /// assuming that "this" is a rotation (in radians, of course), the
557  /// equivalent set of rotations which are closest to the "base" rotation
558  /// are found. The equivalent rotations are the same as the original
559  /// rotations +2*n*PI
560  void roundAngles(const UT_Vector3T<T> &base);
561 
562  /// conversion between degrees and radians
563  // @{
564  void degToRad();
565  void radToDeg();
566  // @}
567 
568  /// It seems that given any rotation matrix and transform order,
569  /// there are two distinct triples of rotations that will result in
570  /// the same overall rotation. This method will find the closest of
571  /// the two after finding the closest using the above method.
572  void roundAngles(const UT_Vector3T<T> &b, const UT_XformOrder &o);
573 
574  /// Return the dual of the vector
575  /// The dual is a matrix which acts like the cross product when
576  /// multiplied by other vectors.
577  /// The following are equivalent:
578  /// a.getDual(A); c = colVecMult(A, b)
579  /// c = cross(a, b)
580  template <typename S>
581  void getDual(UT_Matrix3T<S> &dual) const;
582 
583  /// Protected I/O methods
584  // @{
585  void save(std::ostream &os, bool binary = false) const;
586  bool load(UT_IStream &is);
587  // @}
588 
589  /// @{
590  /// Methods to serialize to a JSON stream. The vector is stored as an
591  /// array of 3 reals.
592  bool save(UT_JSONWriter &w) const;
593  bool save(UT_JSONValue &v) const;
594  bool load(UT_JSONParser &p);
595  /// @}
596 
597  /// @{
598  /// Method to return the angle (in radians) between this and another vector
600  {
601  return ::UTangleBetween(*this, v);
602  }
603  /// @}
604 
605  /// Returns the vector size
606  static int entries() { return tuple_size; }
607 
608 private:
609  /// I/O friends
610  // @{
611  friend std::ostream &operator<<(std::ostream &os, const UT_Vector3T<T> &v)
612  {
613  v.save(os);
614  return os;
615  }
616  // @}
617 };
618 
619 // Required for constructor
620 #include "UT_Vector2.h"
621 #include "UT_Vector4.h"
622 
623 template <typename T>
625 {
626  vec[0] = v.x();
627  vec[1] = v.y();
628  vec[2] = T(0);
629 }
630 template <typename T>
632 {
633  vec[0] = v.x();
634  vec[1] = v.y();
635  vec[2] = v.z();
636 }
637 
638 // Free floating functions:
639 template <typename T>
640 inline
642 {
643  return UT_Vector3T<T>(v1.x()+v2.x(), v1.y()+v2.y(), v1.z()+v2.z());
644 }
645 
646 template <typename T>
647 inline
649 {
650  return UT_Vector3T<T>(v1.x()-v2.x(), v1.y()-v2.y(), v1.z()-v2.z());
651 }
652 
653 template <typename T, typename S>
654 inline
656 {
657  return UT_Vector3T<T>(v.x()+scalar, v.y()+scalar, v.z()+scalar);
658 }
659 
660 template <typename T>
661 inline
663 {
664  return UT_Vector3T<T>(v1.x()*v2.x(), v1.y()*v2.y(), v1.z()*v2.z());
665 }
666 
667 template <typename T>
668 inline
670 {
671  return UT_Vector3T<T>(v1.x()/v2.x(), v1.y()/v2.y(), v1.z()/v2.z());
672 }
673 
674 template <typename T, typename S>
675 inline
677 {
678  return UT_Vector3T<T>(v.x()+scalar, v.y()+scalar, v.z()+scalar);
679 }
680 
681 template <typename T, typename S>
682 inline
684 {
685  return UT_Vector3T<T>(v.x()-scalar, v.y()-scalar, v.z()-scalar);
686 }
687 
688 template <typename T, typename S>
689 inline
691 {
692  return UT_Vector3T<T>(scalar-v.x(), scalar-v.y(), scalar-v.z());
693 }
694 
695 template <typename T, typename S>
696 inline
698 {
699  return UT_Vector3T<T>(v.x()*scalar, v.y()*scalar, v.z()*scalar);
700 }
701 
702 template <typename T, typename S>
703 inline
705 {
706  return UT_Vector3T<T>(v.x()*scalar, v.y()*scalar, v.z()*scalar);
707 }
708 
709 template <typename T, typename S>
710 inline
712 {
714  {
715  // This has to be T because S may be int for "v = v/2" code
716  // For the same reason we must cast the 1
717  T inv = ((T)1) / scalar;
718  return UT_Vector3T<T>(v.x()*inv, v.y()*inv, v.z()*inv);
719  }
720  return UT_Vector3T<T>(v.x()/scalar, v.y()/scalar, v.z()/scalar);
721 }
722 
723 template <typename T, typename S>
724 inline
726 {
727  return UT_Vector3T<T>(scalar/v.x(), scalar/v.y(), scalar/v.z());
728 }
729 
730 template <typename T>
731 inline
733 {
734  return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z();
735 }
736 
737 template <typename T>
738 inline
740 {
741  // compute the cross product:
742  return UT_Vector3T<T>(
743  v1.y()*v2.z() - v1.z()*v2.y(),
744  v1.z()*v2.x() - v1.x()*v2.z(),
745  v1.x()*v2.y() - v1.y()*v2.x()
746  );
747 }
748 
749 template <typename T>
750 inline
752 {
753  UT_Vector3T<fpreal64> v1crossv2 = cross(v1, v2);
754  fpreal v1dotv2 = dot(v1, v2);
755  return SYSatan2(v1crossv2.length(), v1dotv2);
756 }
757 
758 template <typename T>
759 inline
761 {
762  return UT_Vector3T<T>(
763  SYSmin(v1.x(), v2.x()),
764  SYSmin(v1.y(), v2.y()),
765  SYSmin(v1.z(), v2.z())
766  );
767 }
768 
769 template <typename T>
770 inline
772 {
773  return UT_Vector3T<T>(
774  SYSmax(v1.x(), v2.x()),
775  SYSmax(v1.y(), v2.y()),
776  SYSmax(v1.z(), v2.z())
777  );
778 }
779 
780 template <typename T,typename S>
781 inline
783 {
784  return UT_Vector3T<T>(
785  SYSlerp(v1.x(), v2.x(), t),
786  SYSlerp(v1.y(), v2.y(), t),
787  SYSlerp(v1.z(), v2.z(), t));
788 }
789 
790 template <typename T>
791 inline
793 {
794  return SYShat(v.x(), s.x()) * SYShat(v.y(), s.y()) * SYShat(v.z(), s.z());
795 }
796 
797 template <typename T>
798 inline
800 {
801  const T xhat = SYShat(v.x(), s.x());
802  const T yhat = SYShat(v.y(), s.y());
803  const T zhat = SYShat(v.z(), s.z());
804  return UT_Vector3T<T>(SYSdhat(v.x(), s.x()) * yhat * zhat,
805  xhat * SYSdhat(v.y(), s.y()) * zhat,
806  xhat * yhat * SYSdhat(v.z(), s.z()));
807 }
808 
809 template <typename T>
810 inline
812 {
813  return dot(u, v) / v.length2() * v;
814 }
815 
816 template <typename T>
817 inline
819 {
820  return (v1 - v2).length();
821 }
822 template <typename T>
823 inline
825 {
826  return (v1 - v2).length2();
827 }
828 
829 // calculate distance squared of pos to the line segment defined by pt1 to pt2
830 template <typename T>
831 inline
833  const UT_Vector3T<T> &pt1, const UT_Vector3T<T> &pt2 )
834 {
835  UT_Vector3T<T> vec;
836  T proj_t;
837  T veclen2;
838 
839  vec = pt2 - pt1;
840  proj_t = vec.dot( pos - pt1 );
841  veclen2 = vec.length2();
842 
843  if( proj_t <= (T)0.0 )
844  {
845  // in bottom cap region, calculate distance from pt1
846  vec = pos - pt1;
847  }
848  else if( proj_t >= veclen2 )
849  {
850  // in top cap region, calculate distance from pt2
851  vec = pos - pt2;
852  }
853  else
854  {
855  // middle region, calculate distance from projected pt
856  proj_t /= veclen2;
857  vec = (pt1 + (proj_t * vec)) - pos;
858  }
859 
860  return dot(vec, vec);
861 }
862 
863 // TODO: review the effiency of the following routine, there is a faster
864 // way to get just the distance.
865 template <typename T>
866 inline
868  const UT_Vector3T<T> &q0, const UT_Vector3T<T> &q1)
869 {
870  UT_Vector2 t = segmentClosest(p0, p1, q0, q1);
871  UT_Vector3 a = p0 + (p1 - p0) * t[0];
872  UT_Vector3 b = q0 + (q1 - q0) * t[1];
873  return distance2(a, b);
874 }
875 
876 template <typename T>
877 inline
879  const UT_Vector3T<T> &q0, const UT_Vector3T<T> &q1)
880 {
881  return SYSsqrt(segmentDistance2(p0, p1, q0, q1));
882 }
883 
884 /// Given a 3D position, input, and a 3D parallelpiped with corner p0 and
885 /// directions du, dv, and dw, finds the 0 or 1 locations in the parameter
886 /// space of that parallelpiped that correspond with the input position.
887 /// Only a parameter location approximately between 0 and 1
888 /// is accepted. The return value is the number of accepted parameter locations,
889 /// i.e. 0 or 1.
890 template <typename T>
892  const UT_Vector3T<T> &p0,
893  const UT_Vector3T<T> &du, const UT_Vector3T<T> &dv, const UT_Vector3T<T> &dw,
894  UT_Vector3T<T> &output)
895 {
896  const UT_Vector3T<T> orig = input - p0;
897 
898  const UT_Matrix3T<T> matrix(
899  du.x(), dv.x(), dw.x(),
900  du.y(), dv.y(), dw.y(),
901  du.z(), dv.z(), dw.z()
902  );
903 
904  bool failed = matrix.solve(orig.x(), orig.y(), orig.z(), output);
905  return !failed &&
906  SYSisGreaterOrEqual(output.x(), 0) && SYSisLessOrEqual(output.x(), 1) &&
907  SYSisGreaterOrEqual(output.y(), 0) && SYSisLessOrEqual(output.y(), 1) &&
908  SYSisGreaterOrEqual(output.z(), 0) && SYSisLessOrEqual(output.z(), 1);
909 }
910 
911 template <typename T>
912 inline size_t hash_value(const UT_Vector3T<T> &val)
913 {
914  return val.hash();
915 }
916 
917 // Overload for custom formatting of UT_Vector3T<T> with UTformat.
918 template<typename T>
919 UT_API size_t format(char *buffer, size_t buffer_size, const UT_Vector3T<T> &v);
920 
921 
922 template<typename T>
924 {
926  typedef T DataType;
927  static const exint TupleSize = 3;
928  static const bool isVectorType = true;
929 };
930 
931 #endif
UT_Vector3T< T > rowVecMult(const UT_Vector3T< T > &v, const UT_Matrix3T< S > &m)
Definition: UT_Matrix3.h:1295
SYS_FORCE_INLINE UT_Vector3T(const UT_Vector3T< S > &v)
Our own type of any given value_type.
Definition: UT_Vector3.h:247
SYS_FORCE_INLINE T & g(void)
Definition: UT_Vector3.h:506
UT_Vector3T< T > SYSlerp(const UT_Vector3T< T > &v1, const UT_Vector3T< T > &v2, S t)
Componentwise linear interpolation.
Definition: UT_Vector3.h:782
SYS_FORCE_INLINE constexpr const T * data() const noexcept
UT_API double intersectLines(const UT_Vector3T< T > &p1, const UT_Vector3T< T > &v1, const UT_Vector3T< T > &p2, const UT_Vector3T< T > &v2, T &t1, T &t2)
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
T dot(const UT_Vector3T< T > &v1, const UT_Vector3T< T > &v2)
The dot and cross products between two vectors (see operator*() too)
Definition: UT_Vector3.h:732
SYS_FORCE_INLINE void operator*=(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that)
int findMaxAbsAxis() const
These allow you to find out what indices to use for different axes.
Definition: UT_Vector3.h:397
T & z(void)
Definition: UT_Vector4.h:379
UT_Vector3T< T > SYSbilerp(const UT_Vector3T< T > &u0v0, const UT_Vector3T< T > &u1v0, const UT_Vector3T< T > &u0v1, const UT_Vector3T< T > &u1v1, S u, S v)
Definition: UT_Vector3.h:94
T distance2(const UT_Vector3T< T > &p1, const UT_Vector3T< T > &p2)
Compute the distance squared.
Definition: UT_Vector3.h:824
T distance3d(const UT_Vector3T< T > &p1, const UT_Vector3T< T > &p2)
Compute the distance between two points.
Definition: UT_Vector3.h:818
int UTinverseTrilerpFlat(const UT_Vector3T< T > &input, const UT_Vector3T< T > &p0, const UT_Vector3T< T > &du, const UT_Vector3T< T > &dv, const UT_Vector3T< T > &dw, UT_Vector3T< T > &output)
Definition: UT_Vector3.h:891
SYS_FORCE_INLINE ThisType & operator=(const ThisType &that)=default
const GLdouble * v
Definition: glcorearb.h:836
SYS_FORCE_INLINE T operator()(unsigned i) const
Definition: UT_Vector3.h:515
void assign(const T *v)
Set the values of the vector components.
Definition: UT_Vector3.h:533
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
#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
fpreal64 UTangleBetween(const UT_Vector3T< T > &v1, const UT_Vector3T< T > &v2)
The angle between two vectors in radians.
Definition: UT_Vector3.h:751
void homogenize(void)
Express the point in homogeneous coordinates or vice-versa.
Definition: UT_Vector3.h:540
UT_Vector3T< T > SYSmin(const UT_Vector3T< T > &v1, const UT_Vector3T< T > &v2)
Componentwise min and maximum.
Definition: UT_Vector3.h:760
SYS_FORCE_INLINE T x(void) const
Definition: UT_Vector3.h:499
T & x(void)
Definition: UT_Vector2.h:285
T segmentDistance(const UT_Vector3T< T > &p0, const UT_Vector3T< T > &p1, const UT_Vector3T< T > &a, const UT_Vector3T< T > &b)
Returns the distance between two line segments: p0-p1 and a-b.
Definition: UT_Vector3.h:878
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
UT_Vector3T< T > operator/(const UT_Vector3T< T > &v, S scalar)
Definition: UT_Vector3.h:711
#define SYSabs(a)
Definition: SYS_Math.h:1369
unsigned hash() const
Compute a hash.
Definition: UT_Vector3.h:523
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
static const exint TupleSize
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:817
SYS_FORCE_INLINE T & x(void)
Definition: UT_Vector3.h:498
UT_Vector3T< T > SYStrihatgrad(const UT_Vector3T< T > &v, const UT_Vector3T< T > &s)
Gradient of trilinear hat function over kernel widths in s.
Definition: UT_Vector3.h:799
T SYStrihat(const UT_Vector3T< T > &v, const UT_Vector3T< T > &s)
Trilinear hat function over kernel widths in s.
Definition: UT_Vector3.h:792
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
T segmentPointDist2(const UT_Vector3T< T > &pos, const UT_Vector3T< T > &pt1, const UT_Vector3T< T > &pt2)
Definition: UT_Vector3.h:832
SYS_FORCE_INLINE T & z(void)
Definition: UT_Vector3.h:502
void getFrameOfReference(UT_Vector3T< T > &X, UT_Vector3T< T > &Y) const
Definition: UT_Vector3.h:410
long long int64
Definition: SYS_Types.h:107
SYS_FORCE_INLINE T & operator()(unsigned i)
Definition: UT_Vector3.h:510
SYS_FORCE_INLINE T g(void) const
Definition: UT_Vector3.h:507
SYS_FORCE_INLINE UT_Vector3T(const fpreal32 v[tuple_size])
Definition: UT_Vector3.h:229
GLfloat f
Definition: glcorearb.h:1925
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
UT_API UT_Vector2T< T > segmentClosest(const UT_Vector3T< T > &p0, const UT_Vector3T< T > &p1, const UT_Vector3T< T > &a, const UT_Vector3T< T > &b)
T distance(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2)
Definition: UT_Vector4.h:698
void clampZero(T tol=T(0.00001f))
Definition: UT_Vector3.h:277
int64 exint
Definition: SYS_Types.h:116
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:125
double fpreal64
Definition: SYS_Types.h:192
size_t hash_value(const UT_Vector3T< T > &val)
Definition: UT_Vector3.h:912
static const bool isVectorType
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
SYS_FORCE_INLINE UT_Vector3T(const int64 v[tuple_size])
Definition: UT_Vector3.h:238
static int entries()
Returns the vector size.
Definition: UT_Vector3.h:606
SYS_FORCE_INLINE UT_StorageAtLeast32Bit< T, T >::Type length2() const noexcept
UT_Vector3T< T > SYSmax(const UT_Vector3T< T > &v1, const UT_Vector3T< T > &v2)
Definition: UT_Vector3.h:771
class UT_API UT_Vector3T
UT_API UT_Vector2T< T > segmentClosestParallel(const UT_Vector3T< T > &p0, const UT_Vector3T< T > &p1, const UT_Vector3T< T > &a, const UT_Vector3T< T > &b)
SYS_FORCE_INLINE T b(void) const
Definition: UT_Vector3.h:509
T segmentDistance2(const UT_Vector3T< T > &p0, const UT_Vector3T< T > &p1, const UT_Vector3T< T > &a, const UT_Vector3T< T > &b)
Returns the squared distance between two line segments: p0-p1 and a-b.
Definition: UT_Vector3.h:867
GLboolean * data
Definition: glcorearb.h:130
int int32
Definition: SYS_Types.h:35
SYS_FORCE_INLINE UT_Vector3T(T v)
Definition: UT_Vector3.h:213
T & y(void)
Definition: UT_Vector4.h:377
GridType::Ptr normalize(const GridType &grid, bool threaded, InterruptT *interrupt)
Normalize the vectors of the given vector-valued grid.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
UT_Vector3T< T > colVecMult3(const UT_Matrix4T< S > &m, const UT_Vector3T< T > &v)
Definition: UT_Matrix4.h:1812
SYS_FORCE_INLINE UT_StorageNum< typename UT_StorageBetter< T, S >::Type >::MathFloat distance(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const
int solve(T cx, T cy, T cz, UT_Vector3T< S > &result) const
SYS_FORCE_INLINE UT_Vector3T(const fpreal64 v[tuple_size])
Definition: UT_Vector3.h:232
SYS_FORCE_INLINE T & b(void)
Definition: UT_Vector3.h:508
SYS_FORCE_INLINE UT_Vector3T< T > operator-() const
Definition: UT_Vector3.h:272
UT_FixedVector< T, 3, true > Base
Definition: UT_Vector3.h:187
SYS_FORCE_INLINE UT_StorageAtLeast32Bit< T, S >::Type dot(const UT_FixedVector< S, SIZE, S_INSTANTIATED > &that) const
SYS_FORCE_INLINE UT_Vector3T(const int32 v[tuple_size])
Definition: UT_Vector3.h:235
void assign(T xx=0.0f, T yy=0.0f, T zz=0.0f)
Set the values of the vector components.
Definition: UT_Vector3.h:528
SYS_FORCE_INLINE UT_Vector3T< T > & operator=(const UT_Vector3T< S > &v)
Definition: UT_Vector3.h:261
SYS_FORCE_INLINE T & y(void)
Definition: UT_Vector3.h:500
double fpreal
Definition: SYS_Types.h:270
void dehomogenize(void)
Express the point in homogeneous coordinates or vice-versa.
Definition: UT_Vector3.h:545
UT_API bool intersectSegments(const UT_Vector3T< T > &p0, const UT_Vector3T< T > &p1, const UT_Vector3T< T > &a, const UT_Vector3T< T > &b, T &t)
SYS_FORCE_INLINE T z(void) const
Definition: UT_Vector3.h:503
UT_Vector3T< T > rowVecMult3(const UT_Vector3T< T > &v, const UT_Matrix4T< S > &m)
Definition: UT_Matrix4.h:1790
SYS_FORCE_INLINE UT_Vector3T(T vx, T vy, T vz)
Definition: UT_Vector3.h:220
UT_FixedVector< T, 3 > FixedVectorType
Definition: UT_Vector3.h:925
SYS_FORCE_INLINE T y(void) const
Definition: UT_Vector3.h:501
png_infop png_bytep png_size_t buffer_size
Definition: png.h:2124
GLint GLenum GLint x
Definition: glcorearb.h:408
UT_Vector3T< T > colVecMult(const UT_Matrix3T< S > &m, const UT_Vector3T< T > &v)
Definition: UT_Matrix3.h:1313
GLfloat GLfloat v1
Definition: glcorearb.h:816
SYS_FORCE_INLINE T r(void) const
Definition: UT_Vector3.h:505
GLuint GLfloat * val
Definition: glcorearb.h:1607
UT_Vector3T< T > cross(const UT_Vector3T< T > &v1, const UT_Vector3T< T > &v2)
The dot and cross products between two vectors (see operator*() too)
Definition: UT_Vector3.h:739
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:75
SYS_FORCE_INLINE Storage::MathFloat normalize()
T & x(void)
Definition: UT_Vector4.h:375
T & y(void)
Definition: UT_Vector2.h:287
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
SYS_FORCE_INLINE UT_Vector3T()=default
UT_Vector3T< T > project(const UT_Vector3T< T > &u, const UT_Vector3T< T > &v)
The orthogonal projection of a vector u onto a vector v.
Definition: UT_Vector3.h:811
SYS_FORCE_INLINE UT_Vector3T(const UT_FixedVector< S, tuple_size, S_INSTANTIATED > &v)
Arbitrary UT_FixedVector of the same size.
Definition: UT_Vector3.h:253
SYS_FORCE_INLINE void normal(const UT_Vector3T< T > &va, const UT_Vector3T< T > &vb)
Definition: UT_Vector3.h:374
SYS_FORCE_INLINE void multiplyComponents(const UT_Vector3T< T > &v)
Definition: UT_Vector3.h:285
SYS_FORCE_INLINE Storage::MathFloat length() const
SYS_FORCE_INLINE T & r(void)
Definition: UT_Vector3.h:504
SYS_FORCE_INLINE UT_Vector3T(const fpreal16 v[tuple_size])
Definition: UT_Vector3.h:226
float fpreal32
Definition: SYS_Types.h:191
UT_API size_t format(char *buffer, size_t buffer_size, const UT_Vector3T< T > &v)
fpreal64 angleTo(const UT_Vector3T< T > &v) const
Definition: UT_Vector3.h:599
SYS_FORCE_INLINE void cross(const UT_Vector3T< T > &v)
Definition: UT_Vector3.h:369
int findMinAbsAxis() const
These allow you to find out what indices to use for different axes.
Definition: UT_Vector3.h:389