Search in sources :

Example 1 with Vector2f

use of org.scijava.vecmath.Vector2f in project TrakEM2 by trakem2.

the class DisplayCanvas method animateBrowsing.

/**
 * Smoothly move the canvas from its current position until the given rectangle is included within the srcRect.
 * If the given rectangle is larger than the srcRect, it will refuse to work (for now).
 */
public boolean animateBrowsing(final Rectangle target_, final Layer target_layer) {
    // Crop target to world's 2D dimensions
    final Area a = new Area(target_);
    a.intersect(new Area(display.getLayerSet().get2DBounds()));
    final Rectangle target = a.getBounds();
    if (0 == target.width || 0 == target.height) {
        return false;
    }
    // animate at all?
    if (this.srcRect.contains(target) && target_layer == display.getLayer()) {
        // So: don't animate, but at least highlight the target
        playHighlight(target);
        return false;
    }
    // The motion will be displaced by some screen pixels at every time step.
    final int ox = srcRect.x + srcRect.width / 2;
    final int oy = srcRect.y + srcRect.height / 2;
    final int tx = target.x + target.width / 2;
    final int ty = target.y + target.height / 2;
    final Vector2f v = new Vector2f(tx - ox, ty - oy);
    v.normalize();
    v.scale(20 / (float) magnification);
    // The layer range
    final Layer start_layer = display.getLayer();
    /*
		int ithis = display.getLayerSet().indexOf(start_layer);
		int itarget = display.getLayerSet().indexOf(target_layer);
		final java.util.List<Layer> layers = display.getLayerSet().getLayers(ithis, itarget);
		*/
    final Calibration cal = display.getLayerSet().getCalibrationCopy();
    final double pixelWidth = cal.pixelWidth;
    final double pixelHeight = cal.pixelHeight;
    // final double dist_to_travel = Math.sqrt(Math.pow((tx - ox)*pixelWidth, 2) + Math.pow((ty - oy)*pixelHeight, 2)
    // + Math.pow((start_layer.getZ() - target_layer.getZ()) * pixelWidth, 2));
    // vector in calibrated coords between origin and target
    final Vector3d g = new Vector3d((tx - ox) * pixelWidth, (ty - oy) * pixelHeight, (target_layer.getZ() - start_layer.getZ()) * pixelWidth);
    final ScheduledFuture<?>[] sf = new ScheduledFuture[1];
    sf[0] = animate(new Runnable() {

        @Override
        public void run() {
            if (DisplayCanvas.this.srcRect.contains(target)) {
                // reached destination
                if (display.getLayer() != target_layer)
                    display.toLayer(target_layer);
                playHighlight(target);
                cancelAnimation(sf[0]);
            } else {
                setSrcRect(srcRect.x + (int) v.x, srcRect.y + (int) v.y, srcRect.width, srcRect.height);
                // which layer?
                if (start_layer != target_layer) {
                    final int cx = srcRect.x + srcRect.width / 2;
                    final int cy = srcRect.y + srcRect.height / 2;
                    final double dist = Math.sqrt(Math.pow((cx - ox) * pixelWidth, 2) + Math.pow((cy - oy) * pixelHeight, 2) + Math.pow((display.getLayer().getZ() - start_layer.getZ()) * pixelWidth, 2));
                    final Vector3d gg = new Vector3d(g);
                    gg.normalize();
                    gg.scale((float) dist);
                    final Layer la = display.getLayerSet().getNearestLayer(start_layer.getZ() + gg.z / pixelWidth);
                    if (la != display.getLayer()) {
                        display.toLayer(la);
                    }
                }
                display.repaintAll2();
            }
        }
    }, 0, 50, TimeUnit.MILLISECONDS);
    return true;
}
Also used : Area(java.awt.geom.Area) Vector3d(org.scijava.vecmath.Vector3d) Vector2f(org.scijava.vecmath.Vector2f) Rectangle(java.awt.Rectangle) Calibration(ij.measure.Calibration) Point(java.awt.Point) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 2 with Vector2f

use of org.scijava.vecmath.Vector2f in project TrakEM2 by trakem2.

the class DisplayCanvas method animateBrowsing.

/**
 * Smoothly move the canvas from x0,y0,layer0 to x1,y1,layer1
 */
protected void animateBrowsing(final int dx, final int dy) {
    // check preconditions
    final float mag = (float) this.magnification;
    final Rectangle startSrcRect = (Rectangle) this.srcRect.clone();
    // The motion will be displaced by some screen pixels at every time step.
    final Vector2f v = new Vector2f(dx, dy);
    final float sqdist_to_travel = v.lengthSquared();
    v.normalize();
    v.scale(20 / mag);
    // the current deltas
    final Point2f cp = new Point2f(0, 0);
    // 
    final ScheduledFuture<?>[] sf = new ScheduledFuture[1];
    sf[0] = animate(new Runnable() {

        @Override
        public void run() {
            cp.add(v);
            // Utils.log2("advanced by x,y = " + cp.x + ", " + cp.y);
            int x, y;
            if (v.lengthSquared() >= sqdist_to_travel) {
                // set target position
                x = startSrcRect.x + dx;
                y = startSrcRect.y + dy;
                // quit animation
                cancelAnimation(sf[0]);
            } else {
                // set position
                x = startSrcRect.x + (int) (cp.x);
                y = startSrcRect.y + (int) (cp.y);
            }
            setSrcRect(x, y, startSrcRect.width, startSrcRect.height);
            display.repaintAll2();
        }
    }, 0, 50, TimeUnit.MILLISECONDS);
}
Also used : Point2f(org.scijava.vecmath.Point2f) Vector2f(org.scijava.vecmath.Vector2f) Rectangle(java.awt.Rectangle) ScheduledFuture(java.util.concurrent.ScheduledFuture) Point(java.awt.Point)

Aggregations

Point (java.awt.Point)2 Rectangle (java.awt.Rectangle)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 Vector2f (org.scijava.vecmath.Vector2f)2 Calibration (ij.measure.Calibration)1 Area (java.awt.geom.Area)1 Point2f (org.scijava.vecmath.Point2f)1 Vector3d (org.scijava.vecmath.Vector3d)1