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"
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
30PXR_NAMESPACE_OPEN_SCOPE
31
32class GfVec3f;
33
34template <>
35struct GfIsGfVec<class GfVec3f> { static const bool value = true; };
36
46{
47public:
49 typedef float ScalarType;
50 static const size_t dimension = 3;
51
54 GfVec3f() = default;
55
57 constexpr explicit GfVec3f(float value)
58 : _data{ value, value, value }
59 {
60 }
61
63 constexpr GfVec3f(float s0, float s1, float s2)
64 : _data{ s0, s1, s2 }
65 {
66 }
67
69 template <class Scl>
70 constexpr explicit GfVec3f(Scl const *p)
71 : _data{ p[0], p[1], p[2] }
72 {
73 }
74
76 explicit GfVec3f(class GfVec3d const &other);
77
79 GfVec3f(class GfVec3h const &other);
80
82 GfVec3f(class GfVec3i const &other);
83
85 static GfVec3f XAxis() {
86 GfVec3f result(0);
87 result[0] = 1;
88 return result;
89 }
91 static GfVec3f YAxis() {
92 GfVec3f result(0);
93 result[1] = 1;
94 return result;
95 }
97 static GfVec3f ZAxis() {
98 GfVec3f result(0);
99 result[2] = 1;
100 return result;
101 }
102
105 static GfVec3f Axis(size_t i) {
106 GfVec3f result(0);
107 if (i < 3)
108 result[i] = 1;
109 return result;
110 }
111
113 GfVec3f &Set(float s0, float s1, float s2) {
114 _data[0] = s0;
115 _data[1] = s1;
116 _data[2] = s2;
117 return *this;
118 }
119
121 GfVec3f &Set(float const *a) {
122 return Set(a[0], a[1], a[2]);
123 }
124
126 float const *data() const { return _data; }
127 float *data() { return _data; }
128 float const *GetArray() const { return data(); }
129
131 float const &operator[](size_t i) const { return _data[i]; }
132 float &operator[](size_t i) { return _data[i]; }
133
135 friend inline size_t hash_value(GfVec3f const &vec) {
136 return TfHash::Combine(vec[0], vec[1], vec[2]);
137 }
138
140 bool operator==(GfVec3f const &other) const {
141 return _data[0] == other[0] &&
142 _data[1] == other[1] &&
143 _data[2] == other[2];
144 }
145 bool operator!=(GfVec3f const &other) const {
146 return !(*this == other);
147 }
148
149 // TODO Add inequality for other vec types...
151 GF_API
152 bool operator==(class GfVec3d const &other) const;
154 GF_API
155 bool operator==(class GfVec3h const &other) const;
157 GF_API
158 bool operator==(class GfVec3i const &other) const;
159
162 return GfVec3f(-_data[0], -_data[1], -_data[2]);
163 }
164
166 GfVec3f &operator+=(GfVec3f const &other) {
167 _data[0] += other[0];
168 _data[1] += other[1];
169 _data[2] += other[2];
170 return *this;
171 }
172 friend GfVec3f operator+(GfVec3f const &l, GfVec3f const &r) {
173 return GfVec3f(l) += r;
174 }
175
177 GfVec3f &operator-=(GfVec3f const &other) {
178 _data[0] -= other[0];
179 _data[1] -= other[1];
180 _data[2] -= other[2];
181 return *this;
182 }
183 friend GfVec3f operator-(GfVec3f const &l, GfVec3f const &r) {
184 return GfVec3f(l) -= r;
185 }
186
188 GfVec3f &operator*=(double s) {
189 _data[0] *= s;
190 _data[1] *= s;
191 _data[2] *= s;
192 return *this;
193 }
194 GfVec3f operator*(double s) const {
195 return GfVec3f(*this) *= s;
196 }
197 friend GfVec3f operator*(double s, GfVec3f const &v) {
198 return v * s;
199 }
200
202 // TODO should divide by the scalar type.
203 GfVec3f &operator/=(double s) {
204 // TODO This should not multiply by 1/s, it should do the division.
205 // Doing the division is more numerically stable when s is close to
206 // zero.
207 return *this *= (1.0 / s);
208 }
209 GfVec3f operator/(double s) const {
210 return *this * (1.0 / s);
211 }
212
214 float operator*(GfVec3f const &v) const {
215 return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
216 }
217
222 GfVec3f GetProjection(GfVec3f const &v) const {
223 return v * (*this * v);
224 }
225
231 GfVec3f GetComplement(GfVec3f const &b) const {
232 return *this - this->GetProjection(b);
233 }
234
236 float GetLengthSq() const {
237 return *this * *this;
238 }
239
241 float GetLength() const {
242 return GfSqrt(GetLengthSq());
243 }
244
253 float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
254 // TODO this seems suspect... suggest dividing by length so long as
255 // length is not zero.
256 float length = GetLength();
257 *this /= (length > eps) ? length : eps;
258 return length;
259 }
260
261 GfVec3f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
262 GfVec3f normalized(*this);
263 normalized.Normalize(eps);
264 return normalized;
265 }
266
276 GF_API
278 GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
279 const bool normalize,
280 double eps = GF_MIN_ORTHO_TOLERANCE);
281
286 GF_API
288 float eps = GF_MIN_VECTOR_LENGTH) const;
289
290
291private:
292 float _data[3];
293};
294
297GF_API std::ostream& operator<<(std::ostream &, GfVec3f const &);
298
299
300PXR_NAMESPACE_CLOSE_SCOPE
301
302#include "pxr/base/gf/vec3d.h"
303#include "pxr/base/gf/vec3h.h"
304#include "pxr/base/gf/vec3i.h"
305
306PXR_NAMESPACE_OPEN_SCOPE
307
308inline
309GfVec3f::GfVec3f(class GfVec3d const &other)
310{
311 _data[0] = other[0];
312 _data[1] = other[1];
313 _data[2] = other[2];
314}
315inline
316GfVec3f::GfVec3f(class GfVec3h const &other)
317{
318 _data[0] = other[0];
319 _data[1] = other[1];
320 _data[2] = other[2];
321}
322inline
323GfVec3f::GfVec3f(class GfVec3i const &other)
324{
325 _data[0] = other[0];
326 _data[1] = other[1];
327 _data[2] = other[2];
328}
329
331inline GfVec3f
332GfCompMult(GfVec3f const &v1, GfVec3f const &v2) {
333 return GfVec3f(
334 v1[0] * v2[0],
335 v1[1] * v2[1],
336 v1[2] * v2[2]
337 );
338}
339
341inline GfVec3f
342GfCompDiv(GfVec3f const &v1, GfVec3f const &v2) {
343 return GfVec3f(
344 v1[0] / v2[0],
345 v1[1] / v2[1],
346 v1[2] / v2[2]
347 );
348}
349
351inline float
352GfDot(GfVec3f const &v1, GfVec3f const &v2) {
353 return v1 * v2;
354}
355
356
358inline float
360{
361 return v.GetLength();
362}
363
367inline float
369{
370 return v->Normalize(eps);
371}
372
376inline GfVec3f
378{
379 return v.GetNormalized(eps);
380}
381
386inline GfVec3f
387GfGetProjection(GfVec3f const &a, GfVec3f const &b)
388{
389 return a.GetProjection(b);
390}
391
396inline GfVec3f
397GfGetComplement(GfVec3f const &a, GfVec3f const &b)
398{
399 return a.GetComplement(b);
400}
401
404inline bool
405GfIsClose(GfVec3f const &v1, GfVec3f const &v2, double tolerance)
406{
407 GfVec3f delta = v1 - v2;
408 return delta.GetLengthSq() <= tolerance * tolerance;
409}
410
411
412GF_API bool
413GfOrthogonalizeBasis(GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
414 bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
415
416GF_API void
417GfBuildOrthonormalFrame(GfVec3f const &v0,
418 GfVec3f* v1,
419 GfVec3f* v2,
420 float eps = GF_MIN_VECTOR_LENGTH);
421
423inline GfVec3f
424GfCross(GfVec3f const &v1, GfVec3f const &v2)
425{
426 return GfVec3f(
427 v1[1] * v2[2] - v1[2] * v2[1],
428 v1[2] * v2[0] - v1[0] * v2[2],
429 v1[0] * v2[1] - v1[1] * v2[0]);
430}
431
434inline GfVec3f
435operator^(GfVec3f const &v1, GfVec3f const &v2)
436{
437 return GfCross(v1, v2);
438}
439
441GF_API GfVec3f
442GfSlerp(double alpha, GfVec3f const &v0, GfVec3f const &v1);
443
444
445
446PXR_NAMESPACE_CLOSE_SCOPE
447
448#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 3 double components.
Definition: vec3d.h:46
Basic type for a vector of 3 float components.
Definition: vec3f.h:46
GfVec3f & Set(float s0, float s1, float s2)
Set all elements with passed arguments.
Definition: vec3f.h:113
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:63
GfVec3f & operator-=(GfVec3f const &other)
Subtraction.
Definition: vec3f.h:177
static GfVec3f XAxis()
Create a unit vector along the X-axis.
Definition: vec3f.h:85
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:105
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:57
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:241
GfVec3f operator-() const
Create a vec with negated elements.
Definition: vec3f.h:161
GfVec3f & operator*=(double s)
Multiplication by scalar.
Definition: vec3f.h:188
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec3f.h:253
GfVec3f GetProjection(GfVec3f const &v) const
Returns the projection of this onto v.
Definition: vec3f.h:222
float const & operator[](size_t i) const
Indexing.
Definition: vec3f.h:131
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:236
friend size_t hash_value(GfVec3f const &vec)
Hash.
Definition: vec3f.h:135
float operator*(GfVec3f const &v) const
See GfDot().
Definition: vec3f.h:214
static GfVec3f YAxis()
Create a unit vector along the Y-axis.
Definition: vec3f.h:91
constexpr GfVec3f(Scl const *p)
Construct with pointer to values.
Definition: vec3f.h:70
bool operator==(GfVec3f const &other) const
Equality comparison.
Definition: vec3f.h:140
static GfVec3f ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3f.h:97
GfVec3f & operator+=(GfVec3f const &other)
Addition.
Definition: vec3f.h:166
GfVec3f & operator/=(double s)
Division by scalar.
Definition: vec3f.h:203
GF_API bool operator==(class GfVec3i const &other) const
Equality comparison.
float const * data() const
Direct data access.
Definition: vec3f.h:126
float ScalarType
Scalar element type and dimension.
Definition: vec3f.h:49
GfVec3f GetComplement(GfVec3f const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec3f.h:231
GfVec3f & Set(float const *a)
Set all elements with a pointer to data.
Definition: vec3f.h:121
Basic type for a vector of 3 GfHalf components.
Definition: vec3h.h:47
Basic type for a vector of 3 int components.
Definition: vec3i.h:44
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:187
#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 &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
#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:19
float GfDot(GfVec3f const &v1, GfVec3f const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec3f.h:352
GfVec3f GfGetComplement(GfVec3f const &a, GfVec3f const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec3f.h:397
GfVec3f GfCross(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition: vec3f.h:424
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:387
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:405
GfVec3f GfCompDiv(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec3f.h:342
float GfGetLength(GfVec3f const &v)
Returns the geometric length of v.
Definition: vec3f.h:359
GfVec3f GfCompMult(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec3f.h:332
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:368
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:377
GfVec3f operator^(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition: vec3f.h:435