use of org.scijava.java3d.PickInfo.IntersectionInfo in project GDSC-SMLM by aherbert.
the class ImageJ3DResultsViewer method createImage3DUniverse.
/**
* Creates the image 3D universe with a unique name.
*
* @param title the title
* @param titleList the title list (of titles to ignore)
* @return the image 3D universe
*/
private Image3DUniverse createImage3DUniverse(String title, List<String> titleList) {
// Get a unique name by appending numbers to the end
String title2 = title;
int counter = 2;
while (titleList.contains(title2)) {
title2 = title + " " + (counter++);
}
final Image3DUniverse universe = new Image3DUniverse();
universe.addUniverseListener(new LocalUniverseListener());
universe.setShowBoundingBoxUponSelection(false);
universe.showAttribute(DefaultUniverse.ATTRIBUTE_SCALEBAR, false);
// Capture a canvas mouse click/region and identify the coordinates.
final ImageCanvas3D canvas = (ImageCanvas3D) universe.getCanvas();
final BranchGroup scene = universe.getScene();
final MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent event) {
if (!consumeEvent(event)) {
return;
}
// Consume the event
event.consume();
// This finds the vertex indices of the rendered object.
final Pair<Content, IntersectionInfo> pair = getPickedContent(canvas, scene, event.getX(), event.getY());
if (pair == null) {
// Do the same as the mouseClicked in Image3DUniverse
universe.select(null);
return;
}
// Only process content added from localisations
final Content c = pair.getKey();
if (!(c.getUserData() instanceof ResultsMetaData)) {
// Do the same as the mouseClicked in Image3DUniverse
universe.select(c);
return;
}
final ResultsMetaData data = (ResultsMetaData) c.getUserData();
final MemoryPeakResults results = data.results;
// Look up the localisation from the clicked vertex
final ContentInstant content = c.getCurrent();
int index = -1;
if (content.getContent() instanceof CustomMeshNode) {
final CustomMeshNode node = (CustomMeshNode) content.getContent();
final CustomMesh mesh = node.getMesh();
int vertexCount;
final GeometryArray ga = (GeometryArray) mesh.getGeometry();
// Default to the number of vertices
vertexCount = ga.getValidVertexCount();
final int countPerLocalisation = vertexCount / results.size();
// Determine the localisation
final int vertexIndex = pair.getValue().getVertexIndices()[0];
index = vertexIndex / countPerLocalisation;
} else if (content.getContent() instanceof ItemGroupNode) {
// All shapes have the index as the user data
final Object o = pair.getValue().getGeometry().getUserData();
if (o instanceof Integer) {
index = (Integer) pair.getValue().getGeometry().getUserData();
}
}
if (index == -1) {
return;
}
final PeakResult result = results.get(index);
if (event.getClickCount() > 1) {
// Centre on the localisation
final Point3d coordinate = new Point3d();
coordinate.set(data.points.get(index));
// Handle the local transform of the content ...
final Transform3D vWorldToLocal = getVworldToLocal(content);
vWorldToLocal.transform(coordinate);
universe.centerAt(coordinate);
} else if (event.isShiftDown()) {
// Ctrl+Shift held down to remove selected
data.removeFromSelectionModel(result);
} else {
// Ctrl held down to set selection
data.addToSelectionModel(result);
}
}
private boolean consumeEvent(final MouseEvent event) {
if (event.isConsumed() || event.getButton() != MouseEvent.BUTTON1 || !(event.isControlDown())) {
return false;
}
if (event.getClickCount() == 1) {
return true;
}
return (event.getClickCount() == 2);
}
};
// 0 = ImageCanvas3D
// 1 = DefaultUniverse
// 2 = Image3DUniverse
final MouseListener[] l = canvas.getMouseListeners();
for (int i = 0; i < l.length; i++) {
if (l[i].getClass().getName().contains("Image3DUniverse")) {
// We want to be before the Image3DUniverse to allow consuming the click event.
// Only allow the click event.
// This disables the right-click pop-up menu.
// It doesn't have anything of use for localisations anyway.
canvas.removeMouseListener(l[i]);
canvas.addMouseListener(mouseListener);
canvas.addMouseListener(new MouseListenerWrapper(l[i], MouseListenerWrapper.MOUSE_CLICKED));
}
}
// 0 = ImageCanvas3D
// 1 = DefaultUniverse
// 2 = Image3DUniverse
// 3 = EventCatcher (from scijava)
final MouseMotionListener[] ml = canvas.getMouseMotionListeners();
for (int i = 0; i < ml.length; i++) {
if (ml[i].getClass().getName().contains("Image3DUniverse")) {
// Ignore this as it just shows the name in the IJ status bar
canvas.removeMouseMotionListener(ml[i]);
}
}
// Finally display the window
universe.show();
final ImageWindow3D window = universe.getWindow();
GUI.center(window);
window.setTitle(title2);
WindowManager.addWindow(window);
window.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent event) {
WindowManager.removeWindow(window);
}
});
// Add a new menu for SMLM functionality
createSmlmMenuBar(universe);
return universe;
}
Aggregations