HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vec2d.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_VEC2D_H
12 #define PXR_BASE_GF_VEC2D_H
13 
14 /// \file gf/vec2d.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 GfVec2d;
33 
34 template <>
35 struct GfIsGfVec<class GfVec2d> { static const bool value = true; };
36 
37 /// \class GfVec2d
38 /// \ingroup group_gf_LinearAlgebra
39 ///
40 /// Basic type for a vector of 2 double components.
41 ///
42 /// Represents a vector of 2 components of type \c double.
43 /// It is intended to be fast and simple.
44 ///
45 class GfVec2d
46 {
47 public:
48  /// Scalar element type and dimension.
49  typedef double ScalarType;
50  static const size_t dimension = 2;
51 
52  /// Default constructor does no initialization.
53  GfVec2d() = default;
54 
55  /// Initialize all elements to a single value.
56  constexpr explicit GfVec2d(double value)
57  : _data{ value, value }
58  {
59  }
60 
61  /// Initialize all elements with explicit arguments.
62  constexpr GfVec2d(double s0, double s1)
63  : _data{ s0, s1 }
64  {
65  }
66 
67  /// Construct with pointer to values.
68  template <class Scl>
69  constexpr explicit GfVec2d(Scl const *p)
70  : _data{ p[0], p[1] }
71  {
72  }
73 
74  /// Implicitly convert from GfVec2f.
75  GfVec2d(class GfVec2f const &other);
76 
77  /// Implicitly convert from GfVec2h.
78  GfVec2d(class GfVec2h const &other);
79 
80  /// Implicitly convert from GfVec2i.
81  GfVec2d(class GfVec2i const &other);
82 
83  /// Create a unit vector along the X-axis.
84  static GfVec2d XAxis() {
85  GfVec2d result(0);
86  result[0] = 1;
87  return result;
88  }
89  /// Create a unit vector along the Y-axis.
90  static GfVec2d YAxis() {
91  GfVec2d 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 GfVec2d Axis(size_t i) {
99  GfVec2d result(0);
100  if (i < 2)
101  result[i] = 1;
102  return result;
103  }
104 
105  /// Set all elements with passed arguments.
106  GfVec2d &Set(double s0, double s1) {
107  _data[0] = s0;
108  _data[1] = s1;
109  return *this;
110  }
111 
112  /// Set all elements with a pointer to data.
113  GfVec2d &Set(double const *a) {
114  return Set(a[0], a[1]);
115  }
116 
117  /// Direct data access.
118  double const *data() const { return _data; }
119  double *data() { return _data; }
120  double const *GetArray() const { return data(); }
121 
122  /// Indexing.
123  double const &operator[](size_t i) const { return _data[i]; }
124  double &operator[](size_t i) { return _data[i]; }
125 
126  /// Hash.
127  friend inline size_t hash_value(GfVec2d const &vec) {
128  return TfHash::Combine(vec[0], vec[1]);
129  }
130 
131  /// Equality comparison.
132  bool operator==(GfVec2d const &other) const {
133  return _data[0] == other[0] &&
134  _data[1] == other[1];
135  }
136  bool operator!=(GfVec2d 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 GfVec2f 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  GfVec2d operator-() const {
153  return GfVec2d(-_data[0], -_data[1]);
154  }
155 
156  /// Addition.
157  GfVec2d &operator+=(GfVec2d const &other) {
158  _data[0] += other[0];
159  _data[1] += other[1];
160  return *this;
161  }
162  friend GfVec2d operator+(GfVec2d const &l, GfVec2d const &r) {
163  return GfVec2d(l) += r;
164  }
165 
166  /// Subtraction.
167  GfVec2d &operator-=(GfVec2d const &other) {
168  _data[0] -= other[0];
169  _data[1] -= other[1];
170  return *this;
171  }
172  friend GfVec2d operator-(GfVec2d const &l, GfVec2d const &r) {
173  return GfVec2d(l) -= r;
174  }
175 
176  /// Multiplication by scalar.
177  GfVec2d &operator*=(double s) {
178  _data[0] *= s;
179  _data[1] *= s;
180  return *this;
181  }
182  GfVec2d operator*(double s) const {
183  return GfVec2d(*this) *= s;
184  }
185  friend GfVec2d operator*(double s, GfVec2d const &v) {
186  return v * s;
187  }
188 
189  /// Division by scalar.
190  // TODO should divide by the scalar type.
191  GfVec2d &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  GfVec2d operator/(double s) const {
198  return *this * (1.0 / s);
199  }
200 
201  /// See GfDot().
202  double operator*(GfVec2d 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  GfVec2d GetProjection(GfVec2d 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  GfVec2d GetComplement(GfVec2d const &b) const {
220  return *this - this->GetProjection(b);
221  }
222 
223  /// Squared length.
224  double GetLengthSq() const {
225  return *this * *this;
226  }
227 
228  /// Length
229  double 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  double Normalize(double eps = GF_MIN_VECTOR_LENGTH) {
242  // TODO this seems suspect... suggest dividing by length so long as
243  // length is not zero.
244  double length = GetLength();
245  *this /= (length > eps) ? length : eps;
246  return length;
247  }
248 
250  GfVec2d normalized(*this);
251  normalized.Normalize(eps);
252  return normalized;
253  }
254 
255 
256 private:
257  double _data[2];
258 };
259 
260 /// Output a GfVec2d.
261 /// \ingroup group_gf_DebuggingOutput
262 GF_API std::ostream& operator<<(std::ostream &, GfVec2d const &);
263 
264 
266 
267 #include "pxr/base/gf/vec2f.h"
268 #include "pxr/base/gf/vec2h.h"
269 #include "pxr/base/gf/vec2i.h"
270 
272 
273 inline
274 GfVec2d::GfVec2d(class GfVec2f const &other)
275 {
276  _data[0] = other[0];
277  _data[1] = other[1];
278 }
279 inline
280 GfVec2d::GfVec2d(class GfVec2h const &other)
281 {
282  _data[0] = other[0];
283  _data[1] = other[1];
284 }
285 inline
286 GfVec2d::GfVec2d(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 GfVec2d
294 GfCompMult(GfVec2d const &v1, GfVec2d const &v2) {
295  return GfVec2d(
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 GfVec2d
303 GfCompDiv(GfVec2d const &v1, GfVec2d const &v2) {
304  return GfVec2d(
305  v1[0] / v2[0],
306  v1[1] / v2[1]
307  );
308 }
309 
310 /// Returns the dot (inner) product of two vectors.
311 inline double
312 GfDot(GfVec2d const &v1, GfVec2d const &v2) {
313  return v1 * v2;
314 }
315 
316 
317 /// Returns the geometric length of \c v.
318 inline double
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 double
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 GfVec2d
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 GfVec2d
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 GfVec2d
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(GfVec2d const &v1, GfVec2d const &v2, double tolerance)
366 {
367  GfVec2d delta = v1 - v2;
368  return delta.GetLengthSq() <= tolerance * tolerance;
369 }
370 
371 
372 
374 
375 #endif // PXR_BASE_GF_VEC2D_H
GfVec2d operator-() const
Create a vec with negated elements.
Definition: vec2d.h:152
double const * data() const
Direct data access.
Definition: vec2d.h:118
friend GfVec2d operator*(double s, GfVec2d const &v)
Definition: vec2d.h:185
double GfDot(GfVec2d const &v1, GfVec2d const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2d.h:312
GfVec2d GfGetProjection(GfVec2d const &a, GfVec2d const &b)
Definition: vec2d.h:347
double GfSqrt(double f)
Definition: math.h:187
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
const GLdouble * v
Definition: glcorearb.h:837
double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec2d.h:241
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GfVec2d operator*(double s) const
Definition: vec2d.h:182
friend GfVec2d operator-(GfVec2d const &l, GfVec2d const &r)
Definition: vec2d.h:172
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
GF_API std::ostream & operator<<(std::ostream &, GfVec2d const &)
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
bool operator!=(GfVec2d const &other) const
Definition: vec2d.h:136
static GfVec2d XAxis()
Create a unit vector along the X-axis.
Definition: vec2d.h:84
double * data()
Definition: vec2d.h:119
**But if you need a result
Definition: thread.h:622
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
double GetLength() const
Length.
Definition: vec2d.h:229
Definition: vec2d.h:45
bool GfIsClose(GfVec2d const &v1, GfVec2d const &v2, double tolerance)
Definition: vec2d.h:365
static const size_t dimension
Definition: vec2d.h:50
GfVec2d operator/(double s) const
Definition: vec2d.h:197
double & operator[](size_t i)
Definition: vec2d.h:124
GfVec2d GfGetNormalized(GfVec2d const &v, double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec2d.h:337
Definition: vec2h.h:46
constexpr GfVec2d(Scl const *p)
Construct with pointer to values.
Definition: vec2d.h:69
double const * GetArray() const
Definition: vec2d.h:120
constexpr GfVec2d(double s0, double s1)
Initialize all elements with explicit arguments.
Definition: vec2d.h:62
friend GfVec2d operator+(GfVec2d const &l, GfVec2d const &r)
Definition: vec2d.h:162
GfVec2d GfCompMult(GfVec2d const &v1, GfVec2d const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2d.h:294
GfVec2d & operator*=(double s)
Multiplication by scalar.
Definition: vec2d.h:177
double ScalarType
Scalar element type and dimension.
Definition: vec2d.h:49
constexpr GfVec2d(double value)
Initialize all elements to a single value.
Definition: vec2d.h:56
double GfNormalize(GfVec2d *v, double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec2d.h:328
GLint GLenum GLboolean normalized
Definition: glcorearb.h:872
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GfVec2d & operator/=(double s)
Division by scalar.
Definition: vec2d.h:191
GfVec2d & operator+=(GfVec2d const &other)
Addition.
Definition: vec2d.h:157
double GetLengthSq() const
Squared length.
Definition: vec2d.h:224
GfVec2d GetComplement(GfVec2d const &b) const
Definition: vec2d.h:219
static GfVec2d YAxis()
Create a unit vector along the Y-axis.
Definition: vec2d.h:90
double operator*(GfVec2d const &v) const
See GfDot().
Definition: vec2d.h:202
friend size_t hash_value(GfVec2d const &vec)
Hash.
Definition: vec2d.h:127
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
bool operator==(GfVec2d const &other) const
Equality comparison.
Definition: vec2d.h:132
static GfVec2d Axis(size_t i)
Definition: vec2d.h:98
GfVec2d()=default
Default constructor does no initialization.
Definition: vec2f.h:45
GfVec2d & Set(double s0, double s1)
Set all elements with passed arguments.
Definition: vec2d.h:106
GLfloat GLfloat v1
Definition: glcorearb.h:817
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GfVec2d GfCompDiv(GfVec2d const &v1, GfVec2d const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2d.h:303
double GfGetLength(GfVec2d const &v)
Returns the geometric length of v.
Definition: vec2d.h:319
GLboolean r
Definition: glcorearb.h:1222
GfVec2d & Set(double const *a)
Set all elements with a pointer to data.
Definition: vec2d.h:113
GfVec2d GfGetComplement(GfVec2d const &a, GfVec2d const &b)
Definition: vec2d.h:357
double const & operator[](size_t i) const
Indexing.
Definition: vec2d.h:123
GfVec2d GetNormalized(double eps=GF_MIN_VECTOR_LENGTH) const
Definition: vec2d.h:249
GfVec2d GetProjection(GfVec2d const &v) const
Definition: vec2d.h:210
#define GF_MIN_VECTOR_LENGTH
Definition: limits.h:17
#define GF_API
Definition: api.h:23
GfVec2d & operator-=(GfVec2d const &other)
Subtraction.
Definition: vec2d.h:167