vec2h.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_VEC2H_H
29 #define PXR_BASE_GF_VEC2H_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 GfVec2h;
52 
53 template <>
54 struct GfIsGfVec<class GfVec2h> { static const bool value = true; };
55 
64 class GfVec2h
65 {
66 public:
68  typedef GfHalf ScalarType;
69  static const size_t dimension = 2;
70 
72  GfVec2h() = default;
73 
75  constexpr explicit GfVec2h(GfHalf value)
76  : _data{ value, value }
77  {
78  }
79 
81  constexpr GfVec2h(GfHalf s0, GfHalf s1)
82  : _data{ s0, s1 }
83  {
84  }
85 
87  template <class Scl>
88  constexpr explicit GfVec2h(Scl const *p)
89  : _data{ p[0], p[1] }
90  {
91  }
92 
94  explicit GfVec2h(class GfVec2d const &other);
95 
97  explicit GfVec2h(class GfVec2f const &other);
98 
100  GfVec2h(class GfVec2i const &other);
101 
103  static GfVec2h XAxis() {
104  GfVec2h result(0);
105  result[0] = 1;
106  return result;
107  }
109  static GfVec2h YAxis() {
110  GfVec2h result(0);
111  result[1] = 1;
112  return result;
113  }
114 
117  static GfVec2h Axis(size_t i) {
118  GfVec2h result(0);
119  if (i < 2)
120  result[i] = 1;
121  return result;
122  }
123 
126  _data[0] = s0;
127  _data[1] = s1;
128  return *this;
129  }
130 
132  GfVec2h &Set(GfHalf const *a) {
133  return Set(a[0], a[1]);
134  }
135 
137  GfHalf const *data() const { return _data; }
138  GfHalf *data() { return _data; }
139  GfHalf const *GetArray() const { return data(); }
140 
142  GfHalf const &operator[](size_t i) const { return _data[i]; }
143  GfHalf &operator[](size_t i) { return _data[i]; }
144 
146  friend inline size_t hash_value(GfVec2h const &vec) {
147  size_t h = 0;
148  boost::hash_combine(h, vec[0]);
149  boost::hash_combine(h, vec[1]);
150  return h;
151  }
152 
154  bool operator==(GfVec2h const &other) const {
155  return _data[0] == other[0] &&
156  _data[1] == other[1];
157  }
158  bool operator!=(GfVec2h const &other) const {
159  return !(*this == other);
160  }
161 
162  // TODO Add inequality for other vec types...
164  GF_API
165  bool operator==(class GfVec2d const &other) const;
167  GF_API
168  bool operator==(class GfVec2f const &other) const;
170  GF_API
171  bool operator==(class GfVec2i const &other) const;
172 
174  GfVec2h operator-() const {
175  return GfVec2h(-_data[0], -_data[1]);
176  }
177 
179  GfVec2h &operator+=(GfVec2h const &other) {
180  _data[0] += other[0];
181  _data[1] += other[1];
182  return *this;
183  }
184  friend GfVec2h operator+(GfVec2h const &l, GfVec2h const &r) {
185  return GfVec2h(l) += r;
186  }
187 
189  GfVec2h &operator-=(GfVec2h const &other) {
190  _data[0] -= other[0];
191  _data[1] -= other[1];
192  return *this;
193  }
194  friend GfVec2h operator-(GfVec2h const &l, GfVec2h const &r) {
195  return GfVec2h(l) -= r;
196  }
197 
199  GfVec2h &operator*=(double s) {
200  _data[0] *= s;
201  _data[1] *= s;
202  return *this;
203  }
204  GfVec2h operator*(double s) const {
205  return GfVec2h(*this) *= s;
206  }
207  friend GfVec2h operator*(double s, GfVec2h const &v) {
208  return v * s;
209  }
210 
212  // TODO should divide by the scalar type.
213  GfVec2h &operator/=(double s) {
214  // TODO This should not multiply by 1/s, it should do the division.
215  // Doing the division is more numerically stable when s is close to
216  // zero.
217  return *this *= (1.0 / s);
218  }
219  GfVec2h operator/(double s) const {
220  return *this * (1.0 / s);
221  }
222 
224  GfHalf operator*(GfVec2h const &v) const {
225  return _data[0] * v[0] + _data[1] * v[1];
226  }
227 
232  GfVec2h GetProjection(GfVec2h const &v) const {
233  return v * (*this * v);
234  }
235 
241  GfVec2h GetComplement(GfVec2h const &b) const {
242  return *this - this->GetProjection(b);
243  }
244 
246  GfHalf GetLengthSq() const {
247  return *this * *this;
248  }
249 
251  GfHalf GetLength() const {
252  return GfSqrt(GetLengthSq());
253  }
254 
263  GfHalf Normalize(GfHalf eps = 0.001) {
264  // TODO this seems suspect... suggest dividing by length so long as
265  // length is not zero.
266  GfHalf length = GetLength();
267  *this /= (length > eps) ? length : eps;
268  return length;
269  }
270 
271  GfVec2h GetNormalized(GfHalf eps = 0.001) const {
272  GfVec2h normalized(*this);
273  normalized.Normalize(eps);
274  return normalized;
275  }
276 
277 
278 private:
279  GfHalf _data[2];
280 };
281 
284 GF_API std::ostream& operator<<(std::ostream &, GfVec2h const &);
285 
286 
287 PXR_NAMESPACE_CLOSE_SCOPE
288 
289 #include "pxr/base/gf/vec2d.h"
290 #include "pxr/base/gf/vec2f.h"
291 #include "pxr/base/gf/vec2i.h"
292 
293 PXR_NAMESPACE_OPEN_SCOPE
294 
295 inline
296 GfVec2h::GfVec2h(class GfVec2d const &other)
297 {
298  _data[0] = other[0];
299  _data[1] = other[1];
300 }
301 inline
302 GfVec2h::GfVec2h(class GfVec2f const &other)
303 {
304  _data[0] = other[0];
305  _data[1] = other[1];
306 }
307 inline
308 GfVec2h::GfVec2h(class GfVec2i const &other)
309 {
310  _data[0] = other[0];
311  _data[1] = other[1];
312 }
313 
315 inline GfVec2h
316 GfCompMult(GfVec2h const &v1, GfVec2h const &v2) {
317  return GfVec2h(
318  v1[0] * v2[0],
319  v1[1] * v2[1]
320  );
321 }
322 
324 inline GfVec2h
325 GfCompDiv(GfVec2h const &v1, GfVec2h const &v2) {
326  return GfVec2h(
327  v1[0] / v2[0],
328  v1[1] / v2[1]
329  );
330 }
331 
333 inline GfHalf
334 GfDot(GfVec2h const &v1, GfVec2h const &v2) {
335  return v1 * v2;
336 }
337 
338 
340 inline GfHalf
342 {
343  return v.GetLength();
344 }
345 
349 inline GfHalf
350 GfNormalize(GfVec2h *v, GfHalf eps = 0.001)
351 {
352  return v->Normalize(eps);
353 }
354 
358 inline GfVec2h
359 GfGetNormalized(GfVec2h const &v, GfHalf eps = 0.001)
360 {
361  return v.GetNormalized(eps);
362 }
363 
368 inline GfVec2h
369 GfGetProjection(GfVec2h const &a, GfVec2h const &b)
370 {
371  return a.GetProjection(b);
372 }
373 
378 inline GfVec2h
379 GfGetComplement(GfVec2h const &a, GfVec2h const &b)
380 {
381  return a.GetComplement(b);
382 }
383 
386 inline bool
387 GfIsClose(GfVec2h const &v1, GfVec2h const &v2, double tolerance)
388 {
389  GfVec2h delta = v1 - v2;
390  return delta.GetLengthSq() <= tolerance * tolerance;
391 }
392 
393 
394 
395 PXR_NAMESPACE_CLOSE_SCOPE
396 
397 #endif // PXR_BASE_GF_VEC2H_H
GfVec2h GfCompDiv(GfVec2h const &v1, GfVec2h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2h.h:325
GfVec2h & operator *=(double s)
Multiplication by scalar.
Definition: vec2h.h:199
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
GfVec2h GetComplement(GfVec2h const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec2h.h:241
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.
GfVec2h()=default
Default constructor does no initialization.
GfVec2h & operator/=(double s)
Division by scalar.
Definition: vec2h.h:213
GfVec2h GfGetNormalized(GfVec2h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec2h.h:359
GfVec2h GfGetComplement(GfVec2h const &a, GfVec2h const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec2h.h:379
Low-level utilities for informing users of various internal and external diagnostic conditions.
bool GfIsClose(GfVec2h const &v1, GfVec2h const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition: vec2h.h:387
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition: traits.h:36
GfHalf GfGetLength(GfVec2h const &v)
Returns the geometric length of v.
Definition: vec2h.h:341
Basic type for a vector of 2 double components.
Definition: vec2d.h:63
GfVec2h & operator-=(GfVec2h const &other)
Subtraction.
Definition: vec2h.h:189
GfHalf GetLength() const
Length.
Definition: vec2h.h:251
bool operator==(GfVec2h const &other) const
Equality comparison.
Definition: vec2h.h:154
constexpr GfVec2h(GfHalf value)
Initialize all elements to a single value.
Definition: vec2h.h:75
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec2h.h:263
Basic type for a vector of 2 GfHalf components.
Definition: vec2h.h:64
constexpr GfVec2h(Scl const *p)
Construct with pointer to values.
Definition: vec2h.h:88
GfHalf GfNormalize(GfVec2h *v, GfHalf eps=0.001)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec2h.h:350
static GfVec2h XAxis()
Create a unit vector along the X-axis.
Definition: vec2h.h:103
static GfVec2h Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec2h.h:117
GF_API std::ostream & operator<<(std::ostream &, GfVec2h const &)
Output a GfVec2h.
friend size_t hash_value(GfVec2h const &vec)
Hash.
Definition: vec2h.h:146
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec2h.h:142
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:80
GfVec2h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec2h.h:132
GfVec2h GfCompMult(GfVec2h const &v1, GfVec2h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2h.h:316
GfVec2h & operator+=(GfVec2h const &other)
Addition.
Definition: vec2h.h:179
static GfVec2h YAxis()
Create a unit vector along the Y-axis.
Definition: vec2h.h:109
GfHalf GetLengthSq() const
Squared length.
Definition: vec2h.h:246
Basic type for a vector of 2 float components.
Definition: vec2f.h:63
GfVec2h GetProjection(GfVec2h const &v) const
Returns the projection of this onto v.
Definition: vec2h.h:232
Defines useful mathematical limits.
GfHalf const * data() const
Direct data access.
Definition: vec2h.h:137
constexpr GfVec2h(GfHalf s0, GfHalf s1)
Initialize all elements with explicit arguments.
Definition: vec2h.h:81
GfHalf ScalarType
Scalar element type and dimension.
Definition: vec2h.h:68
GfVec2h operator-() const
Create a vec with negated elements.
Definition: vec2h.h:174
GfHalf GfDot(GfVec2h const &v1, GfVec2h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2h.h:334
GfVec2h GfGetProjection(GfVec2h const &a, GfVec2h const &b)
Returns the projection of a onto b.
Definition: vec2h.h:369
GfVec2h & Set(GfHalf s0, GfHalf s1)
Set all elements with passed arguments.
Definition: vec2h.h:125