Intersects function

Help
hahnakane
2009-05-24
2013-01-10
  • hahnakane
    hahnakane
    2009-05-24

    how do i use the intersect function in RTree? the function in RTree is:

      public void intersects(Rectangle r, IntProcedure v){
        Node rootNode = getNode(rootNodeId);
        intersects(r, v, rootNode);
      }

    i'm not sure how to pass in the second parameter.

    my code:

    Rtree tree=new RTree();

    //after adding rectangles to the tree, i want to see if a particular rectangle intersects the tree.

    Rectangle rec=new Rectangle(1,2,3,4);
    tree.intersects(rec, ???)

    //Since IntProcedure is an interface, what class implements IntProcedure? Typically I would say IntProcedure intproc= new "Class that implements IntProcedure".
    //I could really use your help

     
    • Aled Morris
      Aled Morris
      2009-05-24

      You need to create a class that implements the TIntProcedure interface. The following example should make it clear:

      Cheers, Aled.

      -------------------------------
      JsiDemo.java

      import gnu.trove.TIntObjectHashMap;
      import gnu.trove.TIntObjectIterator;
      import gnu.trove.TIntProcedure;

      import java.util.Properties;

      import org.apache.log4j.ConsoleAppender;
      import org.apache.log4j.Level;
      import org.apache.log4j.Logger;
      import org.apache.log4j.PatternLayout;

      import com.infomatiq.jsi.Rectangle;
      import com.infomatiq.jsi.SpatialIndex;
      import com.infomatiq.jsi.rtree.RTree;

      public class JsiDemo {
        public static void main(String[] args)
        {
          final Logger rootLogger = Logger.getRootLogger();
          rootLogger.setLevel(Level.DEBUG);
         
          rootLogger.addAppender(new ConsoleAppender(new PatternLayout("%-5p [%t]: %m%n")));
       
          Properties p = new Properties();
          p.setProperty("MinNodeEntries", "10");
          p.setProperty("MaxNodeEntries", "50");
         
          SpatialIndex si = new RTree();
       
          si.init(p);
         
          final TIntObjectHashMap myRectangles = new TIntObjectHashMap();
          myRectangles.put(1, new Rectangle(0, 0, 1, 1));
          myRectangles.put(2, new Rectangle(1, 0, 2, 1));
          myRectangles.put(3, new Rectangle(0, 1, 1, 2));
          myRectangles.put(4, new Rectangle(1, 1, 2, 2));
         
          // Add rectangles to RTree
          TIntObjectIterator iter = myRectangles.iterator();
          while (iter.hasNext()) {
            iter.advance();
            si.add((Rectangle)iter.value(), iter.key());
          }
         
          // Print rectangles that intersect (0.5, -0.5), (1.5, 0.5)
          // Use an anonymous class to implement
          // TIntProcedure.
          rootLogger.info("Finding rectangles that intersect (0.5, -0.5), (1.5, 0.5)");
          si.intersects(new Rectangle(0.5f, -0.5f, 1.5f, 0.5f),
              new TIntProcedure() {
                public boolean execute(int Id) {
                  rootLogger.info("Found intersecting rectangle: Id " + Id + " " + ((Rectangle)myRectangles.get(Id)));
                  return true;
                }
              }
          );
         
          // Or you can explicitly define a class that
          // implements the TIntProcedure interface, if you need to maintain state
          // between the calls to the execute() method.
          class MyIntProcedure implements TIntProcedure
          {
            private StringBuffer ids = new StringBuffer();
            private float totalArea = 0;
           
            public boolean execute(int Id) {
                if (ids.length() > 0) ids.append(" ");
                ids.append(Id);
                totalArea += ((Rectangle)myRectangles.get(Id)).area();         
                return true;
            }
           
            public String getIds() {
              return "(" + ids.toString() + ")";
            }
           
            public float totalArea() {
              return totalArea;
            }
          }
         
          MyIntProcedure myProc = new MyIntProcedure();
         
          rootLogger.info("Finding rectangles that intersect (1.5, 0.5), (2.5, 1.5)");
          si.intersects(new Rectangle(1.5f, 0.5f, 2.5f, 1.5f), myProc);
         
          rootLogger.info("The rectangles " + myProc.getIds() + " " + " have a total area of " + myProc.totalArea());
        }
      }