Direction.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 DRAIN_DIRECTION_H
32 #define DRAIN_DIRECTION_H
33 
34 #include <map>
35 #include <drain/PseudoTuple.h>
36 #include <drain/util/Dictionary.h>
37 
38 namespace drain
39 {
40 namespace image
41 {
42 
43 
44 
45 // Faster than Point2D (with UniTuple init)
46 struct Position {
47 
48  typedef int value_t;
49  value_t i=0;
50  value_t j=0;
51 
52  inline
53  Position(int i=0, int j=0) : i(i), j(j) {
54  };
55 
56  inline
57  Position(const Position & pos){
58  *this = pos;
59  }
60 
61  inline
62  Position & operator=(const Position & pos){
63  i = pos.i;
64  j = pos.j;
65  return *this;
66  }
67 
68  inline
69  bool operator==(const Position & pos) const {
70  return ((i == pos.i) && (j == pos.j));
71  }
72 
73  inline
74  bool operator!=(const Position & pos) const {
75  return ((i != pos.i) || (j != pos.j));
76  }
77 
78  inline
79  Position & add(const Position & offset){
80  i += offset.i;
81  j += offset.j;
82  return *this;
83  }
84 
85 
86 };
87 
89 
90 // Keep for future templated methods.
91 /*
92 #define DIR_TURN_090(dir) (((dir << 2) & 0xff) | (dir >> 6))
93 #define DIR_TURN_180(dir) (((dir << 4) & 0xff) | (dir >> 4))
94 #define DIR_TURN_270(dir) (((dir << 6) & 0xff) | (dir >> 2))
95 */
96 #define DIR_TURN_DEG(dir, deg) (((dir << (deg/45)) & 0xff) | (dir >> (8 - deg/45)))
97 
98 
99 struct Direction {
100 
101  typedef short int value_t;
102 
103  static const value_t NONE = 0;
104  static const value_t UP=1;
105  static const value_t UP_RIGHT=2;
106  static const value_t RIGHT=4;
107  static const value_t DOWN_RIGHT=8;
108  static const value_t DOWN=16;
109  static const value_t DOWN_LEFT=32;
110  static const value_t LEFT=64;
111  static const value_t UP_LEFT=128;
112 
113  static const value_t RECTANGULAR = UP | RIGHT | DOWN | LEFT;
114  static const value_t DIAGONAL = UP_RIGHT | DOWN_RIGHT | DOWN_LEFT | UP_LEFT;
115 
116  static
117  bool isDiagonal(value_t dir){
118  return (dir & DIAGONAL) > 0;
119  }
120 
121 
122  // https://www.w3.org/TR/xml-entity-names/025.html
123  // static
125 
126  static const dict_t arrows;
127 
129 
134 
137  /*
138  static inline
139  value_t turn90(value_t d){
140  return ((d << 2)&0xff) | (d >> 6);
141  }
142 
143  static inline
144  value_t turn180(value_t d){
145  return ((d << 4)&0xff) | (d >> 4);
146  }
147 
148  static inline
149  value_t turn270(value_t d){
150  return ((d << 6)&0xff) | (d >> 2);
151  }
152  */
153 
154  /*
155  template <dir DIR>
156  static inline
157  dir dir90minus(){
158  return (DIR >> 2) | ((DIR << 6)&0xff);
159  }
160  */
161 
163 
172  static
173  const std::map<value_t,Position> offset;
174 
175 };
176 
177 inline
178 std::ostream &operator<<(std::ostream &ostr, const Position & p){
179  ostr << '[' << p.i << ',' << p.j << ']';
180  return ostr;
181 }
182 
183 } // image::
184 
185 } // drain::
186 
187 #endif
188 
189 
190 
Two-way mapping between strings and objects of template class T.
Definition: Dictionary.h:63
Definition: PseudoTuple.h:60
Definition: DataSelector.cpp:1277
Definition: Direction.h:99
static const std::map< value_t, Position > offset
Opposite direction.
Definition: Direction.h:173
Definition: Direction.h:46