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 PXR_BASE_GF_VEC2D_H
29 #define PXR_BASE_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 #include "pxr/base/tf/hash.h"
41 
42 #include <cstddef>
43 #include <cmath>
44 
45 #include <iosfwd>
46 
48 
49 class GfVec2d;
50 
51 template <>
52 struct GfIsGfVec<class GfVec2d> { static const bool value = true; };
53 
54 /// \class GfVec2d
55 /// \ingroup group_gf_LinearAlgebra
56 ///
57 /// Basic type for a vector of 2 double components.
58 ///
59 /// Represents a vector of 2 components of type \c double.
60 /// It is intended to be fast and simple.
61 ///
62 class GfVec2d
63 {
64 public:
65  /// Scalar element type and dimension.
66  typedef double ScalarType;
67  static const size_t dimension = 2;
68 
69  /// Default constructor does no initialization.
70  GfVec2d() = default;
71 
72  /// Initialize all elements to a single value.
73  constexpr explicit GfVec2d(double value)
74  : _data{ value, value }
75  {
76  }
77 
78  /// Initialize all elements with explicit arguments.
79  constexpr GfVec2d(double s0, double s1)
80  : _data{ s0, s1 }
81  {
82  }
83 
84  /// Construct with pointer to values.
85  template <class Scl>
86  constexpr explicit GfVec2d(Scl const *p)
87  : _data{ p[0], p[1] }
88  {
89  }
90 
91  /// Implicitly convert from GfVec2f.
92  GfVec2d(class GfVec2f const &other);
93 
94  /// Implicitly convert from GfVec2h.
95  GfVec2d(class GfVec2h const &other);
96 
97  /// Implicitly convert from GfVec2i.
98  GfVec2d(class GfVec2i const &other);
99 
100  /// Create a unit vector along the X-axis.
101  static GfVec2d XAxis() {
102  GfVec2d result(0);
103  result[0] = 1;
104  return result;
105  }
106  /// Create a unit vector along the Y-axis.
107  static GfVec2d YAxis() {
108  GfVec2d 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 GfVec2d Axis(size_t i) {
116  GfVec2d result(0);
117  if (i < 2)
118  result[i] = 1;
119  return result;
120  }
121 
122  /// Set all elements with passed arguments.
123  GfVec2d &Set(double s0, double s1) {
124  _data[0] = s0;
125  _data[1] = s1;
126  return *this;
127  }
128 
129  /// Set all elements with a pointer to data.
130  GfVec2d &Set(double const *a) {
131  return Set(a[0], a[1]);
132  }
133 
134  /// Direct data access.
135  double const *data() const { return _data; }
136  double *data() { return _data; }
137  double const *GetArray() const { return data(); }
138 
139  /// Indexing.
140  double const &operator[](size_t i) const { return _data[i]; }
141  double &operator[](size_t i) { return _data[i]; }
142 
143  /// Hash.
144  friend inline size_t hash_value(GfVec2d const &vec) {
145  return TfHash::Combine(vec[0], vec[1]);
146  }
147 
148  /// Equality comparison.
149  bool operator==(GfVec2d const &other) const {
150  return _data[0] == other[0] &&
151  _data[1] == other[1];
152  }
153  bool operator!=(GfVec2d 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 GfVec2f 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  GfVec2d operator-() const {
170  return GfVec2d(-_data[0], -_data[1]);
171  }
172 
173  /// Addition.
174  GfVec2d &operator+=(GfVec2d const &other) {
175  _data[0] += other[0];
176  _data[1] += other[1];
177  return *this;
178  }
179  friend GfVec2d operator+(GfVec2d const &l, GfVec2d const &r) {
180  return GfVec2d(l) += r;
181  }
182 
183  /// Subtraction.
184  GfVec2d &operator-=(GfVec2d const &other) {
185  _data[0] -= other[0];
186  _data[1] -= other[1];
187  return *this;
188  }
189  friend GfVec2d operator-(GfVec2d const &l, GfVec2d const &r) {
190  return GfVec2d(l) -= r;
191  }
192 
193  /// Multiplication by scalar.
194  GfVec2d &operator*=(double s) {
195  _data[0] *= s;
196  _data[1] *= s;
197  return *this;
198  }
199  GfVec2d operator*(double s) const {
200  return GfVec2d(*this) *= s;
201  }
202  friend GfVec2d operator*(double s, GfVec2d const &v) {
203  return v * s;
204  }
205 
206  /// Division by scalar.
207  // TODO should divide by the scalar type.
208  GfVec2d &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  GfVec2d operator/(double s) const {
215  return *this * (1.0 / s);
216  }
217 
218  /// See GfDot().
219  double operator*(GfVec2d 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  GfVec2d GetProjection(GfVec2d 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  GfVec2d GetComplement(GfVec2d const &b) const {
237  return *this - this->GetProjection(b);
238  }
239 
240  /// Squared length.
241  double GetLengthSq() const {
242  return *this * *this;
243  }
244 
245  /// Length
246  double 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  double Normalize(double eps = GF_MIN_VECTOR_LENGTH) {
259  // TODO this seems suspect... suggest dividing by length so long as
260  // length is not zero.
261  double length = GetLength();
262  *this /= (length > eps) ? length : eps;
263  return length;
264  }
265 
267  GfVec2d normalized(*this);
268  normalized.Normalize(eps);
269  return normalized;
270  }
271 
272 
273 private:
274  double _data[2];
275 };
276 
277 /// Output a GfVec2d.
278 /// \ingroup group_gf_DebuggingOutput
279 GF_API std::ostream& operator<<(std::ostream &, GfVec2d const &);
280 
281 
283 
284 #include "pxr/base/gf/vec2f.h"
285 #include "pxr/base/gf/vec2h.h"
286 #include "pxr/base/gf/vec2i.h"
287 
289 
290 inline
291 GfVec2d::GfVec2d(class GfVec2f const &other)
292 {
293  _data[0] = other[0];
294  _data[1] = other[1];
295 }
296 inline
297 GfVec2d::GfVec2d(class GfVec2h const &other)
298 {
299  _data[0] = other[0];
300  _data[1] = other[1];
301 }
302 inline
303 GfVec2d::GfVec2d(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 GfVec2d
311 GfCompMult(GfVec2d const &v1, GfVec2d const &v2) {
312  return GfVec2d(
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 GfVec2d
320 GfCompDiv(GfVec2d const &v1, GfVec2d const &v2) {
321  return GfVec2d(
322  v1[0] / v2[0],
323  v1[1] / v2[1]
324  );
325 }
326 
327 /// Returns the dot (inner) product of two vectors.
328 inline double
329 GfDot(GfVec2d const &v1, GfVec2d const &v2) {
330  return v1 * v2;
331 }
332 
333 
334 /// Returns the geometric length of \c v.
335 inline double
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 double
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 GfVec2d
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 GfVec2d
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 GfVec2d
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(GfVec2d const &v1, GfVec2d const &v2, double tolerance)
383 {
384  GfVec2d delta = v1 - v2;
385  return delta.GetLengthSq() <= tolerance * tolerance;
386 }
387 
388 
389 
391 
392 #endif // PXR_BASE_GF_VEC2D_H
GfVec2d operator-() const
Create a vec with negated elements.
Definition: vec2d.h:169
double const * data() const
Direct data access.
Definition: vec2d.h:135
friend GfVec2d operator*(double s, GfVec2d const &v)
Definition: vec2d.h:202
double GfDot(GfVec2d const &v1, GfVec2d const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2d.h:329
GfVec2d GfGetProjection(GfVec2d const &a, GfVec2d const &b)
Definition: vec2d.h:364
double GfSqrt(double f)
Definition: math.h:80
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
const GLdouble * v
Definition: glcorearb.h:837
double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec2d.h:258
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GfVec2d operator*(double s) const
Definition: vec2d.h:199
friend GfVec2d operator-(GfVec2d const &l, GfVec2d const &r)
Definition: vec2d.h:189
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:153
static GfVec2d XAxis()
Create a unit vector along the X-axis.
Definition: vec2d.h:101
double * data()
Definition: vec2d.h:136
**But if you need a result
Definition: thread.h:613
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
double GetLength() const
Length.
Definition: vec2d.h:246
Definition: vec2d.h:62
bool GfIsClose(GfVec2d const &v1, GfVec2d const &v2, double tolerance)
Definition: vec2d.h:382
static const size_t dimension
Definition: vec2d.h:67
GfVec2d operator/(double s) const
Definition: vec2d.h:214
double & operator[](size_t i)
Definition: vec2d.h:141
GfVec2d GfGetNormalized(GfVec2d const &v, double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec2d.h:354
Definition: vec2h.h:63
constexpr GfVec2d(Scl const *p)
Construct with pointer to values.
Definition: vec2d.h:86
double const * GetArray() const
Definition: vec2d.h:137
constexpr GfVec2d(double s0, double s1)
Initialize all elements with explicit arguments.
Definition: vec2d.h:79
friend GfVec2d operator+(GfVec2d const &l, GfVec2d const &r)
Definition: vec2d.h:179
GfVec2d GfCompMult(GfVec2d const &v1, GfVec2d const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2d.h:311
GfVec2d & operator*=(double s)
Multiplication by scalar.
Definition: vec2d.h:194
double ScalarType
Scalar element type and dimension.
Definition: vec2d.h:66
constexpr GfVec2d(double value)
Initialize all elements to a single value.
Definition: vec2d.h:73
double GfNormalize(GfVec2d *v, double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec2d.h:345
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:208
GfVec2d & operator+=(GfVec2d const &other)
Addition.
Definition: vec2d.h:174
double GetLengthSq() const
Squared length.
Definition: vec2d.h:241
GfVec2d GetComplement(GfVec2d const &b) const
Definition: vec2d.h:236
static GfVec2d YAxis()
Create a unit vector along the Y-axis.
Definition: vec2d.h:107
double operator*(GfVec2d const &v) const
See GfDot().
Definition: vec2d.h:219
friend size_t hash_value(GfVec2d const &vec)
Hash.
Definition: vec2d.h:144
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:492
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
bool operator==(GfVec2d const &other) const
Equality comparison.
Definition: vec2d.h:149
static GfVec2d Axis(size_t i)
Definition: vec2d.h:115
GfVec2d()=default
Default constructor does no initialization.
Definition: vec2f.h:62
GfVec2d & Set(double s0, double s1)
Set all elements with passed arguments.
Definition: vec2d.h:123
GLfloat GLfloat v1
Definition: glcorearb.h:817
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GfVec2d GfCompDiv(GfVec2d const &v1, GfVec2d const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2d.h:320
double GfGetLength(GfVec2d const &v)
Returns the geometric length of v.
Definition: vec2d.h:336
Definition: core.h:1131
GLboolean r
Definition: glcorearb.h:1222
GfVec2d & Set(double const *a)
Set all elements with a pointer to data.
Definition: vec2d.h:130
GfVec2d GfGetComplement(GfVec2d const &a, GfVec2d const &b)
Definition: vec2d.h:374
double const & operator[](size_t i) const
Indexing.
Definition: vec2d.h:140
GfVec2d GetNormalized(double eps=GF_MIN_VECTOR_LENGTH) const
Definition: vec2d.h:266
GfVec2d GetProjection(GfVec2d const &v) const
Definition: vec2d.h:227
#define GF_MIN_VECTOR_LENGTH
Definition: limits.h:34
#define GF_API
Definition: api.h:40
GfVec2d & operator-=(GfVec2d const &other)
Subtraction.
Definition: vec2d.h:184