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"
19#include "pxr/base/gf/api.h"
20#include "pxr/base/gf/limits.h"
21#include "pxr/base/gf/traits.h"
22#include "pxr/base/gf/math.h"
23#include "pxr/base/tf/hash.h"
24
25#include <cstddef>
26#include <cmath>
27
28#include <iosfwd>
29
30PXR_NAMESPACE_OPEN_SCOPE
31
32class GfVec2f;
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
54 GfVec2f() = default;
55
57 constexpr explicit GfVec2f(float value)
58 : _data{ value, value }
59 {
60 }
61
63 constexpr GfVec2f(float s0, float s1)
64 : _data{ s0, s1 }
65 {
66 }
67
69 template <class Scl>
70 constexpr explicit GfVec2f(Scl const *p)
71 : _data{ p[0], p[1] }
72 {
73 }
74
76 explicit GfVec2f(class GfVec2d const &other);
77
79 GfVec2f(class GfVec2h const &other);
80
82 GfVec2f(class GfVec2i const &other);
83
85 static GfVec2f XAxis() {
86 GfVec2f result(0);
87 result[0] = 1;
88 return result;
89 }
91 static GfVec2f YAxis() {
92 GfVec2f result(0);
93 result[1] = 1;
94 return result;
95 }
96
99 static GfVec2f Axis(size_t i) {
100 GfVec2f result(0);
101 if (i < 2)
102 result[i] = 1;
103 return result;
104 }
105
107 GfVec2f &Set(float s0, float s1) {
108 _data[0] = s0;
109 _data[1] = s1;
110 return *this;
111 }
112
114 GfVec2f &Set(float const *a) {
115 return Set(a[0], a[1]);
116 }
117
119 float const *data() const { return _data; }
120 float *data() { return _data; }
121 float const *GetArray() const { return data(); }
122
124 float const &operator[](size_t i) const { return _data[i]; }
125 float &operator[](size_t i) { return _data[i]; }
126
128 friend inline size_t hash_value(GfVec2f const &vec) {
129 return TfHash::Combine(vec[0], vec[1]);
130 }
131
133 bool operator==(GfVec2f const &other) const {
134 return _data[0] == other[0] &&
135 _data[1] == other[1];
136 }
137 bool operator!=(GfVec2f const &other) const {
138 return !(*this == other);
139 }
140
141 // TODO Add inequality for other vec types...
143 GF_API
144 bool operator==(class GfVec2d const &other) const;
146 GF_API
147 bool operator==(class GfVec2h const &other) const;
149 GF_API
150 bool operator==(class GfVec2i const &other) const;
151
154 return GfVec2f(-_data[0], -_data[1]);
155 }
156
158 GfVec2f &operator+=(GfVec2f const &other) {
159 _data[0] += other[0];
160 _data[1] += other[1];
161 return *this;
162 }
163 friend GfVec2f operator+(GfVec2f const &l, GfVec2f const &r) {
164 return GfVec2f(l) += r;
165 }
166
168 GfVec2f &operator-=(GfVec2f const &other) {
169 _data[0] -= other[0];
170 _data[1] -= other[1];
171 return *this;
172 }
173 friend GfVec2f operator-(GfVec2f const &l, GfVec2f const &r) {
174 return GfVec2f(l) -= r;
175 }
176
178 GfVec2f &operator*=(double s) {
179 _data[0] *= s;
180 _data[1] *= s;
181 return *this;
182 }
183 GfVec2f operator*(double s) const {
184 return GfVec2f(*this) *= s;
185 }
186 friend GfVec2f operator*(double s, GfVec2f const &v) {
187 return v * s;
188 }
189
191 // TODO should divide by the scalar type.
192 GfVec2f &operator/=(double s) {
193 // TODO This should not multiply by 1/s, it should do the division.
194 // Doing the division is more numerically stable when s is close to
195 // zero.
196 return *this *= (1.0 / s);
197 }
198 GfVec2f operator/(double s) const {
199 return *this * (1.0 / s);
200 }
201
203 float operator*(GfVec2f const &v) const {
204 return _data[0] * v[0] + _data[1] * v[1];
205 }
206
211 GfVec2f GetProjection(GfVec2f const &v) const {
212 return v * (*this * v);
213 }
214
220 GfVec2f GetComplement(GfVec2f const &b) const {
221 return *this - this->GetProjection(b);
222 }
223
225 float GetLengthSq() const {
226 return *this * *this;
227 }
228
230 float GetLength() const {
231 return GfSqrt(GetLengthSq());
232 }
233
242 float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
243 // TODO this seems suspect... suggest dividing by length so long as
244 // length is not zero.
245 float length = GetLength();
246 *this /= (length > eps) ? length : eps;
247 return length;
248 }
249
250 GfVec2f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
251 GfVec2f normalized(*this);
252 normalized.Normalize(eps);
253 return normalized;
254 }
255
256
257private:
258 float _data[2];
259};
260
263GF_API std::ostream& operator<<(std::ostream &, GfVec2f const &);
264
265
266PXR_NAMESPACE_CLOSE_SCOPE
267
268#include "pxr/base/gf/vec2d.h"
269#include "pxr/base/gf/vec2h.h"
270#include "pxr/base/gf/vec2i.h"
271
272PXR_NAMESPACE_OPEN_SCOPE
273
274inline
275GfVec2f::GfVec2f(class GfVec2d const &other)
276{
277 _data[0] = other[0];
278 _data[1] = other[1];
279}
280inline
281GfVec2f::GfVec2f(class GfVec2h const &other)
282{
283 _data[0] = other[0];
284 _data[1] = other[1];
285}
286inline
287GfVec2f::GfVec2f(class GfVec2i const &other)
288{
289 _data[0] = other[0];
290 _data[1] = other[1];
291}
292
294inline GfVec2f
295GfCompMult(GfVec2f const &v1, GfVec2f const &v2) {
296 return GfVec2f(
297 v1[0] * v2[0],
298 v1[1] * v2[1]
299 );
300}
301
303inline GfVec2f
304GfCompDiv(GfVec2f const &v1, GfVec2f const &v2) {
305 return GfVec2f(
306 v1[0] / v2[0],
307 v1[1] / v2[1]
308 );
309}
310
312inline float
313GfDot(GfVec2f const &v1, GfVec2f const &v2) {
314 return v1 * v2;
315}
316
317
319inline float
321{
322 return v.GetLength();
323}
324
328inline float
330{
331 return v->Normalize(eps);
332}
333
337inline GfVec2f
339{
340 return v.GetNormalized(eps);
341}
342
347inline GfVec2f
348GfGetProjection(GfVec2f const &a, GfVec2f const &b)
349{
350 return a.GetProjection(b);
351}
352
357inline GfVec2f
358GfGetComplement(GfVec2f const &a, GfVec2f const &b)
359{
360 return a.GetComplement(b);
361}
362
365inline bool
366GfIsClose(GfVec2f const &v1, GfVec2f const &v2, double tolerance)
367{
368 GfVec2f delta = v1 - v2;
369 return delta.GetLengthSq() <= tolerance * tolerance;
370}
371
372
373
374PXR_NAMESPACE_CLOSE_SCOPE
375
376#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:63
friend size_t hash_value(GfVec2f const &vec)
Hash.
Definition: vec2f.h:128
GfVec2f & operator+=(GfVec2f const &other)
Addition.
Definition: vec2f.h:158
GfVec2f GetComplement(GfVec2f const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec2f.h:220
GfVec2f & operator/=(double s)
Division by scalar.
Definition: vec2f.h:192
GfVec2f & operator-=(GfVec2f const &other)
Subtraction.
Definition: vec2f.h:168
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:107
static GfVec2f Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec2f.h:99
static GfVec2f XAxis()
Create a unit vector along the X-axis.
Definition: vec2f.h:85
constexpr GfVec2f(Scl const *p)
Construct with pointer to values.
Definition: vec2f.h:70
float GetLength() const
Length.
Definition: vec2f.h:230
static GfVec2f YAxis()
Create a unit vector along the Y-axis.
Definition: vec2f.h:91
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec2f.h:242
float operator*(GfVec2f const &v) const
See GfDot().
Definition: vec2f.h:203
GfVec2f operator-() const
Create a vec with negated elements.
Definition: vec2f.h:153
float const & operator[](size_t i) const
Indexing.
Definition: vec2f.h:124
float GetLengthSq() const
Squared length.
Definition: vec2f.h:225
bool operator==(GfVec2f const &other) const
Equality comparison.
Definition: vec2f.h:133
GfVec2f & Set(float const *a)
Set all elements with a pointer to data.
Definition: vec2f.h:114
GfVec2f & operator*=(double s)
Multiplication by scalar.
Definition: vec2f.h:178
GfVec2f GetProjection(GfVec2f const &v) const
Returns the projection of this onto v.
Definition: vec2f.h:211
constexpr GfVec2f(float value)
Initialize all elements to a single value.
Definition: vec2f.h:57
GF_API bool operator==(class GfVec2d const &other) const
Equality comparison.
float const * data() const
Direct data access.
Definition: vec2f.h:119
float ScalarType
Scalar element type and dimension.
Definition: vec2f.h:49
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:187
#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 &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
Defines useful mathematical limits.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition: traits.h:19
float GfGetLength(GfVec2f const &v)
Returns the geometric length of v.
Definition: vec2f.h:320
GfVec2f GfGetProjection(GfVec2f const &a, GfVec2f const &b)
Returns the projection of a onto b.
Definition: vec2f.h:348
GfVec2f GfGetComplement(GfVec2f const &a, GfVec2f const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec2f.h:358
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:366
float GfDot(GfVec2f const &v1, GfVec2f const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2f.h:313
GfVec2f GfCompMult(GfVec2f const &v1, GfVec2f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2f.h:295
GfVec2f GfCompDiv(GfVec2f const &v1, GfVec2f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2f.h:304
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:338
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:329