All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  // TODO should use GfSqrt.
253  return sqrt(GetLengthSq());
254  }
255 
264  GfHalf Normalize(GfHalf eps = 0.001) {
265  // TODO this seems suspect... suggest dividing by length so long as
266  // length is not zero.
267  GfHalf length = GetLength();
268  *this /= (length > eps) ? length : eps;
269  return length;
270  }
271 
272  GfVec2h GetNormalized(GfHalf eps = 0.001) const {
273  GfVec2h normalized(*this);
274  normalized.Normalize(eps);
275  return normalized;
276  }
277 
278 
279 private:
280  GfHalf _data[2];
281 };
282 
285 GF_API std::ostream& operator<<(std::ostream &, GfVec2h const &);
286 
287 
288 PXR_NAMESPACE_CLOSE_SCOPE
289 
290 #include "pxr/base/gf/vec2d.h"
291 #include "pxr/base/gf/vec2f.h"
292 #include "pxr/base/gf/vec2i.h"
293 
294 PXR_NAMESPACE_OPEN_SCOPE
295 
296 inline
297 GfVec2h::GfVec2h(class GfVec2d const &other)
298 {
299  _data[0] = other[0];
300  _data[1] = other[1];
301 }
302 inline
303 GfVec2h::GfVec2h(class GfVec2f const &other)
304 {
305  _data[0] = other[0];
306  _data[1] = other[1];
307 }
308 inline
309 GfVec2h::GfVec2h(class GfVec2i const &other)
310 {
311  _data[0] = other[0];
312  _data[1] = other[1];
313 }
314 
316 inline GfVec2h
317 GfCompMult(GfVec2h const &v1, GfVec2h const &v2) {
318  return GfVec2h(
319  v1[0] * v2[0],
320  v1[1] * v2[1]
321  );
322 }
323 
325 inline GfVec2h
326 GfCompDiv(GfVec2h const &v1, GfVec2h const &v2) {
327  return GfVec2h(
328  v1[0] / v2[0],
329  v1[1] / v2[1]
330  );
331 }
332 
334 inline GfHalf
335 GfDot(GfVec2h const &v1, GfVec2h const &v2) {
336  return v1 * v2;
337 }
338 
339 
341 inline GfHalf
343 {
344  return v.GetLength();
345 }
346 
350 inline GfHalf
351 GfNormalize(GfVec2h *v, GfHalf eps = 0.001)
352 {
353  return v->Normalize(eps);
354 }
355 
359 inline GfVec2h
360 GfGetNormalized(GfVec2h const &v, GfHalf eps = 0.001)
361 {
362  return v.GetNormalized(eps);
363 }
364 
369 inline GfVec2h
370 GfGetProjection(GfVec2h const &a, GfVec2h const &b)
371 {
372  return a.GetProjection(b);
373 }
374 
379 inline GfVec2h
380 GfGetComplement(GfVec2h const &a, GfVec2h const &b)
381 {
382  return a.GetComplement(b);
383 }
384 
387 inline bool
388 GfIsClose(GfVec2h const &v1, GfVec2h const &v2, double tolerance)
389 {
390  GfVec2h delta = v1 - v2;
391  return delta.GetLengthSq() <= tolerance * tolerance;
392 }
393 
394 
395 
396 PXR_NAMESPACE_CLOSE_SCOPE
397 
398 #endif // PXR_BASE_GF_VEC2H_H
GfVec2d GfGetProjection(GfVec2d const &a, GfVec2d const &b)
Returns the projection of a onto b.
Definition: vec2d.h:369
bool operator==(GfVec2h const &other) const
Equality comparison.
Definition: vec2h.h:154
bool GfIsClose(double a, double b, double epsilon)
Returns true if a and b are with epsilon of each other.
Definition: math.h:42
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
decltype(std::declval< Left >()*std::declval< Right >()) GfCompMult(Left left, Right right)
Returns component-wise multiplication of vectors.
Definition: math.h:253
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
Low-level utilities for informing users of various internal and external diagnostic conditions...
A metafunction with a static const bool member &#39;value&#39; that is true for GfVec types, like GfVec2i, GfVec4d, etc and false for all other types.
Definition: traits.h:36
GfHalf GetLength() const
Length.
Definition: vec2h.h:251
Basic type for a vector of 2 double components.
Definition: vec2d.h:63
GfHalf GetLengthSq() const
Squared length.
Definition: vec2h.h:246
GfVec2h & operator-=(GfVec2h const &other)
Subtraction.
Definition: vec2h.h:189
constexpr GfVec2h(GfHalf value)
Initialize all elements to a single value.
Definition: vec2h.h:75
GfVec2d GfGetNormalized(GfVec2d const &v, double eps=GF_MIN_VECTOR_LENGTH)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec2d.h:359
GfVec2h operator-() const
Create a vec with negated elements.
Definition: vec2h.h:174
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec2h.h:264
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
decltype(std::declval< Left >()/std::declval< Right >()) GfCompDiv(Left left, Right right)
Returns component-wise quotient of vectors.
Definition: math.h:264
GfHalf const * data() const
Direct data access.
Definition: vec2h.h:137
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
decltype(std::declval< Left >()*std::declval< Right >()) GfDot(Left left, Right right)
Returns the dot (inner) product of two vectors.
Definition: math.h:242
friend size_t hash_value(GfVec2h const &vec)
Hash.
Definition: vec2h.h:146
double GfNormalize(GfVec2d *v, double eps=GF_MIN_VECTOR_LENGTH)
Normalizes *v in place to unit length, returning the length before normalization. ...
Definition: vec2d.h:350
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec2h.h:142
GfVec2h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec2h.h:132
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
GfVec2h GetProjection(GfVec2h const &v) const
Returns the projection of this onto v.
Definition: vec2h.h:232
GfVec2h GetComplement(GfVec2h const &b) const
Returns the orthogonal complement of this-&gt;GetProjection(b).
Definition: vec2h.h:241
GfHalf operator*(GfVec2h const &v) const
See GfDot().
Definition: vec2h.h:224
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
GfVec2h & operator*=(double s)
Multiplication by scalar.
Definition: vec2h.h:199
Basic type for a vector of 2 float components.
Definition: vec2f.h:63
Defines useful mathematical limits.
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
double GfGetLength(GfVec2d const &v)
Returns the geometric length of v.
Definition: vec2d.h:341
GfVec2d GfGetComplement(GfVec2d const &a, GfVec2d const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec2d.h:379
GfVec2h & Set(GfHalf s0, GfHalf s1)
Set all elements with passed arguments.
Definition: vec2h.h:125