Loading...
Searching...
No Matches
vec2f.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
8// This file is generated by a script. Do not edit directly. Edit the
9// vec.template.h file to make changes.
10
11#ifndef PXR_BASE_GF_VEC2F_H
12#define PXR_BASE_GF_VEC2F_H
13
16
17#include "pxr/pxr.h"
18#include "pxr/base/gf/api.h"
19#include "pxr/base/gf/limits.h"
20#include "pxr/base/gf/traits.h"
21#include "pxr/base/gf/math.h"
23#include "pxr/base/tf/hash.h"
24
25#include <cstddef>
26#include <type_traits>
27#include <cmath>
28
29#include <iosfwd>
30
31PXR_NAMESPACE_OPEN_SCOPE
32
33
34template <>
35struct GfIsGfVec<class GfVec2f> { static const bool value = true; };
36
46{
47public:
49 typedef float ScalarType;
50 static const size_t dimension = 2;
51
63 float x, y;
65
68 GfVec2f() = default;
69
71 constexpr explicit GfVec2f(float value)
72 : x(value), y(value)
73 {
74 }
75
77 constexpr GfVec2f(float s0, float s1)
78 : x(s0), y(s1)
79 {
80 }
81
83 template <class Scl>
84 constexpr explicit GfVec2f(Scl const *p)
85 : x(p[0]), y(p[1])
86 {
87 }
88
90 explicit GfVec2f(class GfVec2d const &other);
91
93 GfVec2f(class GfVec2h const &other);
94
96 GfVec2f(class GfVec2i const &other);
98 static GfVec2f XAxis() {
99 GfVec2f result(0);
100 result[0] = 1;
101 return result;
102 }
103
105 static GfVec2f YAxis() {
106 GfVec2f result(0);
107 result[1] = 1;
108 return result;
109 }
110
113 static GfVec2f Axis(size_t i) {
114 GfVec2f result(0);
115 if (i < 2)
116 result[i] = 1;
117 return result;
118 }
119
121 GfVec2f &Set(float s0, float s1) {
122 x = s0;
123 y = s1;
124 return *this;
125 }
126
128 GfVec2f &Set(float const *a) {
129 return Set(a[0], a[1]);
130 }
131
137 float const *data() const { return &x; }
138 float *data() { return &x; }
139 float const *GetArray() const { return data(); }
140
142 float const &operator[](size_t i) const { return data()[i]; }
143 float &operator[](size_t i) { return data()[i]; }
144
146 friend inline size_t hash_value(GfVec2f const &vec) {
147 return TfHash::Combine(vec.x, vec.y);
148 }
149
151 bool operator==(GfVec2f const &other) const {
152 return x == other.x &&
153 y == other.y;
154 }
155 bool operator!=(GfVec2f const &other) const {
156 return !(*this == other);
157 }
158
159 // TODO Add inequality for other vec types...
161 GF_API
162 bool operator==(class GfVec2d const &other) const;
164 GF_API
165 bool operator==(class GfVec2h const &other) const;
167 GF_API
168 bool operator==(class GfVec2i const &other) const;
169
172 return GfVec2f(-x, -y);
173 }
174
176 GfVec2f &operator+=(GfVec2f const &other) {
177 x += other.x;
178 y += other.y;
179 return *this;
180 }
181 friend GfVec2f operator+(GfVec2f const &l, GfVec2f const &r) {
182 return GfVec2f(l) += r;
183 }
184
186 GfVec2f &operator-=(GfVec2f const &other) {
187 x -= other.x;
188 y -= other.y;
189 return *this;
190 }
191 friend GfVec2f operator-(GfVec2f const &l, GfVec2f const &r) {
192 return GfVec2f(l) -= r;
193 }
194
196 GfVec2f &operator*=(double s) {
197 x *= s;
198 y *= s;
199 return *this;
200 }
201 GfVec2f operator*(double s) const {
202 return GfVec2f(*this) *= s;
203 }
204 friend GfVec2f operator*(double s, GfVec2f const &v) {
205 return v * s;
206 }
207
209 // TODO should divide by the scalar type.
210 GfVec2f &operator/=(double s) {
211 // TODO This should not multiply by 1/s, it should do the division.
212 // Doing the division is more numerically stable when s is close to
213 // zero.
214 return *this *= (1.0 / s);
215 }
216 GfVec2f operator/(double s) const {
217 return *this * (1.0 / s);
218 }
219
221 float operator*(GfVec2f const &v) const {
222 return x * v.x + y * v.y;
223 }
224
229 GfVec2f GetProjection(GfVec2f const &v) const {
230 return v * (*this * v);
231 }
232
238 GfVec2f GetComplement(GfVec2f const &b) const {
239 return *this - this->GetProjection(b);
240 }
241
243 float GetLengthSq() const {
244 return *this * *this;
245 }
246
248 float GetLength() const {
249 return GfSqrt(GetLengthSq());
250 }
251
260 float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
261 // TODO this seems suspect... suggest dividing by length so long as
262 // length is not zero.
263 float length = GetLength();
264 *this /= (length > eps) ? length : eps;
265 return length;
266 }
267
268 GfVec2f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
269 GfVec2f normalized(*this);
270 normalized.Normalize(eps);
271 return normalized;
272 }
273
274
290#if defined (doxygen)
291 constexpr GfVec2f xy() const;
292 constexpr GfVec2f yx() const;
294#else // !doxygen
295 // Note that these are templated on their own return type so that the
296 // declarations cost nothing in compile time & debug info unless they are
297 // actually called in a TU. Naming a different type is an error and
298 // static_assert()ed against.
299 template <class Vec = GfVec2f> constexpr Vec xy() const;
300 template <class Vec = GfVec2f> constexpr Vec yx() const;
301#endif // doxygen
302 };
303
306GF_API std::ostream& operator<<(std::ostream &, GfVec2f const &);
307
308PXR_NAMESPACE_CLOSE_SCOPE
309
310// Other scalar types of the same dimension, for the conversions above.
311#include "pxr/base/gf/vec2d.h"
312#include "pxr/base/gf/vec2h.h"
313#include "pxr/base/gf/vec2i.h"
314
315PXR_NAMESPACE_OPEN_SCOPE
316
317inline
318GfVec2f::GfVec2f(class GfVec2d const &other)
319{
320 x = other.x;
321 y = other.y;
322}
323inline
324GfVec2f::GfVec2f(class GfVec2h const &other)
325{
326 x = other.x;
327 y = other.y;
328}
329inline
330GfVec2f::GfVec2f(class GfVec2i const &other)
331{
332 x = other.x;
333 y = other.y;
334}
335
336template <class Vec>
337constexpr Vec
338GfVec2f::xy() const
339{
340 static_assert(std::is_same<Vec, GfVec2f>::value);
341 return Vec(x, y);
342}
343
344template <class Vec>
345constexpr Vec
346GfVec2f::yx() const
347{
348 static_assert(std::is_same<Vec, GfVec2f>::value);
349 return Vec(y, x);
350}
351
352
354inline GfVec2f
355GfCompMult(GfVec2f const &v1, GfVec2f const &v2) {
356 return GfVec2f(
357 v1.x * v2.x,
358 v1.y * v2.y
359 );
360}
361
363inline GfVec2f
364GfCompDiv(GfVec2f const &v1, GfVec2f const &v2) {
365 return GfVec2f(
366 v1.x / v2.x,
367 v1.y / v2.y
368 );
369}
370
372inline float
373GfDot(GfVec2f const &v1, GfVec2f const &v2) {
374 return v1 * v2;
375}
376
377
379inline float
381{
382 return v.GetLength();
383}
384
388inline float
390{
391 return v->Normalize(eps);
392}
393
397inline GfVec2f
399{
400 return v.GetNormalized(eps);
401}
402
407inline GfVec2f
408GfGetProjection(GfVec2f const &a, GfVec2f const &b)
409{
410 return a.GetProjection(b);
411}
412
417inline GfVec2f
418GfGetComplement(GfVec2f const &a, GfVec2f const &b)
419{
420 return a.GetComplement(b);
421}
422
425inline bool
426GfIsClose(GfVec2f const &v1, GfVec2f const &v2, double tolerance)
427{
428 GfVec2f delta = v1 - v2;
429 return delta.GetLengthSq() <= tolerance * tolerance;
430}
431
432
433
434PXR_NAMESPACE_CLOSE_SCOPE
435
436#endif // PXR_BASE_GF_VEC2F_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
Basic type for a vector of 2 double components.
Definition vec2d.h:46
Basic type for a vector of 2 float components.
Definition vec2f.h:46
GfVec2f()=default
GfVec2f value-initializes to zero and performs no default initialization, like float or double.
constexpr GfVec2f(float s0, float s1)
Initialize all elements with explicit arguments.
Definition vec2f.h:77
friend size_t hash_value(GfVec2f const &vec)
Hash.
Definition vec2f.h:146
GfVec2f & operator+=(GfVec2f const &other)
Addition.
Definition vec2f.h:176
GfVec2f GetComplement(GfVec2f const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition vec2f.h:238
GfVec2f & operator/=(double s)
Division by scalar.
Definition vec2f.h:210
GfVec2f & operator-=(GfVec2f const &other)
Subtraction.
Definition vec2f.h:186
GF_API bool operator==(class GfVec2h const &other) const
Equality comparison.
GfVec2f & Set(float s0, float s1)
Set all elements with passed arguments.
Definition vec2f.h:121
static GfVec2f Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition vec2f.h:113
static GfVec2f XAxis()
Create a unit vector along the X-axis.
Definition vec2f.h:98
constexpr GfVec2f(Scl const *p)
Construct with pointer to values.
Definition vec2f.h:84
float ScalarType
Scalar element type and dimension.
Definition vec2f.h:49
float GetLength() const
Length.
Definition vec2f.h:248
static GfVec2f YAxis()
Create a unit vector along the Y-axis.
Definition vec2f.h:105
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition vec2f.h:260
float operator*(GfVec2f const &v) const
See GfDot().
Definition vec2f.h:221
GfVec2f operator-() const
Create a vec with negated elements.
Definition vec2f.h:171
float const & operator[](size_t i) const
Indexing.
Definition vec2f.h:142
float GetLengthSq() const
Squared length.
Definition vec2f.h:243
bool operator==(GfVec2f const &other) const
Equality comparison.
Definition vec2f.h:151
GfVec2f & Set(float const *a)
Set all elements with a pointer to data.
Definition vec2f.h:128
GfVec2f & operator*=(double s)
Multiplication by scalar.
Definition vec2f.h:196
GfVec2f GetProjection(GfVec2f const &v) const
Returns the projection of this onto v.
Definition vec2f.h:229
constexpr GfVec2f(float value)
Initialize all elements to a single value.
Definition vec2f.h:71
GF_API bool operator==(class GfVec2d const &other) const
Equality comparison.
float const * data() const
Direct data access.
Definition vec2f.h:137
GF_API bool operator==(class GfVec2i const &other) const
Equality comparison.
Basic type for a vector of 2 GfHalf components.
Definition vec2h.h:47
Basic type for a vector of 2 int components.
Definition vec2i.h:44
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition hash.h:487
Assorted mathematical utility functions.
double GfSqrt(double f)
Return sqrt(f).
Definition math.h:190
#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:17
GF_API std::ostream & operator<<(std::ostream &, GfVec2f const &)
Output a GfVec2f.
Defines useful mathematical limits.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition traits.h:23
float GfGetLength(GfVec2f const &v)
Returns the geometric length of v.
Definition vec2f.h:380
GfVec2f GfGetProjection(GfVec2f const &a, GfVec2f const &b)
Returns the projection of a onto b.
Definition vec2f.h:408
GfVec2f GfGetComplement(GfVec2f const &a, GfVec2f const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition vec2f.h:418
bool GfIsClose(GfVec2f const &v1, GfVec2f const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition vec2f.h:426
float GfDot(GfVec2f const &v1, GfVec2f const &v2)
Returns the dot (inner) product of two vectors.
Definition vec2f.h:373
GfVec2f GfCompMult(GfVec2f const &v1, GfVec2f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition vec2f.h:355
GfVec2f GfCompDiv(GfVec2f const &v1, GfVec2f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition vec2f.h:364
GfVec2f GfGetNormalized(GfVec2f const &v, float eps=GF_MIN_VECTOR_LENGTH)
Returns a normalized (unit-length) vector with the same direction as v.
Definition vec2f.h:398
float GfNormalize(GfVec2f *v, float eps=GF_MIN_VECTOR_LENGTH)
Normalizes *v in place to unit length, returning the length before normalization.
Definition vec2f.h:389