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