/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package browser;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JPanel;
import vtk.vtkActor;
import vtk.vtkAxesActor;
import vtk.vtkCellArray;
import vtk.vtkCleanPolyData;
import vtk.vtkFloatArray;
import vtk.vtkPanel;
import vtk.vtkPoints;
import vtk.vtkPolyData;
import vtk.vtkPolyDataMapper;
import vtk.vtkRenderer;
import vtk.vtkTubeFilter;
import vtk.vtkUnsignedCharArray;
/**
*
* @author brandon
*/
public class VTKTubePanel extends JPanel {
vtkPanel renWin;
private vtkAxesActor axes;
public vtkPanel getRenWin() {
return renWin;
}
public vtkRenderer getRenderer() {
return renWin.GetRenderer();
}
public VTKTubePanel() {
setLayout(new BorderLayout());
renWin = new vtkPanel();
add(renWin, BorderLayout.CENTER);
// set the minimum size to ensure the screen will resize
renWin.setMinimumSize(new Dimension(100, 100));
//addAxis();
}
public void addAxis() {
setAxes(new vtkAxesActor());
getAxes().SetShaftTypeToLine();
getAxes().GetYAxisShaftProperty().SetOpacity(0);
getAxes().GetZAxisShaftProperty().SetOpacity(0);
getAxes().GetYAxisTipProperty().SetOpacity(0);
getAxes().GetZAxisTipProperty().SetOpacity(0);
getAxes().SetYAxisLabelText("");
getAxes().SetZAxisLabelText("");
// axes.SetXAxisLabelText("x");
// axes.SetYAxisLabelText("y");
// axes.SetZAxisLabelText("z");
getRenderer().AddActor(getAxes());
// vtkAxes axisObj = new vtkAxes();
// axisObj.SetScaleFactor(1.0);
// vtkTubeFilter axisTube = new vtkTubeFilter();
// axisTube.SetInput(axisObj.GetOutput());
// axisTube.SetRadius(10000000.0);
// axisTube.SetNumberOfSides(20);
// vtkPolyDataMapper axisMapper = new vtkPolyDataMapper();
// axisMapper.SetInput(axisTube.GetOutput());
// axisMapper.ScalarVisibilityOff();
// vtkActor axis = new vtkActor();
// axis.SetMapper(axisMapper);
// axis.PickableOff();
// axis.GetProperty().SetColor(255, 255, 255);
// getRenderer().AddActor(axis);
}
public void addTube(vtkPoints points, vtkUnsignedCharArray colorArray, vtkFloatArray radiusArray, double opacity) {
vtkCellArray cellArray = new vtkCellArray();
cellArray.InsertNextCell(points.GetNumberOfPoints());
for (int j = 0; j < points.GetNumberOfPoints(); j++) {
cellArray.InsertCellPoint(j);
}
vtkPolyData polyData = new vtkPolyData();
polyData.SetPoints(points);
polyData.SetLines(cellArray);
polyData.GetPointData().AddArray(colorArray);
polyData.GetPointData().SetScalars(radiusArray);
vtkCleanPolyData cleanFilter = new vtkCleanPolyData();
cleanFilter.SetInput(polyData);
cleanFilter.Update();
vtkTubeFilter tube = new vtkTubeFilter();
tube.SetVaryRadiusToVaryRadiusByAbsoluteScalar();
tube.SetInput(cleanFilter.GetOutput());
tube.SetNumberOfSides(20);
tube.CappingOn();
vtkPolyDataMapper mapper = new vtkPolyDataMapper();
mapper.SetInput(tube.GetOutput());
mapper.ScalarVisibilityOn();
mapper.SetScalarModeToUsePointFieldData();
mapper.SelectColorArray("colors");
vtkActor actor = new vtkActor();
actor.SetMapper(mapper);
actor.GetProperty().SetOpacity(opacity);
actor.GetProperty().BackfaceCullingOff();
actor.GetProperty().FrontfaceCullingOff();
getRenderer().AddActor(actor);
getRenderer().TwoSidedLightingOn();
getRenderer().LightFollowCameraOn();
}
/*public void addTube(String filename) throws FileNotFoundException, IOException, Exception {
BufferedReader br = new BufferedReader(new FileReader(filename));
double opacity = 0;
vtkPoints points = null;
vtkUnsignedCharArray colorArray = null;
vtkFloatArray radiusArray = null;
for (String line = br.readLine(); line != null; line = br.readLine()) {
// check if this is a line that describes a new tube
if (line.startsWith("##")) {
// draw the previous tube
if (points != null) {
addTube(points, colorArray, radiusArray, opacity);
}
String[] values = line.split("\t");
opacity = Double.parseDouble(values[1]);
// initialize the points array
points = new vtkPoints();
// initialize the color array
colorArray = new vtkUnsignedCharArray();
colorArray.SetNumberOfComponents(3);
colorArray.SetName("colors");
// initialize the radius array
radiusArray = new vtkFloatArray();
radiusArray.SetName("radius");
} else {
String[] values = line.split("\t");
double x = Double.parseDouble(values[0]);
double y = Double.parseDouble(values[1]);
double z = Double.parseDouble(values[2]);
int colorR = Integer.parseInt(values[3]);
int colorG = Integer.parseInt(values[4]);
int colorB = Integer.parseInt(values[5]);
double radius = Double.parseDouble(values[6]);
points.InsertNextPoint(x, y, z);
colorArray.InsertNextTuple3(colorR, colorG, colorB);
radiusArray.InsertNextValue(radius);
}
}
// add the last tube
if (points != null) {
addTube(points, colorArray, radiusArray, opacity);
}
}*/
public void addTube(ArrayList<TubePosition> tubePositions) throws FileNotFoundException, IOException, Exception {
double opacity = 1.0;
vtkPoints points = null;
vtkUnsignedCharArray colorArray = null;
vtkFloatArray radiusArray = null;
// initialize the points array
points = new vtkPoints();
// initialize the color array
colorArray = new vtkUnsignedCharArray();
colorArray.SetNumberOfComponents(3);
colorArray.SetName("colors");
// initialize the radius array
radiusArray = new vtkFloatArray();
radiusArray.SetName("radius");
for (TubePosition tubePosition : tubePositions) {
double x = tubePosition.x;
double y = tubePosition.y;
double z = tubePosition.z;
int colorR = (int) tubePosition.r;
int colorG = (int) tubePosition.g;
int colorB = (int) tubePosition.b;
double radius = tubePosition.radius;
points.InsertNextPoint(x, y, z);
colorArray.InsertNextTuple3(colorR, colorG, colorB);
radiusArray.InsertNextValue(radius);
}
// add the lastube
if (points != null) {
addTube(points, colorArray, radiusArray, opacity);
}
}
/**
* @return the axes
*/
public vtkAxesActor getAxes() {
return axes;
}
/**
* @param axes the axes to set
*/
public void setAxes(vtkAxesActor axes) {
this.axes = axes;
}
}