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