HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vec3h.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 GF_VEC3H_H
29 #define GF_VEC3H_H
30 
31 /// \file gf/vec3h.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 
42 #include <hboost/functional/hash.hpp>
43 
44 #include <cstddef>
45 #include <cmath>
46 
47 #include <iosfwd>
48 
50 
51 class GfVec3h;
52 
53 template <>
54 struct GfIsGfVec<class GfVec3h> { static const bool value = true; };
55 
56 /// \class GfVec3h
57 /// \ingroup group_gf_LinearAlgebra
58 ///
59 /// Basic type for a vector of 3 GfHalf components.
60 ///
61 /// Represents a vector of 3 components of type \c GfHalf.
62 /// It is intended to be fast and simple.
63 ///
64 class GfVec3h
65 {
66 public:
67  /// Scalar element type and dimension.
68  typedef GfHalf ScalarType;
69  static const size_t dimension = 3;
70 
71  /// Default constructor does no initialization.
72  GfVec3h() = default;
73 
74  /// Initialize all elements to a single value.
75  constexpr explicit GfVec3h(GfHalf value)
76  : _data{ value, value, value }
77  {
78  }
79 
80  /// Initialize all elements with explicit arguments.
81  constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
82  : _data{ s0, s1, s2 }
83  {
84  }
85 
86  /// Construct with pointer to values.
87  template <class Scl>
88  constexpr explicit GfVec3h(Scl const *p)
89  : _data{ p[0], p[1], p[2] }
90  {
91  }
92 
93  /// Construct from GfVec3d.
94  explicit GfVec3h(class GfVec3d const &other);
95 
96  /// Construct from GfVec3f.
97  explicit GfVec3h(class GfVec3f const &other);
98 
99  /// Implicitly convert from GfVec3i.
100  GfVec3h(class GfVec3i const &other);
101 
102  /// Create a unit vector along the X-axis.
103  static GfVec3h XAxis() {
104  GfVec3h result(0);
105  result[0] = 1;
106  return result;
107  }
108  /// Create a unit vector along the Y-axis.
109  static GfVec3h YAxis() {
110  GfVec3h result(0);
111  result[1] = 1;
112  return result;
113  }
114  /// Create a unit vector along the Z-axis.
115  static GfVec3h ZAxis() {
116  GfVec3h result(0);
117  result[2] = 1;
118  return result;
119  }
120 
121  /// Create a unit vector along the i-th axis, zero-based. Return the zero
122  /// vector if \p i is greater than or equal to 3.
123  static GfVec3h Axis(size_t i) {
124  GfVec3h result(0);
125  if (i < 3)
126  result[i] = 1;
127  return result;
128  }
129 
130  /// Set all elements with passed arguments.
132  _data[0] = s0;
133  _data[1] = s1;
134  _data[2] = s2;
135  return *this;
136  }
137 
138  /// Set all elements with a pointer to data.
139  GfVec3h &Set(GfHalf const *a) {
140  return Set(a[0], a[1], a[2]);
141  }
142 
143  /// Direct data access.
144  GfHalf const *data() const { return _data; }
145  GfHalf *data() { return _data; }
146  GfHalf const *GetArray() const { return data(); }
147 
148  /// Indexing.
149  GfHalf const &operator[](size_t i) const { return _data[i]; }
150  GfHalf &operator[](size_t i) { return _data[i]; }
151 
152  /// Hash.
153  friend inline size_t hash_value(GfVec3h const &vec) {
154  size_t h = 0;
155  hboost::hash_combine(h, vec[0]);
156  hboost::hash_combine(h, vec[1]);
157  hboost::hash_combine(h, vec[2]);
158  return h;
159  }
160 
161  /// Equality comparison.
162  bool operator==(GfVec3h const &other) const {
163  return _data[0] == other[0] &&
164  _data[1] == other[1] &&
165  _data[2] == other[2];
166  }
167  bool operator!=(GfVec3h const &other) const {
168  return !(*this == other);
169  }
170 
171  // TODO Add inequality for other vec types...
172  /// Equality comparison.
173  GF_API
174  bool operator==(class GfVec3d const &other) const;
175  /// Equality comparison.
176  GF_API
177  bool operator==(class GfVec3f const &other) const;
178  /// Equality comparison.
179  GF_API
180  bool operator==(class GfVec3i const &other) const;
181 
182  /// Create a vec with negated elements.
183  GfVec3h operator-() const {
184  return GfVec3h(-_data[0], -_data[1], -_data[2]);
185  }
186 
187  /// Addition.
188  GfVec3h &operator+=(GfVec3h const &other) {
189  _data[0] += other[0];
190  _data[1] += other[1];
191  _data[2] += other[2];
192  return *this;
193  }
194  friend GfVec3h operator+(GfVec3h const &l, GfVec3h const &r) {
195  return GfVec3h(l) += r;
196  }
197 
198  /// Subtraction.
199  GfVec3h &operator-=(GfVec3h const &other) {
200  _data[0] -= other[0];
201  _data[1] -= other[1];
202  _data[2] -= other[2];
203  return *this;
204  }
205  friend GfVec3h operator-(GfVec3h const &l, GfVec3h const &r) {
206  return GfVec3h(l) -= r;
207  }
208 
209  /// Multiplication by scalar.
210  GfVec3h &operator*=(double s) {
211  _data[0] *= s;
212  _data[1] *= s;
213  _data[2] *= s;
214  return *this;
215  }
216  GfVec3h operator*(double s) const {
217  return GfVec3h(*this) *= s;
218  }
219  friend GfVec3h operator*(double s, GfVec3h const &v) {
220  return v * s;
221  }
222 
223  /// Division by scalar.
224  // TODO should divide by the scalar type.
225  GfVec3h &operator/=(double s) {
226  // TODO This should not multiply by 1/s, it should do the division.
227  // Doing the division is more numerically stable when s is close to
228  // zero.
229  return *this *= (1.0 / s);
230  }
231  GfVec3h operator/(double s) const {
232  return *this * (1.0 / s);
233  }
234 
235  /// See GfDot().
236  GfHalf operator*(GfVec3h const &v) const {
237  return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
238  }
239 
240  /// Returns the projection of \p this onto \p v. That is:
241  /// \code
242  /// v * (*this * v)
243  /// \endcode
244  GfVec3h GetProjection(GfVec3h const &v) const {
245  return v * (*this * v);
246  }
247 
248  /// Returns the orthogonal complement of \p this->GetProjection(b).
249  /// That is:
250  /// \code
251  /// *this - this->GetProjection(b)
252  /// \endcode
253  GfVec3h GetComplement(GfVec3h const &b) const {
254  return *this - this->GetProjection(b);
255  }
256 
257  /// Squared length.
258  GfHalf GetLengthSq() const {
259  return *this * *this;
260  }
261 
262  /// Length
263  GfHalf GetLength() const {
264  // TODO should use GfSqrt.
265  return sqrt(GetLengthSq());
266  }
267 
268  /// Normalizes the vector in place to unit length, returning the
269  /// length before normalization. If the length of the vector is
270  /// smaller than \p eps, then the vector is set to vector/\c eps.
271  /// The original length of the vector is returned. See also GfNormalize().
272  ///
273  /// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
274  /// By tickling the code, it no longer tries to write into
275  /// an illegal memory address (in the code section of memory).
276  GfHalf Normalize(GfHalf eps = 0.001) {
277  // TODO this seems suspect... suggest dividing by length so long as
278  // length is not zero.
279  GfHalf length = GetLength();
280  *this /= (length > eps) ? length : eps;
281  return length;
282  }
283 
284  GfVec3h GetNormalized(GfHalf eps = 0.001) const {
285  GfVec3h normalized(*this);
286  normalized.Normalize(eps);
287  return normalized;
288  }
289 
290  /// Orthogonalize and optionally normalize a set of basis vectors. This
291  /// uses an iterative method that is very stable even when the vectors are
292  /// far from orthogonal (close to colinear). The number of iterations and
293  /// thus the computation time does increase as the vectors become close to
294  /// colinear, however. Returns a bool specifying whether the solution
295  /// converged after a number of iterations. If it did not converge, the
296  /// returned vectors will be as close as possible to orthogonal within the
297  /// iteration limit. Colinear vectors will be unaltered, and the method
298  /// will return false.
299  GF_API
300  static bool OrthogonalizeBasis(
301  GfVec3h *tx, GfVec3h *ty, GfVec3h *tz,
302  const bool normalize,
303  double eps = GF_MIN_ORTHO_TOLERANCE);
304 
305  /// Sets \c v1 and \c v2 to unit vectors such that v1, v2 and *this are
306  /// mutually orthogonal. If the length L of *this is smaller than \c eps,
307  /// then v1 and v2 will have magnitude L/eps. As a result, the function
308  /// delivers a continuous result as *this shrinks in length.
309  GF_API
311  GfHalf eps = 0.001) const;
312 
313 
314 private:
315  GfHalf _data[3];
316 };
317 
318 /// Output a GfVec3h.
319 /// \ingroup group_gf_DebuggingOutput
320 GF_API std::ostream& operator<<(std::ostream &, GfVec3h const &);
321 
322 
324 
325 #include "pxr/base/gf/vec3d.h"
326 #include "pxr/base/gf/vec3f.h"
327 #include "pxr/base/gf/vec3i.h"
328 
330 
331 inline
332 GfVec3h::GfVec3h(class GfVec3d const &other)
333 {
334  _data[0] = other[0];
335  _data[1] = other[1];
336  _data[2] = other[2];
337 }
338 inline
339 GfVec3h::GfVec3h(class GfVec3f const &other)
340 {
341  _data[0] = other[0];
342  _data[1] = other[1];
343  _data[2] = other[2];
344 }
345 inline
346 GfVec3h::GfVec3h(class GfVec3i const &other)
347 {
348  _data[0] = other[0];
349  _data[1] = other[1];
350  _data[2] = other[2];
351 }
352 
353 /// Returns component-wise multiplication of vectors \p v1 and \p v2.
354 inline GfVec3h
355 GfCompMult(GfVec3h const &v1, GfVec3h const &v2) {
356  return GfVec3h(
357  v1[0] * v2[0],
358  v1[1] * v2[1],
359  v1[2] * v2[2]
360  );
361 }
362 
363 /// Returns component-wise quotient of vectors \p v1 and \p v2.
364 inline GfVec3h
365 GfCompDiv(GfVec3h const &v1, GfVec3h const &v2) {
366  return GfVec3h(
367  v1[0] / v2[0],
368  v1[1] / v2[1],
369  v1[2] / v2[2]
370  );
371 }
372 
373 /// Returns the dot (inner) product of two vectors.
374 inline GfHalf
375 GfDot(GfVec3h const &v1, GfVec3h const &v2) {
376  return v1 * v2;
377 }
378 
379 
380 /// Returns the geometric length of \c v.
381 inline GfHalf
383 {
384  return v.GetLength();
385 }
386 
387 /// Normalizes \c *v in place to unit length, returning the length before
388 /// normalization. If the length of \c *v is smaller than \p eps then \c *v is
389 /// set to \c *v/eps. The original length of \c *v is returned.
390 inline GfHalf
391 GfNormalize(GfVec3h *v, GfHalf eps = 0.001)
392 {
393  return v->Normalize(eps);
394 }
395 
396 /// Returns a normalized (unit-length) vector with the same direction as \p v.
397 /// If the length of this vector is smaller than \p eps, the vector divided by
398 /// \p eps is returned.
399 inline GfVec3h
400 GfGetNormalized(GfVec3h const &v, GfHalf eps = 0.001)
401 {
402  return v.GetNormalized(eps);
403 }
404 
405 /// Returns the projection of \p a onto \p b. That is:
406 /// \code
407 /// b * (a * b)
408 /// \endcode
409 inline GfVec3h
411 {
412  return a.GetProjection(b);
413 }
414 
415 /// Returns the orthogonal complement of \p a.GetProjection(b). That is:
416 /// \code
417 /// a - a.GetProjection(b)
418 /// \endcode
419 inline GfVec3h
421 {
422  return a.GetComplement(b);
423 }
424 
425 /// Tests for equality within a given tolerance, returning \c true if the
426 /// length of the difference vector is less than or equal to \p tolerance.
427 inline bool
428 GfIsClose(GfVec3h const &v1, GfVec3h const &v2, double tolerance)
429 {
430  GfVec3h delta = v1 - v2;
431  return delta.GetLengthSq() <= tolerance * tolerance;
432 }
433 
434 
435 GF_API bool
437  bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
438 
439 GF_API void
441  GfVec3h* v1,
442  GfVec3h* v2,
443  GfHalf eps = 0.001);
444 
445 /// Returns the cross product of \p v1 and \p v2.
446 inline GfVec3h
447 GfCross(GfVec3h const &v1, GfVec3h const &v2)
448 {
449  return GfVec3h(
450  v1[1] * v2[2] - v1[2] * v2[1],
451  v1[2] * v2[0] - v1[0] * v2[2],
452  v1[0] * v2[1] - v1[1] * v2[0]);
453 }
454 
455 /// Returns the cross product of \p v1 and \p v2.
456 /// \see GfCross()
457 inline GfVec3h
458 operator^(GfVec3h const &v1, GfVec3h const &v2)
459 {
460  return GfCross(v1, v2);
461 }
462 
463 /// Spherical linear interpolation in three dimensions.
465 GfSlerp(double alpha, GfVec3h const &v0, GfVec3h const &v1);
466 
467 
468 
470 
471 #endif // GF_VEC3H_H
GLdouble s
Definition: glew.h:1390
GfVec3h GfCross(GfVec3h const &v1, GfVec3h const &v2)
Returns the cross product of v1 and v2.
Definition: vec3h.h:447
GfHalf GfGetLength(GfVec3h const &v)
Returns the geometric length of v.
Definition: vec3h.h:382
GfVec3h operator/(double s) const
Definition: vec3h.h:231
GfHalf & operator[](size_t i)
Definition: vec3h.h:150
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1520
GfHalf GetLength() const
Length.
Definition: vec3h.h:263
*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:643
GfVec3h GetProjection(GfVec3h const &v) const
Definition: vec3h.h:244
static const size_t dimension
Definition: vec3h.h:69
GfHalf const * GetArray() const
Definition: vec3h.h:146
GfHalf GetLengthSq() const
Squared length.
Definition: vec3h.h:258
GfHalf * data()
Definition: vec3h.h:145
GfVec3h GetNormalized(GfHalf eps=0.001) const
Definition: vec3h.h:284
GfVec3h operator*(double s) const
Definition: vec3h.h:216
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
vfloat4 sqrt(const vfloat4 &a)
Definition: simd.h:7231
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec3h.h:149
GfHalf Normalize(GfHalf eps=0.001)
Definition: vec3h.h:276
Definition: vec3f.h:63
GfVec3h & operator*=(double s)
Multiplication by scalar.
Definition: vec3h.h:210
constexpr GfVec3h(GfHalf value)
Initialize all elements to a single value.
Definition: vec3h.h:75
GfHalf GfDot(GfVec3h const &v1, GfVec3h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec3h.h:375
GLdouble l
Definition: glew.h:9122
const GLdouble * v
Definition: glew.h:1391
GfHalf ScalarType
Scalar element type and dimension.
Definition: vec3h.h:68
GfVec3h & operator+=(GfVec3h const &other)
Addition.
Definition: vec3h.h:188
GfVec3h & operator-=(GfVec3h const &other)
Subtraction.
Definition: vec3h.h:199
GF_API bool GfOrthogonalizeBasis(GfVec3h *tx, GfVec3h *ty, GfVec3h *tz, bool normalize, double eps=GF_MIN_ORTHO_TOLERANCE)
GfVec3h operator-() const
Create a vec with negated elements.
Definition: vec3h.h:183
static GfVec3h ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3h.h:115
GLfloat GLfloat GLfloat v2
Definition: glew.h:1856
static GfVec3h XAxis()
Create a unit vector along the X-axis.
Definition: vec3h.h:103
constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
Initialize all elements with explicit arguments.
Definition: vec3h.h:81
GF_API void BuildOrthonormalFrame(GfVec3h *v1, GfVec3h *v2, GfHalf eps=0.001) const
GfVec3h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec3h.h:139
GfVec3h GetComplement(GfVec3h const &b) const
Definition: vec3h.h:253
GF_API std::ostream & operator<<(std::ostream &, GfVec3h const &)
GfVec3h GfCompDiv(GfVec3h const &v1, GfVec3h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec3h.h:365
GLuint GLsizei GLsizei * length
Definition: glew.h:1825
GfVec3h GfGetComplement(GfVec3h const &a, GfVec3h const &b)
Definition: vec3h.h:420
static GfVec3h YAxis()
Create a unit vector along the Y-axis.
Definition: vec3h.h:109
friend GfVec3h operator+(GfVec3h const &l, GfVec3h const &r)
Definition: vec3h.h:194
Definition: vec3i.h:61
GfVec3h & operator/=(double s)
Division by scalar.
Definition: vec3h.h:225
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:8011
GridType::Ptr normalize(const GridType &grid, bool threaded, InterruptT *interrupt)
Normalize the vectors of the given vector-valued grid.
GfHalf const * data() const
Direct data access.
Definition: vec3h.h:144
GF_API void GfBuildOrthonormalFrame(GfVec3h const &v0, GfVec3h *v1, GfVec3h *v2, GfHalf eps=0.001)
GF_API GfVec3h GfSlerp(double alpha, GfVec3h const &v0, GfVec3h const &v1)
Spherical linear interpolation in three dimensions.
friend size_t hash_value(GfVec3h const &vec)
Hash.
Definition: vec3h.h:153
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
GLfloat GLfloat p
Definition: glew.h:16321
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
bool GfIsClose(GfVec3h const &v1, GfVec3h const &v2, double tolerance)
Definition: vec3h.h:428
GfVec3h operator^(GfVec3h const &v1, GfVec3h const &v2)
Definition: vec3h.h:458
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
Definition: glew.h:12681
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
Definition: vec3d.h:63
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
GfVec3h GfGetProjection(GfVec3h const &a, GfVec3h const &b)
Definition: vec3h.h:410
GfHalf GfNormalize(GfVec3h *v, GfHalf eps=0.001)
Definition: vec3h.h:391
GLfloat v0
Definition: glew.h:1848
GfVec3h GfGetNormalized(GfVec3h const &v, GfHalf eps=0.001)
Definition: vec3h.h:400
bool operator==(GfVec3h const &other) const
Equality comparison.
Definition: vec3h.h:162
GLuint64EXT * result
Definition: glew.h:14007
constexpr GfVec3h(Scl const *p)
Construct with pointer to values.
Definition: vec3h.h:88
GfVec3h()=default
Default constructor does no initialization.
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s0
Definition: glew.h:12681
friend GfVec3h operator-(GfVec3h const &l, GfVec3h const &r)
Definition: vec3h.h:205
GfHalf operator*(GfVec3h const &v) const
See GfDot().
Definition: vec3h.h:236
static GF_API bool OrthogonalizeBasis(GfVec3h *tx, GfVec3h *ty, GfVec3h *tz, const bool normalize, double eps=GF_MIN_ORTHO_TOLERANCE)
Definition: vec3h.h:64
GLsizei const GLfloat * value
Definition: glew.h:1849
bool operator!=(GfVec3h const &other) const
Definition: vec3h.h:167
GfVec3h & Set(GfHalf s0, GfHalf s1, GfHalf s2)
Set all elements with passed arguments.
Definition: vec3h.h:131
GLint GLenum GLboolean normalized
Definition: glew.h:1905
GLfloat GLfloat v1
Definition: glew.h:1852
GfVec3h GfCompMult(GfVec3h const &v1, GfVec3h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec3h.h:355
#define GF_MIN_ORTHO_TOLERANCE
Definition: limits.h:39
friend GfVec3h operator*(double s, GfVec3h const &v)
Definition: vec3h.h:219
#define GF_API
Definition: api.h:40
static GfVec3h Axis(size_t i)
Definition: vec3h.h:123