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 #include "pxr/base/tf/hash.h"
42 
43 #include <cstddef>
44 #include <cmath>
45 
46 #include <iosfwd>
47 
48 PXR_NAMESPACE_OPEN_SCOPE
49 
50 class GfVec4h;
51 
52 template <>
53 struct GfIsGfVec<class GfVec4h> { static const bool value = true; };
54 
63 class GfVec4h
64 {
65 public:
67  typedef GfHalf ScalarType;
68  static const size_t dimension = 4;
69 
71  GfVec4h() = default;
72 
74  constexpr explicit GfVec4h(GfHalf value)
75  : _data{ value, value, value, value }
76  {
77  }
78 
80  constexpr GfVec4h(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
81  : _data{ s0, s1, s2, s3 }
82  {
83  }
84 
86  template <class Scl>
87  constexpr explicit GfVec4h(Scl const *p)
88  : _data{ p[0], p[1], p[2], p[3] }
89  {
90  }
91 
93  explicit GfVec4h(class GfVec4d const &other);
94 
96  explicit GfVec4h(class GfVec4f const &other);
97 
99  GfVec4h(class GfVec4i const &other);
100 
102  static GfVec4h XAxis() {
103  GfVec4h result(0);
104  result[0] = 1;
105  return result;
106  }
108  static GfVec4h YAxis() {
109  GfVec4h result(0);
110  result[1] = 1;
111  return result;
112  }
114  static GfVec4h ZAxis() {
115  GfVec4h result(0);
116  result[2] = 1;
117  return result;
118  }
120  static GfVec4h WAxis() {
121  GfVec4h result(0);
122  result[3] = 1;
123  return result;
124  }
125 
128  static GfVec4h Axis(size_t i) {
129  GfVec4h result(0);
130  if (i < 4)
131  result[i] = 1;
132  return result;
133  }
134 
136  GfVec4h &Set(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3) {
137  _data[0] = s0;
138  _data[1] = s1;
139  _data[2] = s2;
140  _data[3] = s3;
141  return *this;
142  }
143 
145  GfVec4h &Set(GfHalf const *a) {
146  return Set(a[0], a[1], a[2], a[3]);
147  }
148 
150  GfHalf const *data() const { return _data; }
151  GfHalf *data() { return _data; }
152  GfHalf const *GetArray() const { return data(); }
153 
155  GfHalf const &operator[](size_t i) const { return _data[i]; }
156  GfHalf &operator[](size_t i) { return _data[i]; }
157 
159  friend inline size_t hash_value(GfVec4h const &vec) {
160  return TfHash::Combine(vec[0], vec[1], vec[2], vec[3]);
161  }
162 
164  bool operator==(GfVec4h const &other) const {
165  return _data[0] == other[0] &&
166  _data[1] == other[1] &&
167  _data[2] == other[2] &&
168  _data[3] == other[3];
169  }
170  bool operator!=(GfVec4h const &other) const {
171  return !(*this == other);
172  }
173 
174  // TODO Add inequality for other vec types...
176  GF_API
177  bool operator==(class GfVec4d const &other) const;
179  GF_API
180  bool operator==(class GfVec4f const &other) const;
182  GF_API
183  bool operator==(class GfVec4i const &other) const;
184 
186  GfVec4h operator-() const {
187  return GfVec4h(-_data[0], -_data[1], -_data[2], -_data[3]);
188  }
189 
191  GfVec4h &operator+=(GfVec4h const &other) {
192  _data[0] += other[0];
193  _data[1] += other[1];
194  _data[2] += other[2];
195  _data[3] += other[3];
196  return *this;
197  }
198  friend GfVec4h operator+(GfVec4h const &l, GfVec4h const &r) {
199  return GfVec4h(l) += r;
200  }
201 
203  GfVec4h &operator-=(GfVec4h const &other) {
204  _data[0] -= other[0];
205  _data[1] -= other[1];
206  _data[2] -= other[2];
207  _data[3] -= other[3];
208  return *this;
209  }
210  friend GfVec4h operator-(GfVec4h const &l, GfVec4h const &r) {
211  return GfVec4h(l) -= r;
212  }
213 
215  GfVec4h &operator*=(double s) {
216  _data[0] *= s;
217  _data[1] *= s;
218  _data[2] *= s;
219  _data[3] *= s;
220  return *this;
221  }
222  GfVec4h operator*(double s) const {
223  return GfVec4h(*this) *= s;
224  }
225  friend GfVec4h operator*(double s, GfVec4h const &v) {
226  return v * s;
227  }
228 
230  // TODO should divide by the scalar type.
231  GfVec4h &operator/=(double s) {
232  // TODO This should not multiply by 1/s, it should do the division.
233  // Doing the division is more numerically stable when s is close to
234  // zero.
235  return *this *= (1.0 / s);
236  }
237  GfVec4h operator/(double s) const {
238  return *this * (1.0 / s);
239  }
240 
242  GfHalf operator*(GfVec4h const &v) const {
243  return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
244  }
245 
250  GfVec4h GetProjection(GfVec4h const &v) const {
251  return v * (*this * v);
252  }
253 
259  GfVec4h GetComplement(GfVec4h const &b) const {
260  return *this - this->GetProjection(b);
261  }
262 
264  GfHalf GetLengthSq() const {
265  return *this * *this;
266  }
267 
269  GfHalf GetLength() const {
270  return GfSqrt(GetLengthSq());
271  }
272 
281  GfHalf Normalize(GfHalf eps = 0.001) {
282  // TODO this seems suspect... suggest dividing by length so long as
283  // length is not zero.
284  GfHalf length = GetLength();
285  *this /= (length > eps) ? length : eps;
286  return length;
287  }
288 
289  GfVec4h GetNormalized(GfHalf eps = 0.001) const {
290  GfVec4h normalized(*this);
291  normalized.Normalize(eps);
292  return normalized;
293  }
294 
295 
296 private:
297  GfHalf _data[4];
298 };
299 
302 GF_API std::ostream& operator<<(std::ostream &, GfVec4h const &);
303 
304 
305 PXR_NAMESPACE_CLOSE_SCOPE
306 
307 #include "pxr/base/gf/vec4d.h"
308 #include "pxr/base/gf/vec4f.h"
309 #include "pxr/base/gf/vec4i.h"
310 
311 PXR_NAMESPACE_OPEN_SCOPE
312 
313 inline
314 GfVec4h::GfVec4h(class GfVec4d const &other)
315 {
316  _data[0] = other[0];
317  _data[1] = other[1];
318  _data[2] = other[2];
319  _data[3] = other[3];
320 }
321 inline
322 GfVec4h::GfVec4h(class GfVec4f const &other)
323 {
324  _data[0] = other[0];
325  _data[1] = other[1];
326  _data[2] = other[2];
327  _data[3] = other[3];
328 }
329 inline
330 GfVec4h::GfVec4h(class GfVec4i const &other)
331 {
332  _data[0] = other[0];
333  _data[1] = other[1];
334  _data[2] = other[2];
335  _data[3] = other[3];
336 }
337 
339 inline GfVec4h
340 GfCompMult(GfVec4h const &v1, GfVec4h const &v2) {
341  return GfVec4h(
342  v1[0] * v2[0],
343  v1[1] * v2[1],
344  v1[2] * v2[2],
345  v1[3] * v2[3]
346  );
347 }
348 
350 inline GfVec4h
351 GfCompDiv(GfVec4h const &v1, GfVec4h const &v2) {
352  return GfVec4h(
353  v1[0] / v2[0],
354  v1[1] / v2[1],
355  v1[2] / v2[2],
356  v1[3] / v2[3]
357  );
358 }
359 
361 inline GfHalf
362 GfDot(GfVec4h const &v1, GfVec4h const &v2) {
363  return v1 * v2;
364 }
365 
366 
368 inline GfHalf
370 {
371  return v.GetLength();
372 }
373 
377 inline GfHalf
378 GfNormalize(GfVec4h *v, GfHalf eps = 0.001)
379 {
380  return v->Normalize(eps);
381 }
382 
386 inline GfVec4h
387 GfGetNormalized(GfVec4h const &v, GfHalf eps = 0.001)
388 {
389  return v.GetNormalized(eps);
390 }
391 
396 inline GfVec4h
397 GfGetProjection(GfVec4h const &a, GfVec4h const &b)
398 {
399  return a.GetProjection(b);
400 }
401 
406 inline GfVec4h
407 GfGetComplement(GfVec4h const &a, GfVec4h const &b)
408 {
409  return a.GetComplement(b);
410 }
411 
414 inline bool
415 GfIsClose(GfVec4h const &v1, GfVec4h const &v2, double tolerance)
416 {
417  GfVec4h delta = v1 - v2;
418  return delta.GetLengthSq() <= tolerance * tolerance;
419 }
420 
421 
422 
423 PXR_NAMESPACE_CLOSE_SCOPE
424 
425 #endif // PXR_BASE_GF_VEC4H_H
Basic type for a vector of 4 int components.
Definition: vec4i.h:60
GfVec4h GfGetNormalized(GfVec4h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec4h.h:387
GfVec4h & operator-=(GfVec4h const &other)
Subtraction.
Definition: vec4h.h:203
GfVec4h & Set(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
Set all elements with passed arguments.
Definition: vec4h.h:136
pxr_half::half GfHalf
A 16-bit floating point data type.
Definition: half.h:41
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:397
Basic type for a vector of 4 double components.
Definition: vec4d.h:62
GfVec4h GfCompDiv(GfVec4h const &v1, GfVec4h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec4h.h:351
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:80
static GfVec4h YAxis()
Create a unit vector along the Y-axis.
Definition: vec4h.h:108
friend size_t hash_value(GfVec4h const &vec)
Hash.
Definition: vec4h.h:159
Basic type for a vector of 4 GfHalf components.
Definition: vec4h.h:63
GfHalf GfGetLength(GfVec4h const &v)
Returns the geometric length of v.
Definition: vec4h.h:369
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
GfHalf GetLength() const
Length.
Definition: vec4h.h:269
GfVec4h & operator+=(GfVec4h const &other)
Addition.
Definition: vec4h.h:191
GfVec4h & operator/=(double s)
Division by scalar.
Definition: vec4h.h:231
GfVec4h GfCompMult(GfVec4h const &v1, GfVec4h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec4h.h:340
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec4h.h:281
bool operator==(GfVec4h const &other) const
Equality comparison.
Definition: vec4h.h:164
GfHalf GfDot(GfVec4h const &v1, GfVec4h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec4h.h:362
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:114
GfVec4h GetComplement(GfVec4h const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec4h.h:259
static GfVec4h XAxis()
Create a unit vector along the X-axis.
Definition: vec4h.h:102
GfHalf GetLengthSq() const
Squared length.
Definition: vec4h.h:264
GfVec4h GetProjection(GfVec4h const &v) const
Returns the projection of this onto v.
Definition: vec4h.h:250
Basic type for a vector of 4 float components.
Definition: vec4f.h:62
GfVec4h GfGetComplement(GfVec4h const &a, GfVec4h const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec4h.h:407
static GfVec4h Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec4h.h:128
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec4h.h:155
GfVec4h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec4h.h:145
GfVec4h()=default
Default constructor does no initialization.
GfHalf const * data() const
Direct data access.
Definition: vec4h.h:150
GfHalf ScalarType
Scalar element type and dimension.
Definition: vec4h.h:67
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:74
Defines useful mathematical limits.
static GfVec4h WAxis()
Create a unit vector along the W-axis.
Definition: vec4h.h:120
GfHalf GfNormalize(GfVec4h *v, GfHalf eps=0.001)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec4h.h:378
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:415
GfVec4h operator-() const
Create a vec with negated elements.
Definition: vec4h.h:186
GfVec4h & operator *=(double s)
Multiplication by scalar.
Definition: vec4h.h:215
constexpr GfVec4h(Scl const *p)
Construct with pointer to values.
Definition: vec4h.h:87