Search in sources :

Example 16 with Pipe

use of ini.trakem2.display.Pipe in project TrakEM2 by trakem2.

the class DBLoader method fetchPipe.

private Pipe fetchPipe(Project project, long id) throws Exception {
    // strange query, but can't distinguish between pipes and balls otherwise
    ResultSet r = connection.prepareStatement("SELECT ab_displayables.id, title, ab_displayables.width, height, alpha, visible, color_red, color_green, color_blue, ab_zdisplayables.id, ab_pipe_points.pipe_id, ab_displayables.locked, m00, m10, m01, m11, m02, m12 FROM ab_zdisplayables, ab_displayables, ab_pipe_points WHERE ab_zdisplayables.id=ab_displayables.id AND ab_zdisplayables.id=ab_pipe_points.pipe_id AND ab_zdisplayables.id=" + id).executeQuery();
    Pipe p = null;
    if (r.next()) {
        p = new Pipe(project, id, r.getString("title"), (float) r.getDouble("width"), (float) r.getDouble("height"), r.getFloat("alpha"), r.getBoolean("visible"), new Color(r.getInt("color_red"), r.getInt("color_green"), r.getInt("color_blue")), r.getBoolean("locked"), new AffineTransform(r.getDouble("m00"), r.getDouble("m10"), r.getDouble("m01"), r.getDouble("m11"), r.getDouble("m02"), r.getDouble("m12")));
    }
    r.close();
    return p;
}
Also used : Color(java.awt.Color) ResultSet(java.sql.ResultSet) AffineTransform(java.awt.geom.AffineTransform) Pipe(ini.trakem2.display.Pipe)

Example 17 with Pipe

use of ini.trakem2.display.Pipe in project TrakEM2 by trakem2.

the class DBLoader method fetchLayer.

/**
 * Load all objects into the Layer: Profile and Pipe from the hs_pt (full of ProjectThing wrapping them), and Patch, LayerSet, DLabel, etc from the database.
 */
