[vtkusers] locate a vtkActor2D ???
Cyril Giraudon
giraudon at ifrance.com
Sun Jul 18 19:06:49 EDT 2004
Hello vtk-users,
I suppose it's a largely known problem but i haven't found the solution
anywhere.
/////////////////////
// Suppose you have :
/////////////////////
X[1:m], Y[1:n] two vectors.
X takes values from XMIN to XMAX with the step XSTEP.
Y takes values from YMIN to YMAX with the step YSTEP.
Z[1:m;1:n] a matrix defined by Z=f(X,Y).
ZMAX is the maximum value of Z.
ZMIN is the minimum value of Z.
I 'd like to draw a surface which represent Z.
The problem is the following :
if X and Y are contained in [0,1] all is OK,
otherwise I can see only Z where X and Z are
contained in [0,1].
Have I to normalize and translate X and Y ?
Or Is there a classical solution I haven't try (customize Coordinate
System for example ...) ?
Thanks a lot.
Cyril.
------------------------------------------------------------------
Here is the java code.
Java (1.4.2).
VTK 4.2.
------------------------------------------------------------------
public class Surface extends JComponent implements VtkPanelContainer
{
private vtkPanel renWin;
public Surface(float[] X, float[]Y, float[][]Z)
{
// Computation of :
// XMIN, XMAX, XSTEP
// YMIN, YMAX, YSTEP
// ZMIN, ZMAX
/////////////////////////////
// You create a vtkPolydata :
/////////////////////////////
vtkFloatArray XYpoints = new vtkFloatArray();
vtkFloatArray Zvalues = new vtkFloatArray();
XYpoints.SetNumberOfComponents(3);
XYpoints.SetNumberOfTuples(m*n);
int n = 0;
float x, y, z;
for(int i=0;i<m;i++)
{
x = X[i]
for(int j=0;j<n;j++)
{
y = Y[j];
XYpoints.SetTuple3(n, x, y, 0);
Zvalues.InsertValue(n, z);
n++;
}
}
vtkCellArray mesh = new vtkCellArray();
for(int i=0;i<m-1;i++)
{
for(int j=0;j<Datasize-1;j++)
{
mesh.InsertNextCell(4);
mesh.InsertCellPoint((0+j)+m*i);
mesh.InsertCellPoint((n+j)+m*i);
mesh.InsertCellPoint((n+1+j)+m*i);
mesh.InsertCellPoint((1+j)+m*i);
}
}
///////////////////////////////////////////////////
// I think I should use vtkStructuredGrid but it's
// for a course needs.
///////////////////////////////////////////////////
vtkPoints points = new vtkPoints();
points.SetData(XYpoints);
vtkPolyData polydata = new vtkPolyData();
polydata.SetPoints(points);
polydata.SetPolys(mesh);
polydata.GetPointData().SetScalars(Zvalues);
///////////////////////////////////////
// Now we build the data representation
///////////////////////////////////////
vtkCoordinate coo = new vtkCoordinate();
coo.SetCoordinateSystemToNormalizedViewport();
vtkPolyDataMapper2D contMapper = new vtkPolyDataMapper2D();
contMapper.SetTransformCoordinate(coo);
contMapper.SetInput(polydata);
contMapper.ScalarVisibilityOn();
contMapper.SetScalarRange(ZMIN, ZMAX);
vtkLookupTable mycolors = new vtkLookupTable();
mycolors.SetTableRange(ZMIN, ZMAX);
contMapper.SetLookupTable(mycolors);
vtkActor2D contActor = new vtkActor2D();
contActor.SetMapper(contMapper);
renWin.GetRenderer().SetBackground(1., 1., 1.);
renWin.GetRenderer().AddActor(contActor);
setLayout(new BorderLayout());
add(renWin, BorderLayout.CENTER);
public vtkPanel getRenWin()
{
return renWin;
}
public static void main(String s[])
{
Surface panel = new Surface();
JFrame frame = new JFrame("Contours2D");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add("Center", panel);
frame.pack();
frame.setVisible(true);
}
}
--
Cyril Giraudon <giraudon at ifrance.com>
More information about the vtkusers
mailing list