Loading...
Searching...
No Matches
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"
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
47PXR_NAMESPACE_OPEN_SCOPE
48
49class GfVec3f;
50
51template <>
52struct GfIsGfVec<class GfVec3f> { static const bool value = true; };
53
63{
64public:
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
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
294 GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
295 const bool normalize,
296 double eps = GF_MIN_ORTHO_TOLERANCE);
297
302 GF_API
304 float eps = GF_MIN_VECTOR_LENGTH) const;
305
306
307private:
308 float _data[3];
309};
310
313GF_API std::ostream& operator<<(std::ostream &, GfVec3f const &);
314
315
316PXR_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
322PXR_NAMESPACE_OPEN_SCOPE
323
324inline
325GfVec3f::GfVec3f(class GfVec3d const &other)
326{
327 _data[0] = other[0];
328 _data[1] = other[1];
329 _data[2] = other[2];
330}
331inline
332GfVec3f::GfVec3f(class GfVec3h const &other)
333{
334 _data[0] = other[0];
335 _data[1] = other[1];
336 _data[2] = other[2];
337}
338inline
339GfVec3f::GfVec3f(class GfVec3i const &other)
340{
341 _data[0] = other[0];
342 _data[1] = other[1];
343 _data[2] = other[2];
344}
345
347inline GfVec3f
348GfCompMult(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
357inline GfVec3f
358GfCompDiv(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
367inline float
368GfDot(GfVec3f const &v1, GfVec3f const &v2) {
369 return v1 * v2;
370}
371
372
374inline float
376{
377 return v.GetLength();
378}
379
383inline float
385{
386 return v->Normalize(eps);
387}
388
392inline GfVec3f
394{
395 return v.GetNormalized(eps);
396}
397
402inline GfVec3f
403GfGetProjection(GfVec3f const &a, GfVec3f const &b)
404{
405 return a.GetProjection(b);
406}
407
412inline GfVec3f
413GfGetComplement(GfVec3f const &a, GfVec3f const &b)
414{
415 return a.GetComplement(b);
416}
417
420inline bool
421GfIsClose(GfVec3f const &v1, GfVec3f const &v2, double tolerance)
422{
423 GfVec3f delta = v1 - v2;
424 return delta.GetLengthSq() <= tolerance * tolerance;
425}
426
427
428GF_API bool
429GfOrthogonalizeBasis(GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
430 bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
431
432GF_API void
433GfBuildOrthonormalFrame(GfVec3f const &v0,
434 GfVec3f* v1,
435 GfVec3f* v2,
436 float eps = GF_MIN_VECTOR_LENGTH);
437
439inline GfVec3f
440GfCross(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
450inline GfVec3f
451operator^(GfVec3f const &v1, GfVec3f const &v2)
452{
453 return GfCross(v1, v2);
454}
455
457GF_API GfVec3f
458GfSlerp(double alpha, GfVec3f const &v0, GfVec3f const &v1);
459
460
461
462PXR_NAMESPACE_CLOSE_SCOPE
463
464#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:63
Basic type for a vector of 3 float components.
Definition: vec3f.h:63
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:79
GfVec3f & operator-=(GfVec3f const &other)
Subtraction.
Definition: vec3f.h:193
static GfVec3f XAxis()
Create a unit vector along the X-axis.
Definition: vec3f.h:101
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
Default constructor does no initialization.
constexpr GfVec3f(float value)
Initialize all elements to a single value.
Definition: vec3f.h:73
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:257
GfVec3f operator-() const
Create a vec with negated elements.
Definition: vec3f.h:177
GfVec3f & operator*=(double s)
Multiplication by scalar.
Definition: vec3f.h:204
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
GfVec3f GetProjection(GfVec3f const &v) const
Returns the projection of this onto v.
Definition: vec3f.h:238
float const & operator[](size_t i) const
Indexing.
Definition: vec3f.h:147
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:252
friend size_t hash_value(GfVec3f const &vec)
Hash.
Definition: vec3f.h:151
float operator*(GfVec3f const &v) const
See GfDot().
Definition: vec3f.h:230
static GfVec3f YAxis()
Create a unit vector along the Y-axis.
Definition: vec3f.h:107
constexpr GfVec3f(Scl const *p)
Construct with pointer to values.
Definition: vec3f.h:86
bool operator==(GfVec3f const &other) const
Equality comparison.
Definition: vec3f.h:156
static GfVec3f ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3f.h:113
GfVec3f & operator+=(GfVec3f const &other)
Addition.
Definition: vec3f.h:182
GfVec3f & operator/=(double s)
Division by scalar.
Definition: vec3f.h:219
GF_API bool operator==(class GfVec3i const &other) const
Equality comparison.
float const * data() const
Direct data access.
Definition: vec3f.h:142
float ScalarType
Scalar element type and dimension.
Definition: vec3f.h:66
GfVec3f GetComplement(GfVec3f const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec3f.h:247
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:64
Basic type for a vector of 3 int components.
Definition: vec3i.h:61
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:492
Assorted mathematical utility functions.
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:80
#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
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:39
Defines useful mathematical limits.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition: traits.h:36
float GfDot(GfVec3f const &v1, GfVec3f const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec3f.h:368
GfVec3f GfGetComplement(GfVec3f const &a, GfVec3f const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec3f.h:413
GfVec3f GfCross(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition: vec3f.h:440
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:403
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
GfVec3f GfCompDiv(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec3f.h:358
float GfGetLength(GfVec3f const &v)
Returns the geometric length of v.
Definition: vec3f.h:375
GfVec3f GfCompMult(GfVec3f const &v1, GfVec3f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec3f.h:348
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
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
GfVec3f operator^(GfVec3f const &v1, GfVec3f const &v2)
Returns the cross product of v1 and v2.
Definition: vec3f.h:451