private Layer fetchLayer(Project project, long id, HashMap hs_pt) throws Exception {
    ResultSet r = connection.prepareStatement("SELECT * FROM ab_layers WHERE id=" + id).executeQuery();
    Layer layer = null;
    if (r.next()) {
        long layer_id = r.getLong("id");
        layer = new Layer(project, layer_id, r.getDouble("z"), r.getDouble("thickness"));
        // find the Layer's parent
        long parent_id = r.getLong("layer_set_id");
        Object set = hs_pt.get(new Long(parent_id));
        if (null != set) {
            ((LayerSet) set).addSilently(layer);
        } else {
            Utils.log("Loader.fetchLayer: WARNING no parent for layer " + layer);
        }
        // add the displayables from hs_pt that correspond to this layer (and all other objects that belong to the layer)
        HashMap hs_d = new HashMap();
        ResultSet rd = connection.prepareStatement("SELECT ab_displayables.id, ab_profiles.id, layer_id, stack_index FROM ab_displayables,ab_profiles WHERE ab_displayables.id=ab_profiles.id AND layer_id=" + layer_id).executeQuery();
        while (rd.next()) {
            Long idd = new Long(rd.getLong("id"));
            Object ob = hs_pt.get(idd);
            // Utils.log("Found profile with id=" + idd + " and ob = " + ob);
            if (null != ob) {
                hs_d.put(new Integer(rd.getInt("stack_index")), ob);
            }
        }
        rd.close();
        // fetch LayerSet objects (which are also Displayable), and put them in the hs_pt (this is hackerous)
        ResultSet rls = connection.prepareStatement("SELECT * FROM ab_layer_sets, ab_displayables WHERE ab_layer_sets.id=ab_displayables.id AND ab_layer_sets.parent_layer_id=" + id).executeQuery();
        while (rls.next()) {
            long ls_id = rls.getLong("id");
            LayerSet layer_set = new LayerSet(project, ls_id, rls.getString("title"), (float) rls.getDouble("width"), (float) rls.getDouble("height"), rls.getDouble("rot_x"), rls.getDouble("rot_y"), rls.getDouble("rot_z"), (float) rls.getDouble("layer_width"), (float) rls.getDouble("layer_height"), rls.getBoolean("locked"), rls.getInt("snapshots_mode"), new AffineTransform(rls.getDouble("m00"), rls.getDouble("m10"), rls.getDouble("m01"), rls.getDouble("m11"), rls.getDouble("m02"), rls.getDouble("m12")));
            hs_pt.put(new Long(ls_id), layer_set);
            hs_d.put(new Integer(rls.getInt("stack_index")), layer_set);
            layer_set.setLayer(layer, false);
            // find the pipes (or other possible ZDisplayable objects) in the hs_pt that belong to this LayerSet and add them silently
            ResultSet rpi = connection.prepareStatement("SELECT ab_displayables.id, ab_zdisplayables.id, layer_id, layer_set_id, stack_index FROM ab_displayables,ab_zdisplayables WHERE ab_displayables.id=ab_zdisplayables.id AND layer_set_id=" + ls_id + " ORDER BY stack_index ASC").executeQuery();
            while (rpi.next()) {
                Long idd = new Long(rpi.getLong("id"));
                Object ob = hs_pt.get(idd);
                if (null != ob && ob instanceof ZDisplayable) {
                    layer_set.addSilently((ZDisplayable) ob);
                } else {
                    Utils.log("fetchLayer: failed to add a ZDisplayable to the layer_set. zdispl id = " + idd);
                }
            }
            rpi.close();
        }
        rls.close();
        // add Patch objects from ab_patches joint-called with ab_displayables
        ResultSet rp = connection.prepareStatement("SELECT ab_patches.id, ab_displayables.id, layer_id, title, width, height, stack_index, imp_type, locked, min, max, m00, m10, m01, m11, m02, m12 FROM ab_patches,ab_displayables WHERE ab_patches.id=ab_displayables.id AND ab_displayables.layer_id=" + layer_id).executeQuery();
        while (rp.next()) {
            long patch_id = rp.getLong("id");
            Patch patch = new Patch(project, patch_id, rp.getString("title"), (float) rp.getDouble("width"), (float) rp.getDouble("height"), rp.getInt("o_width"), rp.getInt("o_height"), rp.getInt("imp_type"), rp.getBoolean("locked"), rp.getDouble("min"), rp.getDouble("max"), new AffineTransform(rp.getDouble("m00"), rp.getDouble("m10"), rp.getDouble("m01"), rp.getDouble("m11"), rp.getDouble("m02"), rp.getDouble("m12")));
            // collecting all Displayable objects to reconstruct links
            hs_pt.put(new Long(patch_id), patch);
            hs_d.put(new Integer(rp.getInt("stack_index")), patch);
        }
        rp.close();
        // add DLabel objects
        ResultSet rl = connection.prepareStatement("SELECT ab_labels.id, ab_displayables.id, layer_id, title, width, height, m00, m10, m01, m11, m02, m12, stack_index, font_name, font_style, font_size, ab_labels.type, locked FROM ab_labels,ab_displayables WHERE ab_labels.id=ab_displayables.id AND ab_displayables.layer_id=" + layer_id).executeQuery();
        while (rl.next()) {
            long label_id = rl.getLong("id");
            DLabel label = new DLabel(project, label_id, rl.getString("title"), (float) rl.getDouble("width"), (float) rl.getDouble("height"), rl.getInt("type"), rl.getString("font_name"), rl.getInt("font_style"), rl.getInt("font_size"), rl.getBoolean("locked"), new AffineTransform(rl.getDouble("m00"), rl.getDouble("m10"), rl.getDouble("m01"), rl.getDouble("m11"), rl.getDouble("m02"), rl.getDouble("m12")));
            // collecting all Displayable objects to reconstruct links
            hs_pt.put(new Long(label_id), label);
            hs_d.put(new Integer(rl.getInt("stack_index")), label);
        }
        rl.close();
        // Add silently to the Layer ordered by stack index
        Set e = hs_d.keySet();
        Object[] si = new Object[hs_d.size()];
        si = e.toArray(si);
        // will it sort an array of integers correctly? Who knows!
        Arrays.sort(si);
        for (int i = 0; i < si.length; i++) {
            // Utils.log("Loader layer.addSilently: adding " + (DBObject)hs_d.get(si[i]));
            layer.addSilently((DBObject) hs_d.get(si[i]));
        }
        // find displays and open later, when fully loaded.
        ResultSet rdi = connection.prepareStatement("SELECT * FROM ab_displays WHERE layer_id=" + layer.getId()).executeQuery();
        while (rdi.next()) {
            fetchDisplay(rdi, layer);
        }
        rdi.close();
    }
    r.close();
    return layer;
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) LayerSet(ini.trakem2.display.LayerSet) LayerSet(ini.trakem2.display.LayerSet) HashMap(java.util.HashMap) Layer(ini.trakem2.display.Layer) Point(java.awt.Point) PGpoint(org.postgresql.geometric.PGpoint) ZDisplayable(ini.trakem2.display.ZDisplayable) DLabel(ini.trakem2.display.DLabel) ResultSet(java.sql.ResultSet) AffineTransform(java.awt.geom.AffineTransform) Patch(ini.trakem2.display.Patch)

