Commit a72a502b68b38318bcf4bfe7fec38b33c95c4411
Committed by
Jürgen Knödlseder
1 parent
644a7dd4
Implemented contains() and overlaps() for GSkyRegionCircle.
Showing
1 changed file
with
65 additions
and
9 deletions
src/sky/GSkyRegionRect.cpp
| @@ -28,6 +28,7 @@ | @@ -28,6 +28,7 @@ | ||
| 28 | #ifdef HAVE_CONFIG_H | 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include <config.h> | 29 | #include <config.h> |
| 30 | #endif | 30 | #endif |
| 31 | +#include "GSkyRegionCircle.hpp" | ||
| 31 | #include "GSkyRegionRect.hpp" | 32 | #include "GSkyRegionRect.hpp" |
| 32 | #include "GTools.hpp" | 33 | #include "GTools.hpp" |
| 33 | 34 | ||
| @@ -588,13 +589,41 @@ bool GSkyRegionRect::contains(const GSkyDir& dir) const | @@ -588,13 +589,41 @@ bool GSkyRegionRect::contains(const GSkyDir& dir) const | ||
| 588 | bool GSkyRegionRect::contains(const GSkyRegion& reg) const | 589 | bool GSkyRegionRect::contains(const GSkyRegion& reg) const |
| 589 | { | 590 | { |
| 590 | // Initialise return value | 591 | // Initialise return value |
| 591 | - bool fully_inside = false; | 592 | + bool is_fully_inside = false; |
| 592 | 593 | ||
| 593 | - throw GException::feature_not_implemented(G_CONTAINS, | ||
| 594 | - "Cannot compare Rect region type yet"); | 594 | + // If other region is circle use a simple way to calculate |
| 595 | + if (reg.type() == "Circle") { | ||
| 596 | + | ||
| 597 | + // Create circular region from reg | ||
| 598 | + const GSkyRegionCircle* regcirc = | ||
| 599 | + dynamic_cast<const GSkyRegionCircle*>(®); | ||
| 600 | + | ||
| 601 | + // Transform circle center to local coordinate system | ||
| 602 | + GSkyDir local_centre = transform_to_local(regcirc->centre()); | ||
| 603 | + | ||
| 604 | + // Get circle radius | ||
| 605 | + const double circ_rad = regcirc->radius(); | ||
| 606 | + | ||
| 607 | + // Check extension of circle along declination axis | ||
| 608 | + if ((std::abs(local_centre.dec_deg()) + circ_rad) <= m_halfheight) { | ||
| 609 | + | ||
| 610 | + // Check extension of circle along right ascension axis | ||
| 611 | + if ((std::abs(local_centre.ra_deg()) + circ_rad) <= m_halfwidth) { | ||
| 612 | + | ||
| 613 | + // Circle is fully contained in this rectangle | ||
| 614 | + is_fully_inside = true; | ||
| 615 | + } | ||
| 616 | + } | ||
| 617 | + } // Region was of type "Circle" | ||
| 618 | + | ||
| 619 | + // ... otherwise throw an exception | ||
| 620 | + else { | ||
| 621 | + throw GException::feature_not_implemented(G_CONTAINS, | ||
| 622 | + "Cannot compare rectangular region with other than circle yet"); | ||
| 623 | + } | ||
| 595 | 624 | ||
| 596 | // Return value | 625 | // Return value |
| 597 | - return fully_inside; | 626 | + return is_fully_inside; |
| 598 | } | 627 | } |
| 599 | 628 | ||
| 600 | 629 | ||
| @@ -608,15 +637,42 @@ bool GSkyRegionRect::contains(const GSkyRegion& reg) const | @@ -608,15 +637,42 @@ bool GSkyRegionRect::contains(const GSkyRegion& reg) const | ||
| 608 | ***************************************************************************/ | 637 | ***************************************************************************/ |
| 609 | bool GSkyRegionRect::overlaps(const GSkyRegion& reg) const | 638 | bool GSkyRegionRect::overlaps(const GSkyRegion& reg) const |
| 610 | { | 639 | { |
| 611 | - | ||
| 612 | // Initialise return value | 640 | // Initialise return value |
| 613 | - bool overlap = false; | 641 | + bool is_overlapping = false; |
| 614 | 642 | ||
| 615 | - throw GException::feature_not_implemented(G_OVERLAPS, | ||
| 616 | - "Cannot compare Rect region type yet"); | 643 | + // If other region is circle use a simple way to calculate |
| 644 | + if (reg.type() == "Circle") { | ||
| 645 | + | ||
| 646 | + // Create circular region from reg | ||
| 647 | + const GSkyRegionCircle* regcirc = | ||
| 648 | + dynamic_cast<const GSkyRegionCircle*>(®); | ||
| 649 | + | ||
| 650 | + // Transform circle center to local coordinate system | ||
| 651 | + GSkyDir local_centre = transform_to_local(regcirc->centre()); | ||
| 652 | + | ||
| 653 | + // Get circle radius | ||
| 654 | + const double circ_rad = regcirc->radius(); | ||
| 655 | + | ||
| 656 | + // Check extension of circle along declination axis | ||
| 657 | + if (std::abs(local_centre.dec_deg()) <= (m_halfheight + circ_rad)) { | ||
| 658 | + | ||
| 659 | + // Check extension of circle along right ascension axis | ||
| 660 | + if (std::abs(local_centre.ra_deg()) <= (m_halfwidth + circ_rad)) { | ||
| 661 | + | ||
| 662 | + // Circle is fully contained in this rectangle | ||
| 663 | + is_overlapping = true; | ||
| 664 | + } | ||
| 665 | + } | ||
| 666 | + } // Region was of type "Circle" | ||
| 667 | + | ||
| 668 | + // ... otherwise throw an exception | ||
| 669 | + else { | ||
| 670 | + throw GException::feature_not_implemented(G_OVERLAPS, | ||
| 671 | + "Cannot compare rectangular region with other than circle yet"); | ||
| 672 | + } | ||
| 617 | 673 | ||
| 618 | // Return value | 674 | // Return value |
| 619 | - return overlap; | 675 | + return is_overlapping; |
| 620 | } | 676 | } |
| 621 | 677 | ||
| 622 | 678 |