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 //
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 
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 
48 PXR_NAMESPACE_OPEN_SCOPE
49 
50 class GfVec3h;
51 
52 template <>
53 struct GfIsGfVec<class GfVec3h> { static const bool value = true; };
54 
63 class GfVec3h
64 {
65 public:
67  typedef GfHalf ScalarType;
68  static const size_t dimension = 3;
69 
71  GfVec3h() = default;
72 
74  constexpr explicit GfVec3h(GfHalf value)
75  : _data{ value, value, value }
76  {
77  }
78 
80  constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
81  : _data{ s0, s1, s2 }
82  {
83  }
84 
86  template <class Scl>
87  constexpr explicit GfVec3h(Scl const *p)
88  : _data{ p[0], p[1], p[2] }
89  {
90  }
91 
93  explicit GfVec3h(class GfVec3d const &other);
94 
96  explicit GfVec3h(class GfVec3f const &other);
97 
99  GfVec3h(class GfVec3i const &other);
100 
102  static GfVec3h XAxis() {
103  GfVec3h result(0);
104  result[0] = 1;
105  return result;
106  }
108  static GfVec3h YAxis() {
109  GfVec3h result(0);
110  result[1] = 1;
111  return result;
112  }
114  static GfVec3h ZAxis() {
115  GfVec3h result(0);
116  result[2] = 1;
117  return result;
118  }
119 
122  static GfVec3h Axis(size_t i) {
123  GfVec3h result(0);
124  if (i < 3)
125  result[i] = 1;
126  return result;
127  }
128 
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 
138  GfVec3h &Set(GfHalf const *a) {
139  return Set(a[0], a[1], a[2]);
140  }
141 
143  GfHalf const *data() const { return _data; }
144  GfHalf *data() { return _data; }
145  GfHalf const *GetArray() const { return data(); }
146 
148  GfHalf const &operator[](size_t i) const { return _data[i]; }
149  GfHalf &operator[](size_t i) { return _data[i]; }
150 
152  friend inline size_t hash_value(GfVec3h const &vec) {
153  return TfHash::Combine(vec[0], vec[1], vec[2]);
154  }
155 
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...
168  GF_API
169  bool operator==(class GfVec3d const &other) const;
171  GF_API
172  bool operator==(class GfVec3f const &other) const;
174  GF_API
175  bool operator==(class GfVec3i const &other) const;
176 
178  GfVec3h operator-() const {
179  return GfVec3h(-_data[0], -_data[1], -_data[2]);
180  }
181 
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 
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 
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 
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 
231  GfHalf operator*(GfVec3h const &v) const {
232  return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
233  }
234 
239  GfVec3h GetProjection(GfVec3h const &v) const {
240  return v * (*this * v);
241  }
242 
248  GfVec3h GetComplement(GfVec3h const &b) const {
249  return *this - this->GetProjection(b);
250  }
251 
253  GfHalf GetLengthSq() const {
254  return *this * *this;
255  }
256 
258  GfHalf GetLength() const {
259  return GfSqrt(GetLengthSq());
260  }
261 
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 
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 
303  GF_API
304  void BuildOrthonormalFrame(GfVec3h *v1, GfVec3h *v2,
305  GfHalf eps = 0.001) const;
306 
307 
308 private:
309  GfHalf _data[3];
310 };
311 
314 GF_API std::ostream& operator<<(std::ostream &, GfVec3h const &);
315 
316 
317 PXR_NAMESPACE_CLOSE_SCOPE
318 
319 #include "pxr/base/gf/vec3d.h"
320 #include "pxr/base/gf/vec3f.h"
321 #include "pxr/base/gf/vec3i.h"
322 
323 PXR_NAMESPACE_OPEN_SCOPE
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 
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 
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 
368 inline GfHalf
369 GfDot(GfVec3h const &v1, GfVec3h const &v2) {
370  return v1 * v2;
371 }
372 
373 
375 inline GfHalf
377 {
378  return v.GetLength();
379 }
380 
384 inline GfHalf
385 GfNormalize(GfVec3h *v, GfHalf eps = 0.001)
386 {
387  return v->Normalize(eps);
388 }
389 
393 inline GfVec3h
394 GfGetNormalized(GfVec3h const &v, GfHalf eps = 0.001)
395 {
396  return v.GetNormalized(eps);
397 }
398 
403 inline GfVec3h
404 GfGetProjection(GfVec3h const &a, GfVec3h const &b)
405 {
406  return a.GetProjection(b);
407 }
408 
413 inline GfVec3h
414 GfGetComplement(GfVec3h const &a, GfVec3h const &b)
415 {
416  return a.GetComplement(b);
417 }
418 
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
430 GfOrthogonalizeBasis(GfVec3h *tx, GfVec3h *ty, GfVec3h *tz,
431  bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
432 
433 GF_API void
434 GfBuildOrthonormalFrame(GfVec3h const &v0,
435  GfVec3h* v1,
436  GfVec3h* v2,
437  GfHalf eps = 0.001);
438 
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 
451 inline GfVec3h
452 operator^(GfVec3h const &v1, GfVec3h const &v2)
453 {
454  return GfCross(v1, v2);
455 }
456 
458 GF_API GfVec3h
459 GfSlerp(double alpha, GfVec3h const &v0, GfVec3h const &v1);
460 
461 
462 
463 PXR_NAMESPACE_CLOSE_SCOPE
464 
465 #endif // PXR_BASE_GF_VEC3H_H
GfVec3h GetComplement(GfVec3h const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec3h.h:248
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
constexpr GfVec3h(GfHalf value)
Initialize all elements to a single value.
Definition: vec3h.h:74
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec3h.h:148
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec3h.h:270
constexpr GfVec3h(Scl const *p)
Construct with pointer to values.
Definition: vec3h.h:87
GfVec3h & operator+=(GfVec3h const &other)
Addition.
Definition: vec3h.h:183
pxr_half::half GfHalf
A 16-bit floating point data type.
Definition: half.h:41
Assorted mathematical utility functions.
This header serves to simply bring in the half float datatype and provide a hash_value function.
Basic type for a vector of 3 float components.
Definition: vec3f.h:62
static GfVec3h Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec3h.h:122
GfHalf GfDot(GfVec3h const &v1, GfVec3h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec3h.h:369
GfVec3h & operator-=(GfVec3h const &other)
Subtraction.
Definition: vec3h.h:194
Low-level utilities for informing users of various internal and external diagnostic conditions.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition: traits.h:36
static GfVec3h XAxis()
Create a unit vector along the X-axis.
Definition: vec3h.h:102
static GfVec3h ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3h.h:114
#define GF_MIN_ORTHO_TOLERANCE
This constant is used to determine when a set of basis vectors is close to orthogonal.
Definition: limits.h:39
GfVec3h & operator *=(double s)
Multiplication by scalar.
Definition: vec3h.h:205
GfVec3h & operator/=(double s)
Division by scalar.
Definition: vec3h.h:220
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
static GfVec3h YAxis()
Create a unit vector along the Y-axis.
Definition: vec3h.h:108
GfVec3h operator-() const
Create a vec with negated elements.
Definition: vec3h.h:178
GfHalf GetLengthSq() const
Squared length.
Definition: vec3h.h:253
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)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec3h.h:414
Basic type for a vector of 3 int components.
Definition: vec3i.h:60
static GF_API bool OrthogonalizeBasis(GfVec3h *tx, GfVec3h *ty, GfVec3h *tz, const bool normalize, double eps=GF_MIN_ORTHO_TOLERANCE)
Orthogonalize and optionally normalize a set of basis vectors.
GF_API void BuildOrthonormalFrame(GfVec3h *v1, GfVec3h *v2, GfHalf eps=0.001) const
Sets v1 and v2 to unit vectors such that v1, v2 and *this are mutually orthogonal.
GfHalf ScalarType
Scalar element type and dimension.
Definition: vec3h.h:67
GF_API std::ostream & operator<<(std::ostream &, GfVec3h const &)
Output a GfVec3h.
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:80
GfVec3h & Set(GfHalf s0, GfHalf s1, GfHalf s2)
Set all elements with passed arguments.
Definition: vec3h.h:130
GfVec3h GetProjection(GfVec3h const &v) const
Returns the projection of this onto v.
Definition: vec3h.h:239
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
bool GfIsClose(GfVec3h const &v1, GfVec3h const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition: vec3h.h:422
GfVec3h operator^(GfVec3h const &v1, GfVec3h const &v2)
Returns the cross product of v1 and v2.
Definition: vec3h.h:452
GfVec3h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec3h.h:138
Basic type for a vector of 3 double components.
Definition: vec3d.h:62
Defines useful mathematical limits.
GfVec3h GfGetProjection(GfVec3h const &a, GfVec3h const &b)
Returns the projection of a onto b.
Definition: vec3h.h:404
GfHalf GfNormalize(GfVec3h *v, GfHalf eps=0.001)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec3h.h:385
GfVec3h GfGetNormalized(GfVec3h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec3h.h:394
GfHalf GetLength() const
Length.
Definition: vec3h.h:258
Basic type for a vector of 3 GfHalf components.
Definition: vec3h.h:63
GfVec3h()=default
Default constructor does no initialization.
constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
Initialize all elements with explicit arguments.
Definition: vec3h.h:80
GfVec3h GfCompMult(GfVec3h const &v1, GfVec3h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec3h.h:349
GfHalf const * data() const
Direct data access.
Definition: vec3h.h:143
bool operator==(GfVec3h const &other) const
Equality comparison.
Definition: vec3h.h:157