private RangeSet<Integer> expectedSubRangeSet(
     RangeSet<Integer> rangeSet, Range<Integer> subRange) {
   RangeSet<Integer> expected = TreeRangeSet.create();
   for (Range<Integer> range : rangeSet.asRanges()) {
     if (range.isConnected(subRange)) {
       expected.add(range.intersection(subRange));
     }
   }
   return expected;
 }
Beispiel #2
0
 void addWithoutSaving(String jobFullName, int n) {
   synchronized (
       usages) { // XXX why not synchronized (this) like some, though not all, other accesses?
     RangeSet r = usages.get(jobFullName);
     if (r == null) {
       r = new RangeSet();
       usages.put(jobFullName, r);
     }
     r.add(n);
   }
 }
Beispiel #3
0
  /**
   * Decide if the bounding box is visible
   *
   * @param ymax
   * @param ymin
   * @param xmin
   * @param xmax
   * @return
   */
  private boolean bbox_visible(int ymax, int ymin, int xmin, int xmax) {
    int rp1x, rp1z;
    int rp2x, rp2z;
    int p1x, p1y, p2x, p2y, x1, x2;

    if (all_visible) // unused feature, presumably for debugging
    return true;
    // check a few simple cases up front
    if (rset.isEmpty()) return false;
    if (ang >= 45 && ang <= 135 && viewy > ymax) return false;
    if (ang >= 225 && ang <= 315 && viewy < ymin) return false;
    if (ang >= 135 && ang <= 225 && viewx < xmin) return false;
    if ((ang >= 315 || ang <= 45) && viewx > xmax) return false;

    xmin -= viewx;
    ymin -= viewy;
    xmax -= viewx;
    ymax -= viewy;
    p1x = xmin;
    p2x = xmax;
    p1y = ymin;
    p2y = ymax;
    if (ymin < 0 && ymax > 0) {
      p1y = ymin;
      p2y = ymax;
      if (xmin < 0) {
        if (xmax > 0) return true;
        p1x = p2x = xmax;
      } else p1x = p2x = xmin;
    } else if (xmin < 0 && xmax > 0) {
      if (ymin < 0) p1y = p2y = ymax;
      else p1y = p2y = ymin;
    } else if ((xmin > 0 && ymin > 0) || (xmin < 0 && ymin < 0)) {
      p1x = xmax;
      p2x = xmin;
    }
    rp1x = -viewd_unscale(view_dy * p1x - view_dx * p1y);
    rp1z = -viewd_unscale(view_dx * p1x + view_dy * p1y);
    rp2x = -viewd_unscale(view_dy * p2x - view_dx * p2y);
    rp2z = -viewd_unscale(view_dx * p2x + view_dy * p2y);
    RangePair rp = new RangePair(rp1x, rp1z, rp2x, rp2z);
    if (!clip3d(rp)) return false;
    x1 = rp.x1 * zscale / rp.z1 + (view_width / 2);
    x2 = rp.x2 * zscale / rp.z2 + (view_width / 2);
    if (x1 > x2) {
      int xj = x1;
      x1 = x2;
      x2 = xj;
    }
    int[] p = {x1, x2};
    return (rset.intersect(p));
  }
 public void testRangeContaining2() {
   RangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 10));
   rangeSet.remove(Range.open(5, 7));
   assertEquals(Range.closed(3, 5), rangeSet.rangeContaining(5));
   assertTrue(rangeSet.contains(5));
   assertEquals(Range.closed(7, 10), rangeSet.rangeContaining(8));
   assertTrue(rangeSet.contains(8));
   assertNull(rangeSet.rangeContaining(6));
   assertFalse(rangeSet.contains(6));
 }
 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 testEnclosing(RangeSet<Integer> rangeSet) {
    for (Range<Integer> query : QUERY_RANGES) {
      boolean expectEnclose = false;
      for (Range<Integer> expectedRange : rangeSet.asRanges()) {
        if (expectedRange.encloses(query)) {
          expectEnclose = true;
          break;
        }
      }

      assertEquals(
          rangeSet + " was incorrect on encloses(" + query + ")",
          expectEnclose,
          rangeSet.encloses(query));
    }
  }
 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);
     }
   }
 }
