Пример #1
0
 private void visualize(RNode n, java.io.PrintWriter pw, int x0, int y0, int w, int h) {
   pw.printf(
       "<div style=\"position:absolute; left: %d; top: %d; width: %d; height: %d; border: 1px dashed\">\n",
       x0, y0, w, h);
   pw.println("<pre>");
   pw.println("Node: " + n.toString() + " \n(root==" + (n == root) + ") \n");
   pw.println("Coords start: " + Arrays.toString(n.getMbr().getP_start()) + "\n");
   pw.println("coords end: " + Arrays.toString(n.getMbr().getP_end()) + "\n");
   pw.println("# Children: " + ((n.children() == null) ? 0 : n.children().size()) + "\n");
   if (n instanceof REntry) {
     REntry n1 = (REntry) n;
     pw.println("Content: " + n1.getContent());
   }
   pw.println("isLeaf: " + n.isLeaf() + "\n");
   pw.println("</pre>");
   int numChildren = (n.children() == null) ? 0 : n.children().size();
   for (int i = 0; i < numChildren; i++) {
     visualize(
         n.children().get(i),
         pw,
         (int) (x0 + (i * w / (float) numChildren)),
         y0 + elemHeight,
         (int) (w / (float) numChildren),
         h - elemHeight);
   }
   pw.println("</div>");
 }
Пример #2
0
 private RNode[] makePartition(RNode n) throws DimensionalException {
   numOfPartitions++;
   numOfNodes += 2;
   RNode[] parts = new RNode[] {n, new RNode(n.getMbr(), n.isLeaf(), M_order)};
   parts[1].setParent(n.getParent());
   if (parts[1].getParent() != null) {
     parts[1].getParent().children().add(parts[1]);
   }
   List<RNode> cpyc = new LinkedList<RNode>(n.children());
   n.children().clear();
   RNode[] seeds = getSeeds(cpyc);
   parts[0].children().add(seeds[0]);
   parts[1].children().add(seeds[1]);
   update(parts[0]);
   update(parts[1]);
   while (!cpyc.isEmpty()) {
     if (parts[0].missingEntries(m_order) == 0
         && parts[1].missingEntries(m_order) + cpyc.size() == m_order) {
       parts[1].children().addAll(cpyc);
       cpyc.clear();
       update(parts[0]);
       update(parts[1]);
       return parts;
     } else if (parts[1].missingEntries(m_order) == 0
         && parts[0].missingEntries(m_order) + cpyc.size() == m_order) {
       parts[0].children().addAll(cpyc);
       cpyc.clear();
       update(parts[0]);
       update(parts[1]);
       return parts;
     }
     RNode next = chooseNext(cpyc, seeds), chosen;
     double enl1 = getEnlargement(parts[0], next);
     double enl2 = getEnlargement(parts[1], next);
     if (enl1 < enl2) chosen = parts[0];
     else if (enl2 < enl1) chosen = parts[1];
     else {
       double ar1 = parts[0].getMbr().getArea();
       double ar2 = parts[1].getMbr().getArea();
       if (ar1 < ar2) chosen = parts[0];
       else if (ar2 < ar1) chosen = parts[1];
       else {
         if (parts[0].children().size() < parts[1].children().size()) chosen = parts[0];
         else if (parts[0].children().size() > parts[1].children().size()) chosen = parts[1];
         else chosen = parts[new Random().nextInt(2)];
       }
     }
     chosen.children().add(next);
     update(chosen);
   }
   return parts;
 }
Пример #3
0
  public void insert(REntry e) throws DimensionalException {

    if (e.getDimension() != dimension)
      throw new DimensionalException(
          "Can only insert n-dimensional objects in n-dimensional RTree");
    RNode theLeaf = findFittingLeaf(root, e);
    theLeaf.children().add(e);
    e.setParent(theLeaf);
    size++;
    // numOfNodes++;
    if (theLeaf.children().size() > M_order) {
      RNode[] parts = makePartition(theLeaf);
      refresh(parts[0], parts[1]);
    } else {
      refresh(theLeaf, null);
    }
  }
Пример #4
0
 private RNode findFittingLeaf(RNode n, REntry e) throws DimensionalException {
   if (n.isLeaf()) return n;
   double min = Double.MAX_VALUE;
   List<RNode> minNodes = new ArrayList<RNode>();
   for (RNode nc : n.children()) {
     double d = getEnlargement(nc, e);
     if (d < min) {
       min = d;
       minNodes.clear();
       minNodes.add(nc);
     } else if (d == min) {
       minNodes.add(nc);
     }
   }
   RNode next;
   if (minNodes.size() > 1) next = getMinimalAreaNode(minNodes);
   else next = minNodes.get(0);
   return findFittingLeaf(next, e);
 }
Пример #5
0
 /**
  * shrinks the mbr area to the just necessary
  *
  * @param n
  */
 private void update(RNode n) throws DimensionalException {
   double[] minCoords = new double[dimension];
   double[] maxCoords = new double[dimension];
   for (int i = 0; i < dimension; i++) {
     minCoords[i] = Double.MAX_VALUE;
     maxCoords[i] = -Double.MAX_VALUE;
     for (RNode c : n.children()) {
       c.setParent(n);
       double minp = Math.min(c.getMbr().getP_start()[i], c.getMbr().getP_end()[i]);
       double maxp = Math.max(c.getMbr().getP_end()[i], c.getMbr().getP_start()[i]);
       if (minp < minCoords[i]) {
         minCoords[i] = minp;
       }
       if (maxp > maxCoords[i]) {
         maxCoords[i] = maxp;
       }
     }
   }
   n.setMbr(new MBR(minCoords, maxCoords, dimension));
 }