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