HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vec3h.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 ////////////////////////////////////////////////////////////////////////
8 // This file is generated by a script. Do not edit directly. Edit the
9 // vec.template.h file to make changes.
10 
11 #ifndef PXR_BASE_GF_VEC3H_H
12 #define PXR_BASE_GF_VEC3H_H
13 
14 /// \file gf/vec3h.h
15 /// \ingroup group_gf_LinearAlgebra
16 
17 #include "pxr/pxr.h"
18 #include "pxr/base/tf/diagnostic.h"
19 #include "pxr/base/gf/api.h"
20 #include "pxr/base/gf/limits.h"
21 #include "pxr/base/gf/traits.h"
22 #include "pxr/base/gf/math.h"
23 #include "pxr/base/gf/half.h"
24 #include "pxr/base/tf/hash.h"
25 
26 #include <cstddef>
27 #include <cmath>
28 
29 #include <iosfwd>
30 
32 
33 class GfVec3h;
34 
35 template <>
36 struct GfIsGfVec<class GfVec3h> { static const bool value = true; };
37 
38 /// \class GfVec3h
39 /// \ingroup group_gf_LinearAlgebra
40 ///
41 /// Basic type for a vector of 3 GfHalf components.
42 ///
43 /// Represents a vector of 3 components of type \c GfHalf.
44 /// It is intended to be fast and simple.
45 ///
46 class GfVec3h
47 {
48 public:
49  /// Scalar element type and dimension.
50  typedef GfHalf ScalarType;
51  static const size_t dimension = 3;
52 
53  /// Default constructor does no initialization.
54  GfVec3h() = default;
55 
56  /// Initialize all elements to a single value.
57  constexpr explicit GfVec3h(GfHalf value)
58  : _data{ value, value, value }
59  {
60  }
61 
62  /// Initialize all elements with explicit arguments.
63  constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
64  : _data{ s0, s1, s2 }
65  {
66  }
67 
68  /// Construct with pointer to values.
69  template <class Scl>
70  constexpr explicit GfVec3h(Scl const *p)
71  : _data{ p[0], p[1], p[2] }
72  {
73  }
74 
75  /// Construct from GfVec3d.
76  explicit GfVec3h(class GfVec3d const &other);
77 
78  /// Construct from GfVec3f.
79  explicit GfVec3h(class GfVec3f const &other);
80 
81  /// Implicitly convert from GfVec3i.
82  GfVec3h(class GfVec3i const &other);
83 
84  /// Create a unit vector along the X-axis.
85  static GfVec3h XAxis() {
86  GfVec3h result(0);
87  result[0] = 1;
88  return result;
89  }
90  /// Create a unit vector along the Y-axis.
91  static GfVec3h YAxis() {
92  GfVec3h result(0);
93  result[1] = 1;
94  return result;
95  }
96  /// Create a unit vector along the Z-axis.
97  static GfVec3h ZAxis() {
98  GfVec3h result(0);
99  result[2] = 1;
100  return result;
101  }
102 
103  /// Create a unit vector along the i-th axis, zero-based. Return the zero
104  /// vector if \p i is greater than or equal to 3.
105  static GfVec3h Axis(size_t i) {
106  GfVec3h result(0);
107  if (i < 3)
108  result[i] = 1;
109  return result;
110  }
111 
112  /// Set all elements with passed arguments.
113  GfVec3h &Set(GfHalf s0, GfHalf s1, GfHalf s2) {
114  _data[0] = s0;
115  _data[1] = s1;
116  _data[2] = s2;
117  return *this;
118  }
119 
120  /// Set all elements with a pointer to data.
121  GfVec3h &Set(GfHalf const *a) {
122  return Set(a[0], a[1], a[2]);
123  }
124 
125  /// Direct data access.
126  GfHalf const *data() const { return _data; }
127  GfHalf *data() { return _data; }
128  GfHalf const *GetArray() const { return data(); }
129 
130  /// Indexing.
131  GfHalf const &operator[](size_t i) const { return _data[i]; }
132  GfHalf &operator[](size_t i) { return _data[i]; }
133 
134  /// Hash.
135  friend inline size_t hash_value(GfVec3h const &vec) {
136  return TfHash::Combine(vec[0], vec[1], vec[2]);
137  }
138 
139  /// Equality comparison.
140  bool operator==(GfVec3h const &other) const {
141  return _data[0] == other[0] &&
142  _data[1] == other[1] &&
143  _data[2] == other[2];
144  }
145  bool operator!=(GfVec3h const &other) const {
146  return !(*this == other);
147  }
148 
149  // TODO Add inequality for other vec types...
150  /// Equality comparison.
151  GF_API
152  bool operator==(class GfVec3d const &other) const;
153  /// Equality comparison.
154  GF_API
155  bool operator==(class GfVec3f const &other) const;
156  /// Equality comparison.
157  GF_API
158  bool operator==(class GfVec3i const &other) const;
159 
160  /// Create a vec with negated elements.
161  GfVec3h operator-() const {
162  return GfVec3h(-_data[0], -_data[1], -_data[2]);
163  }
164 
165  /// Addition.
166  GfVec3h &operator+=(GfVec3h const &other) {
167  _data[0] += other[0];
168  _data[1] += other[1];
169  _data[2] += other[2];
170  return *this;
171  }
172  friend GfVec3h operator+(GfVec3h const &l, GfVec3h const &r) {
173  return GfVec3h(l) += r;
174  }
175 
176  /// Subtraction.
177  GfVec3h &operator-=(GfVec3h const &other) {
178  _data[0] -= other[0];
179  _data[1] -= other[1];
180  _data[2] -= other[2];
181  return *this;
182  }
183  friend GfVec3h operator-(GfVec3h const &l, GfVec3h const &r) {
184  return GfVec3h(l) -= r;
185  }
186 
187  /// Multiplication by scalar.
188  GfVec3h &operator*=(double s) {
189  _data[0] *= s;
190  _data[1] *= s;
191  _data[2] *= s;
192  return *this;
193  }
194  GfVec3h operator*(double s) const {
195  return GfVec3h(*this) *= s;
196  }
197  friend GfVec3h operator*(double s, GfVec3h const &v) {
198  return v * s;
199  }
200 
201  /// Division by scalar.
202  // TODO should divide by the scalar type.
203  GfVec3h &operator/=(double s) {
204  // TODO This should not multiply by 1/s, it should do the division.
205  // Doing the division is more numerically stable when s is close to
206  // zero.
207  return *this *= (1.0 / s);
208  }
209  GfVec3h operator/(double s) const {
210  return *this * (1.0 / s);
211  }
212 
213  /// See GfDot().
214  GfHalf operator*(GfVec3h const &v) const {
215  return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
216  }
217 
218  /// Returns the projection of \p this onto \p v. That is:
219  /// \code
220  /// v * (*this * v)
221  /// \endcode
222  GfVec3h GetProjection(GfVec3h const &v) const {
223  return v * (*this * v);
224  }
225 
226  /// Returns the orthogonal complement of \p this->GetProjection(b).
227  /// That is:
228  /// \code
229  /// *this - this->GetProjection(b)
230  /// \endcode
231  GfVec3h GetComplement(GfVec3h const &b) const {
232  return *this - this->GetProjection(b);
233  }
234 
235  /// Squared length.
236  GfHalf GetLengthSq() const {
237  return *this * *this;
238  }
239 
240  /// Length
241  GfHalf GetLength() const {
242  return GfSqrt(GetLengthSq());
243  }
244 
245  /// Normalizes the vector in place to unit length, returning the
246  /// length before normalization. If the length of the vector is
247  /// smaller than \p eps, then the vector is set to vector/\c eps.
248  /// The original length of the vector is returned. See also GfNormalize().
249  ///
250  /// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
251  /// By tickling the code, it no longer tries to write into
252  /// an illegal memory address (in the code section of memory).
253  GfHalf Normalize(GfHalf eps = 0.001) {
254  // TODO this seems suspect... suggest dividing by length so long as
255  // length is not zero.
256  GfHalf length = GetLength();
257  *this /= (length > eps) ? length : eps;
258  return length;
259  }
260 
261  GfVec3h GetNormalized(GfHalf eps = 0.001) const {
262  GfVec3h normalized(*this);
263  normalized.Normalize(eps);
264  return normalized;
265  }
266 
267  /// Orthogonalize and optionally normalize a set of basis vectors. This
268  /// uses an iterative method that is very stable even when the vectors are
269  /// far from orthogonal (close to colinear). The number of iterations and
270  /// thus the computation time does increase as the vectors become close to
271  /// colinear, however. Returns a bool specifying whether the solution
272  /// converged after a number of iterations. If it did not converge, the
273  /// returned vectors will be as close as possible to orthogonal within the
274  /// iteration limit. Colinear vectors will be unaltered, and the method
275  /// will return false.
276  GF_API
277  static bool OrthogonalizeBasis(
278  GfVec3h *tx, GfVec3h *ty, GfVec3h *tz,
279  const bool normalize,
280  double eps = GF_MIN_ORTHO_TOLERANCE);
281 
282  /// Sets \c v1 and \c v2 to unit vectors such that v1, v2 and *this are
283  /// mutually orthogonal. If the length L of *this is smaller than \c eps,
284  /// then v1 and v2 will have magnitude L/eps. As a result, the function
285  /// delivers a continuous result as *this shrinks in length.
286  GF_API
288  GfHalf eps = 0.001) const;
289 
290 
291 private:
292  GfHalf _data[3];
293 };
294 
295 /// Output a GfVec3h.
296 /// \ingroup group_gf_DebuggingOutput
297 GF_API std::ostream& operator<<(std::ostream &, GfVec3h const &);
298 
299 
301 
302 #include "pxr/base/gf/vec3d.h"
303 #include "pxr/base/gf/vec3f.h"
304 #include "pxr/base/gf/vec3i.h"
305 
307 
308 inline
309 GfVec3h::GfVec3h(class GfVec3d const &other)
310 {
311  _data[0] = other[0];
312  _data[1] = other[1];
313  _data[2] = other[2];
314 }
315 inline
316 GfVec3h::GfVec3h(class GfVec3f const &other)
317 {
318  _data[0] = other[0];
319  _data[1] = other[1];
320  _data[2] = other[2];
321 }
322 inline
323 GfVec3h::GfVec3h(class GfVec3i const &other)
324 {
325  _data[0] = other[0];
326  _data[1] = other[1];
327  _data[2] = other[2];
328 }
329 
330 /// Returns component-wise multiplication of vectors \p v1 and \p v2.
331 inline GfVec3h
332 GfCompMult(GfVec3h const &v1, GfVec3h const &v2) {
333  return GfVec3h(
334  v1[0] * v2[0],
335  v1[1] * v2[1],
336  v1[2] * v2[2]
337  );
338 }
339 
340 /// Returns component-wise quotient of vectors \p v1 and \p v2.
341 inline GfVec3h
342 GfCompDiv(GfVec3h const &v1, GfVec3h const &v2) {
343  return GfVec3h(
344  v1[0] / v2[0],
345  v1[1] / v2[1],
346  v1[2] / v2[2]
347  );
348 }
349 
350 /// Returns the dot (inner) product of two vectors.
351 inline GfHalf
352 GfDot(GfVec3h const &v1, GfVec3h const &v2) {
353  return v1 * v2;
354 }
355 
356 
357 /// Returns the geometric length of \c v.
358 inline GfHalf
360 {
361  return v.GetLength();
362 }
363 
364 /// Normalizes \c *v in place to unit length, returning the length before
365 /// normalization. If the length of \c *v is smaller than \p eps then \c *v is
366 /// set to \c *v/eps. The original length of \c *v is returned.
367 inline GfHalf
368 GfNormalize(GfVec3h *v, GfHalf eps = 0.001)
369 {
370  return v->Normalize(eps);
371 }
372 
373 /// Returns a normalized (unit-length) vector with the same direction as \p v.
374 /// If the length of this vector is smaller than \p eps, the vector divided by
375 /// \p eps is returned.
376 inline GfVec3h
377 GfGetNormalized(GfVec3h const &v, GfHalf eps = 0.001)
378 {
379  return v.GetNormalized(eps);
380 }
381 
382 /// Returns the projection of \p a onto \p b. That is:
383 /// \code
384 /// b * (a * b)
385 /// \endcode
386 inline GfVec3h
388 {
389  return a.GetProjection(b);
390 }
391 
392 /// Returns the orthogonal complement of \p a.GetProjection(b). That is:
393 /// \code
394 /// a - a.GetProjection(b)
395 /// \endcode
396 inline GfVec3h
398 {
399  return a.GetComplement(b);
400 }
401 
402 /// Tests for equality within a given tolerance, returning \c true if the
403 /// length of the difference vector is less than or equal to \p tolerance.
404 inline bool
405 GfIsClose(GfVec3h const &v1, GfVec3h const &v2, double tolerance)
406 {
407  GfVec3h delta = v1 - v2;
408  return delta.GetLengthSq() <= tolerance * tolerance;
409 }
410 
411 
412 GF_API bool
414  bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
415 
416 GF_API void
418  GfVec3h* v1,
419  GfVec3h* v2,
420  GfHalf eps = 0.001);
421 
422 /// Returns the cross product of \p v1 and \p v2.
423 inline GfVec3h
424 GfCross(GfVec3h const &v1, GfVec3h const &v2)
425 {
426  return GfVec3h(
427  v1[1] * v2[2] - v1[2] * v2[1],
428  v1[2] * v2[0] - v1[0] * v2[2],
429  v1[0] * v2[1] - v1[1] * v2[0]);
430 }
431 
432 /// Returns the cross product of \p v1 and \p v2.
433 /// \see GfCross()
434 inline GfVec3h
435 operator^(GfVec3h const &v1, GfVec3h const &v2)
436 {
437  return GfCross(v1, v2);
438 }
439 
440 /// Spherical linear interpolation in three dimensions.
442 GfSlerp(double alpha, GfVec3h const &v0, GfVec3h const &v1);
443 
444 
445 
447 
448 #endif // PXR_BASE_GF_VEC3H_H
GfVec3h GfCross(GfVec3h const &v1, GfVec3h const &v2)
Returns the cross product of v1 and v2.
Definition: vec3h.h:424
GfHalf GfGetLength(GfVec3h const &v)
Returns the geometric length of v.
Definition: vec3h.h:359
GfVec3h operator/(double s) const
Definition: vec3h.h:209
GfHalf & operator[](size_t i)
Definition: vec3h.h:132
double GfSqrt(double f)
Definition: math.h:187
GfHalf GetLength() const
Length.
Definition: vec3h.h:241
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
GfVec3h GetProjection(GfVec3h const &v) const
Definition: vec3h.h:222
static const size_t dimension
Definition: vec3h.h:51
GfHalf const * GetArray() const
Definition: vec3h.h:128
const GLdouble * v
Definition: glcorearb.h:837
GfHalf GetLengthSq() const
Squared length.
Definition: vec3h.h:236
GfHalf * data()
Definition: vec3h.h:127
GfVec3h GetNormalized(GfHalf eps=0.001) const
Definition: vec3h.h:261
GfVec3h operator*(double s) const
Definition: vec3h.h:194
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec3h.h:131
GfHalf Normalize(GfHalf eps=0.001)
Definition: vec3h.h:253
Definition: vec3f.h:45
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GfVec3h & operator*=(double s)
Multiplication by scalar.
Definition: vec3h.h:188
GLdouble s
Definition: glad.h:3009
constexpr GfVec3h(GfHalf value)
Initialize all elements to a single value.
Definition: vec3h.h:57
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
GfHalf GfDot(GfVec3h const &v1, GfVec3h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec3h.h:352
GfHalf ScalarType
Scalar element type and dimension.
Definition: vec3h.h:50
**But if you need a result
Definition: thread.h:622
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
GfVec3h & operator+=(GfVec3h const &other)
Addition.
Definition: vec3h.h:166
GfVec3h & operator-=(GfVec3h const &other)
Subtraction.
Definition: vec3h.h:177
GF_API bool GfOrthogonalizeBasis(GfVec3h *tx, GfVec3h *ty, GfVec3h *tz, bool normalize, double eps=GF_MIN_ORTHO_TOLERANCE)
GfVec3h operator-() const
Create a vec with negated elements.
Definition: vec3h.h:161
static GfVec3h ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3h.h:97
static GfVec3h XAxis()
Create a unit vector along the X-axis.
Definition: vec3h.h:85
constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
Initialize all elements with explicit arguments.
Definition: vec3h.h:63
GF_API void BuildOrthonormalFrame(GfVec3h *v1, GfVec3h *v2, GfHalf eps=0.001) const
GfVec3h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec3h.h:121
GfVec3h GetComplement(GfVec3h const &b) const
Definition: vec3h.h:231
GF_API std::ostream & operator<<(std::ostream &, GfVec3h const &)
GfVec3h GfCompDiv(GfVec3h const &v1, GfVec3h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec3h.h:342
GfVec3h GfGetComplement(GfVec3h const &a, GfVec3h const &b)
Definition: vec3h.h:397
static GfVec3h YAxis()
Create a unit vector along the Y-axis.
Definition: vec3h.h:91
GLfloat GLfloat GLfloat alpha
Definition: glcorearb.h:112
friend GfVec3h operator+(GfVec3h const &l, GfVec3h const &r)
Definition: vec3h.h:172
Definition: vec3i.h:43
GfVec3h & operator/=(double s)
Division by scalar.
Definition: vec3h.h:203
GLint GLenum GLboolean normalized
Definition: glcorearb.h:872
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GfHalf const * data() const
Direct data access.
Definition: vec3h.h:126
GLfloat v0
Definition: glcorearb.h:816
GF_API void GfBuildOrthonormalFrame(GfVec3h const &v0, GfVec3h *v1, GfVec3h *v2, GfHalf eps=0.001)
GF_API GfVec3h GfSlerp(double alpha, GfVec3h const &v0, GfVec3h const &v1)
Spherical linear interpolation in three dimensions.
friend size_t hash_value(GfVec3h const &vec)
Hash.
Definition: vec3h.h:135
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
bool GfIsClose(GfVec3h const &v1, GfVec3h const &v2, double tolerance)
Definition: vec3h.h:405
GfVec3h operator^(GfVec3h const &v1, GfVec3h const &v2)
Definition: vec3h.h:435
Definition: vec3d.h:45
GLfloat GLfloat v1
Definition: glcorearb.h:817
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
FMT_CONSTEXPR basic_fp< F > normalize(basic_fp< F > value)
Definition: format.h:1701
GfVec3h GfGetProjection(GfVec3h const &a, GfVec3h const &b)
Definition: vec3h.h:387
GfHalf GfNormalize(GfVec3h *v, GfHalf eps=0.001)
Definition: vec3h.h:368
GfVec3h GfGetNormalized(GfVec3h const &v, GfHalf eps=0.001)
Definition: vec3h.h:377
bool operator==(GfVec3h const &other) const
Equality comparison.
Definition: vec3h.h:140
constexpr GfVec3h(Scl const *p)
Construct with pointer to values.
Definition: vec3h.h:70
GfVec3h()=default
Default constructor does no initialization.
GLboolean r
Definition: glcorearb.h:1222
friend GfVec3h operator-(GfVec3h const &l, GfVec3h const &r)
Definition: vec3h.h:183
GfHalf operator*(GfVec3h const &v) const
See GfDot().
Definition: vec3h.h:214
static GF_API bool OrthogonalizeBasis(GfVec3h *tx, GfVec3h *ty, GfVec3h *tz, const bool normalize, double eps=GF_MIN_ORTHO_TOLERANCE)
Definition: vec3h.h:46
bool operator!=(GfVec3h const &other) const
Definition: vec3h.h:145
GfVec3h & Set(GfHalf s0, GfHalf s1, GfHalf s2)
Set all elements with passed arguments.
Definition: vec3h.h:113
GfVec3h GfCompMult(GfVec3h const &v1, GfVec3h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec3h.h:332
#define GF_MIN_ORTHO_TOLERANCE
Definition: limits.h:22
friend GfVec3h operator*(double s, GfVec3h const &v)
Definition: vec3h.h:197
#define GF_API
Definition: api.h:23
static GfVec3h Axis(size_t i)
Definition: vec3h.h:105