use of org.scijava.java3d.View 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;
}
use of org.scijava.java3d.View in project GDSC-SMLM by aherbert.
the class ImageJ3DResultsViewer method menuActionPerformed.
private void menuActionPerformed(ActionEvent event) {
final Object src = event.getSource();
ContentAction action = null;
// Adapted from univ.resetView()
if (src == resetRotation) {
univ.fireTransformationStarted();
// rotate so that y shows downwards
final Transform3D t = new Transform3D();
final AxisAngle4d aa = new AxisAngle4d(1, 0, 0, Math.PI);
t.set(aa);
univ.getRotationTG().setTransform(t);
univ.fireTransformationUpdated();
univ.fireTransformationFinished();
return;
}
if (src == resetTranslation) {
univ.fireTransformationStarted();
final Transform3D t = new Transform3D();
univ.getTranslateTG().setTransform(t);
univ.recalculateGlobalMinMax();
univ.getViewPlatformTransformer().centerAt(univ.getGlobalCenterPoint());
univ.fireTransformationUpdated();
univ.fireTransformationFinished();
return;
}
if (src == resetZoom) {
univ.fireTransformationStarted();
final Transform3D t = new Transform3D();
univ.getZoomTG().setTransform(t);
final Point3d max = new Point3d();
final Point3d min = new Point3d();
univ.getGlobalMaxPoint(max);
univ.getGlobalMinPoint(min);
final float range = (float) (max.x - min.x);
final double d = (range) / Math.tan(Math.PI / 8);
univ.getViewPlatformTransformer().zoomTo(d);
univ.fireTransformationUpdated();
univ.fireTransformationFinished();
return;
}
if (src == updateSettings) {
final ImageJ3DResultsViewerSettings.Builder settings = SettingsManager.readImageJ3DResultsViewerSettings(0).toBuilder();
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
final ResultsSettings.Builder s = ResultsSettings.newBuilder();
ResultsTableSettings localResultsTableSettings = resultsTableSettings.get();
s.setResultsTableSettings(localResultsTableSettings);
gd.addMessage("Click on the image to view localisation data.\nCtrl/Alt key must be pressed.");
final TextField[] tf = new TextField[1];
gd.addStringField("Highlight_colour", settings.getHighlightColour(), new OptionListener<String>() {
@Override
public boolean collectOptions(String value) {
createHighlightColour(value);
int red;
int green;
int blue;
final Color3f color = highlightColor.get();
if (color == null) {
red = blue = 0;
green = 255;
} else {
red = (int) (color.x * 255);
green = (int) (color.y * 255);
blue = (int) (color.z * 255);
}
final ExtendedGenericDialog egd = new ExtendedGenericDialog("Highlight colour", null);
egd.addSlider("Red", 0, 255, red);
egd.addSlider("Green", 0, 255, green);
egd.addSlider("Blue", 0, 255, blue);
egd.showDialog(true, gd);
if (egd.wasCanceled()) {
return false;
}
red = (int) egd.getNextNumber();
green = (int) egd.getNextNumber();
blue = (int) egd.getNextNumber();
final Color c = new Color(red, green, blue);
final String cvalue = c.getRed() + "," + c.getGreen() + "," + c.getBlue();
tf[0].setText(cvalue);
return true;
}
@Override
public boolean collectOptions() {
return false;
}
});
tf[0] = gd.getLastTextField();
gd.addCheckbox("Add_to_selection", settings.getAddToSelection());
ResultsManager.addTableResultsOptions(gd, s, ResultsManager.FLAG_NO_SECTION_HEADER);
gd.addMessage("Allow the 'Find Eye Point' command to save to settings");
gd.addCheckbox("Save_eye_point", settings.getSaveEyePoint());
// Same as CropResults
gd.addChoice("Crop_name_option", CropResults.NAME_OPTIONS, settings.getNameOption(), new OptionListener<Integer>() {
@Override
public boolean collectOptions(Integer value) {
settings.setNameOption(value);
final ExtendedGenericDialog egd = new ExtendedGenericDialog(TITLE);
if (settings.getNameOption() == CropResults.NAME_OPTION_NAME) {
return false;
} else if (settings.getNameOption() == CropResults.NAME_OPTION_SUFFIX) {
final String name = (TextUtils.isNullOrEmpty(settings.getNameSuffix())) ? " Cropped" : settings.getNameSuffix();
egd.addStringField("Name_suffix", name, MathUtils.clip(20, 60, name.length()));
} else if (settings.getNameOption() == CropResults.NAME_OPTION_SEQUENCE) {
final String name = settings.getNameSuffix();
egd.addStringField("Name_suffix", name, MathUtils.clip(20, 60, name.length()));
int counter = settings.getNameCounter();
if (counter < 1) {
counter = 1;
}
egd.addNumericField("Name_counter", counter, 0);
} else {
throw new IllegalStateException("Unknown name option: " + settings.getNameOption());
}
egd.showDialog(true, gd);
if (egd.wasCanceled()) {
return false;
}
if (settings.getNameOption() == CropResults.NAME_OPTION_SUFFIX) {
settings.setNameSuffix(egd.getNextString());
} else if (settings.getNameOption() == CropResults.NAME_OPTION_SEQUENCE) {
settings.setNameSuffix(egd.getNextString());
settings.setNameCounter(Math.max(1, (int) egd.getNextNumber()));
}
return true;
}
@Override
public boolean collectOptions() {
return false;
}
});
gd.addCheckbox("Update_existing_tables", localResultsTableSettings.getUpdateExistingTables());
addHelp(gd);
gd.showDialog();
if (gd.wasCanceled()) {
return;
}
settings.setHighlightColour(gd.getNextString());
boolean add = gd.getNextBoolean();
addToSelection.set(add);
settings.setAddToSelection(add);
final ResultsTableSettings.Builder resultsTableSettingsBuilder = s.getResultsTableSettingsBuilder();
resultsTableSettingsBuilder.setShowTable(gd.getNextBoolean());
settings.setSaveEyePoint(gd.getNextBoolean());
settings.setNameOption(gd.getNextChoiceIndex());
resultsTableSettingsBuilder.setUpdateExistingTables(gd.getNextBoolean());
createHighlightColour(settings.getHighlightColour());
// Save updated settings
localResultsTableSettings = resultsTableSettingsBuilder.build();
settings.setResultsTableSettings(localResultsTableSettings);
SettingsManager.writeSettings(settings);
// Update the table settings for all the selection models
if (resultsTableSettingsBuilder.getUpdateExistingTables()) {
for (final Triple<PeakResultTableModel, ?, ?> t : resultsTables.values()) {
t.getLeft().setTableSettings(localResultsTableSettings);
}
}
action = new UpdateHighlightColourAction();
}
if (src == toggleDynamicTransparency) {
final long total = getTotalTransparentObjects(univ, "");
final View view = univ.getViewer().getView();
final boolean activate = view.getTransparencySortingPolicy() == View.TRANSPARENCY_SORT_NONE;
activateDynamicTransparency(univ, total, activate);
return;
}
// Actions to perform on content
if (src == changeColour) {
action = new ChangeColourContentAction();
} else if (src == resetAll) {
univ.resetView();
univ.select(null);
action = new ResetViewContentAction(false);
} else if (src == resetSelectedView) {
action = new ResetViewContentAction(true);
} else if (src == findEyePoint) {
action = new FindEyePointContentAction();
} else if (src == sortBackToFront) {
action = new SortContentAction(false);
} else if (src == sortFrontToBack) {
action = new SortContentAction(true);
} else if (src == colourSurface) {
action = new ColourSurfaceContentAction();
} else if (src == toggleTransparent) {
action = new ToggleTransparentAction();
} else if (src == toggleShaded) {
action = new ToggleShadedAction();
} else if (src == changePointSize) {
action = new ChangePointSizeContentAction();
} else if (src == increasePointSize) {
action = UpdatePointSizeContentAction.INCREASE;
} else if (src == decreasePointSize) {
action = UpdatePointSizeContentAction.DECREASE;
} else if (src == cropResults) {
action = new CropResultsAction();
} else if (src == showHelp) {
showHelp();
return;
}
if (action == null) {
return;
}
if (univ.getSelected() != null) {
action.run(univ.getSelected());
} else {
for (final Iterator<Content> it = univ.contents(); it.hasNext(); ) {
if (action.run(it.next()) < 0) {
break;
}
}
}
action.finish();
}
use of org.scijava.java3d.View in project GDSC-SMLM by aherbert.
the class ImageJ3DResultsViewer method activateDynamicTransparency.
private static void activateDynamicTransparency(Image3DUniverse univ, long size, boolean enable) {
if (size == 0) {
IJ.log("No transparent objects");
enable = false;
}
final View view = univ.getViewer().getView();
final boolean isEnabled = view.getTransparencySortingPolicy() == View.TRANSPARENCY_SORT_GEOMETRY;
if (enable == isEnabled) {
return;
}
if (enable) {
if (size > 20000L) {
final ExtendedGenericDialog egd = new ExtendedGenericDialog(TITLE);
egd.addMessage("The results contain " + size + " transparent objects.\nDynamic transparency may take a long time to render.");
egd.setOKLabel("Dynamic");
egd.setCancelLabel("Standard");
egd.showDialog();
if (egd.wasCanceled()) {
return;
}
}
view.setTransparencySortingPolicy(View.TRANSPARENCY_SORT_GEOMETRY);
IJ.log("Enabled dynamic transparency");
// Q. I am not sure if this is required if objects are sorted?
// view.setDepthBufferFreezeTransparent(false)
} else {
view.setTransparencySortingPolicy(View.TRANSPARENCY_SORT_NONE);
IJ.log("Disabled dynamic transparency");
}
final JMenuBar menubar = univ.getMenuBar();
final JMenu menu = menubar.getMenu(menubar.getMenuCount() - 1);
for (int i = 0; i < menu.getItemCount(); i++) {
final JMenuItem item = menu.getItem(i);
if (item == null) {
continue;
}
if (item.getText().equals("Toggle dynamic transparency")) {
((JCheckBoxMenuItem) item).setSelected(enable);
}
}
}
use of org.scijava.java3d.View in project GDSC-SMLM by aherbert.
the class ImageJ3DResultsViewerDemo method run.
@Override
public void run(String arg) {
if (ImageJ3DViewerUtils.JAVA_3D_VERSION == null) {
IJ.error(TITLE, "Java 3D is not available");
return;
}
final boolean renderingTest = true;
if (renderingTest) {
// Put all the shapes from rendering on a view and see how they look.
final Image3DUniverse univ = new Image3DUniverse();
univ.showAttribute(DefaultUniverse.ATTRIBUTE_SCALEBAR, false);
univ.show();
final ImageWindow3D w = univ.getWindow();
GUI.center(w);
// Test how many vertices a primitive sphere has
float x = 0;
final float y = 0;
final float space = 2.5f;
for (final Rendering rendering : Rendering.values()) {
final Shape3D shape = Shape3DHelper.createShape(rendering, 3);
final Appearance a = shape.getAppearance();
final ItemMesh mesh = new ReferenceItemMesh(new Point3f[] { new Point3f(x, y, 0) }, (GeometryArray) shape.getGeometry(), a, null, null, 0f);
ImageJUtils.log("R=%s %s Vc=%d V=%d T=%d", rendering, shape.getGeometry().getClass().getSimpleName(), mesh.getVerticesCountPerItem(), mesh.getVerticesPerItem(), Shape3DHelper.getNumberOfTriangles(rendering));
if (rendering == Rendering.POINT) {
a.getPointAttributes().setPointSize(10);
}
univ.addCustomMesh(mesh, x + "," + y);
x += space;
}
return;
}
final boolean sphereTest = true;
if (sphereTest) {
// Sphere test
// Put lots of spheres on a view and see how they look.
// Is it worth supporting an ItemTriangleStripMesh so we can control
// the sphere better than the icosahedron?
final Image3DUniverse univ = new Image3DUniverse();
univ.showAttribute(DefaultUniverse.ATTRIBUTE_SCALEBAR, false);
univ.show();
final ImageWindow3D w = univ.getWindow();
GUI.center(w);
// Test how many vertices a primitive sphere has
float x = 0;
float y = 0;
final float space = 2.5f;
Appearance app = null;
for (int d = 0; d < 4; d++) {
final List<Point3f> points = customnode.MeshMaker.createIcosahedron(d, 1f);
final Pair<Point3f[], int[]> p = CustomContentHelper.createIndexedObject(points);
final int v = points.size();
final int t = v / 3;
ImageJUtils.log("Icosahedron divisions = %d, V=%d, T=%d, Vi=%d (%.2f), i=%d", d, v, t, p.getKey().length, v / (double) p.getKey().length, p.getValue().length);
CustomMesh mesh = new ItemTriangleMesh(points.toArray(new Point3f[0]), new Point3f[] { new Point3f(x, y, 0) }, null, null, 0);
app = mesh.getAppearance();
univ.addCustomMesh(mesh, x + "," + y + "," + t);
final float y2 = y + space;
mesh = new ItemIndexedTriangleMesh(p.getKey(), p.getValue(), new Point3f[] { new Point3f(x, y2, 0) }, null, null, 0);
univ.addCustomMesh(mesh, x + "," + y2 + "," + t);
x += space;
}
// Avoid null pointer warnings
if (app == null) {
throw new NullPointerException();
}
// The T=800 sphere looks about the same as the Icosahedron(div=3) T=1280
// This may be a better super-high resolution option.
x = 0;
y += 2 * space;
app = (Appearance) app.cloneNodeComponent(true);
// a.getColoringAttributes().setColor(0, 1, 0);
app.getMaterial().setDiffuseColor(0, 1, 0);
for (int d = 4; d < 50; d += 4) {
// This is a triangle strip array so is more space efficient
final Sphere s = new Sphere(1, Primitive.GENERATE_NORMALS, d);
final int t = s.getNumTriangles();
ImageJUtils.log("Sphere divisions = %d, V=%d, T=%d", d, s.getNumVertices(), t);
final ItemGeometryGroup g = new ItemGeometryGroup(new Point3f[] { new Point3f(x, y, 0) }, (GeometryArray) s.getShape().getGeometry(), app, null, null, null);
final String name = x + "," + y + "," + t;
final CustomContent content = new CustomContent(name, true);
content.getCurrent().display(new ItemGroupNode(g));
univ.addContent(content);
x += space;
}
return;
}
LocalList<Point3f> pointList;
float scale;
if (MemoryPeakResults.isMemoryEmpty()) {
pointList = new LocalList<>();
int range;
// 9 points
range = 1;
// range = 49; // 99^3 = 970299 points
for (int x = -range; x <= range; x++) {
for (int y = -range; y <= range; y++) {
for (int z = -range; z <= range; z++) {
pointList.add(new Point3f(x, y, z));
}
}
}
scale = 0.25f;
} else {
final ImageJ3DResultsViewerSettings.Builder settings = SettingsManager.readImageJ3DResultsViewerSettings(0).toBuilder();
final ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
gd.addMessage("Select a dataset to display");
ResultsManager.addInput(gd, settings.getInputOption(), InputSource.MEMORY);
gd.showDialog();
if (gd.wasCanceled()) {
return;
}
final String name = ResultsManager.getInputSource(gd);
settings.setInputOption(name);
SettingsManager.writeSettings(settings);
final MemoryPeakResults results = ResultsManager.loadInputResults(name, false, null, null);
if (MemoryPeakResults.isEmpty(results)) {
IJ.error(TITLE, "No results could be loaded");
IJ.showStatus("");
return;
}
pointList = ImageJ3DResultsViewer.getPoints(results, settings);
if (pointList == null) {
return;
}
scale = 10f;
}
final Image3DUniverse univ = new Image3DUniverse();
univ.showAttribute(DefaultUniverse.ATTRIBUTE_SCALEBAR, false);
univ.show();
final ImageWindow3D w = univ.getWindow();
GUI.center(w);
final View view = univ.getViewer().getView();
view.setTransparencySortingPolicy(View.TRANSPARENCY_SORT_GEOMETRY);
// I am not sure if this is required if objects are sorted.
// view.setDepthBufferFreezeTransparent(false)
IJ.showStatus("Creating points ...");
final Point3f[] points = pointList.toArray(new Point3f[0]);
final Point3f[] sizes = new Point3f[] { new Point3f(scale, scale, scale) };
final Appearance appearance = new Appearance();
final TransparencyAttributes ta = new TransparencyAttributes();
ta.setTransparency(0.5f);
ta.setTransparencyMode(TransparencyAttributes.FASTEST);
appearance.setTransparencyAttributes(ta);
final ItemGeometryGroup pointGroup = new ItemGeometryGroup(points, null, appearance, sizes, null, null);
// pointGroup = new OrderedItemGeometryGroup(points, null, appearance, sizes, null, null);
// // This supports transparency sorting
// BranchGroup bg = new BranchGroup();
// bg.addChild(pointGroup);
// bg.compile();
// univ.getScene().addChild(bg);
// // This does not since ContentInstant uses an OrderedPath to show:
// // the content; the object bounding box, coordinate system and point list
// final Content c = new Content("Test");
// final ContentInstant content = c.getCurrent();
// content.display(new PointGroupNode(pointGroup));
// univ.addContent(c);
// This does since ItemGeometryNode uses a Group to show the points as individual shapes
// and the CustomContentInstant uses a group not an ordered group so show all the adornments.
IJ.showStatus("Displaying points ...");
final CustomContent c = new CustomContent("Test", false);
final ContentInstant content = c.getCurrent();
content.display(new ItemGroupNode(pointGroup));
univ.addContent(c);
IJ.showStatus("Done");
}
Aggregations