Search in sources :

Example 1 with Content

use of ij3d.Content in project TrakEM2 by trakem2.

the class Display3DGUI method randomizeColors.

public static final void randomizeColors(final Image3DUniverse univ) {
    final ArrayList<Content> cs = new ArrayList<Content>(getOrderedContents(univ));
    for (int i = 0; i < cs.size(); ++i) {
        if (i < colors.length) {
            cs.get(i).setColor(new Color3f(colors[i]));
        } else {
            cs.get(i).setColor(new Color3f((float) Math.random(), (float) Math.random(), (float) Math.random()));
        }
    }
    // Update the color bars if something is selected:
    final Content content = univ.getSelected();
    if (null != content)
        univ.fireContentChanged(content);
}
Also used : Content(ij3d.Content) Color3f(org.scijava.vecmath.Color3f) ArrayList(java.util.ArrayList)

Example 2 with Content

use of ij3d.Content in project TrakEM2 by trakem2.

the class Display3DGUI method newPanelColors.

private static final JPanel newPanelColors(final Image3DUniverse univ) {
    final JPanel p = new JPanel();
    p.setBackground(Color.white);
    final GridBagLayout gb = new GridBagLayout();
    final GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.NORTHWEST;
    c.fill = GridBagConstraints.HORIZONTAL;
    p.setLayout(gb);
    final String[] labels = new String[] { "Red", "Green", "Blue" };
    final JScrollBar[] sliders = new JScrollBar[3];
    final JTextField[] typers = new JTextField[3];
    for (int i = 0; i < 3; ++i) {
        final JScrollBar slider = new JScrollBar(JScrollBar.HORIZONTAL, 255, 1, 0, 256);
        sliders[i] = slider;
        final JTextField typer = new IntegerField(255, 3);
        typers[i] = typer;
        final int k = i;
        slider.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(final AdjustmentEvent e) {
                final Content content = univ.getSelected();
                if (null == content) {
                    Utils.log("Nothing selected!");
                    return;
                }
                Color3f color = content.getColor();
                // default to yellow
                if (null == color)
                    color = new Color3f(1, 1, 0);
                final float[] co = new float[3];
                color.get(co);
                co[k] = e.getValue() / 255.0f;
                content.setColor(new Color3f(co));
                typer.setText(Integer.toString(e.getValue()));
            }
        });
        typer.addKeyListener(new SliderTyperLink(univ, slider, typer));
        final JLabel l = new JLabel(labels[i]);
        // Layout
        c.gridx = 0;
        c.gridy = i;
        gb.setConstraints(l, c);
        p.add(l);
        c.gridx = 1;
        c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        gb.setConstraints(slider, c);
        p.add(slider);
        c.gridx = 2;
        c.weightx = 0;
        c.fill = GridBagConstraints.NONE;
        gb.setConstraints(typer, c);
        p.add(typer);
    }
    // Alpha slider
    c.gridx = 0;
    c.gridy += 1;
    final JLabel aL = new JLabel("Alpha:");
    gb.setConstraints(aL, c);
    p.add(aL);
    c.gridx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1;
    final JScrollBar alphaSlider = new JScrollBar(JScrollBar.HORIZONTAL, 255, 1, 0, 256);
    gb.setConstraints(alphaSlider, c);
    p.add(alphaSlider);
    c.gridx = 2;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0;
    final JTextField alphaTyper = new IntegerField(255, 3);
    gb.setConstraints(alphaTyper, c);
    p.add(alphaTyper);
    alphaSlider.addAdjustmentListener(new AdjustmentListener() {

        @Override
        public void adjustmentValueChanged(final AdjustmentEvent e) {
            final Content content = univ.getSelected();
            if (null == content) {
                Utils.log("Nothing selected!");
                return;
            }
            final float alpha = e.getValue() / 255.0f;
            content.setTransparency(1 - alpha);
            alphaTyper.setText(Integer.toString(e.getValue()));
        }
    });
    alphaTyper.addKeyListener(new SliderTyperLink(univ, alphaSlider, alphaTyper));
    // Button to colorize randomly
    c.gridx = 0;
    c.gridy += 1;
    c.gridwidth = 3;
    c.weightx = 1;
    c.insets = new Insets(15, 4, 4, 4);
    final JButton r = new JButton("Assign random colors to all");
    r.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            randomizeColors(univ);
        }
    });
    gb.setConstraints(r, c);
    p.add(r);
    addTitledLineBorder(p, "Colors");
    univ.addUniverseListener(new UniverseListener() {

        @Override
        public void universeClosed() {
        }

        @Override
        public void transformationUpdated(final View arg0) {
        }

        @Override
        public void transformationStarted(final View arg0) {
        }

        @Override
        public void transformationFinished(final View arg0) {
        }

        @Override
        public void contentSelected(final Content arg0) {
            if (null == arg0) {
                return;
            }
            Color3f color = arg0.getColor();
            // default to yellow
            if (null == color)
                color = new Color3f(1, 1, 0);
            final float[] co = new float[3];
            color.get(co);
            for (int i = 0; i < 3; ++i) {
                // Disallow the slider from firing an event when its value is adjusted
                sliders[i].setValueIsAdjusting(true);
                final int val = (int) (co[i] * 255);
                typers[i].setText(Integer.toString(val));
                sliders[i].setValue(val);
            }
            // After all are set, re-enable, which triggers events (the color will be set three times...)
            for (int i = 0; i < 3; ++i) {
                sliders[i].setValueIsAdjusting(false);
            }
            // Alpha slider:
            alphaSlider.setValueIsAdjusting(true);
            final int alpha = (int) ((1 - arg0.getTransparency()) * 255);
            alphaTyper.setText(Integer.toString(alpha));
            alphaSlider.setValue(alpha);
            alphaSlider.setValueIsAdjusting(false);
        }

        @Override
        public void contentRemoved(final Content arg0) {
        }

        @Override
        public void contentChanged(final Content arg0) {
            if (arg0 == univ.getSelected()) {
                contentSelected(arg0);
            }
        }

        @Override
        public void contentAdded(final Content arg0) {
        }

        @Override
        public void canvasResized() {
        }
    });
    return p;
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) AdjustmentEvent(java.awt.event.AdjustmentEvent) Color3f(org.scijava.vecmath.Color3f) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) IntegerField(ini.trakem2.utils.IntegerField) JTextField(javax.swing.JTextField) UniverseListener(ij3d.UniverseListener) View(org.scijava.java3d.View) JScrollBar(javax.swing.JScrollBar) ActionListener(java.awt.event.ActionListener) Content(ij3d.Content) AdjustmentListener(java.awt.event.AdjustmentListener)

