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  /// GfVec2i value-initializes to zero and performs no default
51  /// initialization, like float or double.
52  GfVec2i() = default;
53 
54  /// Initialize all elements to a single value.
55  constexpr explicit GfVec2i(int value)
56  : _data{ value, value }
57  {
58  }
59 
60  /// Initialize all elements with explicit arguments.
61  constexpr GfVec2i(int s0, int s1)
62  : _data{ s0, s1 }
63  {
64  }
65 
66  /// Construct with pointer to values.
67  template <class Scl>
68  constexpr explicit GfVec2i(Scl const *p)
69  : _data{ p[0], p[1] }
70  {
71  }
72 
73  /// Create a unit vector along the X-axis.
74  static GfVec2i XAxis() {
75  GfVec2i result(0);
76  result[0] = 1;
77  return result;
78  }
79  /// Create a unit vector along the Y-axis.
80  static GfVec2i YAxis() {
81  GfVec2i result(0);
82  result[1] = 1;
83  return result;
84  }
85 
86  /// Create a unit vector along the i-th axis, zero-based. Return the zero
87  /// vector if \p i is greater than or equal to 2.
88  static GfVec2i Axis(size_t i) {
89  GfVec2i result(0);
90  if (i < 2)
91  result[i] = 1;
92  return result;
93  }
94 
95  /// Set all elements with passed arguments.
96  GfVec2i &Set(int s0, int s1) {
97  _data[0] = s0;
98  _data[1] = s1;
99  return *this;
100  }
101 
102  /// Set all elements with a pointer to data.
103  GfVec2i &Set(int const *a) {
104  return Set(a[0], a[1]);
105  }
106 
107  /// Direct data access.
108  int const *data() const { return _data; }
109  int *data() { return _data; }
110  int const *GetArray() const { return data(); }
111 
112  /// Indexing.
113  int const &operator[](size_t i) const { return _data[i]; }
114  int &operator[](size_t i) { return _data[i]; }
115 
116  /// Hash.
117  friend inline size_t hash_value(GfVec2i const &vec) {
118  return TfHash::Combine(vec[0], vec[1]);
119  }
120 
121  /// Equality comparison.
122  bool operator==(GfVec2i const &other) const {
123  return _data[0] == other[0] &&
124  _data[1] == other[1];
125  }
126  bool operator!=(GfVec2i const &other) const {
127  return !(*this == other);
128  }
129 
130  // TODO Add inequality for other vec types...
131  /// Equality comparison.
132  GF_API
133  bool operator==(class GfVec2d const &other) const;
134  /// Equality comparison.
135  GF_API
136  bool operator==(class GfVec2f const &other) const;
137  /// Equality comparison.
138  GF_API
139  bool operator==(class GfVec2h const &other) const;
140 
141  /// Create a vec with negated elements.
142  GfVec2i operator-() const {
143  return GfVec2i(-_data[0], -_data[1]);
144  }
145 
146  /// Addition.
147  GfVec2i &operator+=(GfVec2i const &other) {
148  _data[0] += other[0];
149  _data[1] += other[1];
150  return *this;
151  }
152  friend GfVec2i operator+(GfVec2i const &l, GfVec2i const &r) {
153  return GfVec2i(l) += r;
154  }
155 
156  /// Subtraction.
157  GfVec2i &operator-=(GfVec2i const &other) {
158  _data[0] -= other[0];
159  _data[1] -= other[1];
160  return *this;
161  }
162  friend GfVec2i operator-(GfVec2i const &l, GfVec2i const &r) {
163  return GfVec2i(l) -= r;
164  }
165 
166  /// Multiplication by scalar.
167  GfVec2i &operator*=(double s) {
168  _data[0] *= s;
169  _data[1] *= s;
170  return *this;
171  }
172  GfVec2i operator*(double s) const {
173  return GfVec2i(*this) *= s;
174  }
175  friend GfVec2i operator*(double s, GfVec2i const &v) {
176  return v * s;
177  }
178 
179  /// Division by scalar.
181  _data[0] /= s;
182  _data[1] /= s;
183  return *this;
184  }
185  GfVec2i operator/(int s) const {
186  return GfVec2i(*this) /= s;
187  }
188 
189  /// See GfDot().
190  int operator*(GfVec2i const &v) const {
191  return _data[0] * v[0] + _data[1] * v[1];
192  }
193 
194  /// Returns the projection of \p this onto \p v. That is:
195  /// \code
196  /// v * (*this * v)
197  /// \endcode
198  GfVec2i GetProjection(GfVec2i const &v) const {
199  return v * (*this * v);
200  }
201 
202  /// Returns the orthogonal complement of \p this->GetProjection(b).
203  /// That is:
204  /// \code
205  /// *this - this->GetProjection(b)
206  /// \endcode
207  GfVec2i GetComplement(GfVec2i const &b) const {
208  return *this - this->GetProjection(b);
209  }
210 
211  /// Squared length.
212  int GetLengthSq() const {
213  return *this * *this;
214  }
215 
216 
217 private:
218  int _data[2];
219 };
220 
221 /// Output a GfVec2i.
222 /// \ingroup group_gf_DebuggingOutput
223 GF_API std::ostream& operator<<(std::ostream &, GfVec2i const &);
224 
225 
226 /// Returns component-wise multiplication of vectors \p v1 and \p v2.
227 inline GfVec2i
228 GfCompMult(GfVec2i const &v1, GfVec2i const &v2) {
229  return GfVec2i(
230  v1[0] * v2[0],
231  v1[1] * v2[1]
232  );
233 }
234 
235 /// Returns component-wise quotient of vectors \p v1 and \p v2.
236 inline GfVec2i
237 GfCompDiv(GfVec2i const &v1, GfVec2i const &v2) {
238  return GfVec2i(
239  v1[0] / v2[0],
240  v1[1] / v2[1]
241  );
242 }
243 
244 /// Returns the dot (inner) product of two vectors.
245 inline int
246 GfDot(GfVec2i const &v1, GfVec2i const &v2) {
247  return v1 * v2;
248 }
249 
250 
252 
253 #endif // PXR_BASE_GF_VEC2I_H
int const & operator[](size_t i) const
Indexing.
Definition: vec2i.h:113
friend GfVec2i operator+(GfVec2i const &l, GfVec2i const &r)
Definition: vec2i.h:152
GfVec2i GetProjection(GfVec2i const &v) const
Definition: vec2i.h:198
bool operator!=(GfVec2i const &other) const
Definition: vec2i.h:126
friend GfVec2i operator*(double s, GfVec2i const &v)
Definition: vec2i.h:175
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
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GfVec2i()=default
int const * GetArray() const
Definition: vec2i.h:110
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:122
GfVec2i & operator*=(double s)
Multiplication by scalar.
Definition: vec2i.h:167
static GfVec2i XAxis()
Create a unit vector along the X-axis.
Definition: vec2i.h:74
int GetLengthSq() const
Squared length.
Definition: vec2i.h:212
GfVec2i & Set(int const *a)
Set all elements with a pointer to data.
Definition: vec2i.h:103
GfVec2i operator-() const
Create a vec with negated elements.
Definition: vec2i.h:142
GfVec2i operator/(int s) const
Definition: vec2i.h:185
Definition: vec2h.h:46
int GfDot(GfVec2i const &v1, GfVec2i const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2i.h:246
GfVec2i GfCompMult(GfVec2i const &v1, GfVec2i const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2i.h:228
friend GfVec2i operator-(GfVec2i const &l, GfVec2i const &r)
Definition: vec2i.h:162
int const * data() const
Direct data access.
Definition: vec2i.h:108
GfVec2i & operator+=(GfVec2i const &other)
Addition.
Definition: vec2i.h:147
static GfVec2i Axis(size_t i)
Definition: vec2i.h:88
friend size_t hash_value(GfVec2i const &vec)
Hash.
Definition: vec2i.h:117
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:190
GfVec2i & Set(int s0, int s1)
Set all elements with passed arguments.
Definition: vec2i.h:96
int * data()
Definition: vec2i.h:109
GfVec2i & operator/=(int s)
Division by scalar.
Definition: vec2i.h:180
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
static GfVec2i YAxis()
Create a unit vector along the Y-axis.
Definition: vec2i.h:80
GfVec2i GfCompDiv(GfVec2i const &v1, GfVec2i const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2i.h:237
Definition: vec2f.h:45
int & operator[](size_t i)
Definition: vec2i.h:114
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:55
GfVec2i GetComplement(GfVec2i const &b) const
Definition: vec2i.h:207
constexpr GfVec2i(Scl const *p)
Construct with pointer to values.
Definition: vec2i.h:68
GfVec2i & operator-=(GfVec2i const &other)
Subtraction.
Definition: vec2i.h:157
GLboolean r
Definition: glcorearb.h:1222
GfVec2i operator*(double s) const
Definition: vec2i.h:172
#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:61