[vtkusers] Strange behaviour of vtkXYPlotActor class

John Platt jcplatt at dsl.pipex.com
Thu Dec 13 17:39:35 EST 2007


Hi,

 

It may be easier to use data object input rather than a data set. You
could try the following.

 

#include "vtkFloatArray.h"

#include "vtkFieldData.h"

#include "vtkDataObject.h"

#include "vtkXYPlotActor.h"

#include "vtkXYPlotWidget.h"

#include "vtkMath.h"

 

#include "vtkRenderWindow.h"

#include "vtkRenderer.h"

#include "vtkRenderWindowInteractor.h"

 

int APIENTRY wWinMain(HINSTANCE hInstance,

                     HINSTANCE hPrevInstance,

                     LPSTR     lpCmdLine,

                     int       nCmdShow)

{

   vtkFloatArray* vtkXYValues = vtkFloatArray::New();

   vtkXYValues->SetNumberOfComponents( static_cast<vtkIdType>(2) );

   for ( int i = 0; i < 360; ++i )

   {

      float xy[2];

      xy[0] = static_cast<float>(i);

      xy[1] = sin( xy[0]*vtkMath::DegreesToRadians() );

      vtkXYValues->InsertNextTuple( xy );

   }

 

   vtkFieldData* vtkGraphFieldData = vtkFieldData::New();

   vtkGraphFieldData->AddArray( vtkXYValues );

   vtkXYValues->Delete();

 

   vtkDataObject* vtkGraphDataObject = vtkDataObject::New();

   vtkGraphDataObject->SetFieldData( vtkGraphFieldData );

   vtkGraphFieldData->Delete();

 

   vtkXYPlotActor* vtkGraph = vtkXYPlotActor::New();

   vtkGraph->AddDataObjectInput( vtkGraphDataObject );

   vtkGraphDataObject->Delete();

 

   vtkGraph->SetDataObjectPlotModeToColumns(); 

   vtkGraph->SetXValuesToValue();

 

   // Must state the component for the X & Y otherwise 0 is assumed for
both!!

   int curve = 0;

   vtkGraph->SetDataObjectXComponent( curve, 0 );   

   vtkGraph->SetDataObjectYComponent( curve, 1 );

 

   vtkGraph->SetXRange( 0, 360 );

   vtkGraph->SetYRange( -1, 1 );

   vtkGraph->SetPlotColor( curve, 1,0,0 );

 

   vtkRenderer* renderer = vtkRenderer::New();

   renderer->AddActor2D( vtkGraph );

 

   vtkRenderWindow* renderWin = vtkRenderWindow::New();

   renderWin->AddRenderer( renderer );

 

   vtkRenderWindowInteractor* interactor =
vtkRenderWindowInteractor::New();

   interactor->SetRenderWindow( renderWin );

 

   vtkXYPlotWidget* vtkXYPlotWidget = vtkXYPlotWidget::New(); 

   vtkXYPlotWidget->SetXYPlotActor( vtkGraph );

   vtkXYPlotWidget->SetInteractor( interactor );

   vtkXYPlotWidget->SetEnabled( true );

 

   interactor->Initialize();

   interactor->Start();

 

   return 0;

}

 

HTH John.

 

-----Original Message-----
From: vtkusers-bounces+jcplatt=dsl.pipex.com at vtk.org
[mailto:vtkusers-bounces+jcplatt=dsl.pipex.com at vtk.org] On Behalf Of
Isidro Moreno
Sent: 13 December 2007 18:37
To: vtkusers at vtk.org
Subject: [vtkusers] Strange behaviour of vtkXYPlotActor class

 

Hi! I'm a beginer in VTK and I've been trying to draw the sinus func
with vtkxyplotactor for teaching purposes. The strange thing is that
although I indicate which are the x-y components in the values array,
the system draws the "arc sin" function. I don't know how to understand
this; Am I wrong? or the system swap components?

 

Here's the code:

 

#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkPolyData.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkXYPlotActor.h"
#include "vtkDataSetCollection.h"

 

int main(){

 

//  CONSOLE: how many points entry to form the "line".

//  CONSOLA: Ingreso de la cantidad de puntos que formarán la "curva".

 

    int data_puntos;
    cout<<"Ingrese cantidad de puntos (1-50): ";
    cin>>data_puntos;

 

//  Creación de una Celda - Definición topológica y geométrica

 

//  Definición Topológica
//  Inserción de 1(una) celda con 'n' puntos
    vtkCellArray *cellarray=vtkCellArray::New();
    cellarray->InsertNextCell(data_puntos);

 

//  Definición Geométrica y relación con la topología

 

    vtkPoints *puntos=vtkPoints::New();

 

    for(int j=0;j<data_puntos;j++){
        cellarray->InsertCellPoint(j); // ___ j = nºID de cada punto
        puntos->InsertPoint(
              j, // _________________________ nº ID
              j/5.0, // _____________________ valor de "x"
              sin(j/5.0), // ________________ valor de "y"
              0.0 // _________________________valor de "z"
              );}

 

//  Agregado de información topológica y geométrica al DataSet

 

    vtkPolyData *polydata=vtkPolyData::New();
    polydata->SetPoints(puntos);   // Puntos
    polydata->SetLines(cellarray); // Celdas - 1(una)

 

    polydata->GetPointData()->SetScalars(puntos->GetData());
    polydata->GetPointData()->Update();

 

//  Creación de un Actor

 

    vtkXYPlotActor *actor=vtkXYPlotActor::New();

 

    actor->SetXValuesToValue();
    actor->SetDataObjectXComponent(0,1);
    actor->SetDataObjectYComponent(0,2);

 

    actor->AddInput(polydata);
    

// The following didn't help me enough... : ( 

    cout<<endl
 
<<"GetInputList()->GetNumberOfItems()="<<actor->GetInputList()->GetNumbe
rOfItems()<<endl
 
<<"GetDataObjectXComponent(0)="<<actor->GetDataObjectXComponent(0)<<endl
 
<<"GetDataObjectYComponent(0)="<<actor->GetDataObjectYComponent(0)<<endl
        <<"GetXValues()="<<actor->GetXValues()<<endl
        <<"GetXValuesAsString()="<<actor->GetXValuesAsString()<<endl
        <<"GetPointComponent(0)="<<actor->GetPointComponent(0);

 

//  Creación de objetos de renderización

 

    vtkRenderer *renderer=vtkRenderer::New();
    renderer->AddActor2D(actor);
    renderer->SetBackground(0.0,0.0,0.0);

 

    vtkRenderWindow *ventana=vtkRenderWindow::New();
    ventana->AddRenderer(renderer);

 

    vtkRenderWindowInteractor *interac=vtkRenderWindowInteractor::New();
    interac->SetRenderWindow(ventana);

 

    ventana->Render(); // Inicio de la renderización
    interac->Initialize();
    interac->Start();  // Inicia el "looping"

 

    polydata->Delete();
    puntos->Delete();
    cellarray->Delete();
    actor->Delete();
    renderer->Delete();
    ventana->Delete();
    interac->Delete();

 

    return 0;}

 

Thanks in advance! Isidro.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20071213/aef93b4d/attachment.htm>


More information about the vtkusers mailing list