Loading...
Searching...
No Matches
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"
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"
22#include "pxr/base/gf/half.h"
24#include "pxr/base/tf/hash.h"
25
26#include <cstddef>
27#include <type_traits>
28#include <cmath>
29
30#include <iosfwd>
31
32PXR_NAMESPACE_OPEN_SCOPE
33
34
35template <>
36struct GfIsGfVec<class GfVec2h> { static const bool value = true; };
37
47{
48public:
51 static const size_t dimension = 2;
52
64 GfHalf x, y;
66
69 GfVec2h() = default;
70
72 constexpr explicit GfVec2h(GfHalf value)
73 : x(value), y(value)
74 {
75 }
76
78 constexpr GfVec2h(GfHalf s0, GfHalf s1)
79 : x(s0), y(s1)
80 {
81 }
82
84 template <class Scl>
85 constexpr explicit GfVec2h(Scl const *p)
86 : x(p[0]), y(p[1])
87 {
88 }
89
91 explicit GfVec2h(class GfVec2d const &other);
92
94 explicit GfVec2h(class GfVec2f const &other);
95
97 GfVec2h(class GfVec2i const &other);
99 static GfVec2h XAxis() {
100 GfVec2h result(0);
101 result[0] = 1;
102 return result;
103 }
104
106 static GfVec2h YAxis() {
107 GfVec2h result(0);
108 result[1] = 1;
109 return result;
110 }
111
114 static GfVec2h Axis(size_t i) {
115 GfVec2h result(0);
116 if (i < 2)
117 result[i] = 1;
118 return result;
119 }
120
123 x = s0;
124 y = s1;
125 return *this;
126 }
127
129 GfVec2h &Set(GfHalf const *a) {
130 return Set(a[0], a[1]);
131 }
132
138 GfHalf const *data() const { return &x; }
139 GfHalf *data() { return &x; }
140 GfHalf const *GetArray() const { return data(); }
141
143 GfHalf const &operator[](size_t i) const { return data()[i]; }
144 GfHalf &operator[](size_t i) { return data()[i]; }
145
147 friend inline size_t hash_value(GfVec2h const &vec) {
148 return TfHash::Combine(vec.x, vec.y);
149 }
150
152 bool operator==(GfVec2h const &other) const {
153 return x == other.x &&
154 y == other.y;
155 }
156 bool operator!=(GfVec2h const &other) const {
157 return !(*this == other);
158 }
159
160 // TODO Add inequality for other vec types...
162 GF_API
163 bool operator==(class GfVec2d const &other) const;
165 GF_API
166 bool operator==(class GfVec2f const &other) const;
168 GF_API
169 bool operator==(class GfVec2i const &other) const;
170
173 return GfVec2h(-x, -y);
174 }
175
177 GfVec2h &operator+=(GfVec2h const &other) {
178 x += other.x;
179 y += other.y;
180 return *this;
181 }
182 friend GfVec2h operator+(GfVec2h const &l, GfVec2h const &r) {
183 return GfVec2h(l) += r;
184 }
185
187 GfVec2h &operator-=(GfVec2h const &other) {
188 x -= other.x;
189 y -= other.y;
190 return *this;
191 }
192 friend GfVec2h operator-(GfVec2h const &l, GfVec2h const &r) {
193 return GfVec2h(l) -= r;
194 }
195
197 GfVec2h &operator*=(double s) {
198 x *= s;
199 y *= s;
200 return *this;
201 }
202 GfVec2h operator*(double s) const {
203 return GfVec2h(*this) *= s;
204 }
205 friend GfVec2h operator*(double s, GfVec2h const &v) {
206 return v * s;
207 }
208
210 // TODO should divide by the scalar type.
211 GfVec2h &operator/=(double s) {
212 // TODO This should not multiply by 1/s, it should do the division.
213 // Doing the division is more numerically stable when s is close to
214 // zero.
215 return *this *= (1.0 / s);
216 }
217 GfVec2h operator/(double s) const {
218 return *this * (1.0 / s);
219 }
220
222 GfHalf operator*(GfVec2h const &v) const {
223 return x * v.x + y * v.y;
224 }
225
230 GfVec2h GetProjection(GfVec2h const &v) const {
231 return v * (*this * v);
232 }
233
239 GfVec2h GetComplement(GfVec2h const &b) const {
240 return *this - this->GetProjection(b);
241 }
242
245 return *this * *this;
246 }
247
250 return GfSqrt(GetLengthSq());
251 }
252
261 GfHalf Normalize(GfHalf eps = 0.001) {
262 // TODO this seems suspect... suggest dividing by length so long as
263 // length is not zero.
264 GfHalf length = GetLength();
265 *this /= (length > eps) ? length : eps;
266 return length;
267 }
268
269 GfVec2h GetNormalized(GfHalf eps = 0.001) const {
270 GfVec2h normalized(*this);
271 normalized.Normalize(eps);
272 return normalized;
273 }
274
275
291#if defined (doxygen)
292 constexpr GfVec2h xy() const;
293 constexpr GfVec2h yx() const;
295#else // !doxygen
296 // Note that these are templated on their own return type so that the
297 // declarations cost nothing in compile time & debug info unless they are
298 // actually called in a TU. Naming a different type is an error and
299 // static_assert()ed against.
300 template <class Vec = GfVec2h> constexpr Vec xy() const;
301 template <class Vec = GfVec2h> constexpr Vec yx() const;
302#endif // doxygen
303 };
304
307GF_API std::ostream& operator<<(std::ostream &, GfVec2h const &);
308
309PXR_NAMESPACE_CLOSE_SCOPE
310
311// Other scalar types of the same dimension, for the conversions above.
312#include "pxr/base/gf/vec2d.h"
313#include "pxr/base/gf/vec2f.h"
314#include "pxr/base/gf/vec2i.h"
315
316PXR_NAMESPACE_OPEN_SCOPE
317
318inline
319GfVec2h::GfVec2h(class GfVec2d const &other)
320{
321 x = other.x;
322 y = other.y;
323}
324inline
325GfVec2h::GfVec2h(class GfVec2f const &other)
326{
327 x = other.x;
328 y = other.y;
329}
330inline
331GfVec2h::GfVec2h(class GfVec2i const &other)
332{
333 x = other.x;
334 y = other.y;
335}
336
337template <class Vec>
338constexpr Vec
339GfVec2h::xy() const
340{
341 static_assert(std::is_same<Vec, GfVec2h>::value);
342 return Vec(x, y);
343}
344
345template <class Vec>
346constexpr Vec
347GfVec2h::yx() const
348{
349 static_assert(std::is_same<Vec, GfVec2h>::value);
350 return Vec(y, x);
351}
352
353
355inline GfVec2h
356GfCompMult(GfVec2h const &v1, GfVec2h const &v2) {
357 return GfVec2h(
358 v1.x * v2.x,
359 v1.y * v2.y
360 );
361}
362
364inline GfVec2h
365GfCompDiv(GfVec2h const &v1, GfVec2h const &v2) {
366 return GfVec2h(
367 v1.x / v2.x,
368 v1.y / v2.y
369 );
370}
371
373inline GfHalf
374GfDot(GfVec2h const &v1, GfVec2h const &v2) {
375 return v1 * v2;
376}
377
378
380inline GfHalf
382{
383 return v.GetLength();
384}
385
389inline GfHalf
390GfNormalize(GfVec2h *v, GfHalf eps = 0.001)
391{
392 return v->Normalize(eps);
393}
394
398inline GfVec2h
399GfGetNormalized(GfVec2h const &v, GfHalf eps = 0.001)
400{
401 return v.GetNormalized(eps);
402}
403
408inline GfVec2h
409GfGetProjection(GfVec2h const &a, GfVec2h const &b)
410{
411 return a.GetProjection(b);
412}
413
418inline GfVec2h
419GfGetComplement(GfVec2h const &a, GfVec2h const &b)
420{
421 return a.GetComplement(b);
422}
423
426inline bool
427GfIsClose(GfVec2h const &v1, GfVec2h const &v2, double tolerance)
428{
429 GfVec2h delta = v1 - v2;
430 return delta.GetLengthSq() <= tolerance * tolerance;
431}
432
433
434
435PXR_NAMESPACE_CLOSE_SCOPE
436
437#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:114
GfHalf ScalarType
Scalar element type and dimension.
Definition vec2h.h:50
GfVec2h()=default
GfVec2h value-initializes to zero and performs no default initialization, like float or double.
constexpr GfVec2h(Scl const *p)
Construct with pointer to values.
Definition vec2h.h:85
GfHalf GetLength() const
Length.
Definition vec2h.h:249
GfVec2h operator-() const
Create a vec with negated elements.
Definition vec2h.h:172
GfVec2h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition vec2h.h:129
GfVec2h & operator-=(GfVec2h const &other)
Subtraction.
Definition vec2h.h:187
constexpr GfVec2h(GfHalf s0, GfHalf s1)
Initialize all elements with explicit arguments.
Definition vec2h.h:78
GfVec2h & operator/=(double s)
Division by scalar.
Definition vec2h.h:211
GfHalf operator*(GfVec2h const &v) const
See GfDot().
Definition vec2h.h:222
bool operator==(GfVec2h const &other) const
Equality comparison.
Definition vec2h.h:152
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition vec2h.h:261
GfVec2h & operator*=(double s)
Multiplication by scalar.
Definition vec2h.h:197
GfHalf GetLengthSq() const
Squared length.
Definition vec2h.h:244
GF_API bool operator==(class GfVec2f const &other) const
Equality comparison.
GfHalf const & operator[](size_t i) const
Indexing.
Definition vec2h.h:143
GF_API bool operator==(class GfVec2d const &other) const
Equality comparison.
GfHalf const * data() const
Direct data access.
Definition vec2h.h:138
static GfVec2h YAxis()
Create a unit vector along the Y-axis.
Definition vec2h.h:106
GfVec2h & operator+=(GfVec2h const &other)
Addition.
Definition vec2h.h:177
friend size_t hash_value(GfVec2h const &vec)
Hash.
Definition vec2h.h:147
GfVec2h GetComplement(GfVec2h const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition vec2h.h:239
static GfVec2h XAxis()
Create a unit vector along the X-axis.
Definition vec2h.h:99
constexpr GfVec2h(GfHalf value)
Initialize all elements to a single value.
Definition vec2h.h:72
GfVec2h GetProjection(GfVec2h const &v) const
Returns the projection of this onto v.
Definition vec2h.h:230
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:122
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
GF_API std::ostream & operator<<(std::ostream &, GfVec2h const &)
Output a GfVec2h.
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:26
Defines useful mathematical limits.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition traits.h:23
GfHalf GfNormalize(GfVec2h *v, GfHalf eps=0.001)
Normalizes *v in place to unit length, returning the length before normalization.
Definition vec2h.h:390
GfVec2h GfGetProjection(GfVec2h const &a, GfVec2h const &b)
Returns the projection of a onto b.
Definition vec2h.h:409
GfHalf GfGetLength(GfVec2h const &v)
Returns the geometric length of v.
Definition vec2h.h:381
GfHalf GfDot(GfVec2h const &v1, GfVec2h const &v2)
Returns the dot (inner) product of two vectors.
Definition vec2h.h:374
GfVec2h GfCompMult(GfVec2h const &v1, GfVec2h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition vec2h.h:356
GfVec2h GfCompDiv(GfVec2h const &v1, GfVec2h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition vec2h.h:365
GfVec2h GfGetNormalized(GfVec2h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition vec2h.h:399
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:427
GfVec2h GfGetComplement(GfVec2h const &a, GfVec2h const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition vec2h.h:419