[vtkusers] Draw 2D Ellipse annotation/overlay over a 2D graphics object
NIPL
msvinod+vtk at gmail.com
Mon Feb 16 04:45:08 EST 2009
Hi all,
I want to draw a 2D ellipse annotation over a DICOM image loaded in a 2D
View.
There is no builtin function/class for ellipse. But there is a
vtkSphereSource and it is 3D. Can I derive a 2D ellipse from it by using
vtkPolyDataMapper2D? I tried using the vtkPoints first and have some issues.
I am using the vtkDotnet wrapper for c#.
I have done like this:
Method 1:
vtk.vtkPoints ellipsePoints = new vtk.vtkPoints();
double angle = 0;
double r1, r2;
int id = 0;
int CenterX = 0, CenterY = 0;
CenterX = 100;
CenterY = 100;
r1 = 40;
r2 = 20;
while (angle <= 2F * Math.PI + (Math.PI / 30F))
{
ellipsePoints.InsertPoint(id, r1 * Math.Cos(angle) + CenterX, r2
* Math.Sin(angle) + CenterY, 0);
angle = angle + (Math.PI / 30F);
id++;
}
vtk.vtkPolyLine ellipse = new vtk.vtkPolyLine();
ellipse.GetPointIds().SetNumberOfIds(id);
for (int i = 0; i < id; i++)
{
ellipse.GetPointIds().SetId(i, i);
}
vtk.vtkUnstructuredGrid grid = new
vtk.vtkUnstructuredGrid();
grid.Allocate(1, 1);
grid.InsertNextCell(ellipse.GetCellType(),
ellipse.GetPointIds());
grid.SetPoints(ellipsePoints);
grid.Update();
vtk.vtkDataSetMapper m = new vtk.vtkDataSetMapper();
m.SetInput(grid);
m.SetColorModeToMapScalars();
vtk.vtkActor aSphere = new vtk.vtkActor();
aSphere.SetMapper(m);
aSphere.GetProperty().SetColor(0, 0, 1); // color blue
aSphere.GetProperty().SetInterpolationToFlat();
aSphere.VisibilityOn();
// a renderer for the data
//vtk.vtkRenderer ren1 = new vtk.vtkRenderer();
irenderer.AddActor(aSphere);
iwindow.Render();
But the ellipse is no visible while i draw.
Method 2:
vtk.vtkSphereSource vtkEllipse = new vtk.vtkSphereSource();
vtkEllipse.SetRadius(100);
vtkEllipse.SetCenter(100, 100, 0);
vtkEllipse.SetThetaResolution(360);
vtkEllipse.SetPhiResolution(90);
vtkEllipse.SetEndPhi(90);
vtkEllipse.SetStartPhi(70);
vtk.vtkTransformPolyDataFilter tf = new
vtk.vtkTransformPolyDataFilter();
vtk.vtkTransform tr = new vtk.vtkTransform();
tr.Identity();
tf.SetInput(vtkEllipse.GetOutput());
tf.SetTransform(tr);
tf.Update();
vtk.vtkPolyDataMapper2D m = new vtk.vtkPolyDataMapper2D();
vtk.vtkActor2D vtkActor = new vtk.vtkActor2D();
m.SetInput(tf.GetOutput());
vtkActor.SetMapper(m);
vtkActor.VisibilityOn();
vtkActor.GetProperty().SetColor(1, .5, 0);
irenderer.AddViewProp(vtkActor);
iwindow.Render();
But this is not what i want. the line width is not correct.
Method 3:
double angle = 0;
double r1, r2;
int id = 0;
int CenterX , CenterY;
CenterX = 100;
CenterY = 100;
r1 = 40;
r2 = 20;
vtk.vtkPoints ellipsePoints = new vtk.vtkPoints();
ellipsePoints.SetNumberOfPoints(31);
while (angle <= 2F * Math.PI + (Math.PI / 30F))
{
ellipsePoints.SetPoint(id, r1 * Math.Cos(angle) +
CenterX, r2 * Math.Sin(angle) + CenterY, 0);
angle = angle + (Math.PI / 30F);
id++;
}
//id = ellipsePoints.GetNumberOfPoints();
vtk.vtkCellArray c_lines = new vtk.vtkCellArray();
c_lines.Allocate(1, 1);
c_lines.InsertNextCell(id);
c_lines.SetNumberOfCells(id);
for (int k = 0; k < id; ++k)
c_lines.InsertCellPoint(k);
vtk.vtkPolyData c_polyData = new vtk.vtkPolyData();
c_polyData.ReleaseDataFlagOn();
c_polyData.Allocate(1, 1);
c_polyData.SetPoints(ellipsePoints);
c_polyData.SetLines(c_lines);
vtk.vtkPolyDataMapper2D c_polyDataMapper = new
vtk.vtkPolyDataMapper2D();
c_polyDataMapper.SetInput(c_polyData);
vtk.vtkActor2D c_actor = new vtk.vtkActor2D();
//c_actor.GetProperty().SetOpacity(.50);
//c_actor.GetProperty().SetLineWidth(2.0f);
c_actor.SetMapper(c_polyDataMapper);
c_actor.VisibilityOn();
c_actor.GetProperty().SetColor(1, .5, 0);
irenderer.AddViewProp(c_actor);
iwindow.Render();
c_polyData.Dispose();
c_lines.Dispose();
ellipsePoints.Dispose();
This one is working fine. I can see the ellipse and it looks great. But when
I draw a second ellipse on same image, it crashes. The constructor returns
null pointer.
Is my logic correct or am I missing something? The Method 1 is not crashing
but it is is not visible. Can anyone please help me fixing it?
Thanks in advance.
--
View this message in context: http://www.nabble.com/Draw-2D-Ellipse-annotation-overlay-over-a-2D-graphics-object-tp22034221p22034221.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list