use of ini.trakem2.display.Selection in project TrakEM2 by trakem2.
the class Compare method scoreAllToAll.
/**
* Returns the half matrix of scores, with values copied from one half matrix to the other, and a diagonal of zeros.
* @param distance_type ranges from 0 to 5, and includes: 0=Levenshtein, 1=Dissimilarity, 2=Average physical distance, 3=Median physical distance, 4=Cummulative physical distance and 5=Standard deviation.
*/
public static float[][] scoreAllToAll(final VectorString3D[] vs, final int distance_type, final double delta, final boolean skip_ends, final int max_mut, final float min_chunk, final boolean direct, final boolean substring_matching, final Worker worker) {
final float[][] scores = new float[vs.length][vs.length];
final AtomicInteger ai = new AtomicInteger(0);
final Thread[] threads = MultiThreading.newThreads();
for (int ithread = 0; ithread < threads.length; ithread++) {
threads[ithread] = new Thread() {
@Override
public void run() {
for (int i = ai.getAndIncrement(); i < vs.length; i = ai.getAndIncrement()) {
final VectorString3D vs1 = vs[i];
for (int j = i + 1; j < vs.length; j++) {
if (null != worker && worker.hasQuitted())
return;
// TODO should add 'distance_type' as well for the selection of the best match when not direct.
final Object[] ob = findBestMatch(vs[i], vs[j], delta, skip_ends, max_mut, min_chunk, distance_type, direct, substring_matching);
/*
switch (distance_type) {
case 0: // Levenshtein
scores[i][j] = (float)((Editions)ob[0]).getDistance();
break;
case 1: // dissimilarity
scores[i][j] = (float)((Double)ob[1]).doubleValue();
break;
case 2: // average physical distance between mutation pairs
scores[i][j] = (float)((Editions)ob[0]).getPhysicalDistance(skip_ends, max_mut, min_chunk, true);
break;
case 3: // median physical distance between mutation pairs
scores[i][j] = (float)((Editions)ob[0]).getStatistics(skip_ends, max_mut, min_chunk, false)[3]; // 3 is median
break;
case 4: // cummulative physical distance between mutation pairs
scores[i][j] = (float)((Editions)ob[0]).getPhysicalDistance(skip_ends, max_mut, min_chunk, false);
break;
case 5: // stdDev of distances between mutation pairs
scores[i][j] = (float)((Editions)ob[0]).getStdDev(skip_ends, max_mut, min_chunk);
break;
}
*/
final Editions ed = (Editions) ob[0];
scores[i][j] = (float) getScore(ed, skip_ends, max_mut, min_chunk, distance_type);
// mirror value
scores[j][i] = scores[i][j];
}
}
// //
}
};
}
MultiThreading.startAndJoin(threads);
if (null != worker && worker.hasQuitted())
return null;
return scores;
}
use of ini.trakem2.display.Selection in project TrakEM2 by trakem2.
the class LayerTree method mousePressed.
public void mousePressed(MouseEvent me) {
Object source = me.getSource();
if (!source.equals(this) || !project.isInputEnabled()) {
return;
}
// ignore if doing multiple selection
if (!Utils.isPopupTrigger(me) && (me.isShiftDown() || (!ij.IJ.isMacOSX() && me.isControlDown()))) {
return;
}
int x = me.getX();
int y = me.getY();
// check if there is a multiple selection
TreePath[] paths = this.getSelectionPaths();
if (null != paths && paths.length > 1) {
if (Utils.isPopupTrigger(me)) {
// check that all items are of the same type
String type_first = ((LayerThing) ((DefaultMutableTreeNode) paths[0].getLastPathComponent()).getUserObject()).getType();
for (int i = 1; i < paths.length; i++) {
String type = ((LayerThing) ((DefaultMutableTreeNode) paths[i].getLastPathComponent()).getUserObject()).getType();
if (!type.equals(type_first)) {
Utils.showMessage("All selected items must be of the same type for operations on multiple items.");
return;
}
}
// prepare popup menu
JPopupMenu popup = new JPopupMenu();
JMenuItem item = null;
if (type_first.equals("layer")) {
item = new JMenuItem("Reverse layer Z coords");
item.addActionListener(this);
popup.add(item);
item = new JMenuItem("Translate layers in Z...");
item.addActionListener(this);
popup.add(item);
item = new JMenuItem("Scale Z and thickness...");
item.addActionListener(this);
popup.add(item);
item = new JMenuItem("Delete...");
item.addActionListener(this);
popup.add(item);
}
if (popup.getSubElements().length > 0) {
popup.show(this, x, y);
}
}
// disable commands depending upon a single node being selected
selected_node = null;
return;
}
// find the node and set it selected
TreePath path = getPathForLocation(x, y);
if (null == path) {
return;
}
setSelectionPath(path);
selected_node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (2 == me.getClickCount() && !Utils.isPopupTrigger(me) && MouseEvent.BUTTON1 == me.getButton()) {
// create a new Display
LayerThing thing = (LayerThing) selected_node.getUserObject();
DBObject ob = (DBObject) thing.getObject();
if (thing.getType().toLowerCase().replace('_', ' ').equals("layer set") && null == ((LayerSet) ob).getParent()) {
// the top level LayerSet
return;
}
// new Display(ob.getProject(), thing.getType().toLowerCase().equals("layer") ? (Layer)ob : ((LayerSet)ob).getParent());
Display.createDisplay(ob.getProject(), thing.getType().toLowerCase().equals("layer") ? (Layer) ob : ((LayerSet) ob).getParent());
return;
} else if (Utils.isPopupTrigger(me)) {
JPopupMenu popup = getPopupMenu(selected_node);
if (null == popup)
return;
popup.show(this, x, y);
return;
}
}
use of ini.trakem2.display.Selection in project TrakEM2 by trakem2.
the class LayerTree method actionPerformed.
public void actionPerformed(ActionEvent ae) {
try {
String command = ae.getActionCommand();
// commands for multiple selections:
TreePath[] paths = this.getSelectionPaths();
if (null != paths && paths.length > 1) {
if (command.equals("Reverse layer Z coords")) {
// check that all layers belong to the same layer set
// just do it
Layer[] layer = new Layer[paths.length];
LayerSet ls = null;
for (int i = 0; i < paths.length; i++) {
layer[i] = (Layer) ((LayerThing) ((DefaultMutableTreeNode) paths[i].getLastPathComponent()).getUserObject()).getObject();
if (null == ls)
ls = layer[i].getParent();
else if (!ls.equals(layer[i].getParent())) {
Utils.showMessage("To reverse, all layers must belong to the same layer set");
return;
}
}
final ArrayList<Layer> al = new ArrayList<Layer>();
for (int i = 0; i < layer.length; i++) al.add(layer[i]);
ls.addLayerEditedStep(al);
// ASSSUMING layers are already Z ordered! CHECK
for (int i = 0, j = layer.length - 1; i < layer.length / 2; i++, j--) {
double z = layer[i].getZ();
layer[i].setZ(layer[j].getZ());
layer[j].setZ(z);
}
updateList(ls);
ls.addLayerEditedStep(al);
Display.updateLayerScroller(ls);
} else if (command.equals("Translate layers in Z...")) {
GenericDialog gd = ControlWindow.makeGenericDialog("Range");
gd.addMessage("Translate selected range in the Z axis:");
gd.addNumericField("by: ", 0, 4);
gd.showDialog();
if (gd.wasCanceled())
return;
// else, displace
double dz = gd.getNextNumber();
if (Double.isNaN(dz)) {
Utils.showMessage("Invalid number");
return;
}
HashSet<LayerSet> hs_parents = new HashSet<LayerSet>();
for (int i = 0; i < paths.length; i++) {
Layer layer = (Layer) ((LayerThing) ((DefaultMutableTreeNode) paths[i].getLastPathComponent()).getUserObject()).getObject();
layer.setZ(layer.getZ() + dz);
hs_parents.add(layer.getParent());
}
for (LayerSet ls : hs_parents) {
updateList(ls);
}
// now update all profile's Z ordering in the ProjectTree
ProjectThing root_pt = project.getRootProjectThing();
for (final ProjectThing pt : root_pt.findChildrenOfType("profile_list")) {
pt.fixZOrdering();
project.getProjectTree().updateList(pt);
}
project.getProjectTree().updateUILater();
// Display.updateLayerScroller((LayerSet)((DefaultMutableTreeNode)getModel().getRoot()).getUserObject());
} else if (command.equals("Delete...")) {
if (!Utils.check("Really remove all selected layers?"))
return;
for (int i = 0; i < paths.length; i++) {
DefaultMutableTreeNode lnode = (DefaultMutableTreeNode) paths[i].getLastPathComponent();
LayerThing lt = (LayerThing) lnode.getUserObject();
Layer layer = (Layer) lt.getObject();
if (!layer.remove(false)) {
Utils.showMessage("Could not delete layer " + layer);
this.updateUILater();
return;
}
if (lt.remove(false)) {
((DefaultTreeModel) this.getModel()).removeNodeFromParent(lnode);
}
}
this.updateUILater();
} else if (command.equals("Scale Z and thickness...")) {
GenericDialog gd = new GenericDialog("Scale Z");
gd.addNumericField("scale: ", 1.0, 2);
gd.showDialog();
double scale = gd.getNextNumber();
if (Double.isNaN(scale) || 0 == scale) {
Utils.showMessage("Imvalid scaling factor: " + scale);
return;
}
for (int i = 0; i < paths.length; i++) {
DefaultMutableTreeNode lnode = (DefaultMutableTreeNode) paths[i].getLastPathComponent();
LayerThing lt = (LayerThing) lnode.getUserObject();
Layer layer = (Layer) lt.getObject();
layer.setZ(layer.getZ() * scale);
layer.setThickness(layer.getThickness() * scale);
}
this.updateUILater();
} else {
Utils.showMessage("Don't know what to do with command " + command + " for multiple selected nodes");
}
return;
}
// commands for single selection:
if (null == selected_node)
return;
LayerThing thing = (LayerThing) selected_node.getUserObject();
LayerThing new_thing = null;
TemplateThing tt = null;
Object ob = null;
int i_position = -1;
if (command.startsWith("new ")) {
String name = command.substring(4).toLowerCase();
if (name.equals("layer")) {
// Create new Layer and add it to the selected node
LayerSet set = (LayerSet) thing.getObject();
Layer new_layer = Layer.create(thing.getProject(), set);
if (null == new_layer)
return;
tt = thing.getChildTemplate("layer");
ob = new_layer;
Display.updateTitle(set);
} else if (name.equals("layer set")) {
// with space in the middle
// Create a new LayerSet and add it in the middle
Layer layer = (Layer) thing.getObject();
LayerSet new_set = layer.getParent().create(layer);
if (null == new_set)
return;
layer.add(new_set);
// add it at the end of the list
// with space, not underscore
tt = thing.getChildTemplate("layer set");
ob = new_set;
i_position = selected_node.getChildCount();
Display.update(layer);
} else {
Utils.log("LayerTree.actionPerformed: don't know what to do with the command: " + command);
return;
}
} else if (command.equals("many new layers...")) {
LayerSet set = (LayerSet) thing.getObject();
List<Layer> layers = Layer.createMany(set.getProject(), set);
// add them to the tree as LayerThing
if (null == layers)
return;
for (Layer la : layers) {
// null layers will be skipped
addLayer(set, la);
}
Display.updateTitle(set);
return;
} else if (command.equals("Show")) {
// create a new Display
DBObject dbo = (DBObject) thing.getObject();
// the top level LayerSet
if (thing.getType().equals("layer_set") && null == ((LayerSet) dbo).getParent())
return;
Display.createDisplay(dbo.getProject(), thing.getType().equals("layer") ? (Layer) dbo : ((LayerSet) dbo).getParent());
return;
} else if (command.equals("Show centered in Display")) {
LayerSet ls = (LayerSet) thing.getObject();
Display.showCentered(ls.getParent(), ls, false, false);
} else if (command.equals("Delete...")) {
remove(true, thing, selected_node);
return;
} else if (command.equals("Import stack...")) {
if (thing.getObject() instanceof LayerSet) {
LayerSet set = (LayerSet) thing.getObject();
Layer layer = null;
if (0 == set.getLayers().size()) {
layer = Layer.create(set.getProject(), set);
if (null == layer)
return;
tt = thing.getChildTemplate("Layer");
ob = layer;
} else
// click on a desired, existing layer.
return;
layer.getProject().getLoader().importStack(layer, null, true);
} else if (thing.getObject() instanceof Layer) {
Layer layer = (Layer) thing.getObject();
layer.getProject().getLoader().importStack(layer, null, true);
return;
}
} else if (command.equals("Import grid...")) {
if (thing.getObject() instanceof Layer) {
Layer layer = (Layer) thing.getObject();
layer.getProject().getLoader().importGrid(layer);
}
} else if (command.equals("Import sequence as grid...")) {
if (thing.getObject() instanceof Layer) {
Layer layer = (Layer) thing.getObject();
layer.getProject().getLoader().importSequenceAsGrid(layer);
}
} else if (command.equals("Import from text file...")) {
if (thing.getObject() instanceof Layer) {
Layer layer = (Layer) thing.getObject();
layer.getProject().getLoader().importImages(layer);
}
} else if (command.equals("Resize LayerSet...")) {
if (thing.getObject() instanceof LayerSet) {
LayerSet ls = (LayerSet) thing.getObject();
ij.gui.GenericDialog gd = ControlWindow.makeGenericDialog("Resize LayerSet");
gd.addNumericField("new width: ", ls.getLayerWidth(), 3);
gd.addNumericField("new height: ", ls.getLayerHeight(), 3);
gd.addChoice("Anchor: ", LayerSet.ANCHORS, LayerSet.ANCHORS[0]);
gd.showDialog();
if (gd.wasCanceled())
return;
float new_width = (float) gd.getNextNumber(), new_height = (float) gd.getNextNumber();
// will complain and prevent cropping existing Displayable objects
ls.setDimensions(new_width, new_height, gd.getNextChoiceIndex());
}
} else if (command.equals("Autoresize LayerSet")) {
if (thing.getObject() instanceof LayerSet) {
LayerSet ls = (LayerSet) thing.getObject();
ls.setMinimumDimensions();
}
} else if (command.equals("Adjust...")) {
if (thing.getObject() instanceof Layer) {
Layer layer = (Layer) thing.getObject();
ij.gui.GenericDialog gd = ControlWindow.makeGenericDialog("Adjust Layer");
gd.addNumericField("new z: ", layer.getZ(), 4);
gd.addNumericField("new thickness: ", layer.getThickness(), 4);
gd.showDialog();
if (gd.wasCanceled())
return;
double new_z = gd.getNextNumber();
layer.setThickness(gd.getNextNumber());
if (new_z != layer.getZ()) {
layer.setZ(new_z);
// move in the tree
/*
DefaultMutableTreeNode child = findNode(thing, this);
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)child.getParent();
parent.remove(child);
// reinsert
int n = parent.getChildCount();
int i = 0;
for (; i < n; i++) {
DefaultMutableTreeNode child_node = (DefaultMutableTreeNode)parent.getChildAt(i);
LayerThing child_thing = (LayerThing)child_node.getUserObject();
if (!child_thing.getType().equals("Layer")) continue;
double iz = ((Layer)child_thing.getObject()).getZ();
if (iz < new_z) continue;
// else, add the layer here, after this one
break;
}
((DefaultTreeModel)this.getModel()).insertNodeInto(child, parent, i);
*/
// fix tree crappiness (empty slot ?!?)
/* // the fix doesn't work. ARGH TODO
Enumeration e = parent.children();
parent.removeAllChildren();
i = 0;
while (e.hasMoreElements()) {
//parent.add((DefaultMutableTreeNode)e.nextElement());
((DefaultTreeModel)this.getModel()).insertNodeInto(child, parent, i);
i++;
}*/
// easier and correct: overkill
updateList(layer.getParent());
// set selected
DefaultMutableTreeNode child = findNode(thing, this);
TreePath treePath = new TreePath(child.getPath());
this.scrollPathToVisible(treePath);
this.setSelectionPath(treePath);
}
}
return;
} else if (command.equals("Rename...")) {
GenericDialog gd = ControlWindow.makeGenericDialog("Rename");
gd.addStringField("new name: ", thing.getTitle());
gd.showDialog();
if (gd.wasCanceled())
return;
project.getRootLayerSet().addUndoStep(new RenameThingStep(thing));
thing.setTitle(gd.getNextString());
project.getRootLayerSet().addUndoStep(new RenameThingStep(thing));
} else if (command.equals("Translate layers in Z...")) {
// / TODO: this method should use multiple selections directly on the tree
if (thing.getObject() instanceof LayerSet) {
LayerSet ls = (LayerSet) thing.getObject();
ArrayList<Layer> al_layers = ls.getLayers();
String[] layer_names = new String[al_layers.size()];
for (int i = 0; i < layer_names.length; i++) {
layer_names[i] = ls.getProject().findLayerThing(al_layers.get(i)).toString();
}
GenericDialog gd = ControlWindow.makeGenericDialog("Range");
gd.addMessage("Translate selected range in the Z axis:");
gd.addChoice("from: ", layer_names, layer_names[0]);
gd.addChoice("to: ", layer_names, layer_names[layer_names.length - 1]);
gd.addNumericField("by: ", 0, 4);
gd.showDialog();
if (gd.wasCanceled())
return;
// else, displace
double dz = gd.getNextNumber();
if (Double.isNaN(dz)) {
Utils.showMessage("Invalid number");
return;
}
int i_start = gd.getNextChoiceIndex();
int i_end = gd.getNextChoiceIndex();
for (int i = i_start; i <= i_end; i++) {
Layer layer = (Layer) al_layers.get(i);
layer.setZ(layer.getZ() + dz);
}
// update node labels and position
updateList(ls);
}
} else if (command.equals("Reverse layer Z coords...")) {
// / TODO: this method should use multiple selections directly on the tree
if (thing.getObject() instanceof LayerSet) {
LayerSet ls = (LayerSet) thing.getObject();
ArrayList<Layer> al_layers = ls.getLayers();
String[] layer_names = new String[al_layers.size()];
for (int i = 0; i < layer_names.length; i++) {
layer_names[i] = ls.getProject().findLayerThing(al_layers.get(i)).toString();
}
GenericDialog gd = ControlWindow.makeGenericDialog("Range");
gd.addMessage("Reverse Z coordinates of selected range:");
gd.addChoice("from: ", layer_names, layer_names[0]);
gd.addChoice("to: ", layer_names, layer_names[layer_names.length - 1]);
gd.showDialog();
if (gd.wasCanceled())
return;
int i_start = gd.getNextChoiceIndex();
int i_end = gd.getNextChoiceIndex();
for (int i = i_start, j = i_end; i < i_end / 2; i++, j--) {
Layer layer1 = (Layer) al_layers.get(i);
double z1 = layer1.getZ();
Layer layer2 = (Layer) al_layers.get(j);
layer1.setZ(layer2.getZ());
layer2.setZ(z1);
}
// update node labels and position
updateList(ls);
}
} else if (command.equals("Search...")) {
Search.showWindow();
} else if (command.equals("Reset layer Z and thickness")) {
LayerSet ls = ((LayerSet) thing.getObject());
List<Layer> layers = ls.getLayers();
ls.addLayerEditedStep(layers);
int i = 0;
for (final Layer la : ls.getLayers()) {
la.setZ(i++);
la.setThickness(1);
}
ls.addLayerEditedStep(layers);
} else {
Utils.log("LayerTree.actionPerformed: don't know what to do with the command: " + command);
return;
}
if (null == tt)
return;
new_thing = new LayerThing(tt, thing.getProject(), ob);
if (-1 == i_position && new_thing.getType().equals("layer")) {
// find the node whose 'z' is larger than z, and add the Layer before that.
// (just because there could be objects other than LayerThing with a Layer in it in the future, so set.getLayers().indexOf(layer) may not be useful)
double z = ((Layer) ob).getZ();
int n = selected_node.getChildCount();
int i = 0;
for (; i < n; i++) {
DefaultMutableTreeNode child_node = (DefaultMutableTreeNode) selected_node.getChildAt(i);
LayerThing child_thing = (LayerThing) child_node.getUserObject();
if (!child_thing.getType().equals("layer")) {
continue;
}
double iz = ((Layer) child_thing.getObject()).getZ();
if (iz < z) {
continue;
}
// else, add the layer here, after this one
break;
}
i_position = i;
}
thing.addChild(new_thing);
DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(new_thing);
((DefaultTreeModel) this.getModel()).insertNodeInto(new_node, selected_node, i_position);
TreePath treePath = new TreePath(new_node.getPath());
this.scrollPathToVisible(treePath);
this.setSelectionPath(treePath);
if (new_thing.getType().equals("layer set")) {
// add the first layer to it, and open it in a Display
LayerSet newls = (LayerSet) new_thing.getObject();
Layer la = new Layer(newls.getProject(), 0, 1, newls);
addLayer(newls, la);
new Display(newls.getProject(), la);
}
} catch (Exception e) {
IJError.print(e);
}
}
use of ini.trakem2.display.Selection in project TrakEM2 by trakem2.
the class DistortionCorrectionTask method correctDistortionFromSelection.
public static final Bureaucrat correctDistortionFromSelection(final Selection selection) {
final List<Patch> patches = new ArrayList<Patch>();
for (final Displayable d : Display.getFront().getSelection().getSelected()) if (d instanceof Patch)
patches.add((Patch) d);
if (patches.size() < 2) {
Utils.log("No images in the selection.");
return null;
}
final Worker worker = new Worker("Lens correction") {
@Override
public final void run() {
try {
startedWorking();
if (!correctDistortionFromSelectionParam.setup(selection))
return;
DistortionCorrectionTask.run(correctDistortionFromSelectionParam.clone(), patches, selection.getActive(), selection.getLayer(), null);
Display.repaint();
} catch (final Exception e) {
IJError.print(e);
} finally {
finishedWorking();
}
}
};
return Bureaucrat.createAndStart(worker, selection.getProject());
}
use of ini.trakem2.display.Selection in project TrakEM2 by trakem2.
the class DistortionCorrectionTask method correctDistortionFromSelectionTask.
public static final Bureaucrat correctDistortionFromSelectionTask(final Selection selection) {
final Worker worker = new Worker("Distortion Correction", false, true) {
@Override
public void run() {
startedWorking();
try {
correctDistortionFromSelection(selection);
Display.repaint(selection.getLayer());
} catch (final Throwable e) {
IJError.print(e);
} finally {
finishedWorking();
}
}
@Override
public void cleanup() {
if (!selection.isEmpty())
selection.getLayer().getParent().undoOneStep();
}
};
return Bureaucrat.createAndStart(worker, selection.getProject());
}
Aggregations