HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vec2h.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_VEC2H_H
12 #define PXR_BASE_GF_VEC2H_H
13 
14 /// \file gf/vec2h.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/gf/half.h"
24 #include "pxr/base/tf/hash.h"
25 
26 #include <cstddef>
27 #include <cmath>
28 
29 #include <iosfwd>
30 
32 
33 class GfVec2h;
34 
35 template <>
36 struct GfIsGfVec<class GfVec2h> { static const bool value = true; };
37 
38 /// \class GfVec2h
39 /// \ingroup group_gf_LinearAlgebra
40 ///
41 /// Basic type for a vector of 2 GfHalf components.
42 ///
43 /// Represents a vector of 2 components of type \c GfHalf.
44 /// It is intended to be fast and simple.
45 ///
46 class GfVec2h
47 {
48 public:
49  /// Scalar element type and dimension.
50  typedef GfHalf ScalarType;
51  static const size_t dimension = 2;
52 
53  /// GfVec2h value-initializes to zero and performs no default
54  /// initialization, like float or double.
55  GfVec2h() = default;
56 
57  /// Initialize all elements to a single value.
58  constexpr explicit GfVec2h(GfHalf value)
59  : _data{ value, value }
60  {
61  }
62 
63  /// Initialize all elements with explicit arguments.
64  constexpr GfVec2h(GfHalf s0, GfHalf s1)
65  : _data{ s0, s1 }
66  {
67  }
68 
69  /// Construct with pointer to values.
70  template <class Scl>
71  constexpr explicit GfVec2h(Scl const *p)
72  : _data{ p[0], p[1] }
73  {
74  }
75 
76  /// Construct from GfVec2d.
77  explicit GfVec2h(class GfVec2d const &other);
78 
79  /// Construct from GfVec2f.
80  explicit GfVec2h(class GfVec2f const &other);
81 
82  /// Implicitly convert from GfVec2i.
83  GfVec2h(class GfVec2i const &other);
84 
85  /// Create a unit vector along the X-axis.
86  static GfVec2h XAxis() {
87  GfVec2h result(0);
88  result[0] = 1;
89  return result;
90  }
91  /// Create a unit vector along the Y-axis.
92  static GfVec2h YAxis() {
93  GfVec2h result(0);
94  result[1] = 1;
95  return result;
96  }
97 
98  /// Create a unit vector along the i-th axis, zero-based. Return the zero
99  /// vector if \p i is greater than or equal to 2.
100  static GfVec2h Axis(size_t i) {
101  GfVec2h result(0);
102  if (i < 2)
103  result[i] = 1;
104  return result;
105  }
106 
107  /// Set all elements with passed arguments.
109  _data[0] = s0;
110  _data[1] = s1;
111  return *this;
112  }
113 
114  /// Set all elements with a pointer to data.
115  GfVec2h &Set(GfHalf const *a) {
116  return Set(a[0], a[1]);
117  }
118 
119  /// Direct data access.
120  GfHalf const *data() const { return _data; }
121  GfHalf *data() { return _data; }
122  GfHalf const *GetArray() const { return data(); }
123 
124  /// Indexing.
125  GfHalf const &operator[](size_t i) const { return _data[i]; }
126  GfHalf &operator[](size_t i) { return _data[i]; }
127 
128  /// Hash.
129  friend inline size_t hash_value(GfVec2h const &vec) {
130  return TfHash::Combine(vec[0], vec[1]);
131  }
132 
133  /// Equality comparison.
134  bool operator==(GfVec2h const &other) const {
135  return _data[0] == other[0] &&
136  _data[1] == other[1];
137  }
138  bool operator!=(GfVec2h const &other) const {
139  return !(*this == other);
140  }
141 
142  // TODO Add inequality for other vec types...
143  /// Equality comparison.
144  GF_API
145  bool operator==(class GfVec2d const &other) const;
146  /// Equality comparison.
147  GF_API
148  bool operator==(class GfVec2f const &other) const;
149  /// Equality comparison.
150  GF_API
151  bool operator==(class GfVec2i const &other) const;
152 
153  /// Create a vec with negated elements.
154  GfVec2h operator-() const {
155  return GfVec2h(-_data[0], -_data[1]);
156  }
157 
158  /// Addition.
159  GfVec2h &operator+=(GfVec2h const &other) {
160  _data[0] += other[0];
161  _data[1] += other[1];
162  return *this;
163  }
164  friend GfVec2h operator+(GfVec2h const &l, GfVec2h const &r) {
165  return GfVec2h(l) += r;
166  }
167 
168  /// Subtraction.
169  GfVec2h &operator-=(GfVec2h const &other) {
170  _data[0] -= other[0];
171  _data[1] -= other[1];
172  return *this;
173  }
174  friend GfVec2h operator-(GfVec2h const &l, GfVec2h const &r) {
175  return GfVec2h(l) -= r;
176  }
177 
178  /// Multiplication by scalar.
179  GfVec2h &operator*=(double s) {
180  _data[0] *= s;
181  _data[1] *= s;
182  return *this;
183  }
184  GfVec2h operator*(double s) const {
185  return GfVec2h(*this) *= s;
186  }
187  friend GfVec2h operator*(double s, GfVec2h const &v) {
188  return v * s;
189  }
190 
191  /// Division by scalar.
192  // TODO should divide by the scalar type.
193  GfVec2h &operator/=(double s) {
194  // TODO This should not multiply by 1/s, it should do the division.
195  // Doing the division is more numerically stable when s is close to
196  // zero.
197  return *this *= (1.0 / s);
198  }
199  GfVec2h operator/(double s) const {
200  return *this * (1.0 / s);
201  }
202 
203  /// See GfDot().
204  GfHalf operator*(GfVec2h const &v) const {
205  return _data[0] * v[0] + _data[1] * v[1];
206  }
207 
208  /// Returns the projection of \p this onto \p v. That is:
209  /// \code
210  /// v * (*this * v)
211  /// \endcode
212  GfVec2h GetProjection(GfVec2h const &v) const {
213  return v * (*this * v);
214  }
215 
216  /// Returns the orthogonal complement of \p this->GetProjection(b).
217  /// That is:
218  /// \code
219  /// *this - this->GetProjection(b)
220  /// \endcode
221  GfVec2h GetComplement(GfVec2h const &b) const {
222  return *this - this->GetProjection(b);
223  }
224 
225  /// Squared length.
226  GfHalf GetLengthSq() const {
227  return *this * *this;
228  }
229 
230  /// Length
231  GfHalf GetLength() const {
232  return GfSqrt(GetLengthSq());
233  }
234 
235  /// Normalizes the vector in place to unit length, returning the
236  /// length before normalization. If the length of the vector is
237  /// smaller than \p eps, then the vector is set to vector/\c eps.
238  /// The original length of the vector is returned. See also GfNormalize().
239  ///
240  /// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
241  /// By tickling the code, it no longer tries to write into
242  /// an illegal memory address (in the code section of memory).
243  GfHalf Normalize(GfHalf eps = 0.001) {
244  // TODO this seems suspect... suggest dividing by length so long as
245  // length is not zero.
246  GfHalf length = GetLength();
247  *this /= (length > eps) ? length : eps;
248  return length;
249  }
250 
251  GfVec2h GetNormalized(GfHalf eps = 0.001) const {
252  GfVec2h normalized(*this);
253  normalized.Normalize(eps);
254  return normalized;
255  }
256 
257 
258 private:
259  GfHalf _data[2];
260 };
261 
262 /// Output a GfVec2h.
263 /// \ingroup group_gf_DebuggingOutput
264 GF_API std::ostream& operator<<(std::ostream &, GfVec2h const &);
265 
266 
268 
269 #include "pxr/base/gf/vec2d.h"
270 #include "pxr/base/gf/vec2f.h"
271 #include "pxr/base/gf/vec2i.h"
272 
274 
275 inline
276 GfVec2h::GfVec2h(class GfVec2d const &other)
277 {
278  _data[0] = other[0];
279  _data[1] = other[1];
280 }
281 inline
282 GfVec2h::GfVec2h(class GfVec2f const &other)
283 {
284  _data[0] = other[0];
285  _data[1] = other[1];
286 }
287 inline
288 GfVec2h::GfVec2h(class GfVec2i const &other)
289 {
290  _data[0] = other[0];
291  _data[1] = other[1];
292 }
293 
294 /// Returns component-wise multiplication of vectors \p v1 and \p v2.
295 inline GfVec2h
296 GfCompMult(GfVec2h const &v1, GfVec2h const &v2) {
297  return GfVec2h(
298  v1[0] * v2[0],
299  v1[1] * v2[1]
300  );
301 }
302 
303 /// Returns component-wise quotient of vectors \p v1 and \p v2.
304 inline GfVec2h
305 GfCompDiv(GfVec2h const &v1, GfVec2h const &v2) {
306  return GfVec2h(
307  v1[0] / v2[0],
308  v1[1] / v2[1]
309  );
310 }
311 
312 /// Returns the dot (inner) product of two vectors.
313 inline GfHalf
314 GfDot(GfVec2h const &v1, GfVec2h const &v2) {
315  return v1 * v2;
316 }
317 
318 
319 /// Returns the geometric length of \c v.
320 inline GfHalf
322 {
323  return v.GetLength();
324 }
325 
326 /// Normalizes \c *v in place to unit length, returning the length before
327 /// normalization. If the length of \c *v is smaller than \p eps then \c *v is
328 /// set to \c *v/eps. The original length of \c *v is returned.
329 inline GfHalf
330 GfNormalize(GfVec2h *v, GfHalf eps = 0.001)
331 {
332  return v->Normalize(eps);
333 }
334 
335 /// Returns a normalized (unit-length) vector with the same direction as \p v.
336 /// If the length of this vector is smaller than \p eps, the vector divided by
337 /// \p eps is returned.
338 inline GfVec2h
339 GfGetNormalized(GfVec2h const &v, GfHalf eps = 0.001)
340 {
341  return v.GetNormalized(eps);
342 }
343 
344 /// Returns the projection of \p a onto \p b. That is:
345 /// \code
346 /// b * (a * b)
347 /// \endcode
348 inline GfVec2h
350 {
351  return a.GetProjection(b);
352 }
353 
354 /// Returns the orthogonal complement of \p a.GetProjection(b). That is:
355 /// \code
356 /// a - a.GetProjection(b)
357 /// \endcode
358 inline GfVec2h
360 {
361  return a.GetComplement(b);
362 }
363 
364 /// Tests for equality within a given tolerance, returning \c true if the
365 /// length of the difference vector is less than or equal to \p tolerance.
366 inline bool
367 GfIsClose(GfVec2h const &v1, GfVec2h const &v2, double tolerance)
368 {
369  GfVec2h delta = v1 - v2;
370  return delta.GetLengthSq() <= tolerance * tolerance;
371 }
372 
373 
374 
376 
377 #endif // PXR_BASE_GF_VEC2H_H
GfHalf const * data() const
Direct data access.
Definition: vec2h.h:120
GfVec2h()=default
GfVec2h GfCompDiv(GfVec2h const &v1, GfVec2h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2h.h:305
GfHalf GetLengthSq() const
Squared length.
Definition: vec2h.h:226
constexpr GfVec2h(Scl const *p)
Construct with pointer to values.
Definition: vec2h.h:71
GfVec2h & operator-=(GfVec2h const &other)
Subtraction.
Definition: vec2h.h:169
GfVec2h & operator*=(double s)
Multiplication by scalar.
Definition: vec2h.h:179
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
friend GfVec2h operator*(double s, GfVec2h const &v)
Definition: vec2h.h:187
const GLdouble * v
Definition: glcorearb.h:837
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
GfVec2h GfGetNormalized(GfVec2h const &v, GfHalf eps=0.001)
Definition: vec2h.h:339
GfVec2h GfGetComplement(GfVec2h const &a, GfVec2h const &b)
Definition: vec2h.h:359
bool GfIsClose(GfVec2h const &v1, GfVec2h const &v2, double tolerance)
Definition: vec2h.h:367
**But if you need a result
Definition: thread.h:622
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
GfVec2h operator*(double s) const
Definition: vec2h.h:184
GfHalf GfGetLength(GfVec2h const &v)
Returns the geometric length of v.
Definition: vec2h.h:321
GfVec2h GetComplement(GfVec2h const &b) const
Definition: vec2h.h:221
Definition: vec2d.h:45
GfVec2h operator/(double s) const
Definition: vec2h.h:199
static const size_t dimension
Definition: vec2h.h:51
GfVec2h & operator/=(double s)
Division by scalar.
Definition: vec2h.h:193
bool operator==(GfVec2h const &other) const
Equality comparison.
Definition: vec2h.h:134
friend GfVec2h operator-(GfVec2h const &l, GfVec2h const &r)
Definition: vec2h.h:174
GfHalf GetLength() const
Length.
Definition: vec2h.h:231
Definition: vec2h.h:46
GfVec2h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec2h.h:115
GfHalf GfNormalize(GfVec2h *v, GfHalf eps=0.001)
Definition: vec2h.h:330
GfHalf const * GetArray() const
Definition: vec2h.h:122
friend GfVec2h operator+(GfVec2h const &l, GfVec2h const &r)
Definition: vec2h.h:164
GfVec2h GetNormalized(GfHalf eps=0.001) const
Definition: vec2h.h:251
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec2h.h:125
friend size_t hash_value(GfVec2h const &vec)
Hash.
Definition: vec2h.h:129
constexpr GfVec2h(GfHalf s0, GfHalf s1)
Initialize all elements with explicit arguments.
Definition: vec2h.h:64
GLint GLenum GLboolean normalized
Definition: glcorearb.h:872
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
constexpr GfVec2h(GfHalf value)
Initialize all elements to a single value.
Definition: vec2h.h:58
GfVec2h GfCompMult(GfVec2h const &v1, GfVec2h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2h.h:296
bool operator!=(GfVec2h const &other) const
Definition: vec2h.h:138
GfVec2h GetProjection(GfVec2h const &v) const
Definition: vec2h.h:212
GfVec2h operator-() const
Create a vec with negated elements.
Definition: vec2h.h:154
GfVec2h & Set(GfHalf s0, GfHalf s1)
Set all elements with passed arguments.
Definition: vec2h.h:108
static GfVec2h Axis(size_t i)
Definition: vec2h.h:100
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
GF_API std::ostream & operator<<(std::ostream &, GfVec2h const &)
Definition: vec2f.h:45
GLfloat GLfloat v1
Definition: glcorearb.h:817
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GfHalf * data()
Definition: vec2h.h:121
GfHalf & operator[](size_t i)
Definition: vec2h.h:126
static GfVec2h YAxis()
Create a unit vector along the Y-axis.
Definition: vec2h.h:92
static GfVec2h XAxis()
Create a unit vector along the X-axis.
Definition: vec2h.h:86
GLboolean r
Definition: glcorearb.h:1222
GfVec2h & operator+=(GfVec2h const &other)
Addition.
Definition: vec2h.h:159
GfHalf GfDot(GfVec2h const &v1, GfVec2h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2h.h:314
GfVec2h GfGetProjection(GfVec2h const &a, GfVec2h const &b)
Definition: vec2h.h:349
GfHalf Normalize(GfHalf eps=0.001)
Definition: vec2h.h:243
#define GF_API
Definition: api.h:23
GfHalf operator*(GfVec2h const &v) const
See GfDot().
Definition: vec2h.h:204
GfHalf ScalarType
Scalar element type and dimension.
Definition: vec2h.h:50