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