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