Search in sources :

Example 6 with Profile

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

the class ImageJCommandListener method commandExecuting.

// I know, I could create a hashtable and then map methods of this class to each command key ... this is just easier, and performance-wise nobody cares
// Or even a hastable with String command keys and then a number as value, and use a gigantic switch block. So much pain to write. WHAT I REALLY WANT is a switch that takes a String and is fast because it has its own hash setup.
public String commandExecuting(final String command) {
    // Utils.log2("Command: " + command);
    // 1 - check source
    ImagePlus current = WindowManager.getCurrentImage();
    // not a trakem2 display: continue happily
    if (!(current instanceof FakeImagePlus))
        return command;
    // 2 - identify project
    final FakeImagePlus fimp = (FakeImagePlus) current;
    final Display display = fimp.getDisplay();
    final LayerSet layer_set = display.getLayer().getParent();
    final Project project = display.getProject();
    final ProjectTree ptree = project.getProjectTree();
    final Displayable active = display.getActive();
    final Selection selection = display.getSelection();
    // FILE menu
    if (command.equals("Save")) {
        project.save();
        return null;
    } else // EDIT menu
    if (command.equals("Undo")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Cut")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Copy")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Copy to System")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Paste")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Clear")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Clear Outside")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Fill")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Draw")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Invert")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else // EDIT - SELECTION menu
    if (command.equals("Select All")) {
        if (ProjectToolbar.SELECT == Toolbar.getToolId()) {
            selection.selectAll();
            return null;
        }
        return command;
    } else if (command.equals("Select None")) {
        if (ProjectToolbar.SELECT == Toolbar.getToolId()) {
            display.select(null);
            return null;
        }
        return command;
    } else if (command.equals("Restore Selection")) {
        if (ProjectToolbar.SELECT == Toolbar.getToolId()) {
            selection.restore();
            return null;
        }
        return command;
    } else // IMAGE - TYPE menu
    if (command.equals("8-bit")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("16-bit")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("32-bit")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("8-bit Color")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("RGB Color")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("RGB Stack") || command.equals("HSB Stack")) {
        Utils.showMessage("Can't convert to " + command);
        return null;
    } else // IMAGE - ADJUST menu
    if (command.equals("Brightness/Contrast...")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Window/Level...")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Color Balance...")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Threshold...")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Size...")) {
        if (null != active)
            selection.specify();
        return null;
    } else if (command.equals("Canvas Size...")) {
        display.resizeCanvas();
        return null;
    } else // IMAGE menu
    if (command.equals("Show Info...")) {
        // TODO perhaps it should show only for images ...
        if (null == active) {
            ptree.showInfo(project.getRootProjectThing());
        } else {
            ProjectThing pt = project.findProjectThing(active);
            if (null != pt)
                ptree.showInfo(pt);
        }
        return null;
    } else // IMAGE - COLOR menu
    if (in(command, new String[] { "RGB Split", "RGB Merge...", "Stack to RGB", "Make Composite" })) {
        notAvailable(command);
        return null;
    } else if (command.equals("Show LUT")) {
        return setTempCurrentImage(command, active);
    } else if (command.equals("Edit LUT...")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("Average Color")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("RGB to CIELAB")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else if (command.equals("RGB to Luminance")) {
        // TODO forward to the active image, if any
        niy(command);
        return null;
    } else // IMAGE STACK menu
    if (in(command, new String[] { "Add Slice", "Delete Slice" })) {
        Utils.showMessage("Go to the Layer Tree and right-click to add/delete a layer.");
        return null;
    } else if (command.equals("Next Slice [>]")) {
        display.nextLayer(IJ.shiftKeyDown() ? Event.SHIFT_MASK : 0);
        return null;
    } else if (command.equals("Previous Slice [<]")) {
        display.previousLayer(IJ.shiftKeyDown() ? Event.SHIFT_MASK : 0);
        return null;
    } else if (in(command, new String[] { "Set Slice", "Images to Stack", "Stack to Images", "Make Montage..." })) {
        notAvailable(command);
        return null;
    } else if (command.equals("Reslice [/]...")) {
        // TODO
        niy(command);
        return null;
    } else if (command.equals("Z Project...")) {
        // TODO
        niy(command);
        return null;
    } else if (command.equals("3D Project...")) {
        // TODO
        niy(command);
        return null;
    } else if (command.equals("Plot Z-axis Profile")) {
        // TODO
        niy(command);
        return null;
    } else if (command.equals("Start Animation [\\]")) {
        // TODO
        niy(command);
        return null;
    } else if (command.equals("Stop Animation")) {
        // TODO
        niy(command);
        return null;
    } else // IMAGE menu again
    if (command.equals("Crop")) {
        notAvailable(command);
        return null;
    } else if (in(command, new String[] { "Translate...", "Scale..." })) {
        if (null != active)
            selection.specify();
        return null;
    } else if (command.equals("Duplicate...")) {
        if (null != active && active.getClass().equals(Patch.class)) {
            // TODO stacks?
            // 2.5 security factor: for awt in non-1.6.0 machines
            project.getLoader().releaseToFit((long) (project.getLoader().estimateImageFileSize((Patch) active, 0) * 2.5));
            new ImagePlus(active.getTitle(), ((Patch) active).getImageProcessor().duplicate()).show();
        }
        return null;
    } else if (command.equals("Rename...")) {
        if (null != active) {
            active.adjustProperties();
            Display.updateSelection();
        }
        return null;
    } else // IMAGE ROTATE menu
    if (command.equals("Flip Horizontally")) {
        selection.apply(2, new double[] { -1, 1 });
        return null;
    } else if (command.equals("Flip Vertically")) {
        selection.apply(2, new double[] { 1, -1 });
        return null;
    } else if (command.equals("Rotate 90 Degrees Right")) {
        selection.apply(1, new double[] { 90 });
        return null;
    } else if (command.equals("Rotate 90 Degrees Left")) {
        selection.apply(1, new double[] { -90 });
        return null;
    } else if (command.equals("Arbitrarily...")) {
        if (null != active)
            selection.specify();
        return null;
    } else // IMAGE ZOOM menu
    if (command.equals("To Selection")) {
        Roi roi = fimp.getRoi();
        if (null != roi) {
            Rectangle b = roi.getBounds();
            b.x -= b.width / 2;
            b.y -= b.height / 2;
            b.width *= 2;
            b.height *= 2;
            display.getCanvas().showCentered(b);
        }
        return null;
    } else if (command.equals("View 100%")) {
        // TODO
        niy(command);
        return null;
    } else // ANALYZE menu
    if (command.equals("Measure")) {
        // Minimal measurement: area of closed ROIs, length of unclosed ROIs, calibrated.
        Roi roi = fimp.getRoi();
        if (null != roi) {
            Calibration cal = fimp.getCalibration();
            AffineTransform caff = new AffineTransform();
            caff.scale(cal.pixelWidth, cal.pixelHeight);
            if (M.isAreaROI(roi)) {
                Area area = M.getArea(roi);
                area = area.createTransformedArea(caff);
                ResultsTable rt = Utils.createResultsTable("ROI area", new String[] { "area", "perimeter" });
                rt.incrementCounter();
                rt.addLabel("units", cal.getUnit());
                rt.addValue(0, Math.abs(AreaCalculations.area(area.getPathIterator(null))));
                rt.addValue(1, roi.getLength());
                rt.show("ROI area");
            } else {
                ResultsTable rt = Utils.createResultsTable("ROI length", new String[] { "length" });
                rt.incrementCounter();
                rt.addLabel("units", cal.getUnit());
                rt.addValue(0, roi.getLength());
                rt.show("ROI length");
            }
            return null;
        } else if (null != active) {
            // Measure the active displayable
            if (active.getClass() == Patch.class) {
                // measure like 'm' would in ImageJ for an image
                ImagePlus imp = ((Patch) active).getImagePlus();
                imp.setCalibration(active.getLayer().getParent().getCalibrationCopy());
                IJ.run(imp, "Measure", "");
            } else {
                // Call measure like ProjectThing does
                ProjectThing pt = active.getProject().findProjectThing(active);
                if (active instanceof Profile)
                    ((ProjectThing) pt.getParent()).measure();
                else
                    pt.measure();
            }
            return null;
        }
        Utils.log("Draw a ROI or select an object!");
        return null;
    } else if (in(command, new String[] { "Analyze Particles...", "Histogram", "Plot Profile", "Surface Plot...", "Color Inspector 3D", "3D Surface Plot", "Color Histogram" })) {
        return setTempCurrentImage(command, active);
    } else if (command.equals("Label")) {
        notAvailable(command);
        return null;
    } else // PLUGINS menu
    if (command.equals("Volume Viewer")) {
        return runOnVirtualLayerSet(command, layer_set, display);
    } else if (command.equals("3D Viewer")) {
        // it's virtual and non-caching, will appear as a regular ImageJ stack
        layer_set.createLayerStack(Displayable.class, ImagePlus.COLOR_RGB, display.getDisplayChannelAlphas()).getImagePlus().show();
        return command;
    } else // PROCESS menu and submenus
    if (in(command, new String[] { "FFT", "Fast FFT (2D/3D)" })) {
        return setTempCurrentImage(command, active);
    } else if (in(command, new String[] { "Bandpass Filter...", "Custom Filter...", "FD Math...", "Swap Quadrants", "Convolve...", "Gaussian Blur...", "Median...", "Mean...", "Minimum...", "Maximum...", "Unsharp Mask...", "Variance...", "Show Circular Masks...", "Subtract Background..." })) {
        return duplicate(command, active);
    } else if (in(command, new String[] { "Smooth", "Sharpen", "Find Edges", "Enhance Contrast", "Add Noise", "Add Specified Noise...", "Salt and Pepper", "Despeckle", "Remove Outliers...", "North", "Northeast", "East", "Southeast", "South", "Southwest", "West", "Northwest", "Make Binary", "Convert to Mask", "Find Maxima...", "Erode", "Dilate", "Open ", "Close-", "Outline", "Fill Holes", "Skeletonize", "Distance Map", "Ultimate Points", "Watershed", "Add...", "Subtract...", "Multiply...", "Divide...", "AND...", "OR...", "XOR...", "Min...", "Max...", "Gamma...", "Log", "Exp", "Square", "Square Root", "Reciprocal", "NaN Background", "Abs" })) {
        return duplicate(command, active);
    }
    /*else {
			// continue happily
			//Utils.log2("Skipping " + command);
		}*/
    // If it's part of "Save As", ignore it
    Menu menu = Menus.getSaveAsMenu();
    for (int i = menu.getItemCount() - 1; i > -1; i--) {
        if (command.equals(menu.getItem(i).getActionCommand())) {
            notAvailable(command);
            return null;
        }
    }
    // Give it back to ImageJ
    return command;
}
Also used : Rectangle(java.awt.Rectangle) Calibration(ij.measure.Calibration) ImagePlus(ij.ImagePlus) Roi(ij.gui.Roi) Project(ini.trakem2.Project) Area(java.awt.geom.Area) ProjectTree(ini.trakem2.tree.ProjectTree) AffineTransform(java.awt.geom.AffineTransform) ResultsTable(ij.measure.ResultsTable) Menu(java.awt.Menu) ProjectThing(ini.trakem2.tree.ProjectThing)

