Loading...
Searching...
No Matches
Alignment.h
1/*
2
3MIT License
4
5Copyright (c) 2023 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31/*
32 * TreeSVG.h
33 *
34 * Created on: Nov, 2024
35 * Author: mpeura
36 */
37
38#ifndef DRAIN_ALIGNMENT1
39#define DRAIN_ALIGNMENT1
40
41#include <drain/Enum.h>
42#include <string>
43
44namespace drain {
45
46namespace image {
47
48
50struct AlignBase {
51
52public:
53
55 enum Pos {
56 UNDEFINED_POS = 0,
57 MIN = 1, // LEFT (if HORZ) or TOP (when VERT) 0b01010101,
58 MAX = 2, // RIGHT (if HORZ) or BOTTOM (when VERT) 0b10101010,
59 MID = 3, // 0b11111111,
60 FILL = 4,
61 // ABSOLUTE?
62 };
63
64 enum Axis {
65 // Index, must contain 0 and 1 for HORZ and VERT
66 HORZ = 0, // 0b00110011,
67 VERT = 1, // b11001100,
68 UNDEFINED_AXIS = 2,
69 };
70
71
73 /*
74 enum Axis {
75 // Index, must contain 0 and 1 for HORZ and VERT
76 UNDEFINED_AXIS = 0,
77 HORZ = 1, // 0b00110011,
78 VERT = 2, // b11001100,
79 };
80 */
81
82 static inline
83 Axis flip(Axis axis){
84 switch (axis){
85 case HORZ:
86 return VERT;
87 case VERT:
88 return HORZ;
89 default:
90 return UNDEFINED_AXIS;
91 }
92 };
93
94 //typedef drain::Enum<Coord>::dict_t pos_dict_t;
95
96 static inline
98 switch (pos){
99 case MAX:
100 return MIN;
101 case MIN:
102 return MAX;
103 case MID:
104 case FILL:
105 case UNDEFINED_POS:
106 default:
107 return pos;
108 }
109 };
110
111};
112
114
125template <typename AX = AlignBase::Axis, AlignBase::Axis A = AlignBase::Axis::UNDEFINED_AXIS> //, typename POS = AlignBase::Pos> // , Align::Coord POS = Align::Coord::UNDEFINED_POS>
126struct Alignment {
127
128 // Align::Axis
129 AX axis; // = V; // compiler error if different type?
130
131 //AlignBase::Pos pos
132 AlignBase::Pos pos = AlignBase::Pos::UNDEFINED_POS; // or middle?
133 //POS pos = AlignBase::Pos::UNDEFINED_POS; // or middle?
134
136 inline
137 Alignment(AlignBase::Pos pos = AlignBase::Pos::UNDEFINED_POS) : axis(A), pos(pos){
138 }
139
141 inline
142 Alignment(const Alignment & ac) : axis(ac.axis), pos(ac.pos){
143 }
144
145 Alignment & operator=(const Alignment &) = default;
146
147
148 inline
149 Alignment(AlignBase::Axis axis, AlignBase::Pos pos = AlignBase::Pos::UNDEFINED_POS): axis(axis), pos(pos){
150 }
151
152 template <typename AX2, AlignBase::Axis A2>
153 inline
154 Alignment(const Alignment<AX2,A2> & align) : pos(align.pos){ // axis(ac.axis),
155 axis = align.axis; // error if const
156 }
157
159 inline virtual
161
162
163 inline
164 const AlignBase::Axis & get(const AlignBase::Axis & defaultValue) const {
165 if (axis != AlignBase::Axis::UNDEFINED_AXIS){
166 return axis;
167 }
168 else {
169 return defaultValue;
170 }
171 }
172
173 inline
174 const AlignBase::Pos & get(const AlignBase::Pos & defaultValue) const {
175 if (pos != AlignBase::Pos::UNDEFINED_POS){
176 return pos;
177 }
178 else {
179 return defaultValue;
180 }
181 }
182
183
184 virtual inline
185 void reset(){
186 axis = AlignBase::Axis::UNDEFINED_AXIS;
187 pos = AlignBase::Pos::UNDEFINED_POS;
188 }
189
190
191 template <typename AX2, AlignBase::Axis A2>
192 inline
193 bool operator==(const Alignment<AX2,A2> & align) const {
194 return (align.axis == axis) && (align.pos == pos);
195 // return compare(ad) == 0;
196 }
197
198 /*
199 inline
200 Alignment<AX,A> & operator=(Alignment<AX,A> & align){
201 // axis = align.axis;
202 pos = align.pos;
203 return *this;
204 }
205 */
206
207
208
209
210};
211
212
213
214
216
217 enum Topol {
218 INSIDE = 0,
219 OUTSIDE = 1,
220 UNDEFINED_TOPOL = 2,
221 };
222
223
224};
225
226
228
244template <typename AX = AlignBase::Axis, AlignBase::Axis A = AlignBase::Axis::UNDEFINED_AXIS> // , Align::Coord POS = Align::Coord::UNDEFINED_POS>
245struct CompleteAlignment : public Alignment<AX,A> {
246
247 MutualAlign::Topol topol = MutualAlign::Topol::INSIDE; // or undef?
248
250 template <class ...TT>
251 CompleteAlignment(const TT... args) : Alignment<AX,A>() {
252 set(args...);
253 }
254
255 inline
257
258 virtual inline
259 bool isSet() const {
260 return (this->axis != AlignBase::Axis::UNDEFINED_AXIS) &&
261 (this->pos != AlignBase::Pos::UNDEFINED_POS) &&
262 (topol != MutualAlign::Topol::UNDEFINED_TOPOL);
263 // return Alignment<AX,A>::isSet() && (topol != MutualAlign::Topol::UNDEFINED_TOPOL);
264 }
265
266 inline
267 const MutualAlign::Topol & getOrDefault(const MutualAlign::Topol & defaultValue) const {
268 if (topol != MutualAlign::Topol::UNDEFINED_TOPOL){
269 return topol;
270 }
271 else {
272 return defaultValue;
273 }
274 }
275
276
277 // Sets all members to UNDEFINED state.
278 virtual inline
279 void reset(){
280 Alignment<AX,A>::reset();
281 topol = MutualAlign::Topol::UNDEFINED_TOPOL;
282 // this->updateAlign();
283 }
284
286
289 // template <typename AX2, AlignBase::Axis A2, class ...TT>
290 //void set(const Alignment<const AX2,A2> & align, const TT... args){
291 template <class ...TT>
292 void set(const Alignment<AX,A> & align, const TT... args){
293 // this->axis = align.axis;
294 this->pos = align.pos;
295 set(args...);
296 }
297
299 template <typename AX2, AlignBase::Axis A2, class ...TT>
300 void set(const Alignment<AX2,A2> & align, const TT... args){
301 this->axis = align.axis;
302 this->pos = align.pos;
303 set(args...);
304 }
305
306
307
308 template <class ...TT>
309 void set(MutualAlign::Topol topol, const TT... args){
310 this->topol = topol;
311 set(args...);
312 }
313
314 template <class ...TT>
315 void set(AlignBase::Axis axis, const TT... args){
316 this->axis = axis;
317 set(args...);
318 }
319
320 template <class ...TT>
321 void set(AlignBase::Pos coord, const TT... args){
322 this->pos = coord;
323 set(args...);
324 }
325
326 template <class ...TT>
327 void set(const std::string & key, const TT... args){
328 if (Enum<MutualAlign::Topol>::setValue(key, topol)){
329 // ok
330 }
331 else if (Enum<Alignment<> >::setValue(key, *this)){ // RIGHT or?
332 // ok
333 }
334 else if (Enum<AlignBase::Axis>::setValue(key, this->axis)){
335 // ok
336 }
337 else if (Enum<AlignBase::Pos>::setValue(key, this->pos)){
338 // ok
339 }
340 else {
341 // Advice: keys
342 throw std::runtime_error(drain::StringBuilder<>("key '", key, "' not found. Appeared in: ", args...));
343 }
344
345 set(args...);
346 }
347
348 template <class ...TT>
349 void set(const char *key, const TT... args){
350 set(std::string(key), args...);
351 }
352
353
354protected:
355
356 inline
357 void set(){
358 // this->updateAlign(); // ok?
359 }
360
361};
362
363
364typedef CompleteAlignment<const AlignBase::Axis, AlignBase::Axis::HORZ> CompleteHorzAlign;
365typedef CompleteAlignment<const AlignBase::Axis, AlignBase::Axis::VERT> CompleteVertAlign;
366
367
368} // image::
369
370DRAIN_ENUM_DICT(image::AlignBase::Axis);
371DRAIN_ENUM_DICT(image::AlignBase::Pos);
372//DRAIN_ENUM_DICT(image::Alignment<>);
373DRAIN_ENUM_DICT(image::MutualAlign::Topol);
374
375DRAIN_ENUM_OSTREAM(drain::image::AlignBase::Axis)
376DRAIN_ENUM_OSTREAM(drain::image::AlignBase::Pos)
377
378
379template <typename AX, drain::image::AlignBase::Axis A>
380inline
381std::ostream & operator<<(std::ostream &ostr, const drain::image::Alignment<AX,A> & align){
382 //return ostr << align.axis << '_' << align.pos; // enums resolved above
383 return ostr << align.axis << '_' << align.pos;
384}
385
386
387//DRAIN_ENUM_DICT(drain::image::Alignment<>);
388DRAIN_ENUM_OSTREAM(drain::image::MutualAlign::Topol)
389
390template <typename AX, drain::image::AlignBase::Axis A>
391std::ostream & operator<<(std::ostream &ostr, const drain::image::CompleteAlignment<AX,A> & ad){
392 return ostr << ad.topol << '_' << ad.axis << ':' << ad.pos;
393}
394
395} // drain::
396
397
398
399
400// DRAIN_ENUM_OSTREAM(drain::image::Alignment<>);
401
402// DRAIN_ENUM_OSTREAM(drain::image::AlignBase::Axis);
403// DRAIN_ENUM_OSTREAM(drain::image::AlignBase::Pos);
404
405
406
407#endif // DRAIN_ALIGNMENT
408
Definition StringBuilder.h:58
Definition Data.h:1473
std::ostream & operator<<(std::ostream &ostr, const PlainData< DT > &d)
Definition Data.h:631
static bool setValue(const std::string &key, E &value)
Assign string values to an enumeration type.
Definition Enum.h:78
Low level alignment instructions.
Definition Alignment.h:50
static Axis flip(Axis axis)
supporting simultaneous HORZ|VERT
Definition Alignment.h:83
Pos
Reference position at Axis (HORZ or VERT)
Definition Alignment.h:55
Container for Axis and Pos.
Definition Alignment.h:126
Alignment(const Alignment &ac)
Copy constructor.
Definition Alignment.h:142
Alignment(AlignBase::Pos pos=AlignBase::Pos::UNDEFINED_POS)
Default constructor.
Definition Alignment.h:137
virtual ~Alignment()
Destructor.
Definition Alignment.h:160
"Alternative" partial alignment configuration for single object. Partial means that either OBJECT its...
Definition Alignment.h:245
void set(const Alignment< AX2, A2 > &align, const TT... args)
Variant for non-const axis AX2.
Definition Alignment.h:300
void set(const Alignment< AX, A > &align, const TT... args)
Variant for same-AX-type, HORZ or VERT.
Definition Alignment.h:292
CompleteAlignment(const TT... args)
Constructor not setting Axis.
Definition Alignment.h:251
Definition Alignment.h:215