Skip to content
Snippets Groups Projects
Commit b414374d authored by Bharatesh Chakravarthi S B's avatar Bharatesh Chakravarthi S B Committed by Andrew Maclean
Browse files

Add java visualization examples (#528)

* Create LineWidth.java

* Create LineWidth.md

* Add TestLineWidth.png - Test Image

* Create Legend.java

* Create Legend.md

* Add TestLegend.png - Test Image

* Create ProjectSphere.java

* Create ProjectSphere.md

* Add TestProjectSphere.png - Test Image

* Create ProteinRibbons.java

* Create ProteinRibbons.md

* Add TestProteinRibbons.png - Test Image

* Create ScalarVisibility.java

* Create ScalarVisibility.md

* Add TestScalarVisibility.png - Test Image

* Update Java.md

* Update ScalarVisibility.java

* Add TestScalarVisibility.png - Test Image
parent 75f71590
No related branches found
No related tags found
No related merge requests found
......@@ -224,6 +224,7 @@ It would be appreciated if there are any Java VTK experts who could convert any
| Example Name | Classes Demonstrated | Description | Image |
| -------------- | ---------------------- | ------------- | ------- |
[Bottle](/Java/Modelling/Bottle) | vtkRotationalExtrusionFilter | Sweeps polygonal data creating "skirt" from free edges and lines, and lines from vertices
[ProjectSphere](/Java/Visualization/ProjectSphere) | vtkProjectSphereFilter | Unroll a sphere or spherical-like model.
[Spring](/Java/Modelling/Spring) | vtkRotationalExtrusionFilter | Rotation in combination with linear displacement and radius variation.
### Working with Structured 3D Data
......@@ -385,10 +386,14 @@ This section includes vtkImageData, vtkStructuredGrid and vtkRectilinearGrid.
[CursorShape](/Java/Visualization/CursorShape) | | Change the shape of the cursor.
[Cutter](/Java/VisualizationAlgorithms/Cutter) | vtkCutter | How to use vtkCutter by cutting through a cube.
[Follower](/Java/Visualization/Follower) | vtkFollower | Draw text that stays right side up.
[Legend](/java/Visualization/Legend) | vtkLegendBoxActor | Draw symbols with text
[LineWidth](/Java/Visualization/LineWidth) | vtkActor vtkProperty | Change the width/thickness of lines in an actor.
[Opacity](/Java/Visualization/Opacity) | vtkActor | Transparency, transparent.
[OrientedGlyphs](/Java/Visualization/OrientedGlyphs) | vtkGlyph3D | Copies oriented and scaled glyph geometry to every input point
[VectorFieldExample](/Java/Visualization/VectorFieldExample) | vtkXMLUnstructuredGridReader, vtkThresholdPoints, vtkGlyph3D | A vector field visualisation.
[NoShading](/Java/Visualization/NoShading) | vtkActor |
[ProteinRibbons](/Java/Visualization/ProteinRibbons) | vtkProteinRibbonFilter | Display pdb ribbons.
[ScalarVisibility](/Java/Visualization/ScalarVisibility) | vtkDataSetMapper | Switch between coloring the actor and coloring the data.
[VisualizeImageData](/Java/Visualization/VisualizeImageData) | vtkDataSetMapper vtkImageData | Visualize the points of an ImageData.
[VisualizeVTP](/Java/Visualization/VisualizeVTP) | vtkXMLPolyDataReader vtkPolyDataMapper | Visualize a VTP File.
[WindowSize](/Java/Visualization/WindowSize) | vtkRenderWindow | Change the size of a window.
......
import vtk.vtkActor;
import vtk.vtkNamedColors;
import vtk.vtkNativeLibrary;
import vtk.vtkPolyDataMapper;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.vtkSphereSource;
import vtk.vtkLegendBoxActor;
import vtk.vtkCubeSource;
import vtk.vtkPolyData;
public class Legend
{
// -----------------------------------------------------------------
// Load VTK library and print which library was not properly loaded
static
{
if (!vtkNativeLibrary.LoadAllNativeLibraries())
{
for (vtkNativeLibrary lib : vtkNativeLibrary.values())
{
if (!lib.IsLoaded())
{
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
}
vtkNativeLibrary.DisableOutputWindow(null);
}
// -----------------------------------------------------------------
public static void main(String s[])
{
vtkNamedColors colors = new vtkNamedColors();
//For boxLegendColor
double boxLegendColor[] = new double[4];
//For ballLegendColor
double ballLegendColor[] = new double[4];
//Renderer Background Color
double Bgcolor[] = new double[4];
//Renderer legend Background Color
double legendBgcolor[] = new double[4];
colors.GetColor("Tomato", boxLegendColor);
colors.GetColor("banana", ballLegendColor);
colors.GetColor("cyan", Bgcolor);
colors.GetColor("warm_grey", legendBgcolor);
vtkSphereSource sphereSource = new vtkSphereSource();
sphereSource.SetCenter(0.0, 0.0, 0.0);
sphereSource.SetRadius(5000.0);
sphereSource.Update();
vtkPolyData polydata = sphereSource.GetOutput();
// Create a mapper
vtkPolyDataMapper mapper = new vtkPolyDataMapper();
mapper.SetInputData(polydata);
vtkActor actor = new vtkActor();
actor.SetMapper(mapper);
vtkLegendBoxActor legend = new vtkLegendBoxActor();
legend.SetNumberOfEntries(2);
vtkCubeSource legendBox = new vtkCubeSource();
legendBox.Update();
legend.SetEntry(0, legendBox.GetOutput(), "Box", boxLegendColor);
legend.SetEntry(1, sphereSource.GetOutput(), "Ball", ballLegendColor);
// place legend in lower right
legend.GetPositionCoordinate().SetCoordinateSystemToView();
legend.GetPositionCoordinate().SetValue(.5, -1.0);
legend.GetPosition2Coordinate().SetCoordinateSystemToView();
legend.GetPosition2Coordinate().SetValue(1.0, -0.5);
legend.UseBackgroundOn();
legend.SetBackgroundColor(legendBgcolor);
// Create the renderer, render window and interactor.
vtkRenderer ren = new vtkRenderer();
vtkRenderWindow renWin = new vtkRenderWindow();
renWin.AddRenderer(ren);
vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
// Visualise
ren.AddActor(actor);
ren.AddActor(legend);
ren.SetBackground(Bgcolor);
renWin.SetSize(300, 300);
renWin.Render();
iren.Initialize();
iren.Start();
}
}
### Description
vtkLegendBoxActor object is used to associate a symbol with a text string.
The user specifies a vtkPolyData to use as the symbol, and a string associated with the symbol. The actor can then be placed in the scene in the same way that any other vtkActor2D can be used.
import vtk.vtkActor;
import vtk.vtkNativeLibrary;
import vtk.vtkPolyDataMapper;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.vtkLineSource;
public class LineWidth
{
// -----------------------------------------------------------------
// Load VTK library and print which library was not properly loaded
static
{
if (!vtkNativeLibrary.LoadAllNativeLibraries())
{
for (vtkNativeLibrary lib : vtkNativeLibrary.values())
{
if (!lib.IsLoaded())
{
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
}
vtkNativeLibrary.DisableOutputWindow(null);
}
// -----------------------------------------------------------------
public static void main(String s[])
{
vtkLineSource lineSource = new vtkLineSource();
vtkPolyDataMapper mapper = new vtkPolyDataMapper();
mapper.SetInputConnection(lineSource.GetOutputPort());
vtkActor actor = new vtkActor();
actor.SetMapper(mapper);
actor.GetProperty().SetLineWidth(5);
// Create the renderer, render window and interactor.
vtkRenderer ren = new vtkRenderer();
vtkRenderWindow renWin = new vtkRenderWindow();
renWin.AddRenderer(ren);
vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
ren.AddActor(actor);
renWin.SetSize(300, 300);
renWin.Render();
iren.Initialize();
iren.Start();
}
}
### Description
vtkLineSource is a source object that creates a polyline defined by two endpoints. The number of segments composing the polyline is controlled by setting the object resolution.
import vtk.vtkActor;
import vtk.vtkNamedColors;
import vtk.vtkNativeLibrary;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.vtkProjectSphereFilter;
import vtk.vtkElevationFilter;
import vtk.vtkParametricSuperEllipsoid;
import vtk.vtkParametricFunctionSource;
import vtk.vtkPolyData;
import vtk.vtkPolyDataMapper;
public class ProjectSphere
{
// -----------------------------------------------------------------
// Load VTK library and print which library was not properly loaded
static
{
if (!vtkNativeLibrary.LoadAllNativeLibraries())
{
for (vtkNativeLibrary lib : vtkNativeLibrary.values())
{
if (!lib.IsLoaded())
{
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
}
vtkNativeLibrary.DisableOutputWindow(null);
}
// -----------------------------------------------------------------
public static void main(String args[])
{
vtkNamedColors colors = new vtkNamedColors();
//Renderer Background Color
double leftRenderercolor[] = new double[4];
double rightRenderercolor[] = new double[4];
colors.GetColor("Tan", leftRenderercolor);
colors.GetColor("LightSlateGray", rightRenderercolor);
vtkParametricSuperEllipsoid surface = new vtkParametricSuperEllipsoid();
surface.SetN1(2.0);
surface.SetN2(0.5);
vtkParametricFunctionSource source = new vtkParametricFunctionSource();
source.SetParametricFunction(surface);
vtkElevationFilter elevationFilter = new vtkElevationFilter();
elevationFilter.SetInputConnection(source.GetOutputPort());
elevationFilter.SetLowPoint(0.0, 0.0, -4.0);
elevationFilter.SetHighPoint(0.0, 0.0, 4.0);
elevationFilter.Update();
// Deep copy the point data since in some versions of VTK, the ProjectSphereFilter modifies the input point data
vtkPolyData pd1 = new vtkPolyData();
pd1.DeepCopy(elevationFilter.GetOutput());
vtkProjectSphereFilter sphereProject1 = new vtkProjectSphereFilter();
sphereProject1.SetInputConnection(elevationFilter.GetOutputPort());
sphereProject1.Update();
vtkPolyDataMapper mapper1 = new vtkPolyDataMapper();
mapper1.SetInputConnection(sphereProject1.GetOutputPort());
mapper1.SetScalarRange(sphereProject1.GetOutput().GetPointData().GetScalars().GetRange());
vtkActor actor1 = new vtkActor();
actor1.SetMapper(mapper1);
vtkPolyDataMapper mapper2 = new vtkPolyDataMapper();
mapper2.SetInputData(pd1);
mapper2.SetScalarRange(pd1.GetPointData().GetScalars().GetRange());
vtkActor actor2 = new vtkActor();
actor2.SetMapper(mapper2);
// A render window
vtkRenderWindow renderWindow = new vtkRenderWindow();
// Define viewport ranges
// (xmin, ymin, xmax, ymax)
double leftViewport[] = new double[] {0.0, 0.0, 0.5, 1.0};
double rightViewport[] = new double[] {0.5, 0.0, 1.0, 1.0};
// Setup both renderers
vtkRenderer leftRenderer = new vtkRenderer();
renderWindow.AddRenderer(leftRenderer);
leftRenderer.SetViewport(leftViewport);
leftRenderer.SetBackground(.6, .5, .4);
vtkRenderer rightRenderer = new vtkRenderer();
renderWindow.AddRenderer(rightRenderer);
rightRenderer.SetViewport(rightViewport);
rightRenderer.SetBackground(.4, .5, .6);
leftRenderer.AddActor(actor2);
rightRenderer.AddActor(actor1);
vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
iren.SetRenderWindow(renderWindow);
leftRenderer.GetActiveCamera().Azimuth(30);
leftRenderer.GetActiveCamera().Elevation(-30);
leftRenderer.ResetCamera();
renderWindow.SetSize(640, 480);
renderWindow.Render();
iren.Initialize();
iren.Start();
}
}
### Description
vtkProjectSphereFilter object is a filter to 'unroll' a sphere. The unroll longitude is -180.
import vtk.vtkActor;
import vtk.vtkNamedColors;
import vtk.vtkNativeLibrary;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.vtkProteinRibbonFilter;
import vtk.vtkPDBReader;
import vtk.vtkInteractorStyleSwitch;
import vtk.vtkPolyDataMapper;
public class ProteinRibbons
{
// -----------------------------------------------------------------
// Load VTK library and print which library was not properly loaded
static
{
if (!vtkNativeLibrary.LoadAllNativeLibraries())
{
for (vtkNativeLibrary lib : vtkNativeLibrary.values())
{
if (!lib.IsLoaded())
{
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
}
vtkNativeLibrary.DisableOutputWindow(null);
}
// -----------------------------------------------------------------
public static void main(String args[])
{
//parse command line arguments
if (args.length != 1)
{
System.err.println("Usage: java -classpath ... Filename(.pdb) e.g 2j6g.pdb");
return;
}
String Filename = args[0];
vtkNamedColors colors = new vtkNamedColors();
//Renderer Background Color
double Bgcolor[] = new double[4];
colors.GetColor("Silver", Bgcolor);
// read protein from pdb
vtkPDBReader reader = new vtkPDBReader();
reader.SetFileName(Filename);
// setup ribbon filter
vtkProteinRibbonFilter ribbonFilter = new vtkProteinRibbonFilter();
ribbonFilter.SetInputConnection(reader.GetOutputPort());
// setup poly data mapper
vtkPolyDataMapper polyDataMapper = new vtkPolyDataMapper();
polyDataMapper.SetInputConnection(ribbonFilter.GetOutputPort());
// setup actor
vtkActor actor = new vtkActor();
actor.SetMapper(polyDataMapper);
actor.GetProperty().SetDiffuse(.7);
actor.GetProperty().SetSpecular(.5);
actor.GetProperty().SetSpecularPower(80.0);
// Create the renderer, render window and interactor.
vtkRenderer ren = new vtkRenderer();
vtkRenderWindow renWin = new vtkRenderWindow();
renWin.AddRenderer(ren);
vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
vtkInteractorStyleSwitch style = new vtkInteractorStyleSwitch();
style.SetCurrentStyleToTrackballCamera();
// Visualise
ren.AddActor(actor);
ren.ResetCameraClippingRange();
ren.SetBackground(Bgcolor);
renWin.SetSize(300, 300);
renWin.SetMultiSamples(0);
renWin.Render();
iren.Initialize();
iren.Start();
}
}
### Description
vtkProteinRibbonFilter object is a polydata algorithm that generates protein ribbons.
vtkPDBReader is a source object that reads Molecule files.
import vtk.vtkActor;
import vtk.vtkNamedColors;
import vtk.vtkNativeLibrary;
import vtk.vtkPolyDataMapper;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.vtkPointSource;
public class ScalarVisibility
{
// -----------------------------------------------------------------
// Load VTK library and print which library was not properly loaded
static
{
if (!vtkNativeLibrary.LoadAllNativeLibraries())
{
for (vtkNativeLibrary lib : vtkNativeLibrary.values())
{
if (!lib.IsLoaded())
{
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
}
vtkNativeLibrary.DisableOutputWindow(null);
}
// -----------------------------------------------------------------
public static void main(String s[])
{
vtkNamedColors colors = new vtkNamedColors();
//For Actor Color
double actorColor[] = new double[4];
colors.GetColor("Red", actorColor);
vtkPointSource pointSource = new vtkPointSource();
vtkPolyDataMapper mapper = new vtkPolyDataMapper();
mapper.SetInputConnection(pointSource.GetOutputPort());
mapper.ScalarVisibilityOff();
vtkActor actor = new vtkActor();
actor.SetMapper( mapper );
actor.GetProperty().SetColor(actorColor);
actor.GetProperty().SetPointSize(8);
// Create the renderer, render window and interactor.
vtkRenderer ren = new vtkRenderer();
vtkRenderWindow renWin = new vtkRenderWindow();
renWin.AddRenderer(ren);
vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
// Visualise
ren.AddActor(actor);
renWin.SetSize(300, 300);
renWin.Render();
iren.Initialize();
iren.Start();
}
}
### Description
vtkPointSource is a source object that creates a user-specified number of points within a specified radius about a specified center point.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment