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 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 //
24 ////////////////////////////////////////////////////////////////////////
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_VEC4F_H
29 #define PXR_BASE_GF_VEC4F_H
30 
31 /// \file gf/vec4f.h
32 /// \ingroup group_gf_LinearAlgebra
33 
34 #include "pxr/pxr.h"
35 #include "pxr/base/tf/diagnostic.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 
48 
49 class GfVec4f;
50 
51 template <>
52 struct GfIsGfVec<class GfVec4f> { static const bool value = true; };
53 
54 /// \class GfVec4f
55 /// \ingroup group_gf_LinearAlgebra
56 ///
57 /// Basic type for a vector of 4 float components.
58 ///
59 /// Represents a vector of 4 components of type \c float.
60 /// It is intended to be fast and simple.
61 ///
62 class GfVec4f
63 {
64 public:
65  /// Scalar element type and dimension.
66  typedef float ScalarType;
67  static const size_t dimension = 4;
68 
69  /// Default constructor does no initialization.
70  GfVec4f() = default;
71 
72  /// Initialize all elements to a single value.
73  constexpr explicit GfVec4f(float value)
74  : _data{ value, value, value, value }
75  {
76  }
77 
78  /// Initialize all elements with explicit arguments.
79  constexpr GfVec4f(float s0, float s1, float s2, float s3)
80  : _data{ s0, s1, s2, s3 }
81  {
82  }
83 
84  /// Construct with pointer to values.
85  template <class Scl>
86  constexpr explicit GfVec4f(Scl const *p)
87  : _data{ p[0], p[1], p[2], p[3] }
88  {
89  }
90 
91  /// Construct from GfVec4d.
92  explicit GfVec4f(class GfVec4d const &other);
93 
94  /// Implicitly convert from GfVec4h.
95  GfVec4f(class GfVec4h const &other);
96 
97  /// Implicitly convert from GfVec4i.
98  GfVec4f(class GfVec4i const &other);
99 
100  /// Create a unit vector along the X-axis.
101  static GfVec4f XAxis() {
102  GfVec4f result(0);
103  result[0] = 1;
104  return result;
105  }
106  /// Create a unit vector along the Y-axis.
107  static GfVec4f YAxis() {
108  GfVec4f result(0);
109  result[1] = 1;
110  return result;
111  }
112  /// Create a unit vector along the Z-axis.
113  static GfVec4f ZAxis() {
114  GfVec4f result(0);
115  result[2] = 1;
116  return result;
117  }
118  /// Create a unit vector along the W-axis.
119  static GfVec4f WAxis() {
120  GfVec4f result(0);
121  result[3] = 1;
122  return result;
123  }
124 
125  /// Create a unit vector along the i-th axis, zero-based. Return the zero
126  /// vector if \p i is greater than or equal to 4.
127  static GfVec4f Axis(size_t i) {
128  GfVec4f result(0);
129  if (i < 4)
130  result[i] = 1;
131  return result;
132  }
133 
134  /// Set all elements with passed arguments.
135  GfVec4f &Set(float s0, float s1, float s2, float s3) {
136  _data[0] = s0;
137  _data[1] = s1;
138  _data[2] = s2;
139  _data[3] = s3;
140  return *this;
141  }
142 
143  /// Set all elements with a pointer to data.
144  GfVec4f &Set(float const *a) {
145  return Set(a[0], a[1], a[2], a[3]);
146  }
147 
148  /// Direct data access.
149  float const *data() const { return _data; }
150  float *data() { return _data; }
151  float const *GetArray() const { return data(); }
152 
153  /// Indexing.
154  float const &operator[](size_t i) const { return _data[i]; }
155  float &operator[](size_t i) { return _data[i]; }
156 
157  /// Hash.
158  friend inline size_t hash_value(GfVec4f const &vec) {
159  return TfHash::Combine(vec[0], vec[1], vec[2], vec[3]);
160  }
161 
162  /// Equality comparison.
163  bool operator==(GfVec4f const &other) const {
164  return _data[0] == other[0] &&
165  _data[1] == other[1] &&
166  _data[2] == other[2] &&
167  _data[3] == other[3];
168  }
169  bool operator!=(GfVec4f const &other) const {
170  return !(*this == other);
171  }
172 
173  // TODO Add inequality for other vec types...
174  /// Equality comparison.
175  GF_API
176  bool operator==(class GfVec4d const &other) const;
177  /// Equality comparison.
178  GF_API
179  bool operator==(class GfVec4h const &other) const;
180  /// Equality comparison.
181  GF_API
182  bool operator==(class GfVec4i const &other) const;
183 
184  /// Create a vec with negated elements.
185  GfVec4f operator-() const {
186  return GfVec4f(-_data[0], -_data[1], -_data[2], -_data[3]);
187  }
188 
189  /// Addition.
190  GfVec4f &operator+=(GfVec4f const &other) {
191  _data[0] += other[0];
192  _data[1] += other[1];
193  _data[2] += other[2];
194  _data[3] += other[3];
195  return *this;
196  }
197  friend GfVec4f operator+(GfVec4f const &l, GfVec4f const &r) {
198  return GfVec4f(l) += r;
199  }
200 
201  /// Subtraction.
202  GfVec4f &operator-=(GfVec4f const &other) {
203  _data[0] -= other[0];
204  _data[1] -= other[1];
205  _data[2] -= other[2];
206  _data[3] -= other[3];
207  return *this;
208  }
209  friend GfVec4f operator-(GfVec4f const &l, GfVec4f const &r) {
210  return GfVec4f(l) -= r;
211  }
212 
213  /// Multiplication by scalar.
214  GfVec4f &operator*=(double s) {
215  _data[0] *= s;
216  _data[1] *= s;
217  _data[2] *= s;
218  _data[3] *= s;
219  return *this;
220  }
221  GfVec4f operator*(double s) const {
222  return GfVec4f(*this) *= s;
223  }
224  friend GfVec4f operator*(double s, GfVec4f const &v) {
225  return v * s;
226  }
227 
228  /// Division by scalar.
229  // TODO should divide by the scalar type.
230  GfVec4f &operator/=(double s) {
231  // TODO This should not multiply by 1/s, it should do the division.
232  // Doing the division is more numerically stable when s is close to
233  // zero.
234  return *this *= (1.0 / s);
235  }
236  GfVec4f operator/(double s) const {
237  return *this * (1.0 / s);
238  }
239 
240  /// See GfDot().
241  float operator*(GfVec4f const &v) const {
242  return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
243  }
244 
245  /// Returns the projection of \p this onto \p v. That is:
246  /// \code
247  /// v * (*this * v)
248  /// \endcode
249  GfVec4f GetProjection(GfVec4f const &v) const {
250  return v * (*this * v);
251  }
252 
253  /// Returns the orthogonal complement of \p this->GetProjection(b).
254  /// That is:
255  /// \code
256  /// *this - this->GetProjection(b)
257  /// \endcode
258  GfVec4f GetComplement(GfVec4f const &b) const {
259  return *this - this->GetProjection(b);
260  }
261 
262  /// Squared length.
263  float GetLengthSq() const {
264  return *this * *this;
265  }
266 
267  /// Length
268  float GetLength() const {
269  return GfSqrt(GetLengthSq());
270  }
271 
272  /// Normalizes the vector in place to unit length, returning the
273  /// length before normalization. If the length of the vector is
274  /// smaller than \p eps, then the vector is set to vector/\c eps.
275  /// The original length of the vector is returned. See also GfNormalize().
276  ///
277  /// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
278  /// By tickling the code, it no longer tries to write into
279  /// an illegal memory address (in the code section of memory).
280  float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
281  // TODO this seems suspect... suggest dividing by length so long as
282  // length is not zero.
283  float length = GetLength();
284  *this /= (length > eps) ? length : eps;
285  return length;
286  }
287 
289  GfVec4f normalized(*this);
290  normalized.Normalize(eps);
291  return normalized;
292  }
293 
294 
295 private:
296  float _data[4];
297 };
298 
299 /// Output a GfVec4f.
300 /// \ingroup group_gf_DebuggingOutput
301 GF_API std::ostream& operator<<(std::ostream &, GfVec4f const &);
302 
303 
305 
306 #include "pxr/base/gf/vec4d.h"
307 #include "pxr/base/gf/vec4h.h"
308 #include "pxr/base/gf/vec4i.h"
309 
311 
312 inline
313 GfVec4f::GfVec4f(class GfVec4d const &other)
314 {
315  _data[0] = other[0];
316  _data[1] = other[1];
317  _data[2] = other[2];
318  _data[3] = other[3];
319 }
320 inline
321 GfVec4f::GfVec4f(class GfVec4h const &other)
322 {
323  _data[0] = other[0];
324  _data[1] = other[1];
325  _data[2] = other[2];
326  _data[3] = other[3];
327 }
328 inline
329 GfVec4f::GfVec4f(class GfVec4i const &other)
330 {
331  _data[0] = other[0];
332  _data[1] = other[1];
333  _data[2] = other[2];
334  _data[3] = other[3];
335 }
336 
337 /// Returns component-wise multiplication of vectors \p v1 and \p v2.
338 inline GfVec4f
339 GfCompMult(GfVec4f const &v1, GfVec4f const &v2) {
340  return GfVec4f(
341  v1[0] * v2[0],
342  v1[1] * v2[1],
343  v1[2] * v2[2],
344  v1[3] * v2[3]
345  );
346 }
347 
348 /// Returns component-wise quotient of vectors \p v1 and \p v2.
349 inline GfVec4f
350 GfCompDiv(GfVec4f const &v1, GfVec4f const &v2) {
351  return GfVec4f(
352  v1[0] / v2[0],
353  v1[1] / v2[1],
354  v1[2] / v2[2],
355  v1[3] / v2[3]
356  );
357 }
358 
359 /// Returns the dot (inner) product of two vectors.
360 inline float
361 GfDot(GfVec4f const &v1, GfVec4f const &v2) {
362  return v1 * v2;
363 }
364 
365 
366 /// Returns the geometric length of \c v.
367 inline float
369 {
370  return v.GetLength();
371 }
372 
373 /// Normalizes \c *v in place to unit length, returning the length before
374 /// normalization. If the length of \c *v is smaller than \p eps then \c *v is
375 /// set to \c *v/eps. The original length of \c *v is returned.
376 inline float
378 {
379  return v->Normalize(eps);
380 }
381 
382 /// Returns a normalized (unit-length) vector with the same direction as \p v.
383 /// If the length of this vector is smaller than \p eps, the vector divided by
384 /// \p eps is returned.
385 inline GfVec4f
387 {
388  return v.GetNormalized(eps);
389 }
390 
391 /// Returns the projection of \p a onto \p b. That is:
392 /// \code
393 /// b * (a * b)
394 /// \endcode
395 inline GfVec4f
397 {
398  return a.GetProjection(b);
399 }
400 
401 /// Returns the orthogonal complement of \p a.GetProjection(b). That is:
402 /// \code
403 /// a - a.GetProjection(b)
404 /// \endcode
405 inline GfVec4f
407 {
408  return a.GetComplement(b);
409 }
410 
411 /// Tests for equality within a given tolerance, returning \c true if the
412 /// length of the difference vector is less than or equal to \p tolerance.
413 inline bool
414 GfIsClose(GfVec4f const &v1, GfVec4f const &v2, double tolerance)
415 {
416  GfVec4f delta = v1 - v2;
417  return delta.GetLengthSq() <= tolerance * tolerance;
418 }
419 
420 
421 
423 
424 #endif // PXR_BASE_GF_VEC4F_H
Definition: vec4i.h:60
GfVec4f & operator*=(double s)
Multiplication by scalar.
Definition: vec4f.h:214
GfVec4f & operator/=(double s)
Division by scalar.
Definition: vec4f.h:230
friend GfVec4f operator-(GfVec4f const &l, GfVec4f const &r)
Definition: vec4f.h:209
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Definition: vec4f.h:280
static GfVec4f XAxis()
Create a unit vector along the X-axis.
Definition: vec4f.h:101
double GfSqrt(double f)
Definition: math.h:80
*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:623
GfVec4f GetProjection(GfVec4f const &v) const
Definition: vec4f.h:249
GfVec4f GetComplement(GfVec4f const &b) const
Definition: vec4f.h:258
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GfVec4f & operator+=(GfVec4f const &other)
Addition.
Definition: vec4f.h:190
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:62
float ScalarType
Scalar element type and dimension.
Definition: vec4f.h:66
**But if you need a result
Definition: thread.h:613
float * data()
Definition: vec4f.h:150
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
static GfVec4f WAxis()
Create a unit vector along the W-axis.
Definition: vec4f.h:119
friend GfVec4f operator+(GfVec4f const &l, GfVec4f const &r)
Definition: vec4f.h:197
GfVec4f GfGetNormalized(GfVec4f const &v, float eps=GF_MIN_VECTOR_LENGTH)
Definition: vec4f.h:386
GF_API std::ostream & operator<<(std::ostream &, GfVec4f const &)
float GfNormalize(GfVec4f *v, float eps=GF_MIN_VECTOR_LENGTH)
Definition: vec4f.h:377
Definition: vec4h.h:63
GfVec4f()=default
Default constructor does no initialization.
float GetLengthSq() const
Squared length.
Definition: vec4f.h:263
static GfVec4f YAxis()
Create a unit vector along the Y-axis.
Definition: vec4f.h:107
float const * GetArray() const
Definition: vec4f.h:151
GfVec4f operator-() const
Create a vec with negated elements.
Definition: vec4f.h:185
static GfVec4f ZAxis()
Create a unit vector along the Z-axis.
Definition: vec4f.h:113
constexpr GfVec4f(float value)
Initialize all elements to a single value.
Definition: vec4f.h:73
float GfGetLength(GfVec4f const &v)
Returns the geometric length of v.
Definition: vec4f.h:368
float GetLength() const
Length.
Definition: vec4f.h:268
GLint GLenum GLboolean normalized
Definition: glcorearb.h:872
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GfVec4f operator/(double s) const
Definition: vec4f.h:236
Definition: vec4f.h:62
constexpr GfVec4f(Scl const *p)
Construct with pointer to values.
Definition: vec4f.h:86
GfVec4f & Set(float s0, float s1, float s2, float s3)
Set all elements with passed arguments.
Definition: vec4f.h:135
float operator*(GfVec4f const &v) const
See GfDot().
Definition: vec4f.h:241
bool operator!=(GfVec4f const &other) const
Definition: vec4f.h:169
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
GfVec4f GetNormalized(float eps=GF_MIN_VECTOR_LENGTH) const
Definition: vec4f.h:288
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
float const & operator[](size_t i) const
Indexing.
Definition: vec4f.h:154
bool GfIsClose(GfVec4f const &v1, GfVec4f const &v2, double tolerance)
Definition: vec4f.h:414
GfVec4f GfGetProjection(GfVec4f const &a, GfVec4f const &b)
Definition: vec4f.h:396
float const * data() const
Direct data access.
Definition: vec4f.h:149
GfVec4f & Set(float const *a)
Set all elements with a pointer to data.
Definition: vec4f.h:144
float GfDot(GfVec4f const &v1, GfVec4f const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec4f.h:361
GLfloat GLfloat v1
Definition: glcorearb.h:817
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
static GfVec4f Axis(size_t i)
Definition: vec4f.h:127
friend size_t hash_value(GfVec4f const &vec)
Hash.
Definition: vec4f.h:158
Definition: core.h:1131
GfVec4f GfGetComplement(GfVec4f const &a, GfVec4f const &b)
Definition: vec4f.h:406
GfVec4f operator*(double s) const
Definition: vec4f.h:221
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:79
GfVec4f GfCompDiv(GfVec4f const &v1, GfVec4f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec4f.h:350
bool operator==(GfVec4f const &other) const
Equality comparison.
Definition: vec4f.h:163
static const size_t dimension
Definition: vec4f.h:67
float & operator[](size_t i)
Definition: vec4f.h:155
GfVec4f GfCompMult(GfVec4f const &v1, GfVec4f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec4f.h:339
#define GF_MIN_VECTOR_LENGTH
Definition: limits.h:34
friend GfVec4f operator*(double s, GfVec4f const &v)
Definition: vec4f.h:224
GfVec4f & operator-=(GfVec4f const &other)
Subtraction.
Definition: vec4f.h:202
#define GF_API
Definition: api.h:40