HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 //
7 ////////////////////////////////////////////////////////////////////////
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 
14 /// \file gf/vec4f.h
15 /// \ingroup group_gf_LinearAlgebra
16 
17 #include "pxr/pxr.h"
18 #include "pxr/base/tf/diagnostic.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 
31 
32 class GfVec4f;
33 
34 template <>
35 struct GfIsGfVec<class GfVec4f> { static const bool value = true; };
36 
37 /// \class GfVec4f
38 /// \ingroup group_gf_LinearAlgebra
39 ///
40 /// Basic type for a vector of 4 float components.
41 ///
42 /// Represents a vector of 4 components of type \c float.
43 /// It is intended to be fast and simple.
44 ///
45 class GfVec4f
46 {
47 public:
48  /// Scalar element type and dimension.
49  typedef float ScalarType;
50  static const size_t dimension = 4;
51 
52  /// Default constructor does no initialization.
53  GfVec4f() = default;
54 
55  /// Initialize all elements to a single value.
56  constexpr explicit GfVec4f(float value)
57  : _data{ value, value, value, value }
58  {
59  }
60 
61  /// Initialize all elements with explicit arguments.
62  constexpr GfVec4f(float s0, float s1, float s2, float s3)
63  : _data{ s0, s1, s2, s3 }
64  {
65  }
66 
67  /// Construct with pointer to values.
68  template <class Scl>
69  constexpr explicit GfVec4f(Scl const *p)
70  : _data{ p[0], p[1], p[2], p[3] }
71  {
72  }
73 
74  /// Construct from GfVec4d.
75  explicit GfVec4f(class GfVec4d const &other);
76 
77  /// Implicitly convert from GfVec4h.
78  GfVec4f(class GfVec4h const &other);
79 
80  /// Implicitly convert from GfVec4i.
81  GfVec4f(class GfVec4i const &other);
82 
83  /// Create a unit vector along the X-axis.
84  static GfVec4f XAxis() {
85  GfVec4f result(0);
86  result[0] = 1;
87  return result;
88  }
89  /// Create a unit vector along the Y-axis.
90  static GfVec4f YAxis() {
91  GfVec4f result(0);
92  result[1] = 1;
93  return result;
94  }
95  /// Create a unit vector along the Z-axis.
96  static GfVec4f ZAxis() {
97  GfVec4f result(0);
98  result[2] = 1;
99  return result;
100  }
101  /// Create a unit vector along the W-axis.
102  static GfVec4f WAxis() {
103  GfVec4f result(0);
104  result[3] = 1;
105  return result;
106  }
107 
108  /// Create a unit vector along the i-th axis, zero-based. Return the zero
109  /// vector if \p i is greater than or equal to 4.
110  static GfVec4f Axis(size_t i) {
111  GfVec4f result(0);
112  if (i < 4)
113  result[i] = 1;
114  return result;
115  }
116 
117  /// Set all elements with passed arguments.
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 
126  /// Set all elements with a pointer to data.
127  GfVec4f &Set(float const *a) {
128  return Set(a[0], a[1], a[2], a[3]);
129  }
130 
131  /// Direct data access.
132  float const *data() const { return _data; }
133  float *data() { return _data; }
134  float const *GetArray() const { return data(); }
135 
136  /// Indexing.
137  float const &operator[](size_t i) const { return _data[i]; }
138  float &operator[](size_t i) { return _data[i]; }
139 
140  /// Hash.
141  friend inline size_t hash_value(GfVec4f const &vec) {
142  return TfHash::Combine(vec[0], vec[1], vec[2], vec[3]);
143  }
144 
145  /// Equality comparison.
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...
157  /// Equality comparison.
158  GF_API
159  bool operator==(class GfVec4d const &other) const;
160  /// Equality comparison.
161  GF_API
162  bool operator==(class GfVec4h const &other) const;
163  /// Equality comparison.
164  GF_API
165  bool operator==(class GfVec4i const &other) const;
166 
167  /// Create a vec with negated elements.
168  GfVec4f operator-() const {
169  return GfVec4f(-_data[0], -_data[1], -_data[2], -_data[3]);
170  }
171 
172  /// Addition.
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 
184  /// Subtraction.
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 
196  /// Multiplication by scalar.
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 
211  /// Division by scalar.
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 
223  /// See GfDot().
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 
228  /// Returns the projection of \p this onto \p v. That is:
229  /// \code
230  /// v * (*this * v)
231  /// \endcode
232  GfVec4f GetProjection(GfVec4f const &v) const {
233  return v * (*this * v);
234  }
235 
236  /// Returns the orthogonal complement of \p this->GetProjection(b).
237  /// That is:
238  /// \code
239  /// *this - this->GetProjection(b)
240  /// \endcode
241  GfVec4f GetComplement(GfVec4f const &b) const {
242  return *this - this->GetProjection(b);
243  }
244 
245  /// Squared length.
246  float GetLengthSq() const {
247  return *this * *this;
248  }
249 
250  /// Length
251  float GetLength() const {
252  return GfSqrt(GetLengthSq());
253  }
254 
255  /// Normalizes the vector in place to unit length, returning the
256  /// length before normalization. If the length of the vector is
257  /// smaller than \p eps, then the vector is set to vector/\c eps.
258  /// The original length of the vector is returned. See also GfNormalize().
259  ///
260  /// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
261  /// By tickling the code, it no longer tries to write into
262  /// an illegal memory address (in the code section of memory).
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 
272  GfVec4f normalized(*this);
273  normalized.Normalize(eps);
274  return normalized;
275  }
276 
277 
278 private:
279  float _data[4];
280 };
281 
282 /// Output a GfVec4f.
283 /// \ingroup group_gf_DebuggingOutput
284 GF_API std::ostream& operator<<(std::ostream &, GfVec4f const &);
285 
286 
288 
289 #include "pxr/base/gf/vec4d.h"
290 #include "pxr/base/gf/vec4h.h"
291 #include "pxr/base/gf/vec4i.h"
292 
294 
295 inline
296 GfVec4f::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 }
303 inline
304 GfVec4f::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 }
311 inline
312 GfVec4f::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 
320 /// Returns component-wise multiplication of vectors \p v1 and \p v2.
321 inline GfVec4f
322 GfCompMult(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 
331 /// Returns component-wise quotient of vectors \p v1 and \p v2.
332 inline GfVec4f
333 GfCompDiv(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 
342 /// Returns the dot (inner) product of two vectors.
343 inline float
344 GfDot(GfVec4f const &v1, GfVec4f const &v2) {
345  return v1 * v2;
346 }
347 
348 
349 /// Returns the geometric length of \c v.
350 inline float
352 {
353  return v.GetLength();
354 }
355 
356 /// Normalizes \c *v in place to unit length, returning the length before
357 /// normalization. If the length of \c *v is smaller than \p eps then \c *v is
358 /// set to \c *v/eps. The original length of \c *v is returned.
359 inline float
361 {
362  return v->Normalize(eps);
363 }
364 
365 /// Returns a normalized (unit-length) vector with the same direction as \p v.
366 /// If the length of this vector is smaller than \p eps, the vector divided by
367 /// \p eps is returned.
368 inline GfVec4f
370 {
371  return v.GetNormalized(eps);
372 }
373 
374 /// Returns the projection of \p a onto \p b. That is:
375 /// \code
376 /// b * (a * b)
377 /// \endcode
378 inline GfVec4f
380 {
381  return a.GetProjection(b);
382 }
383 
384 /// Returns the orthogonal complement of \p a.GetProjection(b). That is:
385 /// \code
386 /// a - a.GetProjection(b)
387 /// \endcode
388 inline GfVec4f
390 {
391  return a.GetComplement(b);
392 }
393 
394 /// Tests for equality within a given tolerance, returning \c true if the
395 /// length of the difference vector is less than or equal to \p tolerance.
396 inline bool
397 GfIsClose(GfVec4f const &v1, GfVec4f const &v2, double tolerance)
398 {
399  GfVec4f delta = v1 - v2;
400  return delta.GetLengthSq() <= tolerance * tolerance;
401 }
402 
403 
404 
406 
407 #endif // PXR_BASE_GF_VEC4F_H
Definition: vec4i.h:43
GfVec4f & operator*=(double s)
Multiplication by scalar.
Definition: vec4f.h:197
GfVec4f & operator/=(double s)
Division by scalar.
Definition: vec4f.h:213
friend GfVec4f operator-(GfVec4f const &l, GfVec4f const &r)
Definition: vec4f.h:192
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Definition: vec4f.h:263
static GfVec4f XAxis()
Create a unit vector along the X-axis.
Definition: vec4f.h:84
double GfSqrt(double f)
Definition: math.h:187
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
GfVec4f GetProjection(GfVec4f const &v) const
Definition: vec4f.h:232
GfVec4f GetComplement(GfVec4f const &b) const
Definition: vec4f.h:241
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GfVec4f & operator+=(GfVec4f const &other)
Addition.
Definition: vec4f.h:173
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
Definition: vec4d.h:45
float ScalarType
Scalar element type and dimension.
Definition: vec4f.h:49
**But if you need a result
Definition: thread.h:622
float * data()
Definition: vec4f.h:133
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
static GfVec4f WAxis()
Create a unit vector along the W-axis.
Definition: vec4f.h:102
friend GfVec4f operator+(GfVec4f const &l, GfVec4f const &r)
Definition: vec4f.h:180
GfVec4f GfGetNormalized(GfVec4f const &v, float eps=GF_MIN_VECTOR_LENGTH)
Definition: vec4f.h:369
GF_API std::ostream & operator<<(std::ostream &, GfVec4f const &)
float GfNormalize(GfVec4f *v, float eps=GF_MIN_VECTOR_LENGTH)
Definition: vec4f.h:360
Definition: vec4h.h:46
GfVec4f()=default
Default constructor does no initialization.
float GetLengthSq() const
Squared length.
Definition: vec4f.h:246
static GfVec4f YAxis()
Create a unit vector along the Y-axis.
Definition: vec4f.h:90
float const * GetArray() const
Definition: vec4f.h:134
GfVec4f operator-() const
Create a vec with negated elements.
Definition: vec4f.h:168
static GfVec4f ZAxis()
Create a unit vector along the Z-axis.
Definition: vec4f.h:96
constexpr GfVec4f(float value)
Initialize all elements to a single value.
Definition: vec4f.h:56
float GfGetLength(GfVec4f const &v)
Returns the geometric length of v.
Definition: vec4f.h:351
float GetLength() const
Length.
Definition: vec4f.h:251
GLint GLenum GLboolean normalized
Definition: glcorearb.h:872
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GfVec4f operator/(double s) const
Definition: vec4f.h:219
Definition: vec4f.h:45
constexpr GfVec4f(Scl const *p)
Construct with pointer to values.
Definition: vec4f.h:69
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
bool operator!=(GfVec4f const &other) const
Definition: vec4f.h:152
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
GfVec4f GetNormalized(float eps=GF_MIN_VECTOR_LENGTH) const
Definition: vec4f.h:271
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
float const & operator[](size_t i) const
Indexing.
Definition: vec4f.h:137
bool GfIsClose(GfVec4f const &v1, GfVec4f const &v2, double tolerance)
Definition: vec4f.h:397
GfVec4f GfGetProjection(GfVec4f const &a, GfVec4f const &b)
Definition: vec4f.h:379
float const * data() const
Direct data access.
Definition: vec4f.h:132
GfVec4f & Set(float const *a)
Set all elements with a pointer to data.
Definition: vec4f.h:127
float GfDot(GfVec4f const &v1, GfVec4f const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec4f.h:344
GLfloat GLfloat v1
Definition: glcorearb.h:817
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
static GfVec4f Axis(size_t i)
Definition: vec4f.h:110
friend size_t hash_value(GfVec4f const &vec)
Hash.
Definition: vec4f.h:141
GfVec4f GfGetComplement(GfVec4f const &a, GfVec4f const &b)
Definition: vec4f.h:389
GfVec4f operator*(double s) const
Definition: vec4f.h:204
GLboolean r
Definition: glcorearb.h:1222
constexpr GfVec4f(float s0, float s1, float s2, float s3)
Initialize all elements with explicit arguments.
Definition: vec4f.h:62
GfVec4f GfCompDiv(GfVec4f const &v1, GfVec4f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec4f.h:333
bool operator==(GfVec4f const &other) const
Equality comparison.
Definition: vec4f.h:146
static const size_t dimension
Definition: vec4f.h:50
float & operator[](size_t i)
Definition: vec4f.h:138
GfVec4f GfCompMult(GfVec4f const &v1, GfVec4f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec4f.h:322
#define GF_MIN_VECTOR_LENGTH
Definition: limits.h:17
friend GfVec4f operator*(double s, GfVec4f const &v)
Definition: vec4f.h:207
GfVec4f & operator-=(GfVec4f const &other)
Subtraction.
Definition: vec4f.h:185
#define GF_API
Definition: api.h:23