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"
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
55 GfVec2h() = default;
56
58 constexpr explicit GfVec2h(GfHalf value)
59 : _data{ value, value }
60 {
61 }
62
64 constexpr GfVec2h(GfHalf s0, GfHalf s1)
65 : _data{ s0, s1 }
66 {
67 }
68
70 template <class Scl>
71 constexpr explicit GfVec2h(Scl const *p)
72 : _data{ p[0], p[1] }
73 {
74 }
75
77 explicit GfVec2h(class GfVec2d const &other);
78
80 explicit GfVec2h(class GfVec2f const &other);
81
83 GfVec2h(class GfVec2i const &other);
84
86 static GfVec2h XAxis() {
87 GfVec2h result(0);
88 result[0] = 1;
89 return result;
90 }
92 static GfVec2h YAxis() {
93 GfVec2h result(0);
94 result[1] = 1;
95 return result;
96 }
97
100 static GfVec2h Axis(size_t i) {
101 GfVec2h result(0);
102 if (i < 2)
103 result[i] = 1;
104 return result;
105 }
106
109 _data[0] = s0;
110 _data[1] = s1;
111 return *this;
112 }
113
115 GfVec2h &Set(GfHalf const *a) {
116 return Set(a[0], a[1]);
117 }
118
120 GfHalf const *data() const { return _data; }
121 GfHalf *data() { return _data; }
122 GfHalf const *GetArray() const { return data(); }
123
125 GfHalf const &operator[](size_t i) const { return _data[i]; }
126 GfHalf &operator[](size_t i) { return _data[i]; }
127
129 friend inline size_t hash_value(GfVec2h const &vec) {
130 return TfHash::Combine(vec[0], vec[1]);
131 }
132
134 bool operator==(GfVec2h const &other) const {
135 return _data[0] == other[0] &&
136 _data[1] == other[1];
137 }
138 bool operator!=(GfVec2h const &other) const {
139 return !(*this == other);
140 }
141
142 // TODO Add inequality for other vec types...
144 GF_API
145 bool operator==(class GfVec2d const &other) const;
147 GF_API
148 bool operator==(class GfVec2f const &other) const;
150 GF_API
151 bool operator==(class GfVec2i const &other) const;
152
155 return GfVec2h(-_data[0], -_data[1]);
156 }
157
159 GfVec2h &operator+=(GfVec2h const &other) {
160 _data[0] += other[0];
161 _data[1] += other[1];
162 return *this;
163 }
164 friend GfVec2h operator+(GfVec2h const &l, GfVec2h const &r) {
165 return GfVec2h(l) += r;
166 }
167
169 GfVec2h &operator-=(GfVec2h const &other) {
170 _data[0] -= other[0];
171 _data[1] -= other[1];
172 return *this;
173 }
174 friend GfVec2h operator-(GfVec2h const &l, GfVec2h const &r) {
175 return GfVec2h(l) -= r;
176 }
177
179 GfVec2h &operator*=(double s) {
180 _data[0] *= s;
181 _data[1] *= s;
182 return *this;
183 }
184 GfVec2h operator*(double s) const {
185 return GfVec2h(*this) *= s;
186 }
187 friend GfVec2h operator*(double s, GfVec2h const &v) {
188 return v * s;
189 }
190
192 // TODO should divide by the scalar type.
193 GfVec2h &operator/=(double s) {
194 // TODO This should not multiply by 1/s, it should do the division.
195 // Doing the division is more numerically stable when s is close to
196 // zero.
197 return *this *= (1.0 / s);
198 }
199 GfVec2h operator/(double s) const {
200 return *this * (1.0 / s);
201 }
202
204 GfHalf operator*(GfVec2h const &v) const {
205 return _data[0] * v[0] + _data[1] * v[1];
206 }
207
212 GfVec2h GetProjection(GfVec2h const &v) const {
213 return v * (*this * v);
214 }
215
221 GfVec2h GetComplement(GfVec2h const &b) const {
222 return *this - this->GetProjection(b);
223 }
224
227 return *this * *this;
228 }
229
232 return GfSqrt(GetLengthSq());
233 }
234
243 GfHalf Normalize(GfHalf eps = 0.001) {
244 // TODO this seems suspect... suggest dividing by length so long as
245 // length is not zero.
246 GfHalf length = GetLength();
247 *this /= (length > eps) ? length : eps;
248 return length;
249 }
250
251 GfVec2h GetNormalized(GfHalf eps = 0.001) const {
252 GfVec2h normalized(*this);
253 normalized.Normalize(eps);
254 return normalized;
255 }
256
257
258private:
259 GfHalf _data[2];
260};
261
264GF_API std::ostream& operator<<(std::ostream &, GfVec2h const &);
265
266
267PXR_NAMESPACE_CLOSE_SCOPE
268
269#include "pxr/base/gf/vec2d.h"
270#include "pxr/base/gf/vec2f.h"
271#include "pxr/base/gf/vec2i.h"
272
273PXR_NAMESPACE_OPEN_SCOPE
274
275inline
276GfVec2h::GfVec2h(class GfVec2d const &other)
277{
278 _data[0] = other[0];
279 _data[1] = other[1];
280}
281inline
282GfVec2h::GfVec2h(class GfVec2f const &other)
283{
284 _data[0] = other[0];
285 _data[1] = other[1];
286}
287inline
288GfVec2h::GfVec2h(class GfVec2i const &other)
289{
290 _data[0] = other[0];
291 _data[1] = other[1];
292}
293
295inline GfVec2h
296GfCompMult(GfVec2h const &v1, GfVec2h const &v2) {
297 return GfVec2h(
298 v1[0] * v2[0],
299 v1[1] * v2[1]
300 );
301}
302
304inline GfVec2h
305GfCompDiv(GfVec2h const &v1, GfVec2h const &v2) {
306 return GfVec2h(
307 v1[0] / v2[0],
308 v1[1] / v2[1]
309 );
310}
311
313inline GfHalf
314GfDot(GfVec2h const &v1, GfVec2h const &v2) {
315 return v1 * v2;
316}
317
318
320inline GfHalf
322{
323 return v.GetLength();
324}
325
329inline GfHalf
330GfNormalize(GfVec2h *v, GfHalf eps = 0.001)
331{
332 return v->Normalize(eps);
333}
334
338inline GfVec2h
339GfGetNormalized(GfVec2h const &v, GfHalf eps = 0.001)
340{
341 return v.GetNormalized(eps);
342}
343
348inline GfVec2h
349GfGetProjection(GfVec2h const &a, GfVec2h const &b)
350{
351 return a.GetProjection(b);
352}
353
358inline GfVec2h
359GfGetComplement(GfVec2h const &a, GfVec2h const &b)
360{
361 return a.GetComplement(b);
362}
363
366inline bool
367GfIsClose(GfVec2h const &v1, GfVec2h const &v2, double tolerance)
368{
369 GfVec2h delta = v1 - v2;
370 return delta.GetLengthSq() <= tolerance * tolerance;
371}
372
373
374
375PXR_NAMESPACE_CLOSE_SCOPE
376
377#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:100
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:71
GfHalf GetLength() const
Length.
Definition: vec2h.h:231
GfVec2h operator-() const
Create a vec with negated elements.
Definition: vec2h.h:154
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:115
GfVec2h & operator-=(GfVec2h const &other)
Subtraction.
Definition: vec2h.h:169
constexpr GfVec2h(GfHalf s0, GfHalf s1)
Initialize all elements with explicit arguments.
Definition: vec2h.h:64
GfVec2h & operator/=(double s)
Division by scalar.
Definition: vec2h.h:193
GfHalf operator*(GfVec2h const &v) const
See GfDot().
Definition: vec2h.h:204
bool operator==(GfVec2h const &other) const
Equality comparison.
Definition: vec2h.h:134
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec2h.h:243
GfVec2h & operator*=(double s)
Multiplication by scalar.
Definition: vec2h.h:179
GfHalf GetLengthSq() const
Squared length.
Definition: vec2h.h:226
GF_API bool operator==(class GfVec2f const &other) const
Equality comparison.
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec2h.h:125
GF_API bool operator==(class GfVec2d const &other) const
Equality comparison.
GfHalf const * data() const
Direct data access.
Definition: vec2h.h:120
static GfVec2h YAxis()
Create a unit vector along the Y-axis.
Definition: vec2h.h:92
GfVec2h & operator+=(GfVec2h const &other)
Addition.
Definition: vec2h.h:159
friend size_t hash_value(GfVec2h const &vec)
Hash.
Definition: vec2h.h:129
GfVec2h GetComplement(GfVec2h const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec2h.h:221
static GfVec2h XAxis()
Create a unit vector along the X-axis.
Definition: vec2h.h:86
constexpr GfVec2h(GfHalf value)
Initialize all elements to a single value.
Definition: vec2h.h:58
GfVec2h GetProjection(GfVec2h const &v) const
Returns the projection of this onto v.
Definition: vec2h.h:212
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:108
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
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:26
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:330
GfVec2h GfGetProjection(GfVec2h const &a, GfVec2h const &b)
Returns the projection of a onto b.
Definition: vec2h.h:349
GfHalf GfGetLength(GfVec2h const &v)
Returns the geometric length of v.
Definition: vec2h.h:321
GfHalf GfDot(GfVec2h const &v1, GfVec2h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2h.h:314
GfVec2h GfCompMult(GfVec2h const &v1, GfVec2h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2h.h:296
GfVec2h GfCompDiv(GfVec2h const &v1, GfVec2h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2h.h:305
GfVec2h GfGetNormalized(GfVec2h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec2h.h:339
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:367
GfVec2h GfGetComplement(GfVec2h const &a, GfVec2h const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec2h.h:359