/* * This file is part of Healpix_cxx. * * Healpix_cxx is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Healpix_cxx is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Healpix_cxx; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * For more information about HEALPix, see http://healpix.sourceforge.net */ /* * Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt * (DLR). */ /*! \file moc_query.cc * Copyright (C) 2014-2015 Max-Planck-Society * \author Martin Reinecke */ #include "moc_query.h" #include "geom_utils.h" #include "lsconstants.h" using namespace std; namespace { template class queryHelper { private: int order, omax; bool inclusive; vector comp; vector > base; vector shortcut; arr cr; arr2 crmin; arr2 crmax; vector > stk; // stack for pixel numbers and their orders I pix; int o; int stacktop; // a place to save a stack position vec3 pv; void check_pixel (int zone, Moc &pixset) { if (zone==0) return; if (o=3) pixset.appendPixel(o,pix); // output all subpixels else // (zone>=1) for (int i=0; i<4; ++i) stk.push_back(make_pair(4*pix+3-i,o+1)); // add children } else if (o>order) // this implies that inclusive==true { if (zone>=2) // pixel center in shape { pixset.appendPixel(order,pix>>(2*(o-order))); // output parent pixel stk.resize(stacktop); // unwind the stack } else // (zone>=1): pixel center in safety range { if (o>(2*(o-order))); // output parent pixel stk.resize(stacktop); // unwind the stack } } } else // o==order { if (zone>=2) pixset.appendPixel(order,pix); else if (inclusive) // and (zone>=1) { if (order=0)&&(myloc=0)&&(myloc &comp_) : order(order_), omax(omax_), inclusive(inclusive_), comp(comp_), base(omax+1), shortcut(comp.size()), cr(comp.size()), crmin(omax+1,comp.size()), crmax(omax+1,comp.size()) { planck_assert(comp.size()>=1,"bad query component vector"); planck_assert(order<=omax,"order>omax"); if (!inclusive) planck_assert(order==omax,"inconsistency"); planck_assert(omax<=T_Healpix_Base::order_max,"omax too high"); for (tsize i=0; i=pi) ? -1.01:cos(comp[i].radius+dr); crmin(o,i) = (comp[i].radius-dr<=0.) ? 1.01:cos(comp[i].radius-dr); } } for (tsize i=0; i result() { Moc pixset; stk.reserve(12+3*omax); // reserve maximum size to avoid reallocation for (int i=0; i<12; ++i) // insert the 12 base pixels in reverse order stk.push_back(make_pair(I(11-i),0)); stacktop=0; // a place to save a stack position while (!stk.empty()) // as long as there are pixels on the stack { // pop current pixel number and order from the stack pix=stk.back().first; o=stk.back().second; stk.pop_back(); pv = base[o].pix2vec(pix); int loc=comp.size()-1; tsize zone=getZone(loc,0,3); check_pixel (zone, pixset); planck_assert(loc==-1,"stack not used up"); } return pixset; } }; double isLeft (const vec3 &a, const vec3 &b, const vec3 &c) { return dotprod(crossprod(a,b),c); } // adapted from code available at http://geomalgorithms.com/a12-_hull-3.html // Original copyright notice follows: // Copyright 2001 softSurfer, 2012 Dan Sunday // This code may be freely used and modified for any purpose // providing that this copyright notice is included with it. // SoftSurfer makes no warranty for this code, and cannot be held // liable for any real or imagined damage resulting from its use. // Users of this code must verify correctness for their application. vector getHull (const vector &vert, const vector &P) { // initialize a deque D[] from bottom to top so that the // 1st three vertices of P[] are a ccw triangle int n = P.size(); arr D(2*n+1); int bot = n-2, top = bot+3; // initial bottom and top deque indices D[bot] = D[top] = P[2]; // 3rd vertex is at both bot and top if (isLeft(vert[P[0]], vert[P[1]], vert[P[2]]) > 0) { D[bot+1] = P[0]; D[bot+2] = P[1]; // ccw vertices are: 2,0,1,2 } else { D[bot+1] = P[1]; D[bot+2] = P[0]; // ccw vertices are: 2,1,0,2 } // compute the hull on the deque D[] for (int i=3; i 0) && (isLeft(vert[D[top-1]], vert[D[top]], vert[P[i]]) > 0) ) continue; // skip an interior vertex // incrementally add an exterior vertex to the deque hull // get the rightmost tangent at the deque bot while (isLeft(vert[D[bot]], vert[D[bot+1]], vert[P[i]]) <= 0) ++bot; // remove bot of deque D[--bot] = P[i]; // insert P[i] at bot of deque // get the leftmost tangent at the deque top while (isLeft(vert[D[top-1]], vert[D[top]], vert[P[i]]) <= 0) --top; // pop top of deque D[++top] = P[i]; // push P[i] onto top of deque } // transcribe deque D[] to the output hull array H[] int nout = top-bot; vector res(nout); for (int h=0; h &vv, const vector &P, vector &comp, bool doLast) { vector hull=getHull(vv,P); vector addHull(hull.size()); // sync both sequences at the first point of the convex hull int ihull=0, ipoly=0, nhull=hull.size(), npoly=P.size(); while (hull[ihull]!=P[ipoly]) ++ipoly; // iterate over the pockets between the polygon and its convex hull int npockets=0; if (P.size()==3) // really P.size(), not vv.size()? for (int i=0; i<3; i++) addHull[i]=true; else { do { int ihull_next = (ihull+1)%nhull, ipoly_next = (ipoly+1)%npoly; if (hull[ihull_next]==P[ipoly_next]) // no pocket found { addHull[ihull]=true; ihull=ihull_next; ipoly=ipoly_next; } else // query pocket { int nvpocket=2; // number of vertices for this pocket while (P[ipoly_next]!=hull[ihull_next]) { ipoly_next = (ipoly_next+1)%npoly; ++nvpocket; } vector ppocket(nvpocket); int idx=0; int ipoly_bw=ipoly_next; while (P[ipoly_bw]!=hull[ihull]) { ppocket[idx++]=P[ipoly_bw]; ipoly_bw=(ipoly_bw+npoly-1)%npoly; } ppocket[idx]=hull[ihull]; // process pocket recursively ++npockets; prepPolyHelper (vv, ppocket, comp, false); ihull=ihull_next; ipoly=ipoly_next; } } while (ihull!=0); } if (npockets>1) comp.push_back(MocQueryComponent(OR,npockets)); if (npockets>0) comp.push_back(MocQueryComponent(NOT)); if (!doLast) addHull.back()=false; // add convex hull for (tsize i=0; i0) ++num_and; if (num_and>1) comp.push_back(MocQueryComponent(AND,num_and)); } } // unnamed namespace template Moc mocQuery (int order, const vector &comp) { return queryHelper(order,order,false,comp).result(); } template Moc mocQuery (int order, const vector &comp); template Moc mocQuery (int order, const vector &comp); template Moc mocQueryInclusive (int order, int omax, const vector &comp) { return queryHelper(order,omax,true,comp).result(); } template Moc mocQueryInclusive (int order, int omax, const vector &comp); template Moc mocQueryInclusive (int order, int omax, const vector &comp); vector prepPolygon (const vector &vertex) { planck_assert(vertex.size()>=3,"not enough vertices in polygon"); vector vv(vertex.size()); for (tsize i=0; i P(vv.size()); for (tsize i=0; i comp; prepPolyHelper(vv,P,comp,true); return comp; }