All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vec2f.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 GF_VEC2F_H
29 #define GF_VEC2F_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 
41 #include <boost/functional/hash.hpp>
42 
43 #include <cstddef>
44 #include <cmath>
45 
46 #include <iosfwd>
47 
48 PXR_NAMESPACE_OPEN_SCOPE
49 
50 class GfVec2f;
51 
52 template <>
53 struct GfIsGfVec<class GfVec2f> { static const bool value = true; };
54 
63 class GfVec2f
64 {
65 public:
67  typedef float ScalarType;
68  static const size_t dimension = 2;
69 
71  GfVec2f() = default;
72 
74  constexpr explicit GfVec2f(float value)
75  : _data{ value, value }
76  {
77  }
78 
80  constexpr GfVec2f(float s0, float s1)
81  : _data{ s0, s1 }
82  {
83  }
84 
86  template <class Scl>
87  constexpr explicit GfVec2f(Scl const *p)
88  : _data{ p[0], p[1] }
89  {
90  }
91 
93  explicit GfVec2f(class GfVec2d const &other);
94 
96  GfVec2f(class GfVec2h const &other);
97 
99  GfVec2f(class GfVec2i const &other);
100 
102  static GfVec2f XAxis() {
103  GfVec2f result(0);
104  result[0] = 1;
105  return result;
106  }
108  static GfVec2f YAxis() {
109  GfVec2f result(0);
110  result[1] = 1;
111  return result;
112  }
113 
116  static GfVec2f Axis(size_t i) {
117  GfVec2f result(0);
118  if (i < 2)
119  result[i] = 1;
120  return result;
121  }
122 
124  GfVec2f &Set(float s0, float s1) {
125  _data[0] = s0;
126  _data[1] = s1;
127  return *this;
128  }
129 
131  GfVec2f &Set(float const *a) {
132  return Set(a[0], a[1]);
133  }
134 
136  float const *data() const { return _data; }
137  float *data() { return _data; }
138  float const *GetArray() const { return data(); }
139 
141  float const &operator[](size_t i) const { return _data[i]; }
142  float &operator[](size_t i) { return _data[i]; }
143 
145  friend inline size_t hash_value(GfVec2f const &vec) {
146  size_t h = 0;
147  boost::hash_combine(h, vec[0]);
148  boost::hash_combine(h, vec[1]);
149  return h;
150  }
151 
153  bool operator==(GfVec2f const &other) const {
154  return _data[0] == other[0] &&
155  _data[1] == other[1];
156  }
157  bool operator!=(GfVec2f const &other) const {
158  return !(*this == other);
159  }
160 
161  // TODO Add inequality for other vec types...
163  GF_API
164  bool operator==(class GfVec2d const &other) const;
166  GF_API
167  bool operator==(class GfVec2h const &other) const;
169  GF_API
170  bool operator==(class GfVec2i const &other) const;
171 
173  GfVec2f operator-() const {
174  return GfVec2f(-_data[0], -_data[1]);
175  }
176 
178  GfVec2f &operator+=(GfVec2f const &other) {
179  _data[0] += other[0];
180  _data[1] += other[1];
181  return *this;
182  }
183  friend GfVec2f operator+(GfVec2f const &l, GfVec2f const &r) {
184  return GfVec2f(l) += r;
185  }
186 
188  GfVec2f &operator-=(GfVec2f const &other) {
189  _data[0] -= other[0];
190  _data[1] -= other[1];
191  return *this;
192  }
193  friend GfVec2f operator-(GfVec2f const &l, GfVec2f const &r) {
194  return GfVec2f(l) -= r;
195  }
196 
198  GfVec2f &operator*=(double s) {
199  _data[0] *= s;
200  _data[1] *= s;
201  return *this;
202  }
203  GfVec2f operator*(double s) const {
204  return GfVec2f(*this) *= s;
205  }
206  friend GfVec2f operator*(double s, GfVec2f const &v) {
207  return v * s;
208  }
209 
211  // TODO should divide by the scalar type.
212  GfVec2f &operator/=(double s) {
213  // TODO This should not multiply by 1/s, it should do the division.
214  // Doing the division is more numerically stable when s is close to
215  // zero.
216  return *this *= (1.0 / s);
217  }
218  GfVec2f operator/(double s) const {
219  return *this * (1.0 / s);
220  }
221 
223  float operator*(GfVec2f const &v) const {
224  return _data[0] * v[0] + _data[1] * v[1];
225  }
226 
231  GfVec2f GetProjection(GfVec2f const &v) const {
232  return v * (*this * v);
233  }
234 
240  GfVec2f GetComplement(GfVec2f const &b) const {
241  return *this - this->GetProjection(b);
242  }
243 
245  float GetLengthSq() const {
246  return *this * *this;
247  }
248 
250  float GetLength() const {
251  // TODO should use GfSqrt.
252  return sqrt(GetLengthSq());
253  }
254 
263  float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
264  // TODO this seems suspect... suggest dividing by length so long as
265  // length is not zero.
266  float length = GetLength();
267  *this /= (length > eps) ? length : eps;
268  return length;
269  }
270 
271  GfVec2f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
272  GfVec2f normalized(*this);
273  normalized.Normalize(eps);
274  return normalized;
275  }
276 
277 
278 private:
279  float _data[2];
280 };
281 
284 GF_API std::ostream& operator<<(std::ostream &, GfVec2f const &);
285 
286 
287 PXR_NAMESPACE_CLOSE_SCOPE
288 
289 #include "pxr/base/gf/vec2d.h"
290 #include "pxr/base/gf/vec2h.h"
291 #include "pxr/base/gf/vec2i.h"
292 
293 PXR_NAMESPACE_OPEN_SCOPE
294 
295 inline
296 GfVec2f::GfVec2f(class GfVec2d const &other)
297 {
298  _data[0] = other[0];
299  _data[1] = other[1];
300 }
301 inline
302 GfVec2f::GfVec2f(class GfVec2h const &other)
303 {
304  _data[0] = other[0];
305  _data[1] = other[1];
306 }
307 inline
308 GfVec2f::GfVec2f(class GfVec2i const &other)
309 {
310  _data[0] = other[0];
311  _data[1] = other[1];
312 }
313 
315 inline GfVec2f
316 GfCompMult(GfVec2f const &v1, GfVec2f const &v2) {
317  return GfVec2f(
318  v1[0] * v2[0],
319  v1[1] * v2[1]
320  );
321 }
322 
324 inline GfVec2f
325 GfCompDiv(GfVec2f const &v1, GfVec2f const &v2) {
326  return GfVec2f(
327  v1[0] / v2[0],
328  v1[1] / v2[1]
329  );
330 }
331 
333 inline float
334 GfDot(GfVec2f const &v1, GfVec2f const &v2) {
335  return v1 * v2;
336 }
337 
338 
340 inline float
342 {
343  return v.GetLength();
344 }
345 
349 inline float
351 {
352  return v->Normalize(eps);
353 }
354 
358 inline GfVec2f
360 {
361  return v.GetNormalized(eps);
362 }
363 
368 inline GfVec2f
369 GfGetProjection(GfVec2f const &a, GfVec2f const &b)
370 {
371  return a.GetProjection(b);
372 }
373 
378 inline GfVec2f
379 GfGetComplement(GfVec2f const &a, GfVec2f const &b)
380 {
381  return a.GetComplement(b);
382 }
383 
386 inline bool
387 GfIsClose(GfVec2f const &v1, GfVec2f const &v2, double tolerance)
388 {
389  GfVec2f delta = v1 - v2;
390  return delta.GetLengthSq() <= tolerance * tolerance;
391 }
392 
393 
394 
395 PXR_NAMESPACE_CLOSE_SCOPE
396 
397 #endif // GF_VEC2F_H
static GfVec2f Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec2f.h:116
GfVec2d GfGetProjection(GfVec2d const &a, GfVec2d const &b)
Returns the projection of a onto b.
Definition: vec2d.h:369
bool GfIsClose(double a, double b, double epsilon)
Returns true if a and b are with epsilon of each other.
Definition: math.h:39
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
float const * data() const
Direct data access.
Definition: vec2f.h:136
constexpr GfVec2f(Scl const *p)
Construct with pointer to values.
Definition: vec2f.h:87
GfVec2f & operator/=(double s)
Division by scalar.
Definition: vec2f.h:212
float const & operator[](size_t i) const
Indexing.
Definition: vec2f.h:141
GfVec2f operator-() const
Create a vec with negated elements.
Definition: vec2f.h:173
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:34
constexpr GfVec2f(float value)
Initialize all elements to a single value.
Definition: vec2f.h:74
bool operator==(GfVec2f const &other) const
Equality comparison.
Definition: vec2f.h:153
Basic type for a vector of 2 double components.
Definition: vec2d.h:63
float ScalarType
Scalar element type and dimension.
Definition: vec2f.h:67
float GetLength() const
Length.
Definition: vec2f.h:250
GfVec2f()=default
Default constructor does no initialization.
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
GfVec2f & operator-=(GfVec2f const &other)
Subtraction.
Definition: vec2f.h:188
static GfVec2f XAxis()
Create a unit vector along the X-axis.
Definition: vec2f.h:102
Basic type for a vector of 2 GfHalf components.
Definition: vec2h.h:64
GfVec2f GetProjection(GfVec2f const &v) const
Returns the projection of this onto v.
Definition: vec2f.h:231
GfVec2f & Set(float const *a)
Set all elements with a pointer to data.
Definition: vec2f.h:131
GfVec2d GfCompMult(GfVec2d const &v1, GfVec2d const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2d.h:316
friend size_t hash_value(GfVec2f const &vec)
Hash.
Definition: vec2f.h:145
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
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec2f.h:263
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
GfVec2f GetComplement(GfVec2f const &b) const
Returns the orthogonal complement of this-&gt;GetProjection(b).
Definition: vec2f.h:240
float GetLengthSq() const
Squared length.
Definition: vec2f.h:245
GfVec2f & Set(float s0, float s1)
Set all elements with passed arguments.
Definition: vec2f.h:124
constexpr GfVec2f(float s0, float s1)
Initialize all elements with explicit arguments.
Definition: vec2f.h:80
Basic type for a vector of 2 float components.
Definition: vec2f.h:63
GfVec2f & operator+=(GfVec2f const &other)
Addition.
Definition: vec2f.h:178
float operator*(GfVec2f const &v) const
See GfDot().
Definition: vec2f.h:223
GfVec2d GfCompDiv(GfVec2d const &v1, GfVec2d const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2d.h:325
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
GfVec2f & operator*=(double s)
Multiplication by scalar.
Definition: vec2f.h:198
#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
GfHalf GfDot(GfHalf a, GfHalf b)
Returns the dot (inner) product of two vectors.
Definition: half.h:55
static GfVec2f YAxis()
Create a unit vector along the Y-axis.
Definition: vec2f.h:108