Example 7 with Profile

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

the class Profile method generateTriangles.

/**
 * Takes a profile_list, scans for its Profile children, makes sublists of continuous profiles (if they happen to be branched), and then creates triangles for them using weighted vector strings.
 */
public static List<Point3f> generateTriangles(final ProjectThing pt, final double scale) {
    if (!pt.getType().equals("profile_list")) {
        Utils.log2("Profile: ignoring unhandable ProjectThing type.");
        return null;
    }
    // should be sorted by Z already
    final ArrayList<ProjectThing> al = pt.getChildren();
    if (al.size() < 2) {
        Utils.log("profile_list " + pt + " has less than two profiles: can't render in 3D.");
        return null;
    }
    // collect all Profile
    final HashSet<Profile> hs = new HashSet<Profile>();
    for (final ProjectThing child : al) {
        final Object ob = child.getObject();
        if (ob instanceof Profile) {
            hs.add((Profile) ob);
        } else {
            Utils.log2("Render: skipping non Profile class child");
        }
    }
    // Create sublists of profiles, following the chain of links.
    final Profile[] p = new Profile[hs.size()];
    hs.toArray(p);
    // find if at least one is visible
    boolean hidden = true;
    for (int i = 0; i < p.length; i++) {
        if (p[i].visible) {
            hidden = false;
            break;
        }
        if (null == p[i] || 0 == p[i].n_points) {
            Utils.log("Cannot generate triangle mesh: empty profile " + p[i] + (null != p[i] ? " at layer " + p[i].getLayer() : ""));
            return null;
        }
    }
    if (hidden)
        return null;
    // collect starts and ends
    final HashSet<Profile> hs_bases = new HashSet<Profile>();
    final HashSet<Profile> hs_done = new HashSet<Profile>();
    final List<Point3f> triangles = new ArrayList<Point3f>();
    do {
        Profile base = null;
        // choose among existing bases
        if (hs_bases.size() > 0) {
            base = 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;
                final 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.");
            break;
        }
        // crawl list to get a sequence of profiles in increasing or decreasing Z order, but not mixed z trends
        final ArrayList<Profile> al_profiles = new ArrayList<Profile>();
        // Utils.log2("Calling accumulate for base " + base);
        al_profiles.add(base);
        final Profile last = accumulate(hs_done, al_profiles, base, 0);
        // if the trend was not empty, add it
        if (last != base) {
            // 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
            final Profile[] profiles = new Profile[al_profiles.size()];
            al_profiles.toArray(profiles);
            final List<Point3f> tri = makeTriangles(profiles, scale);
            if (null != tri)
                triangles.addAll(tri);
        } else {
            // remove base
            hs_bases.remove(base);
        }
    } while (0 != hs_bases.size());
    return triangles;
}
Also used : ArrayList(java.util.ArrayList) Point3f(org.scijava.vecmath.Point3f) ProjectThing(ini.trakem2.tree.ProjectThing) HashSet(java.util.HashSet)