Beispiel #9
0
 public Object unmarshal(HierarchicalStreamReader reader, final UnmarshallingContext context) {
   if (reader.hasMoreChildren()) {
     /* old format where <range> elements are nested like
            <range>
              <start>1337</start>
              <end>1479</end>
            </range>
     */
     return new RangeSet((List<Range>) (collectionConv.unmarshal(reader, context)));
   } else {
     return RangeSet.fromString(reader.getValue(), true);
   }
 }
 public void testRangeContaining1() {
   RangeSet<Integer> rangeSet = TreeRangeSet.create();
   rangeSet.add(Range.closed(3, 10));
   assertEquals(Range.closed(3, 10), rangeSet.rangeContaining(5));
   assertTrue(rangeSet.contains(5));
   assertNull(rangeSet.rangeContaining(1));
   assertFalse(rangeSet.contains(1));
 }
  @Test
  public void whenCreateRangeSet_thenCreated() {
    final RangeSet<Integer> rangeSet = TreeRangeSet.create();
    rangeSet.add(Range.closed(1, 10));
    rangeSet.add(Range.closed(12, 15));

    assertEquals(2, rangeSet.asRanges().size());

    rangeSet.add(Range.closed(10, 12));
    assertTrue(rangeSet.encloses(Range.closed(1, 15)));
    assertEquals(1, rangeSet.asRanges().size());
  }
 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);
     }
   }
 }
Beispiel #13
0
  /**
   * Draws the first person view on the screen during the game
   *
   * @param gw2 graphics handler to manipulate screen
   * @param px x coordinate of current position, only used to get viewx
   * @param py y coordinate of current position, only used to get viewy
   * @param view_dx view direction, x coordinate
   * @param view_dy view direction, y coordinate
   * @param walk_step, only used to get viewx and viewy
   * @param view_offset, only used to get viewx and viewy
   * @param rset
   * @param ang
   */
  public void redrawPlay(
      GraphicsWrapper gw2,
      int px,
      int py,
      int view_dx,
      int view_dy,
      int walk_step,
      int view_offset,
      RangeSet rset,
      int ang) {
    // new adjustment
    this.gw = gw2;
    this.rset = rset;
    this.view_dx = view_dx;
    this.view_dy = view_dy;
    this.ang = ang;

    // calculate view
    viewx =
        (px * map_unit + map_unit / 2)
            + viewd_unscale(view_dx * (step_size * walk_step - view_offset));
    viewy =
        (py * map_unit + map_unit / 2)
            + viewd_unscale(view_dy * (step_size * walk_step - view_offset));
    // update graphics
    // draw black background on lower half
    gw.setColor("black");
    gw.fillRect(0, 0, view_width, view_height / 2);
    // draw dark gray background on upper half
    gw.setColor("darkGray");
    gw.fillRect(0, view_height / 2, view_width, view_height / 2);
    // set color to white and draw what ever can be seen from the current position
    gw.setColor("white");
    rset.set(0, view_width - 1); // reset set of ranges to set with single new element (0,width-1)
    // debug: reset counters
    traverse_node_ct =
        traverse_ssector_ct = drawrect_ct = drawrect_late_ct = drawrect_segment_ct = 0;
    //
    traverse_node(bsp_root);
  }
  void testViewAgainstExpected(RangeSet<Integer> expected, RangeSet<Integer> view) {
    assertEquals(expected, view);
    assertEquals(expected.asRanges(), view.asRanges());
    assertEquals(expected.isEmpty(), view.isEmpty());

    if (!expected.isEmpty()) {
      assertEquals(expected.span(), view.span());
    }

    for (int i = MIN_BOUND - 1; i <= MAX_BOUND + 1; i++) {
      assertEquals(expected.contains(i), view.contains(i));
      assertEquals(expected.rangeContaining(i), view.rangeContaining(i));
    }
    testEnclosing(view);
    if (view instanceof TreeRangeSet) {
      testRangesByLowerBounds((TreeRangeSet<Integer>) view, expected.asRanges());
    }
  }
 private RangeSet<Integer> expectedComplement(RangeSet<Integer> rangeSet) {
   RangeSet<Integer> expected = TreeRangeSet.create();
   expected.add(Range.<Integer>all());
   expected.removeAll(rangeSet);
   return expected;
 }
