[vtkusers] Plotting!

N Smethurst nick.smethurst at free.fr
Wed May 28 15:39:07 EDT 2003


Le Mercredi 28 Mai 2003 15:22, Sharbel Dalal a écrit :
> Hello,
>
> I am trying to plot an x,y function e.g f(x,y)=x^3+5*Y^4.
>
> I know that plotting an f(x)=3*x function can be done using
> vtkXYPlotActor, I was wondering how I can plot the f(x,y) function, id
> really appreciate it if you can provide a small script (python or c++)
> that would show how it can be done because I am not a vtk expert, just
> started
>

Sharbel, you can do something like this if you want to use poly data:

    // To plot as in Octave/Matlab: mesh(x,y,z)
    // Specify the number of rows and columns (rows & cols).

    // Your data:    
    float x[rows][cols];
    float y[rows][cols];
    float z[rows][cols];
    //// Fill them here however you want. ////

    vtkPoints* surfPoints = vtkPoints::New();
    vtkCellArray* surfCells = vtkCellArray::New();
    vtkPolyData* surfPolyData = vtkPolyData::New();
    vtkElevationFilter* surfElevation = vtkElevationFilter::New();
    vtkPolyDataNormals* surfNormals = vtkPolyDataNormals::New();

    // Construct points (copy x,y,z data into points object).
    int count = 0;
    float point[3];
    for (int m=0; m<rows; m++)
        for (int n=0; n<cols; n++) {
            point[0] = x[m][n];
            point[1] = y[m][n];
            point[2] = z[m][n];
            surfPoints->SetPoint(count++,point);
        }

    // Construct triangle strip topology.
    // The number of points in a cell is twice the number of columns.
    vtkIdType pnt[2*cols];
    // Loop for each row pair (the number of rows less one).
    for (int j=0; j<rows-1; j++) {
        count = 0;
        for (int i=0; i<cols; i++) {
            pnt[count++] = j*cols + i;
            pnt[count++] = (j+1)*cols + i;
        }
        surfCells->InsertNextCell(p,pnt);
    }

    surfPolyData->SetPoints(surfPoints);
    surfPolyData->SetStrips(surfCells);

    // Color it.
    surfElevation->SetInput(surfPolyData);
    surfElevation->SetLowPoint(0.0, 0.0, Zmax);
    surfElevation->SetHighPoint(0.0, 0.0, Zmin);

    // For shading.
    surfNormals->SetInput(surfElevation->GetPolyDataOutput());
    surfNormals->SetFeatureAngle(80);

    // mapper, actor, etc.




More information about the vtkusers mailing list