HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
size2.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 #ifndef PXR_BASE_GF_SIZE2_H
8 #define PXR_BASE_GF_SIZE2_H
9 
10 /// \file gf/size2.h
11 /// \ingroup group_gf_LinearAlgebra
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/arch/inttypes.h"
15 #include "pxr/base/gf/vec2i.h"
16 #include "pxr/base/gf/api.h"
17 
18 #include <iosfwd>
19 
21 
22 /// \class GfSize2
23 /// \ingroup group_gf_LinearAlgebra
24 ///
25 /// Two-dimensional array of sizes
26 ///
27 /// GfSize2 is used to represent pairs of counts. It is based on the datatype
28 /// size_t, and thus can only represent non-negative values in each dimension.
29 /// If you need to represent negative numbers as well, use GfVec2i.
30 ///
31 /// Usage of GfSize2 is similar to that of GfVec2i, except that all
32 /// mathematical operations are componentwise (including multiplication).
33 ///
34 class GfSize2 {
35 public:
36  /// Default constructor initializes components to zero.
37  GfSize2() {
38  Set(0, 0);
39  }
40 
41  /// Copy constructor.
42  GfSize2(const GfSize2& o) {
43  *this = o;
44  }
45 
46  /// Conversion from GfVec2i.
47  explicit GfSize2(const GfVec2i&o) {
48  Set(o[0], o[1]);
49  }
50 
51  /// Construct from an array.
52  GfSize2(const size_t v[2]) {
53  Set(v);
54  }
55 
56  /// Construct from two values.
57  GfSize2(size_t v0, size_t v1) {
58  Set(v0, v1);
59  }
60 
61  /// Set to the values in a given array.
62  GfSize2 & Set(const size_t v[2]) {
63  _vec[0] = v[0];
64  _vec[1] = v[1];
65  return *this;
66  }
67 
68  /// Set to values passed directly.
69  GfSize2 & Set(size_t v0, size_t v1) {
70  _vec[0] = v0;
71  _vec[1] = v1;
72  return *this;
73  }
74 
75  /// Array operator.
76  size_t & operator [](size_t i) {
77  return _vec[i];
78  }
79 
80  /// Const array operator.
81  const size_t & operator [](size_t i) const {
82  return _vec[i];
83  }
84 
85  /// Component-wise equality.
86  bool operator ==(const GfSize2 &v) const {
87  return _vec[0] == v._vec[0] && _vec[1] == v._vec[1];
88  }
89 
90  /// Component-wise inequality.
91  bool operator !=(const GfSize2 &v) const {
92  return ! (*this == v);
93  }
94 
95  /// Component-wise in-place addition.
97  _vec[0] += v._vec[0];
98  _vec[1] += v._vec[1];
99  return *this;
100  }
101 
102  /// Component-wise in-place subtraction.
104  _vec[0] -= v._vec[0];
105  _vec[1] -= v._vec[1];
106  return *this;
107  }
108 
109  /// Component-wise in-place multiplication.
111  _vec[0] *= v._vec[0];
112  _vec[1] *= v._vec[1];
113  return *this;
114  }
115 
116  /// Component-wise in-place multiplication by a scalar.
117  GfSize2 & operator *=(int d) {
118  _vec[0] = _vec[0] * d;
119  _vec[1] = _vec[1] * d;
120  return *this;
121  }
122 
123  /// Component-wise in-place division by a scalar.
124  GfSize2 & operator /=(int d) {
125  _vec[0] = _vec[0] / d;
126  _vec[1] = _vec[1] / d;
127  return *this;
128  }
129 
130  /// Component-wise addition.
131  friend GfSize2 operator +(const GfSize2 &v1, const GfSize2 &v2) {
132  return GfSize2(v1._vec[0]+v2._vec[0],
133  v1._vec[1]+v2._vec[1]);
134  }
135 
136  /// Component-wise subtraction.
137  friend GfSize2 operator -(const GfSize2 &v1, const GfSize2 &v2) {
138  return GfSize2(v1._vec[0]-v2._vec[0],
139  v1._vec[1]-v2._vec[1]);
140  }
141 
142  /// Component-wise multiplication.
143  friend GfSize2 operator *(const GfSize2 &v1, const GfSize2 &v2) {
144  return GfSize2(v1._vec[0]*v2._vec[0],
145  v1._vec[1]*v2._vec[1]);
146  }
147 
148  /// Component-wise multiplication by a scalar.
149  friend GfSize2 operator *(const GfSize2 &v1, int s) {
150  return GfSize2(v1._vec[0]*s,
151  v1._vec[1]*s);
152  }
153 
154  /// Component-wise multiplication by a scalar.
155  friend GfSize2 operator *(int s, const GfSize2 &v1) {
156  return GfSize2(v1._vec[0]*s,
157  v1._vec[1]*s);
158  }
159 
160  /// Component-wise division by a scalar.
161  friend GfSize2 operator /(const GfSize2 &v1, int s) {
162  return GfSize2(v1._vec[0]/s,
163  v1._vec[1]/s);
164  }
165 
166  /// Output operator.
167  GF_API
168  friend std::ostream &operator<<(std::ostream &o, GfSize2 const &v);
169 
170  /// Conversion to GfVec2i.
171  operator GfVec2i() const {
172  return GfVec2i(_vec[0], _vec[1]);
173  }
174  private:
175  size_t _vec[2];
176 };
177 
178 // Friend functions must be declared
179 GF_API std::ostream &operator<<(std::ostream &o, GfSize2 const &v);
180 
182 
183 #endif // PXR_BASE_GF_SIZE2_H
Definition: vec2i.h:43
const GLdouble * v
Definition: glcorearb.h:837
GfSize2(const GfSize2 &o)
Copy constructor.
Definition: size2.h:42
GfSize2(const GfVec2i &o)
Conversion from GfVec2i.
Definition: size2.h:47
GLdouble s
Definition: glad.h:3009
GfSize2 & Set(const size_t v[2])
Set to the values in a given array.
Definition: size2.h:62
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
size_t & operator[](size_t i)
Array operator.
Definition: size2.h:76
bool operator==(const GfSize2 &v) const
Component-wise equality.
Definition: size2.h:86
friend GfSize2 operator*(const GfSize2 &v1, const GfSize2 &v2)
Component-wise multiplication.
Definition: size2.h:143
GF_API std::ostream & operator<<(std::ostream &o, GfSize2 const &v)
GfSize2(size_t v0, size_t v1)
Construct from two values.
Definition: size2.h:57
bool operator!=(const GfSize2 &v) const
Component-wise inequality.
Definition: size2.h:91
friend GfSize2 operator/(const GfSize2 &v1, int s)
Component-wise division by a scalar.
Definition: size2.h:161
friend GfSize2 operator-(const GfSize2 &v1, const GfSize2 &v2)
Component-wise subtraction.
Definition: size2.h:137
GLfloat v0
Definition: glcorearb.h:816
GfSize2(const size_t v[2])
Construct from an array.
Definition: size2.h:52
GF_API friend std::ostream & operator<<(std::ostream &o, GfSize2 const &v)
Output operator.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
GfSize2 & operator*=(GfSize2 const &v)
Component-wise in-place multiplication.
Definition: size2.h:110
GfSize2 & operator-=(const GfSize2 &v)
Component-wise in-place subtraction.
Definition: size2.h:103
GfSize2 & operator+=(const GfSize2 &v)
Component-wise in-place addition.
Definition: size2.h:96
Definition: size2.h:34
GLfloat GLfloat v1
Definition: glcorearb.h:817
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
friend GfSize2 operator+(const GfSize2 &v1, const GfSize2 &v2)
Component-wise addition.
Definition: size2.h:131
GfSize2()
Default constructor initializes components to zero.
Definition: size2.h:37
GfSize2 & operator/=(int d)
Component-wise in-place division by a scalar.
Definition: size2.h:124
#define GF_API
Definition: api.h:23
GfSize2 & Set(size_t v0, size_t v1)
Set to values passed directly.
Definition: size2.h:69