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 //
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 
42 #include <boost/functional/hash.hpp>
43 
44 #include <cstddef>
45 #include <cmath>
46 
47 #include <iosfwd>
48 
49 PXR_NAMESPACE_OPEN_SCOPE
50 
51 class GfVec3h;
52 
53 template <>
54 struct GfIsGfVec<class GfVec3h> { static const bool value = true; };
55 
64 class GfVec3h
65 {
66 public:
68  typedef GfHalf ScalarType;
69  static const size_t dimension = 3;
70 
72  GfVec3h() = default;
73 
75  constexpr explicit GfVec3h(GfHalf value)
76  : _data{ value, value, value }
77  {
78  }
79 
81  constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
82  : _data{ s0, s1, s2 }
83  {
84  }
85 
87  template <class Scl>
88  constexpr explicit GfVec3h(Scl const *p)
89  : _data{ p[0], p[1], p[2] }
90  {
91  }
92 
94  explicit GfVec3h(class GfVec3d const &other);
95 
97  explicit GfVec3h(class GfVec3f const &other);
98 
100  GfVec3h(class GfVec3i const &other);
101 
103  static GfVec3h XAxis() {
104  GfVec3h result(0);
105  result[0] = 1;
106  return result;
107  }
109  static GfVec3h YAxis() {
110  GfVec3h result(0);
111  result[1] = 1;
112  return result;
113  }
115  static GfVec3h ZAxis() {
116  GfVec3h result(0);
117  result[2] = 1;
118  return result;
119  }
120 
123  static GfVec3h Axis(size_t i) {
124  GfVec3h result(0);
125  if (i < 3)
126  result[i] = 1;
127  return result;
128  }
129 
131  GfVec3h &Set(GfHalf s0, GfHalf s1, GfHalf s2) {
132  _data[0] = s0;
133  _data[1] = s1;
134  _data[2] = s2;
135  return *this;
136  }
137 
139  GfVec3h &Set(GfHalf const *a) {
140  return Set(a[0], a[1], a[2]);
141  }
142 
144  GfHalf const *data() const { return _data; }
145  GfHalf *data() { return _data; }
146  GfHalf const *GetArray() const { return data(); }
147 
149  GfHalf const &operator[](size_t i) const { return _data[i]; }
150  GfHalf &operator[](size_t i) { return _data[i]; }
151 
153  friend inline size_t hash_value(GfVec3h const &vec) {
154  size_t h = 0;
155  boost::hash_combine(h, vec[0]);
156  boost::hash_combine(h, vec[1]);
157  boost::hash_combine(h, vec[2]);
158  return h;
159  }
160 
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...
173  GF_API
174  bool operator==(class GfVec3d const &other) const;
176  GF_API
177  bool operator==(class GfVec3f const &other) const;
179  GF_API
180  bool operator==(class GfVec3i const &other) const;
181 
183  GfVec3h operator-() const {
184  return GfVec3h(-_data[0], -_data[1], -_data[2]);
185  }
186 
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 
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 
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 
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 
236  GfHalf operator*(GfVec3h const &v) const {
237  return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
238  }
239 
244  GfVec3h GetProjection(GfVec3h const &v) const {
245  return v * (*this * v);
246  }
247 
253  GfVec3h GetComplement(GfVec3h const &b) const {
254  return *this - this->GetProjection(b);
255  }
256 
258  GfHalf GetLengthSq() const {
259  return *this * *this;
260  }
261 
263  GfHalf GetLength() const {
264  // TODO should use GfSqrt.
265  return sqrt(GetLengthSq());
266  }
267 
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 
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 
309  GF_API
310  void BuildOrthonormalFrame(GfVec3h *v1, GfVec3h *v2,
311  GfHalf eps = 0.001) const;
312 
313 
314 private:
315  GfHalf _data[3];
316 };
317 
320 GF_API std::ostream& operator<<(std::ostream &, GfVec3h const &);
321 
322 
323 PXR_NAMESPACE_CLOSE_SCOPE
324 
325 #include "pxr/base/gf/vec3d.h"
326 #include "pxr/base/gf/vec3f.h"
327 #include "pxr/base/gf/vec3i.h"
328 
329 PXR_NAMESPACE_OPEN_SCOPE
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 
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 
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 
374 inline GfHalf
375 GfDot(GfVec3h const &v1, GfVec3h const &v2) {
376  return v1 * v2;
377 }
378 
379 
381 inline GfHalf
383 {
384  return v.GetLength();
385 }
386 
390 inline GfHalf
391 GfNormalize(GfVec3h *v, GfHalf eps = 0.001)
392 {
393  return v->Normalize(eps);
394 }
395 
399 inline GfVec3h
400 GfGetNormalized(GfVec3h const &v, GfHalf eps = 0.001)
401 {
402  return v.GetNormalized(eps);
403 }
404 
409 inline GfVec3h
410 GfGetProjection(GfVec3h const &a, GfVec3h const &b)
411 {
412  return a.GetProjection(b);
413 }
414 
419 inline GfVec3h
420 GfGetComplement(GfVec3h const &a, GfVec3h const &b)
421 {
422  return a.GetComplement(b);
423 }
424 
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
436 GfOrthogonalizeBasis(GfVec3h *tx, GfVec3h *ty, GfVec3h *tz,
437  bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
438 
439 GF_API void
440 GfBuildOrthonormalFrame(GfVec3h const &v0,
441  GfVec3h* v1,
442  GfVec3h* v2,
443  GfHalf eps = 0.001);
444 
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 
457 inline GfVec3h
458 operator^(GfVec3h const &v1, GfVec3h const &v2)
459 {
460  return GfCross(v1, v2);
461 }
462 
464 GF_API GfVec3h
465 GfSlerp(double alpha, GfVec3h const &v0, GfVec3h const &v1);
466 
467 
468 
469 PXR_NAMESPACE_CLOSE_SCOPE
470 
471 #endif // PXR_BASE_GF_VEC3H_H
constexpr GfVec3h(GfHalf value)
Initialize all elements to a single value.
Definition: vec3h.h:75
GfVec2d GfGetProjection(GfVec2d const &a, GfVec2d const &b)
Returns the projection of a onto b.
Definition: vec2d.h:369
bool GfIsClose(double a, double b, double epsilon)
Returns true if a and b are with epsilon of each other.
Definition: math.h:42
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec3h.h:276
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec3h.h:149
GfHalf operator*(GfVec3h const &v) const
See GfDot().
Definition: vec3h.h:236
constexpr GfVec3h(Scl const *p)
Construct with pointer to values.
Definition: vec3h.h:88
GfVec3d operator^(GfVec3d const &v1, GfVec3d const &v2)
Returns the cross product of v1 and v2.
Definition: vec3d.h:457
GfVec3h & operator+=(GfVec3h const &other)
Addition.
Definition: vec3h.h:188
decltype(std::declval< Left >()*std::declval< Right >()) GfCompMult(Left left, Right right)
Returns component-wise multiplication of vectors.
Definition: math.h:253
pxr_half::half GfHalf
A 16-bit floating point data type.
Definition: half.h:43
Assorted mathematical utility functions.
This header serves to simply bring in the half float datatype and provide a hash_value function...
GfVec3h & operator*=(double s)
Multiplication by scalar.
Definition: vec3h.h:210
Basic type for a vector of 3 float components.
Definition: vec3f.h:63
static GfVec3h Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec3h.h:123
GfVec3h & operator-=(GfVec3h const &other)
Subtraction.
Definition: vec3h.h:199
Low-level utilities for informing users of various internal and external diagnostic conditions...
A metafunction with a static const bool member &#39;value&#39; that is true for GfVec types, like GfVec2i, GfVec4d, etc and false for all other types.
Definition: traits.h:36
static GfVec3h XAxis()
Create a unit vector along the X-axis.
Definition: vec3h.h:103
GfVec3d GfCross(GfVec3d const &v1, GfVec3d const &v2)
Returns the cross product of v1 and v2.
Definition: vec3d.h:446
GfHalf GetLength() const
Length.
Definition: vec3h.h:263
static GfVec3h ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3h.h:115
GfVec3h GetProjection(GfVec3h const &v) const
Returns the projection of this onto v.
Definition: vec3h.h:244
GF_API GfQuatd GfSlerp(double alpha, const GfQuatd &q0, const GfQuatd &q1)
Spherically linearly interpolate between q0 and q1.
#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)
Division by scalar.
Definition: vec3h.h:225
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.
static GfVec3h YAxis()
Create a unit vector along the Y-axis.
Definition: vec3h.h:109
GfVec2d GfGetNormalized(GfVec2d const &v, double eps=GF_MIN_VECTOR_LENGTH)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec2d.h:359
decltype(std::declval< Left >()/std::declval< Right >()) GfCompDiv(Left left, Right right)
Returns component-wise quotient of vectors.
Definition: math.h:264
bool operator==(GfVec3h const &other) const
Equality comparison.
Definition: vec3h.h:162
decltype(std::declval< Left >()*std::declval< Right >()) GfDot(Left left, Right right)
Returns the dot (inner) product of two vectors.
Definition: math.h:242
Basic type for a vector of 3 int components.
Definition: vec3i.h:61
double GfNormalize(GfVec2d *v, double eps=GF_MIN_VECTOR_LENGTH)
Normalizes *v in place to unit length, returning the length before normalization. ...
Definition: vec2d.h:350
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.
GfHalf ScalarType
Scalar element type and dimension.
Definition: vec3h.h:68
GfVec3h operator-() const
Create a vec with negated elements.
Definition: vec3h.h:183
GfVec3h GetComplement(GfVec3h const &b) const
Returns the orthogonal complement of this-&gt;GetProjection(b).
Definition: vec3h.h:253
GfVec3h & Set(GfHalf s0, GfHalf s1, GfHalf s2)
Set all elements with passed arguments.
Definition: vec3h.h:131
friend size_t hash_value(GfVec3h const &vec)
Hash.
Definition: vec3h.h:153
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
GfVec3h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec3h.h:139
Basic type for a vector of 3 double components.
Definition: vec3d.h:63
Defines useful mathematical limits.
GfHalf GetLengthSq() const
Squared length.
Definition: vec3h.h:258
double GfGetLength(GfVec2d const &v)
Returns the geometric length of v.
Definition: vec2d.h:341
GfHalf const * data() const
Direct data access.
Definition: vec3h.h:144
GfVec2d GfGetComplement(GfVec2d const &a, GfVec2d const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec2d.h:379
Basic type for a vector of 3 GfHalf components.
Definition: vec3h.h:64
GfVec3h()=default
Default constructor does no initialization.
constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
Initialize all elements with explicit arguments.
Definition: vec3h.h:81