vec4h.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_VEC4H_H
29 #define PXR_BASE_GF_VEC4H_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/gf/half.h"
41 
42 #include <boost/functional/hash.hpp>
43 
44 #include <cstddef>
45 #include <cmath>
46 
47 #include <iosfwd>
48 
49 PXR_NAMESPACE_OPEN_SCOPE
50 
51 class GfVec4h;
52 
53 template <>
54 struct GfIsGfVec<class GfVec4h> { static const bool value = true; };
55 
64 class GfVec4h
65 {
66 public:
68  typedef GfHalf ScalarType;
69  static const size_t dimension = 4;
70 
72  GfVec4h() = default;
73 
75  constexpr explicit GfVec4h(GfHalf value)
76  : _data{ value, value, value, value }
77  {
78  }
79 
81  constexpr GfVec4h(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
82  : _data{ s0, s1, s2, s3 }
83  {
84  }
85 
87  template <class Scl>
88  constexpr explicit GfVec4h(Scl const *p)
89  : _data{ p[0], p[1], p[2], p[3] }
90  {
91  }
92 
94  explicit GfVec4h(class GfVec4d const &other);
95 
97  explicit GfVec4h(class GfVec4f const &other);
98 
100  GfVec4h(class GfVec4i const &other);
101 
103  static GfVec4h XAxis() {
104  GfVec4h result(0);
105  result[0] = 1;
106  return result;
107  }
109  static GfVec4h YAxis() {
110  GfVec4h result(0);
111  result[1] = 1;
112  return result;
113  }
115  static GfVec4h ZAxis() {
116  GfVec4h result(0);
117  result[2] = 1;
118  return result;
119  }
121  static GfVec4h WAxis() {
122  GfVec4h result(0);
123  result[3] = 1;
124  return result;
125  }
126 
129  static GfVec4h Axis(size_t i) {
130  GfVec4h result(0);
131  if (i < 4)
132  result[i] = 1;
133  return result;
134  }
135 
137  GfVec4h &Set(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3) {
138  _data[0] = s0;
139  _data[1] = s1;
140  _data[2] = s2;
141  _data[3] = s3;
142  return *this;
143  }
144 
146  GfVec4h &Set(GfHalf const *a) {
147  return Set(a[0], a[1], a[2], a[3]);
148  }
149 
151  GfHalf const *data() const { return _data; }
152  GfHalf *data() { return _data; }
153  GfHalf const *GetArray() const { return data(); }
154 
156  GfHalf const &operator[](size_t i) const { return _data[i]; }
157  GfHalf &operator[](size_t i) { return _data[i]; }
158 
160  friend inline size_t hash_value(GfVec4h const &vec) {
161  size_t h = 0;
162  boost::hash_combine(h, vec[0]);
163  boost::hash_combine(h, vec[1]);
164  boost::hash_combine(h, vec[2]);
165  boost::hash_combine(h, vec[3]);
166  return h;
167  }
168 
170  bool operator==(GfVec4h const &other) const {
171  return _data[0] == other[0] &&
172  _data[1] == other[1] &&
173  _data[2] == other[2] &&
174  _data[3] == other[3];
175  }
176  bool operator!=(GfVec4h const &other) const {
177  return !(*this == other);
178  }
179 
180  // TODO Add inequality for other vec types...
182  GF_API
183  bool operator==(class GfVec4d const &other) const;
185  GF_API
186  bool operator==(class GfVec4f const &other) const;
188  GF_API
189  bool operator==(class GfVec4i const &other) const;
190 
192  GfVec4h operator-() const {
193  return GfVec4h(-_data[0], -_data[1], -_data[2], -_data[3]);
194  }
195 
197  GfVec4h &operator+=(GfVec4h const &other) {
198  _data[0] += other[0];
199  _data[1] += other[1];
200  _data[2] += other[2];
201  _data[3] += other[3];
202  return *this;
203  }
204  friend GfVec4h operator+(GfVec4h const &l, GfVec4h const &r) {
205  return GfVec4h(l) += r;
206  }
207 
209  GfVec4h &operator-=(GfVec4h const &other) {
210  _data[0] -= other[0];
211  _data[1] -= other[1];
212  _data[2] -= other[2];
213  _data[3] -= other[3];
214  return *this;
215  }
216  friend GfVec4h operator-(GfVec4h const &l, GfVec4h const &r) {
217  return GfVec4h(l) -= r;
218  }
219 
221  GfVec4h &operator*=(double s) {
222  _data[0] *= s;
223  _data[1] *= s;
224  _data[2] *= s;
225  _data[3] *= s;
226  return *this;
227  }
228  GfVec4h operator*(double s) const {
229  return GfVec4h(*this) *= s;
230  }
231  friend GfVec4h operator*(double s, GfVec4h const &v) {
232  return v * s;
233  }
234 
236  // TODO should divide by the scalar type.
237  GfVec4h &operator/=(double s) {
238  // TODO This should not multiply by 1/s, it should do the division.
239  // Doing the division is more numerically stable when s is close to
240  // zero.
241  return *this *= (1.0 / s);
242  }
243  GfVec4h operator/(double s) const {
244  return *this * (1.0 / s);
245  }
246 
248  GfHalf operator*(GfVec4h const &v) const {
249  return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
250  }
251 
256  GfVec4h GetProjection(GfVec4h const &v) const {
257  return v * (*this * v);
258  }
259 
265  GfVec4h GetComplement(GfVec4h const &b) const {
266  return *this - this->GetProjection(b);
267  }
268 
270  GfHalf GetLengthSq() const {
271  return *this * *this;
272  }
273 
275  GfHalf GetLength() const {
276  return GfSqrt(GetLengthSq());
277  }
278 
287  GfHalf Normalize(GfHalf eps = 0.001) {
288  // TODO this seems suspect... suggest dividing by length so long as
289  // length is not zero.
290  GfHalf length = GetLength();
291  *this /= (length > eps) ? length : eps;
292  return length;
293  }
294 
295  GfVec4h GetNormalized(GfHalf eps = 0.001) const {
296  GfVec4h normalized(*this);
297  normalized.Normalize(eps);
298  return normalized;
299  }
300 
301 
302 private:
303  GfHalf _data[4];
304 };
305 
308 GF_API std::ostream& operator<<(std::ostream &, GfVec4h const &);
309 
310 
311 PXR_NAMESPACE_CLOSE_SCOPE
312 
313 #include "pxr/base/gf/vec4d.h"
314 #include "pxr/base/gf/vec4f.h"
315 #include "pxr/base/gf/vec4i.h"
316 
317 PXR_NAMESPACE_OPEN_SCOPE
318 
319 inline
320 GfVec4h::GfVec4h(class GfVec4d const &other)
321 {
322  _data[0] = other[0];
323  _data[1] = other[1];
324  _data[2] = other[2];
325  _data[3] = other[3];
326 }
327 inline
328 GfVec4h::GfVec4h(class GfVec4f const &other)
329 {
330  _data[0] = other[0];
331  _data[1] = other[1];
332  _data[2] = other[2];
333  _data[3] = other[3];
334 }
335 inline
336 GfVec4h::GfVec4h(class GfVec4i const &other)
337 {
338  _data[0] = other[0];
339  _data[1] = other[1];
340  _data[2] = other[2];
341  _data[3] = other[3];
342 }
343 
345 inline GfVec4h
346 GfCompMult(GfVec4h const &v1, GfVec4h const &v2) {
347  return GfVec4h(
348  v1[0] * v2[0],
349  v1[1] * v2[1],
350  v1[2] * v2[2],
351  v1[3] * v2[3]
352  );
353 }
354 
356 inline GfVec4h
357 GfCompDiv(GfVec4h const &v1, GfVec4h const &v2) {
358  return GfVec4h(
359  v1[0] / v2[0],
360  v1[1] / v2[1],
361  v1[2] / v2[2],
362  v1[3] / v2[3]
363  );
364 }
365 
367 inline GfHalf
368 GfDot(GfVec4h const &v1, GfVec4h const &v2) {
369  return v1 * v2;
370 }
371 
372 
374 inline GfHalf
376 {
377  return v.GetLength();
378 }
379 
383 inline GfHalf
384 GfNormalize(GfVec4h *v, GfHalf eps = 0.001)
385 {
386  return v->Normalize(eps);
387 }
388 
392 inline GfVec4h
393 GfGetNormalized(GfVec4h const &v, GfHalf eps = 0.001)
394 {
395  return v.GetNormalized(eps);
396 }
397 
402 inline GfVec4h
403 GfGetProjection(GfVec4h const &a, GfVec4h const &b)
404 {
405  return a.GetProjection(b);
406 }
407 
412 inline GfVec4h
413 GfGetComplement(GfVec4h const &a, GfVec4h const &b)
414 {
415  return a.GetComplement(b);
416 }
417 
420 inline bool
421 GfIsClose(GfVec4h const &v1, GfVec4h const &v2, double tolerance)
422 {
423  GfVec4h delta = v1 - v2;
424  return delta.GetLengthSq() <= tolerance * tolerance;
425 }
426 
427 
428 
429 PXR_NAMESPACE_CLOSE_SCOPE
430 
431 #endif // PXR_BASE_GF_VEC4H_H
Basic type for a vector of 4 int components.
Definition: vec4i.h:61
GfVec4h GfGetNormalized(GfVec4h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec4h.h:393
GfVec4h & operator-=(GfVec4h const &other)
Subtraction.
Definition: vec4h.h:209
GfVec4h & Set(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
Set all elements with passed arguments.
Definition: vec4h.h:137
pxr_half::half GfHalf
A 16-bit floating point data type.
Definition: half.h:43
Assorted mathematical utility functions.
This header serves to simply bring in the half float datatype and provide a hash_value function.
GfVec4h GfGetProjection(GfVec4h const &a, GfVec4h const &b)
Returns the projection of a onto b.
Definition: vec4h.h:403
Basic type for a vector of 4 double components.
Definition: vec4d.h:63
GfVec4h GfCompDiv(GfVec4h const &v1, GfVec4h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec4h.h:357
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
constexpr GfVec4h(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
Initialize all elements with explicit arguments.
Definition: vec4h.h:81
static GfVec4h YAxis()
Create a unit vector along the Y-axis.
Definition: vec4h.h:109
friend size_t hash_value(GfVec4h const &vec)
Hash.
Definition: vec4h.h:160
Basic type for a vector of 4 GfHalf components.
Definition: vec4h.h:64
GfHalf GfGetLength(GfVec4h const &v)
Returns the geometric length of v.
Definition: vec4h.h:375
GfHalf GetLength() const
Length.
Definition: vec4h.h:275
GfVec4h & operator+=(GfVec4h const &other)
Addition.
Definition: vec4h.h:197
GfVec4h & operator/=(double s)
Division by scalar.
Definition: vec4h.h:237
GfVec4h GfCompMult(GfVec4h const &v1, GfVec4h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec4h.h:346
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec4h.h:287
bool operator==(GfVec4h const &other) const
Equality comparison.
Definition: vec4h.h:170
GfHalf GfDot(GfVec4h const &v1, GfVec4h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec4h.h:368
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:80
static GfVec4h ZAxis()
Create a unit vector along the Z-axis.
Definition: vec4h.h:115
GfVec4h GetComplement(GfVec4h const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec4h.h:265
static GfVec4h XAxis()
Create a unit vector along the X-axis.
Definition: vec4h.h:103
GfHalf GetLengthSq() const
Squared length.
Definition: vec4h.h:270
GfVec4h GetProjection(GfVec4h const &v) const
Returns the projection of this onto v.
Definition: vec4h.h:256
Basic type for a vector of 4 float components.
Definition: vec4f.h:63
GfVec4h GfGetComplement(GfVec4h const &a, GfVec4h const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec4h.h:413
static GfVec4h Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec4h.h:129
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec4h.h:156
GfVec4h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec4h.h:146
GfVec4h()=default
Default constructor does no initialization.
GfHalf const * data() const
Direct data access.
Definition: vec4h.h:151
GfHalf ScalarType
Scalar element type and dimension.
Definition: vec4h.h:68
GF_API std::ostream & operator<<(std::ostream &, GfVec4h const &)
Output a GfVec4h.
constexpr GfVec4h(GfHalf value)
Initialize all elements to a single value.
Definition: vec4h.h:75
Defines useful mathematical limits.
static GfVec4h WAxis()
Create a unit vector along the W-axis.
Definition: vec4h.h:121
GfHalf GfNormalize(GfVec4h *v, GfHalf eps=0.001)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec4h.h:384
bool GfIsClose(GfVec4h const &v1, GfVec4h const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition: vec4h.h:421
GfVec4h operator-() const
Create a vec with negated elements.
Definition: vec4h.h:192
GfVec4h & operator *=(double s)
Multiplication by scalar.
Definition: vec4h.h:221
constexpr GfVec4h(Scl const *p)
Construct with pointer to values.
Definition: vec4h.h:88