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