Search in sources :

Example 21 with Profile

use of ini.trakem2.display.Profile 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 22 with Profile

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

the class Render method renderObject.

/**
 * Accepts a 'profile_list' Thing and composes a new Ob
 */
private void renderObject(Thing profile_list) {
    // check preconditions
    if (!profile_list.getType().equals("profile_list"))
        return;
    // do not accept an empty profile_list Thing
    final ArrayList<? extends Thing> al = profile_list.getChildren();
    if (null == al || al.size() < 2)
        return;
    // new style: follows profiles links and generates several obs, one per branch, ensuring that there is oly one profile per layer in the generated Ob for the .shapes file.
    // 1 - gather all profiles
    final HashSet<Profile> hs = new HashSet<Profile>();
    for (final Thing child : al) {
        Object ob = child.getObject();
        if (ob instanceof Profile) {
            hs.add((Profile) ob);
        } else {
            Utils.log2("Render: skipping non Profile class child");
        }
    }
    String name = profile_list.getParent().getTitle();
    final ArrayList<String> al_used_names = new ArrayList<String>();
    // make unique object name, since it'll be the group
    String name2 = name;
    int k = 1;
    while (ht_objects.containsKey(name2)) {
        name2 = name + "-" + k;
        k++;
    }
    name = name2;
    al_used_names.add(name);
    // 2 - start at the last found profile with the lowest Z, and recurse until done
    // Utils.log2("Calling renderSubObjects with " + hs.size() + " profiles");
    renderSubObjects(hs, al_used_names);
/* //old style, assumes a single profile per section
		Profile[] profiles = new Profile[al.size()];
		Iterator it = al.iterator();
		int i = 0;
		while (it.hasNext()) {
			Thing child = (Thing)it.next();
			Displayable displ = (Displayable)child.getObject();
			profiles[i] = (Profile)displ; //this cast is safe (as long as I'm the only programmer and I remember that Thing objects added to a 'profile_list' Thing are of class Profile only)
			i++;
		}
		// make unique object name, since it'll be the group
		String name = profile_list.getParent().getTitle();
		String name2 = name;
		int k = 1;
		while (ht_objects.containsKey(name2)) {
			name2 = name + "_" + k;
			k++;
		}
		name = name2;
		// store
		ht_objects.put(name, new Ob(name, profiles));
		*/
}
Also used : ArrayList(java.util.ArrayList) Profile(ini.trakem2.display.Profile) Thing(ini.trakem2.tree.Thing) ProjectThing(ini.trakem2.tree.ProjectThing) HashSet(java.util.HashSet)

Example 23 with Profile

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

the class Render method accumulate.

/**
 * Recursive; returns the last added profile.
 */
private Profile accumulate(final HashSet<Profile> hs_done, final ArrayList<Profile> al, final Profile step, int z_trend) {
    final HashSet<Displayable> hs_linked = step.getLinked(Profile.class);
    if (al.size() > 1 && hs_linked.size() > 2) {
        // base found
        return step;
    }
    double step_z = step.getLayer().getZ();
    Profile next_step = null;
    boolean started = false;
    for (final Displayable ob : hs_linked) {
        // loop only one cycle, to move only in one direction
        if (al.contains(ob) || started || hs_done.contains(ob))
            continue;
        started = true;
        next_step = (Profile) ob;
        double next_z = next_step.getLayer().getZ();
        if (0 == z_trend) {
            // define trend
            if (next_z > step_z) {
                z_trend = 1;
            } else {
                z_trend = -1;
            }
            // add!
            al.add(next_step);
        } else {
            // if the z trend is broken, finish
            if ((next_z > step_z && 1 == z_trend) || (next_z < step_z && -1 == z_trend)) {
                // z trend continues
                al.add(next_step);
            } else {
                // z trend broken
                next_step = null;
            }
        }
    }
    Profile last = step;
    // Utils.log2("next_step is " + next_step);
    if (null != next_step) {
        hs_done.add(next_step);
        last = accumulate(hs_done, al, next_step, z_trend);
    }
    // Utils.log2("returning last " + last);
    return last;
}
Also used : Displayable(ini.trakem2.display.Displayable) Profile(ini.trakem2.display.Profile)

Example 24 with Profile

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

the class Render method renderSubObjects.

/**
 * Render an object from the given profile, following the chain of links, until reaching a profile that is linked to more than two profiles.
 */