Example 3 with Content

use of ij3d.Content in project TrakEM2 by trakem2.

the class Display3DGUI method newPanelRemoveContents.

private static final JPanel newPanelRemoveContents(final Image3DUniverse univ) {
    final JPanel p = new JPanel();
    p.setBackground(Color.white);
    final GridBagLayout gb = new GridBagLayout();
    final GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.SOUTHWEST;
    c.fill = GridBagConstraints.HORIZONTAL;
    p.setLayout(gb);
    final JLabel label = new JLabel("RegEx:");
    final JTextField regex = new JTextField();
    final JButton remove = new JButton("X");
    final ActionListener a = new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            String s = regex.getText();
            if (0 == s.length())
                return;
            if (!s.startsWith("^"))
                s = "^.*" + s;
            if (!s.endsWith("$"))
                s = s + ".*$";
            Pattern pattern = null;
            try {
                pattern = Pattern.compile(s);
            } catch (final PatternSyntaxException pse) {
                JOptionPane.showMessageDialog(univ.getWindow(), "Error parsing the regular expression:\n" + pse.getMessage());
                return;
            }
            for (final Content c : new ArrayList<Content>(getOrderedContents(univ))) {
                if (pattern.matcher(c.getName()).matches()) {
                    univ.removeContent(c.getName());
                    Utils.log("Removed " + c.getName());
                }
            }
        }
    };
    remove.addActionListener(a);
    regex.addActionListener(a);
    gb.setConstraints(label, c);
    p.add(label);
    c.gridx = 1;
    c.weightx = 1;
    c.fill = GridBagConstraints.BOTH;
    gb.setConstraints(regex, c);
    p.add(regex);
    c.gridx = 2;
    c.weightx = 0;
    c.fill = GridBagConstraints.NONE;
    gb.setConstraints(remove, c);
    p.add(remove);
    addTitledLineBorder(p, "Remove content");
    return p;
}
Also used : JPanel(javax.swing.JPanel) Pattern(java.util.regex.Pattern) GridBagConstraints(java.awt.GridBagConstraints) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) ArrayList(java.util.ArrayList) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) ActionListener(java.awt.event.ActionListener) Content(ij3d.Content) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 4 with Content

