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 //
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 
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 <boost/functional/hash.hpp>
42 
43 #include <cstddef>
44 #include <cmath>
45 
46 #include <iosfwd>
47 
48 PXR_NAMESPACE_OPEN_SCOPE
49 
50 class GfVec2d;
51 
52 template <>
53 struct GfIsGfVec<class GfVec2d> { static const bool value = true; };
54 
63 class GfVec2d
64 {
65 public:
67  typedef double ScalarType;
68  static const size_t dimension = 2;
69 
71  GfVec2d() = default;
72 
74  constexpr explicit GfVec2d(double value)
75  : _data{ value, value }
76  {
77  }
78 
80  constexpr GfVec2d(double s0, double s1)
81  : _data{ s0, s1 }
82  {
83  }
84 
86  template <class Scl>
87  constexpr explicit GfVec2d(Scl const *p)
88  : _data{ p[0], p[1] }
89  {
90  }
91 
93  GfVec2d(class GfVec2f const &other);
94 
96  GfVec2d(class GfVec2h const &other);
97 
99  GfVec2d(class GfVec2i const &other);
100 
102  static GfVec2d XAxis() {
103  GfVec2d result(0);
104  result[0] = 1;
105  return result;
106  }
108  static GfVec2d YAxis() {
109  GfVec2d result(0);
110  result[1] = 1;
111  return result;
112  }
113 
116  static GfVec2d Axis(size_t i) {
117  GfVec2d result(0);
118  if (i < 2)
119  result[i] = 1;
120  return result;
121  }
122 
124  GfVec2d &Set(double s0, double s1) {
125  _data[0] = s0;
126  _data[1] = s1;
127  return *this;
128  }
129 
131  GfVec2d &Set(double const *a) {
132  return Set(a[0], a[1]);
133  }
134 
136  double const *data() const { return _data; }
137  double *data() { return _data; }
138  double const *GetArray() const { return data(); }
139 
141  double const &operator[](size_t i) const { return _data[i]; }
142  double &operator[](size_t i) { return _data[i]; }
143 
145  friend inline size_t hash_value(GfVec2d const &vec) {
146  size_t h = 0;
147  boost::hash_combine(h, vec[0]);
148  boost::hash_combine(h, vec[1]);
149  return h;
150  }
151 
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...
163  GF_API
164  bool operator==(class GfVec2f const &other) const;
166  GF_API
167  bool operator==(class GfVec2h const &other) const;
169  GF_API
170  bool operator==(class GfVec2i const &other) const;
171 
173  GfVec2d operator-() const {
174  return GfVec2d(-_data[0], -_data[1]);
175  }
176 
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 
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 
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 
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 
223  double operator*(GfVec2d const &v) const {
224  return _data[0] * v[0] + _data[1] * v[1];
225  }
226 
231  GfVec2d GetProjection(GfVec2d const &v) const {
232  return v * (*this * v);
233  }
234 
240  GfVec2d GetComplement(GfVec2d const &b) const {
241  return *this - this->GetProjection(b);
242  }
243 
245  double GetLengthSq() const {
246  return *this * *this;
247  }
248 
250  double GetLength() const {
251  // TODO should use GfSqrt.
252  return sqrt(GetLengthSq());
253  }
254 
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 
271  GfVec2d GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const {
272  GfVec2d normalized(*this);
273  normalized.Normalize(eps);
274  return normalized;
275  }
276 
277 
278 private:
279  double _data[2];
280 };
281 
284 GF_API std::ostream& operator<<(std::ostream &, GfVec2d const &);
285 
286 
287 PXR_NAMESPACE_CLOSE_SCOPE
288 
289 #include "pxr/base/gf/vec2f.h"
290 #include "pxr/base/gf/vec2h.h"
291 #include "pxr/base/gf/vec2i.h"
292 
293 PXR_NAMESPACE_OPEN_SCOPE
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 
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 
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 
333 inline double
334 GfDot(GfVec2d const &v1, GfVec2d const &v2) {
335  return v1 * v2;
336 }
337 
338 
340 inline double
342 {
343  return v.GetLength();
344 }
345 
349 inline double
351 {
352  return v->Normalize(eps);
353 }
354 
358 inline GfVec2d
360 {
361  return v.GetNormalized(eps);
362 }
363 
368 inline GfVec2d
369 GfGetProjection(GfVec2d const &a, GfVec2d const &b)
370 {
371  return a.GetProjection(b);
372 }
373 
378 inline GfVec2d
379 GfGetComplement(GfVec2d const &a, GfVec2d const &b)
380 {
381  return a.GetComplement(b);
382 }
383 
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 
395 PXR_NAMESPACE_CLOSE_SCOPE
396 
397 #endif // GF_VEC2D_H
GfVec2d & operator+=(GfVec2d const &other)
Addition.
Definition: vec2d.h:178
GfVec2d GfGetProjection(GfVec2d const &a, GfVec2d const &b)
Returns the projection of a onto b.
Definition: vec2d.h:369
bool GfIsClose(double a, double b, double epsilon)
Returns true if a and b are with epsilon of each other.
Definition: math.h:39
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
double operator*(GfVec2d const &v) const
See GfDot().
Definition: vec2d.h:223
static GfVec2d YAxis()
Create a unit vector along the Y-axis.
Definition: vec2d.h:108
A metafunction with a static const bool member &#39;value&#39; that is true for GfVec types, like GfVec2i, GfVec4d, etc and false for all other types.
Definition: traits.h:34
GfVec2d operator-() const
Create a vec with negated elements.
Definition: vec2d.h:173
Basic type for a vector of 2 double components.
Definition: vec2d.h:63
double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec2d.h:263
bool operator==(GfVec2d const &other) const
Equality comparison.
Definition: vec2d.h:153
GfVec2d GfGetNormalized(GfVec2d const &v, double eps=GF_MIN_VECTOR_LENGTH)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec2d.h:359
GfVec2d & operator-=(GfVec2d const &other)
Subtraction.
Definition: vec2d.h:188
Basic type for a vector of 2 GfHalf components.
Definition: vec2h.h:64
GfVec2d GfCompMult(GfVec2d const &v1, GfVec2d const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2d.h:316
constexpr GfVec2d(double value)
Initialize all elements to a single value.
Definition: vec2d.h:74
double GetLengthSq() const
Squared length.
Definition: vec2d.h:245
double GfNormalize(GfVec2d *v, double eps=GF_MIN_VECTOR_LENGTH)
Normalizes *v in place to unit length, returning the length before normalization. ...
Definition: vec2d.h:350
GfVec2d & Set(double const *a)
Set all elements with a pointer to data.
Definition: vec2d.h:131
constexpr GfVec2d(Scl const *p)
Construct with pointer to values.
Definition: vec2d.h:87
GfVec2d & operator*=(double s)
Multiplication by scalar.
Definition: vec2d.h:198
GfVec2d GetProjection(GfVec2d const &v) const
Returns the projection of this onto v.
Definition: vec2d.h:231
static GfVec2d XAxis()
Create a unit vector along the X-axis.
Definition: vec2d.h:102
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
friend size_t hash_value(GfVec2d const &vec)
Hash.
Definition: vec2d.h:145
GfVec2d & operator/=(double s)
Division by scalar.
Definition: vec2d.h:212
GfVec2d & Set(double s0, double s1)
Set all elements with passed arguments.
Definition: vec2d.h:124
Basic type for a vector of 2 float components.
Definition: vec2f.h:63
constexpr GfVec2d(double s0, double s1)
Initialize all elements with explicit arguments.
Definition: vec2d.h:80
static GfVec2d Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec2d.h:116
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
double const & operator[](size_t i) const
Indexing.
Definition: vec2d.h:141
GfVec2d GfGetComplement(GfVec2d const &a, GfVec2d const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec2d.h:379
double GetLength() const
Length.
Definition: vec2d.h:250
double ScalarType
Scalar element type and dimension.
Definition: vec2d.h:67
GfVec2d()=default
Default constructor does no initialization.
#define GF_MIN_VECTOR_LENGTH
This constant is used to determine whether the length of a vector is too small to handle accurately...
Definition: limits.h:34
GfHalf GfDot(GfHalf a, GfHalf b)
Returns the dot (inner) product of two vectors.
Definition: half.h:55
GfVec2d GetComplement(GfVec2d const &b) const
Returns the orthogonal complement of this-&gt;GetProjection(b).
Definition: vec2d.h:240
double const * data() const
Direct data access.
Definition: vec2d.h:136