/**
  * Transforms a RangetSet in an array of long.
  *
  * @param rangeSet rangeSet
  * @return an array of pixels at a given order
  */
 public static long[] decodeRangeSet(final RangeSet rangeSet) {
   assert rangeSet != null;
   long[] pixels = new long[(int) rangeSet.nval()];
   RangeSet.ValueIterator valueIter = rangeSet.valueIterator();
   int i = 0;
   while (valueIter.hasNext()) {
     pixels[i] = valueIter.next();
     i++;
   }
   return pixels;
 }
 /**
  * Returns a hierarchical index of a cone.
  *
  * @param index Healpix index
  * @param shape cone
  * @return the pixel numbers as a hierarchical index
  * @throws Exception Healpix Exception
  */
 protected static HealpixMoc computeConeIndex(final HealpixIndex index, final Shape shape)
     throws Exception {
   final HealpixMoc moc = new HealpixMoc();
   final Cone cone = (Cone) shape;
   final RangeSet rangeSet =
       index.queryDiscInclusive(cone.getCenter(), cone.getRadius(), TYPICAL_CHOICE_FACT);
   final RangeSet.ValueIterator valueIter = rangeSet.valueIterator();
   while (valueIter.hasNext()) {
     final long pixNest = valueIter.next();
     moc.add(new MocCell(index.getOrder(), pixNest));
   }
   return moc;
 }
 public void testQueryPolygon() throws Exception {
   System.out.println("Testing queryPolygon()");
   Pointing[] corner = new Pointing[4];
   corner[0] = new Pointing(new Vec3(1, 0.01, 0.01));
   corner[1] = new Pointing(new Vec3(1, 1, -0.3));
   corner[2] = new Pointing(new Vec3(0.01, 1, 0.01));
   corner[3] = new Pointing(new Vec3(0.01, 0.01, 1));
   RangeSet lrs = HealpixProc.queryPolygonNest(10, corner);
   assertEquals("QueryPolygon problem", lrs.nval(), 1696714);
   lrs = HealpixProc.queryPolygonInclusiveNest(10, corner, 4);
   assertEquals("QueryPolygon problem", lrs.nval(), 1700206);
   lrs = HealpixProc.queryPolygonRing(10, corner);
   assertEquals("QueryPolygon problem", lrs.nval(), 1696714);
   lrs = HealpixProc.queryPolygonInclusiveRing(10, corner, 4);
   assertEquals("QueryPolygon problem", lrs.nval(), 1700206);
 }
 public void test_query_disc_strict() throws Exception {
   System.out.println("Testing non-inclusive queryDisc()");
   Random rng = new Random(5);
   for (int o = 0; o <= 5; ++o) {
     int npix = (int) HealpixProc.order2Npix(o);
     boolean[] map = new boolean[npix];
     Vec3[] vmap = new Vec3[npix];
     for (int m = 0; m < npix; ++m) {
       map[m] = false;
       vmap[m] = HealpixProc.pix2vecRing(o, m);
     }
     for (int m = 0; m < nsamples; ++m) {
       Pointing ptg = random_dir(rng);
       double rad = Math.PI * rng.nextDouble();
       RangeSet rs = HealpixProc.queryDiscRing(o, ptg, rad);
       Vec3 vptg = new Vec3(ptg);
       double cosrad = Math.cos(rad);
       for (int i = 0; i < rs.nranges(); ++i)
         for (long j = rs.ivbegin(i); j < rs.ivend(i); ++j) map[(int) j] = true;
       for (int i = 0; i < npix; ++i) {
         boolean inside = vmap[i].dot(vptg) > cosrad;
         assertFalse("query_disc_strict problem", inside ^ map[i]);
       }
       for (int i = 0; i < rs.nranges(); ++i)
         for (long j = rs.ivbegin(i); j < rs.ivend(i); ++j) map[(int) j] = false;
     }
   }
 }
 public void test_query_disc() throws Exception {
   System.out.println("Testing queryDisc() empirically");
   int omax = 17;
   Random rng = new Random(5);
   for (int o = 0; o <= omax; ++o) {
     int niter = Math.max(1, Math.min(nsamples / 1000, 100000 >> o));
     for (int m = 0; m < niter; ++m) {
       Pointing ptg = random_dir(rng);
       double rad = Math.PI * rng.nextDouble();
       RangeSet rs = HealpixProc.queryDiscRing(o, ptg, rad);
       long nval = rs.nval();
       rs = HealpixProc.queryDiscNest(o, ptg, rad);
       assertEquals("queryDisc problem 1", nval, rs.nval());
       rs = HealpixProc.queryDiscInclusiveRing(o, ptg, rad, 4);
       long nv1 = rs.nval();
       rs = HealpixProc.queryDiscInclusiveNest(o, ptg, rad, 4);
       long nv2 = rs.nval();
       assertTrue("queryDisc problem 2", nv1 >= nv2);
       assertTrue("queryDisc problem 3", nv2 >= nval);
     }
   }
 }
 public void testQueryPolygon2() throws Exception {
   System.out.println("Testing queryPolygon() empirically");
   int omax = 17;
   Random rng = new Random(5);
   for (int o = 0; o <= omax; ++o) {
     int niter = Math.max(1, Math.min(nsamples / 1000, 100000 >> o));
     for (int m = 0; m < niter; ++m) {
       Pointing[] corner = new Pointing[3];
       corner[0] = random_dir(rng);
       corner[1] = random_dir(rng);
       corner[2] = random_dir(rng);
       RangeSet rs = HealpixProc.queryPolygonRing(o, corner);
       long nval = rs.nval();
       rs = HealpixProc.queryPolygonNest(o, corner);
       assertEquals("queryPolygon problem 1", nval, rs.nval());
       rs = HealpixProc.queryPolygonInclusiveRing(o, corner, 4);
       long nv1 = rs.nval();
       rs = HealpixProc.queryPolygonInclusiveNest(o, corner, 4);
       long nv2 = rs.nval();
       assertTrue("queryPolygon problem 2", nv1 >= nv2);
       assertTrue("queryPolygon problem 3", nv2 >= nval);
     }
   }
 }
  /**
   * Returns a hierarchical index of a polygon.
   *
   * <p>When the polygon is clockwise, the index is computed on this polygon.<br>
   * When the polygon is counterclockwised, the index is computed on the complement of this polygon
   *
   * @param index Healpix index
   * @param shape polygon
   * @return the HealpixMoc;
   * @throws Exception Healpix Exception
   */
  protected static HealpixMoc computePolygonIndex(final HealpixIndex index, final Shape shape)
      throws Exception {
    RangeSet rangeSet;
    HealpixMoc moc = new HealpixMoc();
    Polygon polygon = (Polygon) shape;

    final Resampling resample = new Resampling(polygon);
    polygon = resample.processResampling();
    final List<Polygon> polygons = polygon.triangulate();
    rangeSet = new RangeSet();
    for (Polygon p : polygons) {
      final RangeSet rangeSetTmp = new RangeSet(rangeSet);
      rangeSet.setToUnion(rangeSetTmp, computePolygon(p.getPoints(), index));
    }

    final RangeSet.ValueIterator valueIter = rangeSet.valueIterator();
    while (valueIter.hasNext()) {
      moc.add(new MocCell(index.getOrder(), valueIter.next()));
    }
    if (polygon.isClockwised()) {
      moc = moc.complement();
    }
    return moc;
  }