HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vec2i.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 //
7 ////////////////////////////////////////////////////////////////////////
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_VEC2I_H
12 #define PXR_BASE_GF_VEC2I_H
13 
14 /// \file gf/vec2i.h
15 /// \ingroup group_gf_LinearAlgebra
16 
17 #include "pxr/pxr.h"
18 #include "pxr/base/tf/diagnostic.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/tf/hash.h"
23 
24 #include <cstddef>
25 
26 #include <iosfwd>
27 
29 
30 class GfVec2i;
31 
32 template <>
33 struct GfIsGfVec<class GfVec2i> { static const bool value = true; };
34 
35 /// \class GfVec2i
36 /// \ingroup group_gf_LinearAlgebra
37 ///
38 /// Basic type for a vector of 2 int components.
39 ///
40 /// Represents a vector of 2 components of type \c int.
41 /// It is intended to be fast and simple.
42 ///
43 class GfVec2i
44 {
45 public:
46  /// Scalar element type and dimension.
47  typedef int ScalarType;
48  static const size_t dimension = 2;
49 
50  /// Default constructor does no initialization.
51  GfVec2i() = default;
52 
53  /// Initialize all elements to a single value.
54  constexpr explicit GfVec2i(int value)
55  : _data{ value, value }
56  {
57  }
58 
59  /// Initialize all elements with explicit arguments.
60  constexpr GfVec2i(int s0, int s1)
61  : _data{ s0, s1 }
62  {
63  }
64 
65  /// Construct with pointer to values.
66  template <class Scl>
67  constexpr explicit GfVec2i(Scl const *p)
68  : _data{ p[0], p[1] }
69  {
70  }
71 
72  /// Create a unit vector along the X-axis.
73  static GfVec2i XAxis() {
74  GfVec2i result(0);
75  result[0] = 1;
76  return result;
77  }
78  /// Create a unit vector along the Y-axis.
79  static GfVec2i YAxis() {
80  GfVec2i result(0);
81  result[1] = 1;
82  return result;
83  }
84 
85  /// Create a unit vector along the i-th axis, zero-based. Return the zero
86  /// vector if \p i is greater than or equal to 2.
87  static GfVec2i Axis(size_t i) {
88  GfVec2i result(0);
89  if (i < 2)
90  result[i] = 1;
91  return result;
92  }
93 
94  /// Set all elements with passed arguments.
95  GfVec2i &Set(int s0, int s1) {
96  _data[0] = s0;
97  _data[1] = s1;
98  return *this;
99  }
100 
101  /// Set all elements with a pointer to data.
102  GfVec2i &Set(int const *a) {
103  return Set(a[0], a[1]);
104  }
105 
106  /// Direct data access.
107  int const *data() const { return _data; }
108  int *data() { return _data; }
109  int const *GetArray() const { return data(); }
110 
111  /// Indexing.
112  int const &operator[](size_t i) const { return _data[i]; }
113  int &operator[](size_t i) { return _data[i]; }
114 
115  /// Hash.
116  friend inline size_t hash_value(GfVec2i const &vec) {
117  return TfHash::Combine(vec[0], vec[1]);
118  }
119 
120  /// Equality comparison.
121  bool operator==(GfVec2i const &other) const {
122  return _data[0] == other[0] &&
123  _data[1] == other[1];
124  }
125  bool operator!=(GfVec2i const &other) const {
126  return !(*this == other);
127  }
128 
129  // TODO Add inequality for other vec types...
130  /// Equality comparison.
131  GF_API
132  bool operator==(class GfVec2d const &other) const;
133  /// Equality comparison.
134  GF_API
135  bool operator==(class GfVec2f const &other) const;
136  /// Equality comparison.
137  GF_API
138  bool operator==(class GfVec2h const &other) const;
139 
140  /// Create a vec with negated elements.
141  GfVec2i operator-() const {
142  return GfVec2i(-_data[0], -_data[1]);
143  }
144 
145  /// Addition.
146  GfVec2i &operator+=(GfVec2i const &other) {
147  _data[0] += other[0];
148  _data[1] += other[1];
149  return *this;
150  }
151  friend GfVec2i operator+(GfVec2i const &l, GfVec2i const &r) {
152  return GfVec2i(l) += r;
153  }
154 
155  /// Subtraction.
156  GfVec2i &operator-=(GfVec2i const &other) {
157  _data[0] -= other[0];
158  _data[1] -= other[1];
159  return *this;
160  }
161  friend GfVec2i operator-(GfVec2i const &l, GfVec2i const &r) {
162  return GfVec2i(l) -= r;
163  }
164 
165  /// Multiplication by scalar.
166  GfVec2i &operator*=(double s) {
167  _data[0] *= s;
168  _data[1] *= s;
169  return *this;
170  }
171  GfVec2i operator*(double s) const {
172  return GfVec2i(*this) *= s;
173  }
174  friend GfVec2i operator*(double s, GfVec2i const &v) {
175  return v * s;
176  }
177 
178  /// Division by scalar.
180  _data[0] /= s;
181  _data[1] /= s;
182  return *this;
183  }
184  GfVec2i operator/(int s) const {
185  return GfVec2i(*this) /= s;
186  }
187 
188  /// See GfDot().
189  int operator*(GfVec2i const &v) const {
190  return _data[0] * v[0] + _data[1] * v[1];
191  }
192 
193  /// Returns the projection of \p this onto \p v. That is:
194  /// \code
195  /// v * (*this * v)
196  /// \endcode
197  GfVec2i GetProjection(GfVec2i const &v) const {
198  return v * (*this * v);
199  }
200 
201  /// Returns the orthogonal complement of \p this->GetProjection(b).
202  /// That is:
203  /// \code
204  /// *this - this->GetProjection(b)
205  /// \endcode
206  GfVec2i GetComplement(GfVec2i const &b) const {
207  return *this - this->GetProjection(b);
208  }
209 
210  /// Squared length.
211  int GetLengthSq() const {
212  return *this * *this;
213  }
214 
215 
216 private:
217  int _data[2];
218 };
219 
220 /// Output a GfVec2i.
221 /// \ingroup group_gf_DebuggingOutput
222 GF_API std::ostream& operator<<(std::ostream &, GfVec2i const &);
223 
224 
225 /// Returns component-wise multiplication of vectors \p v1 and \p v2.
226 inline GfVec2i
227 GfCompMult(GfVec2i const &v1, GfVec2i const &v2) {
228  return GfVec2i(
229  v1[0] * v2[0],
230  v1[1] * v2[1]
231  );
232 }
233 
234 /// Returns component-wise quotient of vectors \p v1 and \p v2.
235 inline GfVec2i
236 GfCompDiv(GfVec2i const &v1, GfVec2i const &v2) {
237  return GfVec2i(
238  v1[0] / v2[0],
239  v1[1] / v2[1]
240  );
241 }
242 
243 /// Returns the dot (inner) product of two vectors.
244 inline int
245 GfDot(GfVec2i const &v1, GfVec2i const &v2) {
246  return v1 * v2;
247 }
248 
249 
251 
252 #endif // PXR_BASE_GF_VEC2I_H
int const & operator[](size_t i) const
Indexing.
Definition: vec2i.h:112
friend GfVec2i operator+(GfVec2i const &l, GfVec2i const &r)
Definition: vec2i.h:151
GfVec2i GetProjection(GfVec2i const &v) const
Definition: vec2i.h:197
bool operator!=(GfVec2i const &other) const
Definition: vec2i.h:125
friend GfVec2i operator*(double s, GfVec2i const &v)
Definition: vec2i.h:174
Definition: vec2i.h:43
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GfVec2i()=default
Default constructor does no initialization.
int const * GetArray() const
Definition: vec2i.h:109
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
**But if you need a result
Definition: thread.h:622
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
GF_API std::ostream & operator<<(std::ostream &, GfVec2i const &)
Definition: vec2d.h:45
bool operator==(GfVec2i const &other) const
Equality comparison.
Definition: vec2i.h:121
GfVec2i & operator*=(double s)
Multiplication by scalar.
Definition: vec2i.h:166
static GfVec2i XAxis()
Create a unit vector along the X-axis.
Definition: vec2i.h:73
int GetLengthSq() const
Squared length.
Definition: vec2i.h:211
GfVec2i & Set(int const *a)
Set all elements with a pointer to data.
Definition: vec2i.h:102
GfVec2i operator-() const
Create a vec with negated elements.
Definition: vec2i.h:141
GfVec2i operator/(int s) const
Definition: vec2i.h:184
Definition: vec2h.h:46
int GfDot(GfVec2i const &v1, GfVec2i const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2i.h:245
GfVec2i GfCompMult(GfVec2i const &v1, GfVec2i const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2i.h:227
friend GfVec2i operator-(GfVec2i const &l, GfVec2i const &r)
Definition: vec2i.h:161
int const * data() const
Direct data access.
Definition: vec2i.h:107
GfVec2i & operator+=(GfVec2i const &other)
Addition.
Definition: vec2i.h:146
static GfVec2i Axis(size_t i)
Definition: vec2i.h:87
friend size_t hash_value(GfVec2i const &vec)
Hash.
Definition: vec2i.h:116
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
int ScalarType
Scalar element type and dimension.
Definition: vec2i.h:47
int operator*(GfVec2i const &v) const
See GfDot().
Definition: vec2i.h:189
GfVec2i & Set(int s0, int s1)
Set all elements with passed arguments.
Definition: vec2i.h:95
int * data()
Definition: vec2i.h:108
GfVec2i & operator/=(int s)
Division by scalar.
Definition: vec2i.h:179
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
static GfVec2i YAxis()
Create a unit vector along the Y-axis.
Definition: vec2i.h:79
GfVec2i GfCompDiv(GfVec2i const &v1, GfVec2i const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2i.h:236
Definition: vec2f.h:45
int & operator[](size_t i)
Definition: vec2i.h:113
GLfloat GLfloat v1
Definition: glcorearb.h:817
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
constexpr GfVec2i(int value)
Initialize all elements to a single value.
Definition: vec2i.h:54
GfVec2i GetComplement(GfVec2i const &b) const
Definition: vec2i.h:206
constexpr GfVec2i(Scl const *p)
Construct with pointer to values.
Definition: vec2i.h:67
GfVec2i & operator-=(GfVec2i const &other)
Subtraction.
Definition: vec2i.h:156
GLboolean r
Definition: glcorearb.h:1222
GfVec2i operator*(double s) const
Definition: vec2i.h:171
#define GF_API
Definition: api.h:23
static const size_t dimension
Definition: vec2i.h:48
constexpr GfVec2i(int s0, int s1)
Initialize all elements with explicit arguments.
Definition: vec2i.h:60