... |
... |
@@ -7,65 +7,64 @@
|
7 |
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
8 |
8 |
*
|
9 |
9 |
* Permission to use, modify and distribute this software is granted
|
10 |
10 |
* provided that this copyright notice appears in all copies. For
|
11 |
11 |
* precise terms see the accompanying LICENSE file.
|
12 |
12 |
*
|
13 |
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
14 |
14 |
* express or implied, and with no claim as to its suitability for any
|
15 |
15 |
* purpose.
|
16 |
16 |
*
|
17 |
17 |
*/
|
18 |
18 |
|
19 |
19 |
#ifndef LEMON_DIM2_H
|
20 |
20 |
#define LEMON_DIM2_H
|
21 |
21 |
|
22 |
22 |
#include <iostream>
|
23 |
23 |
|
24 |
24 |
///\ingroup misc
|
25 |
25 |
///\file
|
26 |
26 |
///\brief A simple two dimensional vector and a bounding box implementation
|
27 |
27 |
///
|
28 |
28 |
/// The class \ref lemon::dim2::Point "dim2::Point" implements
|
29 |
29 |
/// a two dimensional vector with the usual operations.
|
30 |
30 |
///
|
31 |
|
/// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox"
|
32 |
|
/// can be used to determine
|
|
31 |
/// The class \ref lemon::dim2::Box "dim2::Box" can be used to determine
|
33 |
32 |
/// the rectangular bounding box of a set of
|
34 |
33 |
/// \ref lemon::dim2::Point "dim2::Point"'s.
|
35 |
34 |
|
36 |
35 |
namespace lemon {
|
37 |
36 |
|
38 |
37 |
///Tools for handling two dimensional coordinates
|
39 |
38 |
|
40 |
39 |
///This namespace is a storage of several
|
41 |
40 |
///tools for handling two dimensional coordinates
|
42 |
41 |
namespace dim2 {
|
43 |
42 |
|
44 |
43 |
/// \addtogroup misc
|
45 |
44 |
/// @{
|
46 |
45 |
|
47 |
|
/// A simple two dimensional vector (plain vector) implementation
|
|
46 |
/// Two dimensional vector (plain vector)
|
48 |
47 |
|
49 |
48 |
/// A simple two dimensional vector (plain vector) implementation
|
50 |
49 |
/// with the usual vector operations.
|
51 |
50 |
template<typename T>
|
52 |
51 |
class Point {
|
53 |
52 |
|
54 |
53 |
public:
|
55 |
54 |
|
56 |
55 |
typedef T Value;
|
57 |
56 |
|
58 |
57 |
///First coordinate
|
59 |
58 |
T x;
|
60 |
59 |
///Second coordinate
|
61 |
60 |
T y;
|
62 |
61 |
|
63 |
62 |
///Default constructor
|
64 |
63 |
Point() {}
|
65 |
64 |
|
66 |
65 |
///Construct an instance from coordinates
|
67 |
66 |
Point(T a, T b) : x(a), y(b) { }
|
68 |
67 |
|
69 |
68 |
///Returns the dimension of the vector (i.e. returns 2).
|
70 |
69 |
|
71 |
70 |
///The dimension of the vector.
|
... |
... |
@@ -239,357 +238,357 @@
|
239 |
238 |
///Rotate by 180 degrees
|
240 |
239 |
|
241 |
240 |
///Returns the parameter rotated by 180 degrees.
|
242 |
241 |
///\relates Point
|
243 |
242 |
///
|
244 |
243 |
template<typename T>
|
245 |
244 |
inline Point<T> rot180(const Point<T> &z)
|
246 |
245 |
{
|
247 |
246 |
return Point<T>(-z.x,-z.y);
|
248 |
247 |
}
|
249 |
248 |
|
250 |
249 |
///Rotate by 270 degrees
|
251 |
250 |
|
252 |
251 |
///Returns the parameter rotated by 90 degrees in negative direction.
|
253 |
252 |
///\relates Point
|
254 |
253 |
///
|
255 |
254 |
template<typename T>
|
256 |
255 |
inline Point<T> rot270(const Point<T> &z)
|
257 |
256 |
{
|
258 |
257 |
return Point<T>(z.y,-z.x);
|
259 |
258 |
}
|
260 |
259 |
|
261 |
260 |
|
262 |
261 |
|
263 |
|
/// A class to calculate or store the bounding box of plain vectors.
|
|
262 |
/// Bounding box of plain vectors (\ref Point points).
|
264 |
263 |
|
265 |
|
/// A class to calculate or store the bounding box of plain vectors.
|
266 |
|
///
|
267 |
|
template<typename T>
|
268 |
|
class BoundingBox {
|
|
264 |
/// A class to calculate or store the bounding box of plain vectors
|
|
265 |
/// (\ref Point points).
|
|
266 |
template<typename T>
|
|
267 |
class Box {
|
269 |
268 |
Point<T> _bottom_left, _top_right;
|
270 |
269 |
bool _empty;
|
271 |
270 |
public:
|
272 |
271 |
|
273 |
|
///Default constructor: creates an empty bounding box
|
274 |
|
BoundingBox() { _empty = true; }
|
|
272 |
///Default constructor: creates an empty box
|
|
273 |
Box() { _empty = true; }
|
275 |
274 |
|
276 |
|
///Construct an instance from one point
|
277 |
|
BoundingBox(Point<T> a) {
|
|
275 |
///Construct a box from one point
|
|
276 |
Box(Point<T> a) {
|
278 |
277 |
_bottom_left = _top_right = a;
|
279 |
278 |
_empty = false;
|
280 |
279 |
}
|
281 |
280 |
|
282 |
|
///Construct an instance from two points
|
|
281 |
///Construct a box from two points
|
283 |
282 |
|
284 |
|
///Construct an instance from two points.
|
|
283 |
///Construct a box from two points.
|
285 |
284 |
///\param a The bottom left corner.
|
286 |
285 |
///\param b The top right corner.
|
287 |
286 |
///\warning The coordinates of the bottom left corner must be no more
|
288 |
287 |
///than those of the top right one.
|
289 |
|
BoundingBox(Point<T> a,Point<T> b)
|
|
288 |
Box(Point<T> a,Point<T> b)
|
290 |
289 |
{
|
291 |
290 |
_bottom_left = a;
|
292 |
291 |
_top_right = b;
|
293 |
292 |
_empty = false;
|
294 |
293 |
}
|
295 |
294 |
|
296 |
|
///Construct an instance from four numbers
|
|
295 |
///Construct a box from four numbers
|
297 |
296 |
|
298 |
|
///Construct an instance from four numbers.
|
|
297 |
///Construct a box from four numbers.
|
299 |
298 |
///\param l The left side of the box.
|
300 |
299 |
///\param b The bottom of the box.
|
301 |
300 |
///\param r The right side of the box.
|
302 |
301 |
///\param t The top of the box.
|
303 |
302 |
///\warning The left side must be no more than the right side and
|
304 |
303 |
///bottom must be no more than the top.
|
305 |
|
BoundingBox(T l,T b,T r,T t)
|
|
304 |
Box(T l,T b,T r,T t)
|
306 |
305 |
{
|
307 |
306 |
_bottom_left=Point<T>(l,b);
|
308 |
307 |
_top_right=Point<T>(r,t);
|
309 |
308 |
_empty = false;
|
310 |
309 |
}
|
311 |
310 |
|
312 |
|
///Return \c true if the bounding box is empty.
|
|
311 |
///Return \c true if the box is empty.
|
313 |
312 |
|
314 |
|
///Return \c true if the bounding box is empty (i.e. return \c false
|
|
313 |
///Return \c true if the box is empty (i.e. return \c false
|
315 |
314 |
///if at least one point was added to the box or the coordinates of
|
316 |
315 |
///the box were set).
|
317 |
316 |
///
|
318 |
|
///The coordinates of an empty bounding box are not defined.
|
|
317 |
///The coordinates of an empty box are not defined.
|
319 |
318 |
bool empty() const {
|
320 |
319 |
return _empty;
|
321 |
320 |
}
|
322 |
321 |
|
323 |
|
///Make the BoundingBox empty
|
|
322 |
///Make the box empty
|
324 |
323 |
void clear() {
|
325 |
324 |
_empty = true;
|
326 |
325 |
}
|
327 |
326 |
|
328 |
327 |
///Give back the bottom left corner of the box
|
329 |
328 |
|
330 |
329 |
///Give back the bottom left corner of the box.
|
331 |
|
///If the bounding box is empty, then the return value is not defined.
|
|
330 |
///If the box is empty, then the return value is not defined.
|
332 |
331 |
Point<T> bottomLeft() const {
|
333 |
332 |
return _bottom_left;
|
334 |
333 |
}
|
335 |
334 |
|
336 |
335 |
///Set the bottom left corner of the box
|
337 |
336 |
|
338 |
337 |
///Set the bottom left corner of the box.
|
339 |
338 |
///\pre The box must not be empty.
|
340 |
339 |
void bottomLeft(Point<T> p) {
|
341 |
340 |
_bottom_left = p;
|
342 |
341 |
}
|
343 |
342 |
|
344 |
343 |
///Give back the top right corner of the box
|
345 |
344 |
|
346 |
345 |
///Give back the top right corner of the box.
|
347 |
|
///If the bounding box is empty, then the return value is not defined.
|
|
346 |
///If the box is empty, then the return value is not defined.
|
348 |
347 |
Point<T> topRight() const {
|
349 |
348 |
return _top_right;
|
350 |
349 |
}
|
351 |
350 |
|
352 |
351 |
///Set the top right corner of the box
|
353 |
352 |
|
354 |
353 |
///Set the top right corner of the box.
|
355 |
354 |
///\pre The box must not be empty.
|
356 |
355 |
void topRight(Point<T> p) {
|
357 |
356 |
_top_right = p;
|
358 |
357 |
}
|
359 |
358 |
|
360 |
359 |
///Give back the bottom right corner of the box
|
361 |
360 |
|
362 |
361 |
///Give back the bottom right corner of the box.
|
363 |
|
///If the bounding box is empty, then the return value is not defined.
|
|
362 |
///If the box is empty, then the return value is not defined.
|
364 |
363 |
Point<T> bottomRight() const {
|
365 |
364 |
return Point<T>(_top_right.x,_bottom_left.y);
|
366 |
365 |
}
|
367 |
366 |
|
368 |
367 |
///Set the bottom right corner of the box
|
369 |
368 |
|
370 |
369 |
///Set the bottom right corner of the box.
|
371 |
370 |
///\pre The box must not be empty.
|
372 |
371 |
void bottomRight(Point<T> p) {
|
373 |
372 |
_top_right.x = p.x;
|
374 |
373 |
_bottom_left.y = p.y;
|
375 |
374 |
}
|
376 |
375 |
|
377 |
376 |
///Give back the top left corner of the box
|
378 |
377 |
|
379 |
378 |
///Give back the top left corner of the box.
|
380 |
|
///If the bounding box is empty, then the return value is not defined.
|
|
379 |
///If the box is empty, then the return value is not defined.
|
381 |
380 |
Point<T> topLeft() const {
|
382 |
381 |
return Point<T>(_bottom_left.x,_top_right.y);
|
383 |
382 |
}
|
384 |
383 |
|
385 |
384 |
///Set the top left corner of the box
|
386 |
385 |
|
387 |
386 |
///Set the top left corner of the box.
|
388 |
387 |
///\pre The box must not be empty.
|
389 |
388 |
void topLeft(Point<T> p) {
|
390 |
389 |
_top_right.y = p.y;
|
391 |
390 |
_bottom_left.x = p.x;
|
392 |
391 |
}
|
393 |
392 |
|
394 |
393 |
///Give back the bottom of the box
|
395 |
394 |
|
396 |
395 |
///Give back the bottom of the box.
|
397 |
|
///If the bounding box is empty, then the return value is not defined.
|
|
396 |
///If the box is empty, then the return value is not defined.
|
398 |
397 |
T bottom() const {
|
399 |
398 |
return _bottom_left.y;
|
400 |
399 |
}
|
401 |
400 |
|
402 |
401 |
///Set the bottom of the box
|
403 |
402 |
|
404 |
403 |
///Set the bottom of the box.
|
405 |
404 |
///\pre The box must not be empty.
|
406 |
405 |
void bottom(T t) {
|
407 |
406 |
_bottom_left.y = t;
|
408 |
407 |
}
|
409 |
408 |
|
410 |
409 |
///Give back the top of the box
|
411 |
410 |
|
412 |
411 |
///Give back the top of the box.
|
413 |
|
///If the bounding box is empty, then the return value is not defined.
|
|
412 |
///If the box is empty, then the return value is not defined.
|
414 |
413 |
T top() const {
|
415 |
414 |
return _top_right.y;
|
416 |
415 |
}
|
417 |
416 |
|
418 |
417 |
///Set the top of the box
|
419 |
418 |
|
420 |
419 |
///Set the top of the box.
|
421 |
420 |
///\pre The box must not be empty.
|
422 |
421 |
void top(T t) {
|
423 |
422 |
_top_right.y = t;
|
424 |
423 |
}
|
425 |
424 |
|
426 |
425 |
///Give back the left side of the box
|
427 |
426 |
|
428 |
427 |
///Give back the left side of the box.
|
429 |
|
///If the bounding box is empty, then the return value is not defined.
|
|
428 |
///If the box is empty, then the return value is not defined.
|
430 |
429 |
T left() const {
|
431 |
430 |
return _bottom_left.x;
|
432 |
431 |
}
|
433 |
432 |
|
434 |
433 |
///Set the left side of the box
|
435 |
434 |
|
436 |
435 |
///Set the left side of the box.
|
437 |
436 |
///\pre The box must not be empty.
|
438 |
437 |
void left(T t) {
|
439 |
438 |
_bottom_left.x = t;
|
440 |
439 |
}
|
441 |
440 |
|
442 |
441 |
/// Give back the right side of the box
|
443 |
442 |
|
444 |
443 |
/// Give back the right side of the box.
|
445 |
|
///If the bounding box is empty, then the return value is not defined.
|
|
444 |
///If the box is empty, then the return value is not defined.
|
446 |
445 |
T right() const {
|
447 |
446 |
return _top_right.x;
|
448 |
447 |
}
|
449 |
448 |
|
450 |
449 |
///Set the right side of the box
|
451 |
450 |
|
452 |
451 |
///Set the right side of the box.
|
453 |
452 |
///\pre The box must not be empty.
|
454 |
453 |
void right(T t) {
|
455 |
454 |
_top_right.x = t;
|
456 |
455 |
}
|
457 |
456 |
|
458 |
457 |
///Give back the height of the box
|
459 |
458 |
|
460 |
459 |
///Give back the height of the box.
|
461 |
|
///If the bounding box is empty, then the return value is not defined.
|
|
460 |
///If the box is empty, then the return value is not defined.
|
462 |
461 |
T height() const {
|
463 |
462 |
return _top_right.y-_bottom_left.y;
|
464 |
463 |
}
|
465 |
464 |
|
466 |
465 |
///Give back the width of the box
|
467 |
466 |
|
468 |
467 |
///Give back the width of the box.
|
469 |
|
///If the bounding box is empty, then the return value is not defined.
|
|
468 |
///If the box is empty, then the return value is not defined.
|
470 |
469 |
T width() const {
|
471 |
470 |
return _top_right.x-_bottom_left.x;
|
472 |
471 |
}
|
473 |
472 |
|
474 |
|
///Checks whether a point is inside a bounding box
|
|
473 |
///Checks whether a point is inside the box
|
475 |
474 |
bool inside(const Point<T>& u) const {
|
476 |
475 |
if (_empty)
|
477 |
476 |
return false;
|
478 |
477 |
else {
|
479 |
478 |
return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
|
480 |
479 |
(u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
|
481 |
480 |
}
|
482 |
481 |
}
|
483 |
482 |
|
484 |
|
///Increments a bounding box with a point
|
|
483 |
///Increments the box with a point
|
485 |
484 |
|
486 |
|
///Increments a bounding box with a point.
|
|
485 |
///Increments the box with a point.
|
487 |
486 |
///
|
488 |
|
BoundingBox& add(const Point<T>& u){
|
|
487 |
Box& add(const Point<T>& u){
|
489 |
488 |
if (_empty) {
|
490 |
489 |
_bottom_left = _top_right = u;
|
491 |
490 |
_empty = false;
|
492 |
491 |
}
|
493 |
492 |
else {
|
494 |
493 |
if (_bottom_left.x > u.x) _bottom_left.x = u.x;
|
495 |
494 |
if (_bottom_left.y > u.y) _bottom_left.y = u.y;
|
496 |
495 |
if (_top_right.x < u.x) _top_right.x = u.x;
|
497 |
496 |
if (_top_right.y < u.y) _top_right.y = u.y;
|
498 |
497 |
}
|
499 |
498 |
return *this;
|
500 |
499 |
}
|
501 |
500 |
|
502 |
|
///Increments a bounding box to contain another bounding box
|
|
501 |
///Increments the box to contain another box
|
503 |
502 |
|
504 |
|
///Increments a bounding box to contain another bounding box.
|
|
503 |
///Increments the box to contain another box.
|
505 |
504 |
///
|
506 |
|
BoundingBox& add(const BoundingBox &u){
|
|
505 |
Box& add(const Box &u){
|
507 |
506 |
if ( !u.empty() ){
|
508 |
507 |
add(u._bottom_left);
|
509 |
508 |
add(u._top_right);
|
510 |
509 |
}
|
511 |
510 |
return *this;
|
512 |
511 |
}
|
513 |
512 |
|
514 |
|
///Intersection of two bounding boxes
|
|
513 |
///Intersection of two boxes
|
515 |
514 |
|
516 |
|
///Intersection of two bounding boxes.
|
|
515 |
///Intersection of two boxes.
|
517 |
516 |
///
|
518 |
|
BoundingBox operator&(const BoundingBox& u) const {
|
519 |
|
BoundingBox b;
|
|
517 |
Box operator&(const Box& u) const {
|
|
518 |
Box b;
|
520 |
519 |
if (_empty || u._empty) {
|
521 |
520 |
b._empty = true;
|
522 |
521 |
} else {
|
523 |
522 |
b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
|
524 |
523 |
b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
|
525 |
524 |
b._top_right.x = std::min(_top_right.x, u._top_right.x);
|
526 |
525 |
b._top_right.y = std::min(_top_right.y, u._top_right.y);
|
527 |
526 |
b._empty = b._bottom_left.x > b._top_right.x ||
|
528 |
527 |
b._bottom_left.y > b._top_right.y;
|
529 |
528 |
}
|
530 |
529 |
return b;
|
531 |
530 |
}
|
532 |
531 |
|
533 |
|
};//class Boundingbox
|
|
532 |
};//class Box
|
534 |
533 |
|
535 |
534 |
|
536 |
|
///Read a bounding box from a stream
|
|
535 |
///Read a box from a stream
|
537 |
536 |
|
538 |
|
///Read a bounding box from a stream.
|
539 |
|
///\relates BoundingBox
|
|
537 |
///Read a box from a stream.
|
|
538 |
///\relates Box
|
540 |
539 |
template<typename T>
|
541 |
|
inline std::istream& operator>>(std::istream &is, BoundingBox<T>& b) {
|
|
540 |
inline std::istream& operator>>(std::istream &is, Box<T>& b) {
|
542 |
541 |
char c;
|
543 |
542 |
Point<T> p;
|
544 |
543 |
if (is >> c) {
|
545 |
544 |
if (c != '(') is.putback(c);
|
546 |
545 |
} else {
|
547 |
546 |
is.clear();
|
548 |
547 |
}
|
549 |
548 |
if (!(is >> p)) return is;
|
550 |
549 |
b.bottomLeft(p);
|
551 |
550 |
if (is >> c) {
|
552 |
551 |
if (c != ',') is.putback(c);
|
553 |
552 |
} else {
|
554 |
553 |
is.clear();
|
555 |
554 |
}
|
556 |
555 |
if (!(is >> p)) return is;
|
557 |
556 |
b.topRight(p);
|
558 |
557 |
if (is >> c) {
|
559 |
558 |
if (c != ')') is.putback(c);
|
560 |
559 |
} else {
|
561 |
560 |
is.clear();
|
562 |
561 |
}
|
563 |
562 |
return is;
|
564 |
563 |
}
|
565 |
564 |
|
566 |
|
///Write a bounding box to a stream
|
|
565 |
///Write a box to a stream
|
567 |
566 |
|
568 |
|
///Write a bounding box to a stream.
|
569 |
|
///\relates BoundingBox
|
|
567 |
///Write a box to a stream.
|
|
568 |
///\relates Box
|
570 |
569 |
template<typename T>
|
571 |
|
inline std::ostream& operator<<(std::ostream &os, const BoundingBox<T>& b)
|
|
570 |
inline std::ostream& operator<<(std::ostream &os, const Box<T>& b)
|
572 |
571 |
{
|
573 |
572 |
os << "(" << b.bottomLeft() << "," << b.topRight() << ")";
|
574 |
573 |
return os;
|
575 |
574 |
}
|
576 |
575 |
|
577 |
576 |
///Map of x-coordinates of a \ref Point "Point"-map
|
578 |
577 |
|
579 |
578 |
///\ingroup maps
|
580 |
579 |
///Map of x-coordinates of a \ref Point "Point"-map.
|
581 |
580 |
///
|
582 |
581 |
template<class M>
|
583 |
582 |
class XMap
|
584 |
583 |
{
|
585 |
584 |
M& _map;
|
586 |
585 |
public:
|
587 |
586 |
|
588 |
587 |
typedef typename M::Value::Value Value;
|
589 |
588 |
typedef typename M::Key Key;
|
590 |
589 |
///\e
|
591 |
590 |
XMap(M& map) : _map(map) {}
|
592 |
591 |
Value operator[](Key k) const {return _map[k].x;}
|
593 |
592 |
void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
|
594 |
593 |
};
|
595 |
594 |
|