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