private void renderSubObjects(final HashSet<Profile> hs_all, final ArrayList<String> al_used_names) {
    int size = hs_all.size();
    Profile[] p = new Profile[size];
    hs_all.toArray(p);
    // collect starts and ends
    HashSet<Profile> hs_bases = new HashSet<Profile>();
    HashSet<Profile> hs_done = new HashSet<Profile>();
    do {
        Profile base = null;
        // choose among existing bases
        if (hs_bases.size() > 0) {
            base = (Profile) hs_bases.iterator().next();
        } else {
            // find a new base, simply by taking the lowest Z or remaining profiles
            double min_z = Double.MAX_VALUE;
            for (int i = 0; i < p.length; i++) {
                if (hs_done.contains(p[i]))
                    continue;
                double z = p[i].getLayer().getZ();
                if (z < min_z) {
                    min_z = z;
                    base = p[i];
                }
            }
            // add base
            if (null != base)
                hs_bases.add(base);
        }
        if (null == base) {
            Utils.log2("No more bases.");
            return;
        }
        // crawl list to get a sequence of profiles in increasing or decreasing Z order, but not mixed z trends
        ArrayList<Profile> al_profiles = new ArrayList<Profile>();
        // Utils.log2("Calling accumulate for base " + base);
        al_profiles.add(base);
        Profile last = accumulate(hs_done, al_profiles, base, 0);
        // if the trend was not empty, add it
        if (last != base) {
            // Utils.log2("creating Ob with " + al_profiles.size());
            // count as done
            hs_done.addAll(al_profiles);
            // add new possible base (which may have only 2 links if it was from a broken Z trend)
            hs_bases.add(last);
            // create 3D object from base to base
            Profile[] profiles = new Profile[al_profiles.size()];
            al_profiles.toArray(profiles);
            String name = createName(al_used_names);
            String name2 = name;
            int k = 1;
            while (ht_objects.containsKey(name2)) {
                name2 = name + "_" + k;
            }
            name = name2;
            al_used_names.add(name);
            ht_objects.put(name, new Ob(name, profiles));
            counter++;
            Utils.log("count: " + counter + " vs " + ht_objects.size());
        // Utils.log2("Storing ob with name=[" + name + "] and n=" + profiles.length);
        } else {
            // remove base
            // Utils.log2("Removing base " + base);
            hs_bases.remove(base);
        }
    } while (0 != hs_bases.size());
}
Also used : ArrayList(java.util.ArrayList) Profile(ini.trakem2.display.Profile) HashSet(java.util.HashSet)

Example 25 with Profile

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

the class Profile method makeTriangles.

/**
 * Make a mesh as a calibrated list of 3D triangles.
 */
private static List<Point3f> makeTriangles(final Profile[] p, final double scale) {
    try {
        final VectorString2D[] sv = new VectorString2D[p.length];
        // dummy initialization
        boolean closed = true;
        final Calibration cal = p[0].getLayerSet().getCalibrationCopy();
        cal.pixelWidth *= scale;
        cal.pixelHeight *= scale;
        for (int i = 0; i < p.length; i++) {
            if (0 == p[i].n_points)
                continue;
            if (0 == i)
                closed = p[i].closed;
            else if (p[i].closed != closed) {
                Utils.log2("All profiles should be either open or closed, not mixed.");
                return null;
            }
            sv[i] = p[i].getPerimeter2D(cal);
        }
        return SkinMaker.generateTriangles(sv, -1, -1, closed);
    } catch (final Exception e) {
        IJError.print(e);
    }
    return null;
}
Also used : VectorString2D(ini.trakem2.vector.VectorString2D) Calibration(ij.measure.Calibration) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException)

Aggregations

Profile (ini.trakem2.display.Profile)11 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)9 Displayable (ini.trakem2.display.Displayable)8 ZDisplayable (ini.trakem2.display.ZDisplayable)8 ProjectThing (ini.trakem2.tree.ProjectThing)8 HashMap (java.util.HashMap)6 GenericDialog (ij.gui.GenericDialog)5 Layer (ini.trakem2.display.Layer)5 Point (java.awt.Point)4 Roi (ij.gui.Roi)3 AreaTree (ini.trakem2.display.AreaTree)3 DLabel (ini.trakem2.display.DLabel)3 LayerSet (ini.trakem2.display.LayerSet)3 Patch (ini.trakem2.display.Patch)3 DBObject (ini.trakem2.persistence.DBObject)3 Color (java.awt.Color)3 TreePath (javax.swing.tree.TreePath)3 ImagePlus (ij.ImagePlus)2 PolygonRoi (ij.gui.PolygonRoi)2