Beispiel #16
0
  /**
   * Draws segment on screen via graphics attribute gc Helper method for traverse_ssector
   *
   * @param seg whose seen attribute may be set to true
   * @param ox1
   * @param y1
   * @param ox2
   * @param y2
   */
  private void drawrect(Seg seg, int ox1, int y1, int ox2, int y2) {
    // int y11, y12, y21, y22;
    // Log.v("inside drawrect", "hooray");
    int z1 = 0;
    int z2 = 100;

    drawrect_ct++; // debug, counter
    ox1 -= viewx;
    y1 -= viewy;
    z1 -= viewz;
    ox2 -= viewx;
    y2 -= viewy;
    z2 -= viewz;

    int y11, y12, y21, y22;
    y11 = y21 = -z1;
    y12 = y22 = -z2;

    int x1;
    int x2;
    x1 = -viewd_unscale(view_dy * ox1 - view_dx * y1);
    z1 = -viewd_unscale(view_dx * ox1 + view_dy * y1);
    x2 = -viewd_unscale(view_dy * ox2 - view_dx * y2);
    z2 = -viewd_unscale(view_dx * ox2 + view_dy * y2);

    RangePair rp = new RangePair(x1, z1, x2, z2);
    if (!clip3d(rp)) {
      return;
    }

    y11 = y11 * zscale / rp.z1 + (view_height / 2); // constant from here
    y12 = y12 * zscale / rp.z1 + (view_height / 2); // constant from here
    y21 = y21 * zscale / rp.z2 + (view_height / 2); // constant from here
    y22 = y22 * zscale / rp.z2 + (view_height / 2); // constant from here
    x1 = rp.x1 * zscale / rp.z1 + (view_width / 2); // constant from here
    x2 = rp.x2 * zscale / rp.z2 + (view_width / 2); // constant from here
    if (x1 >= x2) {
        /* reject backfaces */
      // Log.v("hatefulthings", "grrr");
      return;
    }
    int x1i = x1;
    int xd = x2 - x1;
    gw.setColor(seg.col);

    boolean drawn = false;
    drawrect_late_ct++; // debug, counter
    // loop variable is x1i, upper limit x2 is fixed
    // Log.v("more things", ""+x1i);
    // Log.v("more more things", ""+x2);
    while (x1i <= x2) {
      // check if there is an intersection,
      // if there is none proceed exit the loop,
      // if there is one, get it as (x1i,x2i)
      int[] p = {x1i, x2};
      if (!rset.intersect(p)) break;
      x1i = p[0];
      int x2i = p[1];
      // let's work on the intersection (x1i,x2i)
      int xps[] = {x1i, x1i, x2i + 1, x2i + 1};
      int yps[] = {
        y11 + (x1i - x1) * (y21 - y11) / xd,
        y12 + (x1i - x1) * (y22 - y12) / xd + 1,
        y22 + (x2i - x2) * (y22 - y12) / xd + 1,
        y21 + (x2i - x2) * (y21 - y11) / xd
      };
      System.out.println("I get to here");
      gw.fillPolygon(xps, yps, 4);
      drawn = true;
      rset.remove(x1i, x2i);
      x1i = x2i + 1;
      drawrect_segment_ct++; // debug, counter
    }
    if (drawn && !seg.seen) {
      udpateSeenCellsForSegment(seg);
    }
  }