Search in sources :

Example 26 with Displayable

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

the class Display3D method removeFrom3D.

/**
 * Remove all basic type children contained in {@code pt} and its children, recursively.
 *
 * @param pt
 */
public static void removeFrom3D(final ProjectThing pt) {
    final HashSet<ProjectThing> hs = pt.findBasicTypeChildren();
    if (null == hs || 0 == hs.size()) {
        Utils.logAll("Nothing to remove from 3D.");
        return;
    }
    // Ignore Profile instances ("profile_list" takes care of them)
    for (final ProjectThing child : hs) {
        if (child.getByType().equals("profile"))
            continue;
        // Find the LayerSet
        LayerSet lset = null;
        if (child.getType().equals("profile_list")) {
            if (!child.hasChildren())
                continue;
            for (final ProjectThing p : child.getChildren()) {
                if (null != p.getObject() && p.getObject() instanceof Profile) {
                    lset = ((Displayable) p.getObject()).getLayerSet();
                    break;
                }
            }
            if (null == lset)
                continue;
        } else if (child.getType().equals("profile")) {
            // Taken care of by "profile list"
            continue;
        } else {
            final Displayable d = (Displayable) child.getObject();
            if (null == d) {
                Utils.log("Null object for ProjectThing " + child);
                continue;
            }
            lset = d.getLayerSet();
        }
        if (null == lset) {
            Utils.log("No LayerSet found for " + child);
            continue;
        }
        final Display3D d3d = getDisplay(lset);
        if (null == d3d) {
            Utils.log("No Display 3D found for " + child);
            // no Display3D window open
            continue;
        }
        final String oldTitle = d3d.ht_pt_meshes.remove(child);
        if (null == oldTitle) {
            Utils.log("Could not find a title for " + child);
            continue;
        }
        Utils.log("Removed from 3D view: " + oldTitle);
        d3d.getUniverse().removeContent(oldTitle);
    }
}
Also used : ProjectThing(ini.trakem2.tree.ProjectThing)

Example 27 with Displayable

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

the class DisplayCanvas method mouseDragged.

