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  return GfSqrt(GetLengthSq());
265  }
266 
275  GfHalf Normalize(GfHalf eps = 0.001) {
276  // TODO this seems suspect... suggest dividing by length so long as
277  // length is not zero.
278  GfHalf length = GetLength();
279  *this /= (length > eps) ? length : eps;
280  return length;
281  }
282 
283  GfVec3h GetNormalized(GfHalf eps = 0.001) const {
284  GfVec3h normalized(*this);
285  normalized.Normalize(eps);
286  return normalized;
287  }
288 
298  GF_API
299  static bool OrthogonalizeBasis(
300  GfVec3h *tx, GfVec3h *ty, GfVec3h *tz,
301  const bool normalize,
302  double eps = GF_MIN_ORTHO_TOLERANCE);
303 
308  GF_API
309  void BuildOrthonormalFrame(GfVec3h *v1, GfVec3h *v2,
310  GfHalf eps = 0.001) const;
311 
312 
313 private:
314  GfHalf _data[3];
315 };
316 
319 GF_API std::ostream& operator<<(std::ostream &, GfVec3h const &);
320 
321 
322 PXR_NAMESPACE_CLOSE_SCOPE
323 
324 #include "pxr/base/gf/vec3d.h"
325 #include "pxr/base/gf/vec3f.h"
326 #include "pxr/base/gf/vec3i.h"
327 
328 PXR_NAMESPACE_OPEN_SCOPE
329 
330 inline
331 GfVec3h::GfVec3h(class GfVec3d const &other)
332 {
333  _data[0] = other[0];
334  _data[1] = other[1];
335  _data[2] = other[2];
336 }
337 inline
338 GfVec3h::GfVec3h(class GfVec3f const &other)
339 {
340  _data[0] = other[0];
341  _data[1] = other[1];
342  _data[2] = other[2];
343 }
344 inline
345 GfVec3h::GfVec3h(class GfVec3i const &other)
346 {
347  _data[0] = other[0];
348  _data[1] = other[1];
349  _data[2] = other[2];
350 }
351 
353 inline GfVec3h
354 GfCompMult(GfVec3h const &v1, GfVec3h const &v2) {
355  return GfVec3h(
356  v1[0] * v2[0],
357  v1[1] * v2[1],
358  v1[2] * v2[2]
359  );
360 }
361 
363 inline GfVec3h
364 GfCompDiv(GfVec3h const &v1, GfVec3h const &v2) {
365  return GfVec3h(
366  v1[0] / v2[0],
367  v1[1] / v2[1],
368  v1[2] / v2[2]
369  );
370 }
371 
373 inline GfHalf
374 GfDot(GfVec3h const &v1, GfVec3h const &v2) {
375  return v1 * v2;
376 }
377 
378 
380 inline GfHalf
382 {
383  return v.GetLength();
384 }
385 
389 inline GfHalf
390 GfNormalize(GfVec3h *v, GfHalf eps = 0.001)
391 {
392  return v->Normalize(eps);
393 }
394 
398 inline GfVec3h
399 GfGetNormalized(GfVec3h const &v, GfHalf eps = 0.001)
400 {
401  return v.GetNormalized(eps);
402 }
403 
408 inline GfVec3h
409 GfGetProjection(GfVec3h const &a, GfVec3h const &b)
410 {
411  return a.GetProjection(b);
412 }
413 
418 inline GfVec3h
419 GfGetComplement(GfVec3h const &a, GfVec3h const &b)
420 {
421  return a.GetComplement(b);
422 }
423 
426 inline bool
427 GfIsClose(GfVec3h const &v1, GfVec3h const &v2, double tolerance)
428 {
429  GfVec3h delta = v1 - v2;
430  return delta.GetLengthSq() <= tolerance * tolerance;
431 }
432 
433 
434 GF_API bool
435 GfOrthogonalizeBasis(GfVec3h *tx, GfVec3h *ty, GfVec3h *tz,
436  bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
437 
438 GF_API void
439 GfBuildOrthonormalFrame(GfVec3h const &v0,
440  GfVec3h* v1,
441  GfVec3h* v2,
442  GfHalf eps = 0.001);
443 
445 inline GfVec3h
446 GfCross(GfVec3h const &v1, GfVec3h const &v2)
447 {
448  return GfVec3h(
449  v1[1] * v2[2] - v1[2] * v2[1],
450  v1[2] * v2[0] - v1[0] * v2[2],
451  v1[0] * v2[1] - v1[1] * v2[0]);
452 }
453 
456 inline GfVec3h
457 operator^(GfVec3h const &v1, GfVec3h const &v2)
458 {
459  return GfCross(v1, v2);
460 }
461 
463 GF_API GfVec3h
464 GfSlerp(double alpha, GfVec3h const &v0, GfVec3h const &v1);
465 
466 
467 
468 PXR_NAMESPACE_CLOSE_SCOPE
469 
470 #endif // PXR_BASE_GF_VEC3H_H
GfVec3h GetComplement(GfVec3h const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec3h.h:253
GfVec3h GfCross(GfVec3h const &v1, GfVec3h const &v2)
Returns the cross product of v1 and v2.
Definition: vec3h.h:446
GfHalf GfGetLength(GfVec3h const &v)
Returns the geometric length of v.
Definition: vec3h.h:381
constexpr GfVec3h(GfHalf value)
Initialize all elements to a single value.
Definition: vec3h.h:75
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec3h.h:149
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec3h.h:275
constexpr GfVec3h(Scl const *p)
Construct with pointer to values.
Definition: vec3h.h:88
GfVec3h & operator+=(GfVec3h const &other)
Addition.
Definition: vec3h.h:188
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.
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
GfHalf GfDot(GfVec3h const &v1, GfVec3h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec3h.h:374
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 '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:103
static GfVec3h ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3h.h:115
#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:210
GfVec3h & operator/=(double s)
Division by scalar.
Definition: vec3h.h:225
static GfVec3h YAxis()
Create a unit vector along the Y-axis.
Definition: vec3h.h:109
GfVec3h operator-() const
Create a vec with negated elements.
Definition: vec3h.h:183
GfHalf GetLengthSq() const
Squared length.
Definition: vec3h.h:258
GfVec3h GfCompDiv(GfVec3h const &v1, GfVec3h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec3h.h:364
GfVec3h GfGetComplement(GfVec3h const &a, GfVec3h const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec3h.h:419
Basic type for a vector of 3 int components.
Definition: vec3i.h:61
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:68
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:131
GfVec3h GetProjection(GfVec3h const &v) const
Returns the projection of this onto v.
Definition: vec3h.h:244
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
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:427
GfVec3h operator^(GfVec3h const &v1, GfVec3h const &v2)
Returns the cross product of v1 and v2.
Definition: vec3h.h:457
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.
GfVec3h GfGetProjection(GfVec3h const &a, GfVec3h const &b)
Returns the projection of a onto b.
Definition: vec3h.h:409
GfHalf GfNormalize(GfVec3h *v, GfHalf eps=0.001)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec3h.h:390
GfVec3h GfGetNormalized(GfVec3h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec3h.h:399
GfHalf GetLength() const
Length.
Definition: vec3h.h:263
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
GfVec3h GfCompMult(GfVec3h const &v1, GfVec3h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec3h.h:354
GfHalf const * data() const
Direct data access.
Definition: vec3h.h:144
bool operator==(GfVec3h const &other) const
Equality comparison.
Definition: vec3h.h:162