Search in sources :

Example 1 with BranchGroup

use of org.scijava.java3d.BranchGroup in project GDSC-SMLM by aherbert.

the class CustomContentInstant method addCustomSwitch.

/**
 * Adds a custom switchable item to the content. The content can be optionally displayed.
 *
 * <p>The before flag species if the switch should be inserted into the group before the standard
 * content, or added after. This is relevant if using an ordered group and transparent objects.
 * Any transparent object must be drawn after non-transparent objects.
 *
 * @param node the node
 * @param before the before flag
 * @return the switch number
 */
public int addCustomSwitch(Node node, boolean before) {
    if (switchMap == null) {
        switchMap = new TIntObjectHashMap<>();
    }
    final int index = switchMap.size();
    final Switch s = new Switch();
    switchMap.put(index, s);
    s.setCapability(Switch.ALLOW_SWITCH_WRITE);
    s.setCapability(Switch.ALLOW_SWITCH_READ);
    s.setCapability(Group.ALLOW_CHILDREN_WRITE);
    s.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    s.addChild(node);
    // Only a branch group can be added to a live scene
    final BranchGroup bg = new BranchGroup();
    bg.addChild(s);
    if (before) {
        ordered.insertChild(bg, 0);
        customBefore++;
    } else {
        ordered.addChild(bg);
    }
    // Account for the standard switches
    return index;
}
Also used : Switch(org.scijava.java3d.Switch) BranchGroup(org.scijava.java3d.BranchGroup)

Example 2 with BranchGroup

use of org.scijava.java3d.BranchGroup 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;
}
Also used : CustomMeshNode(customnode.CustomMeshNode) Transform3D(org.scijava.java3d.Transform3D) ItemGroupNode(uk.ac.sussex.gdsc.smlm.ij.ij3d.ItemGroupNode) IndexedGeometryArray(org.scijava.java3d.IndexedGeometryArray) GeometryArray(org.scijava.java3d.GeometryArray) WindowAdapter(java.awt.event.WindowAdapter) MouseMotionListener(java.awt.event.MouseMotionListener) PeakResult(uk.ac.sussex.gdsc.smlm.results.PeakResult) MouseListener(java.awt.event.MouseListener) Point3d(org.scijava.vecmath.Point3d) CustomMesh(customnode.CustomMesh) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) IntersectionInfo(org.scijava.java3d.PickInfo.IntersectionInfo) MouseEvent(java.awt.event.MouseEvent) Image3DUniverse(ij3d.Image3DUniverse) ImageCanvas3D(ij3d.ImageCanvas3D) BranchGroup(org.scijava.java3d.BranchGroup) MouseAdapter(java.awt.event.MouseAdapter) ContentInstant(ij3d.ContentInstant) CustomContentInstant(uk.ac.sussex.gdsc.smlm.ij.ij3d.CustomContentInstant) ImageWindow3D(ij3d.ImageWindow3D) Content(ij3d.Content) CustomContent(uk.ac.sussex.gdsc.smlm.ij.ij3d.CustomContent) WindowEvent(java.awt.event.WindowEvent) NamedObject(uk.ac.sussex.gdsc.smlm.data.NamedObject)

Aggregations

BranchGroup (org.scijava.java3d.BranchGroup)2 CustomMesh (customnode.CustomMesh)1 CustomMeshNode (customnode.CustomMeshNode)1 Content (ij3d.Content)1 ContentInstant (ij3d.ContentInstant)1 Image3DUniverse (ij3d.Image3DUniverse)1 ImageCanvas3D (ij3d.ImageCanvas3D)1 ImageWindow3D (ij3d.ImageWindow3D)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 MouseListener (java.awt.event.MouseListener)1 MouseMotionListener (java.awt.event.MouseMotionListener)1 WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 GeometryArray (org.scijava.java3d.GeometryArray)1 IndexedGeometryArray (org.scijava.java3d.IndexedGeometryArray)1 IntersectionInfo (org.scijava.java3d.PickInfo.IntersectionInfo)1 Switch (org.scijava.java3d.Switch)1 Transform3D (org.scijava.java3d.Transform3D)1 Point3d (org.scijava.vecmath.Point3d)1