@Override
public void mouseDragged(final MouseEvent me) {
    super.flags = me.getModifiers();
    if (popup)
        return;
    // ban if beyond bounds:
    if (x_p < srcRect.x || y_p < srcRect.y || x_p > srcRect.x + srcRect.width || y_p > srcRect.y + srcRect.height) {
        return;
    }
    if (ProjectToolbar.SELECT == ProjectToolbar.getToolId() && locked) {
        Utils.log2("Selection is locked.");
        return;
    }
    dragging = true;
    x_d_old = x_d;
    y_d_old = y_d;
    // offscreen
    x_d = srcRect.x + (int) (me.getX() / magnification);
    y_d = srcRect.y + (int) (me.getY() / magnification);
    this.xMouse = x_d;
    this.yMouse = y_d;
    // protection:
    final int me_x = me.getX();
    final int me_y = me.getY();
    if (me_x < 0 || me_x > this.getWidth() || me_y < 0 || me_y > this.getHeight()) {
        x_d = x_d_old;
        y_d = y_d_old;
        return;
    }
    final int tool = ProjectToolbar.getToolId();
    switch(tool) {
        case // TODO : create a zooms-area tool
        Toolbar.MAGNIFIER:
            return;
        case Toolbar.HAND:
            final int srx = srcRect.x, sry = srcRect.y;
            scroll(me.getX(), me.getY());
            if (0 != srx - srcRect.x || 0 != sry - srcRect.y) {
                // update the offscreen images.
                update_graphics = true;
                display.getNavigator().repaint(false);
                repaint(true);
            }
            return;
    }
    if (input_disabled2)
        return;
    // debug:
    // Utils.log2("x_d,y_d : " + x_d + "," + y_d + "   x_d_old, y_d_old : " + x_d_old + "," + y_d_old + "  dx, dy : " + (x_d_old - x_d) + "," + (y_d_old - y_d));
    // Code for Matthias' FreehandProfile (TODO this should be done on mousePressed, not on mouseDragged)
    final Displayable active = display.getActive();
    if (null != active && active.getClass() == Profile.class) {
        try {
            if (r == null) {
                r = new Robot(this.getGraphicsConfiguration().getDevice());
            }
        } catch (final AWTException e) {
            e.printStackTrace();
        }
    }
    switch(tool) {
        case ProjectToolbar.PENCIL:
            if (null != active && active.isVisible() && active.getClass() == Profile.class) {
                if (freehandProfile == null)
                    // starting painting out of the DisplayCanvas border
                    return;
                final double dx = x_d - x_d_old;
                final double dy = y_d - y_d_old;
                freehandProfile.mouseDragged(me, x_d, y_d, dx, dy);
                repaint();
                // r.mouseMove( mousePos[0], mousePos[1]);
                return;
            }
            break;
        case Toolbar.RECTANGLE:
        case Toolbar.OVAL:
        case Toolbar.POLYGON:
        case Toolbar.FREEROI:
        case Toolbar.LINE:
        case Toolbar.POLYLINE:
        case Toolbar.FREELINE:
        case Toolbar.ANGLE:
        case Toolbar.POINT:
            // pass the mouse event to superclass ImageCanvas.
            super.mouseDragged(me);
            repaint(false);
            return;
    }
    // no ROIs beyond this point
    if (tool >= ProjectToolbar.SELECT)
        imp.killRoi();
    else
        return;
    // check:
    if (display.isReadOnly())
        return;
    if (null != active && active.isVisible()) {
        // prevent dragging beyond the layer limits
        if (display.getLayer().contains(x_d, y_d, 1)) {
            Rectangle box2;
            switch(tool) {
                case ProjectToolbar.SELECT:
                    display.getMode().mouseDragged(me, x_p, y_p, x_d, y_d, x_d_old, y_d_old);
                    box2 = display.getMode().getRepaintBounds();
                    box.add(box2);
                    // repaint all Displays (where it was and where it is now, hence the sum of both boxes):
                    Display.repaint(display.getLayer(), Selection.PADDING, box, false, active.isLinked() || active.getClass() == Patch.class);
                    // box for next mouse dragged iteration
                    box = box2;
                    break;
                default:
                    active.mouseDragged(me, display.getLayer(), x_p, y_p, x_d, y_d, x_d_old, y_d_old);
                    // the line above must repaint on its own
                    break;
            }
        } else {
            // beyond_srcRect = true;
            Utils.log("DisplayCanvas.mouseDragged: preventing drag beyond layer limits.");
        }
    } else if (display.getMode() instanceof ManualAlignMode || display.getMode() instanceof InspectPatchTrianglesMode) {
        if (display.getLayer().contains(x_d, y_d, 1)) {
            if (tool >= ProjectToolbar.SELECT) {
                display.getMode().mouseDragged(me, x_p, y_p, x_d, y_d, x_d_old, y_d_old);
            }
        }
    }
}
Also used : InspectPatchTrianglesMode(ini.trakem2.display.inspect.InspectPatchTrianglesMode) Rectangle(java.awt.Rectangle) Robot(java.awt.Robot) Point(java.awt.Point) AWTException(java.awt.AWTException)

Example 28 with Displayable

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

the class ManualAlignMode method apply.

