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 //
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 
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 
47 PXR_NAMESPACE_OPEN_SCOPE
48 
49 class GfVec4d;
50 
51 template <>
52 struct GfIsGfVec<class GfVec4d> { static const bool value = true; };
53 
62 class GfVec4d
63 {
64 public:
66  typedef double ScalarType;
67  static const size_t dimension = 4;
68 
70  GfVec4d() = default;
71 
73  constexpr explicit GfVec4d(double value)
74  : _data{ value, value, value, value }
75  {
76  }
77 
79  constexpr GfVec4d(double s0, double s1, double s2, double s3)
80  : _data{ s0, s1, s2, s3 }
81  {
82  }
83 
85  template <class Scl>
86  constexpr explicit GfVec4d(Scl const *p)
87  : _data{ p[0], p[1], p[2], p[3] }
88  {
89  }
90 
92  GfVec4d(class GfVec4f const &other);
93 
95  GfVec4d(class GfVec4h const &other);
96 
98  GfVec4d(class GfVec4i const &other);
99 
101  static GfVec4d XAxis() {
102  GfVec4d result(0);
103  result[0] = 1;
104  return result;
105  }
107  static GfVec4d YAxis() {
108  GfVec4d result(0);
109  result[1] = 1;
110  return result;
111  }
113  static GfVec4d ZAxis() {
114  GfVec4d result(0);
115  result[2] = 1;
116  return result;
117  }
119  static GfVec4d WAxis() {
120  GfVec4d result(0);
121  result[3] = 1;
122  return result;
123  }
124 
127  static GfVec4d Axis(size_t i) {
128  GfVec4d result(0);
129  if (i < 4)
130  result[i] = 1;
131  return result;
132  }
133 
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 
144  GfVec4d &Set(double const *a) {
145  return Set(a[0], a[1], a[2], a[3]);
146  }
147 
149  double const *data() const { return _data; }
150  double *data() { return _data; }
151  double const *GetArray() const { return data(); }
152 
154  double const &operator[](size_t i) const { return _data[i]; }
155  double &operator[](size_t i) { return _data[i]; }
156 
158  friend inline size_t hash_value(GfVec4d const &vec) {
159  return TfHash::Combine(vec[0], vec[1], vec[2], vec[3]);
160  }
161 
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...
175  GF_API
176  bool operator==(class GfVec4f const &other) const;
178  GF_API
179  bool operator==(class GfVec4h const &other) const;
181  GF_API
182  bool operator==(class GfVec4i const &other) const;
183 
185  GfVec4d operator-() const {
186  return GfVec4d(-_data[0], -_data[1], -_data[2], -_data[3]);
187  }
188 
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 
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 
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 
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 
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 
249  GfVec4d GetProjection(GfVec4d const &v) const {
250  return v * (*this * v);
251  }
252 
258  GfVec4d GetComplement(GfVec4d const &b) const {
259  return *this - this->GetProjection(b);
260  }
261 
263  double GetLengthSq() const {
264  return *this * *this;
265  }
266 
268  double GetLength() const {
269  return GfSqrt(GetLengthSq());
270  }
271 
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 
288  GfVec4d GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const {
289  GfVec4d normalized(*this);
290  normalized.Normalize(eps);
291  return normalized;
292  }
293 
294 
295 private:
296  double _data[4];
297 };
298 
301 GF_API std::ostream& operator<<(std::ostream &, GfVec4d const &);
302 
303 
304 PXR_NAMESPACE_CLOSE_SCOPE
305 
306 #include "pxr/base/gf/vec4f.h"
307 #include "pxr/base/gf/vec4h.h"
308 #include "pxr/base/gf/vec4i.h"
309 
310 PXR_NAMESPACE_OPEN_SCOPE
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 
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 
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 
360 inline double
361 GfDot(GfVec4d const &v1, GfVec4d const &v2) {
362  return v1 * v2;
363 }
364 
365 
367 inline double
369 {
370  return v.GetLength();
371 }
372 
376 inline double
378 {
379  return v->Normalize(eps);
380 }
381 
385 inline GfVec4d
387 {
388  return v.GetNormalized(eps);
389 }
390 
395 inline GfVec4d
396 GfGetProjection(GfVec4d const &a, GfVec4d const &b)
397 {
398  return a.GetProjection(b);
399 }
400 
405 inline GfVec4d
406 GfGetComplement(GfVec4d const &a, GfVec4d const &b)
407 {
408  return a.GetComplement(b);
409 }
410 
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 
422 PXR_NAMESPACE_CLOSE_SCOPE
423 
424 #endif // PXR_BASE_GF_VEC4D_H
Basic type for a vector of 4 int components.
Definition: vec4i.h:60
double ScalarType
Scalar element type and dimension.
Definition: vec4d.h:66
static GfVec4d XAxis()
Create a unit vector along the X-axis.
Definition: vec4d.h:101
constexpr GfVec4d(double value)
Initialize all elements to a single value.
Definition: vec4d.h:73
GfVec4d & operator *=(double s)
Multiplication by scalar.
Definition: vec4d.h:214
GfVec4d & operator/=(double s)
Division by scalar.
Definition: vec4d.h:230
static GfVec4d YAxis()
Create a unit vector along the Y-axis.
Definition: vec4d.h:107
Assorted mathematical utility functions.
double const * data() const
Direct data access.
Definition: vec4d.h:149
Basic type for a vector of 4 double components.
Definition: vec4d.h:62
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
bool operator==(GfVec4d const &other) const
Equality comparison.
Definition: vec4d.h:163
GfVec4d GfGetComplement(GfVec4d const &a, GfVec4d const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec4d.h:406
GfVec4d GfCompDiv(GfVec4d const &v1, GfVec4d const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec4d.h:350
Basic type for a vector of 4 GfHalf components.
Definition: vec4h.h:63
GfVec4d GfGetProjection(GfVec4d const &a, GfVec4d const &b)
Returns the projection of a onto b.
Definition: vec4d.h:396
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
GfVec4d GetComplement(GfVec4d const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec4d.h:258
GfVec4d & operator-=(GfVec4d const &other)
Subtraction.
Definition: vec4d.h:202
constexpr GfVec4d(double s0, double s1, double s2, double s3)
Initialize all elements with explicit arguments.
Definition: vec4d.h:79
double GetLengthSq() const
Squared length.
Definition: vec4d.h:263
double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec4d.h:280
double const & operator[](size_t i) const
Indexing.
Definition: vec4d.h:154
GfVec4d & Set(double s0, double s1, double s2, double s3)
Set all elements with passed arguments.
Definition: vec4d.h:135
GfVec4d GfGetNormalized(GfVec4d const &v, double eps=GF_MIN_VECTOR_LENGTH)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec4d.h:386
double GfNormalize(GfVec4d *v, double eps=GF_MIN_VECTOR_LENGTH)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec4d.h:377
static GfVec4d ZAxis()
Create a unit vector along the Z-axis.
Definition: vec4d.h:113
GfVec4d GetProjection(GfVec4d const &v) const
Returns the projection of this onto v.
Definition: vec4d.h:249
GF_API std::ostream & operator<<(std::ostream &, GfVec4d const &)
Output a GfVec4d.
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:80
double GfGetLength(GfVec4d const &v)
Returns the geometric length of v.
Definition: vec4d.h:368
static GfVec4d Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec4d.h:127
double GetLength() const
Length.
Definition: vec4d.h:268
Basic type for a vector of 4 float components.
Definition: vec4f.h:62
static GfVec4d WAxis()
Create a unit vector along the W-axis.
Definition: vec4d.h:119
GfVec4d GfCompMult(GfVec4d const &v1, GfVec4d const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec4d.h:339
double GfDot(GfVec4d const &v1, GfVec4d const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec4d.h:361
Defines useful mathematical limits.
constexpr GfVec4d(Scl const *p)
Construct with pointer to values.
Definition: vec4d.h:86
GfVec4d & Set(double const *a)
Set all elements with a pointer to data.
Definition: vec4d.h:144
GfVec4d()=default
Default constructor does no initialization.
GfVec4d & operator+=(GfVec4d const &other)
Addition.
Definition: vec4d.h:190
bool GfIsClose(GfVec4d const &v1, GfVec4d const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition: vec4d.h:414
friend size_t hash_value(GfVec4d const &vec)
Hash.
Definition: vec4d.h:158
GfVec4d operator-() const
Create a vec with negated elements.
Definition: vec4d.h:185
#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