Loading...
Searching...
No Matches
vec2h.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
25// This file is generated by a script. Do not edit directly. Edit the
26// vec.template.h file to make changes.
27
28#ifndef PXR_BASE_GF_VEC2H_H
29#define PXR_BASE_GF_VEC2H_H
30
33
34#include "pxr/pxr.h"
36#include "pxr/base/gf/api.h"
37#include "pxr/base/gf/limits.h"
38#include "pxr/base/gf/traits.h"
39#include "pxr/base/gf/math.h"
40#include "pxr/base/gf/half.h"
41#include "pxr/base/tf/hash.h"
42
43#include <cstddef>
44#include <cmath>
45
46#include <iosfwd>
47
48PXR_NAMESPACE_OPEN_SCOPE
49
50class GfVec2h;
51
52template <>
53struct GfIsGfVec<class GfVec2h> { static const bool value = true; };
54
64{
65public:
68 static const size_t dimension = 2;
69
71 GfVec2h() = default;
72
74 constexpr explicit GfVec2h(GfHalf value)
75 : _data{ value, value }
76 {
77 }
78
80 constexpr GfVec2h(GfHalf s0, GfHalf s1)
81 : _data{ s0, s1 }
82 {
83 }
84
86 template <class Scl>
87 constexpr explicit GfVec2h(Scl const *p)
88 : _data{ p[0], p[1] }
89 {
90 }
91
93 explicit GfVec2h(class GfVec2d const &other);
94
96 explicit GfVec2h(class GfVec2f const &other);
97
99 GfVec2h(class GfVec2i const &other);
100
102 static GfVec2h XAxis() {
103 GfVec2h result(0);
104 result[0] = 1;
105 return result;
106 }
108 static GfVec2h YAxis() {
109 GfVec2h result(0);
110 result[1] = 1;
111 return result;
112 }
113
116 static GfVec2h Axis(size_t i) {
117 GfVec2h result(0);
118 if (i < 2)
119 result[i] = 1;
120 return result;
121 }
122
125 _data[0] = s0;
126 _data[1] = s1;
127 return *this;
128 }
129
131 GfVec2h &Set(GfHalf const *a) {
132 return Set(a[0], a[1]);
133 }
134
136 GfHalf const *data() const { return _data; }
137 GfHalf *data() { return _data; }
138 GfHalf const *GetArray() const { return data(); }
139
141 GfHalf const &operator[](size_t i) const { return _data[i]; }
142 GfHalf &operator[](size_t i) { return _data[i]; }
143
145 friend inline size_t hash_value(GfVec2h const &vec) {
146 return TfHash::Combine(vec[0], vec[1]);
147 }
148
150 bool operator==(GfVec2h const &other) const {
151 return _data[0] == other[0] &&
152 _data[1] == other[1];
153 }
154 bool operator!=(GfVec2h const &other) const {
155 return !(*this == other);
156 }
157
158 // TODO Add inequality for other vec types...
160 GF_API
161 bool operator==(class GfVec2d const &other) const;
163 GF_API
164 bool operator==(class GfVec2f const &other) const;
166 GF_API
167 bool operator==(class GfVec2i const &other) const;
168
171 return GfVec2h(-_data[0], -_data[1]);
172 }
173
175 GfVec2h &operator+=(GfVec2h const &other) {
176 _data[0] += other[0];
177 _data[1] += other[1];
178 return *this;
179 }
180 friend GfVec2h operator+(GfVec2h const &l, GfVec2h const &r) {
181 return GfVec2h(l) += r;
182 }
183
185 GfVec2h &operator-=(GfVec2h const &other) {
186 _data[0] -= other[0];
187 _data[1] -= other[1];
188 return *this;
189 }
190 friend GfVec2h operator-(GfVec2h const &l, GfVec2h const &r) {
191 return GfVec2h(l) -= r;
192 }
193
195 GfVec2h &operator*=(double s) {
196 _data[0] *= s;
197 _data[1] *= s;
198 return *this;
199 }
200 GfVec2h operator*(double s) const {
201 return GfVec2h(*this) *= s;
202 }
203 friend GfVec2h operator*(double s, GfVec2h const &v) {
204 return v * s;
205 }
206
208 // TODO should divide by the scalar type.
209 GfVec2h &operator/=(double s) {
210 // TODO This should not multiply by 1/s, it should do the division.
211 // Doing the division is more numerically stable when s is close to
212 // zero.
213 return *this *= (1.0 / s);
214 }
215 GfVec2h operator/(double s) const {
216 return *this * (1.0 / s);
217 }
218
220 GfHalf operator*(GfVec2h const &v) const {
221 return _data[0] * v[0] + _data[1] * v[1];
222 }
223
228 GfVec2h GetProjection(GfVec2h const &v) const {
229 return v * (*this * v);
230 }
231
237 GfVec2h GetComplement(GfVec2h const &b) const {
238 return *this - this->GetProjection(b);
239 }
240
243 return *this * *this;
244 }
245
248 return GfSqrt(GetLengthSq());
249 }
250
259 GfHalf Normalize(GfHalf eps = 0.001) {
260 // TODO this seems suspect... suggest dividing by length so long as
261 // length is not zero.
262 GfHalf length = GetLength();
263 *this /= (length > eps) ? length : eps;
264 return length;
265 }
266
267 GfVec2h GetNormalized(GfHalf eps = 0.001) const {
268 GfVec2h normalized(*this);
269 normalized.Normalize(eps);
270 return normalized;
271 }
272
273
274private:
275 GfHalf _data[2];
276};
277
280GF_API std::ostream& operator<<(std::ostream &, GfVec2h const &);
281
282
283PXR_NAMESPACE_CLOSE_SCOPE
284
285#include "pxr/base/gf/vec2d.h"
286#include "pxr/base/gf/vec2f.h"
287#include "pxr/base/gf/vec2i.h"
288
289PXR_NAMESPACE_OPEN_SCOPE
290
291inline
292GfVec2h::GfVec2h(class GfVec2d const &other)
293{
294 _data[0] = other[0];
295 _data[1] = other[1];
296}
297inline
298GfVec2h::GfVec2h(class GfVec2f const &other)
299{
300 _data[0] = other[0];
301 _data[1] = other[1];
302}
303inline
304GfVec2h::GfVec2h(class GfVec2i const &other)
305{
306 _data[0] = other[0];
307 _data[1] = other[1];
308}
309
311inline GfVec2h
312GfCompMult(GfVec2h const &v1, GfVec2h const &v2) {
313 return GfVec2h(
314 v1[0] * v2[0],
315 v1[1] * v2[1]
316 );
317}
318
320inline GfVec2h
321GfCompDiv(GfVec2h const &v1, GfVec2h const &v2) {
322 return GfVec2h(
323 v1[0] / v2[0],
324 v1[1] / v2[1]
325 );
326}
327
329inline GfHalf
330GfDot(GfVec2h const &v1, GfVec2h const &v2) {
331 return v1 * v2;
332}
333
334
336inline GfHalf
338{
339 return v.GetLength();
340}
341
345inline GfHalf
346GfNormalize(GfVec2h *v, GfHalf eps = 0.001)
347{
348 return v->Normalize(eps);
349}
350
354inline GfVec2h
355GfGetNormalized(GfVec2h const &v, GfHalf eps = 0.001)
356{
357 return v.GetNormalized(eps);
358}
359
364inline GfVec2h
365GfGetProjection(GfVec2h const &a, GfVec2h const &b)
366{
367 return a.GetProjection(b);
368}
369
374inline GfVec2h
375GfGetComplement(GfVec2h const &a, GfVec2h const &b)
376{
377 return a.GetComplement(b);
378}
379
382inline bool
383GfIsClose(GfVec2h const &v1, GfVec2h const &v2, double tolerance)
384{
385 GfVec2h delta = v1 - v2;
386 return delta.GetLengthSq() <= tolerance * tolerance;
387}
388
389
390
391PXR_NAMESPACE_CLOSE_SCOPE
392
393#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:63
Basic type for a vector of 2 float components.
Definition: vec2f.h:63
Basic type for a vector of 2 GfHalf components.
Definition: vec2h.h:64
static GfVec2h Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec2h.h:116
GfVec2h()=default
Default constructor does no initialization.
constexpr GfVec2h(Scl const *p)
Construct with pointer to values.
Definition: vec2h.h:87
GfHalf GetLength() const
Length.
Definition: vec2h.h:247
GfVec2h operator-() const
Create a vec with negated elements.
Definition: vec2h.h:170
GfHalf ScalarType
Scalar element type and dimension.
Definition: vec2h.h:67
GfVec2h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec2h.h:131
GfVec2h & operator-=(GfVec2h const &other)
Subtraction.
Definition: vec2h.h:185
constexpr GfVec2h(GfHalf s0, GfHalf s1)
Initialize all elements with explicit arguments.
Definition: vec2h.h:80
GfVec2h & operator/=(double s)
Division by scalar.
Definition: vec2h.h:209
GfHalf operator*(GfVec2h const &v) const
See GfDot().
Definition: vec2h.h:220
bool operator==(GfVec2h const &other) const
Equality comparison.
Definition: vec2h.h:150
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec2h.h:259
GfVec2h & operator*=(double s)
Multiplication by scalar.
Definition: vec2h.h:195
GfHalf GetLengthSq() const
Squared length.
Definition: vec2h.h:242
GF_API bool operator==(class GfVec2f const &other) const
Equality comparison.
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec2h.h:141
GF_API bool operator==(class GfVec2d const &other) const
Equality comparison.
GfHalf const * data() const
Direct data access.
Definition: vec2h.h:136
static GfVec2h YAxis()
Create a unit vector along the Y-axis.
Definition: vec2h.h:108
GfVec2h & operator+=(GfVec2h const &other)
Addition.
Definition: vec2h.h:175
friend size_t hash_value(GfVec2h const &vec)
Hash.
Definition: vec2h.h:145
GfVec2h GetComplement(GfVec2h const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec2h.h:237
static GfVec2h XAxis()
Create a unit vector along the X-axis.
Definition: vec2h.h:102
constexpr GfVec2h(GfHalf value)
Initialize all elements to a single value.
Definition: vec2h.h:74
GfVec2h GetProjection(GfVec2h const &v) const
Returns the projection of this onto v.
Definition: vec2h.h:228
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:124
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:492
Assorted mathematical utility functions.
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:80
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:41
Defines useful mathematical limits.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition: traits.h:36
GfHalf GfNormalize(GfVec2h *v, GfHalf eps=0.001)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec2h.h:346
GfVec2h GfGetProjection(GfVec2h const &a, GfVec2h const &b)
Returns the projection of a onto b.
Definition: vec2h.h:365
GfHalf GfGetLength(GfVec2h const &v)
Returns the geometric length of v.
Definition: vec2h.h:337
GfHalf GfDot(GfVec2h const &v1, GfVec2h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2h.h:330
GfVec2h GfCompMult(GfVec2h const &v1, GfVec2h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2h.h:312
GfVec2h GfCompDiv(GfVec2h const &v1, GfVec2h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2h.h:321
GfVec2h GfGetNormalized(GfVec2h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec2h.h:355
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:383
GfVec2h GfGetComplement(GfVec2h const &a, GfVec2h const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec2h.h:375