Java Code Examples for org.apache.lucene.util.BitUtil#interleave()

The following examples show how to use org.apache.lucene.util.BitUtil#interleave() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Tessellator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected Node(final double[] x, final double[] y, final int index, final int vertexIndex, final boolean isGeo) {
  this.idx = index;
  this.vrtxIdx = vertexIndex;
  this.polyX = x;
  this.polyY = y;
  // casting to float is safe as original values for non-geo are represented as floats
  this.y = isGeo ? encodeLatitude(polyY[vrtxIdx]) : XYEncodingUtils.encode((float) polyY[vrtxIdx]);
  this.x = isGeo ? encodeLongitude(polyX[vrtxIdx]) : XYEncodingUtils.encode((float) polyX[vrtxIdx]);
  this.morton = BitUtil.interleave(this.x ^ 0x80000000, this.y ^ 0x80000000);
  this.previous = null;
  this.next = null;
  this.previousZ = null;
  this.nextZ = null;
  this.isNextEdgeFromPolygon = true;
}
 
Example 2
Source File: Tessellator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Uses morton code for speed to determine whether or a polygon node forms a valid ear w/ adjacent nodes */
private static final boolean mortonIsEar(final Node ear) {
  // triangle bbox (flip the bits so negative encoded values are < positive encoded values)
  int minTX = StrictMath.min(StrictMath.min(ear.previous.x, ear.x), ear.next.x) ^ 0x80000000;
  int minTY = StrictMath.min(StrictMath.min(ear.previous.y, ear.y), ear.next.y) ^ 0x80000000;
  int maxTX = StrictMath.max(StrictMath.max(ear.previous.x, ear.x), ear.next.x) ^ 0x80000000;
  int maxTY = StrictMath.max(StrictMath.max(ear.previous.y, ear.y), ear.next.y) ^ 0x80000000;

  // z-order range for the current triangle bbox;
  long minZ = BitUtil.interleave(minTX, minTY);
  long maxZ = BitUtil.interleave(maxTX, maxTY);

  // now make sure we don't have other points inside the potential ear;

  // look for points inside the triangle in both directions
  Node p = ear.previousZ;
  Node n = ear.nextZ;
  while (p != null && Long.compareUnsigned(p.morton, minZ) >= 0
      && n != null && Long.compareUnsigned(n.morton, maxZ) <= 0) {
    if (p.idx != ear.previous.idx && p.idx != ear.next.idx &&
        pointInEar(p.getX(), p.getY(), ear.previous.getX(), ear.previous.getY(), ear.getX(), ear.getY(), ear.next.getX(), ear.next.getY()) &&
        area(p.previous.getX(), p.previous.getY(), p.getX(), p.getY(), p.next.getX(), p.next.getY()) >= 0) return false;
    p = p.previousZ;

    if (n.idx != ear.previous.idx && n.idx != ear.next.idx &&
        pointInEar(n.getX(), n.getY(), ear.previous.getX(), ear.previous.getY(), ear.getX(), ear.getY(), ear.next.getX(), ear.next.getY()) &&
        area(n.previous.getX(), n.previous.getY(), n.getX(), n.getY(), n.next.getX(), n.next.getY()) >= 0) return false;
    n = n.nextZ;
  }

  // first look for points inside the triangle in decreasing z-order
  while (p != null && Long.compareUnsigned(p.morton, minZ) >= 0) {
    if (p.idx != ear.previous.idx && p.idx != ear.next.idx
          && pointInEar(p.getX(), p.getY(), ear.previous.getX(), ear.previous.getY(), ear.getX(), ear.getY(), ear.next.getX(), ear.next.getY())
          && area(p.previous.getX(), p.previous.getY(), p.getX(), p.getY(), p.next.getX(), p.next.getY()) >= 0) {
        return false;
      }
    p = p.previousZ;
  }
  // then look for points in increasing z-order
  while (n != null &&
      Long.compareUnsigned(n.morton, maxZ) <= 0) {
      if (n.idx != ear.previous.idx && n.idx != ear.next.idx
          && pointInEar(n.getX(), n.getY(), ear.previous.getX(), ear.previous.getY(), ear.getX(), ear.getY(), ear.next.getX(), ear.next.getY())
          && area(n.previous.getX(), n.previous.getY(), n.getX(), n.getY(), n.next.getX(), n.next.getY()) >= 0) {
        return false;
      }
    n = n.nextZ;
  }
  return true;
}
 
