HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vec4d.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 //
7 ////////////////////////////////////////////////////////////////////////
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_VEC4D_H
12 #define PXR_BASE_GF_VEC4D_H
13 
14 /// \file gf/vec4d.h
15 /// \ingroup group_gf_LinearAlgebra
16 
17 #include "pxr/pxr.h"
18 #include "pxr/base/tf/diagnostic.h"
19 #include "pxr/base/gf/api.h"
20 #include "pxr/base/gf/limits.h"
21 #include "pxr/base/gf/traits.h"
22 #include "pxr/base/gf/math.h"
23 #include "pxr/base/tf/hash.h"
24 
25 #include <cstddef>
26 #include <cmath>
27 
28 #include <iosfwd>
29 
31 
32 class GfVec4d;
33 
34 template <>
35 struct GfIsGfVec<class GfVec4d> { static const bool value = true; };
36 
37 /// \class GfVec4d
38 /// \ingroup group_gf_LinearAlgebra
39 ///
40 /// Basic type for a vector of 4 double components.
41 ///
42 /// Represents a vector of 4 components of type \c double.
43 /// It is intended to be fast and simple.
44 ///
45 class GfVec4d
46 {
47 public:
48  /// Scalar element type and dimension.
49  typedef double ScalarType;
50  static const size_t dimension = 4;
51 
52  /// Default constructor does no initialization.
53  GfVec4d() = default;
54 
55  /// Initialize all elements to a single value.
56  constexpr explicit GfVec4d(double value)
57  : _data{ value, value, value, value }
58  {
59  }
60 
61  /// Initialize all elements with explicit arguments.
62  constexpr GfVec4d(double s0, double s1, double s2, double s3)
63  : _data{ s0, s1, s2, s3 }
64  {
65  }
66 
67  /// Construct with pointer to values.
68  template <class Scl>
69  constexpr explicit GfVec4d(Scl const *p)
70  : _data{ p[0], p[1], p[2], p[3] }
71  {
72  }
73 
74  /// Implicitly convert from GfVec4f.
75  GfVec4d(class GfVec4f const &other);
76 
77  /// Implicitly convert from GfVec4h.
78  GfVec4d(class GfVec4h const &other);
79 
80  /// Implicitly convert from GfVec4i.
81  GfVec4d(class GfVec4i const &other);
82 
83  /// Create a unit vector along the X-axis.
84  static GfVec4d XAxis() {
85  GfVec4d result(0);
86  result[0] = 1;
87  return result;
88  }
89  /// Create a unit vector along the Y-axis.
90  static GfVec4d YAxis() {
91  GfVec4d result(0);
92  result[1] = 1;
93  return result;
94  }
95  /// Create a unit vector along the Z-axis.
96  static GfVec4d ZAxis() {
97  GfVec4d result(0);
98  result[2] = 1;
99  return result;
100  }
101  /// Create a unit vector along the W-axis.
102  static GfVec4d WAxis() {
103  GfVec4d result(0);
104  result[3] = 1;
105  return result;
106  }
107 
108  /// Create a unit vector along the i-th axis, zero-based. Return the zero
109  /// vector if \p i is greater than or equal to 4.
110  static GfVec4d Axis(size_t i) {
111  GfVec4d result(0);
112  if (i < 4)
113  result[i] = 1;
114  return result;
115  }
116 
117  /// Set all elements with passed arguments.
118  GfVec4d &Set(double s0, double s1, double s2, double s3) {
119  _data[0] = s0;
120  _data[1] = s1;
121  _data[2] = s2;
122  _data[3] = s3;
123  return *this;
124  }
125 
126  /// Set all elements with a pointer to data.
127  GfVec4d &Set(double const *a) {
128  return Set(a[0], a[1], a[2], a[3]);
129  }
130 
131  /// Direct data access.
132  double const *data() const { return _data; }
133  double *data() { return _data; }
134  double const *GetArray() const { return data(); }
135 
136  /// Indexing.
137  double const &operator[](size_t i) const { return _data[i]; }
138  double &operator[](size_t i) { return _data[i]; }
139 
140  /// Hash.
141  friend inline size_t hash_value(GfVec4d const &vec) {
142  return TfHash::Combine(vec[0], vec[1], vec[2], vec[3]);
143  }
144 
145  /// Equality comparison.
146  bool operator==(GfVec4d const &other) const {
147  return _data[0] == other[0] &&
148  _data[1] == other[1] &&
149  _data[2] == other[2] &&
150  _data[3] == other[3];
151  }
152  bool operator!=(GfVec4d const &other) const {
153  return !(*this == other);
154  }
155 
156  // TODO Add inequality for other vec types...
157  /// Equality comparison.
158  GF_API
159  bool operator==(class GfVec4f const &other) const;
160  /// Equality comparison.
161  GF_API
162  bool operator==(class GfVec4h const &other) const;
163  /// Equality comparison.
164  GF_API
165  bool operator==(class GfVec4i const &other) const;
166 
167  /// Create a vec with negated elements.
168  GfVec4d operator-() const {
169  return GfVec4d(-_data[0], -_data[1], -_data[2], -_data[3]);
170  }
171 
172  /// Addition.
173  GfVec4d &operator+=(GfVec4d const &other) {
174  _data[0] += other[0];
175  _data[1] += other[1];
176  _data[2] += other[2];
177  _data[3] += other[3];
178  return *this;
179  }
180  friend GfVec4d operator+(GfVec4d const &l, GfVec4d const &r) {
181  return GfVec4d(l) += r;
182  }
183 
184  /// Subtraction.
185  GfVec4d &operator-=(GfVec4d const &other) {
186  _data[0] -= other[0];
187  _data[1] -= other[1];
188  _data[2] -= other[2];
189  _data[3] -= other[3];
190  return *this;
191  }
192  friend GfVec4d operator-(GfVec4d const &l, GfVec4d const &r) {
193  return GfVec4d(l) -= r;
194  }
195 
196  /// Multiplication by scalar.
197  GfVec4d &operator*=(double s) {
198  _data[0] *= s;
199  _data[1] *= s;
200  _data[2] *= s;
201  _data[3] *= s;
202  return *this;
203  }
204  GfVec4d operator*(double s) const {
205  return GfVec4d(*this) *= s;
206  }
207  friend GfVec4d operator*(double s, GfVec4d const &v) {
208  return v * s;
209  }
210 
211  /// Division by scalar.
212  // TODO should divide by the scalar type.
213  GfVec4d &operator/=(double s) {
214  // TODO This should not multiply by 1/s, it should do the division.
215  // Doing the division is more numerically stable when s is close to
216  // zero.
217  return *this *= (1.0 / s);
218  }
219  GfVec4d operator/(double s) const {
220  return *this * (1.0 / s);
221  }
222 
223  /// See GfDot().
224  double operator*(GfVec4d const &v) const {
225  return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
226  }
227 
228  /// Returns the projection of \p this onto \p v. That is:
229  /// \code
230  /// v * (*this * v)
231  /// \endcode
232  GfVec4d GetProjection(GfVec4d const &v) const {
233  return v * (*this * v);
234  }
235 
236  /// Returns the orthogonal complement of \p this->GetProjection(b).
237  /// That is:
238  /// \code
239  /// *this - this->GetProjection(b)
240  /// \endcode
241  GfVec4d GetComplement(GfVec4d const &b) const {
242  return *this - this->GetProjection(b);
243  }
244 
245  /// Squared length.
246  double GetLengthSq() const {
247  return *this * *this;
248  }
249 
250  /// Length
251  double GetLength() const {
252  return GfSqrt(GetLengthSq());
253  }
254 
255  /// Normalizes the vector in place to unit length, returning the
256  /// length before normalization. If the length of the vector is
257  /// smaller than \p eps, then the vector is set to vector/\c eps.
258  /// The original length of the vector is returned. See also GfNormalize().
259  ///
260  /// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
261  /// By tickling the code, it no longer tries to write into
262  /// an illegal memory address (in the code section of memory).
263  double Normalize(double eps = GF_MIN_VECTOR_LENGTH) {
264  // TODO this seems suspect... suggest dividing by length so long as
265  // length is not zero.
266  double length = GetLength();
267  *this /= (length > eps) ? length : eps;
268  return length;
269  }
270 
272  GfVec4d normalized(*this);
273  normalized.Normalize(eps);
274  return normalized;
275  }
276 
277 
278 private:
279  double _data[4];
280 };
281 
282 /// Output a GfVec4d.
283 /// \ingroup group_gf_DebuggingOutput
284 GF_API std::ostream& operator<<(std::ostream &, GfVec4d const &);
285 
286 
288 
289 #include "pxr/base/gf/vec4f.h"
290 #include "pxr/base/gf/vec4h.h"
291 #include "pxr/base/gf/vec4i.h"
292 
294 
295 inline
296 GfVec4d::GfVec4d(class GfVec4f const &other)
297 {
298  _data[0] = other[0];
299  _data[1] = other[1];
300  _data[2] = other[2];
301  _data[3] = other[3];
302 }
303 inline
304 GfVec4d::GfVec4d(class GfVec4h const &other)
305 {
306  _data[0] = other[0];
307  _data[1] = other[1];
308  _data[2] = other[2];
309  _data[3] = other[3];
310 }
311 inline
312 GfVec4d::GfVec4d(class GfVec4i const &other)
313 {
314  _data[0] = other[0];
315  _data[1] = other[1];
316  _data[2] = other[2];
317  _data[3] = other[3];
318 }
319 
320 /// Returns component-wise multiplication of vectors \p v1 and \p v2.
321 inline GfVec4d
322 GfCompMult(GfVec4d const &v1, GfVec4d const &v2) {
323  return GfVec4d(
324  v1[0] * v2[0],
325  v1[1] * v2[1],
326  v1[2] * v2[2],
327  v1[3] * v2[3]
328  );
329 }
330 
331 /// Returns component-wise quotient of vectors \p v1 and \p v2.
332 inline GfVec4d
333 GfCompDiv(GfVec4d const &v1, GfVec4d const &v2) {
334  return GfVec4d(
335  v1[0] / v2[0],
336  v1[1] / v2[1],
337  v1[2] / v2[2],
338  v1[3] / v2[3]
339  );
340 }
341 
342 /// Returns the dot (inner) product of two vectors.
343 inline double
344 GfDot(GfVec4d const &v1, GfVec4d const &v2) {
345  return v1 * v2;
346 }
347 
348 
349 /// Returns the geometric length of \c v.
350 inline double
352 {
353  return v.GetLength();
354 }
355 
356 /// Normalizes \c *v in place to unit length, returning the length before
357 /// normalization. If the length of \c *v is smaller than \p eps then \c *v is
358 /// set to \c *v/eps. The original length of \c *v is returned.
359 inline double
361 {
362  return v->Normalize(eps);
363 }
364 
365 /// Returns a normalized (unit-length) vector with the same direction as \p v.
366 /// If the length of this vector is smaller than \p eps, the vector divided by
367 /// \p eps is returned.
368 inline GfVec4d
370 {
371  return v.GetNormalized(eps);
372 }
373 
374 /// Returns the projection of \p a onto \p b. That is:
375 /// \code
376 /// b * (a * b)
377 /// \endcode
378 inline GfVec4d
380 {
381  return a.GetProjection(b);
382 }
383 
384 /// Returns the orthogonal complement of \p a.GetProjection(b). That is:
385 /// \code
386 /// a - a.GetProjection(b)
387 /// \endcode
388 inline GfVec4d
390 {
391  return a.GetComplement(b);
392 }
393 
394 /// Tests for equality within a given tolerance, returning \c true if the
395 /// length of the difference vector is less than or equal to \p tolerance.
396 inline bool
397 GfIsClose(GfVec4d const &v1, GfVec4d const &v2, double tolerance)
398 {
399  GfVec4d delta = v1 - v2;
400  return delta.GetLengthSq() <= tolerance * tolerance;
401 }
402 
403 
404 
406 
407 #endif // PXR_BASE_GF_VEC4D_H
Definition: vec4i.h:43
constexpr GfVec4d(double value)
Initialize all elements to a single value.
Definition: vec4d.h:56
double GfSqrt(double f)
Definition: math.h:187
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
const GLdouble * v
Definition: glcorearb.h:837
double const & operator[](size_t i) const
Indexing.
Definition: vec4d.h:137
double operator*(GfVec4d const &v) const
See GfDot().
Definition: vec4d.h:224
GLsizei const GLfloat * value
Definition: glcorearb.h:824
static GfVec4d WAxis()
Create a unit vector along the W-axis.
Definition: vec4d.h:102
GfVec4d()=default
Default constructor does no initialization.
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
Definition: vec4d.h:45
GfVec4d operator*(double s) const
Definition: vec4d.h:204
GfVec4d operator/(double s) const
Definition: vec4d.h:219
**But if you need a result
Definition: thread.h:622
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
GfVec4d GfGetComplement(GfVec4d const &a, GfVec4d const &b)
Definition: vec4d.h:389
friend GfVec4d operator-(GfVec4d const &l, GfVec4d const &r)
Definition: vec4d.h:192
GfVec4d GfCompDiv(GfVec4d const &v1, GfVec4d const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec4d.h:333
static GfVec4d ZAxis()
Create a unit vector along the Z-axis.
Definition: vec4d.h:96
constexpr GfVec4d(double s0, double s1, double s2, double s3)
Initialize all elements with explicit arguments.
Definition: vec4d.h:62
Definition: vec4h.h:46
double & operator[](size_t i)
Definition: vec4d.h:138
GfVec4d GfGetProjection(GfVec4d const &a, GfVec4d const &b)
Definition: vec4d.h:379
double GetLengthSq() const
Squared length.
Definition: vec4d.h:246
double const * data() const
Direct data access.
Definition: vec4d.h:132
bool operator==(GfVec4d const &other) const
Equality comparison.
Definition: vec4d.h:146
double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec4d.h:263
static GfVec4d Axis(size_t i)
Definition: vec4d.h:110
double * data()
Definition: vec4d.h:133
GfVec4d GfGetNormalized(GfVec4d const &v, double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec4d.h:369
double GfNormalize(GfVec4d *v, double eps=GF_MIN_VECTOR_LENGTH)
Definition: vec4d.h:360
GfVec4d GetComplement(GfVec4d const &b) const
Definition: vec4d.h:241
GfVec4d operator-() const
Create a vec with negated elements.
Definition: vec4d.h:168
GLint GLenum GLboolean normalized
Definition: glcorearb.h:872
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
double GfGetLength(GfVec4d const &v)
Returns the geometric length of v.
Definition: vec4d.h:351
bool operator!=(GfVec4d const &other) const
Definition: vec4d.h:152
GfVec4d & operator+=(GfVec4d const &other)
Addition.
Definition: vec4d.h:173
Definition: vec4f.h:45
double const * GetArray() const
Definition: vec4d.h:134
static GfVec4d XAxis()
Create a unit vector along the X-axis.
Definition: vec4d.h:84
GfVec4d GfCompMult(GfVec4d const &v1, GfVec4d const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec4d.h:322
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
friend GfVec4d operator*(double s, GfVec4d const &v)
Definition: vec4d.h:207
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
double GfDot(GfVec4d const &v1, GfVec4d const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec4d.h:344
double ScalarType
Scalar element type and dimension.
Definition: vec4d.h:49
static const size_t dimension
Definition: vec4d.h:50
GfVec4d GetNormalized(double eps=GF_MIN_VECTOR_LENGTH) const
Definition: vec4d.h:271
double GetLength() const
Length.
Definition: vec4d.h:251
friend GfVec4d operator+(GfVec4d const &l, GfVec4d const &r)
Definition: vec4d.h:180
GLfloat GLfloat v1
Definition: glcorearb.h:817
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GfVec4d & operator-=(GfVec4d const &other)
Subtraction.
Definition: vec4d.h:185
GfVec4d & Set(double const *a)
Set all elements with a pointer to data.
Definition: vec4d.h:127
GfVec4d & Set(double s0, double s1, double s2, double s3)
Set all elements with passed arguments.
Definition: vec4d.h:118
GLboolean r
Definition: glcorearb.h:1222
GF_API std::ostream & operator<<(std::ostream &, GfVec4d const &)
GfVec4d & operator*=(double s)
Multiplication by scalar.
Definition: vec4d.h:197
constexpr GfVec4d(Scl const *p)
Construct with pointer to values.
Definition: vec4d.h:69
GfVec4d GetProjection(GfVec4d const &v) const
Definition: vec4d.h:232
static GfVec4d YAxis()
Create a unit vector along the Y-axis.
Definition: vec4d.h:90
bool GfIsClose(GfVec4d const &v1, GfVec4d const &v2, double tolerance)
Definition: vec4d.h:397
#define GF_MIN_VECTOR_LENGTH
Definition: limits.h:17
#define GF_API
Definition: api.h:23
friend size_t hash_value(GfVec4d const &vec)
Hash.
Definition: vec4d.h:141
GfVec4d & operator/=(double s)
Division by scalar.
Definition: vec4d.h:213