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
54 GfVec4h() = default;
55
57 constexpr explicit GfVec4h(GfHalf value)
58 : _data{ value, value, value, value }
59 {
60 }
61
63 constexpr GfVec4h(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
64 : _data{ s0, s1, s2, s3 }
65 {
66 }
67
69 template <class Scl>
70 constexpr explicit GfVec4h(Scl const *p)
71 : _data{ p[0], p[1], p[2], p[3] }
72 {
73 }
74
76 explicit GfVec4h(class GfVec4d const &other);
77
79 explicit GfVec4h(class GfVec4f const &other);
80
82 GfVec4h(class GfVec4i const &other);
83
85 static GfVec4h XAxis() {
86 GfVec4h result(0);
87 result[0] = 1;
88 return result;
89 }
91 static GfVec4h YAxis() {
92 GfVec4h result(0);
93 result[1] = 1;
94 return result;
95 }
97 static GfVec4h ZAxis() {
98 GfVec4h result(0);
99 result[2] = 1;
100 return result;
101 }
103 static GfVec4h WAxis() {
104 GfVec4h result(0);
105 result[3] = 1;
106 return result;
107 }
108
111 static GfVec4h Axis(size_t i) {
112 GfVec4h result(0);
113 if (i < 4)
114 result[i] = 1;
115 return result;
116 }
117
120 _data[0] = s0;
121 _data[1] = s1;
122 _data[2] = s2;
123 _data[3] = s3;
124 return *this;
125 }
126
128 GfVec4h &Set(GfHalf const *a) {
129 return Set(a[0], a[1], a[2], a[3]);
130 }
131
133 GfHalf const *data() const { return _data; }
134 GfHalf *data() { return _data; }
135 GfHalf const *GetArray() const { return data(); }
136
138 GfHalf const &operator[](size_t i) const { return _data[i]; }
139 GfHalf &operator[](size_t i) { return _data[i]; }
140
142 friend inline size_t hash_value(GfVec4h const &vec) {
143 return TfHash::Combine(vec[0], vec[1], vec[2], vec[3]);
144 }
145
147 bool operator==(GfVec4h const &other) const {
148 return _data[0] == other[0] &&
149 _data[1] == other[1] &&
150 _data[2] == other[2] &&
151 _data[3] == other[3];
152 }
153 bool operator!=(GfVec4h const &other) const {
154 return !(*this == other);
155 }
156
157 // TODO Add inequality for other vec types...
159 GF_API
160 bool operator==(class GfVec4d const &other) const;
162 GF_API
163 bool operator==(class GfVec4f const &other) const;
165 GF_API
166 bool operator==(class GfVec4i const &other) const;
167
170 return GfVec4h(-_data[0], -_data[1], -_data[2], -_data[3]);
171 }
172
174 GfVec4h &operator+=(GfVec4h const &other) {
175 _data[0] += other[0];
176 _data[1] += other[1];
177 _data[2] += other[2];
178 _data[3] += other[3];
179 return *this;
180 }
181 friend GfVec4h operator+(GfVec4h const &l, GfVec4h const &r) {
182 return GfVec4h(l) += r;
183 }
184
186 GfVec4h &operator-=(GfVec4h const &other) {
187 _data[0] -= other[0];
188 _data[1] -= other[1];
189 _data[2] -= other[2];
190 _data[3] -= other[3];
191 return *this;
192 }
193 friend GfVec4h operator-(GfVec4h const &l, GfVec4h const &r) {
194 return GfVec4h(l) -= r;
195 }
196
198 GfVec4h &operator*=(double s) {
199 _data[0] *= s;
200 _data[1] *= s;
201 _data[2] *= s;
202 _data[3] *= s;
203 return *this;
204 }
205 GfVec4h operator*(double s) const {
206 return GfVec4h(*this) *= s;
207 }
208 friend GfVec4h operator*(double s, GfVec4h const &v) {
209 return v * s;
210 }
211
213 // TODO should divide by the scalar type.
214 GfVec4h &operator/=(double s) {
215 // TODO This should not multiply by 1/s, it should do the division.
216 // Doing the division is more numerically stable when s is close to
217 // zero.
218 return *this *= (1.0 / s);
219 }
220 GfVec4h operator/(double s) const {
221 return *this * (1.0 / s);
222 }
223
225 GfHalf operator*(GfVec4h const &v) const {
226 return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
227 }
228
233 GfVec4h GetProjection(GfVec4h const &v) const {
234 return v * (*this * v);
235 }
236
242 GfVec4h GetComplement(GfVec4h const &b) const {
243 return *this - this->GetProjection(b);
244 }
245
248 return *this * *this;
249 }
250
253 return GfSqrt(GetLengthSq());
254 }
255
264 GfHalf Normalize(GfHalf eps = 0.001) {
265 // TODO this seems suspect... suggest dividing by length so long as
266 // length is not zero.
267 GfHalf length = GetLength();
268 *this /= (length > eps) ? length : eps;
269 return length;
270 }
271
272 GfVec4h GetNormalized(GfHalf eps = 0.001) const {
273 GfVec4h normalized(*this);
274 normalized.Normalize(eps);
275 return normalized;
276 }
277
278
279private:
280 GfHalf _data[4];
281};
282
285GF_API std::ostream& operator<<(std::ostream &, GfVec4h const &);
286
287
288PXR_NAMESPACE_CLOSE_SCOPE
289
290#include "pxr/base/gf/vec4d.h"
291#include "pxr/base/gf/vec4f.h"
292#include "pxr/base/gf/vec4i.h"
293
294PXR_NAMESPACE_OPEN_SCOPE
295
296inline
297GfVec4h::GfVec4h(class GfVec4d const &other)
298{
299 _data[0] = other[0];
300 _data[1] = other[1];
301 _data[2] = other[2];
302 _data[3] = other[3];
303}
304inline
305GfVec4h::GfVec4h(class GfVec4f const &other)
306{
307 _data[0] = other[0];
308 _data[1] = other[1];
309 _data[2] = other[2];
310 _data[3] = other[3];
311}
312inline
313GfVec4h::GfVec4h(class GfVec4i const &other)
314{
315 _data[0] = other[0];
316 _data[1] = other[1];
317 _data[2] = other[2];
318 _data[3] = other[3];
319}
320
322inline GfVec4h
323GfCompMult(GfVec4h const &v1, GfVec4h const &v2) {
324 return GfVec4h(
325 v1[0] * v2[0],
326 v1[1] * v2[1],
327 v1[2] * v2[2],
328 v1[3] * v2[3]
329 );
330}
331
333inline GfVec4h
334GfCompDiv(GfVec4h const &v1, GfVec4h const &v2) {
335 return GfVec4h(
336 v1[0] / v2[0],
337 v1[1] / v2[1],
338 v1[2] / v2[2],
339 v1[3] / v2[3]
340 );
341}
342
344inline GfHalf
345GfDot(GfVec4h const &v1, GfVec4h const &v2) {
346 return v1 * v2;
347}
348
349
351inline GfHalf
353{
354 return v.GetLength();
355}
356
360inline GfHalf
361GfNormalize(GfVec4h *v, GfHalf eps = 0.001)
362{
363 return v->Normalize(eps);
364}
365
369inline GfVec4h
370GfGetNormalized(GfVec4h const &v, GfHalf eps = 0.001)
371{
372 return v.GetNormalized(eps);
373}
374
379inline GfVec4h
380GfGetProjection(GfVec4h const &a, GfVec4h const &b)
381{
382 return a.GetProjection(b);
383}
384
389inline GfVec4h
390GfGetComplement(GfVec4h const &a, GfVec4h const &b)
391{
392 return a.GetComplement(b);
393}
394
397inline bool
398GfIsClose(GfVec4h const &v1, GfVec4h const &v2, double tolerance)
399{
400 GfVec4h delta = v1 - v2;
401 return delta.GetLengthSq() <= tolerance * tolerance;
402}
403
404
405
406PXR_NAMESPACE_CLOSE_SCOPE
407
408#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:242
static GfVec4h ZAxis()
Create a unit vector along the Z-axis.
Definition: vec4h.h:97
GfVec4h & operator-=(GfVec4h const &other)
Subtraction.
Definition: vec4h.h:186
static GfVec4h YAxis()
Create a unit vector along the Y-axis.
Definition: vec4h.h:91
static GfVec4h WAxis()
Create a unit vector along the W-axis.
Definition: vec4h.h:103
GfHalf GetLength() const
Length.
Definition: vec4h.h:252
GfHalf operator*(GfVec4h const &v) const
See GfDot().
Definition: vec4h.h:225
constexpr GfVec4h(Scl const *p)
Construct with pointer to values.
Definition: vec4h.h:70
GfVec4h operator-() const
Create a vec with negated elements.
Definition: vec4h.h:169
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:119
constexpr GfVec4h(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
Initialize all elements with explicit arguments.
Definition: vec4h.h:63
GfHalf Normalize(GfHalf eps=0.001)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec4h.h:264
GfVec4h()=default
Default constructor does no initialization.
friend size_t hash_value(GfVec4h const &vec)
Hash.
Definition: vec4h.h:142
GfVec4h & operator/=(double s)
Division by scalar.
Definition: vec4h.h:214
GfHalf GetLengthSq() const
Squared length.
Definition: vec4h.h:247
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:128
GF_API bool operator==(class GfVec4i const &other) const
Equality comparison.
GfHalf const & operator[](size_t i) const
Indexing.
Definition: vec4h.h:138
GfHalf const * data() const
Direct data access.
Definition: vec4h.h:133
GF_API bool operator==(class GfVec4d const &other) const
Equality comparison.
GfVec4h & operator*=(double s)
Multiplication by scalar.
Definition: vec4h.h:198
static GfVec4h XAxis()
Create a unit vector along the X-axis.
Definition: vec4h.h:85
GfVec4h GetProjection(GfVec4h const &v) const
Returns the projection of this onto v.
Definition: vec4h.h:233
GfVec4h & operator+=(GfVec4h const &other)
Addition.
Definition: vec4h.h:174
bool operator==(GfVec4h const &other) const
Equality comparison.
Definition: vec4h.h:147
static GfVec4h Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec4h.h:111
constexpr GfVec4h(GfHalf value)
Initialize all elements to a single value.
Definition: vec4h.h:57
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:390
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:398
GfHalf GfNormalize(GfVec4h *v, GfHalf eps=0.001)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec4h.h:361
GfVec4h GfCompDiv(GfVec4h const &v1, GfVec4h const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec4h.h:334
GfHalf GfDot(GfVec4h const &v1, GfVec4h const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec4h.h:345
GfVec4h GfGetNormalized(GfVec4h const &v, GfHalf eps=0.001)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec4h.h:370
GfVec4h GfCompMult(GfVec4h const &v1, GfVec4h const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec4h.h:323
GfVec4h GfGetProjection(GfVec4h const &a, GfVec4h const &b)
Returns the projection of a onto b.
Definition: vec4h.h:380
GfHalf GfGetLength(GfVec4h const &v)
Returns the geometric length of v.
Definition: vec4h.h:352