All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
53 GfVec3f() = default;
54
56 constexpr explicit GfVec3f(float value)
57 : _data{ value, value, value }
58 {
59 }
60
62 constexpr GfVec3f(float s0, float s1, float s2)
63 : _data{ s0, s1, s2 }
64 {
65 }
66
68 template <class Scl>
69 constexpr explicit GfVec3f(Scl const *p)
70 : _data{ p[0], p[1], p[2] }
71 {
72 }
73
75 explicit GfVec3f(class GfVec3d const &other);
76
78 GfVec3f(class GfVec3h const &other);
79
81 GfVec3f(class GfVec3i const &other);
82
84 static GfVec3f XAxis() {
85 GfVec3f result(0);
86 result[0] = 1;
87 return result;
88 }
90 static GfVec3f YAxis() {
91 GfVec3f result(0);
92 result[1] = 1;
93 return result;
94 }
96 static GfVec3f ZAxis() {
97 GfVec3f result(0);
98 result[2] = 1;
99 return result;
100 }
101
104 static GfVec3f Axis(size_t i) {
105 GfVec3f result(0);
106 if (i < 3)
107 result[i] = 1;
108 return result;
109 }
110
112 GfVec3f &Set(float s0, float s1, float s2) {
113 _data[0] = s0;
114 _data[1] = s1;
115 _data[2] = s2;
116 return *this;
117 }
118
120 GfVec3f &Set(float const *a) {
121 return Set(a[0], a[1], a[2]);
122 }
123
125 float const *data() const { return _data; }
126 float *data() { return _data; }
127 float const *GetArray() const { return data(); }
128
130 float const &operator[](size_t i) const { return _data[i]; }
131 float &operator[](size_t i) { return _data[i]; }
132
134 friend inline size_t hash_value(GfVec3f const &vec) {
135 return TfHash::Combine(vec[0], vec[1], vec[2]);
136 }
137
139 bool operator==(GfVec3f const &other) const {
140 return _data[0] == other[0] &&
141 _data[1] == other[1] &&
142 _data[2] == other[2];
143 }
144 bool operator!=(GfVec3f const &other) const {
145 return !(*this == other);
146 }
147
148 // TODO Add inequality for other vec types...
150 GF_API
151 bool operator==(class GfVec3d const &other) const;
153 GF_API
154 bool operator==(class GfVec3h const &other) const;
156 GF_API
157 bool operator==(class GfVec3i const &other) const;
158
161 return GfVec3f(-_data[0], -_data[1], -_data[2]);
162 }
163
165 GfVec3f &operator+=(GfVec3f const &other) {
166 _data[0] += other[0];
167 _data[1] += other[1];
168 _data[2] += other[2];
169 return *this;
170 }
171 friend GfVec3f operator+(GfVec3f const &l, GfVec3f const &r) {
172 return GfVec3f(l) += r;
173 }
174
176 GfVec3f &operator-=(GfVec3f const &other) {
177 _data[0] -= other[0];
178 _data[1] -= other[1];
179 _data[2] -= other[2];
180 return *this;
181 }
182 friend GfVec3f operator-(GfVec3f const &l, GfVec3f const &r) {
183 return GfVec3f(l) -= r;
184 }
185
187 GfVec3f &operator*=(double s) {
188 _data[0] *= s;
189 _data[1] *= s;
190 _data[2] *= s;
191 return *this;
192 }
193 GfVec3f operator*(double s) const {
194 return GfVec3f(*this) *= s;
195 }
196 friend GfVec3f operator*(double s, GfVec3f const &v) {
197 return v * s;
198 }
199
201 // TODO should divide by the scalar type.
202 GfVec3f &operator/=(double s) {
203 // TODO This should not multiply by 1/s, it should do the division.
204 // Doing the division is more numerically stable when s is close to
205 // zero.
206 return *this *= (1.0 / s);
207 }
208 GfVec3f operator/(double s) const {
209 return *this * (1.0 / s);
210 }
211
213 float operator*(GfVec3f const &v) const {
214 return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
215 }
216
221 GfVec3f GetProjection(GfVec3f const &v) const {
222 return v * (*this * v);
223 }
224
230 GfVec3f GetComplement(GfVec3f const &b) const {
231 return *this - this->GetProjection(b);
232 }
233
235 float GetLengthSq() const {
236 return *this * *this;
237 }
238
240 float GetLength() const {
241 return GfSqrt(GetLengthSq());
242 }
243
252 float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
253 // TODO this seems suspect... suggest dividing by length so long as
254 // length is not zero.
255 float length = GetLength();
256 *this /= (length > eps) ? length : eps;
257 return length;
258 }
259
260 GfVec3f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
261 GfVec3f normalized(*this);
262 normalized.Normalize(eps);
263 return normalized;
264 }
265
275 GF_API
277 GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
278 const bool normalize,
279 double eps = GF_MIN_ORTHO_TOLERANCE);
280
285 GF_API
287 float eps = GF_MIN_VECTOR_LENGTH) const;
288
289
290private:
291 float _data[3];
292};
293
296GF_API std::ostream& operator<<(std::ostream &, GfVec3f const &);
297
298
299PXR_NAMESPACE_CLOSE_SCOPE
300
301#include "pxr/base/gf/vec3d.h"
302#include "pxr/base/gf/vec3h.h"
303#include "pxr/base/gf/vec3i.h"
304
305PXR_NAMESPACE_OPEN_SCOPE
306
307inline
308GfVec3f::GfVec3f(class GfVec3d const &other)
309{
310 _data[0] = other[0];
311 _data[1] = other[1];
312 _data[2] = other[2];
313}
314inline
315GfVec3f::GfVec3f(class GfVec3h const &other)
316{
317 _data[0] = other[0];
318 _data[1] = other[1];
319 _data[2] = other[2];
320}
321inline
322GfVec3f::GfVec3f(class GfVec3i const &other)
323{
324 _data[0] = other[0];
325 _data[1] = other[1];
326 _data[2] = other[2];
327}
328
330inline GfVec3f
331GfCompMult(GfVec3f const &v1, GfVec3f const &v2) {
332 return GfVec3f(
333 v1[0] * v2[0],
334 v1[1] * v2[1],
335 v1[2] * v2[2]
336 );
337}
338
340inline GfVec3f
341GfCompDiv(GfVec3f const &v1, GfVec3f const &v2) {
342 return GfVec3f(
343 v1[0] / v2[0],
344 v1[1] / v2[1],
345 v1[2] / v2[2]
346 );
347}
348
350inline float
351GfDot(GfVec3f const &v1, GfVec3f const &v2) {
352 return v1 * v2;
353}
354
355
357inline float
359{
360 return v.GetLength();
361}
362
366inline float
368{
369 return v->Normalize(eps);
370}
371
375inline GfVec3f
377{
378 return v.GetNormalized(eps);
379}
380
385inline GfVec3f
386GfGetProjection(GfVec3f const &a, GfVec3f const &b)
387{
388 return a.GetProjection(b);
389}
390
395inline GfVec3f
396GfGetComplement(GfVec3f const &a, GfVec3f const &b)
397{
398 return a.GetComplement(b);
399}
400
403inline bool
404GfIsClose(GfVec3f const &v1, GfVec3f const &v2, double tolerance)
405{
406 GfVec3f delta = v1 - v2;
407 return delta.GetLengthSq() <= tolerance * tolerance;
408}
409
410
411GF_API bool
412GfOrthogonalizeBasis(GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
413 bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
414
415GF_API void
416GfBuildOrthonormalFrame(GfVec3f const &v0,
417 GfVec3f* v1,
418 GfVec3f* v2,
419 float eps = GF_MIN_VECTOR_LENGTH);
420
422inline GfVec3f
423GfCross(GfVec3f const &v1, GfVec3f const &v2)
424{
425 return GfVec3f(
426 v1[1] * v2[2] - v1[2] * v2[1],
427 v1[2] * v2[0] - v1[0] * v2[2],
428 v1[0] * v2[1] - v1[1] * v2[0]);
429}
430
433inline GfVec3f
434operator^(GfVec3f const &v1, GfVec3f const &v2)
435{
436 return GfCross(v1, v2);
437}
438
440GF_API GfVec3f
441GfSlerp(double alpha, GfVec3f const &v0, GfVec3f const &v1);
442
443
444
445PXR_NAMESPACE_CLOSE_SCOPE
446
447#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:112
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:62
GfVec3f & operator-=(GfVec3f const &other)
Subtraction.
Definition: vec3f.h:176
static GfVec3f XAxis()
Create a unit vector along the X-axis.
Definition: vec3f.h:84
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:104
GfVec3f()=default
Default constructor does no initialization.
constexpr GfVec3f(float value)
Initialize all elements to a single value.
Definition: vec3f.h:56
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:240
GfVec3f operator-() const
Create a vec with negated elements.
Definition: vec3f.h:160
GfVec3f & operator*=(double s)
Multiplication by scalar.
Definition: vec3f.h:187
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec3f.h:252
GfVec3f GetProjection(GfVec3f const &v) const
Returns the projection of this onto v.
Definition: vec3f.h:221
float const & operator[](size_t i) const
Indexing.
Definition: vec3f.h:130
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:235
friend size_t hash_value(GfVec3f const &vec)
Hash.
Definition: vec3f.h:134
float operator*(GfVec3f const &v) const
See GfDot().
Definition: vec3f.h:213
static GfVec3f YAxis()
Create a unit vector along the Y-axis.
Definition: vec3f.h:90
constexpr GfVec3f(Scl const *p)
Construct with pointer to values.
Definition: vec3f.h:69
bool operator==(GfVec3f const &other) const
Equality comparison.
Definition: vec3f.h:139
static GfVec3f ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3f.h:96
GfVec3f & operator+=(GfVec3f const &other)
Addition.
Definition: vec3f.h:165
GfVec3f & operator/=(double s)
Division by scalar.
Definition: vec3f.h:202
GF_API bool operator==(class GfVec3i const &other) const
Equality comparison.
float const * data() const
Direct data access.
Definition: vec3f.h:125
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:230
GfVec3f & Set(float const *a)
Set all elements with a pointer to data.
Definition: vec3f.h:120
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:475
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:351
GfVec3f GfGetComplement(GfVec3f const &a, GfVec3f const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec3f.h:396
GfVec3f GfCross(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition: vec3f.h:423
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:386
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:404
GfVec3f GfCompDiv(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec3f.h:341
float GfGetLength(GfVec3f const &v)
Returns the geometric length of v.
Definition: vec3f.h:358
GfVec3f GfCompMult(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec3f.h:331
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:367
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:376
GfVec3f operator^(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition: vec3f.h:434