Example usage for org.apache.commons.math.linear RealVector unitize

List of usage examples for org.apache.commons.math.linear RealVector unitize

Introduction

In this page you can find the example usage for org.apache.commons.math.linear RealVector unitize.

Prototype

void unitize();

Source Link

Document

Converts this vector into a unit vector.

Usage

From source file:net.ostis.scpdev.scg.geometry.LayoutRunnable.java

private void calculateForces() {
    int n = nodes.size();

    RealVector[] forces = new RealVector[n];
    for (int i = 0; i < n; ++i)
        forces[i] = new ArrayRealVector(nullVector);

    Map<IFigure, Integer> obj_f = new HashMap<IFigure, Integer>();

    ///*w  w w  . j a  v  a  2  s. c  om*/
    // Calculation repulsion forces.
    //
    for (int idx = 0; idx < n; ++idx) {
        SCgNodeShape obj = nodes.get(idx);

        obj_f.put(obj, idx);

        Point p1 = translateToCenter(obj.getBounds().getLocation());

        RealVector p1v = new ArrayRealVector(2);
        p1v.setEntry(0, p1.x);
        p1v.setEntry(1, p1.y);

        double l = nullVector.getDistance(p1v);

        RealVector f = p1v.mapMultiply(gravity * (l - 3.0));

        forces[idx] = forces[idx].subtract(f);

        for (int jdx = idx + 1; jdx < n; ++jdx) {
            Point p2 = translateToCenter(nodes.get(jdx).getBounds().getLocation());

            RealVector p2v = new ArrayRealVector(2);
            p2v.setEntry(0, p2.x);
            p2v.setEntry(1, p2.y);

            l = p1v.getDistance(p2v) / 50;

            if (l > max_rep_length)
                continue;

            if (l > 0.5) {
                f = p1v.subtract(p2v).mapMultiply(repulsion / l / l);
            } else {
                f = new ArrayRealVector(new double[] { Math.cos(0.17 * idx) * length * 7,
                        Math.sin(0.17 * (idx + 1)) * length * 7 });
            }

            forces[idx] = forces[idx].add(f);
            forces[jdx] = forces[jdx].subtract(f);
        }
    }

    //
    // Calculation springs.
    //
    for (SCgPairConnection line : lines) {
        SCgPair pair = line.getModel();

        SCgObject begin = pair.getBegin();
        SCgObject end = pair.getEnd();

        if (begin == null || end == null)
            continue;

        IFigure beginFigure = obj2figure.get(begin);
        IFigure endFigure = obj2figure.get(end);

        Point p1 = translateToCenter(beginFigure.getBounds().getLocation());
        Point p2 = translateToCenter(endFigure.getBounds().getLocation());

        RealVector p1v = new ArrayRealVector(2);
        p1v.setEntry(0, p1.x);
        p1v.setEntry(1, p1.y);

        RealVector p2v = new ArrayRealVector(2);
        p2v.setEntry(0, p2.x);
        p2v.setEntry(1, p2.y);

        double l = p1v.getDistance(p2v) / 50;

        RealVector f = null;

        if (l > 0) {
            RealVector pv = p2v.subtract(p1v);
            pv.unitize();

            int cnt = begin.getInputCount() + end.getOutputCount();
            f = pv.mapMultiply(rigidity * (l - Math.max(length, cnt / 3.0)) / l);

            if (nullVector.getDistance(f) > 10)
                f = pv.mapMultiply(rigidity / l);
        } else {
            f = new ArrayRealVector(nullVector);
        }

        if (obj_f.containsKey(beginFigure)) {
            int index = obj_f.get(beginFigure);
            forces[index] = forces[index].add(f);
        }

        if (obj_f.containsKey(endFigure)) {
            int index = obj_f.get(endFigure);
            forces[index] = forces[index].subtract(f);
        }
    }

    double maxf = 0.0;

    for (int idx = 0; idx < n; ++idx) {
        RealVector f = forces[idx];
        f.mapMultiplyToSelf(stepSize);

        maxf = Math.max(maxf, nullVector.getDistance(f));

        IFigure node = nodes.get(idx);
        Point location = translateToCenter(node.getBounds().getLocation());
        location.x += f.getEntry(0);
        location.y += f.getEntry(1);
        node.setLocation(translateFromCenter(location));
    }

    if (maxf > maxForce) {
        stepSize = stepMaxSize;
    } else {
        stepSize *= 0.97;
    }

    needLayout = stepSize > stepMinSize;
}