Loading...
Searching...
No Matches
vec3d.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_VEC3D_H
29#define PXR_BASE_GF_VEC3D_H
30
33
34#include "pxr/pxr.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/tf/hash.h"
41
42#include <cstddef>
43#include <cmath>
44
45#include <iosfwd>
46
47PXR_NAMESPACE_OPEN_SCOPE
48
49class GfVec3d;
50
51template <>
52struct GfIsGfVec<class GfVec3d> { static const bool value = true; };
53
63{
64public:
66 typedef double ScalarType;
67 static const size_t dimension = 3;
68
70 GfVec3d() = default;
71
73 constexpr explicit GfVec3d(double value)
74 : _data{ value, value, value }
75 {
76 }
77
79 constexpr GfVec3d(double s0, double s1, double s2)
80 : _data{ s0, s1, s2 }
81 {
82 }
83
85 template <class Scl>
86 constexpr explicit GfVec3d(Scl const *p)
87 : _data{ p[0], p[1], p[2] }
88 {
89 }
90
92 GfVec3d(class GfVec3f const &other);
93
95 GfVec3d(class GfVec3h const &other);
96
98 GfVec3d(class GfVec3i const &other);
99
101 static GfVec3d XAxis() {
102 GfVec3d result(0);
103 result[0] = 1;
104 return result;
105 }
107 static GfVec3d YAxis() {
108 GfVec3d result(0);
109 result[1] = 1;
110 return result;
111 }
113 static GfVec3d ZAxis() {
114 GfVec3d result(0);
115 result[2] = 1;
116 return result;
117 }
118
121 static GfVec3d Axis(size_t i) {
122 GfVec3d result(0);
123 if (i < 3)
124 result[i] = 1;
125 return result;
126 }
127
129 GfVec3d &Set(double s0, double s1, double s2) {
130 _data[0] = s0;
131 _data[1] = s1;
132 _data[2] = s2;
133 return *this;
134 }
135
137 GfVec3d &Set(double const *a) {
138 return Set(a[0], a[1], a[2]);
139 }
140
142 double const *data() const { return _data; }
143 double *data() { return _data; }
144 double const *GetArray() const { return data(); }
145
147 double const &operator[](size_t i) const { return _data[i]; }
148 double &operator[](size_t i) { return _data[i]; }
149
151 friend inline size_t hash_value(GfVec3d const &vec) {
152 return TfHash::Combine(vec[0], vec[1], vec[2]);
153 }
154
156 bool operator==(GfVec3d const &other) const {
157 return _data[0] == other[0] &&
158 _data[1] == other[1] &&
159 _data[2] == other[2];
160 }
161 bool operator!=(GfVec3d const &other) const {
162 return !(*this == other);
163 }
164
165 // TODO Add inequality for other vec types...
167 GF_API
168 bool operator==(class GfVec3f const &other) const;
170 GF_API
171 bool operator==(class GfVec3h const &other) const;
173 GF_API
174 bool operator==(class GfVec3i const &other) const;
175
178 return GfVec3d(-_data[0], -_data[1], -_data[2]);
179 }
180
182 GfVec3d &operator+=(GfVec3d const &other) {
183 _data[0] += other[0];
184 _data[1] += other[1];
185 _data[2] += other[2];
186 return *this;
187 }
188 friend GfVec3d operator+(GfVec3d const &l, GfVec3d const &r) {
189 return GfVec3d(l) += r;
190 }
191
193 GfVec3d &operator-=(GfVec3d const &other) {
194 _data[0] -= other[0];
195 _data[1] -= other[1];
196 _data[2] -= other[2];
197 return *this;
198 }
199 friend GfVec3d operator-(GfVec3d const &l, GfVec3d const &r) {
200 return GfVec3d(l) -= r;
201 }
202
204 GfVec3d &operator*=(double s) {
205 _data[0] *= s;
206 _data[1] *= s;
207 _data[2] *= s;
208 return *this;
209 }
210 GfVec3d operator*(double s) const {
211 return GfVec3d(*this) *= s;
212 }
213 friend GfVec3d operator*(double s, GfVec3d const &v) {
214 return v * s;
215 }
216
218 // TODO should divide by the scalar type.
219 GfVec3d &operator/=(double s) {
220 // TODO This should not multiply by 1/s, it should do the division.
221 // Doing the division is more numerically stable when s is close to
222 // zero.
223 return *this *= (1.0 / s);
224 }
225 GfVec3d operator/(double s) const {
226 return *this * (1.0 / s);
227 }
228
230 double operator*(GfVec3d const &v) const {
231 return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
232 }
233
238 GfVec3d GetProjection(GfVec3d const &v) const {
239 return v * (*this * v);
240 }
241
247 GfVec3d GetComplement(GfVec3d const &b) const {
248 return *this - this->GetProjection(b);
249 }
250
252 double GetLengthSq() const {
253 return *this * *this;
254 }
255
257 double GetLength() const {
258 return GfSqrt(GetLengthSq());
259 }
260
269 double Normalize(double eps = GF_MIN_VECTOR_LENGTH) {
270 // TODO this seems suspect... suggest dividing by length so long as
271 // length is not zero.
272 double length = GetLength();
273 *this /= (length > eps) ? length : eps;
274 return length;
275 }
276
277 GfVec3d GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const {
278 GfVec3d normalized(*this);
279 normalized.Normalize(eps);
280 return normalized;
281 }
282
292 GF_API
294 GfVec3d *tx, GfVec3d *ty, GfVec3d *tz,
295 const bool normalize,
296 double eps = GF_MIN_ORTHO_TOLERANCE);
297
302 GF_API
304 double eps = GF_MIN_VECTOR_LENGTH) const;
305
306
307private:
308 double _data[3];
309};
310
313GF_API std::ostream& operator<<(std::ostream &, GfVec3d const &);
314
315
316PXR_NAMESPACE_CLOSE_SCOPE
317
318#include "pxr/base/gf/vec3f.h"
319#include "pxr/base/gf/vec3h.h"
320#include "pxr/base/gf/vec3i.h"
321
322PXR_NAMESPACE_OPEN_SCOPE
323
324inline
325GfVec3d::GfVec3d(class GfVec3f const &other)
326{
327 _data[0] = other[0];
328 _data[1] = other[1];
329 _data[2] = other[2];
330}
331inline
332GfVec3d::GfVec3d(class GfVec3h const &other)
333{
334 _data[0] = other[0];
335 _data[1] = other[1];
336 _data[2] = other[2];
337}
338inline
339GfVec3d::GfVec3d(class GfVec3i const &other)
340{
341 _data[0] = other[0];
342 _data[1] = other[1];
343 _data[2] = other[2];
344}
345
347inline GfVec3d
348GfCompMult(GfVec3d const &v1, GfVec3d const &v2) {
349 return GfVec3d(
350 v1[0] * v2[0],
351 v1[1] * v2[1],
352 v1[2] * v2[2]
353 );
354}
355
357inline GfVec3d
358GfCompDiv(GfVec3d const &v1, GfVec3d const &v2) {
359 return GfVec3d(
360 v1[0] / v2[0],
361 v1[1] / v2[1],
362 v1[2] / v2[2]
363 );
364}
365
367inline double
368GfDot(GfVec3d const &v1, GfVec3d const &v2) {
369 return v1 * v2;
370}
371
372
374inline double
376{
377 return v.GetLength();
378}
379
383inline double
385{
386 return v->Normalize(eps);
387}
388
392inline GfVec3d
394{
395 return v.GetNormalized(eps);
396}
397
402inline GfVec3d
403GfGetProjection(GfVec3d const &a, GfVec3d const &b)
404{
405 return a.GetProjection(b);
406}
407
412inline GfVec3d
413GfGetComplement(GfVec3d const &a, GfVec3d const &b)
414{
415 return a.GetComplement(b);
416}
417
420inline bool
421GfIsClose(GfVec3d const &v1, GfVec3d const &v2, double tolerance)
422{
423 GfVec3d delta = v1 - v2;
424 return delta.GetLengthSq() <= tolerance * tolerance;
425}
426
427
428GF_API bool
429GfOrthogonalizeBasis(GfVec3d *tx, GfVec3d *ty, GfVec3d *tz,
430 bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
431
432GF_API void
433GfBuildOrthonormalFrame(GfVec3d const &v0,
434 GfVec3d* v1,
435 GfVec3d* v2,
436 double eps = GF_MIN_VECTOR_LENGTH);
437
439inline GfVec3d
440GfCross(GfVec3d const &v1, GfVec3d const &v2)
441{
442 return GfVec3d(
443 v1[1] * v2[2] - v1[2] * v2[1],
444 v1[2] * v2[0] - v1[0] * v2[2],
445 v1[0] * v2[1] - v1[1] * v2[0]);
446}
447
450inline GfVec3d
451operator^(GfVec3d const &v1, GfVec3d const &v2)
452{
453 return GfCross(v1, v2);
454}
455
457GF_API GfVec3d
458GfSlerp(double alpha, GfVec3d const &v0, GfVec3d const &v1);
459
460
461
462PXR_NAMESPACE_CLOSE_SCOPE
463
464#endif // PXR_BASE_GF_VEC3D_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
Basic type for a vector of 3 double components.
Definition: vec3d.h:63
GfVec3d operator-() const
Create a vec with negated elements.
Definition: vec3d.h:177
GfVec3d & Set(double const *a)
Set all elements with a pointer to data.
Definition: vec3d.h:137
GF_API bool operator==(class GfVec3h const &other) const
Equality comparison.
constexpr GfVec3d(double value)
Initialize all elements to a single value.
Definition: vec3d.h:73
static GfVec3d ZAxis()
Create a unit vector along the Z-axis.
Definition: vec3d.h:113
double GetLengthSq() const
Squared length.
Definition: vec3d.h:252
GfVec3d & operator+=(GfVec3d const &other)
Addition.
Definition: vec3d.h:182
GF_API void BuildOrthonormalFrame(GfVec3d *v1, GfVec3d *v2, double eps=GF_MIN_VECTOR_LENGTH) const
Sets v1 and v2 to unit vectors such that v1, v2 and *this are mutually orthogonal.
constexpr GfVec3d(Scl const *p)
Construct with pointer to values.
Definition: vec3d.h:86
GfVec3d & operator*=(double s)
Multiplication by scalar.
Definition: vec3d.h:204
GF_API bool operator==(class GfVec3f const &other) const
Equality comparison.
GfVec3d GetProjection(GfVec3d const &v) const
Returns the projection of this onto v.
Definition: vec3d.h:238
double ScalarType
Scalar element type and dimension.
Definition: vec3d.h:66
double const * data() const
Direct data access.
Definition: vec3d.h:142
double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec3d.h:269
static GfVec3d XAxis()
Create a unit vector along the X-axis.
Definition: vec3d.h:101
GfVec3d & operator-=(GfVec3d const &other)
Subtraction.
Definition: vec3d.h:193
static GfVec3d YAxis()
Create a unit vector along the Y-axis.
Definition: vec3d.h:107
static GfVec3d Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec3d.h:121
GfVec3d & Set(double s0, double s1, double s2)
Set all elements with passed arguments.
Definition: vec3d.h:129
bool operator==(GfVec3d const &other) const
Equality comparison.
Definition: vec3d.h:156
friend size_t hash_value(GfVec3d const &vec)
Hash.
Definition: vec3d.h:151
GfVec3d()=default
Default constructor does no initialization.
double const & operator[](size_t i) const
Indexing.
Definition: vec3d.h:147
GF_API bool operator==(class GfVec3i const &other) const
Equality comparison.
static GF_API bool OrthogonalizeBasis(GfVec3d *tx, GfVec3d *ty, GfVec3d *tz, const bool normalize, double eps=GF_MIN_ORTHO_TOLERANCE)
Orthogonalize and optionally normalize a set of basis vectors.
GfVec3d GetComplement(GfVec3d const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec3d.h:247
double GetLength() const
Length.
Definition: vec3d.h:257
double operator*(GfVec3d const &v) const
See GfDot().
Definition: vec3d.h:230
GfVec3d & operator/=(double s)
Division by scalar.
Definition: vec3d.h:219
constexpr GfVec3d(double s0, double s1, double s2)
Initialize all elements with explicit arguments.
Definition: vec3d.h:79
Basic type for a vector of 3 float components.
Definition: vec3f.h:63
Basic type for a vector of 3 GfHalf components.
Definition: vec3h.h:64
Basic type for a vector of 3 int components.
Definition: vec3i.h:61
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:492
Assorted mathematical utility functions.
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:80
#define GF_MIN_VECTOR_LENGTH
This constant is used to determine whether the length of a vector is too small to handle accurately.
Definition: limits.h:34
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
#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
Defines useful mathematical limits.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition: traits.h:36
double GfDot(GfVec3d const &v1, GfVec3d const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec3d.h:368
GfVec3d operator^(GfVec3d const &v1, GfVec3d const &v2)
Returns the cross product of v1 and v2.
Definition: vec3d.h:451
GfVec3d GfGetNormalized(GfVec3d const &v, double eps=GF_MIN_VECTOR_LENGTH)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec3d.h:393
double GfNormalize(GfVec3d *v, double eps=GF_MIN_VECTOR_LENGTH)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec3d.h:384
GF_API GfVec3d GfSlerp(double alpha, GfVec3d const &v0, GfVec3d const &v1)
Spherical linear interpolation in three dimensions.
GfVec3d GfCompDiv(GfVec3d const &v1, GfVec3d const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec3d.h:358
GfVec3d GfGetComplement(GfVec3d const &a, GfVec3d const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec3d.h:413
double GfGetLength(GfVec3d const &v)
Returns the geometric length of v.
Definition: vec3d.h:375
GfVec3d GfCompMult(GfVec3d const &v1, GfVec3d const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec3d.h:348
bool GfIsClose(GfVec3d const &v1, GfVec3d const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition: vec3d.h:421
GfVec3d GfGetProjection(GfVec3d const &a, GfVec3d const &b)
Returns the projection of a onto b.
Definition: vec3d.h:403
GfVec3d GfCross(GfVec3d const &v1, GfVec3d const &v2)
Returns the cross product of v1 and v2.
Definition: vec3d.h:440