Frame.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 
32 #ifndef DRAIN_FRAME_H_
33 #define DRAIN_FRAME_H_
34 
35 #include <ostream>
36 #include <stdexcept>
37 
38 #include <drain/Log.h>
39 #include <drain/UniTuple.h>
40 #include <drain/Type.h>
41 #include <drain/util/Point.h> // for Box
42 //#include "drain/util/Range.h"
43 
44 
45 namespace drain {
46 
47 
48 template <class T>
49 class Frame2D : public drain::UniTuple<T,2> { //: protected AreaGeometryStruct {
50 
51 public:
52 
53  T & width;
54  T & height;
55 
56  inline
57  Frame2D(T width=0, T height=0) : width(this->next()), height(this->next()){ //, area(0){
58  this->set(width, height?height:width);
59  };
60 
61  Frame2D(const Frame2D<T> & geometry) : width(this->next()), height(this->next()) {
62  //this->set(geometry.tuple());
63  this->set(geometry.width, geometry.height);
64  }
65 
66  // Reference, N>=2
67  template <size_t N>
68  //template <T N>
69  Frame2D(drain::UniTuple<T,N> & tuple, T i) :
70  drain::UniTuple<T,2>(tuple, i),
71  width(this->next()),
72  height(this->next()){
73  //updateTuple();
74  };
75 
76  virtual ~Frame2D(){};
77 
78 
79  Frame2D & operator=(const Frame2D & geometry){
80  this->set(geometry.width, geometry.height);
81  //this->assign(geometry);
82  return *this;
83  }
84 
85 
86  template <class T2>
87  inline
88  Frame2D & operator=(const T2 & frame){
89  this->set(0,0); // could be init!
90  this->assign(frame);
91  return *this;
92  }
93 
94 
95  inline
96  void setWidth(T w){
97  width = w;
98  // updateTuple();
99  }
100 
101  inline
102  void setHeight(T h){
103  height = h;
104  // updateTuple();
105  }
106 
107 
108  inline
109  T getWidth() const {
110  return width;
111  };
112 
113  inline
114  T getHeight() const {
115  return height;
116  };
117 
118  inline
119  T getArea() const {
120  return width*height;
121  };
122 
123  inline
124  bool empty() const {
125  return (width==0) || (height==0);
126  };
127 
128 
129 
130 };
131 
132 DRAIN_TYPENAME_T(Frame2D, T);
133 
135 
138 template <class T>
139 struct Box : public drain::Point2D<T>, public drain::Frame2D<T> {
140 
141 public:
142 
143  inline
144  Box(T x=0, T y=0, T width=0, T height=0) : drain::Point2D<T>(x, y), drain::Frame2D<T>(width, height) {
145  }
146 
147  inline
148  Box(const Box & box) : drain::Point2D<T>(box), drain::Frame2D<T>(box) {
149  }
150 
151 };
152 
153 DRAIN_TYPENAME_T(Box, T);
154 
155 
156 template <class T>
157 std::ostream &operator<<(std::ostream &ostr,const drain::Box<T> &box)
158 {
159  ostr << (const drain::Point2D<T> &)box << ' ' << (const drain::Frame2D<T> &)box; // [' << p.x << ',' << p.y << ',' << p.z << ']';
160  return ostr;
161 }
162 
163 } // drain
164 
165 #endif
Definition: Frame.h:49
Tuple of N elements of type T.
Definition: UniTuple.h:65
Definition: DataSelector.cpp:1277
Something that has coordinates (x,y) and dimensions (width, height).
Definition: Frame.h:139
Definition: Point.h:48