Loading...
Searching...
No Matches
vec3f.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//
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_VEC3F_H
12#define PXR_BASE_GF_VEC3F_H
13
16
17#include "pxr/pxr.h"
18#include "pxr/base/gf/api.h"
19#include "pxr/base/gf/limits.h"
20#include "pxr/base/gf/traits.h"
21#include "pxr/base/gf/math.h"
23#include "pxr/base/tf/hash.h"
24
25#include <cstddef>
26#include <type_traits>
27#include <cmath>
28
29#include <iosfwd>
30
31PXR_NAMESPACE_OPEN_SCOPE
32
33class GfVec2f;
34
35template <>
36struct GfIsGfVec<class GfVec3f> { static const bool value = true; };
37
47{
48public:
50 typedef float ScalarType;
51 static const size_t dimension = 3;
52
64 float x, y, z;
66
69 GfVec3f() = default;
70
72 constexpr explicit GfVec3f(float value)
73 : x(value), y(value), z(value)
74 {
75 }
76
78 constexpr GfVec3f(float s0, float s1, float s2)
79 : x(s0), y(s1), z(s2)
80 {
81 }
82
84 template <class Scl>
85 constexpr explicit GfVec3f(Scl const *p)
86 : x(p[0]), y(p[1]), z(p[2])
87 {
88 }
89
91 explicit GfVec3f(class GfVec3d const &other);
92
94 GfVec3f(class GfVec3h const &other);
95
97 GfVec3f(class GfVec3i const &other);
99 static GfVec3f XAxis() {
100 GfVec3f result(0);
101 result[0] = 1;
102 return result;
103 }
104
106 static GfVec3f YAxis() {
107 GfVec3f result(0);
108 result[1] = 1;
109 return result;
110 }
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 x = s0;
131 y = s1;
132 z = s2;
133 return *this;
134 }
135
137 GfVec3f &Set(float const *a) {
138 return Set(a[0], a[1], a[2]);
139 }
140
146 float const *data() const { return &x; }
147 float *data() { return &x; }
148 float const *GetArray() const { return data(); }
149
151 float const &operator[](size_t i) const { return data()[i]; }
152 float &operator[](size_t i) { return data()[i]; }
153
155 friend inline size_t hash_value(GfVec3f const &vec) {
156 return TfHash::Combine(vec.x, vec.y, vec.z);
157 }
158
160 bool operator==(GfVec3f const &other) const {
161 return x == other.x &&
162 y == other.y &&
163 z == other.z;
164 }
165 bool operator!=(GfVec3f const &other) const {
166 return !(*this == other);
167 }
168
169 // TODO Add inequality for other vec types...
171 GF_API
172 bool operator==(class GfVec3d const &other) const;
174 GF_API
175 bool operator==(class GfVec3h const &other) const;
177 GF_API
178 bool operator==(class GfVec3i const &other) const;
179
182 return GfVec3f(-x, -y, -z);
183 }
184
186 GfVec3f &operator+=(GfVec3f const &other) {
187 x += other.x;
188 y += other.y;
189 z += other.z;
190 return *this;
191 }
192 friend GfVec3f operator+(GfVec3f const &l, GfVec3f const &r) {
193 return GfVec3f(l) += r;
194 }
195
197 GfVec3f &operator-=(GfVec3f const &other) {
198 x -= other.x;
199 y -= other.y;
200 z -= other.z;
201 return *this;
202 }
203 friend GfVec3f operator-(GfVec3f const &l, GfVec3f const &r) {
204 return GfVec3f(l) -= r;
205 }
206
208 GfVec3f &operator*=(double s) {
209 x *= s;
210 y *= s;
211 z *= s;
212 return *this;
213 }
214 GfVec3f operator*(double s) const {
215 return GfVec3f(*this) *= s;
216 }
217 friend GfVec3f operator*(double s, GfVec3f const &v) {
218 return v * s;
219 }
220
222 // TODO should divide by the scalar type.
223 GfVec3f &operator/=(double s) {
224 // TODO This should not multiply by 1/s, it should do the division.
225 // Doing the division is more numerically stable when s is close to
226 // zero.
227 return *this *= (1.0 / s);
228 }
229 GfVec3f operator/(double s) const {
230 return *this * (1.0 / s);
231 }
232
234 float operator*(GfVec3f const &v) const {
235 return x * v.x + y * v.y + z * v.z;
236 }
237
242 GfVec3f GetProjection(GfVec3f const &v) const {
243 return v * (*this * v);
244 }
245
251 GfVec3f GetComplement(GfVec3f const &b) const {
252 return *this - this->GetProjection(b);
253 }
254
256 float GetLengthSq() const {
257 return *this * *this;
258 }
259
261 float GetLength() const {
262 return GfSqrt(GetLengthSq());
263 }
264
273 float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
274 // TODO this seems suspect... suggest dividing by length so long as
275 // length is not zero.
276 float length = GetLength();
277 *this /= (length > eps) ? length : eps;
278 return length;
279 }
280
281 GfVec3f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
282 GfVec3f normalized(*this);
283 normalized.Normalize(eps);
284 return normalized;
285 }
286
296 GF_API
298 GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
299 const bool normalize,
300 double eps = GF_MIN_ORTHO_TOLERANCE);
301
306 GF_API
308 float eps = GF_MIN_VECTOR_LENGTH) const;
309
310
326#if defined (doxygen)
327 constexpr GfVec2f xy() const;
328 constexpr GfVec2f xz() const;
329 constexpr GfVec2f yx() const;
330 constexpr GfVec2f yz() const;
331 constexpr GfVec2f zx() const;
332 constexpr GfVec2f zy() const;
333 constexpr GfVec3f xyz() const;
334 constexpr GfVec3f xzy() const;
335 constexpr GfVec3f yxz() const;
336 constexpr GfVec3f yzx() const;
337 constexpr GfVec3f zxy() const;
338 constexpr GfVec3f zyx() const;
340#else // !doxygen
341 // Note that these are templated on their own return type so that the
342 // declarations cost nothing in compile time & debug info unless they are
343 // actually called in a TU. Naming a different type is an error and
344 // static_assert()ed against.
345 template <class Vec = GfVec2f> constexpr Vec xy() const;
346 template <class Vec = GfVec2f> constexpr Vec xz() const;
347 template <class Vec = GfVec2f> constexpr Vec yx() const;
348 template <class Vec = GfVec2f> constexpr Vec yz() const;
349 template <class Vec = GfVec2f> constexpr Vec zx() const;
350 template <class Vec = GfVec2f> constexpr Vec zy() const;
351 template <class Vec = GfVec3f> constexpr Vec xyz() const;
352 template <class Vec = GfVec3f> constexpr Vec xzy() const;
353 template <class Vec = GfVec3f> constexpr Vec yxz() const;
354 template <class Vec = GfVec3f> constexpr Vec yzx() const;
355 template <class Vec = GfVec3f> constexpr Vec zxy() const;
356 template <class Vec = GfVec3f> constexpr Vec zyx() const;
357#endif // doxygen
358 };
359
362GF_API std::ostream& operator<<(std::ostream &, GfVec3f const &);
363
364PXR_NAMESPACE_CLOSE_SCOPE
365
366// Other scalar types of the same dimension, for the conversions above.
367#include "pxr/base/gf/vec3d.h"
368#include "pxr/base/gf/vec3h.h"
369#include "pxr/base/gf/vec3i.h"
370// Lower dimensions of the same scalar type, for the swizzles above. Note that
371// lower dimensions never include higher ones, so this introduces no cycle.
372#include "pxr/base/gf/vec2f.h"
373
374PXR_NAMESPACE_OPEN_SCOPE
375
376inline
377GfVec3f::GfVec3f(class GfVec3d const &other)
378{
379 x = other.x;
380 y = other.y;
381 z = other.z;
382}
383inline
384GfVec3f::GfVec3f(class GfVec3h const &other)
385{
386 x = other.x;
387 y = other.y;
388 z = other.z;
389}
390inline
391GfVec3f::GfVec3f(class GfVec3i const &other)
392{
393 x = other.x;
394 y = other.y;
395 z = other.z;
396}
397
398template <class Vec>
399constexpr Vec
400GfVec3f::xy() const
401{
402 static_assert(std::is_same<Vec, GfVec2f>::value);
403 return Vec(x, y);
404}
405
406template <class Vec>
407constexpr Vec
408GfVec3f::xz() const
409{
410 static_assert(std::is_same<Vec, GfVec2f>::value);
411 return Vec(x, z);
412}
413
414template <class Vec>
415constexpr Vec
416GfVec3f::yx() const
417{
418 static_assert(std::is_same<Vec, GfVec2f>::value);
419 return Vec(y, x);
420}
421
422template <class Vec>
423constexpr Vec
424GfVec3f::yz() const
425{
426 static_assert(std::is_same<Vec, GfVec2f>::value);
427 return Vec(y, z);
428}
429
430template <class Vec>
431constexpr Vec
432GfVec3f::zx() const
433{
434 static_assert(std::is_same<Vec, GfVec2f>::value);
435 return Vec(z, x);
436}
437
438template <class Vec>
439constexpr Vec
440GfVec3f::zy() const
441{
442 static_assert(std::is_same<Vec, GfVec2f>::value);
443 return Vec(z, y);
444}
445
446template <class Vec>
447constexpr Vec
448GfVec3f::xyz() const
449{
450 static_assert(std::is_same<Vec, GfVec3f>::value);
451 return Vec(x, y, z);
452}
453
454template <class Vec>
455constexpr Vec
456GfVec3f::xzy() const
457{
458 static_assert(std::is_same<Vec, GfVec3f>::value);
459 return Vec(x, z, y);
460}
461
462template <class Vec>
463constexpr Vec
464GfVec3f::yxz() const
465{
466 static_assert(std::is_same<Vec, GfVec3f>::value);
467 return Vec(y, x, z);
468}
469
470template <class Vec>
471constexpr Vec
472GfVec3f::yzx() const
473{
474 static_assert(std::is_same<Vec, GfVec3f>::value);
475 return Vec(y, z, x);
476}
477
478template <class Vec>
479constexpr Vec
480GfVec3f::zxy() const
481{
482 static_assert(std::is_same<Vec, GfVec3f>::value);
483 return Vec(z, x, y);
484}
485
486template <class Vec>
487constexpr Vec
488GfVec3f::zyx() const
489{
490 static_assert(std::is_same<Vec, GfVec3f>::value);
491 return Vec(z, y, x);
492}
493
494
496inline GfVec3f
497GfCompMult(GfVec3f const &v1, GfVec3f const &v2) {
498 return GfVec3f(
499 v1.x * v2.x,
500 v1.y * v2.y,
501 v1.z * v2.z
502 );
503}
504
506inline GfVec3f
507GfCompDiv(GfVec3f const &v1, GfVec3f const &v2) {
508 return GfVec3f(
509 v1.x / v2.x,
510 v1.y / v2.y,
511 v1.z / v2.z
512 );
513}
514
516inline float
517GfDot(GfVec3f const &v1, GfVec3f const &v2) {
518 return v1 * v2;
519}
520
521
523inline float
525{
526 return v.GetLength();
527}
528
532inline float
534{
535 return v->Normalize(eps);
536}
537
541inline GfVec3f
543{
544 return v.GetNormalized(eps);
545}
546
551inline GfVec3f
552GfGetProjection(GfVec3f const &a, GfVec3f const &b)
553{
554 return a.GetProjection(b);
555}
556
561inline GfVec3f
562GfGetComplement(GfVec3f const &a, GfVec3f const &b)
563{
564 return a.GetComplement(b);
565}
566
569inline bool
570GfIsClose(GfVec3f const &v1, GfVec3f const &v2, double tolerance)
571{
572 GfVec3f delta = v1 - v2;
573 return delta.GetLengthSq() <= tolerance * tolerance;
574}
575
576
577GF_API bool
578GfOrthogonalizeBasis(GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
579 bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
580
581GF_API void
582GfBuildOrthonormalFrame(GfVec3f const &v0,
583 GfVec3f* v1,
584 GfVec3f* v2,
585 float eps = GF_MIN_VECTOR_LENGTH);
586
588inline GfVec3f
589GfCross(GfVec3f const &v1, GfVec3f const &v2)
590{
591 return GfVec3f(
592 v1.y * v2.z - v1.z * v2.y,
593 v1.z * v2.x - v1.x * v2.z,
594 v1.x * v2.y - v1.y * v2.x);
595}
596
599inline GfVec3f
600operator^(GfVec3f const &v1, GfVec3f const &v2)
601{
602 return GfCross(v1, v2);
603}
604
606GF_API GfVec3f
607GfSlerp(double alpha, GfVec3f const &v0, GfVec3f const &v1);
608
609
610
611PXR_NAMESPACE_CLOSE_SCOPE
612
613#endif // PXR_BASE_GF_VEC3F_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
Basic type for a vector of 2 float components.
Definition vec2f.h:46
Basic type for a vector of 3 double components.
Definition vec3d.h:47
Basic type for a vector of 3 float components.
Definition vec3f.h:47
GfVec3f & Set(float s0, float s1, float s2)
Set all elements with passed arguments.
Definition vec3f.h:129
GF_API bool operator==(class GfVec3h const &other) const
Equality comparison.
constexpr GfVec3f(float s0, float s1, float s2)
Initialize all elements with explicit arguments.
Definition vec3f.h:78
GfVec3f & operator-=(GfVec3f const &other)
Subtraction.
Definition vec3f.h:197
static GfVec3f XAxis()
Create a unit vector along the X-axis.
Definition vec3f.h:99
GF_API bool operator==(class GfVec3d const &other) const
Equality comparison.
static GfVec3f Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition vec3f.h:121
GfVec3f()=default
GfVec3f value-initializes to zero and performs no default initialization, like float or double.
constexpr GfVec3f(float value)
Initialize all elements to a single value.
Definition vec3f.h:72
float ScalarType
Scalar element type and dimension.
Definition vec3f.h:50
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.
float GetLength() const
Length.
Definition vec3f.h:261
GfVec3f operator-() const
Create a vec with negated elements.
Definition vec3f.h:181
GfVec3f & operator*=(double s)
Multiplication by scalar.
Definition vec3f.h:208
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition vec3f.h:273
GfVec3f GetProjection(GfVec3f const &v) const
Returns the projection of this onto v.
Definition vec3f.h:242
float const & operator[](size_t i) const
Indexing.
Definition vec3f.h:151
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.
float GetLengthSq() const
Squared length.
Definition vec3f.h:256
friend size_t hash_value(GfVec3f const &vec)
Hash.
Definition vec3f.h:155
float operator*(GfVec3f const &v) const
See GfDot().
Definition vec3f.h:234
static GfVec3f YAxis()
Create a unit vector along the Y-axis.
Definition vec3f.h:106
constexpr GfVec3f(Scl const *p)
Construct with pointer to values.
Definition vec3f.h:85
bool operator==(GfVec3f const &other) const
Equality comparison.
Definition vec3f.h:160
static GfVec3f ZAxis()
Create a unit vector along the Z-axis.
Definition vec3f.h:113
GfVec3f & operator+=(GfVec3f const &other)
Addition.
Definition vec3f.h:186
GfVec3f & operator/=(double s)
Division by scalar.
Definition vec3f.h:223
GF_API bool operator==(class GfVec3i const &other) const
Equality comparison.
float const * data() const
Direct data access.
Definition vec3f.h:146
GfVec3f GetComplement(GfVec3f const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition vec3f.h:251
GfVec3f & Set(float const *a)
Set all elements with a pointer to data.
Definition vec3f.h:137
Basic type for a vector of 3 GfHalf components.
Definition vec3h.h:48
Basic type for a vector of 3 int components.
Definition vec3i.h:45
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition hash.h:487
Assorted mathematical utility functions.
double GfSqrt(double f)
Return sqrt(f).
Definition math.h:190
#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:17
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:22
Defines useful mathematical limits.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition traits.h:23
float GfDot(GfVec3f const &v1, GfVec3f const &v2)
Returns the dot (inner) product of two vectors.
Definition vec3f.h:517
GfVec3f GfGetComplement(GfVec3f const &a, GfVec3f const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition vec3f.h:562
GfVec3f GfCross(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition vec3f.h:589
GF_API GfVec3f GfSlerp(double alpha, GfVec3f const &v0, GfVec3f const &v1)
Spherical linear interpolation in three dimensions.
GfVec3f GfGetProjection(GfVec3f const &a, GfVec3f const &b)
Returns the projection of a onto b.
Definition vec3f.h:552
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:570
GfVec3f GfCompDiv(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition vec3f.h:507
float GfGetLength(GfVec3f const &v)
Returns the geometric length of v.
Definition vec3f.h:524
GfVec3f GfCompMult(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition vec3f.h:497
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:533
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:542
GfVec3f operator^(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition vec3f.h:600