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