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