All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
vec4d.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_VEC4D_H
12#define PXR_BASE_GF_VEC4D_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/tf/hash.h"
24
25#include <cstddef>
26#include <cmath>
27
28#include <iosfwd>
29
30PXR_NAMESPACE_OPEN_SCOPE
31
32class GfVec4d;
33
34template <>
35struct GfIsGfVec<class GfVec4d> { static const bool value = true; };
36
46{
47public:
49 typedef double ScalarType;
50 static const size_t dimension = 4;
51
53 GfVec4d() = default;
54
56 constexpr explicit GfVec4d(double value)
57 : _data{ value, value, value, value }
58 {
59 }
60
62 constexpr GfVec4d(double s0, double s1, double s2, double s3)
63 : _data{ s0, s1, s2, s3 }
64 {
65 }
66
68 template <class Scl>
69 constexpr explicit GfVec4d(Scl const *p)
70 : _data{ p[0], p[1], p[2], p[3] }
71 {
72 }
73
75 GfVec4d(class GfVec4f const &other);
76
78 GfVec4d(class GfVec4h const &other);
79
81 GfVec4d(class GfVec4i const &other);
82
84 static GfVec4d XAxis() {
85 GfVec4d result(0);
86 result[0] = 1;
87 return result;
88 }
90 static GfVec4d YAxis() {
91 GfVec4d result(0);
92 result[1] = 1;
93 return result;
94 }
96 static GfVec4d ZAxis() {
97 GfVec4d result(0);
98 result[2] = 1;
99 return result;
100 }
102 static GfVec4d WAxis() {
103 GfVec4d result(0);
104 result[3] = 1;
105 return result;
106 }
107
110 static GfVec4d Axis(size_t i) {
111 GfVec4d result(0);
112 if (i < 4)
113 result[i] = 1;
114 return result;
115 }
116
118 GfVec4d &Set(double s0, double s1, double s2, double s3) {
119 _data[0] = s0;
120 _data[1] = s1;
121 _data[2] = s2;
122 _data[3] = s3;
123 return *this;
124 }
125
127 GfVec4d &Set(double const *a) {
128 return Set(a[0], a[1], a[2], a[3]);
129 }
130
132 double const *data() const { return _data; }
133 double *data() { return _data; }
134 double const *GetArray() const { return data(); }
135
137 double const &operator[](size_t i) const { return _data[i]; }
138 double &operator[](size_t i) { return _data[i]; }
139
141 friend inline size_t hash_value(GfVec4d const &vec) {
142 return TfHash::Combine(vec[0], vec[1], vec[2], vec[3]);
143 }
144
146 bool operator==(GfVec4d const &other) const {
147 return _data[0] == other[0] &&
148 _data[1] == other[1] &&
149 _data[2] == other[2] &&
150 _data[3] == other[3];
151 }
152 bool operator!=(GfVec4d const &other) const {
153 return !(*this == other);
154 }
155
156 // TODO Add inequality for other vec types...
158 GF_API
159 bool operator==(class GfVec4f const &other) const;
161 GF_API
162 bool operator==(class GfVec4h const &other) const;
164 GF_API
165 bool operator==(class GfVec4i const &other) const;
166
169 return GfVec4d(-_data[0], -_data[1], -_data[2], -_data[3]);
170 }
171
173 GfVec4d &operator+=(GfVec4d const &other) {
174 _data[0] += other[0];
175 _data[1] += other[1];
176 _data[2] += other[2];
177 _data[3] += other[3];
178 return *this;
179 }
180 friend GfVec4d operator+(GfVec4d const &l, GfVec4d const &r) {
181 return GfVec4d(l) += r;
182 }
183
185 GfVec4d &operator-=(GfVec4d const &other) {
186 _data[0] -= other[0];
187 _data[1] -= other[1];
188 _data[2] -= other[2];
189 _data[3] -= other[3];
190 return *this;
191 }
192 friend GfVec4d operator-(GfVec4d const &l, GfVec4d const &r) {
193 return GfVec4d(l) -= r;
194 }
195
197 GfVec4d &operator*=(double s) {
198 _data[0] *= s;
199 _data[1] *= s;
200 _data[2] *= s;
201 _data[3] *= s;
202 return *this;
203 }
204 GfVec4d operator*(double s) const {
205 return GfVec4d(*this) *= s;
206 }
207 friend GfVec4d operator*(double s, GfVec4d const &v) {
208 return v * s;
209 }
210
212 // TODO should divide by the scalar type.
213 GfVec4d &operator/=(double s) {
214 // TODO This should not multiply by 1/s, it should do the division.
215 // Doing the division is more numerically stable when s is close to
216 // zero.
217 return *this *= (1.0 / s);
218 }
219 GfVec4d operator/(double s) const {
220 return *this * (1.0 / s);
221 }
222
224 double operator*(GfVec4d const &v) const {
225 return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
226 }
227
232 GfVec4d GetProjection(GfVec4d const &v) const {
233 return v * (*this * v);
234 }
235
241 GfVec4d GetComplement(GfVec4d const &b) const {
242 return *this - this->GetProjection(b);
243 }
244
246 double GetLengthSq() const {
247 return *this * *this;
248 }
249
251 double GetLength() const {
252 return GfSqrt(GetLengthSq());
253 }
254
263 double Normalize(double eps = GF_MIN_VECTOR_LENGTH) {
264 // TODO this seems suspect... suggest dividing by length so long as
265 // length is not zero.
266 double length = GetLength();
267 *this /= (length > eps) ? length : eps;
268 return length;
269 }
270
271 GfVec4d GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const {
272 GfVec4d normalized(*this);
273 normalized.Normalize(eps);
274 return normalized;
275 }
276
277
278private:
279 double _data[4];
280};
281
284GF_API std::ostream& operator<<(std::ostream &, GfVec4d const &);
285
286
287PXR_NAMESPACE_CLOSE_SCOPE
288
289#include "pxr/base/gf/vec4f.h"
290#include "pxr/base/gf/vec4h.h"
291#include "pxr/base/gf/vec4i.h"
292
293PXR_NAMESPACE_OPEN_SCOPE
294
295inline
296GfVec4d::GfVec4d(class GfVec4f const &other)
297{
298 _data[0] = other[0];
299 _data[1] = other[1];
300 _data[2] = other[2];
301 _data[3] = other[3];
302}
303inline
304GfVec4d::GfVec4d(class GfVec4h const &other)
305{
306 _data[0] = other[0];
307 _data[1] = other[1];
308 _data[2] = other[2];
309 _data[3] = other[3];
310}
311inline
312GfVec4d::GfVec4d(class GfVec4i const &other)
313{
314 _data[0] = other[0];
315 _data[1] = other[1];
316 _data[2] = other[2];
317 _data[3] = other[3];
318}
319
321inline GfVec4d
322GfCompMult(GfVec4d const &v1, GfVec4d const &v2) {
323 return GfVec4d(
324 v1[0] * v2[0],
325 v1[1] * v2[1],
326 v1[2] * v2[2],
327 v1[3] * v2[3]
328 );
329}
330
332inline GfVec4d
333GfCompDiv(GfVec4d const &v1, GfVec4d const &v2) {
334 return GfVec4d(
335 v1[0] / v2[0],
336 v1[1] / v2[1],
337 v1[2] / v2[2],
338 v1[3] / v2[3]
339 );
340}
341
343inline double
344GfDot(GfVec4d const &v1, GfVec4d const &v2) {
345 return v1 * v2;
346}
347
348
350inline double
352{
353 return v.GetLength();
354}
355
359inline double
361{
362 return v->Normalize(eps);
363}
364
368inline GfVec4d
370{
371 return v.GetNormalized(eps);
372}
373
378inline GfVec4d
379GfGetProjection(GfVec4d const &a, GfVec4d const &b)
380{
381 return a.GetProjection(b);
382}
383
388inline GfVec4d
389GfGetComplement(GfVec4d const &a, GfVec4d const &b)
390{
391 return a.GetComplement(b);
392}
393
396inline bool
397GfIsClose(GfVec4d const &v1, GfVec4d const &v2, double tolerance)
398{
399 GfVec4d delta = v1 - v2;
400 return delta.GetLengthSq() <= tolerance * tolerance;
401}
402
403
404
405PXR_NAMESPACE_CLOSE_SCOPE
406
407#endif // PXR_BASE_GF_VEC4D_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
GfVec4d operator-() const
Create a vec with negated elements.
Definition: vec4d.h:168
bool operator==(GfVec4d const &other) const
Equality comparison.
Definition: vec4d.h:146
double GetLengthSq() const
Squared length.
Definition: vec4d.h:246
constexpr GfVec4d(Scl const *p)
Construct with pointer to values.
Definition: vec4d.h:69
static GfVec4d XAxis()
Create a unit vector along the X-axis.
Definition: vec4d.h:84
GfVec4d & operator-=(GfVec4d const &other)
Subtraction.
Definition: vec4d.h:185
static GfVec4d ZAxis()
Create a unit vector along the Z-axis.
Definition: vec4d.h:96
double ScalarType
Scalar element type and dimension.
Definition: vec4d.h:49
double const * data() const
Direct data access.
Definition: vec4d.h:132
friend size_t hash_value(GfVec4d const &vec)
Hash.
Definition: vec4d.h:141
static GfVec4d YAxis()
Create a unit vector along the Y-axis.
Definition: vec4d.h:90
double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Normalizes the vector in place to unit length, returning the length before normalization.
Definition: vec4d.h:263
GfVec4d & Set(double const *a)
Set all elements with a pointer to data.
Definition: vec4d.h:127
static GfVec4d Axis(size_t i)
Create a unit vector along the i-th axis, zero-based.
Definition: vec4d.h:110
GfVec4d GetComplement(GfVec4d const &b) const
Returns the orthogonal complement of this->GetProjection(b).
Definition: vec4d.h:241
constexpr GfVec4d(double s0, double s1, double s2, double s3)
Initialize all elements with explicit arguments.
Definition: vec4d.h:62
static GfVec4d WAxis()
Create a unit vector along the W-axis.
Definition: vec4d.h:102
GF_API bool operator==(class GfVec4f const &other) const
Equality comparison.
GfVec4d GetProjection(GfVec4d const &v) const
Returns the projection of this onto v.
Definition: vec4d.h:232
GfVec4d & operator*=(double s)
Multiplication by scalar.
Definition: vec4d.h:197
GF_API bool operator==(class GfVec4i const &other) const
Equality comparison.
GF_API bool operator==(class GfVec4h const &other) const
Equality comparison.
constexpr GfVec4d(double value)
Initialize all elements to a single value.
Definition: vec4d.h:56
double const & operator[](size_t i) const
Indexing.
Definition: vec4d.h:137
GfVec4d()=default
Default constructor does no initialization.
GfVec4d & Set(double s0, double s1, double s2, double s3)
Set all elements with passed arguments.
Definition: vec4d.h:118
GfVec4d & operator/=(double s)
Division by scalar.
Definition: vec4d.h:213
double GetLength() const
Length.
Definition: vec4d.h:251
double operator*(GfVec4d const &v) const
See GfDot().
Definition: vec4d.h:224
GfVec4d & operator+=(GfVec4d const &other)
Addition.
Definition: vec4d.h:173
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
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:475
Assorted mathematical utility functions.
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:187
#define GF_MIN_VECTOR_LENGTH
This constant is used to determine whether the length of a vector is too small to handle accurately.
Definition: limits.h:17
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
Defines useful mathematical limits.
A metafunction with a static const bool member 'value' that is true for GfVec types,...
Definition: traits.h:19
GfVec4d GfGetNormalized(GfVec4d const &v, double eps=GF_MIN_VECTOR_LENGTH)
Returns a normalized (unit-length) vector with the same direction as v.
Definition: vec4d.h:369
GfVec4d GfGetProjection(GfVec4d const &a, GfVec4d const &b)
Returns the projection of a onto b.
Definition: vec4d.h:379
GfVec4d GfCompMult(GfVec4d const &v1, GfVec4d const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec4d.h:322
double GfDot(GfVec4d const &v1, GfVec4d const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec4d.h:344
bool GfIsClose(GfVec4d const &v1, GfVec4d const &v2, double tolerance)
Tests for equality within a given tolerance, returning true if the length of the difference vector is...
Definition: vec4d.h:397
double GfNormalize(GfVec4d *v, double eps=GF_MIN_VECTOR_LENGTH)
Normalizes *v in place to unit length, returning the length before normalization.
Definition: vec4d.h:360
GfVec4d GfCompDiv(GfVec4d const &v1, GfVec4d const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec4d.h:333
double GfGetLength(GfVec4d const &v)
Returns the geometric length of v.
Definition: vec4d.h:351
GfVec4d GfGetComplement(GfVec4d const &a, GfVec4d const &b)
Returns the orthogonal complement of a.GetProjection(b).
Definition: vec4d.h:389