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