Geometry.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef GEOMETRY_H_
32 #define GEOMETRY_H_
33 
34 #include <drain/UniTuple.h>
35 #include <iostream>
36 #include <vector>
37 
38 #include "drain/util/Frame.h"
39 
40 namespace drain
41 {
42 
43 namespace image
44 {
45 
46 
47 
48 class ChannelGeometry : public drain::UniTuple<size_t,2>
49 {
50 public:
51 
52  inline
53  ChannelGeometry(size_t images=1, size_t alphas=0) : imageChannels(next()), alphaChannels(next()){ //, channels(0) {
54  this->set(images, alphas);
55  };
56 
57  ChannelGeometry(const ChannelGeometry & g) : imageChannels(next()), alphaChannels(next()){ //, channels(0) {
58  this->set(g.tuple());
59  };
60 
61  template <size_t N>
62  ChannelGeometry(drain::UniTuple<size_t,N> & tuple, size_t i) :
63  drain::UniTuple<size_t,2>(tuple, i),
64  imageChannels(this->next()),
65  alphaChannels(this->next()){
66  //updateTuple();
67  };
68 
69  inline
70  virtual ~ChannelGeometry(){};
71 
72 
73  inline
74  ChannelGeometry & operator=(const ChannelGeometry & g){
75  set(1,0); // could be init!
76  assignSequence(g);
77  return *this;
78  }
79 
80  /*
81  template <class T>
82  inline
83  ChannelGeometry & operator=(const T & g){
84  set(1,0); // could be init!
85  assign(g);
86  return *this;
87  }
88  */
89 
90 
91  inline
92  void setChannelCount(const ChannelGeometry & g){
93  assignSequence(g);
94  }
95 
96  inline
97  void setChannelCount(size_t imageChannelCount, size_t alphaChannelCount = 0){
98  this->set(imageChannelCount, alphaChannelCount);
99  }
100 
101  inline
102  void setAlphaChannelCount(size_t a){
103  alphaChannels = a;
104  }
105 
107  inline
108  size_t getChannelCount() const {
109  return imageChannels + alphaChannels;
110  };
111 
112 
114  inline
115  size_t getImageChannelCount() const {
116  return imageChannels;
117  };
118 
120  inline
121  size_t getAlphaChannelCount() const {
122  return alphaChannels;
123  };
124 
125 protected:
126 
127  size_t & imageChannels;
128  size_t & alphaChannels;
129 
130 };
131 
132 
134 
135 
145 class Geometry : public drain::UniTuple<size_t,4> { //: public AreaGeometry, public ChannelGeometry {
146 
147 public:
148 
149  AreaGeometry area;
150 
151  ChannelGeometry channels;
152 
153  // condider:
154  // CoordinatePolicy policy?
155 
157 
159  Geometry(size_t width=0, size_t height=0, size_t channels=1, size_t alphas=0) : area(tuple(),0), channels(tuple(),2) {
160  set(width, height, channels, alphas);
161  //updateTuple();
162  }
163 
164  Geometry(const Geometry &g) : area(tuple(),0), channels(tuple(),2) {
165  assignSequence(g.tuple());
166  //updateTuple();
167  }
168 
169  //Geometry(const std::vector<int> &vector, int a = 0);
170 
171  virtual inline
172  ~Geometry(){};
173 
174  virtual inline // CHECK if virtual needed
175  const Geometry & getGeometry() const {
176  return *this;
177  }
178 
179  virtual inline // CHECK if virtual needed
180  Geometry & getGeometry() {
181  return *this;
182  }
183 
184  template <class T>
185  inline
186  void setGeometry(const T &g){
187  //Geometry & operator=(const T & g){
188  set(0,0,1,0); // could be init!
189  assignSequence(g.tuple());
190  //return *this;
191  }
192 
193  inline
194  void setGeometry(const Geometry &g){
195  //setGeometry(g.width, g.height, g.imageChannelCount, g.alphaChannelCount);
196  set(g);
197  }
198 
199  //bool
200  void setGeometry(size_t width, size_t height, size_t imageChannelCount = 1,size_t alphaChannelCount = 0){
201  set(width, height, imageChannelCount, alphaChannelCount);
202  }
203 
204  inline
205  operator const AreaGeometry &() const {
206  return area;
207  }
208 
209 
210  inline
211  size_t getWidth() const {
212  return area.getWidth();
213  };
214 
215  inline
216  size_t getHeight() const {
217  return area.getHeight();
218  };
219 
220  /*
221  template <class T>
222  inline
223  void setArea(const T & a){
224  area.set(a.tuple()); // 2024/09/??
225  };
226  */
227  // template <typename S, size_t N> TupleBase
228  template <typename S, size_t N>
229  inline
230  void setArea(const TupleBase<S,N> & a){
231  //void setArea(const AreaGeometry & a){
232  area.set(a); // 2024/09/??
233  };
234 
235 
236  inline
237  void setArea(size_t width, size_t height){
238  area.set(width, height);
239  };
240 
241  inline
242  size_t getArea() const {
243  return area.getArea();
244  };
245 
246 
248  // template <class T>
249  /*
250  virtual inline
251  void setGeometry(const Geometry & g){
252  geometry.setGeometry(g);
253  }
254  */
255 
256  inline
257  size_t getChannelCount() const {
258  return channels.getChannelCount(); // getGeometry unneeded?
259  };
260 
261  inline
262  void setChannelCount(size_t imageChannels, size_t alphaChannels){
263  channels.set(imageChannels, alphaChannels);
264  };
265 
266  inline
267  void setChannelCount(const ChannelGeometry & geom){
268  channels.setChannelCount(geom);
269  };
270 
271  inline
272  const size_t getImageChannelCount() const {
273  return channels.getImageChannelCount();
274  };
275 
276  inline
277  void setImageChannelCount(size_t imageChannels){
278  channels.set(imageChannels);
279  };
280 
281  inline
282  const size_t getAlphaChannelCount() const {
283  return channels.getAlphaChannelCount();
284  };
285 
286  inline
287  void setAlphaChannelCount(size_t alphaChannels){
288  channels.setAlphaChannelCount(alphaChannels);
289  };
290 
291 
292  inline
293  bool hasAlphaChannel() const {
294  return channels.getAlphaChannelCount() > 0;
295  };
296 
298 
301  size_t getChannelIndex(const std::string & index) const;
302 
303 
304 
305  inline
306  size_t getVolume() const {
307  return area.getArea() * channels.getChannelCount();
308  };
309 
310  inline
311  bool isEmpty() const {
312  return (getVolume() == 0);
313  };
314 
315 
316 
317  // to-be-protected? :
318  /*
319  inline
320  Geometry & operator=(const Geometry &g){
321  assign(g);
322  return *this;
323  }
324 
325  // needed?
326  template <class T>
327  inline
328  Geometry & operator=(const std::vector<T> &v){
329  assign(v);
330  return *this;
331  }
332  */
333 
334  //inline
335  //Geometry & operator=(const Geometry &g);
336  inline
337  bool operator==(const Geometry &g) const {
338  return (tuple() == g.tuple());
339  /*
340  return ((width==g.getWidth()) && (height==g.getHeight())
341  && (imageChannelCount==g.getImageChannelCount()) && (alphaChannelCount==g.getAlphaChannelCount()));
342  */
343  };
344 
345  inline
346  bool operator!=(const Geometry &g) const {
347  return !((*this)==g);
348  };
349 
350  /*
351  inline
352  void toStream(std::ostream &ostr) const {
353  AreaGeometry::toStream(ostr);
354  if (getChannelCount() != 1){
355  ostr << "×";
356  ChannelGeometry::toOStr(ostr);
357  }
358  }
359  */
360 
361  /*
362  inline
363  std::string toString(std::string & s) const {
364  return this->tuple().toStr();
365  }
366  */
367 
368 
369 protected:
370 
372  /*
373  virtual inline
374  void updateTuple(){
375  area.updateTuple();
376  channels.updateTuple();
377  }
378  */
379 
380  //size_t volume;
381 
382 
383 };
384 
385 
386 
388 /*
389 template <class T>
390 Geometry & Geometry::operator=(const std::vector<T> &v){
391  int d = v.size();
392  imageChannelCount = 1;
393  alphaChannelCount = 0;
394  switch (d) {
395  case 4:
396  alphaChannelCount = v[3];
397  break;
398  case 3:
399  imageChannelCount = v[2];
400  break;
401  case 2:
402  this->height = v[1];
403  break;
404  case 1:
405  this->width = v[0];
406  break;
407  default:
408  break;
409  }
410  channelCount = imageChannelCount + alphaChannelCount;
411  update();
412  return (*this);
413 }
414  */
415 
416 /*
417 inline
418 std::ostream & operator<<(std::ostream &ostr, const Geometry &geometry){
419  geometry.toOStr(ostr);
420  return ostr;
421 }
422 */
423 
424 
425 }
426 
427 }
428 
429 #endif /*GEOMETRY_H_*/
430 
431 // Drain
tuplebase_t & assignSequence(T &sequence, bool LENIENT=false)
Proposed for tuples only; derived classes should not shadow this.
Definition: TupleBase.h:244
Tuple of N elements of type T.
Definition: UniTuple.h:65
Definition: Geometry.h:49
size_t getAlphaChannelCount() const
Return the number of alpha channels.
Definition: Geometry.h:121
size_t getImageChannelCount() const
Return the number of actual image channels (excluding alphas)
Definition: Geometry.h:115
size_t getChannelCount() const
Return the number of channels (image and alpha)
Definition: Geometry.h:108
Definition: Geometry.h:145
size_t getChannelIndex(const std::string &index) const
Returns numeric channel index from "r", "g", "b", or "a" or a non-negative number in string format.
Definition: Geometry.cpp:45
Geometry(size_t width=0, size_t height=0, size_t channels=1, size_t alphas=0)
Constructor with dimensions. Channel count is one by default, allowing construction with width and he...
Definition: Geometry.h:159
size_t getChannelCount() const
Set...
Definition: Geometry.h:257
Definition: DataSelector.cpp:1277