# HG changeset patch
# User Peter Kovacs <kpeter@inf.elte.hu>
# Date 1216060940 -7200
# Node ID 2ccb860dfdcb5152b4006558647e2e63b454bb0b
# Parent 81cfc04531e8fcf0a6e72f22ff2871fcb827c416
Some cleanup in the implementation of dim2::BoundingBox
- Rename the private varibles to start with underscore.
- empty() function works correctly in all cases.
A bounding box is empty if _empty is true and _bottom_left <= _top_right.
diff -r 81cfc04531e8 -r 2ccb860dfdcb lemon/dim2.h
|
a
|
b
|
|
| 261 | 261 | |
| 262 | 262 | |
| 263 | 263 | |
| 264 | | /// A class to calculate or store the bounding box of plainvectors. |
| | 264 | /// A class to calculate or store the bounding box of plain vectors. |
| 265 | 265 | |
| 266 | | /// A class to calculate or store the bounding box of plainvectors. |
| 267 | | /// |
| | 266 | /// A class to calculate or store the bounding box of plain vectors. |
| | 267 | /// |
| 268 | 268 | template<typename T> |
| 269 | 269 | class BoundingBox { |
| 270 | | Point<T> bottom_left, top_right; |
| | 270 | Point<T> _bottom_left, _top_right; |
| 271 | 271 | bool _empty; |
| 272 | 272 | public: |
| 273 | 273 | |
| … |
… |
|
| 275 | 275 | BoundingBox() { _empty = true; } |
| 276 | 276 | |
| 277 | 277 | ///Construct an instance from one point |
| 278 | | BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; } |
| | 278 | BoundingBox(Point<T> a) { |
| | 279 | _bottom_left = _top_right = a; |
| | 280 | _empty = false; |
| | 281 | } |
| 279 | 282 | |
| 280 | 283 | ///Construct an instance from two points |
| 281 | 284 | |
| 282 | 285 | ///Construct an instance from two points. |
| 283 | 286 | ///\param a The bottom left corner. |
| 284 | 287 | ///\param b The top right corner. |
| 285 | | ///\warning The coordinates of the bottom left corner must be no more |
| | 288 | ///\warning The coordinates of the bottom left corner should be no more |
| 286 | 289 | ///than those of the top right one. |
| 287 | 290 | BoundingBox(Point<T> a,Point<T> b) |
| 288 | 291 | { |
| 289 | | bottom_left=a; |
| 290 | | top_right=b; |
| | 292 | _bottom_left = a; |
| | 293 | _top_right = b; |
| 291 | 294 | _empty = false; |
| 292 | 295 | } |
| 293 | 296 | |
| … |
… |
|
| 298 | 301 | ///\param b The bottom of the box. |
| 299 | 302 | ///\param r The right side of the box. |
| 300 | 303 | ///\param t The top of the box. |
| 301 | | ///\warning The left side must be no more than the right side and |
| 302 | | ///bottom must be no more than the top. |
| | 304 | ///\warning The left side should be no more than the right side and |
| | 305 | ///bottom should be no more than the top. |
| 303 | 306 | BoundingBox(T l,T b,T r,T t) |
| 304 | 307 | { |
| 305 | | bottom_left=Point<T>(l,b); |
| 306 | | top_right=Point<T>(r,t); |
| | 308 | _bottom_left=Point<T>(l,b); |
| | 309 | _top_right=Point<T>(r,t); |
| 307 | 310 | _empty = false; |
| 308 | 311 | } |
| 309 | 312 | |
| … |
… |
|
| 311 | 314 | |
| 312 | 315 | ///Return \c true if the bounding box is empty (i.e. return \c false |
| 313 | 316 | ///if at least one point was added to the box or the coordinates of |
| 314 | | ///the box were set). |
| | 317 | ///the box were set correctly). |
| 315 | 318 | /// |
| 316 | 319 | ///The coordinates of an empty bounding box are not defined. |
| 317 | 320 | bool empty() const { |
| 318 | | return _empty; |
| | 321 | return _empty || _bottom_left.x > _top_right.x |
| | 322 | || _bottom_left.y > _top_right.y; |
| 319 | 323 | } |
| 320 | 324 | |
| 321 | 325 | ///Make the BoundingBox empty |
| 322 | 326 | void clear() { |
| 323 | | _empty=1; |
| | 327 | _empty = true; |
| 324 | 328 | } |
| 325 | 329 | |
| 326 | 330 | ///Give back the bottom left corner of the box |
| … |
… |
|
| 328 | 332 | ///Give back the bottom left corner of the box. |
| 329 | 333 | ///If the bounding box is empty, then the return value is not defined. |
| 330 | 334 | Point<T> bottomLeft() const { |
| 331 | | return bottom_left; |
| | 335 | return _bottom_left; |
| 332 | 336 | } |
| 333 | 337 | |
| 334 | 338 | ///Set the bottom left corner of the box |
| … |
… |
|
| 336 | 340 | ///Set the bottom left corner of the box. |
| 337 | 341 | ///It should only be used for non-empty box. |
| 338 | 342 | void bottomLeft(Point<T> p) { |
| 339 | | bottom_left = p; |
| | 343 | _bottom_left = p; |
| | 344 | _empty = false; |
| 340 | 345 | } |
| 341 | 346 | |
| 342 | 347 | ///Give back the top right corner of the box |
| … |
… |
|
| 344 | 349 | ///Give back the top right corner of the box. |
| 345 | 350 | ///If the bounding box is empty, then the return value is not defined. |
| 346 | 351 | Point<T> topRight() const { |
| 347 | | return top_right; |
| | 352 | return _top_right; |
| 348 | 353 | } |
| 349 | 354 | |
| 350 | 355 | ///Set the top right corner of the box |
| … |
… |
|
| 352 | 357 | ///Set the top right corner of the box. |
| 353 | 358 | ///It should only be used for non-empty box. |
| 354 | 359 | void topRight(Point<T> p) { |
| 355 | | top_right = p; |
| | 360 | _top_right = p; |
| | 361 | _empty = false; |
| 356 | 362 | } |
| 357 | 363 | |
| 358 | 364 | ///Give back the bottom right corner of the box |
| … |
… |
|
| 360 | 366 | ///Give back the bottom right corner of the box. |
| 361 | 367 | ///If the bounding box is empty, then the return value is not defined. |
| 362 | 368 | Point<T> bottomRight() const { |
| 363 | | return Point<T>(top_right.x,bottom_left.y); |
| | 369 | return Point<T>(_top_right.x,_bottom_left.y); |
| 364 | 370 | } |
| 365 | 371 | |
| 366 | 372 | ///Set the bottom right corner of the box |
| … |
… |
|
| 368 | 374 | ///Set the bottom right corner of the box. |
| 369 | 375 | ///It should only be used for non-empty box. |
| 370 | 376 | void bottomRight(Point<T> p) { |
| 371 | | top_right.x = p.x; |
| 372 | | bottom_left.y = p.y; |
| | 377 | _top_right.x = p.x; |
| | 378 | _bottom_left.y = p.y; |
| | 379 | _empty = false; |
| 373 | 380 | } |
| 374 | 381 | |
| 375 | 382 | ///Give back the top left corner of the box |
| … |
… |
|
| 377 | 384 | ///Give back the top left corner of the box. |
| 378 | 385 | ///If the bounding box is empty, then the return value is not defined. |
| 379 | 386 | Point<T> topLeft() const { |
| 380 | | return Point<T>(bottom_left.x,top_right.y); |
| | 387 | return Point<T>(_bottom_left.x,_top_right.y); |
| 381 | 388 | } |
| 382 | 389 | |
| 383 | 390 | ///Set the top left corner of the box |
| … |
… |
|
| 385 | 392 | ///Set the top left corner of the box. |
| 386 | 393 | ///It should only be used for non-empty box. |
| 387 | 394 | void topLeft(Point<T> p) { |
| 388 | | top_right.y = p.y; |
| 389 | | bottom_left.x = p.x; |
| | 395 | _top_right.y = p.y; |
| | 396 | _bottom_left.x = p.x; |
| | 397 | _empty = false; |
| 390 | 398 | } |
| 391 | 399 | |
| 392 | 400 | ///Give back the bottom of the box |
| … |
… |
|
| 394 | 402 | ///Give back the bottom of the box. |
| 395 | 403 | ///If the bounding box is empty, then the return value is not defined. |
| 396 | 404 | T bottom() const { |
| 397 | | return bottom_left.y; |
| | 405 | return _bottom_left.y; |
| 398 | 406 | } |
| 399 | 407 | |
| 400 | 408 | ///Set the bottom of the box |
| … |
… |
|
| 402 | 410 | ///Set the bottom of the box. |
| 403 | 411 | ///It should only be used for non-empty box. |
| 404 | 412 | void bottom(T t) { |
| 405 | | bottom_left.y = t; |
| | 413 | _bottom_left.y = t; |
| | 414 | _empty = false; |
| 406 | 415 | } |
| 407 | 416 | |
| 408 | 417 | ///Give back the top of the box |
| … |
… |
|
| 410 | 419 | ///Give back the top of the box. |
| 411 | 420 | ///If the bounding box is empty, then the return value is not defined. |
| 412 | 421 | T top() const { |
| 413 | | return top_right.y; |
| | 422 | return _top_right.y; |
| 414 | 423 | } |
| 415 | 424 | |
| 416 | 425 | ///Set the top of the box |
| … |
… |
|
| 418 | 427 | ///Set the top of the box. |
| 419 | 428 | ///It should only be used for non-empty box. |
| 420 | 429 | void top(T t) { |
| 421 | | top_right.y = t; |
| | 430 | _top_right.y = t; |
| | 431 | _empty = false; |
| 422 | 432 | } |
| 423 | 433 | |
| 424 | 434 | ///Give back the left side of the box |
| … |
… |
|
| 426 | 436 | ///Give back the left side of the box. |
| 427 | 437 | ///If the bounding box is empty, then the return value is not defined. |
| 428 | 438 | T left() const { |
| 429 | | return bottom_left.x; |
| | 439 | return _bottom_left.x; |
| 430 | 440 | } |
| 431 | 441 | |
| 432 | 442 | ///Set the left side of the box |
| … |
… |
|
| 434 | 444 | ///Set the left side of the box. |
| 435 | 445 | ///It should only be used for non-empty box. |
| 436 | 446 | void left(T t) { |
| 437 | | bottom_left.x = t; |
| | 447 | _bottom_left.x = t; |
| | 448 | _empty = false; |
| 438 | 449 | } |
| 439 | 450 | |
| 440 | 451 | /// Give back the right side of the box |
| … |
… |
|
| 442 | 453 | /// Give back the right side of the box. |
| 443 | 454 | ///If the bounding box is empty, then the return value is not defined. |
| 444 | 455 | T right() const { |
| 445 | | return top_right.x; |
| | 456 | return _top_right.x; |
| 446 | 457 | } |
| 447 | 458 | |
| 448 | 459 | ///Set the right side of the box |
| … |
… |
|
| 450 | 461 | ///Set the right side of the box. |
| 451 | 462 | ///It should only be used for non-empty box. |
| 452 | 463 | void right(T t) { |
| 453 | | top_right.x = t; |
| | 464 | _top_right.x = t; |
| | 465 | _empty = false; |
| 454 | 466 | } |
| 455 | 467 | |
| 456 | 468 | ///Give back the height of the box |
| … |
… |
|
| 458 | 470 | ///Give back the height of the box. |
| 459 | 471 | ///If the bounding box is empty, then the return value is not defined. |
| 460 | 472 | T height() const { |
| 461 | | return top_right.y-bottom_left.y; |
| | 473 | return _top_right.y-_bottom_left.y; |
| 462 | 474 | } |
| 463 | 475 | |
| 464 | 476 | ///Give back the width of the box |
| … |
… |
|
| 466 | 478 | ///Give back the width of the box. |
| 467 | 479 | ///If the bounding box is empty, then the return value is not defined. |
| 468 | 480 | T width() const { |
| 469 | | return top_right.x-bottom_left.x; |
| | 481 | return _top_right.x-_bottom_left.x; |
| 470 | 482 | } |
| 471 | 483 | |
| 472 | 484 | ///Checks whether a point is inside a bounding box |
| 473 | 485 | bool inside(const Point<T>& u) const { |
| 474 | | if (_empty) |
| | 486 | if ( empty() ) |
| 475 | 487 | return false; |
| 476 | | else{ |
| 477 | | return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 && |
| 478 | | (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 ); |
| | 488 | else { |
| | 489 | return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 && |
| | 490 | (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 ); |
| 479 | 491 | } |
| 480 | 492 | } |
| 481 | 493 | |
| … |
… |
|
| 484 | 496 | ///Increments a bounding box with a point. |
| 485 | 497 | /// |
| 486 | 498 | BoundingBox& add(const Point<T>& u){ |
| 487 | | if (_empty){ |
| 488 | | bottom_left=top_right=u; |
| | 499 | if ( empty() ) { |
| | 500 | _bottom_left = _top_right = u; |
| 489 | 501 | _empty = false; |
| 490 | 502 | } |
| 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; |
| | 503 | else { |
| | 504 | if (_bottom_left.x > u.x) _bottom_left.x = u.x; |
| | 505 | if (_bottom_left.y > u.y) _bottom_left.y = u.y; |
| | 506 | if (_top_right.x < u.x) _top_right.x = u.x; |
| | 507 | if (_top_right.y < u.y) _top_right.y = u.y; |
| 496 | 508 | } |
| 497 | 509 | return *this; |
| 498 | 510 | } |
| … |
… |
|
| 503 | 515 | /// |
| 504 | 516 | BoundingBox& add(const BoundingBox &u){ |
| 505 | 517 | if ( !u.empty() ){ |
| 506 | | this->add(u.bottomLeft()); |
| 507 | | this->add(u.topRight()); |
| | 518 | add(u._bottom_left); |
| | 519 | add(u._top_right); |
| 508 | 520 | } |
| 509 | 521 | return *this; |
| 510 | 522 | } |
| … |
… |
|
| 515 | 527 | /// |
| 516 | 528 | BoundingBox operator&(const BoundingBox& u) const { |
| 517 | 529 | BoundingBox b; |
| 518 | | if (this->_empty || u._empty) { |
| | 530 | if ( empty() || u.empty() ) { |
| 519 | 531 | b._empty = true; |
| 520 | 532 | } else { |
| 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; |
| | 533 | b._empty = false; |
| | 534 | b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x); |
| | 535 | b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y); |
| | 536 | b._top_right.x = std::min(_top_right.x, u._top_right.x); |
| | 537 | b._top_right.y = std::min(_top_right.y, u._top_right.y); |
| 527 | 538 | } |
| 528 | 539 | return b; |
| 529 | 540 | } |