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