This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
53 GfVec2f() = default;
54
56 constexpr explicit GfVec2f(float value)
57 : _data{ value, value }
58 {
59 }
60
62 constexpr GfVec2f(float s0, float s1)
63 : _data{ s0, s1 }
64 {
65 }
66
68 template <class Scl>
69 constexpr explicit GfVec2f(Scl const *p)
70 : _data{ p[0], p[1] }
71 {
72 }
73
75 explicit GfVec2f(class GfVec2d const &other);
76
78 GfVec2f(class GfVec2h const &other);
79
81 GfVec2f(class GfVec2i const &other);
82
84 static GfVec2f XAxis() {
85 GfVec2f result(0);
86 result[0] = 1;
87 return result;
88 }
90 static GfVec2f YAxis() {
91 GfVec2f result(0);
92 result[1] = 1;
93 return result;
94 }
95
98 static GfVec2f Axis(size_t i) {
99 GfVec2f result(0);
100 if (i < 2)
101 result[i] = 1;
102 return result;
103 }
104
106 GfVec2f &Set(float s0, float s1) {
107 _data[0] = s0;
108 _data[1] = s1;
109 return *this;
110 }
111
113 GfVec2f &Set(float const *a) {
114 return Set(a[0], a[1]);
115 }
116
118 float const *data() const { return _data; }
119 float *data() { return _data; }
120 float const *GetArray() const { return data(); }
121
123 float const &operator[](size_t i) const { return _data[i]; }
124 float &operator[](size_t i) { return _data[i]; }
125
127 friend inline size_t hash_value(GfVec2f const &vec) {
128 return TfHash::Combine(vec[0], vec[1]);
129 }
130
132 bool operator==(GfVec2f const &other) const {
133 return _data[0] == other[0] &&
134 _data[1] == other[1];
135 }
136 bool operator!=(GfVec2f const &other) const {
137 return !(*this == other);
138 }
139
140 // TODO Add inequality for other vec types...
142 GF_API
143 bool operator==(class GfVec2d const &other) const;
145 GF_API
146 bool operator==(class GfVec2h const &other) const;
148 GF_API
149 bool operator==(class GfVec2i const &other) const;
150
153 return GfVec2f(-_data[0], -_data[1]);
154 }
155
157 GfVec2f &operator+=(GfVec2f const &other) {
158 _data[0] += other[0];
159 _data[1] += other[1];
160 return *this;
161 }
162 friend GfVec2f operator+(GfVec2f const &l, GfVec2f const &r) {
163 return GfVec2f(l) += r;
164 }
165
167 GfVec2f &operator-=(GfVec2f const &other) {
168 _data[0] -= other[0];
169 _data[1] -= other[1];
170 return *this;
171 }
172 friend GfVec2f operator-(GfVec2f const &l, GfVec2f const &r) {
173 return GfVec2f(l) -= r;
174 }
175
177 GfVec2f &operator*=(double s) {
178 _data[0] *= s;
179 _data[1] *= s;
180 return *this;
181 }
182 GfVec2f operator*(double s) const {
183 return GfVec2f(*this) *= s;
184 }
185 friend GfVec2f operator*(double s, GfVec2f const &v) {
186 return v * s;
187 }
188
190 // TODO should divide by the scalar type.
191 GfVec2f &operator/=(double s) {
192 // TODO This should not multiply by 1/s, it should do the division.
193 // Doing the division is more numerically stable when s is close to
194 // zero.
195 return *this *= (1.0 / s);
196 }
197 GfVec2f operator/(double s) const {
198 return *this * (1.0 / s);
199 }
200
202 float operator*(GfVec2f const &v) const {
203 return _data[0] * v[0] + _data[1] * v[1];
204 }
205
210 GfVec2f GetProjection(GfVec2f const &v) const {
211 return v * (*this * v);
212 }
213
219 GfVec2f GetComplement(GfVec2f const &b) const {
220 return *this - this->GetProjection(b);
221 }
222
224 float GetLengthSq() const {
225 return *this * *this;
226 }
227
229 float GetLength() const {
230 return GfSqrt(GetLengthSq());
231 }
232
241 float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
242 // TODO this seems suspect... suggest dividing by length so long as
243 // length is not zero.
244 float length = GetLength();
245 *this /= (length > eps) ? length : eps;
246 return length;
247 }
248
249 GfVec2f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
250 GfVec2f normalized(*this);
251 normalized.Normalize(eps);
252 return normalized;
253 }
254
255
256private:
257 float _data[2];
258};
259
262GF_API std::ostream& operator<<(std::ostream &, GfVec2f const &);
263
264
265PXR_NAMESPACE_CLOSE_SCOPE
266
267#include "pxr/base/gf/vec2d.h"
268#include "pxr/base/gf/vec2h.h"
269#include "pxr/base/gf/vec2i.h"
270
271PXR_NAMESPACE_OPEN_SCOPE
272
273inline
274GfVec2f::GfVec2f(class GfVec2d const &other)
275{
276 _data[0] = other[0];
277 _data[1] = other[1];
278}
279inline
280GfVec2f::GfVec2f(class GfVec2h const &other)
281{
282 _data[0] = other[0];
283 _data[1] = other[1];
284}
285inline
286GfVec2f::GfVec2f(class GfVec2i const &other)
287{
288 _data[0] = other[0];
289 _data[1] = other[1];
290}
291
293inline GfVec2f
294GfCompMult(GfVec2f const &v1, GfVec2f const &v2) {
295 return GfVec2f(
296 v1[0] * v2[0],
297 v1[1] * v2[1]
298 );
299}
300
302inline GfVec2f
303GfCompDiv(GfVec2f const &v1, GfVec2f const &v2) {
304 return GfVec2f(
305 v1[0] / v2[0],
306 v1[1] / v2[1]
307 );
308}
309
311inline float
312GfDot(GfVec2f const &v1, GfVec2f const &v2) {
313 return v1 * v2;
314}
315
316
318inline float
320{
321 return v.GetLength();
322}
323
327inline float
329{
330 return v->Normalize(eps);
331}
332
336inline GfVec2f
338{
339 return v.GetNormalized(eps);
340}
341
346inline GfVec2f
347GfGetProjection(GfVec2f const &a, GfVec2f const &b)
348{
349 return a.GetProjection(b);
350}
351
356inline GfVec2f
357GfGetComplement(GfVec2f const &a, GfVec2f const &b)
358{
359 return a.GetComplement(b);
360}
361
364inline bool
365GfIsClose(GfVec2f const &v1, GfVec2f const &v2, double tolerance)
366{
367 GfVec2f delta = v1 - v2;
368 return delta.GetLengthSq() <= tolerance * tolerance;
369}
370
371
372
373PXR_NAMESPACE_CLOSE_SCOPE
374
375#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
Default constructor does no initialization.
constexpr GfVec2f(float s0, float s1)
Initialize all elements with explicit arguments.
Definition: vec2f.h:62
friend size_t hash_value(GfVec2f const &vec)
Hash.
Definition: vec2f.h:127
GfVec2f & operator+=(GfVec2f const &other)
Addition.
Definition: vec2f.h:157
GfVec2f GetComplement(GfVec2f const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec2f.h:219
GfVec2f & operator/=(double s)
Division by scalar.
Definition: vec2f.h:191
GfVec2f & operator-=(GfVec2f const &other)
Subtraction.
Definition: vec2f.h:167
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:106
static GfVec2f Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec2f.h:98
static GfVec2f XAxis()
Create a unit vector along the X-axis.
Definition: vec2f.h:84
constexpr GfVec2f(Scl const *p)
Construct with pointer to values.
Definition: vec2f.h:69
float GetLength() const
Length.
Definition: vec2f.h:229
static GfVec2f YAxis()
Create a unit vector along the Y-axis.
Definition: vec2f.h:90
float Normalize(float eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec2f.h:241
float operator*(GfVec2f const &v) const
See GfDot().
Definition: vec2f.h:202
GfVec2f operator-() const
Create a vec with negated elements.
Definition: vec2f.h:152
float const & operator[](size_t i) const
Indexing.
Definition: vec2f.h:123
float GetLengthSq() const
Squared length.
Definition: vec2f.h:224
bool operator==(GfVec2f const &other) const
Equality comparison.
Definition: vec2f.h:132
GfVec2f & Set(float const *a)
Set all elements with a pointer to data.
Definition: vec2f.h:113
GfVec2f & operator*=(double s)
Multiplication by scalar.
Definition: vec2f.h:177
GfVec2f GetProjection(GfVec2f const &v) const
Returns the projection of this onto v.
Definition: vec2f.h:210
constexpr GfVec2f(float value)
Initialize all elements to a single value.
Definition: vec2f.h:56
GF_API bool operator==(class GfVec2d const &other) const
Equality comparison.
float const * data() const
Direct data access.
Definition: vec2f.h:118
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:475
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:319
GfVec2f GfGetProjection(GfVec2f const &a, GfVec2f const &b)
Returns the projection of a onto b.
Definition: vec2f.h:347
GfVec2f GfGetComplement(GfVec2f const &a, GfVec2f const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec2f.h:357
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:365
float GfDot(GfVec2f const &v1, GfVec2f const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2f.h:312
GfVec2f GfCompMult(GfVec2f const &v1, GfVec2f const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2f.h:294
GfVec2f GfCompDiv(GfVec2f const &v1, GfVec2f const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2f.h:303
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:337
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:328