Location: LEMON/LEMON-official/lemon/dim2.h

Load file history
gravatar
deba@inf.elte.hu
Use proper traits class in visitor based algorithms
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/* -*- mode: C++; indent-tabs-mode: nil; -*-
*
* This file is a part of LEMON, a generic C++ optimization library.
*
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
*
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
*
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
* purpose.
*
*/
#ifndef LEMON_DIM2_H
#define LEMON_DIM2_H
#include <iostream>
///\ingroup misc
///\file
///\brief A simple two dimensional vector and a bounding box implementation
///
/// The class \ref lemon::dim2::Point "dim2::Point" implements
/// a two dimensional vector with the usual operations.
///
/// The class \ref lemon::dim2::Box "dim2::Box" can be used to determine
/// the rectangular bounding box of a set of
/// \ref lemon::dim2::Point "dim2::Point"'s.
namespace lemon {
///Tools for handling two dimensional coordinates
///This namespace is a storage of several
///tools for handling two dimensional coordinates
namespace dim2 {
/// \addtogroup misc
/// @{
/// Two dimensional vector (plain vector)
/// A simple two dimensional vector (plain vector) implementation
/// with the usual vector operations.
template<typename T>
class Point {
public:
typedef T Value;
///First coordinate
T x;
///Second coordinate
T y;
///Default constructor
Point() {}
///Construct an instance from coordinates
Point(T a, T b) : x(a), y(b) { }
///Returns the dimension of the vector (i.e. returns 2).
///The dimension of the vector.
///This function always returns 2.
int size() const { return 2; }
///Subscripting operator
///\c p[0] is \c p.x and \c p[1] is \c p.y
///
T& operator[](int idx) { return idx == 0 ? x : y; }
///Const subscripting operator
///\c p[0] is \c p.x and \c p[1] is \c p.y
///
const T& operator[](int idx) const { return idx == 0 ? x : y; }
///Conversion constructor
template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
///Give back the square of the norm of the vector
T normSquare() const {
return x*x+y*y;
}
///Increment the left hand side by \c u
Point<T>& operator +=(const Point<T>& u) {
x += u.x;
y += u.y;
return *this;
}
///Decrement the left hand side by \c u
Point<T>& operator -=(const Point<T>& u) {
x -= u.x;
y -= u.y;
return *this;
}
///Multiply the left hand side with a scalar
Point<T>& operator *=(const T &u) {
x *= u;
y *= u;
return *this;
}
///Divide the left hand side by a scalar
Point<T>& operator /=(const T &u) {
x /= u;
y /= u;
return *this;
}
///Return the scalar product of two vectors
T operator *(const Point<T>& u) const {
return x*u.x+y*u.y;
}
///Return the sum of two vectors
Point<T> operator+(const Point<T> &u) const {
Point<T> b=*this;
return b+=u;
}
///Return the negative of the vector
Point<T> operator-() const {
Point<T> b=*this;
b.x=-b.x; b.y=-b.y;
return b;
}
///Return the difference of two vectors
Point<T> operator-(const Point<T> &u) const {
Point<T> b=*this;
return b-=u;
}
///Return a vector multiplied by a scalar
Point<T> operator*(const T &u) const {
Point<T> b=*this;
return b*=u;
}
///Return a vector divided by a scalar
Point<T> operator/(const T &u) const {
Point<T> b=*this;
return b/=u;
}
///Test equality
bool operator==(const Point<T> &u) const {
return (x==u.x) && (y==u.y);
}
///Test inequality
bool operator!=(Point u) const {
return (x!=u.x) || (y!=u.y);
}
};
///Return a Point
///Return a Point.
///\relates Point
template <typename T>
inline Point<T> makePoint(const T& x, const T& y) {
return Point<T>(x, y);
}
///Return a vector multiplied by a scalar
///Return a vector multiplied by a scalar.
///\relates Point
template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
return x*u;
}
///Read a plain vector from a stream
///Read a plain vector from a stream.
///\relates Point
///
template<typename T>
inline std::istream& operator>>(std::istream &is, Point<T> &z) {
char c;
if (is >> c) {
if (c != '(') is.putback(c);
} else {
is.clear();
}
if (!(is >> z.x)) return is;
if (is >> c) {
if (c != ',') is.putback(c);
} else {
is.clear();
}
if (!(is >> z.y)) return is;
if (is >> c) {
if (c != ')') is.putback(c);
} else {
is.clear();
}
return is;
}
///Write a plain vector to a stream
///Write a plain vector to a stream.
///\relates Point
///
template<typename T>
inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
{
os << "(" << z.x << "," << z.y << ")";
return os;
}
///Rotate by 90 degrees
///Returns the parameter rotated by 90 degrees in positive direction.
///\relates Point
///
template<typename T>
inline Point<T> rot90(const Point<T> &z)
{
return Point<T>(-z.y,z.x);
}
///Rotate by 180 degrees
///Returns the parameter rotated by 180 degrees.
///\relates Point
///
template<typename T>
inline Point<T> rot180(const Point<T> &z)
{
return Point<T>(-z.x,-z.y);
}
///Rotate by 270 degrees
///Returns the parameter rotated by 90 degrees in negative direction.
///\relates Point
///
template<typename T>
inline Point<T> rot270(const Point<T> &z)
{
return Point<T>(z.y,-z.x);
}
/// Bounding box of plain vectors (\ref Point points).
/// A class to calculate or store the bounding box of plain vectors
/// (\ref Point points).
template<typename T>
class Box {
Point<T> _bottom_left, _top_right;
bool _empty;
public:
///Default constructor: creates an empty box
Box() { _empty = true; }
///Construct a box from one point
Box(Point<T> a) {
_bottom_left = _top_right = a;
_empty = false;
}
///Construct a box from two points
///Construct a box from two points.
///\param a The bottom left corner.
///\param b The top right corner.
///\warning The coordinates of the bottom left corner must be no more
///than those of the top right one.
Box(Point<T> a,Point<T> b)
{
_bottom_left = a;
_top_right = b;
_empty = false;
}
///Construct a box from four numbers
///Construct a box from four numbers.
///\param l The left side of the box.
///\param b The bottom of the box.
///\param r The right side of the box.
///\param t The top of the box.
///\warning The left side must be no more than the right side and
///bottom must be no more than the top.
Box(T l,T b,T r,T t)
{
_bottom_left=Point<T>(l,b);
_top_right=Point<T>(r,t);
_empty = false;
}
///Return \c true if the box is empty.
///Return \c true if the box is empty (i.e. return \c false
///if at least one point was added to the box or the coordinates of
///the box were set).
///
///The coordinates of an empty box are not defined.
bool empty() const {
return _empty;
}
///Make the box empty
void clear() {
_empty = true;
}
///Give back the bottom left corner of the box
///Give back the bottom left corner of the box.
///If the box is empty, then the return value is not defined.
Point<T> bottomLeft() const {
return _bottom_left;
}
///Set the bottom left corner of the box
///Set the bottom left corner of the box.
///\pre The box must not be empty.
void bottomLeft(Point<T> p) {
_bottom_left = p;
}
///Give back the top right corner of the box
///Give back the top right corner of the box.
///If the box is empty, then the return value is not defined.
Point<T> topRight() const {
return _top_right;
}
///Set the top right corner of the box
///Set the top right corner of the box.
///\pre The box must not be empty.
void topRight(Point<T> p) {
_top_right = p;
}
///Give back the bottom right corner of the box
///Give back the bottom right corner of the box.
///If the box is empty, then the return value is not defined.
Point<T> bottomRight() const {
return Point<T>(_top_right.x,_bottom_left.y);
}
///Set the bottom right corner of the box
///Set the bottom right corner of the box.
///\pre The box must not be empty.
void bottomRight(Point<T> p) {
_top_right.x = p.x;
_bottom_left.y = p.y;
}
///Give back the top left corner of the box
///Give back the top left corner of the box.
///If the box is empty, then the return value is not defined.
Point<T> topLeft() const {
return Point<T>(_bottom_left.x,_top_right.y);
}
///Set the top left corner of the box
///Set the top left corner of the box.
///\pre The box must not be empty.
void topLeft(Point<T> p) {
_top_right.y = p.y;
_bottom_left.x = p.x;
}
///Give back the bottom of the box
///Give back the bottom of the box.
///If the box is empty, then the return value is not defined.
T bottom() const {
return _bottom_left.y;
}
///Set the bottom of the box
///Set the bottom of the box.
///\pre The box must not be empty.
void bottom(T t) {
_bottom_left.y = t;
}
///Give back the top of the box
///Give back the top of the box.
///If the box is empty, then the return value is not defined.
T top() const {
return _top_right.y;
}
///Set the top of the box
///Set the top of the box.
///\pre The box must not be empty.
void top(T t) {
_top_right.y = t;
}
///Give back the left side of the box
///Give back the left side of the box.
///If the box is empty, then the return value is not defined.
T left() const {
return _bottom_left.x;
}
///Set the left side of the box
///Set the left side of the box.
///\pre The box must not be empty.
void left(T t) {
_bottom_left.x = t;
}
/// Give back the right side of the box
/// Give back the right side of the box.
///If the box is empty, then the return value is not defined.
T right() const {
return _top_right.x;
}
///Set the right side of the box
///Set the right side of the box.
///\pre The box must not be empty.
void right(T t) {
_top_right.x = t;
}
///Give back the height of the box
///Give back the height of the box.
///If the box is empty, then the return value is not defined.
T height() const {
return _top_right.y-_bottom_left.y;
}
///Give back the width of the box
///Give back the width of the box.
///If the box is empty, then the return value is not defined.
T width() const {
return _top_right.x-_bottom_left.x;
}
///Checks whether a point is inside the box
bool inside(const Point<T>& u) const {
if (_empty)
return false;
else {
return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
(u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
}
}
///Increments the box with a point
///Increments the box with a point.
///
Box& add(const Point<T>& u){
if (_empty) {
_bottom_left = _top_right = u;
_empty = false;
}
else {
if (_bottom_left.x > u.x) _bottom_left.x = u.x;
if (_bottom_left.y > u.y) _bottom_left.y = u.y;
if (_top_right.x < u.x) _top_right.x = u.x;
if (_top_right.y < u.y) _top_right.y = u.y;
}
return *this;
}
///Increments the box to contain another box
///Increments the box to contain another box.
///
Box& add(const Box &u){
if ( !u.empty() ){
add(u._bottom_left);
add(u._top_right);
}
return *this;
}
///Intersection of two boxes
///Intersection of two boxes.
///
Box operator&(const Box& u) const {
Box b;
if (_empty || u._empty) {
b._empty = true;
} else {
b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
b._top_right.x = std::min(_top_right.x, u._top_right.x);
b._top_right.y = std::min(_top_right.y, u._top_right.y);
b._empty = b._bottom_left.x > b._top_right.x ||
b._bottom_left.y > b._top_right.y;
}
return b;
}
};//class Box
///Read a box from a stream
///Read a box from a stream.
///\relates Box
template<typename T>
inline std::istream& operator>>(std::istream &is, Box<T>& b) {
char c;
Point<T> p;
if (is >> c) {
if (c != '(') is.putback(c);
} else {
is.clear();
}
if (!(is >> p)) return is;
b.bottomLeft(p);
if (is >> c) {
if (c != ',') is.putback(c);
} else {
is.clear();
}
if (!(is >> p)) return is;
b.topRight(p);
if (is >> c) {
if (c != ')') is.putback(c);
} else {
is.clear();
}
return is;
}
///Write a box to a stream
///Write a box to a stream.
///\relates Box
template<typename T>
inline std::ostream& operator<<(std::ostream &os, const Box<T>& b)
{
os << "(" << b.bottomLeft() << "," << b.topRight() << ")";
return os;
}
///Map of x-coordinates of a \ref Point "Point"-map
///\ingroup maps
///Map of x-coordinates of a \ref Point "Point"-map.
///
template<class M>
class XMap
{
M& _map;
public:
typedef typename M::Value::Value Value;
typedef typename M::Key Key;
///\e
XMap(M& map) : _map(map) {}
Value operator[](Key k) const {return _map[k].x;}
void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
};
///Returns an \ref XMap class
///This function just returns an \ref XMap class.
///
///\ingroup maps
///\relates XMap
template<class M>
inline XMap<M> xMap(M &m)
{
return XMap<M>(m);
}
template<class M>
inline XMap<M> xMap(const M &m)
{
return XMap<M>(m);
}
///Constant (read only) version of \ref XMap
///\ingroup maps
///Constant (read only) version of \ref XMap
///
template<class M>
class ConstXMap
{
const M& _map;
public:
typedef typename M::Value::Value Value;
typedef typename M::Key Key;
///\e
ConstXMap(const M &map) : _map(map) {}
Value operator[](Key k) const {return _map[k].x;}
};
///Returns a \ref ConstXMap class
///This function just returns a \ref ConstXMap class.
///
///\ingroup maps
///\relates ConstXMap
template<class M>
inline ConstXMap<M> xMap(const M &m)
{
return ConstXMap<M>(m);
}
///Map of y-coordinates of a \ref Point "Point"-map
///\ingroup maps
///Map of y-coordinates of a \ref Point "Point"-map.
///
template<class M>
class YMap
{
M& _map;
public:
typedef typename M::Value::Value Value;
typedef typename M::Key Key;
///\e
YMap(M& map) : _map(map) {}
Value operator[](Key k) const {return _map[k].y;}
void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
};
///Returns a \ref YMap class
///This function just returns a \ref YMap class.
///
///\ingroup maps
///\relates YMap
template<class M>
inline YMap<M> yMap(M &m)
{
return YMap<M>(m);
}
template<class M>
inline YMap<M> yMap(const M &m)
{
return YMap<M>(m);
}
///Constant (read only) version of \ref YMap
///\ingroup maps
///Constant (read only) version of \ref YMap
///
template<class M>
class ConstYMap
{
const M& _map;
public:
typedef typename M::Value::Value Value;
typedef typename M::Key Key;
///\e
ConstYMap(const M &map) : _map(map) {}
Value operator[](Key k) const {return _map[k].y;}
};
///Returns a \ref ConstYMap class
///This function just returns a \ref ConstYMap class.
///
///\ingroup maps
///\relates ConstYMap
template<class M>
inline ConstYMap<M> yMap(const M &m)
{
return ConstYMap<M>(m);
}
///\brief Map of the \ref Point::normSquare() "normSquare()"
///of a \ref Point "Point"-map
///
///Map of the \ref Point::normSquare() "normSquare()"
///of a \ref Point "Point"-map.
///\ingroup maps
template<class M>
class NormSquareMap
{
const M& _map;
public:
typedef typename M::Value::Value Value;
typedef typename M::Key Key;
///\e
NormSquareMap(const M &map) : _map(map) {}
Value operator[](Key k) const {return _map[k].normSquare();}
};
///Returns a \ref NormSquareMap class
///This function just returns a \ref NormSquareMap class.
///
///\ingroup maps
///\relates NormSquareMap
template<class M>
inline NormSquareMap<M> normSquareMap(const M &m)
{
return NormSquareMap<M>(m);
}
/// @}
} //namespce dim2
} //namespace lemon
#endif //LEMON_DIM2_H