Example 18 with Pipe

use of ini.trakem2.display.Pipe in project TrakEM2 by trakem2.

the class Pipe method makeTube.

public static double[][][] makeTube(double[] px, double[] py, double[] pz, double[] p_width_i, final int resample, final int parallels, final Calibration cal) {
    int n = px.length;
    // Resampling to get a smoother pipe
    try {
        final VectorString3D vs = new VectorString3D(px, py, pz, false);
        if (null != cal) {
            vs.calibrate(cal);
            for (int i = 0; i < p_width_i.length; i++) p_width_i[i] *= cal.pixelWidth;
        }
        vs.addDependent(p_width_i);
        // Resample to the largest of the two:
        // calibrated
        final double avg_delta = vs.getAverageDelta();
        // can't use resample, pipes would look very segmented
        final double delta = Math.max(avg_delta, 1);
        vs.resample(delta);
        // vs.resample(vs.getAverageDelta() * resample);
        px = vs.getPoints(0);
        py = vs.getPoints(1);
        pz = vs.getPoints(2);
        p_width_i = vs.getDependent(0);
        // Utils.log("lengths:  " + px.length + ", " + py.length + ", " + pz.length + ", " + p_width_i.length);
        n = vs.length();
    } catch (final Exception e) {
        IJError.print(e);
    }
    final double[][][] all_points = new double[n + 2][parallels + 1][3];
    // this was zero when not doing capping
    final int extra = 1;
    for (int cap = 0; cap < parallels + 1; cap++) {
        // p_i[0][0]; //x
        all_points[0][cap][0] = px[0];
        // p_i[1][0]; //y
        all_points[0][cap][1] = py[0];
        // z_values[0];
        all_points[0][cap][2] = pz[0];
        // p_i[0][p_i[0].length-1];
        all_points[all_points.length - 1][cap][0] = px[n - 1];
        // p_i[1][p_i[0].length-1];
        all_points[all_points.length - 1][cap][1] = py[n - 1];
        // z_values[z_values.length-1];
        all_points[all_points.length - 1][cap][2] = pz[n - 1];
    }
    // Math.toRadians(30);
    final double angle = 2 * Math.PI / parallels;
    Vector3 v3_P12;
    Vector3 v3_PR;
    final Vector3[] circle = new Vector3[parallels + 1];
    double sinn, coss;
    final int half_parallels = parallels / 2;
    for (int i = 0; i < n - 1; i++) {
        // Utils.log2(i + " : " + px[i] + ", " + py[i] + ", " + pz[i]);
        // First vector: from one realpoint to the next
        // v3_P12 = new Vector3(p_i[0][i+1] - p_i[0][i], p_i[1][i+1] - p_i[1][i], z_values[i+1] - z_values[i]);
        v3_P12 = new Vector3(px[i + 1] - px[i], py[i + 1] - py[i], pz[i + 1] - pz[i]);
        // BELOW if-else statements needed to correct the orientation of vectors, so there's no discontinuity
        if (v3_P12.y < 0) {
            v3_PR = new Vector3(v3_P12.y, -v3_P12.x, 0);
            v3_PR = v3_PR.normalize(v3_PR);
            v3_PR = v3_PR.scale(p_width_i[i], v3_PR);
            // vectors are perfectly normalized and scaled
            // The problem then must be that they are not properly ortogonal and so appear to have a smaller width.
            // -not only not ortogonal but actually messed up in some way, i.e. bad coords.
            circle[half_parallels] = v3_PR;
            for (int q = half_parallels + 1; q < parallels + 1; q++) {
                sinn = Math.sin(angle * (q - half_parallels));
                coss = Math.cos(angle * (q - half_parallels));
                circle[q] = Vector3.rotate_v_around_axis(v3_PR, v3_P12, sinn, coss);
            }
            circle[0] = circle[parallels];
            for (int qq = 1; qq < half_parallels; qq++) {
                sinn = Math.sin(angle * (qq + half_parallels));
                coss = Math.cos(angle * (qq + half_parallels));
                circle[qq] = Vector3.rotate_v_around_axis(v3_PR, v3_P12, sinn, coss);
            }
        } else {
            // thining problems disappear when both types of y coord are equal, but then shifting appears
            v3_PR = new Vector3(-v3_P12.y, v3_P12.x, 0);
            /*
				Observations:
					-if y coord shifted, then no thinnings but yes shiftings
					-if x coord shifted, THEN PERFECT
					-if both shifted, then both thinnings and shiftings
					-if none shifted, then no shiftings but yes thinnings
				*/
            v3_PR = v3_PR.normalize(v3_PR);
            if (null == v3_PR) {
                Utils.log2("vp_3r is null: most likely a point was repeated in the list, and thus the vector has length zero.");
            }
            v3_PR = v3_PR.scale(p_width_i[i], v3_PR);
            circle[0] = v3_PR;
            for (int q = 1; q < parallels; q++) {
                sinn = Math.sin(angle * q);
                coss = Math.cos(angle * q);
                circle[q] = Vector3.rotate_v_around_axis(v3_PR, v3_P12, sinn, coss);
            }
            circle[parallels] = v3_PR;
        }
        // Adding points to main array
        for (int j = 0; j < parallels + 1; j++) {
            all_points[i + extra][j][0] = /*p_i[0][i]*/
            px[i] + circle[j].x;
            all_points[i + extra][j][1] = /*p_i[1][i]*/
            py[i] + circle[j].y;
            all_points[i + extra][j][2] = /*z_values[i]*/
            pz[i] + circle[j].z;
        }
    }
    for (int k = 0; k < parallels + 1; k++) {
        all_points[n - 1 + extra][k][0] = /*p_i[0][n-1]*/
        px[n - 1] + circle[k].x;
        all_points[n - 1 + extra][k][1] = /*p_i[1][n-1]*/
        py[n - 1] + circle[k].y;
        all_points[n - 1 + extra][k][2] = /*z_values[n-1]*/
        pz[n - 1] + circle[k].z;
    }
    return all_points;
}
Also used : VectorString3D(ini.trakem2.vector.VectorString3D) Vector3(ini.trakem2.utils.Vector3)

