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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 // This file is generated by a script. Do not edit directly. Edit the
26 // vec.template.h file to make changes.
27 
28 #ifndef PXR_BASE_GF_VEC2I_H
29 #define PXR_BASE_GF_VEC2I_H
30 
31 /// \file gf/vec2i.h
32 /// \ingroup group_gf_LinearAlgebra
33 
34 #include "pxr/pxr.h"
35 #include "pxr/base/tf/diagnostic.h"
36 #include "pxr/base/gf/api.h"
37 #include "pxr/base/gf/limits.h"
38 #include "pxr/base/gf/traits.h"
39 #include "pxr/base/tf/hash.h"
40 
41 #include <cstddef>
42 
43 #include <iosfwd>
44 
46 
47 class GfVec2i;
48 
49 template <>
50 struct GfIsGfVec<class GfVec2i> { static const bool value = true; };
51 
52 /// \class GfVec2i
53 /// \ingroup group_gf_LinearAlgebra
54 ///
55 /// Basic type for a vector of 2 int components.
56 ///
57 /// Represents a vector of 2 components of type \c int.
58 /// It is intended to be fast and simple.
59 ///
60 class GfVec2i
61 {
62 public:
63  /// Scalar element type and dimension.
64  typedef int ScalarType;
65  static const size_t dimension = 2;
66 
67  /// Default constructor does no initialization.
68  GfVec2i() = default;
69 
70  /// Initialize all elements to a single value.
71  constexpr explicit GfVec2i(int value)
72  : _data{ value, value }
73  {
74  }
75 
76  /// Initialize all elements with explicit arguments.
77  constexpr GfVec2i(int s0, int s1)
78  : _data{ s0, s1 }
79  {
80  }
81 
82  /// Construct with pointer to values.
83  template <class Scl>
84  constexpr explicit GfVec2i(Scl const *p)
85  : _data{ p[0], p[1] }
86  {
87  }
88 
89  /// Create a unit vector along the X-axis.
90  static GfVec2i XAxis() {
91  GfVec2i result(0);
92  result[0] = 1;
93  return result;
94  }
95  /// Create a unit vector along the Y-axis.
96  static GfVec2i YAxis() {
97  GfVec2i result(0);
98  result[1] = 1;
99  return result;
100  }
101 
102  /// Create a unit vector along the i-th axis, zero-based. Return the zero
103  /// vector if \p i is greater than or equal to 2.
104  static GfVec2i Axis(size_t i) {
105  GfVec2i result(0);
106  if (i < 2)
107  result[i] = 1;
108  return result;
109  }
110 
111  /// Set all elements with passed arguments.
112  GfVec2i &Set(int s0, int s1) {
113  _data[0] = s0;
114  _data[1] = s1;
115  return *this;
116  }
117 
118  /// Set all elements with a pointer to data.
119  GfVec2i &Set(int const *a) {
120  return Set(a[0], a[1]);
121  }
122 
123  /// Direct data access.
124  int const *data() const { return _data; }
125  int *data() { return _data; }
126  int const *GetArray() const { return data(); }
127 
128  /// Indexing.
129  int const &operator[](size_t i) const { return _data[i]; }
130  int &operator[](size_t i) { return _data[i]; }
131 
132  /// Hash.
133  friend inline size_t hash_value(GfVec2i const &vec) {
134  return TfHash::Combine(vec[0], vec[1]);
135  }
136 
137  /// Equality comparison.
138  bool operator==(GfVec2i const &other) const {
139  return _data[0] == other[0] &&
140  _data[1] == other[1];
141  }
142  bool operator!=(GfVec2i const &other) const {
143  return !(*this == other);
144  }
145 
146  // TODO Add inequality for other vec types...
147  /// Equality comparison.
148  GF_API
149  bool operator==(class GfVec2d const &other) const;
150  /// Equality comparison.
151  GF_API
152  bool operator==(class GfVec2f const &other) const;
153  /// Equality comparison.
154  GF_API
155  bool operator==(class GfVec2h const &other) const;
156 
157  /// Create a vec with negated elements.
158  GfVec2i operator-() const {
159  return GfVec2i(-_data[0], -_data[1]);
160  }
161 
162  /// Addition.
163  GfVec2i &operator+=(GfVec2i const &other) {
164  _data[0] += other[0];
165  _data[1] += other[1];
166  return *this;
167  }
168  friend GfVec2i operator+(GfVec2i const &l, GfVec2i const &r) {
169  return GfVec2i(l) += r;
170  }
171 
172  /// Subtraction.
173  GfVec2i &operator-=(GfVec2i const &other) {
174  _data[0] -= other[0];
175  _data[1] -= other[1];
176  return *this;
177  }
178  friend GfVec2i operator-(GfVec2i const &l, GfVec2i const &r) {
179  return GfVec2i(l) -= r;
180  }
181 
182  /// Multiplication by scalar.
183  GfVec2i &operator*=(double s) {
184  _data[0] *= s;
185  _data[1] *= s;
186  return *this;
187  }
188  GfVec2i operator*(double s) const {
189  return GfVec2i(*this) *= s;
190  }
191  friend GfVec2i operator*(double s, GfVec2i const &v) {
192  return v * s;
193  }
194 
195  /// Division by scalar.
197  _data[0] /= s;
198  _data[1] /= s;
199  return *this;
200  }
201  GfVec2i operator/(int s) const {
202  return GfVec2i(*this) /= s;
203  }
204 
205  /// See GfDot().
206  int operator*(GfVec2i const &v) const {
207  return _data[0] * v[0] + _data[1] * v[1];
208  }
209 
210  /// Returns the projection of \p this onto \p v. That is:
211  /// \code
212  /// v * (*this * v)
213  /// \endcode
214  GfVec2i GetProjection(GfVec2i const &v) const {
215  return v * (*this * v);
216  }
217 
218  /// Returns the orthogonal complement of \p this->GetProjection(b).
219  /// That is:
220  /// \code
221  /// *this - this->GetProjection(b)
222  /// \endcode
223  GfVec2i GetComplement(GfVec2i const &b) const {
224  return *this - this->GetProjection(b);
225  }
226 
227  /// Squared length.
228  int GetLengthSq() const {
229  return *this * *this;
230  }
231 
232 
233 private:
234  int _data[2];
235 };
236 
237 /// Output a GfVec2i.
238 /// \ingroup group_gf_DebuggingOutput
239 GF_API std::ostream& operator<<(std::ostream &, GfVec2i const &);
240 
241 
242 /// Returns component-wise multiplication of vectors \p v1 and \p v2.
243 inline GfVec2i
244 GfCompMult(GfVec2i const &v1, GfVec2i const &v2) {
245  return GfVec2i(
246  v1[0] * v2[0],
247  v1[1] * v2[1]
248  );
249 }
250 
251 /// Returns component-wise quotient of vectors \p v1 and \p v2.
252 inline GfVec2i
253 GfCompDiv(GfVec2i const &v1, GfVec2i const &v2) {
254  return GfVec2i(
255  v1[0] / v2[0],
256  v1[1] / v2[1]
257  );
258 }
259 
260 /// Returns the dot (inner) product of two vectors.
261 inline int
262 GfDot(GfVec2i const &v1, GfVec2i const &v2) {
263  return v1 * v2;
264 }
265 
266 
268 
269 #endif // PXR_BASE_GF_VEC2I_H
int const & operator[](size_t i) const
Indexing.
Definition: vec2i.h:129
friend GfVec2i operator+(GfVec2i const &l, GfVec2i const &r)
Definition: vec2i.h:168
GfVec2i GetProjection(GfVec2i const &v) const
Definition: vec2i.h:214
bool operator!=(GfVec2i const &other) const
Definition: vec2i.h:142
friend GfVec2i operator*(double s, GfVec2i const &v)
Definition: vec2i.h:191
Definition: vec2i.h:60
*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:623
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:126
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
**But if you need a result
Definition: thread.h:613
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
GF_API std::ostream & operator<<(std::ostream &, GfVec2i const &)
Definition: vec2d.h:62
bool operator==(GfVec2i const &other) const
Equality comparison.
Definition: vec2i.h:138
GfVec2i & operator*=(double s)
Multiplication by scalar.
Definition: vec2i.h:183
static GfVec2i XAxis()
Create a unit vector along the X-axis.
Definition: vec2i.h:90
int GetLengthSq() const
Squared length.
Definition: vec2i.h:228
GfVec2i & Set(int const *a)
Set all elements with a pointer to data.
Definition: vec2i.h:119
GfVec2i operator-() const
Create a vec with negated elements.
Definition: vec2i.h:158
GfVec2i operator/(int s) const
Definition: vec2i.h:201
Definition: vec2h.h:63
int GfDot(GfVec2i const &v1, GfVec2i const &v2)
Returns the dot (inner) product of two vectors.
Definition: vec2i.h:262
GfVec2i GfCompMult(GfVec2i const &v1, GfVec2i const &v2)
Returns component-wise multiplication of vectors v1 and v2.
Definition: vec2i.h:244
friend GfVec2i operator-(GfVec2i const &l, GfVec2i const &r)
Definition: vec2i.h:178
int const * data() const
Direct data access.
Definition: vec2i.h:124
GfVec2i & operator+=(GfVec2i const &other)
Addition.
Definition: vec2i.h:163
static GfVec2i Axis(size_t i)
Definition: vec2i.h:104
friend size_t hash_value(GfVec2i const &vec)
Hash.
Definition: vec2i.h:133
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
int ScalarType
Scalar element type and dimension.
Definition: vec2i.h:64
int operator*(GfVec2i const &v) const
See GfDot().
Definition: vec2i.h:206
GfVec2i & Set(int s0, int s1)
Set all elements with passed arguments.
Definition: vec2i.h:112
int * data()
Definition: vec2i.h:125
GfVec2i & operator/=(int s)
Division by scalar.
Definition: vec2i.h:196
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
static GfVec2i YAxis()
Create a unit vector along the Y-axis.
Definition: vec2i.h:96
GfVec2i GfCompDiv(GfVec2i const &v1, GfVec2i const &v2)
Returns component-wise quotient of vectors v1 and v2.
Definition: vec2i.h:253
Definition: vec2f.h:62
int & operator[](size_t i)
Definition: vec2i.h:130
GLfloat GLfloat v1
Definition: glcorearb.h:817
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
constexpr GfVec2i(int value)
Initialize all elements to a single value.
Definition: vec2i.h:71
GfVec2i GetComplement(GfVec2i const &b) const
Definition: vec2i.h:223
constexpr GfVec2i(Scl const *p)
Construct with pointer to values.
Definition: vec2i.h:84
Definition: core.h:1131
GfVec2i & operator-=(GfVec2i const &other)
Subtraction.
Definition: vec2i.h:173
GLboolean r
Definition: glcorearb.h:1222
GfVec2i operator*(double s) const
Definition: vec2i.h:188
#define GF_API
Definition: api.h:40
static const size_t dimension
Definition: vec2i.h:65
constexpr GfVec2i(int s0, int s1)
Initialize all elements with explicit arguments.
Definition: vec2i.h:77