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