@Override
public boolean apply() {
    // Check there's more than one layer
    if (m.size() < 2) {
        Utils.showMessage("Need more than one layer to align!");
        return false;
    }
    // Check that the current layer is one of the layers with landmarks.
    // Will be used as reference
    final Layer ref_layer = display.getLayer();
    if (null == m.get(ref_layer)) {
        Utils.showMessage("Please scroll to a layer with landmarks,\nto be used as reference.");
        return false;
    }
    // Check that all layers have the same number of landmarks
    int n_landmarks = -1;
    for (final Map.Entry<Layer, Landmarks> e : m.entrySet()) {
        final Landmarks lm = e.getValue();
        if (-1 == n_landmarks) {
            n_landmarks = lm.points.size();
            continue;
        }
        if (n_landmarks != lm.points.size()) {
            Utils.showMessage("Can't apply: there are different amounts of landmarks per layer.\nSee the log window.");
            for (final Map.Entry<Layer, Landmarks> ee : m.entrySet()) {
                Utils.log(ee.getValue().points.size() + " landmarks in layer " + ee.getKey());
            }
            return false;
        }
    }
    // Sort Layers by Z
    final TreeMap<Layer, Landmarks> sorted = new TreeMap<Layer, Landmarks>(new Comparator<Layer>() {

        @Override
        public boolean equals(final Object ob) {
            return this == ob;
        }

        @Override
        public int compare(final Layer l1, final Layer l2) {
            // Ascending order
            final double dz = l1.getZ() - l2.getZ();
            if (dz < 0)
                return -1;
            else if (dz > 0)
                return 1;
            else
                return 0;
        }
    });
    sorted.putAll(m);
    int iref = 0;
    for (final Layer la : sorted.keySet()) {
        if (la != ref_layer)
            iref++;
        else
            break;
    }
    // Ok, now ask for a model
    final GenericDialog gd = new GenericDialog("Model");
    gd.addChoice("Model:", Align.Param.modelStrings, Align.Param.modelStrings[1]);
    gd.addCheckbox("Propagate to first layer", 0 != iref);
    ((Component) gd.getCheckboxes().get(0)).setEnabled(0 != iref);
    gd.addCheckbox("Propagate to last layer", sorted.size() - 1 != iref);
    ((Component) gd.getCheckboxes().get(1)).setEnabled(sorted.size() - 1 != iref);
    gd.showDialog();
    if (gd.wasCanceled())
        return false;
    final int model_index = gd.getNextChoiceIndex();
    final boolean propagate_to_first = gd.getNextBoolean();
    final boolean propagate_to_last = gd.getNextBoolean();
    int min;
    // Create a model as desired
    final AbstractAffineModel2D<?> model;
    switch(model_index) {
        case 0:
            min = 1;
            model = new TranslationModel2D();
            break;
        case 1:
            min = 2;
            model = new RigidModel2D();
            break;
        case 2:
            min = 2;
            model = new SimilarityModel2D();
            break;
        case 3:
            min = 3;
            model = new AffineModel2D();
            break;
        default:
            Utils.log("Unknown model index!");
            return false;
    }
    if (n_landmarks < min) {
        Utils.showMessage("Need at least " + min + " landmarks for a " + Align.Param.modelStrings[model_index] + " model");
        return false;
    }
    Bureaucrat.createAndStart(new Worker.Task("Aligning layers with landmarks") {

        @Override
        public void exec() {
            // Find layers with landmarks, in increasing Z.
            // Match in pairs.
            // So, get two submaps: from ref_layer to first, and from ref_layer to last
            // strictly lower Z than ref_layer
            final SortedMap<Layer, Landmarks> first_chunk_ = new TreeMap<Layer, Landmarks>(sorted.headMap(ref_layer));
            // .. so add ref_layer
            first_chunk_.put(ref_layer, m.get(ref_layer));
            // equal or larger Z than ref_layer
            final SortedMap<Layer, Landmarks> second_chunk = sorted.tailMap(ref_layer);
            final SortedMap<Layer, Landmarks> first_chunk;
            // Reverse order of first_chunk
            if (first_chunk_.size() > 1) {
                final SortedMap<Layer, Landmarks> fc = new TreeMap<Layer, Landmarks>(new Comparator<Layer>() {

                    @Override
                    public boolean equals(final Object ob) {
                        return this == ob;
                    }

                    @Override
                    public int compare(final Layer l1, final Layer l2) {
                        // Descending order
                        final double dz = l2.getZ() - l1.getZ();
                        if (dz < 0)
                            return -1;
                        else if (dz > 0)
                            return 1;
                        else
                            return 0;
                    }
                });
                fc.putAll(first_chunk_);
                first_chunk = fc;
            } else {
                first_chunk = first_chunk_;
            }
            final LayerSet ls = ref_layer.getParent();
            final Collection<Layer> affected_layers = new HashSet<Layer>(m.keySet());
            // Gather all Patch instances that will be affected
            final ArrayList<Patch> patches = new ArrayList<Patch>();
            for (final Layer la : m.keySet()) patches.addAll(la.getAll(Patch.class));
            if (propagate_to_first && first_chunk.size() > 1) {
                final Collection<Layer> affected = ls.getLayers().subList(0, ls.indexOf(first_chunk.lastKey()));
                for (final Layer la : affected) {
                    patches.addAll(la.getAll(Patch.class));
                }
                affected_layers.addAll(affected);
            }
            if (propagate_to_last && second_chunk.size() > 1) {
                final Collection<Layer> affected = ls.getLayers().subList(ls.indexOf(second_chunk.lastKey()) + 1, ls.size());
                for (final Layer la : affected) {
                    patches.addAll(la.getAll(Patch.class));
                }
            }
            // Transform segmentations along with patches
            AlignTask.transformPatchesAndVectorData(patches, new Runnable() {

                @Override
                public void run() {
                    // Apply!
                    // TODO: when adding non-linear transforms, use this single line for undo instead of all below:
                    // (these transforms may be non-linear as well, which alter mipmaps.)
                    // ls.addTransformStepWithData(affected_layers);
                    // Setup undo:
                    // Find all images in the range of affected layers,
                    // plus all Displayable of those layers (but Patch instances in a separate DoTransforms step,
                    // to avoid adding a "data" undo for them, which would recreate mipmaps when undone).
                    // plus all ZDisplayable that paint in those layers
                    final HashSet<Displayable> ds = new HashSet<Displayable>();
                    final ArrayList<Displayable> patches = new ArrayList<Displayable>();
                    for (final Layer layer : affected_layers) {
                        for (final Displayable d : layer.getDisplayables()) {
                            if (d.getClass() == Patch.class) {
                                patches.add(d);
                            } else {
                                ds.add(d);
                            }
                        }
                    }
                    for (final ZDisplayable zd : ls.getZDisplayables()) {
                        for (final Layer layer : affected_layers) {
                            if (zd.paintsAt(layer)) {
                                ds.add((Displayable) zd);
                                break;
                            }
                        }
                    }
                    if (ds.size() > 0) {
                        final Displayable.DoEdits step = ls.addTransformStepWithData(ds);
                        if (patches.size() > 0) {
                            final ArrayList<DoStep> a = new ArrayList<DoStep>();
                            a.add(new Displayable.DoTransforms().addAll(patches));
                            step.addDependents(a);
                        }
                    }
                    if (first_chunk.size() > 1) {
                        final AffineTransform aff = align(first_chunk, model);
                        if (propagate_to_first) {
                            for (final Layer la : ls.getLayers().subList(0, ls.indexOf(first_chunk.lastKey()))) {
                                // exclusive last
                                la.apply(Patch.class, aff);
                            }
                        }
                    }
                    if (second_chunk.size() > 1) {
                        final AffineTransform aff = align(second_chunk, model);
                        if (propagate_to_last) {
                            for (final Layer la : ls.getLayers().subList(ls.indexOf(second_chunk.lastKey()) + 1, ls.size())) {
                                // exclusive last
                                la.apply(Patch.class, aff);
                            }
                        }
                    }
                    Display.repaint();
                    // Store current state
                    if (ds.size() > 0) {
                        final Displayable.DoEdits step2 = ls.addTransformStepWithData(ds);
                        if (patches.size() > 0) {
                            final ArrayList<DoStep> a2 = new ArrayList<DoStep>();
                            a2.add(new Displayable.DoTransforms().addAll(patches));
                            step2.addDependents(a2);
                        }
                    }
                }
            });
        }
    }, display.getProject());
    return true;
}
Also used : ArrayList(java.util.ArrayList) Comparator(java.util.Comparator) RigidModel2D(mpicbg.models.RigidModel2D) GenericDialog(ij.gui.GenericDialog) AbstractAffineModel2D(mpicbg.models.AbstractAffineModel2D) AffineModel2D(mpicbg.models.AffineModel2D) Worker(ini.trakem2.utils.Worker) SimilarityModel2D(mpicbg.models.SimilarityModel2D) Component(java.awt.Component) HashSet(java.util.HashSet) TreeMap(java.util.TreeMap) Point(mpicbg.models.Point) SortedMap(java.util.SortedMap) Collection(java.util.Collection) AffineTransform(java.awt.geom.AffineTransform) TranslationModel2D(mpicbg.models.TranslationModel2D) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 29 with Displayable

