This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
vec4f.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_VEC4F_H
12#define PXR_BASE_GF_VEC4F_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 GfVec4f;
33
34template <>
35struct GfIsGfVec<class GfVec4f> { static const bool value = true; };
36
46{
47public:
49 typedef float ScalarType;
50 static const size_t dimension = 4;
51
53 GfVec4f() = default;
54
56 constexpr explicit GfVec4f(float value)
57 : _data{ value, value, value, value }
58 {
59 }
60
62 constexpr GfVec4f(float s0, float s1, float s2, float s3)
63 : _data{ s0, s1, s2, s3 }
64 {
65 }
66
68 template <class Scl>
69 constexpr explicit GfVec4f(Scl const *p)
70 : _data{ p[0], p[1], p[2], p[3] }
71 {
72 }
73
75 explicit GfVec4f(class GfVec4d const &other);
76
78 GfVec4f(class GfVec4h const &other);
79
81 GfVec4f(class GfVec4i const &other);
82
84 static GfVec4f XAxis() {
85 GfVec4f result(0);
86 result[0] = 1;
87 return result;
88 }
90 static GfVec4f YAxis() {
91 GfVec4f result(0);
92 result[1] = 1;
93 return result;
94 }
96 static GfVec4f ZAxis() {
97 GfVec4f result(0);
98 result[2] = 1;
99 return result;
100 }
102 static GfVec4f WAxis() {
103 GfVec4f result(0);
104 result[3] = 1;
105 return result;
106 }
107
110 static GfVec4f Axis(size_t i) {
111 GfVec4f result(0);
112 if (i < 4)
113 result[i] = 1;
114 return result;
115 }
116
118 GfVec4f &Set(float s0, float s1, float s2, float s3) {
119 _data[0] = s0;
120 _data[1] = s1;
121 _data[2] = s2;
122 _data[3] = s3;
123 return *this;
124 }
125
127 GfVec4f &Set(float const *a) {
128 return Set(a[0], a[1], a[2], a[3]);
129 }
130
132 float const *data() const { return _data; }
133 float *data() { return _data; }
134 float const *GetArray() const { return data(); }
135
137 float const &operator[](size_t i) const { return _data[i]; }
138 float &operator[](size_t i) { return _data[i]; }
139
141 friend inline size_t hash_value(GfVec4f const &vec) {
142 return TfHash::Combine(vec[0], vec[1], vec[2], vec[3]);
143 }
144
146 bool operator==(GfVec4f const &other) const {
147 return _data[0] == other[0] &&
148 _data[1] == other[1] &&
149 _data[2] == other[2] &&
150 _data[3] == other[3];
151 }
152 bool operator!=(GfVec4f const &other) const {
153 return !(*this == other);
154 }
155
156 // TODO Add inequality for other vec types...
158 GF_API
159 bool operator==(class GfVec4d const &other) const;
161 GF_API
162 bool operator==(class GfVec4h const &other) const;
164 GF_API
165 bool operator==(class GfVec4i const &other) const;
166
169 return GfVec4f(-_data[0], -_data[1], -_data[2], -_data[3]);
170 }
171
173 GfVec4f &operator+=(GfVec4f const &other) {
174 _data[0] += other[0];
175 _data[1] += other[1];
176 _data[2] += other[2];
177 _data[3] += other[3];
178 return *this;
179 }
180 friend GfVec4f operator+(GfVec4f const &l, GfVec4f const &r) {
181 return GfVec4f(l) += r;
182 }
183
185 GfVec4f &operator-=(GfVec4f const &other) {
186 _data[0] -= other[0];
187 _data[1] -= other[1];
188 _data[2] -= other[2];
189 _data[3] -= other[3];
190 return *this;
191 }
192 friend GfVec4f operator-(GfVec4f const &l, GfVec4f const &r) {
193 return GfVec4f(l) -= r;
194 }
195
197 GfVec4f &operator*=(double s) {
198 _data[0] *= s;
199 _data[1] *= s;
200 _data[2] *= s;
201 _data[3] *= s;
202 return *this;
203 }
204 GfVec4f operator*(double s) const {
205 return GfVec4f(*this) *= s;
206 }
207 friend GfVec4f operator*(double s, GfVec4f const &v) {
208 return v * s;
209 }
210
212 // TODO should divide by the scalar type.
213 GfVec4f &operator/=(double s) {
214 // TODO This should not multiply by 1/s, it should do the division.
215 // Doing the division is more numerically stable when s is close to
216 // zero.
217 return *this *= (1.0 / s);
218 }
219 GfVec4f operator/(double s) const {
220 return *this * (1.0 / s);
221 }
222
224 float operator*(GfVec4f const &v) const {
225 return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
226 }
227
232 GfVec4f GetProjection(GfVec4f const &v) const {
233 return v * (*this * v);
234 }
235
241 GfVec4f GetComplement(GfVec4f const &b) const {
242 return *this - this->GetProjection(b);
243 }
244
246 float GetLengthSq() const {
247 return *this * *this;
248 }
249
251 float GetLength() const {
252 return GfSqrt(GetLengthSq());
253 }
254
263 float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
264 // TODO this seems suspect... suggest dividing by length so long as
265 // length is not zero.
266 float length = GetLength();
267 *this /= (length > eps) ? length : eps;
268 return length;
269 }
270
271 GfVec4f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
272 GfVec4f normalized(*this);
273 normalized.Normalize(eps);
274 return normalized;
275 }
276
277
278private:
279 float _data[4];
280};
281
284GF_API std::ostream& operator<<(std::ostream &, GfVec4f const &);
285
286
287PXR_NAMESPACE_CLOSE_SCOPE
288
289#include "pxr/base/gf/vec4d.h"
290#include "pxr/base/gf/vec4h.h"
291#include "pxr/base/gf/vec4i.h"
292
293PXR_NAMESPACE_OPEN_SCOPE
294
295inline
296GfVec4f::GfVec4f(class GfVec4d const &other)
297{
298 _data[0] = other[0];
299 _data[1] = other[1];
300 _data[2] = other[2];
301 _data[3] = other[3];
302}
303inline
304GfVec4f::GfVec4f(class GfVec4h const &other)
305{
306 _data[0] = other[0];
307 _data[1] = other[1];
308 _data[2] = other[2];
309 _data[3] = other[3];
310}
311inline
312GfVec4f::GfVec4f(class GfVec4i const &other)
313{
314 _data[0] = other[0];
315 _data[1] = other[1];
316 _data[2] = other[2];
317 _data[3] = other[3];
318}
319
321inline GfVec4f
322GfCompMult(GfVec4f const &v1, GfVec4f const &v2) {
323 return GfVec4f(
324 v1[0] * v2[0],
325 v1[1] * v2[1],
326 v1[2] * v2[2],
327 v1[3] * v2[3]
328 );
329}
330
332inline GfVec4f
333GfCompDiv(GfVec4f const &v1, GfVec4f const &v2) {
334 return GfVec4f(
335 v1[0] / v2[0],
336 v1[1] / v2[1],
337 v1[2] / v2[2],
338 v1[3] / v2[3]
339 );
340}
341
343inline float
344GfDot(GfVec4f const &v1, GfVec4f const &v2) {
345 return v1 * v2;
346}
347
348
350inline float
352{
353 return v.GetLength();
354}
355
359inline float
361{
362 return v->Normalize(eps);
363}
364
368inline GfVec4f
370{
371 return v.GetNormalized(eps);
372}
373
378inline GfVec4f
379GfGetProjection(GfVec4f const &a, GfVec4f const &b)
380{
381 return a.GetProjection(b);
382}
383
388inline GfVec4f
389GfGetComplement(GfVec4f const &a, GfVec4f const &b)
390{
391 return a.GetComplement(b);
392}
393
396inline bool
397GfIsClose(GfVec4f const &v1, GfVec4f const &v2, double tolerance)
398{
399 GfVec4f delta = v1 - v2;
400 return delta.GetLengthSq() <= tolerance * tolerance;
401}
402
403
404
405PXR_NAMESPACE_CLOSE_SCOPE
406
407#endif // PXR_BASE_GF_VEC4F_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
Basic type for a vector of 4 double components.
Definition: vec4d.h:46
Basic type for a vector of 4 float components.
Definition: vec4f.h:46
bool operator==(GfVec4f const &other) const
Equality comparison.
Definition: vec4f.h:146
GfVec4f & Set(float const *a)
Set all elements with a pointer to data.
Definition: vec4f.h:127
GfVec4f GetProjection(GfVec4f const &v) const
Returns the projection of this onto v.
Definition: vec4f.h:232
GfVec4f & operator-=(GfVec4f const &other)
Subtraction.
Definition: vec4f.h:185
static GfVec4f WAxis()
Create a unit vector along the W-axis.
Definition: vec4f.h:102
friend size_t hash_value(GfVec4f const &vec)
Hash.
Definition: vec4f.h:141
static GfVec4f ZAxis()
Create a unit vector along the Z-axis.
Definition: vec4f.h:96
float GetLength() const
Length.
Definition: vec4f.h:251
constexpr GfVec4f(Scl const *p)
Construct with pointer to values.
Definition: vec4f.h:69
GfVec4f & operator/=(double s)
Division by scalar.
Definition: vec4f.h:213
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec4f.h:263
GfVec4f()=default
Default constructor does no initialization.
GfVec4f GetComplement(GfVec4f const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec4f.h:241
float const & operator[](size_t i) const
Indexing.
Definition: vec4f.h:137
float GetLengthSq() const
Squared length.
Definition: vec4f.h:246
GfVec4f & Set(float s0, float s1, float s2, float s3)
Set all elements with passed arguments.
Definition: vec4f.h:118
float operator*(GfVec4f const &v) const
See GfDot().
Definition: vec4f.h:224
GfVec4f & operator+=(GfVec4f const &other)
Addition.
Definition: vec4f.h:173
GF_API bool operator==(class GfVec4i const &other) const
Equality comparison.
constexpr GfVec4f(float value)
Initialize all elements to a single value.
Definition: vec4f.h:56
GF_API bool operator==(class GfVec4h const &other) const
Equality comparison.
constexpr GfVec4f(float s0, float s1, float s2, float s3)
Initialize all elements with explicit arguments.
Definition: vec4f.h:62
GF_API bool operator==(class GfVec4d const &other) const
Equality comparison.
static GfVec4f XAxis()
Create a unit vector along the X-axis.
Definition: vec4f.h:84
static GfVec4f YAxis()
Create a unit vector along the Y-axis.
Definition: vec4f.h:90
float const * data() const
Direct data access.
Definition: vec4f.h:132
float ScalarType
Scalar element type and dimension.
Definition: vec4f.h:49
static GfVec4f Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec4f.h:110
GfVec4f operator-() const
Create a vec with negated elements.
Definition: vec4f.h:168
GfVec4f & operator*=(double s)
Multiplication by scalar.
Definition: vec4f.h:197
Basic type for a vector of 4 GfHalf components.
Definition: vec4h.h:47
Basic type for a vector of 4 int components.
Definition: vec4i.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].
Defines useful mathematical limits.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition: traits.h:19
GfVec4f GfCompMult(GfVec4f const &v1, GfVec4f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec4f.h:322
GfVec4f GfGetComplement(GfVec4f const &a, GfVec4f const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec4f.h:389
float GfGetLength(GfVec4f const &v)
Returns the geometric length of v.
Definition: vec4f.h:351
bool GfIsClose(GfVec4f const &v1, GfVec4f const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition: vec4f.h:397
float GfDot(GfVec4f const &v1, GfVec4f const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec4f.h:344
float GfNormalize(GfVec4f *v, float eps=GF_MIN_VECTOR_LENGTH)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec4f.h:360
GfVec4f GfCompDiv(GfVec4f const &v1, GfVec4f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec4f.h:333
GfVec4f GfGetNormalized(GfVec4f const &v, float eps=GF_MIN_VECTOR_LENGTH)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec4f.h:369
GfVec4f GfGetProjection(GfVec4f const &a, GfVec4f const &b)
Returns the projection of a onto b.
Definition: vec4f.h:379