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