Loading...
Searching...
No Matches
vec3h.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
8// This file is generated by a script. Do not edit directly. Edit the
9// vec.template.h file to make changes.
10
11#ifndef PXR_BASE_GF_VEC3H_H
12#define PXR_BASE_GF_VEC3H_H
13
16
17#include "pxr/pxr.h"
18#include "pxr/base/gf/api.h"
19#include "pxr/base/gf/limits.h"
20#include "pxr/base/gf/traits.h"
21#include "pxr/base/gf/math.h"
22#include "pxr/base/gf/half.h"
24#include "pxr/base/tf/hash.h"
25
26#include <cstddef>
27#include <type_traits>
28#include <cmath>
29
30#include <iosfwd>
31
32PXR_NAMESPACE_OPEN_SCOPE
33
34class GfVec2h;
35
36template <>
37struct GfIsGfVec<class GfVec3h> { static const bool value = true; };
38
48{
49public:
52 static const size_t dimension = 3;
53
65 GfHalf x, y, z;
67
70 GfVec3h() = default;
71
73 constexpr explicit GfVec3h(GfHalf value)
74 : x(value), y(value), z(value)
75 {
76 }
77
79 constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
80 : x(s0), y(s1), z(s2)
81 {
82 }
83
85 template <class Scl>
86 constexpr explicit GfVec3h(Scl const *p)
87 : x(p[0]), y(p[1]), z(p[2])
88 {
89 }
90
92 explicit GfVec3h(class GfVec3d const &other);
93
95 explicit GfVec3h(class GfVec3f const &other);
96
98 GfVec3h(class GfVec3i const &other);
100 static GfVec3h XAxis() {
101 GfVec3h result(0);
102 result[0] = 1;
103 return result;
104 }
105
107 static GfVec3h YAxis() {
108 GfVec3h result(0);
109 result[1] = 1;
110 return result;
111 }
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
131 x = s0;
132 y = s1;
133 z = s2;
134 return *this;
135 }
136
138 GfVec3h &Set(GfHalf const *a) {
139 return Set(a[0], a[1], a[2]);
140 }
141
147 GfHalf const *data() const { return &x; }
148 GfHalf *data() { return &x; }
149 GfHalf const *GetArray() const { return data(); }
150
152 GfHalf const &operator[](size_t i) const { return data()[i]; }
153 GfHalf &operator[](size_t i) { return data()[i]; }
154
156 friend inline size_t hash_value(GfVec3h const &vec) {
157 return TfHash::Combine(vec.x, vec.y, vec.z);
158 }
159
161 bool operator==(GfVec3h const &other) const {
162 return x == other.x &&
163 y == other.y &&
164 z == other.z;
165 }
166 bool operator!=(GfVec3h const &other) const {
167 return !(*this == other);
168 }
169
170 // TODO Add inequality for other vec types...
172 GF_API
173 bool operator==(class GfVec3d const &other) const;
175 GF_API
176 bool operator==(class GfVec3f const &other) const;
178 GF_API
179 bool operator==(class GfVec3i const &other) const;
180
183 return GfVec3h(-x, -y, -z);
184 }
185
187 GfVec3h &operator+=(GfVec3h const &other) {
188 x += other.x;
189 y += other.y;
190 z += other.z;
191 return *this;
192 }
193 friend GfVec3h operator+(GfVec3h const &l, GfVec3h const &r) {
194 return GfVec3h(l) += r;
195 }
196
198 GfVec3h &operator-=(GfVec3h const &other) {
199 x -= other.x;
200 y -= other.y;
201 z -= other.z;
202 return *this;
203 }
204 friend GfVec3h operator-(GfVec3h const &l, GfVec3h const &r) {
205 return GfVec3h(l) -= r;
206 }
207
209 GfVec3h &operator*=(double s) {
210 x *= s;
211 y *= s;
212 z *= s;
213 return *this;
214 }
215 GfVec3h operator*(double s) const {
216 return GfVec3h(*this) *= s;
217 }
218 friend GfVec3h operator*(double s, GfVec3h const &v) {
219 return v * s;
220 }
221
223 // TODO should divide by the scalar type.
224 GfVec3h &operator/=(double s) {
225 // TODO This should not multiply by 1/s, it should do the division.
226 // Doing the division is more numerically stable when s is close to
227 // zero.
228 return *this *= (1.0 / s);
229 }
230 GfVec3h operator/(double s) const {
231 return *this * (1.0 / s);
232 }
233
235 GfHalf operator*(GfVec3h const &v) const {
236 return x * v.x + y * v.y + z * v.z;
237 }
238
243 GfVec3h GetProjection(GfVec3h const &v) const {
244 return v * (*this * v);
245 }
246
252 GfVec3h GetComplement(GfVec3h const &b) const {
253 return *this - this->GetProjection(b);
254 }
255
258 return *this * *this;
259 }
260
263 return GfSqrt(GetLengthSq());
264 }
265
274 GfHalf Normalize(GfHalf eps = 0.001) {
275 // TODO this seems suspect... suggest dividing by length so long as
276 // length is not zero.
277 GfHalf length = GetLength();
278 *this /= (length > eps) ? length : eps;
279 return length;
280 }
281
282 GfVec3h GetNormalized(GfHalf eps = 0.001) const {
283 GfVec3h normalized(*this);
284 normalized.Normalize(eps);
285 return normalized;
286 }
287
297 GF_API
299 GfVec3h *tx, GfVec3h *ty, GfVec3h *tz,
300 const bool normalize,
301 double eps = GF_MIN_ORTHO_TOLERANCE);
302
307 GF_API
309 GfHalf eps = 0.001) const;
310
311
327#if defined (doxygen)
328 constexpr GfVec2h xy() const;
329 constexpr GfVec2h xz() const;
330 constexpr GfVec2h yx() const;
331 constexpr GfVec2h yz() const;
332 constexpr GfVec2h zx() const;
333 constexpr GfVec2h zy() const;
334 constexpr GfVec3h xyz() const;
335 constexpr GfVec3h xzy() const;
336 constexpr GfVec3h yxz() const;
337 constexpr GfVec3h yzx() const;
338 constexpr GfVec3h zxy() const;
339 constexpr GfVec3h zyx() const;
341#else // !doxygen
342 // Note that these are templated on their own return type so that the
343 // declarations cost nothing in compile time & debug info unless they are
344 // actually called in a TU. Naming a different type is an error and
345 // static_assert()ed against.
346 template <class Vec = GfVec2h> constexpr Vec xy() const;
347 template <class Vec = GfVec2h> constexpr Vec xz() const;
348 template <class Vec = GfVec2h> constexpr Vec yx() const;
349 template <class Vec = GfVec2h> constexpr Vec yz() const;
350 template <class Vec = GfVec2h> constexpr Vec zx() const;
351 template <class Vec = GfVec2h> constexpr Vec zy() const;
352 template <class Vec = GfVec3h> constexpr Vec xyz() const;
353 template <class Vec = GfVec3h> constexpr Vec xzy() const;
354 template <class Vec = GfVec3h> constexpr Vec yxz() const;
355 template <class Vec = GfVec3h> constexpr Vec yzx() const;
356 template <class Vec = GfVec3h> constexpr Vec zxy() const;
357 template <class Vec = GfVec3h> constexpr Vec zyx() const;
358#endif // doxygen
359 };
360
363GF_API std::ostream& operator<<(std::ostream &, GfVec3h const &);
364
365PXR_NAMESPACE_CLOSE_SCOPE
366
367// Other scalar types of the same dimension, for the conversions above.
368#include "pxr/base/gf/vec3d.h"
369#include "pxr/base/gf/vec3f.h"
370#include "pxr/base/gf/vec3i.h"
371// Lower dimensions of the same scalar type, for the swizzles above. Note that
372// lower dimensions never include higher ones, so this introduces no cycle.
373#include "pxr/base/gf/vec2h.h"
374
375PXR_NAMESPACE_OPEN_SCOPE
376
377inline
378GfVec3h::GfVec3h(class GfVec3d const &other)
379{
380 x = other.x;
381 y = other.y;
382 z = other.z;
383}
384inline
385GfVec3h::GfVec3h(class GfVec3f const &other)
386{
387 x = other.x;
388 y = other.y;
389 z = other.z;
390}
391inline
392GfVec3h::GfVec3h(class GfVec3i const &other)
393{
394 x = other.x;
395 y = other.y;
396 z = other.z;
397}
398
399template <class Vec>
400constexpr Vec
401GfVec3h::xy() const
402{
403 static_assert(std::is_same<Vec, GfVec2h>::value);
404 return Vec(x, y);
405}
406
407template <class Vec>
408constexpr Vec
409GfVec3h::xz() const
410{
411 static_assert(std::is_same<Vec, GfVec2h>::value);
412 return Vec(x, z);
413}
414
415template <class Vec>
416constexpr Vec
417GfVec3h::yx() const
418{
419 static_assert(std::is_same<Vec, GfVec2h>::value);
420 return Vec(y, x);
421}
422
423template <class Vec>
424constexpr Vec
425GfVec3h::yz() const
426{
427 static_assert(std::is_same<Vec, GfVec2h>::value);
428 return Vec(y, z);
429}
430
431template <class Vec>
432constexpr Vec
433GfVec3h::zx() const
434{
435 static_assert(std::is_same<Vec, GfVec2h>::value);
436 return Vec(z, x);
437}
438
439template <class Vec>
440constexpr Vec
441GfVec3h::zy() const
442{
443 static_assert(std::is_same<Vec, GfVec2h>::value);
444 return Vec(z, y);
445}
446
447template <class Vec>
448constexpr Vec
449GfVec3h::xyz() const
450{
451 static_assert(std::is_same<Vec, GfVec3h>::value);
452 return Vec(x, y, z);
453}
454
455template <class Vec>
456constexpr Vec
457GfVec3h::xzy() const
458{
459 static_assert(std::is_same<Vec, GfVec3h>::value);
460 return Vec(x, z, y);
461}
462
463template <class Vec>
464constexpr Vec
465GfVec3h::yxz() const
466{
467 static_assert(std::is_same<Vec, GfVec3h>::value);
468 return Vec(y, x, z);
469}
470
471template <class Vec>
472constexpr Vec
473GfVec3h::yzx() const
474{
475 static_assert(std::is_same<Vec, GfVec3h>::value);
476 return Vec(y, z, x);
477}
478
479template <class Vec>
480constexpr Vec
481GfVec3h::zxy() const
482{
483 static_assert(std::is_same<Vec, GfVec3h>::value);
484 return Vec(z, x, y);
485}
486
487template <class Vec>
488constexpr Vec
489GfVec3h::zyx() const
490{
491 static_assert(std::is_same<Vec, GfVec3h>::value);
492 return Vec(z, y, x);
493}
494
495
497inline GfVec3h
498GfCompMult(GfVec3h const &v1, GfVec3h const &v2) {
499 return GfVec3h(
500 v1.x * v2.x,
501 v1.y * v2.y,
502 v1.z * v2.z
503 );
504}
505
507inline GfVec3h
508GfCompDiv(GfVec3h const &v1, GfVec3h const &v2) {
509 return GfVec3h(
510 v1.x / v2.x,
511 v1.y / v2.y,
512 v1.z / v2.z
513 );
514}
515
517inline GfHalf
518GfDot(GfVec3h const &v1, GfVec3h const &v2) {
519 return v1 * v2;
520}
521
522
524inline GfHalf
526{
527 return v.GetLength();
528}
529
533inline GfHalf
534GfNormalize(GfVec3h *v, GfHalf eps = 0.001)
535{
536 return v->Normalize(eps);
537}
538
542inline GfVec3h
543GfGetNormalized(GfVec3h const &v, GfHalf eps = 0.001)
544{
545 return v.GetNormalized(eps);
546}
547
552inline GfVec3h
553GfGetProjection(GfVec3h const &a, GfVec3h const &b)
554{
555 return a.GetProjection(b);
556}
557
562inline GfVec3h
563GfGetComplement(GfVec3h const &a, GfVec3h const &b)
564{
565 return a.GetComplement(b);
566}
567
570inline bool
571GfIsClose(GfVec3h const &v1, GfVec3h const &v2, double tolerance)
572{
573 GfVec3h delta = v1 - v2;
574 return delta.GetLengthSq() <= tolerance * tolerance;
575}
576
577
578GF_API bool
579GfOrthogonalizeBasis(GfVec3h *tx, GfVec3h *ty, GfVec3h *tz,
580 bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
581
582GF_API void
583GfBuildOrthonormalFrame(GfVec3h const &v0,
584 GfVec3h* v1,
585 GfVec3h* v2,
586 GfHalf eps = 0.001);
587
589inline GfVec3h
590GfCross(GfVec3h const &v1, GfVec3h const &v2)
591{
592 return GfVec3h(
593 v1.y * v2.z - v1.z * v2.y,
594 v1.z * v2.x - v1.x * v2.z,
595 v1.x * v2.y - v1.y * v2.x);
596}
597
600inline GfVec3h
601operator^(GfVec3h const &v1, GfVec3h const &v2)
602{
603 return GfCross(v1, v2);
604}
605
607GF_API GfVec3h
608GfSlerp(double alpha, GfVec3h const &v0, GfVec3h const &v1);
609
610
611
612PXR_NAMESPACE_CLOSE_SCOPE
613
614#endif // PXR_BASE_GF_VEC3H_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
Basic type for a vector of 2 GfHalf components.
Definition vec2h.h:47
Basic type for a vector of 3 double components.
Definition vec3d.h:47
Basic type for a vector of 3 float components.
Definition vec3f.h:47
Basic type for a vector of 3 GfHalf components.
Definition vec3h.h:48
GfVec3h & operator/=(double s)
Division by scalar.
Definition vec3h.h:224
GfHalf operator*(GfVec3h const &v) const
See GfDot().
Definition vec3h.h:235
GfHalf ScalarType
Scalar element type and dimension.
Definition vec3h.h:51
GfVec3h & operator-=(GfVec3h const &other)
Subtraction.
Definition vec3h.h:198
GfVec3h operator-() const
Create a vec with negated elements.
Definition vec3h.h:182
GfVec3h & operator*=(double s)
Multiplication by scalar.
Definition vec3h.h:209
GF_API bool operator==(class GfVec3d const &other) const
Equality comparison.
GfVec3h & operator+=(GfVec3h const &other)
Addition.
Definition vec3h.h:187
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 GetLength() const
Length.
Definition vec3h.h:262
GF_API bool operator==(class GfVec3f const &other) const
Equality comparison.
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.
GfVec3h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition vec3h.h:138
GfVec3h GetComplement(GfVec3h const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition vec3h.h:252
constexpr GfVec3h(GfHalf value)
Initialize all elements to a single value.
Definition vec3h.h:73
friend size_t hash_value(GfVec3h const &vec)
Hash.
Definition vec3h.h:156
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition vec3h.h:274
constexpr GfVec3h(Scl const *p)
Construct with pointer to values.
Definition vec3h.h:86
bool operator==(GfVec3h const &other) const
Equality comparison.
Definition vec3h.h:161
GfHalf GetLengthSq() const
Squared length.
Definition vec3h.h:257
static GfVec3h XAxis()
Create a unit vector along the X-axis.
Definition vec3h.h:100
GfVec3h()=default
GfVec3h value-initializes to zero and performs no default initialization, like float or double.
static GfVec3h ZAxis()
Create a unit vector along the Z-axis.
Definition vec3h.h:114
GfVec3h & Set(GfHalf s0, GfHalf s1, GfHalf s2)
Set all elements with passed arguments.
Definition vec3h.h:130
GfHalf const & operator[](size_t i) const
Indexing.
Definition vec3h.h:152
GfHalf const * data() const
Direct data access.
Definition vec3h.h:147
static GfVec3h YAxis()
Create a unit vector along the Y-axis.
Definition vec3h.h:107
GF_API bool operator==(class GfVec3i const &other) const
Equality comparison.
constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
Initialize all elements with explicit arguments.
Definition vec3h.h:79
static GfVec3h Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition vec3h.h:122
GfVec3h GetProjection(GfVec3h const &v) const
Returns the projection of this onto v.
Definition vec3h.h:243
Basic type for a vector of 3 int components.
Definition vec3i.h:45
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition hash.h:487
Assorted mathematical utility functions.
double GfSqrt(double f)
Return sqrt(f).
Definition math.h:190
GF_API std::ostream & operator<<(std::ostream &, GfVec3h const &)
Output a GfVec3h.
#define GF_MIN_ORTHO_TOLERANCE
This constant is used to determine when a set of basis vectors is close to orthogonal.
Definition limits.h:22
This header serves to simply bring in the half float datatype and provide a hash_value function.
pxr_half::half GfHalf
A 16-bit floating point data type.
Definition half.h:26
Defines useful mathematical limits.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition traits.h:23
GF_API GfVec3h GfSlerp(double alpha, GfVec3h const &v0, GfVec3h const &v1)
Spherical linear interpolation in three dimensions.
GfVec3h GfCompDiv(GfVec3h const &v1, GfVec3h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition vec3h.h:508
GfHalf GfGetLength(GfVec3h const &v)
Returns the geometric length of v.
Definition vec3h.h:525
GfVec3h GfGetProjection(GfVec3h const &a, GfVec3h const &b)
Returns the projection of a onto b.
Definition vec3h.h:553
GfVec3h GfGetNormalized(GfVec3h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition vec3h.h:543
GfHalf GfDot(GfVec3h const &v1, GfVec3h const &v2)
Returns the dot (inner) product of two vectors.
Definition vec3h.h:518
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:571
GfVec3h GfGetComplement(GfVec3h const &a, GfVec3h const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition vec3h.h:563
GfVec3h operator^(GfVec3h const &v1, GfVec3h const &v2)
Returns the cross product of v1 and v2.
Definition vec3h.h:601
GfVec3h GfCompMult(GfVec3h const &v1, GfVec3h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition vec3h.h:498
GfVec3h GfCross(GfVec3h const &v1, GfVec3h const &v2)
Returns the cross product of v1 and v2.
Definition vec3h.h:590
GfHalf GfNormalize(GfVec3h *v, GfHalf eps=0.001)
Normalizes *v in place to unit length, returning the length before normalization.
Definition vec3h.h:534