From 08ec3fe0aaa6a831d566dd0137cbbee1622712fa Mon Sep 17 00:00:00 2001 From: Maria Krause Date: Wed, 5 Feb 2014 15:49:29 +0100 Subject: [PATCH] corrected contains and overlaps function --- include/GSkyRegionMap.hpp | 21 ++------------------- src/sky/GSkyRegionMap.cpp | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------- src/sky/GSkyRegions.cpp | 2 +- 3 files changed, 79 insertions(+), 96 deletions(-) diff --git a/include/GSkyRegionMap.hpp b/include/GSkyRegionMap.hpp index db29388..101e087 100644 --- a/include/GSkyRegionMap.hpp +++ b/include/GSkyRegionMap.hpp @@ -33,6 +33,7 @@ #include "GMath.hpp" #include "GSkyDir.hpp" #include "GSkyRegion.hpp" +#include "GSkyRegions.hpp" #include "GFits.hpp" #include "GSkymap.hpp" #include "GSkyRegionCircle.hpp" @@ -71,9 +72,7 @@ public: const GSkymap& map(void) const; bool contains(const GSkyDir& dir) const; bool contains(const GSkyRegion& reg) const; - bool contains(const GSkyPixel& pixel) const; bool overlaps(const GSkyRegion& reg) const; - bool overlaps(const GSkyDir& dir) const; virtual std::string print(const GChatter& chatter = NORMAL) const; std::string write(void) const; virtual void read(const std::string& regstring); @@ -86,30 +85,14 @@ protected: void copy_members(const GSkyRegionMap& region); void free_members(void); void compute_solid_angle(void); - void pixels_sky_region_map(void); + void get_array_nonzero(std::vector pix_array) const; // Protected members GSkymap m_map; //!< The map GSkyPixel dir2pix(const GSkyDir& dir) const; - GSkyDir m_centre; //!< Centre or reference point of the region - double m_radius; //!< Radius of circular the region [deg] }; - -/***********************************************************************//** - * @brief Return region sky map - * - * @return region sky map. - * - * Returns the region sky map. - ***************************************************************************/ -/*inline -const double& GSkyRegionMap::map(void) const -{ - return (m_map); -} -*/ /***********************************************************************//** * @brief Set sky map region * diff --git a/src/sky/GSkyRegionMap.cpp b/src/sky/GSkyRegionMap.cpp index 79fd03f..8604884 100644 --- a/src/sky/GSkyRegionMap.cpp +++ b/src/sky/GSkyRegionMap.cpp @@ -86,6 +86,10 @@ GSkyRegionMap::GSkyRegionMap(const GSkyRegionMap& region) // Compute solid angle compute_solid_angle(); + + // Get non-zero indices + std::vector pix_array; + get_array_nonzero(pix_array); // Return return; @@ -386,58 +390,70 @@ void GSkyRegionMap::read(const GFitsHDU& hdu) } */ /***********************************************************************//** - * @brief Verifies if sky direction falls in map + * @brief Tells if direction is contained in one of the region maps + * + * @param[in] dir A Sky direction. * - * @param[in] dir Sky direction. + * @return True or False * - * This method checks if the specified sky direction falls within the pixels - * covered by the skymap region. The method uses the dir2xy method to convert - * the sky direction into 2D pixel indices, and then checks whether the pixel - * indices fall in the skymap. + * Tells if direction is contained in one of the regions ***************************************************************************/ bool GSkyRegionMap::contains(const GSkyDir& dir) const { + bool fully_inside = false; + + // Get non zeros indices of sky region map + std::vector pix_array; + get_array_nonzero(pix_array); + // Convert sky direction into sky pixel GSkyPixel pixel = dir2pix(dir); - // Return location flag - return (contains(pixel)); + for (int i = 0; i < pix_array.size(); i++ ) { + GSkyDir direc = m_map.pix2dir(pix_array[i]); + if (i < pix_array.size() && dir==direc) { + fully_inside = true; + } + else { + throw GException::feature_not_implemented(G_CONTAINS, + "Cannot compare two different region types yet"); + } + } + + // Return value + return fully_inside; } /***********************************************************************//** - * @brief Checks if sky map pixel falls in map + * @brief Checks if region is fully contained within this region * - * @param[in] pixel Sky map pixel. - * @return Trus if pixels is within map, false otherwise. + * @param[in] reg Sky region map. * - * Checks whether the specified sky map @p pixel falls within the skymap - * or not. + * @exception GException::feature_not_implemented + * Regions differ in type. ***************************************************************************/ -bool GSkymap::contains(const GSkyPixel& pixel) const +bool GSkyRegionMap::contains(const GSkyRegion& reg) const { - // Initialise containment flag - bool inmap = false; + // Initialise return value + bool fully_inside = false; - // Test 1D pixels - if (pixel.is_1D()) { - if (pixel.index()+0.5 >= 0.0 && pixel.index()-0.5 < m_num_pixels) { - inmap = true; + // Get non zeros indices of sky region map + std::vector pix_array; + get_array_nonzero(pix_array); + + for (int i = 0; i < pix_array.size(); i++ ) { + GSkyDir dir = m_map.pix2dir(pix_array[i]); + if (i < pix_array.size() && reg.contains(dir)) { + fully_inside = true; } - } - - // Test 2D pixels - else if (pixel.is_2D()) { - - // If pixel is in range then set containment flag to true - if ((pixel.x()+0.5 >= 0.0 && pixel.x()-0.5 < m_num_x) && - (pixel.y()+0.5 >= 0.0 && pixel.y()-0.5 < m_num_y)) { - inmap = true; + else { + throw GException::feature_not_implemented(G_CONTAINS, + "Cannot compare two different region types yet"); } - } - - // Return containment flag - return inmap; + + // Return value + return fully_inside; } /***********************************************************************//** @@ -445,43 +461,27 @@ bool GSkymap::contains(const GSkyPixel& pixel) const * * @param[in] reg Sky region. * - * @TODO: region sky map overlap with ring or circle region - * * @exception GException::feature_not_implemented * Regions differ in type. ***************************************************************************/ -bool GSkyRegionMap::overlaps(const GSkyDir& dir) const +bool GSkyRegionMap::overlaps(const GSkyRegion& reg) const { // Initialise return value bool overlap = false; - // Convert sky direction into sky pixel - GSkyPixel pixel = dir2pix(dir); - - // Return location flag - return (contains(pixel)); + // Get non zeros indices of sky region map + std::vector pix_array; + get_array_nonzero(pix_array); -/* GSkyDir dir; - for (int i=0; i<360; i++){ - for (int j=0; j<90; j++){ - - const GSkyDir* regdir = - dynamic_cast(&dir); - double direction = dir->radec_deg(i,j); - - if (reg->contains(dir)) { - overlap = true; - } - else { - overlap = false; - } - } + for (int i = 0; i < pix_array.size(); i++ ) { + GSkyDir dir = m_map.pix2dir(pix_array[i]); + reg.contains(dir); + if (overlap) break; } - // Return value - return overlap; - */ -} + // Return if region is in map or not + return overlap; +} /*========================================================================== @@ -539,7 +539,6 @@ void GSkyRegionMap::compute_solid_angle(void) for (int i =0; i < m_map.npix(); ++i) { GSkyPixel pix=m_map.inx2pix(i); std::cout << pix.size() << std::endl; - //value += m_map.solidangle(pix); if (pix.size() > 0){ value += m_map.solidangle(pix); } @@ -557,20 +556,21 @@ void GSkyRegionMap::compute_solid_angle(void) /***********************************************************************//** - * @brief Get an array of pixels of the sky region map + * @brief Create an array of pixels ***************************************************************************/ - void GSkyRegionMap::pixels_sky_region_map(void) - { - double num_pix_map[m_map.npix()]; - - for (int i =0; i < m_map.npix(); ++i) { - GSkyPixel pix=m_map.inx2pix(i); - std::cout << pix.size() << std::endl; +void GSkyRegionMap::get_array_nonzero(std::vector pix_array) const +{ + //std::vector pix_array; + + for (int i = 0; i < m_map.npix(); i++) + { + GSkyPixel pix=m_map.inx2pix(i); + std::cout << pix.size() << std::endl; if (pix.size() > 0){ - num_pix_map[i] = i; - } - } - - // Return - return; -} \ No newline at end of file + pix_array.push_back(pix); + } + } + + std::cout << pix_array.size() << std::endl; + +} diff --git a/src/sky/GSkyRegions.cpp b/src/sky/GSkyRegions.cpp index abe3d37..8619489 100644 --- a/src/sky/GSkyRegions.cpp +++ b/src/sky/GSkyRegions.cpp @@ -842,7 +842,7 @@ std::string GSkyRegions::print(const GChatter& chatter) const /***********************************************************************//** - * @brief Tells if region overlaps one of the regions + * @brief Tells if region one of the regions * * @param[in] reg A sky region * @return True or False -- libgit2 0.21.4