[vtkusers] Is it possible to change size of a vtkActor2D?

luxtheme luxtheme at yahoo.de
Tue Jun 2 05:48:17 EDT 2009


Hi,

vtkActor2D is not able to do this apparently from itself. Therefore, I have written
my own callback-function which simply questions on the mousewheel movement and plots
the 2D actor in the desired size anew. 
I still have the problem with the adaptation of the window size. To find out whether I
chaneges the windowframe, I will question permanently on the cursor position. If there
is better solution for it (e.g. finished functions), let it me know please.

.luxtheme.

my new code: 

//Now you can zoom in or out with your mousewheel
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkCellArray.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkActor2D.h"
#include "vtkProperty2D.h"
#include "vtkCommand.h"

#include <cstdlib>
#include <ctime>

//random signal
class randomSignal
{
public:
    int event_array_time[100];
    int event_array_bit[100];
    int timeLine; 
    int array_index;

    randomSignal(): event_array_time(), event_array_bit(), timeLine(2000), array_index(0) { }

    void generate()
    {
        srand((unsigned)time(0));
        int time = 0;
        int start_bit = 0;

        while(time<timeLine)
        {
            array_index++;
            event_array_time[array_index-1]=time;
            event_array_bit[array_index-1]=start_bit;
            int random_event = (rand()%500)+1;
            time+=random_event;

            start_bit=(start_bit+1)%2;
        }
        event_array_time[array_index-1]=timeLine;
    }

};

void drawSignal(randomSignal *random, vtkActor2D *actor, int Xaxis)
{
    int *event_array_time = random->event_array_time;
    int *event_array_bit = random->event_array_bit;
    int timeLine = random->timeLine; 
    int array_index = random->array_index;

    vtkPoints *points = vtkPoints::New();
    vtkCellArray *lines = vtkCellArray::New();
    int pID = 0;

    vtkPolyData* polyData = vtkPolyData::New();
    vtkPolyDataMapper2D *mapper = vtkPolyDataMapper2D::New();

    //plot signal
    points->InsertNextPoint((double)Xaxis/timeLine*event_array_time[0],300*event_array_bit[0],0);       
    points->InsertNextPoint((double)Xaxis/timeLine*event_array_time[1],300*event_array_bit[0],0);       

    lines->InsertNextCell(2);                   
    lines->InsertCellPoint(pID);   
    lines->InsertCellPoint(pID+1);   
    pID+=2;

    for (int i=1; i<array_index-1; i++) {

        // toggle line
        points->InsertNextPoint((double)Xaxis/timeLine*event_array_time[i],300*event_array_bit[i],0);       
        points->InsertNextPoint((double)Xaxis/timeLine*event_array_time[i],300*event_array_bit[i+1],0);       

        lines->InsertNextCell(2);                   
        lines->InsertCellPoint(pID);   
        lines->InsertCellPoint(pID+1); 
        pID+=2;

        // High-/ Low-line
        points->InsertNextPoint((double)Xaxis/timeLine*event_array_time[i],300*event_array_bit[i],0);       
        points->InsertNextPoint((double)Xaxis/timeLine*event_array_time[i+1],300*event_array_bit[i],0);       

        lines->InsertNextCell(2);                   
        lines->InsertCellPoint(pID);   
        lines->InsertCellPoint(pID+1); 
        pID+=2;
    }

    // Create actor
    polyData->SetPoints(points);        
    polyData->SetLines(lines);        
    mapper->SetInput(polyData);                    
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(0,1,1); 
}

//mouse moved
class callbackZoom: public vtkCommand
{
public:
    static callbackZoom *New()
    { return new callbackZoom; }
    virtual void Execute(vtkObject *caller, unsigned long eventId, void*)
    {
        if(eventId == MouseWheelBackwardEvent) {
            Xaxis=(int)(Xaxis*0.9);
            drawSignal(random, actor, Xaxis);
            renWin->Render();
        } 
        else if(eventId == MouseWheelForwardEvent) {
            Xaxis=(int)(Xaxis*1.1);
            drawSignal(random, actor, Xaxis);
            renWin->Render();
        } 
    }
    
    callbackZoom() : actor(), Xaxis(tempX), renWin(), random() {}
    vtkRenderWindow *renWin;
    randomSignal *random;
    int tempX;
    int& Xaxis;
    vtkActor2D *actor;
};

//MAIN
void main()
{
    int Xaxis = 800;

    // generate random signal
    randomSignal *random = new randomSignal();
    random->generate(); 
    cout << "\n\nAI: " << random->array_index << endl;
            
    vtkActor2D *actor = vtkActor2D::New();
    drawSignal(random, actor, Xaxis);

    // Create a renderer
    vtkRenderer* ren = vtkRenderer::New();
        ren->SetBackground(0.75,0.25,0.25);
        ren->AddActor(actor);

    // Create a render window
    vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
    vtkRenderWindow* renWin = vtkRenderWindow::New();
        renWin->AddRenderer(ren);
        renWin->SetSize( 800,300 );
        renWin->SetInteractor( iren );

    // Event
    callbackZoom *cbZoom = callbackZoom::New();
    cbZoom->actor = actor;
    cbZoom->Xaxis = Xaxis;
    cbZoom->renWin = renWin;
    cbZoom->random = random;
    iren->AddObserver(vtkCommand::MouseWheelBackwardEvent,cbZoom);
    iren->AddObserver(vtkCommand::MouseWheelForwardEvent,cbZoom);

    iren->Initialize();
    renWin->Render();
    iren->Start();

    //...
}

--- Öner F <luxtheme at yahoo.de> schrieb am Di, 26.5.2009:

Von: luxtheme <luxtheme at yahoo.de>
Betreff: [vtkusers] Is it possible to change size of a vtkActor2D?
An: vtkusers at vtk.org
Datum: Dienstag, 26. Mai 2009, 11:35

Hi,

I want to display analog & digital signals in a cartesian coordinate system which 
should be zoomable. I have tried several approaches and simply do not get further.
So I need help.

One of the approaches which I try is vtkActor2D. Problem is, that if I change the
window size, it does not change the size of the 2D actor. First position of the
2D actor I can wonderfully set up, but position2 cannot be changed. Height and
width of the 2D actor remains always constant.
Is it possible to zoom with vtkActor2D? If so, how?
Which additional functions do I need?

.luxtheme.




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


More information about the vtkusers mailing list