Example 19 with Pipe

use of ini.trakem2.display.Pipe in project TrakEM2 by trakem2.

the class Utils method paint.

/**
 * Paints an approximation of the pipe into the set of slices.
 */
public static void paint(final Pipe pipe, final Map<Layer, ImageProcessor> slices, final int value, final float scale) {
    final VectorString3D vs = pipe.asVectorString3D();
    // one pixel
    vs.resample(1);
    final double[] px = vs.getPoints(0);
    final double[] py = vs.getPoints(1);
    final double[] pz = vs.getPoints(2);
    final double[] pr = vs.getDependent(0);
    // For each point
    for (int i = 0; i < px.length - 1; i++) {
        final ImageProcessor ip = slices.get(pipe.getLayerSet().getNearestLayer(pz[i]));
        if (null == ip)
            continue;
        final OvalRoi ov = new OvalRoi((int) ((px[i] - pr[i]) * scale), (int) ((py[i] - pr[i]) * scale), (int) (pr[i] * 2 * scale), (int) (pr[i] * 2 * scale));
        ip.setRoi(ov);
        ip.setValue(value);
        ip.fill(ip.getMask());
    }
}
Also used : ImageProcessor(ij.process.ImageProcessor) VectorString3D(ini.trakem2.vector.VectorString3D) OvalRoi(ij.gui.OvalRoi)

Aggregations

HashSet (java.util.HashSet)7 VectorString3D (ini.trakem2.vector.VectorString3D)6 Color (java.awt.Color)6 HashMap (java.util.HashMap)6 Worker (ini.trakem2.utils.Worker)5 ArrayList (java.util.ArrayList)5 GenericDialog (ij.gui.GenericDialog)4 Project (ini.trakem2.Project)4 ZDisplayable (ini.trakem2.display.ZDisplayable)4 Point (java.awt.Point)4 File (java.io.File)4 SaveDialog (ij.io.SaveDialog)3 DLabel (ini.trakem2.display.DLabel)3 Displayable (ini.trakem2.display.Displayable)3 Patch (ini.trakem2.display.Patch)3 Pipe (ini.trakem2.display.Pipe)3 ProjectThing (ini.trakem2.tree.ProjectThing)3 ImagePlus (ij.ImagePlus)2 PolygonRoi (ij.gui.PolygonRoi)2 Roi (ij.gui.Roi)2