use of ini.trakem2.display.Displayable 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 30 with Displayable

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

the class ImageJCommandListener method duplicate.

/**
 * Duplicate the active image (if any) and set it as active, so the command is run on it.
 */
private String duplicate(final String command, final Displayable active) {
    if (!isPatch(command, active))
        return null;
    Patch pa = (Patch) active;
    Project project = pa.getProject();
    project.getLoader().releaseToFit((long) (project.getLoader().estimateImageFileSize(pa, 0) * 5));
    // don't show it yet
    ImagePlus imp = new ImagePlus("Copy of " + pa.getTitle(), pa.getImageProcessor().duplicate());
    WindowManager.setTempCurrentImage(imp);
    imp.show();
    // now execute command
    return command;
}
Also used : Project(ini.trakem2.Project) ImagePlus(ij.ImagePlus)

Aggregations

Displayable (ini.trakem2.display.Displayable)50 ArrayList (java.util.ArrayList)36 ZDisplayable (ini.trakem2.display.ZDisplayable)29 Patch (ini.trakem2.display.Patch)27 HashMap (java.util.HashMap)24 HashSet (java.util.HashSet)23 Layer (ini.trakem2.display.Layer)21 Rectangle (java.awt.Rectangle)17 ProjectThing (ini.trakem2.tree.ProjectThing)15 DBObject (ini.trakem2.persistence.DBObject)14 ImagePlus (ij.ImagePlus)13 LayerSet (ini.trakem2.display.LayerSet)12 Point (java.awt.Point)11 AffineTransform (java.awt.geom.AffineTransform)11 Map (java.util.Map)11 GenericDialog (ij.gui.GenericDialog)10 Collection (java.util.Collection)10 Worker (ini.trakem2.utils.Worker)9 Area (java.awt.geom.Area)9 TreeMap (java.util.TreeMap)9