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