Changes in / [243:32155c88f59b:240:bea328c5a8d3] in lemon1.0
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

lemon/dim2.h
r241 r220 21 21 22 22 #include <iostream> 23 #include <lemon/core.h> 23 24 24 25 ///\ingroup misc … … 45 46 /// @{ 46 47 47 /// A simple two dimensional vector (plain 48 49 /// A simple two dimensional vector (plain 48 /// A simple two dimensional vector (plainvector) implementation 49 50 /// A simple two dimensional vector (plainvector) implementation 50 51 /// with the usual vector operations. 51 52 template<typename T> … … 186 187 } 187 188 188 ///Read a plain 189 190 ///Read a plain 189 ///Read a plainvector from a stream 190 191 ///Read a plainvector from a stream. 191 192 ///\relates Point 192 193 /// … … 214 215 } 215 216 216 ///Write a plain 217 218 ///Write a plain 217 ///Write a plainvector to a stream 218 219 ///Write a plainvector to a stream. 219 220 ///\relates Point 220 221 /// … … 261 262 262 263 263 /// A class to calculate or store the bounding box of plainvectors.264 265 /// A class to calculate or store the bounding box of plainvectors.266 264 /// A class to calculate or store the bounding box of plainvectors. 265 266 /// A class to calculate or store the bounding box of plainvectors. 267 /// 267 268 template<typename T> 268 269 class BoundingBox { 269 Point<T> _bottom_left, _top_right;270 Point<T> bottom_left, top_right; 270 271 bool _empty; 271 272 public: … … 275 276 276 277 ///Construct an instance from one point 277 BoundingBox(Point<T> a) { 278 _bottom_left = _top_right = a; 279 _empty = false; 280 } 278 BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; } 281 279 282 280 ///Construct an instance from two points … … 289 287 BoundingBox(Point<T> a,Point<T> b) 290 288 { 291 _bottom_left =a;292 _top_right =b;289 bottom_left=a; 290 top_right=b; 293 291 _empty = false; 294 292 } … … 305 303 BoundingBox(T l,T b,T r,T t) 306 304 { 307 _bottom_left=Point<T>(l,b);308 _top_right=Point<T>(r,t);305 bottom_left=Point<T>(l,b); 306 top_right=Point<T>(r,t); 309 307 _empty = false; 310 308 } … … 323 321 ///Make the BoundingBox empty 324 322 void clear() { 325 _empty = true;323 _empty=1; 326 324 } 327 325 … … 331 329 ///If the bounding box is empty, then the return value is not defined. 332 330 Point<T> bottomLeft() const { 333 return _bottom_left;331 return bottom_left; 334 332 } 335 333 … … 337 335 338 336 ///Set the bottom left corner of the box. 339 /// \pre The box must not be empty.337 ///It should only be used for nonempty box. 340 338 void bottomLeft(Point<T> p) { 341 _bottom_left = p;339 bottom_left = p; 342 340 } 343 341 … … 347 345 ///If the bounding box is empty, then the return value is not defined. 348 346 Point<T> topRight() const { 349 return _top_right;347 return top_right; 350 348 } 351 349 … … 353 351 354 352 ///Set the top right corner of the box. 355 /// \pre The box must not be empty.353 ///It should only be used for nonempty box. 356 354 void topRight(Point<T> p) { 357 _top_right = p;355 top_right = p; 358 356 } 359 357 … … 363 361 ///If the bounding box is empty, then the return value is not defined. 364 362 Point<T> bottomRight() const { 365 return Point<T>( _top_right.x,_bottom_left.y);363 return Point<T>(top_right.x,bottom_left.y); 366 364 } 367 365 … … 369 367 370 368 ///Set the bottom right corner of the box. 371 /// \pre The box must not be empty.369 ///It should only be used for nonempty box. 372 370 void bottomRight(Point<T> p) { 373 _top_right.x = p.x;374 _bottom_left.y = p.y;371 top_right.x = p.x; 372 bottom_left.y = p.y; 375 373 } 376 374 … … 380 378 ///If the bounding box is empty, then the return value is not defined. 381 379 Point<T> topLeft() const { 382 return Point<T>( _bottom_left.x,_top_right.y);380 return Point<T>(bottom_left.x,top_right.y); 383 381 } 384 382 … … 386 384 387 385 ///Set the top left corner of the box. 388 /// \pre The box must not be empty.386 ///It should only be used for nonempty box. 389 387 void topLeft(Point<T> p) { 390 _top_right.y = p.y;391 _bottom_left.x = p.x;388 top_right.y = p.y; 389 bottom_left.x = p.x; 392 390 } 393 391 … … 397 395 ///If the bounding box is empty, then the return value is not defined. 398 396 T bottom() const { 399 return _bottom_left.y;397 return bottom_left.y; 400 398 } 401 399 … … 403 401 404 402 ///Set the bottom of the box. 405 /// \pre The box must not be empty.403 ///It should only be used for nonempty box. 406 404 void bottom(T t) { 407 _bottom_left.y = t;405 bottom_left.y = t; 408 406 } 409 407 … … 413 411 ///If the bounding box is empty, then the return value is not defined. 414 412 T top() const { 415 return _top_right.y;413 return top_right.y; 416 414 } 417 415 … … 419 417 420 418 ///Set the top of the box. 421 /// \pre The box must not be empty.419 ///It should only be used for nonempty box. 422 420 void top(T t) { 423 _top_right.y = t;421 top_right.y = t; 424 422 } 425 423 … … 429 427 ///If the bounding box is empty, then the return value is not defined. 430 428 T left() const { 431 return _bottom_left.x;429 return bottom_left.x; 432 430 } 433 431 … … 435 433 436 434 ///Set the left side of the box. 437 /// \pre The box must not be empty.435 ///It should only be used for nonempty box. 438 436 void left(T t) { 439 _bottom_left.x = t;437 bottom_left.x = t; 440 438 } 441 439 … … 445 443 ///If the bounding box is empty, then the return value is not defined. 446 444 T right() const { 447 return _top_right.x;445 return top_right.x; 448 446 } 449 447 … … 451 449 452 450 ///Set the right side of the box. 453 /// \pre The box must not be empty.451 ///It should only be used for nonempty box. 454 452 void right(T t) { 455 _top_right.x = t;453 top_right.x = t; 456 454 } 457 455 … … 461 459 ///If the bounding box is empty, then the return value is not defined. 462 460 T height() const { 463 return _top_right.y_bottom_left.y;461 return top_right.ybottom_left.y; 464 462 } 465 463 … … 469 467 ///If the bounding box is empty, then the return value is not defined. 470 468 T width() const { 471 return _top_right.x_bottom_left.x;469 return top_right.xbottom_left.x; 472 470 } 473 471 … … 476 474 if (_empty) 477 475 return false; 478 else 479 return ( (u.x_bottom_left.x)*(_top_right.xu.x) >= 0 &&480 (u.y_bottom_left.y)*(_top_right.yu.y) >= 0 );476 else{ 477 return ((u.xbottom_left.x)*(top_right.xu.x) >= 0 && 478 (u.ybottom_left.y)*(top_right.yu.y) >= 0 ); 481 479 } 482 480 } … … 487 485 /// 488 486 BoundingBox& add(const Point<T>& u){ 489 if (_empty) 490 _bottom_left = _top_right =u;487 if (_empty){ 488 bottom_left=top_right=u; 491 489 _empty = false; 492 490 } 493 else 494 if ( _bottom_left.x > u.x) _bottom_left.x = u.x;495 if ( _bottom_left.y > u.y) _bottom_left.y = u.y;496 if ( _top_right.x < u.x) _top_right.x = u.x;497 if ( _top_right.y < u.y) _top_right.y = u.y;491 else{ 492 if (bottom_left.x > u.x) bottom_left.x = u.x; 493 if (bottom_left.y > u.y) bottom_left.y = u.y; 494 if (top_right.x < u.x) top_right.x = u.x; 495 if (top_right.y < u.y) top_right.y = u.y; 498 496 } 499 497 return *this; … … 506 504 BoundingBox& add(const BoundingBox &u){ 507 505 if ( !u.empty() ){ 508 add(u._bottom_left);509 add(u._top_right);506 this>add(u.bottomLeft()); 507 this>add(u.topRight()); 510 508 } 511 509 return *this; … … 518 516 BoundingBox operator&(const BoundingBox& u) const { 519 517 BoundingBox b; 520 if ( _empty  u._empty) {518 if (this>_empty  u._empty) { 521 519 b._empty = true; 522 520 } else { 523 b. _bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);524 b. _bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);525 b. _top_right.x = std::min(_top_right.x, u._top_right.x);526 b. _top_right.y = std::min(_top_right.y, u._top_right.y);527 b._empty = b. _bottom_left.x > b._top_right.x 528 b. _bottom_left.y > b._top_right.y;521 b.bottom_left.x = std::max(this>bottom_left.x,u.bottom_left.x); 522 b.bottom_left.y = std::max(this>bottom_left.y,u.bottom_left.y); 523 b.top_right.x = std::min(this>top_right.x,u.top_right.x); 524 b.top_right.y = std::min(this>top_right.y,u.top_right.y); 525 b._empty = b.bottom_left.x > b.top_right.x  526 b.bottom_left.y > b.top_right.y; 529 527 } 530 528 return b; 
test/dim_test.cc
r242 r209 59 59 box1.add(b); 60 60 61 check(box1.left()==1 && box1.bottom()==2 && 62 box1.right()==3 && box1.top()==4, 61 check(box1.bottomLeft().x==1 && 62 box1.bottomLeft().y==2 && 63 box1.topRight().x==3 && 64 box1.topRight().y==4, 63 65 "Wrong addition of points to dim2::BoundingBox."); 64 66 65 check(box1.inside(Point(2,3)), "Wrong inside() in dim2::BoundingBox."); 66 check(box1.inside(Point(1,3)), "Wrong inside() in dim2::BoundingBox."); 67 check(!box1.inside(Point(0,3)), "Wrong inside() in dim2::BoundingBox."); 67 p.x=2; p.y=3; 68 check(box1.inside(p), "Wrong inside() in dim2::BoundingBox."); 68 69 69 BB box2(Point(2,2)); 70 p.x=1; p.y=3; 71 check(box1.inside(p), "Wrong inside() in dim2::BoundingBox."); 72 73 p.x=0; p.y=3; 74 check(!box1.inside(p), "Wrong inside() in dim2::BoundingBox."); 75 76 BB box2(p); 70 77 check(!box2.empty(), "Wrong empty() in dim2::BoundingBox."); 71 72 box2.bottomLeft(Point(2,0)); 73 box2.topRight(Point(5,3)); 74 BB box3 = box1 & box2; 75 check(!box3.empty() && 76 box3.left()==2 && box3.bottom()==2 && 77 box3.right()==3 && box3.top()==3, 78 "Wrong intersection of two dim2::BoundingBox objects."); 79 80 box1.add(box2); 81 check(!box1.empty() && 82 box1.left()==1 && box1.bottom()==0 && 83 box1.right()==5 && box1.top()==4, 84 "Wrong addition of two dim2::BoundingBox objects."); 78 79 box2.add(box1); 80 check(box2.inside(p), "Wrong inside() in dim2::BoundingBox."); 85 81 86 82 return 0;
Note: See TracChangeset
for help on using the changeset viewer.