Example 3
Source File: Tessellator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Uses morton code for speed to determine whether or not and edge defined by a and b overlaps with a polygon edge */
private static final boolean isMortonEdgeFromPolygon(final Node a, final Node b) {
  // edge bbox (flip the bits so negative encoded values are < positive encoded values)
  final int minTX = StrictMath.min(a.x, b.x) ^ 0x80000000;
  final int minTY = StrictMath.min(a.y, b.y) ^ 0x80000000;
  final int maxTX = StrictMath.max(a.x, b.x) ^ 0x80000000;
  final int maxTY = StrictMath.max(a.y, b.y) ^ 0x80000000;

  // z-order range for the current edge;
  final long minZ = BitUtil.interleave(minTX, minTY);
  final long maxZ = BitUtil.interleave(maxTX, maxTY);

  // now make sure we don't have other points inside the potential ear;

  // look for points inside edge in both directions
  Node p = a.previousZ;
  Node n = a.nextZ;
  while (p != null && Long.compareUnsigned(p.morton, minZ) >= 0
      && n != null && Long.compareUnsigned(n.morton, maxZ) <= 0) {
    if (isPointInLine(p, p.next, a) && isPointInLine(p, p.next, b)) {
      return p.isNextEdgeFromPolygon;
    }
    if (isPointInLine(p, p.previous, a) && isPointInLine(p, p.previous, b)) {
      return p.previous.isNextEdgeFromPolygon;
    }

    p = p.previousZ;

    if (isPointInLine(n, n.next, a) && isPointInLine(n, n.next, b)) {
      return n.isNextEdgeFromPolygon;
    }
    if (isPointInLine(n, n.previous, a) && isPointInLine(n, n.previous, b)) {
      return n.previous.isNextEdgeFromPolygon;
    }

    n = n.nextZ;
  }

  // first look for points inside the edge in decreasing z-order
  while (p != null && Long.compareUnsigned(p.morton, minZ) >= 0) {
    if (isPointInLine(p, p.next, a) && isPointInLine(p, p.next, b)) {
      return p.isNextEdgeFromPolygon;
    }
    if (isPointInLine(p, p.previous, a) && isPointInLine(p, p.previous, b)) {
      return p.previous.isNextEdgeFromPolygon;
    }
    p = p.previousZ;
  }
  // then look for points in increasing z-order
  while (n != null &&
      Long.compareUnsigned(n.morton, maxZ) <= 0) {
    if (isPointInLine(n, n.next, a) && isPointInLine(n, n.next, b)) {
      return n.isNextEdgeFromPolygon;
    }
    if (isPointInLine(n, n.previous, a) && isPointInLine(n, n.previous, b)) {
      return n.previous.isNextEdgeFromPolygon;
    }
    n = n.nextZ;
  }
  return false;
}
 
Example 4
Source File: MortonEncoder.java    From crate with Apache License 2.0 3 votes vote down vote up
/**
 * Main encoding method to quantize lat/lon points and bit interleave them into a binary morton code
 * in the range of 0x00000000... : 0xFFFFFFFF...
 *
 * @param latitude latitude value: must be within standard +/-90 coordinate bounds.
 * @param longitude longitude value: must be within standard +/-180 coordinate bounds.
 * @return bit interleaved encoded values as a 64-bit {@code long}
 * @throws IllegalArgumentException if latitude or longitude is out of bounds
 */
public static long encode(double latitude, double longitude) {
    checkLatitude(latitude);
    checkLongitude(longitude);
    // encode lat/lon flipping the sign bit so negative ints sort before positive ints
    final int latEnc = encodeLatitude(latitude) ^ 0x80000000;
    final int lonEnc = encodeLongitude(longitude) ^ 0x80000000;
    return BitUtil.interleave(lonEnc, latEnc);
}