Example 8 with Profile

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

the class ProjectThing method measure.

/**
 * Call on children things, and on itself if it contains a basic data type directly.
 *  All children of the same type report to the same table.
 *  Result tables are returned without ever displaying them.
 */
public HashMap<Class<?>, ResultsTable> measure(HashMap<Class<?>, ResultsTable> ht) {
    if (null == ht)
        ht = new HashMap<Class<?>, ResultsTable>();
    if (null != object && object instanceof Displayable) {
        Displayable d = (Displayable) object;
        if (d.isVisible()) {
            ResultsTable rt = d.measure(ht.get(d.getClass()));
            if (null != rt)
                ht.put(d.getClass(), rt);
            // Areas:
            if (object instanceof AreaContainer) {
                ResultsTable rta = ((AreaContainer) object).measureAreas(ht.get(AreaContainer.class));
                if (null != rta)
                    ht.put(AreaContainer.class, rta);
            }
        } else {
            Utils.log("Measure: skipping hidden object " + d.getProject().getMeaningfulTitle(d));
        }
    }
    if (null == al_children)
        return ht;
    // profile list: always special ...
    if (template.getType().equals("profile_list") && null != al_children) {
        synchronized (al_children) {
            if (al_children.size() > 1) {
                Profile[] p = new Profile[al_children.size()];
                for (int i = 0; i < al_children.size(); i++) p[i] = (Profile) al_children.get(i).object;
                ResultsTable rt = Profile.measure(p, ht.get(Profile.class), this.id);
                if (null != rt)
                    ht.put(Profile_List.class, rt);
            // return ht; // don't return: do each profile separately as well
            }
        }
    }
    synchronized (al_children) {
        for (ProjectThing child : al_children) child.measure(ht);
    }
    return ht;
}
Also used : ZDisplayable(ini.trakem2.display.ZDisplayable) Displayable(ini.trakem2.display.Displayable) HashMap(java.util.HashMap) AreaContainer(ini.trakem2.display.AreaContainer) ResultsTable(ij.measure.ResultsTable) Profile(ini.trakem2.display.Profile)

