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 //
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 
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 
47 PXR_NAMESPACE_OPEN_SCOPE
48 
49 class GfVec3f;
50 
51 template <>
52 struct GfIsGfVec<class GfVec3f> { static const bool value = true; };
53 
62 class GfVec3f
63 {
64 public:
66  typedef float ScalarType;
67  static const size_t dimension = 3;
68 
70  GfVec3f() = default;
71 
73  constexpr explicit GfVec3f(float value)
74  : _data{ value, value, value }
75  {
76  }
77 
79  constexpr GfVec3f(float s0, float s1, float s2)
80  : _data{ s0, s1, s2 }
81  {
82  }
83 
85  template <class Scl>
86  constexpr explicit GfVec3f(Scl const *p)
87  : _data{ p[0], p[1], p[2] }
88  {
89  }
90 
92  explicit GfVec3f(class GfVec3d const &other);
93 
95  GfVec3f(class GfVec3h const &other);
96 
98  GfVec3f(class GfVec3i const &other);
99 
101  static GfVec3f XAxis() {
102  GfVec3f result(0);
103  result[0] = 1;
104  return result;
105  }
107  static GfVec3f YAxis() {
108  GfVec3f result(0);
109  result[1] = 1;
110  return result;
111  }
113  static GfVec3f ZAxis() {
114  GfVec3f result(0);
115  result[2] = 1;
116  return result;
117  }
118 
121  static GfVec3f Axis(size_t i) {
122  GfVec3f result(0);
123  if (i < 3)
124  result[i] = 1;
125  return result;
126  }
127 
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 
137  GfVec3f &Set(float const *a) {
138  return Set(a[0], a[1], a[2]);
139  }
140 
142  float const *data() const { return _data; }
143  float *data() { return _data; }
144  float const *GetArray() const { return data(); }
145 
147  float const &operator[](size_t i) const { return _data[i]; }
148  float &operator[](size_t i) { return _data[i]; }
149 
151  friend inline size_t hash_value(GfVec3f const &vec) {
152  return TfHash::Combine(vec[0], vec[1], vec[2]);
153  }
154 
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...
167  GF_API
168  bool operator==(class GfVec3d const &other) const;
170  GF_API
171  bool operator==(class GfVec3h const &other) const;
173  GF_API
174  bool operator==(class GfVec3i const &other) const;
175 
177  GfVec3f operator-() const {
178  return GfVec3f(-_data[0], -_data[1], -_data[2]);
179  }
180 
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 
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 
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 
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 
230  float operator*(GfVec3f const &v) const {
231  return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
232  }
233 
238  GfVec3f GetProjection(GfVec3f const &v) const {
239  return v * (*this * v);
240  }
241 
247  GfVec3f GetComplement(GfVec3f const &b) const {
248  return *this - this->GetProjection(b);
249  }
250 
252  float GetLengthSq() const {
253  return *this * *this;
254  }
255 
257  float GetLength() const {
258  return GfSqrt(GetLengthSq());
259  }
260 
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 
277  GfVec3f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
278  GfVec3f normalized(*this);
279  normalized.Normalize(eps);
280  return normalized;
281  }
282 
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 
302  GF_API
303  void BuildOrthonormalFrame(GfVec3f *v1, GfVec3f *v2,
304  float eps = GF_MIN_VECTOR_LENGTH) const;
305 
306 
307 private:
308  float _data[3];
309 };
310 
313 GF_API std::ostream& operator<<(std::ostream &, GfVec3f const &);
314 
315 
316 PXR_NAMESPACE_CLOSE_SCOPE
317 
318 #include "pxr/base/gf/vec3d.h"
319 #include "pxr/base/gf/vec3h.h"
320 #include "pxr/base/gf/vec3i.h"
321 
322 PXR_NAMESPACE_OPEN_SCOPE
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 
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 
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 
367 inline float
368 GfDot(GfVec3f const &v1, GfVec3f const &v2) {
369  return v1 * v2;
370 }
371 
372 
374 inline float
376 {
377  return v.GetLength();
378 }
379 
383 inline float
385 {
386  return v->Normalize(eps);
387 }
388 
392 inline GfVec3f
394 {
395  return v.GetNormalized(eps);
396 }
397 
402 inline GfVec3f
403 GfGetProjection(GfVec3f const &a, GfVec3f const &b)
404 {
405  return a.GetProjection(b);
406 }
407 
412 inline GfVec3f
413 GfGetComplement(GfVec3f const &a, GfVec3f const &b)
414 {
415  return a.GetComplement(b);
416 }
417 
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
429 GfOrthogonalizeBasis(GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
430  bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
431 
432 GF_API void
433 GfBuildOrthonormalFrame(GfVec3f const &v0,
434  GfVec3f* v1,
435  GfVec3f* v2,
436  float eps = GF_MIN_VECTOR_LENGTH);
437 
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 
450 inline GfVec3f
451 operator^(GfVec3f const &v1, GfVec3f const &v2)
452 {
453  return GfCross(v1, v2);
454 }
455 
457 GF_API GfVec3f
458 GfSlerp(double alpha, GfVec3f const &v0, GfVec3f const &v1);
459 
460 
461 
462 PXR_NAMESPACE_CLOSE_SCOPE
463 
464 #endif // PXR_BASE_GF_VEC3F_H
static GfVec3f ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3f.h:113
GfVec3f & operator-=(GfVec3f const &other)
Subtraction.
Definition: vec3f.h:193
GfVec3f operator-() const
Create a vec with negated elements.
Definition: vec3f.h:177
static GF_API bool OrthogonalizeBasis(GfVec3f *tx, GfVec3f *ty, GfVec3f *tz, const bool normalize, double eps=GF_MIN_ORTHO_TOLERANCE)
Orthogonalize and optionally normalize a set of basis vectors.
Assorted mathematical utility functions.
GfVec3f GfGetNormalized(GfVec3f const &v, float eps=GF_MIN_VECTOR_LENGTH)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec3f.h:393
friend size_t hash_value(GfVec3f const &vec)
Hash.
Definition: vec3f.h:151
GfVec3f GfGetComplement(GfVec3f const &a, GfVec3f const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec3f.h:413
Basic type for a vector of 3 float components.
Definition: vec3f.h:62
GfVec3f operator^(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition: vec3f.h:451
Low-level utilities for informing users of various internal and external diagnostic conditions.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition: traits.h:36
GfVec3f()=default
Default constructor does no initialization.
GF_API std::ostream & operator<<(std::ostream &, GfVec3f const &)
Output a GfVec3f.
#define GF_MIN_ORTHO_TOLERANCE
This constant is used to determine when a set of basis vectors is close to orthogonal.
Definition: limits.h:39
GfVec3f & operator+=(GfVec3f const &other)
Addition.
Definition: vec3f.h:182
GfVec3f & Set(float const *a)
Set all elements with a pointer to data.
Definition: vec3f.h:137
bool GfIsClose(GfVec3f const &v1, GfVec3f const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition: vec3f.h:421
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
GfVec3f GfGetProjection(GfVec3f const &a, GfVec3f const &b)
Returns the projection of a onto b.
Definition: vec3f.h:403
static GfVec3f Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec3f.h:121
float const & operator[](size_t i) const
Indexing.
Definition: vec3f.h:147
GfVec3f & Set(float s0, float s1, float s2)
Set all elements with passed arguments.
Definition: vec3f.h:129
Basic type for a vector of 3 int components.
Definition: vec3i.h:60
GF_API GfVec3f GfSlerp(double alpha, GfVec3f const &v0, GfVec3f const &v1)
Spherical linear interpolation in three dimensions.
static GfVec3f YAxis()
Create a unit vector along the Y-axis.
Definition: vec3f.h:107
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:80
constexpr GfVec3f(float s0, float s1, float s2)
Initialize all elements with explicit arguments.
Definition: vec3f.h:79
static GfVec3f XAxis()
Create a unit vector along the X-axis.
Definition: vec3f.h:101
constexpr GfVec3f(float value)
Initialize all elements to a single value.
Definition: vec3f.h:73
float GetLengthSq() const
Squared length.
Definition: vec3f.h:252
float GfGetLength(GfVec3f const &v)
Returns the geometric length of v.
Definition: vec3f.h:375
constexpr GfVec3f(Scl const *p)
Construct with pointer to values.
Definition: vec3f.h:86
GfVec3f GfCompMult(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec3f.h:348
float GetLength() const
Length.
Definition: vec3f.h:257
float GfDot(GfVec3f const &v1, GfVec3f const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec3f.h:368
float GfNormalize(GfVec3f *v, float eps=GF_MIN_VECTOR_LENGTH)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec3f.h:384
Basic type for a vector of 3 double components.
Definition: vec3d.h:62
Defines useful mathematical limits.
GfVec3f GfCompDiv(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec3f.h:358
GfVec3f & operator *=(double s)
Multiplication by scalar.
Definition: vec3f.h:204
GfVec3f & operator/=(double s)
Division by scalar.
Definition: vec3f.h:219
GfVec3f GfCross(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition: vec3f.h:440
GfVec3f GetProjection(GfVec3f const &v) const
Returns the projection of this onto v.
Definition: vec3f.h:238
bool operator==(GfVec3f const &other) const
Equality comparison.
Definition: vec3f.h:156
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec3f.h:269
float ScalarType
Scalar element type and dimension.
Definition: vec3f.h:66
float const * data() const
Direct data access.
Definition: vec3f.h:142
Basic type for a vector of 3 GfHalf components.
Definition: vec3h.h:63
GfVec3f GetComplement(GfVec3f const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec3f.h:247
GF_API void BuildOrthonormalFrame(GfVec3f *v1, GfVec3f *v2, float eps=GF_MIN_VECTOR_LENGTH) const
Sets v1 and v2 to unit vectors such that v1, v2 and *this are mutually orthogonal.
#define GF_MIN_VECTOR_LENGTH
This constant is used to determine whether the length of a vector is too small to handle accurately.
Definition: limits.h:34