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 
41 #include <boost/functional/hash.hpp>
42 
43 #include <cstddef>
44 #include <cmath>
45 
46 #include <iosfwd>
47 
48 PXR_NAMESPACE_OPEN_SCOPE
49 
50 class GfVec3f;
51 
52 template <>
53 struct GfIsGfVec<class GfVec3f> { static const bool value = true; };
54 
63 class GfVec3f
64 {
65 public:
67  typedef float ScalarType;
68  static const size_t dimension = 3;
69 
71  GfVec3f() = default;
72 
74  constexpr explicit GfVec3f(float value)
75  : _data{ value, value, value }
76  {
77  }
78 
80  constexpr GfVec3f(float s0, float s1, float s2)
81  : _data{ s0, s1, s2 }
82  {
83  }
84 
86  template <class Scl>
87  constexpr explicit GfVec3f(Scl const *p)
88  : _data{ p[0], p[1], p[2] }
89  {
90  }
91 
93  explicit GfVec3f(class GfVec3d const &other);
94 
96  GfVec3f(class GfVec3h const &other);
97 
99  GfVec3f(class GfVec3i const &other);
100 
102  static GfVec3f XAxis() {
103  GfVec3f result(0);
104  result[0] = 1;
105  return result;
106  }
108  static GfVec3f YAxis() {
109  GfVec3f result(0);
110  result[1] = 1;
111  return result;
112  }
114  static GfVec3f ZAxis() {
115  GfVec3f result(0);
116  result[2] = 1;
117  return result;
118  }
119 
122  static GfVec3f Axis(size_t i) {
123  GfVec3f result(0);
124  if (i < 3)
125  result[i] = 1;
126  return result;
127  }
128 
130  GfVec3f &Set(float s0, float s1, float s2) {
131  _data[0] = s0;
132  _data[1] = s1;
133  _data[2] = s2;
134  return *this;
135  }
136 
138  GfVec3f &Set(float const *a) {
139  return Set(a[0], a[1], a[2]);
140  }
141 
143  float const *data() const { return _data; }
144  float *data() { return _data; }
145  float const *GetArray() const { return data(); }
146 
148  float const &operator[](size_t i) const { return _data[i]; }
149  float &operator[](size_t i) { return _data[i]; }
150 
152  friend inline size_t hash_value(GfVec3f const &vec) {
153  size_t h = 0;
154  boost::hash_combine(h, vec[0]);
155  boost::hash_combine(h, vec[1]);
156  boost::hash_combine(h, vec[2]);
157  return h;
158  }
159 
161  bool operator==(GfVec3f const &other) const {
162  return _data[0] == other[0] &&
163  _data[1] == other[1] &&
164  _data[2] == other[2];
165  }
166  bool operator!=(GfVec3f const &other) const {
167  return !(*this == other);
168  }
169 
170  // TODO Add inequality for other vec types...
172  GF_API
173  bool operator==(class GfVec3d const &other) const;
175  GF_API
176  bool operator==(class GfVec3h const &other) const;
178  GF_API
179  bool operator==(class GfVec3i const &other) const;
180 
182  GfVec3f operator-() const {
183  return GfVec3f(-_data[0], -_data[1], -_data[2]);
184  }
185 
187  GfVec3f &operator+=(GfVec3f const &other) {
188  _data[0] += other[0];
189  _data[1] += other[1];
190  _data[2] += other[2];
191  return *this;
192  }
193  friend GfVec3f operator+(GfVec3f const &l, GfVec3f const &r) {
194  return GfVec3f(l) += r;
195  }
196 
198  GfVec3f &operator-=(GfVec3f const &other) {
199  _data[0] -= other[0];
200  _data[1] -= other[1];
201  _data[2] -= other[2];
202  return *this;
203  }
204  friend GfVec3f operator-(GfVec3f const &l, GfVec3f const &r) {
205  return GfVec3f(l) -= r;
206  }
207 
209  GfVec3f &operator*=(double s) {
210  _data[0] *= s;
211  _data[1] *= s;
212  _data[2] *= s;
213  return *this;
214  }
215  GfVec3f operator*(double s) const {
216  return GfVec3f(*this) *= s;
217  }
218  friend GfVec3f operator*(double s, GfVec3f const &v) {
219  return v * s;
220  }
221 
223  // TODO should divide by the scalar type.
224  GfVec3f &operator/=(double s) {
225  // TODO This should not multiply by 1/s, it should do the division.
226  // Doing the division is more numerically stable when s is close to
227  // zero.
228  return *this *= (1.0 / s);
229  }
230  GfVec3f operator/(double s) const {
231  return *this * (1.0 / s);
232  }
233 
235  float operator*(GfVec3f const &v) const {
236  return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
237  }
238 
243  GfVec3f GetProjection(GfVec3f const &v) const {
244  return v * (*this * v);
245  }
246 
252  GfVec3f GetComplement(GfVec3f const &b) const {
253  return *this - this->GetProjection(b);
254  }
255 
257  float GetLengthSq() const {
258  return *this * *this;
259  }
260 
262  float GetLength() const {
263  return GfSqrt(GetLengthSq());
264  }
265 
274  float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
275  // TODO this seems suspect... suggest dividing by length so long as
276  // length is not zero.
277  float length = GetLength();
278  *this /= (length > eps) ? length : eps;
279  return length;
280  }
281 
282  GfVec3f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
283  GfVec3f normalized(*this);
284  normalized.Normalize(eps);
285  return normalized;
286  }
287 
297  GF_API
298  static bool OrthogonalizeBasis(
299  GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
300  const bool normalize,
301  double eps = GF_MIN_ORTHO_TOLERANCE);
302 
307  GF_API
308  void BuildOrthonormalFrame(GfVec3f *v1, GfVec3f *v2,
309  float eps = GF_MIN_VECTOR_LENGTH) const;
310 
311 
312 private:
313  float _data[3];
314 };
315 
318 GF_API std::ostream& operator<<(std::ostream &, GfVec3f const &);
319 
320 
321 PXR_NAMESPACE_CLOSE_SCOPE
322 
323 #include "pxr/base/gf/vec3d.h"
324 #include "pxr/base/gf/vec3h.h"
325 #include "pxr/base/gf/vec3i.h"
326 
327 PXR_NAMESPACE_OPEN_SCOPE
328 
329 inline
330 GfVec3f::GfVec3f(class GfVec3d const &other)
331 {
332  _data[0] = other[0];
333  _data[1] = other[1];
334  _data[2] = other[2];
335 }
336 inline
337 GfVec3f::GfVec3f(class GfVec3h const &other)
338 {
339  _data[0] = other[0];
340  _data[1] = other[1];
341  _data[2] = other[2];
342 }
343 inline
344 GfVec3f::GfVec3f(class GfVec3i const &other)
345 {
346  _data[0] = other[0];
347  _data[1] = other[1];
348  _data[2] = other[2];
349 }
350 
352 inline GfVec3f
353 GfCompMult(GfVec3f const &v1, GfVec3f const &v2) {
354  return GfVec3f(
355  v1[0] * v2[0],
356  v1[1] * v2[1],
357  v1[2] * v2[2]
358  );
359 }
360 
362 inline GfVec3f
363 GfCompDiv(GfVec3f const &v1, GfVec3f const &v2) {
364  return GfVec3f(
365  v1[0] / v2[0],
366  v1[1] / v2[1],
367  v1[2] / v2[2]
368  );
369 }
370 
372 inline float
373 GfDot(GfVec3f const &v1, GfVec3f const &v2) {
374  return v1 * v2;
375 }
376 
377 
379 inline float
381 {
382  return v.GetLength();
383 }
384 
388 inline float
390 {
391  return v->Normalize(eps);
392 }
393 
397 inline GfVec3f
399 {
400  return v.GetNormalized(eps);
401 }
402 
407 inline GfVec3f
408 GfGetProjection(GfVec3f const &a, GfVec3f const &b)
409 {
410  return a.GetProjection(b);
411 }
412 
417 inline GfVec3f
418 GfGetComplement(GfVec3f const &a, GfVec3f const &b)
419 {
420  return a.GetComplement(b);
421 }
422 
425 inline bool
426 GfIsClose(GfVec3f const &v1, GfVec3f const &v2, double tolerance)
427 {
428  GfVec3f delta = v1 - v2;
429  return delta.GetLengthSq() <= tolerance * tolerance;
430 }
431 
432 
433 GF_API bool
434 GfOrthogonalizeBasis(GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
435  bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
436 
437 GF_API void
438 GfBuildOrthonormalFrame(GfVec3f const &v0,
439  GfVec3f* v1,
440  GfVec3f* v2,
441  float eps = GF_MIN_VECTOR_LENGTH);
442 
444 inline GfVec3f
445 GfCross(GfVec3f const &v1, GfVec3f const &v2)
446 {
447  return GfVec3f(
448  v1[1] * v2[2] - v1[2] * v2[1],
449  v1[2] * v2[0] - v1[0] * v2[2],
450  v1[0] * v2[1] - v1[1] * v2[0]);
451 }
452 
455 inline GfVec3f
456 operator^(GfVec3f const &v1, GfVec3f const &v2)
457 {
458  return GfCross(v1, v2);
459 }
460 
462 GF_API GfVec3f
463 GfSlerp(double alpha, GfVec3f const &v0, GfVec3f const &v1);
464 
465 
466 
467 PXR_NAMESPACE_CLOSE_SCOPE
468 
469 #endif // PXR_BASE_GF_VEC3F_H
static GfVec3f ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3f.h:114
GfVec3f & operator-=(GfVec3f const &other)
Subtraction.
Definition: vec3f.h:198
GfVec3f operator-() const
Create a vec with negated elements.
Definition: vec3f.h:182
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:398
friend size_t hash_value(GfVec3f const &vec)
Hash.
Definition: vec3f.h:152
GfVec3f GfGetComplement(GfVec3f const &a, GfVec3f const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec3f.h:418
Basic type for a vector of 3 float components.
Definition: vec3f.h:63
GfVec3f operator^(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition: vec3f.h:456
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:187
GfVec3f & Set(float const *a)
Set all elements with a pointer to data.
Definition: vec3f.h:138
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:426
GfVec3f GfGetProjection(GfVec3f const &a, GfVec3f const &b)
Returns the projection of a onto b.
Definition: vec3f.h:408
static GfVec3f Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec3f.h:122
float const & operator[](size_t i) const
Indexing.
Definition: vec3f.h:148
GfVec3f & Set(float s0, float s1, float s2)
Set all elements with passed arguments.
Definition: vec3f.h:130
Basic type for a vector of 3 int components.
Definition: vec3i.h:61
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:108
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:80
static GfVec3f XAxis()
Create a unit vector along the X-axis.
Definition: vec3f.h:102
constexpr GfVec3f(float value)
Initialize all elements to a single value.
Definition: vec3f.h:74
float GetLengthSq() const
Squared length.
Definition: vec3f.h:257
float GfGetLength(GfVec3f const &v)
Returns the geometric length of v.
Definition: vec3f.h:380
constexpr GfVec3f(Scl const *p)
Construct with pointer to values.
Definition: vec3f.h:87
GfVec3f GfCompMult(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec3f.h:353
float GetLength() const
Length.
Definition: vec3f.h:262
float GfDot(GfVec3f const &v1, GfVec3f const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec3f.h:373
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:389
Basic type for a vector of 3 double components.
Definition: vec3d.h:63
Defines useful mathematical limits.
GfVec3f GfCompDiv(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec3f.h:363
GfVec3f & operator *=(double s)
Multiplication by scalar.
Definition: vec3f.h:209
GfVec3f & operator/=(double s)
Division by scalar.
Definition: vec3f.h:224
GfVec3f GfCross(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition: vec3f.h:445
GfVec3f GetProjection(GfVec3f const &v) const
Returns the projection of this onto v.
Definition: vec3f.h:243
bool operator==(GfVec3f const &other) const
Equality comparison.
Definition: vec3f.h:161
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec3f.h:274
float ScalarType
Scalar element type and dimension.
Definition: vec3f.h:67
float const * data() const
Direct data access.
Definition: vec3f.h:143
Basic type for a vector of 3 GfHalf components.
Definition: vec3h.h:64
GfVec3f GetComplement(GfVec3f const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec3f.h:252
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