Example 9 with Profile

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

the class Project method getParentTitle.

/**
 * Returns the title of the enclosing abstract node in the ProjectTree.
 */
public String getParentTitle(final Displayable d) {
    try {
        ProjectThing thing = findProjectThing(d);
        ProjectThing parent = (ProjectThing) thing.getParent();
        if (d instanceof Profile) {
            // skip the profile_list
            parent = (ProjectThing) parent.getParent();
        }
        if (null == parent)
            Utils.log2("null parent for " + d);
        if (null != parent && null == parent.getObject()) {
            Utils.log2("null ob for parent " + parent + " of " + d);
        }
        // the abstract thing should be enclosing a String object
        return parent.getObject().toString();
    } catch (Exception e) {
        IJError.print(e);
        return null;
    }
}
Also used : ProjectThing(ini.trakem2.tree.ProjectThing) Profile(ini.trakem2.display.Profile)

Example 10 with Profile

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

the class Graph method extractAndShowGraph.

/**
 * Shows a dialog to pick which classes is one interested in.
 */
public static final void extractAndShowGraph(final LayerSet ls) {
    GenericDialog gd = new GenericDialog("Graph elements");
    Class<Displayable>[] c = new Class[] { AreaList.class, AreaTree.class, Ball.class, Connector.class, Patch.class, Pipe.class, Polyline.class, Profile.class, DLabel.class, Treeline.class };
    String[] types = new String[] { "AreaList", "AreaTree", "Ball", "Connector", "Image", "Pipe", "Polyline", "Profile", "Text", "Treeline" };
    boolean[] states = new boolean[] { true, true, false, false, false, false, true, true, false, true };
    assert (c.length == types.length && types.length == states.length);
    for (int i = 0; i < c.length; i++) {
        if (ZDisplayable.class.isAssignableFrom(c[i])) {
            if (!ls.contains(c[i]))
                states[i] = false;
        } else if (!ls.containsDisplayable(c[i]))
            states[i] = false;
    }
    gd.addCheckboxGroup(types.length, 1, types, states, new String[] { "Include only:" });
    gd.showDialog();
    if (gd.wasCanceled())
        return;
    HashSet<Class<Displayable>> only = new HashSet<Class<Displayable>>();
    for (int i = 0; i < types.length; i++) {
        if (gd.getNextBoolean())
            only.add(c[i]);
    }
    Graph.extractAndShowGraph(ls, only);
}
Also used : AreaTree(ini.trakem2.display.AreaTree) Ball(ini.trakem2.display.Ball) Connector(ini.trakem2.display.Connector) Displayable(ini.trakem2.display.Displayable) ZDisplayable(ini.trakem2.display.ZDisplayable) Pipe(ini.trakem2.display.Pipe) AreaList(ini.trakem2.display.AreaList) Profile(ini.trakem2.display.Profile) Point(java.awt.Point) DLabel(ini.trakem2.display.DLabel) Treeline(ini.trakem2.display.Treeline) GenericDialog(ij.gui.GenericDialog) Polyline(ini.trakem2.display.Polyline) Patch(ini.trakem2.display.Patch) HashSet(java.util.HashSet)

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