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