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 PXR_BASE_GF_VEC2D_H
29 #define PXR_BASE_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 #include "pxr/base/tf/hash.h"
41 
42 #include <cstddef>
43 #include <cmath>
44 
45 #include <iosfwd>
46 
47 PXR_NAMESPACE_OPEN_SCOPE
48 
49 class GfVec2d;
50 
51 template <>
52 struct GfIsGfVec<class GfVec2d> { static const bool value = true; };
53 
62 class GfVec2d
63 {
64 public:
66  typedef double ScalarType;
67  static const size_t dimension = 2;
68 
70  GfVec2d() = default;
71 
73  constexpr explicit GfVec2d(double value)
74  : _data{ value, value }
75  {
76  }
77 
79  constexpr GfVec2d(double s0, double s1)
80  : _data{ s0, s1 }
81  {
82  }
83 
85  template <class Scl>
86  constexpr explicit GfVec2d(Scl const *p)
87  : _data{ p[0], p[1] }
88  {
89  }
90 
92  GfVec2d(class GfVec2f const &other);
93 
95  GfVec2d(class GfVec2h const &other);
96 
98  GfVec2d(class GfVec2i const &other);
99 
101  static GfVec2d XAxis() {
102  GfVec2d result(0);
103  result[0] = 1;
104  return result;
105  }
107  static GfVec2d YAxis() {
108  GfVec2d result(0);
109  result[1] = 1;
110  return result;
111  }
112 
115  static GfVec2d Axis(size_t i) {
116  GfVec2d result(0);
117  if (i < 2)
118  result[i] = 1;
119  return result;
120  }
121 
123  GfVec2d &Set(double s0, double s1) {
124  _data[0] = s0;
125  _data[1] = s1;
126  return *this;
127  }
128 
130  GfVec2d &Set(double const *a) {
131  return Set(a[0], a[1]);
132  }
133 
135  double const *data() const { return _data; }
136  double *data() { return _data; }
137  double const *GetArray() const { return data(); }
138 
140  double const &operator[](size_t i) const { return _data[i]; }
141  double &operator[](size_t i) { return _data[i]; }
142 
144  friend inline size_t hash_value(GfVec2d const &vec) {
145  return TfHash::Combine(vec[0], vec[1]);
146  }
147 
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...
159  GF_API
160  bool operator==(class GfVec2f const &other) const;
162  GF_API
163  bool operator==(class GfVec2h const &other) const;
165  GF_API
166  bool operator==(class GfVec2i const &other) const;
167 
169  GfVec2d operator-() const {
170  return GfVec2d(-_data[0], -_data[1]);
171  }
172 
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 
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 
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 
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 
219  double operator*(GfVec2d const &v) const {
220  return _data[0] * v[0] + _data[1] * v[1];
221  }
222 
227  GfVec2d GetProjection(GfVec2d const &v) const {
228  return v * (*this * v);
229  }
230 
236  GfVec2d GetComplement(GfVec2d const &b) const {
237  return *this - this->GetProjection(b);
238  }
239 
241  double GetLengthSq() const {
242  return *this * *this;
243  }
244 
246  double GetLength() const {
247  return GfSqrt(GetLengthSq());
248  }
249 
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 
266  GfVec2d GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const {
267  GfVec2d normalized(*this);
268  normalized.Normalize(eps);
269  return normalized;
270  }
271 
272 
273 private:
274  double _data[2];
275 };
276 
279 GF_API std::ostream& operator<<(std::ostream &, GfVec2d const &);
280 
281 
282 PXR_NAMESPACE_CLOSE_SCOPE
283 
284 #include "pxr/base/gf/vec2f.h"
285 #include "pxr/base/gf/vec2h.h"
286 #include "pxr/base/gf/vec2i.h"
287 
288 PXR_NAMESPACE_OPEN_SCOPE
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 
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 
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 
328 inline double
329 GfDot(GfVec2d const &v1, GfVec2d const &v2) {
330  return v1 * v2;
331 }
332 
333 
335 inline double
337 {
338  return v.GetLength();
339 }
340 
344 inline double
346 {
347  return v->Normalize(eps);
348 }
349 
353 inline GfVec2d
355 {
356  return v.GetNormalized(eps);
357 }
358 
363 inline GfVec2d
364 GfGetProjection(GfVec2d const &a, GfVec2d const &b)
365 {
366  return a.GetProjection(b);
367 }
368 
373 inline GfVec2d
374 GfGetComplement(GfVec2d const &a, GfVec2d const &b)
375 {
376  return a.GetComplement(b);
377 }
378 
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 
390 PXR_NAMESPACE_CLOSE_SCOPE
391 
392 #endif // PXR_BASE_GF_VEC2D_H
GfVec2d & operator+=(GfVec2d const &other)
Addition.
Definition: vec2d.h:174
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)
Returns the projection of a onto b.
Definition: vec2d.h:364
double const * data() const
Direct data access.
Definition: vec2d.h:135
GfVec2d & operator *=(double s)
Multiplication by scalar.
Definition: vec2d.h:194
Basic type for a vector of 2 int components.
Definition: vec2i.h:60
Assorted mathematical utility functions.
static GfVec2d YAxis()
Create a unit vector along the Y-axis.
Definition: vec2d.h:107
Low-level utilities for informing users of various internal and external diagnostic conditions.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition: traits.h:36
GF_API std::ostream & operator<<(std::ostream &, GfVec2d const &)
Output a GfVec2d.
Basic type for a vector of 2 double components.
Definition: vec2d.h:62
bool GfIsClose(GfVec2d const &v1, GfVec2d const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition: vec2d.h:382
double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec2d.h:258
double const & operator[](size_t i) const
Indexing.
Definition: vec2d.h:140
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
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:354
GfVec2d & operator-=(GfVec2d const &other)
Subtraction.
Definition: vec2d.h:184
Basic type for a vector of 2 GfHalf components.
Definition: vec2h.h:63
GfVec2d GfCompMult(GfVec2d const &v1, GfVec2d const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2d.h:311
constexpr GfVec2d(double value)
Initialize all elements to a single value.
Definition: vec2d.h:73
double GetLengthSq() const
Squared length.
Definition: vec2d.h:241
double GetLength() const
Length.
Definition: vec2d.h:246
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:345
GfVec2d & Set(double const *a)
Set all elements with a pointer to data.
Definition: vec2d.h:130
bool operator==(GfVec2d const &other) const
Equality comparison.
Definition: vec2d.h:149
constexpr GfVec2d(Scl const *p)
Construct with pointer to values.
Definition: vec2d.h:86
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:80
static GfVec2d XAxis()
Create a unit vector along the X-axis.
Definition: vec2d.h:101
friend size_t hash_value(GfVec2d const &vec)
Hash.
Definition: vec2d.h:144
GfVec2d & operator/=(double s)
Division by scalar.
Definition: vec2d.h:208
GfVec2d & Set(double s0, double s1)
Set all elements with passed arguments.
Definition: vec2d.h:123
Basic type for a vector of 2 float components.
Definition: vec2f.h:62
constexpr GfVec2d(double s0, double s1)
Initialize all elements with explicit arguments.
Definition: vec2d.h:79
static GfVec2d Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec2d.h:115
Defines useful mathematical limits.
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
GfVec2d GetProjection(GfVec2d const &v) const
Returns the projection of this onto v.
Definition: vec2d.h:227
GfVec2d GfGetComplement(GfVec2d const &a, GfVec2d const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec2d.h:374
GfVec2d operator-() const
Create a vec with negated elements.
Definition: vec2d.h:169
double ScalarType
Scalar element type and dimension.
Definition: vec2d.h:66
GfVec2d GetComplement(GfVec2d const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec2d.h:236
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