Loading...
Searching...
No Matches
vec4h.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_VEC4H_H
12#define PXR_BASE_GF_VEC4H_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 GfVec4h;
34
35template <>
36struct GfIsGfVec<class GfVec4h> { static const bool value = true; };
37
47{
48public:
51 static const size_t dimension = 4;
52
55 GfVec4h() = default;
56
58 constexpr explicit GfVec4h(GfHalf value)
59 : _data{ value, value, value, value }
60 {
61 }
62
64 constexpr GfVec4h(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
65 : _data{ s0, s1, s2, s3 }
66 {
67 }
68
70 template <class Scl>
71 constexpr explicit GfVec4h(Scl const *p)
72 : _data{ p[0], p[1], p[2], p[3] }
73 {
74 }
75
77 explicit GfVec4h(class GfVec4d const &other);
78
80 explicit GfVec4h(class GfVec4f const &other);
81
83 GfVec4h(class GfVec4i const &other);
84
86 static GfVec4h XAxis() {
87 GfVec4h result(0);
88 result[0] = 1;
89 return result;
90 }
92 static GfVec4h YAxis() {
93 GfVec4h result(0);
94 result[1] = 1;
95 return result;
96 }
98 static GfVec4h ZAxis() {
99 GfVec4h result(0);
100 result[2] = 1;
101 return result;
102 }
104 static GfVec4h WAxis() {
105 GfVec4h result(0);
106 result[3] = 1;
107 return result;
108 }
109
112 static GfVec4h Axis(size_t i) {
113 GfVec4h result(0);
114 if (i < 4)
115 result[i] = 1;
116 return result;
117 }
118
121 _data[0] = s0;
122 _data[1] = s1;
123 _data[2] = s2;
124 _data[3] = s3;
125 return *this;
126 }
127
129 GfVec4h &Set(GfHalf const *a) {
130 return Set(a[0], a[1], a[2], a[3]);
131 }
132
134 GfHalf const *data() const { return _data; }
135 GfHalf *data() { return _data; }
136 GfHalf const *GetArray() const { return data(); }
137
139 GfHalf const &operator[](size_t i) const { return _data[i]; }
140 GfHalf &operator[](size_t i) { return _data[i]; }
141
143 friend inline size_t hash_value(GfVec4h const &vec) {
144 return TfHash::Combine(vec[0], vec[1], vec[2], vec[3]);
145 }
146
148 bool operator==(GfVec4h const &other) const {
149 return _data[0] == other[0] &&
150 _data[1] == other[1] &&
151 _data[2] == other[2] &&
152 _data[3] == other[3];
153 }
154 bool operator!=(GfVec4h const &other) const {
155 return !(*this == other);
156 }
157
158 // TODO Add inequality for other vec types...
160 GF_API
161 bool operator==(class GfVec4d const &other) const;
163 GF_API
164 bool operator==(class GfVec4f const &other) const;
166 GF_API
167 bool operator==(class GfVec4i const &other) const;
168
171 return GfVec4h(-_data[0], -_data[1], -_data[2], -_data[3]);
172 }
173
175 GfVec4h &operator+=(GfVec4h const &other) {
176 _data[0] += other[0];
177 _data[1] += other[1];
178 _data[2] += other[2];
179 _data[3] += other[3];
180 return *this;
181 }
182 friend GfVec4h operator+(GfVec4h const &l, GfVec4h const &r) {
183 return GfVec4h(l) += r;
184 }
185
187 GfVec4h &operator-=(GfVec4h const &other) {
188 _data[0] -= other[0];
189 _data[1] -= other[1];
190 _data[2] -= other[2];
191 _data[3] -= other[3];
192 return *this;
193 }
194 friend GfVec4h operator-(GfVec4h const &l, GfVec4h const &r) {
195 return GfVec4h(l) -= r;
196 }
197
199 GfVec4h &operator*=(double s) {
200 _data[0] *= s;
201 _data[1] *= s;
202 _data[2] *= s;
203 _data[3] *= s;
204 return *this;
205 }
206 GfVec4h operator*(double s) const {
207 return GfVec4h(*this) *= s;
208 }
209 friend GfVec4h operator*(double s, GfVec4h const &v) {
210 return v * s;
211 }
212
214 // TODO should divide by the scalar type.
215 GfVec4h &operator/=(double s) {
216 // TODO This should not multiply by 1/s, it should do the division.
217 // Doing the division is more numerically stable when s is close to
218 // zero.
219 return *this *= (1.0 / s);
220 }
221 GfVec4h operator/(double s) const {
222 return *this * (1.0 / s);
223 }
224
226 GfHalf operator*(GfVec4h const &v) const {
227 return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
228 }
229
234 GfVec4h GetProjection(GfVec4h const &v) const {
235 return v * (*this * v);
236 }
237
243 GfVec4h GetComplement(GfVec4h const &b) const {
244 return *this - this->GetProjection(b);
245 }
246
249 return *this * *this;
250 }
251
254 return GfSqrt(GetLengthSq());
255 }
256
265 GfHalf Normalize(GfHalf eps = 0.001) {
266 // TODO this seems suspect... suggest dividing by length so long as
267 // length is not zero.
268 GfHalf length = GetLength();
269 *this /= (length > eps) ? length : eps;
270 return length;
271 }
272
273 GfVec4h GetNormalized(GfHalf eps = 0.001) const {
274 GfVec4h normalized(*this);
275 normalized.Normalize(eps);
276 return normalized;
277 }
278
279
280private:
281 GfHalf _data[4];
282};
283
286GF_API std::ostream& operator<<(std::ostream &, GfVec4h const &);
287
288
289PXR_NAMESPACE_CLOSE_SCOPE
290
291#include "pxr/base/gf/vec4d.h"
292#include "pxr/base/gf/vec4f.h"
293#include "pxr/base/gf/vec4i.h"
294
295PXR_NAMESPACE_OPEN_SCOPE
296
297inline
298GfVec4h::GfVec4h(class GfVec4d const &other)
299{
300 _data[0] = other[0];
301 _data[1] = other[1];
302 _data[2] = other[2];
303 _data[3] = other[3];
304}
305inline
306GfVec4h::GfVec4h(class GfVec4f const &other)
307{
308 _data[0] = other[0];
309 _data[1] = other[1];
310 _data[2] = other[2];
311 _data[3] = other[3];
312}
313inline
314GfVec4h::GfVec4h(class GfVec4i const &other)
315{
316 _data[0] = other[0];
317 _data[1] = other[1];
318 _data[2] = other[2];
319 _data[3] = other[3];
320}
321
323inline GfVec4h
324GfCompMult(GfVec4h const &v1, GfVec4h const &v2) {
325 return GfVec4h(
326 v1[0] * v2[0],
327 v1[1] * v2[1],
328 v1[2] * v2[2],
329 v1[3] * v2[3]
330 );
331}
332
334inline GfVec4h
335GfCompDiv(GfVec4h const &v1, GfVec4h const &v2) {
336 return GfVec4h(
337 v1[0] / v2[0],
338 v1[1] / v2[1],
339 v1[2] / v2[2],
340 v1[3] / v2[3]
341 );
342}
343
345inline GfHalf
346GfDot(GfVec4h const &v1, GfVec4h const &v2) {
347 return v1 * v2;
348}
349
350
352inline GfHalf
354{
355 return v.GetLength();
356}
357
361inline GfHalf
362GfNormalize(GfVec4h *v, GfHalf eps = 0.001)
363{
364 return v->Normalize(eps);
365}
366
370inline GfVec4h
371GfGetNormalized(GfVec4h const &v, GfHalf eps = 0.001)
372{
373 return v.GetNormalized(eps);
374}
375
380inline GfVec4h
381GfGetProjection(GfVec4h const &a, GfVec4h const &b)
382{
383 return a.GetProjection(b);
384}
385
390inline GfVec4h
391GfGetComplement(GfVec4h const &a, GfVec4h const &b)
392{
393 return a.GetComplement(b);
394}
395
398inline bool
399GfIsClose(GfVec4h const &v1, GfVec4h const &v2, double tolerance)
400{
401 GfVec4h delta = v1 - v2;
402 return delta.GetLengthSq() <= tolerance * tolerance;
403}
404
405
406
407PXR_NAMESPACE_CLOSE_SCOPE
408
409#endif // PXR_BASE_GF_VEC4H_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
Basic type for a vector of 4 double components.
Definition: vec4d.h:46
Basic type for a vector of 4 float components.
Definition: vec4f.h:46
Basic type for a vector of 4 GfHalf components.
Definition: vec4h.h:47
GfVec4h GetComplement(GfVec4h const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec4h.h:243
static GfVec4h ZAxis()
Create a unit vector along the Z-axis.
Definition: vec4h.h:98
GfVec4h & operator-=(GfVec4h const &other)
Subtraction.
Definition: vec4h.h:187
static GfVec4h YAxis()
Create a unit vector along the Y-axis.
Definition: vec4h.h:92
static GfVec4h WAxis()
Create a unit vector along the W-axis.
Definition: vec4h.h:104
GfHalf GetLength() const
Length.
Definition: vec4h.h:253
GfHalf operator*(GfVec4h const &v) const
See GfDot().
Definition: vec4h.h:226
constexpr GfVec4h(Scl const *p)
Construct with pointer to values.
Definition: vec4h.h:71
GfVec4h operator-() const
Create a vec with negated elements.
Definition: vec4h.h:170
GfHalf ScalarType
Scalar element type and dimension.
Definition: vec4h.h:50
GfVec4h & Set(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
Set all elements with passed arguments.
Definition: vec4h.h:120
constexpr GfVec4h(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
Initialize all elements with explicit arguments.
Definition: vec4h.h:64
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec4h.h:265
GfVec4h()=default
GfVec4h value-initializes to zero and performs no default initialization, like float or double.
friend size_t hash_value(GfVec4h const &vec)
Hash.
Definition: vec4h.h:143
GfVec4h & operator/=(double s)
Division by scalar.
Definition: vec4h.h:215
GfHalf GetLengthSq() const
Squared length.
Definition: vec4h.h:248
GF_API bool operator==(class GfVec4f const &other) const
Equality comparison.
GfVec4h & Set(GfHalf const *a)
Set all elements with a pointer to data.
Definition: vec4h.h:129
GF_API bool operator==(class GfVec4i const &other) const
Equality comparison.
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec4h.h:139
GfHalf const * data() const
Direct data access.
Definition: vec4h.h:134
GF_API bool operator==(class GfVec4d const &other) const
Equality comparison.
GfVec4h & operator*=(double s)
Multiplication by scalar.
Definition: vec4h.h:199
static GfVec4h XAxis()
Create a unit vector along the X-axis.
Definition: vec4h.h:86
GfVec4h GetProjection(GfVec4h const &v) const
Returns the projection of this onto v.
Definition: vec4h.h:234
GfVec4h & operator+=(GfVec4h const &other)
Addition.
Definition: vec4h.h:175
bool operator==(GfVec4h const &other) const
Equality comparison.
Definition: vec4h.h:148
static GfVec4h Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec4h.h:112
constexpr GfVec4h(GfHalf value)
Initialize all elements to a single value.
Definition: vec4h.h:58
Basic type for a vector of 4 int components.
Definition: vec4i.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
GfVec4h GfGetComplement(GfVec4h const &a, GfVec4h const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec4h.h:391
bool GfIsClose(GfVec4h const &v1, GfVec4h const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition: vec4h.h:399
GfHalf GfNormalize(GfVec4h *v, GfHalf eps=0.001)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec4h.h:362
GfVec4h GfCompDiv(GfVec4h const &v1, GfVec4h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec4h.h:335
GfHalf GfDot(GfVec4h const &v1, GfVec4h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec4h.h:346
GfVec4h GfGetNormalized(GfVec4h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec4h.h:371
GfVec4h GfCompMult(GfVec4h const &v1, GfVec4h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec4h.h:324
GfVec4h GfGetProjection(GfVec4h const &a, GfVec4h const &b)
Returns the projection of a onto b.
Definition: vec4h.h:381
GfHalf GfGetLength(GfVec4h const &v)
Returns the geometric length of v.
Definition: vec4h.h:353