Ilwis-Objects  1.0
GIS and Remote Sensing framework for data access and processing
 All Classes Functions Enumerations Pages
size.h
1 #ifndef SIZE_H
2 #define SIZE_H
3 
4 #include <QSize>
5 #include "Kernel_global.h"
6 
7 namespace Ilwis {
8 
14 template <typename T=quint32> class KERNELSHARED_EXPORT Size
15 {
16 public:
20  Size() : _xsize(0), _ysize(0), _zsize(0) {}
27  Size(T x, T y, T z) : _xsize(x), _ysize(y), _zsize(z) {}
32  Size(const Size& sz) : _xsize(sz.xsize()), _ysize(sz.ysize()), _zsize(sz.zsize()) {}
37  Size(const QSize& sz) : _xsize(sz.width()), _ysize(sz.height()), _zsize(1) {}
43  Size& operator+=(const Size& sz){
44  if ( !isValid() ) {
45  *this = Size();
46  return *this;
47  }
48 
49  _xsize += sz.xsize();
50  _ysize += sz.ysize();
51  _zsize += sz.zsize();
52  check();
53  return *this;
54  }
55 
61  Size& operator-=(const Size& sz){
62  if ( !isValid() ) {
63  *this = Size();
64  return *this;
65  }
66 
67  _xsize -= sz.xsize();
68  _ysize -= sz.ysize();
69  _zsize -= sz.zsize();
70  check();
71  return *this;
72  }
73 
79  Size& operator*=(double f){
80  if ( !isValid() || f == rUNDEF || f < 0) {
81  *this = Size();
82  return *this;
83  }
84 
85  _xsize *= f;
86  _ysize *= f;
87  _zsize *= f;
88  check();
89  return *this;
90  }
91 
96  QSize toQSize() const{
97  return QSize(xsize(), ysize());
98  }
103  T xsize() const{
104  return _xsize;
105  }
110  T ysize() const{
111  return _ysize;
112  }
117  T zsize() const{
118  return _zsize;
119  }
124  void xsize(T x){
125  _xsize = x;
126  }
127 
132  void ysize(T y){
133  _ysize = y;
134  }
135 
140  void zsize(T z){
141  _zsize = z;
142  }
143 
149  quint64 linearSize() const{
150  if (!isValid())
151  return 0;
152  return (quint64)xsize() * (quint64)ysize() * (quint64)zsize();
153  }
154 
155  bool contains(T x, T y, T z=0) const{
156  return x <= xsize() && y <= ysize() && z <= zsize();
157  }
158 
159  bool isValid() const{
160  if ( isNumericalUndef(_xsize) || isNumericalUndef(_ysize) || isNumericalUndef(_zsize)) // should not be possible but better safe then sorry
161  return false;
162 
163  return _xsize >0 && _ysize > 0 && _zsize > 0;
164  }
169  bool isNull() const{
170  return _xsize == 0 && _ysize == 0 && _zsize == 0;
171  }
172 private:
173  void check(){
174  {
175  if ( _xsize < 0)
176  xsize(0);
177  if ( _ysize < 0)
178  ysize(0);
179  if (_zsize < 0)
180  zsize(0);
181  }
182  }
183 
184  T _xsize;
185  T _ysize;
186  T _zsize;
187 
188 };
189 
190 //bool operator==(const Size& sz1, const Size& sz2) {
191 // return sz1.xsize() == sz2.xsize() && sz1.ysize() == sz2.ysize() && sz1.zsize() == sz2.zsize();
192 //}
193 
194 //bool operator!=(const Size& sz1, const Size& sz2) {
195 // return ! operator==(sz1, sz2);
196 //}
197 
198 template<typename T> Ilwis::Size<T> operator+(const Ilwis::Size<T>& sz1, const Ilwis::Size<T>& sz2){
199  if((!sz1.isNull() && !sz1.isValid()) || (!sz2.isNull() && !sz2.isValid())){
200  return Size<T>();
201  }
202  return Size<T>(sz1.xsize() + sz2.xsize(), sz1.ysize() + sz2.ysize(), sz1.zsize() + sz2.zsize());
203 }
204 
205 template<typename T> Ilwis::Size<T> operator-(const Ilwis::Size<T>& sz1, const Ilwis::Size<T>& sz2){
206  if((!sz1.isNull() && !sz1.isValid()) || (!sz2.isNull() && !sz2.isValid())){
207  return Size<T>();
208  }
209 
210  return Size<T>(sz1.xsize() - sz2.xsize(), sz1.ysize() - sz2.ysize(), sz1.zsize() - sz2.zsize());
211 }
212 
213 template<typename T> bool operator==(const Ilwis::Size<T>& sz1, const Ilwis::Size<T>& sz2){
214  if ( !sz1.isValid() && !sz2.isValid())
215  return true;
216 
217  return sz1.xsize() == sz2.xsize() && sz1.ysize() == sz2.ysize() && sz1.zsize() == sz2.zsize();
218 }
219 
220 template<typename T> bool operator!=(const Ilwis::Size<T>& sz1, const Ilwis::Size<T>& sz2){
221  return !operator ==(sz1, sz2);
222 }
223 }
224 Q_DECLARE_METATYPE(Ilwis::Size<quint32>)
225 Q_DECLARE_METATYPE(Ilwis::Size<double>)
226 
227 #endif // SIZE_H