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