Wednesday, June 13, 2007

Point and Line Plotter in VTK - A simple wrapper class

*Purpose: Beginners of VTK usually create a lot of actors for some primitive geometric objects (e.g. such as some simple points and lines). It is a waste to construct so many high level objects. Instead, we can always group multiple geometric primitives into a single actor. The rendering speed increases a lot when you do so.

I have created two simple wrapper classes to help us do the job. Their names are MyVTKPointPlotter and MyVTKLinePlotter. The classes are not well designed yet, but it works for many cases.

Sample usage:
// In demo.cpp

// Generate some random line and random points to plot
for (....)
{
pointPlotter.PlotPoint(....)
linePlotter.PlotLine(....)
}

// Add actors to the renderer
ren->AddActor(pointPlotter.CreateActor());
ren->AddActor(linePlotter.CreateActor());



(Please first follow the VTK installation guide to build VTK from source)




(P.S.) I haven't update this blog for almost a month. Sorry~

8 comments:

Yingcai Wu said...

Awesome site!

I really appreciate the effort that must have gone in to create this site.

Keep up the good work,

Cheers.

Anonymous said...

Wrapper works very good and fast. Thanks for your help!

Anonymous said...

Thanks for your example.
It works well.
However, may I know that if I wanna plot the point without the line. What should I modify?

Cheers!!^^

Anonymous said...

What should I do to modify your program to show the points one by one not but show the last results in one time?

Anonymous said...

Good Thing!

Anonymous said...

I 've changed the code a bit to make a simpler version of it.
Here it is:

/////////////////////////////////////////////////////////////////////////////////////
// Name: MyVTKLinePlotter.h
// Purpose: Plot a lot of straight line, and create a single actor to render
// Author: Chung Kai Lun Peter
////////////////////////////////////////////////////////////////////////////////////

#ifndef MY_VTK_LINE_PLOTTER_H
#define MY_VTK_LINE_PLOTTER_H

class vtkPoints;
class vtkCellArray ;
class vtkActor;
class vtkPolyData;


class MyVTKLinePlotter
{
public:

MyVTKLinePlotter();
~MyVTKLinePlotter();

void SetAllLineWidth(int width = 1);
void PlotLine(double m[3], double n[3]);
void PlotLine(double x, double y, double z, double x2, double y2, double z2);
void RemoveAllLines();

vtkActor* GetActor();

private:
void Allocate();
void Free();

int m_curPointID ;
int m_allLineWidth ;

vtkActor* actor;
vtkPoints* m_points;
vtkCellArray* m_lines;
};

#endif // MY_VTK_LINE_PLOTTER_H







Header file :
===================================



#include "MyVTKLinePlotter.h"
#include "vtkLookupTable.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkPointData.h"
#include "vtkProperty.h"

//---------------------------------------------------------------------------
MyVTKLinePlotter::MyVTKLinePlotter()
:m_curPointID(0), m_allLineWidth(1)
{
actor = NULL;
m_points = NULL;
m_lines = NULL;
Allocate();
}
//---------------------------------------------------------------------------
MyVTKLinePlotter::~MyVTKLinePlotter()
{
Free();
}
//---------------------------------------------------------------------------
void MyVTKLinePlotter::Allocate()
{
actor = vtkActor::New();
m_points = vtkPoints::New();
m_lines = vtkCellArray::New();
}
//---------------------------------------------------------------------------
void MyVTKLinePlotter::Free()
{
if(actor)
{
actor->Delete();
actor = NULL;
}
if(m_points)
{
m_points->Delete();
m_points = NULL;
}
if(m_lines)
{
m_lines->Delete();
m_lines = NULL;
}
m_curPointID = 0;
}
//---------------------------------------------------------------------------
void MyVTKLinePlotter::PlotLine(double m[3], double n[3])
{
m_points->InsertNextPoint(m);
m_points->InsertNextPoint(n);

m_lines->InsertNextCell(2);
m_lines->InsertCellPoint(m_curPointID);
m_lines->InsertCellPoint(m_curPointID+1);

m_curPointID+=2;
}
//---------------------------------------------------------------------------
void MyVTKLinePlotter::PlotLine(double x, double y, double z, double x2, double y2, double z2)
{
double m[3], n[3];

m[0]=x;
m[1]=y;
m[2]=z;

n[0]=x2;
n[1]=y2;
n[2]=z2;

PlotLine(m,n);
}
//---------------------------------------------------------------------------
void MyVTKLinePlotter::RemoveAllLines()
{
Free();
Allocate();
}
//---------------------------------------------------------------------------
void MyVTKLinePlotter::SetAllLineWidth(int width)
{
m_allLineWidth = width ;
}
//---------------------------------------------------------------------------
vtkActor* MyVTKLinePlotter::GetActor()
{
// Create poly data
vtkPolyData* polyData = vtkPolyData::New();
polyData->SetPoints(m_points);
polyData->SetLines(m_lines);

// create mapper
vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
mapper->SetInput(polyData);

// create actor
actor->SetMapper(mapper);
actor->GetProperty()->SetLineWidth(m_allLineWidth);

polyData->Delete();
mapper->Delete();
return actor;
}
//---------------------------------------------------------------------------

Anonymous said...

Note on my previous post:
-------------------------
The actor returned by "GetActor" gets deleted automatically when the object is destroyed.

Cheers,
KEL3.

mastram sexy stories ocm said...

Hes willing to do this over the phone, provided you guysnot trace the call. Whats wrong.
free sister brother fuck stories
hot and wild kinky sex stories
free erotic gay sex stories
adult theater stories
free fetish spanking enema stories
Hes willing to do this over the phone, provided you guysnot trace the call. Whats wrong.