Search in sources :

Example 6 with Vector3d

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

the class Compare method testCreateOrigin.

public static void testCreateOrigin(final LayerSet ls, final VectorString3D vs1, final VectorString3D vs2, final VectorString3D vs3) {
    try {
        // create vectors
        final double delta = (vs1.getAverageDelta() + vs2.getAverageDelta() + vs3.getAverageDelta()) / 3;
        vs1.resample(delta);
        vs2.resample(delta);
        vs3.resample(delta);
        // 
        final Vector3d[] o = createOrigin(vs1, vs2, vs3, Compare.TRANS_ROT);
        Display3D.addMesh(ls, makeVSFromP(o[0], o[3]), "v1", Color.green);
        Display3D.addMesh(ls, makeVSFromP(o[1], o[3]), "v2", Color.orange);
        Display3D.addMesh(ls, makeVSFromP(o[2], o[3]), "v3", Color.red);
        System.out.println("v1:" + o[0]);
        System.out.println("v2:" + o[1]);
        System.out.println("v3:" + o[2]);
        // create matrix:
        final Matrix3d rotm = new Matrix3d(o[0].x, o[1].x, o[2].x, o[0].y, o[1].y, o[2].y, o[0].z, o[1].z, o[2].z);
        final Transform3D rot = new Transform3D(rotm, new Vector3d(), 1.0);
        rot.invert();
        // DOESN'T WORK // Transform3D trans =  new Transform3D(new Matrix3d(1, 0, 0, 0, 1, 0, 0, 0, 1), new Vector3d(-o[3].x, -o[3].y, -o[3].z), 1.0);
        System.out.println("o3: " + o[3].toString());
        // test:
        for (int i = 0; i < 3; i++) {
            o[i].x += o[3].x;
            o[i].y += o[3].y;
            o[i].z += o[3].z;
        }
        for (int i = 0; i < 3; i++) {
            // can't use translation matrix: doesn't work
            o[i].sub(o[3]);
            // trans.transform(o[i]);
            rot.transform(o[i]);
        }
        // expect: 1, 0, 0
        System.out.println("v1:" + o[0]);
        // expect: 0, 1, 0
        System.out.println("v2:" + o[1]);
        // expect: 0, 0, 1
        System.out.println("v3:" + o[2]);
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Also used : Matrix3d(org.scijava.vecmath.Matrix3d) Vector3d(org.scijava.vecmath.Vector3d) Transform3D(org.scijava.java3d.Transform3D)

Example 7 with Vector3d

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

the class Compare method obtainOrigin.

/**
 * Generate calibrated origin of coordinates.
 */
public static Object[] obtainOrigin(final Line3D[] axes, final int transform_type, final Vector3d[] o_ref) {
    // pipe's axes
    final VectorString3D[] vs = new VectorString3D[3];
    for (int i = 0; i < 3; i++) vs[i] = axes[i].asVectorString3D();
    final Calibration cal = (null != axes[0].getLayerSet() ? axes[0].getLayerSet().getCalibration() : null);
    // 1 - calibrate
    if (null != cal) {
        for (int i = 0; i < 3; i++) vs[i].calibrate(cal);
    }
    // 2 - resample (although it's done before transforming, it's only for aesthetic purposes: it doesn't matter, won't ever go into dynamic programming machinery)
    double delta = 0;
    for (int i = 0; i < 3; i++) delta += vs[i].getAverageDelta();
    delta /= 3;
    for (int i = 0; i < 3; i++) vs[i].resample(delta);
    // return origin vectors for pipe's project
    // requires resampled vs
    final Vector3d[] o = Compare.createOrigin(vs[0], vs[1], vs[2], transform_type, o_ref);
    return new Object[] { vs, o };
}
Also used : VectorString3D(ini.trakem2.vector.VectorString3D) Vector3d(org.scijava.vecmath.Vector3d) Calibration(ij.measure.Calibration)

Example 8 with Vector3d

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

the class Compare method createOrigin.

public static Vector3d[] createOrigin(final VectorString3D x, final VectorString3D y, final VectorString3D z, final int transform_type, final Vector3d[] o_ref) {
    // Aproximate an origin of coordinates
    final VectorString3D[] vs = new VectorString3D[] { z, y, x };
    final ArrayList<Point3d> ps = new ArrayList<Point3d>();
    final int[] dir = new int[] { 1, 1, 1 };
    for (int i = 0; i < vs.length; i++) {
        for (int k = i + 1; k < vs.length; k++) {
            double min_dist = Double.MAX_VALUE;
            int ia = 0, ib = 0;
            for (int a = 0; a < vs[i].length(); a++) {
                for (int b = 0; b < vs[k].length(); b++) {
                    final double d = VectorString3D.distance(vs[i], a, vs[k], b);
                    if (d < min_dist) {
                        min_dist = d;
                        ia = a;
                        ib = b;
                    }
                }
            }
            ps.add(new Point3d((vs[i].getPoint(0, ia) + vs[k].getPoint(0, ib)) / 2, (vs[i].getPoint(1, ia) + vs[k].getPoint(1, ib)) / 2, (vs[i].getPoint(2, ia) + vs[k].getPoint(2, ib)) / 2));
            // determine orientation of the VectorString3D relative to the origin
            if (ia > vs[i].length() / 2)
                dir[i] = -1;
            if (ib > vs[k].length() / 2)
                dir[k] = -1;
        // WARNING: we don't check for the case where it contradicts
        }
    }
    final Vector3d origin = new Vector3d();
    final int len = ps.size();
    for (final Point3d p : ps) {
        p.x /= len;
        p.y /= len;
        p.z /= len;
    }
    for (final Point3d p : ps) origin.add(p);
    // aproximate a vector for each axis
    final Vector3d vz = z.sumVector();
    final Vector3d vy = y.sumVector();
    final Vector3d vx = x.sumVector();
    // adjust orientation, so vectors point away from the origin towards the other end of the vectorstring
    vz.scale(dir[0]);
    vy.scale(dir[1]);
    vx.scale(dir[2]);
    /*
		Utils.log2("dir[0]=" + dir[0]);
		Utils.log2("dir[1]=" + dir[1]);
		Utils.log2("dir[2]=" + dir[2]);
		*/
    Vector3d v1 = vx, v2 = vy;
    final Vector3d v3 = vz;
    if (Compare.TRANS_ROT == transform_type || Compare.TRANS_ROT_SCALE == transform_type) {
        // 1 - compute MEDIAL vector: perpendicular to the plane made by peduncle and dorsal lobe
        final Vector3d vc_medial = new Vector3d();
        vc_medial.cross(vz, vy);
        /* // OLD WAY
			// check orientation:
			Vector3d vc_med = new Vector3d(vc_medial);
			vc_med.normalize();
			Vector3d vx_norm = new Vector3d(vx);
			vx_norm.normalize();
			vc_med.add(vx_norm); // adding the actual medial lobe vector
			// if the sum is smaller, then it means it should be inverted (it was the other side)
			if (vc_med.length() < vx_norm.length()) {
				vc_medial.scale(-1);
				Utils.log2("Mirroring X axis");
			}
			*/
        // 2 - compute DORSAL vector: perpedicular to the plane made by v1 and vc_medial
        final Vector3d vc_dorsal = new Vector3d();
        vc_dorsal.cross(vz, vc_medial);
        // check orientation
        final Vector3d vc_dor = new Vector3d(vc_dorsal);
        vc_dor.add(vy);
        // if the sum is smaller, invert
        if (vc_dor.length() < vy.length()) {
            vc_dorsal.scale(-1);
            Utils.log("Mirroring Y axis");
        }
        /*
			if (Compare.TRANS_ROT == transform_type) {
				// just in case, for rounding issues
				vc_medial.normalize();
				vc_dorsal.normalize();
			}
			*/
        v1 = vc_medial;
        v2 = vc_dorsal;
        if (Compare.TRANS_ROT == transform_type && null != o_ref) {
            // Scale each query axis to length of the reference one
            // so that there are no scaling differences.
            v1.normalize();
            v1.scale(o_ref[0].length());
            v2.normalize();
            v2.scale(o_ref[1].length());
            v3.normalize();
            v3.scale(o_ref[2].length());
        }
    }
    return new Vector3d[] { // X axis : medial lobe
    v1, // Y axis : dorsal lobe
    v2, // Z axis : peduncle
    v3, // x,y,z origin of coordinates
    origin };
}
Also used : VectorString3D(ini.trakem2.vector.VectorString3D) Vector3d(org.scijava.vecmath.Vector3d) Point3d(org.scijava.vecmath.Point3d) ArrayList(java.util.ArrayList)

Example 9 with Vector3d

use of org.scijava.vecmath.Vector3d 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 10 with Vector3d

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

the class M method distancePointToLine.

/**
 * In 3D
 */
public static double distancePointToLine(final double px, final double py, final double pz, final double lx1, final double ly1, final double lz1, final double lx2, final double ly2, final double lz2) {
    final double segment_length = new Vector3d(lx2 - lx1, ly2 - ly1, lz2 - lz1).length();
    if (0 == segment_length)
        return 0;
    final Vector3d cross = new Vector3d();
    cross.cross(new Vector3d(px - lx1, py - ly1, pz - lz1), new Vector3d(px - lx2, py - ly2, pz - lz2));
    return cross.length() / segment_length;
}
Also used : Vector3d(org.scijava.vecmath.Vector3d)

Aggregations

Vector3d (org.scijava.vecmath.Vector3d)11 Calibration (ij.measure.Calibration)4 VectorString3D (ini.trakem2.vector.VectorString3D)3 Transform3D (org.scijava.java3d.Transform3D)3 ArrayList (java.util.ArrayList)2 Point3d (org.scijava.vecmath.Point3d)2 Project (ini.trakem2.Project)1 Line3D (ini.trakem2.display.Line3D)1 ProjectThing (ini.trakem2.tree.ProjectThing)1 Point (java.awt.Point)1 Rectangle (java.awt.Rectangle)1 Area (java.awt.geom.Area)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 TreeMap (java.util.TreeMap)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 Matrix3d (org.scijava.vecmath.Matrix3d)1 Tuple3d (org.scijava.vecmath.Tuple3d)1 Vector2f (org.scijava.vecmath.Vector2f)1