use of ij3d.Content in project TrakEM2 by trakem2.

the class Display3D method show.

/**
 * Scan the {@link ProjectThing} children and assign the renderable ones to an existing {@link Display3D} for their {@link LayerSet}, or open a new one. If {@code true == wait && -1 != resample}, then the method returns only when the mesh/es have been added.
 */
public static Future<Vector<Future<Content>>> show(final ProjectThing pt, final boolean wait, final int resample) {
    if (null == pt)
        return null;
    final Future<Vector<Future<Content>>> fu = launchers.submit(new Callable<Vector<Future<Content>>>() {

        @Override
        public Vector<Future<Content>> call() {
            // Scan the given ProjectThing for 3D-viewable items
            // So: find arealist, pipe, ball, and profile_list types
            final HashSet<ProjectThing> hs = pt.findBasicTypeChildren();
            if (null == hs || 0 == hs.size()) {
                Utils.logAll("Node " + pt + " does not contain any 3D-displayable children");
                return null;
            }
            // Remove profile if it lives under a profile_list
            for (final Iterator<ProjectThing> it = hs.iterator(); it.hasNext(); ) {
                final ProjectThing pt = it.next();
                if (null != pt.getObject() && pt.getObject().getClass() == Profile.class && pt.getParent().getType().equals("profile_list")) {
                    it.remove();
                }
            }
            setWaitingCursor();
            // Start new scheduler to publish/add meshes to the 3D Viewer every 5 seconds and when done.
            final Hashtable<Display3D, Vector<Content>> contents = new Hashtable<Display3D, Vector<Content>>();
            final ScheduledExecutorService updater = Executors.newScheduledThreadPool(1);
            final AtomicInteger counter = new AtomicInteger();
            updater.scheduleWithFixedDelay(new Runnable() {

                @Override
                public void run() {
                    // Obtain a copy of the contents queue
                    final HashMap<Display3D, Vector<Content>> m = new HashMap<Display3D, Vector<Content>>();
                    synchronized (contents) {
                        m.putAll(contents);
                        contents.clear();
                    }
                    if (m.isEmpty())
                        return;
                    // Add all to the corresponding Display3D
                    for (final Map.Entry<Display3D, Vector<Content>> e : m.entrySet()) {
                        e.getKey().universe.addContentLater(e.getValue());
                        counter.getAndAdd(e.getValue().size());
                    }
                    Utils.showStatus(new StringBuilder("Rendered ").append(counter.get()).append('/').append(hs.size()).toString());
                }
            }, 100, 4000, TimeUnit.MILLISECONDS);
            // A list of all generated Content objects
            final Vector<Future<Content>> list = new Vector<Future<Content>>();
            for (final Iterator<ProjectThing> it = hs.iterator(); it.hasNext(); ) {
                // obtain the Displayable object under the node
                final ProjectThing child = it.next();
                final Object obc = child.getObject();
                final Displayable displ = obc.getClass().equals(String.class) ? null : (Displayable) obc;
                if (null != displ) {
                    if (displ.getClass().equals(Profile.class)) {
                        // handled by profile_list Thing
                        continue;
                    }
                    if (!displ.isVisible()) {
                        Utils.log("Skipping non-visible node " + displ);
                        continue;
                    }
                }
                // obtain the containing LayerSet
                final Display3D d3d;
                if (null != displ)
                    d3d = Display3D.get(displ.getLayerSet());
                else if (child.getType().equals("profile_list")) {
                    final ArrayList<ProjectThing> al_children = child.getChildren();
                    if (null == al_children || 0 == al_children.size())
                        continue;
                    // else, get the first Profile and get its LayerSet
                    d3d = Display3D.get(((Displayable) ((ProjectThing) al_children.get(0)).getObject()).getLayerSet());
                } else {
                    Utils.log("Don't know what to do with node " + child);
                    d3d = null;
                }
                if (null == d3d) {
                    Utils.log("Could not get a proper 3D display for node " + displ);
                    // java3D not installed most likely
                    return null;
                }
                boolean already;
                synchronized (d3d.ht_pt_meshes) {
                    already = d3d.ht_pt_meshes.containsKey(child);
                }
                if (already) {
                    if (child.getObject() instanceof ZDisplayable) {
                        Utils.log("Updating 3D view of " + child.getObject());
                    } else {
                        Utils.log("Updating 3D view of " + child);
                    }
                }
                list.add(d3d.executors.submit(new Callable<Content>() {

                    @Override
                    public Content call() {
                        Content c = null;
                        try {
                            c = d3d.createMesh(child, displ, resample).call();
                            Vector<Content> vc;
                            synchronized (contents) {
                                vc = contents.get(d3d);
                                if (null == vc)
                                    vc = new Vector<Content>();
                                contents.put(d3d, vc);
                            }
                            vc.add(c);
                        } catch (final Exception e) {
                            IJError.print(e);
                        }
                        return c;
                    }
                }));
                // If it's the last one:
                if (!it.hasNext()) {
                    // Add the concluding task, that waits on all and shuts down the scheduler
                    d3d.executors.submit(new Runnable() {

                        @Override
                        public void run() {
                            // Wait until all are done
                            for (final Future<Content> c : list) {
                                try {
                                    c.get();
                                } catch (final Throwable t) {
                                    IJError.print(t);
                                }
                            }
                            try {
                                // Shutdown scheduler and execute remaining tasks
                                for (final Runnable r : updater.shutdownNow()) {
                                    r.run();
                                }
                            } catch (final Throwable e) {
                                IJError.print(e);
                            }
                            // Reset cursor
                            doneWaiting();
                            Utils.showStatus(new StringBuilder("Done rendering ").append(counter.get()).append('/').append(hs.size()).toString());
                        }
                    });
                }
            }
            return list;
        }
    });
    if (wait && -1 != resample) {
        try {
            fu.get();
        } catch (final Throwable t) {
            IJError.print(t);
        }
    }
    return fu;
}
Also used : HashMap(java.util.HashMap) Iterator(java.util.Iterator) Vector(java.util.Vector) ProjectThing(ini.trakem2.tree.ProjectThing) HashSet(java.util.HashSet) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Hashtable(java.util.Hashtable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Content(ij3d.Content) Future(java.util.concurrent.Future) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with Content

use of ij3d.Content in project TrakEM2 by trakem2.

the class Display3D method showVolume.

public static void showVolume(final Patch p) {
    final Display3D d3d = get(p.getLayerSet());
    d3d.adjustResampling();
    // d3d.universe.resetView();
    final String title = makeTitle(p) + " volume";
    // remove if present
    d3d.universe.removeContent(title);
    final PatchStack ps = p.makePatchStack();
    final ImagePlus imp = get8BitStack(ps);
    final Content ct = d3d.universe.addVoltex(imp, null, title, 0, new boolean[] { true, true, true }, d3d.resample);
    setTransform(ct, ps.getPatch(0));
    // locks the added content
    ct.setLocked(true);
}
Also used : PatchStack(ini.trakem2.imaging.PatchStack) Content(ij3d.Content) ImagePlus(ij.ImagePlus)

Aggregations

Content (ij3d.Content)18 Color3f (org.scijava.vecmath.Color3f)7 CustomContent (uk.ac.sussex.gdsc.smlm.ij.ij3d.CustomContent)5 ArrayList (java.util.ArrayList)4 ImagePlus (ij.ImagePlus)3 Color (java.awt.Color)3 Transform3D (org.scijava.java3d.Transform3D)3 CustomContentInstant (uk.ac.sussex.gdsc.smlm.ij.ij3d.CustomContentInstant)3 ItemGroupNode (uk.ac.sussex.gdsc.smlm.ij.ij3d.ItemGroupNode)3 CustomMesh (customnode.CustomMesh)2 CustomMeshNode (customnode.CustomMeshNode)2 CustomTriangleMesh (customnode.CustomTriangleMesh)2 Calibration (ij.measure.Calibration)2 ContentInstant (ij3d.ContentInstant)2 Image3DUniverse (ij3d.Image3DUniverse)2 PatchStack (ini.trakem2.imaging.PatchStack)2 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2