Example usage for org.apache.commons.math3.geometry.partitioning BSPTree setAttribute

List of usage examples for org.apache.commons.math3.geometry.partitioning BSPTree setAttribute

Introduction

In this page you can find the example usage for org.apache.commons.math3.geometry.partitioning BSPTree setAttribute.

Prototype

public void setAttribute(final Object attribute) 

Source Link

Document

Associate an attribute with the instance.

Usage

From source file:edu.stanford.cfuller.imageanalysistools.util.VoronoiDiagram.java

/**
* Constrcuts a new VoronoiDiagram from a list of input points.
* 
* Each of these points will be assigned a unique region in the final diagram;
* the ordering of the points determines the region label (1-indexed) in the diagram.
* 
* @param pointsInput   a List of Vector2D objects that describe the set of points
*                   around which the diagram will be constructed.
*//*from   w w  w  .j a v  a2s.com*/
public VoronoiDiagram(List<Vector2D> pointsInput) {

    this.regionLookup = new java.util.HashMap<Vector2D, Integer>();

    for (int i = 0; i < pointsInput.size(); i++) {
        regionLookup.put(pointsInput.get(i), i + 1);
    }

    this.diagram = new BSPTree<Euclidean2D>();

    if (pointsInput.size() <= 1) {
        return;
    }

    List<PointPair> allPairs = new java.util.ArrayList<PointPair>();

    for (int i = 0; i < pointsInput.size(); i++) {

        for (int j = i + 1; j < pointsInput.size(); j++) {

            allPairs.add(new PointPair(pointsInput.get(i), pointsInput.get(j)));

        }

    }

    java.util.Collections.sort(allPairs);

    divideRecursive(this.diagram, allPairs);

    for (Vector2D point : pointsInput) {
        BSPTree<Euclidean2D> node = this.diagram.getCell(point);
        node.setAttribute(this.regionLookup.get(point));
    }

}

From source file:edu.stanford.cfuller.imageanalysistools.util.VoronoiDiagram.java

/**
* Gets the number of the region in which a supplied point lies.
* 
* @param point a Vector2D describing the point whose region is being queried.
* @return an int that is the label of the region in which the supplied vector lies.
*          this label corresponds to the (1-indexed) order in which the points were supplied
*          to the constructor./*from  ww  w  .jav a  2 s .  com*/
*/
public int getRegionNumber(Vector2D point) {
    final int unlabeledValue = 100;
    try {
        BSPTree<Euclidean2D> node = this.diagram.getCell(point);

        while (node.getAttribute() == null) {
            if (isLeaf(node)) {
                Integer value = findClosestRegion(point);
                node.setAttribute(value);
                break;
            }
            final double eps = 1e-6; // bump it slightly if it's exactly on an edge.
            Vector2D newpoint = point.add(new Vector2D(eps, 0));
            node = this.diagram.getCell(newpoint);

            if (node.getAttribute() != null)
                break;

            if (isLeaf(node)) {
                Integer value = findClosestRegion(point);
                node.setAttribute(value);
                break;
            }

            newpoint = point.add(new Vector2D(0, eps));
            node = this.diagram.getCell(newpoint);
        }

        return ((Integer) node.getAttribute());
        //return (node.hashCode());
    } catch (ClassCastException e) {
        return 0;
    }
}

From source file:edu.stanford.cfuller.imageanalysistools.util.VoronoiDiagram.java

private void divideRecursive(BSPTree<Euclidean2D> leaf, List<PointPair> pairsToUse) {

    if (pairsToUse.isEmpty())
        return;/*from w  w  w  .j  a  v  a 2s.  c o m*/

    PointPair p = pairsToUse.remove(0);

    while (!leaf.insertCut(this.constructBisectingHyperplane(p.getFirst(), p.getSecond()))) {
        if (pairsToUse.isEmpty())
            return;
        p = pairsToUse.remove(0);
    }

    leaf.setAttribute(null);

    BSPTree<Euclidean2D> minus = leaf.getMinus();
    BSPTree<Euclidean2D> plus = leaf.getPlus();

    minus.setAttribute(null);
    plus.setAttribute(null);

    List<PointPair> secondPairs = new java.util.ArrayList<PointPair>();
    secondPairs.addAll(pairsToUse);

    divideRecursive(minus, pairsToUse);
    divideRecursive(plus, secondPairs);

}