[vtkusers] Show edges with picker
nmitreski
nimitreski at gmail.com
Tue Jan 22 09:04:14 EST 2008
Finally some kind of solution. I' m getting edges from one cell (triangle).
Here is solution
#include "vtkPolyData.h"
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkCellArray.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkPoints.h"
int main(int argc, char *argv[])
{
vtkConeSource *cone = vtkConeSource::New();
cone->SetRadius(1.0);
cone->SetHeight(3.0);
cone->SetResolution(8);
cone->CappingOff();
vtkPolyData *mesh = vtkPolyData::New();
mesh = cone->GetOutput();
mesh->Update(); //important part
mesh->BuildLinks();
vtkPolyData *contour = vtkPolyData::New();
contour->SetPoints(mesh->GetPoints());
vtkPoints *points = vtkPoints::New();
points = mesh->GetPoints();
vtkIdType num_pts = points->GetNumberOfPoints();
cout<<"Number of Points = "<<num_pts<<endl;
vtkCellArray *lines = vtkCellArray::New();
vtkIdType npts, *pts;
mesh->GetCellPoints(4, npts, pts);
cout<<"npts = "<<npts<<endl;
cout<<"pts[0] = "<<pts[0]<<endl;
cout<<"pts[1] = "<<pts[1]<<endl;
cout<<"pts[2] = "<<pts[2]<<endl;
vtkIdType ids[2];
ids[0] = pts[0]; ids[1] = pts[1];
lines->InsertNextCell(2, ids);
ids[0] = pts[1]; ids[1] = pts[2];
lines->InsertNextCell(2, ids);
ids[0] = pts[2]; ids[1] = pts[0];
lines->InsertNextCell(2, ids);
contour->SetLines(lines);
vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
coneMapper->SetInput(cone->GetOutput());
vtkPolyDataMapper *contourMapper = vtkPolyDataMapper::New();
contourMapper->SetInput(contour);
vtkActor *coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);
vtkActor *contourActor = vtkActor::New();
contourActor->SetMapper(contourMapper);
contourActor->GetProperty()->SetColor(1, 0, 0);
vtkRenderer *ren = vtkRenderer::New();
vtkRenderWindow *win = vtkRenderWindow::New();
win->AddRenderer(ren);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(win);
ren->AddActor(coneActor);
ren->AddActor(contourActor);
ren->SetBackground(0.1, 0.2, 0.4);
iren->Initialize();
win->Render();
iren->Start();
//deleting styff
cone->Delete();
coneMapper->Delete();
coneActor->Delete();
//new polydata
contour->Delete();
lines->Delete();
contourMapper->Delete();
contourActor->Delete();
ren->Delete();
win->Delete();
iren->Delete();
return 0;
}
nmitreski wrote:
>
> Thanks for advice. You give me some direction for solving problem. It has
> some little problem in line
>
> vtkIdType ids[2];
> ids[0] = pts[0]; ids[1] = pts[1];
>
> when debugging the code, I receive error message in this line, maybe
> previous command
>
> mesh->GetCellPoints( cell_id, npts, pts );
>
> did not give wanted values for npts and pts.
>
> Bob-139 wrote:
>>
>> 2008/1/16, nmitreski <nimitreski at gmail.com>:
>>>
>>> Hi.
>>> I have some problem. I'm trying to display edges of picked cell using
>>> cell
>>> picker. I'm trying that on cone (vtkConeSource), and I just like to
>>> display
>>> edges from picked cell. (something like edges from just 1 triangle)
>>> Can somebody help me? If you have some working example for picking cells
>>> in
>>> C++, please send it to me.
>>>
>>
>> I think that you have to add another actor to the renderer, or better
>> make use of a vtkAssembly. The new polydata from which you are mapping
>> should have only three lines contouring the cell. The actual code is
>> something like the following:
>>
>>
>> // mesh is the input polyData
>> // here picker is a vtkCellPicker instance.
>> // giving a cell id you want to highlight,
>> // picked from x, y display position.
>>
>>
>> vtkPolyData* contour= vtkPolyData::New();
>> vtkCellArray* lines= vtkCellArray::New();
>> vtkAssembly* assem= vtkAssembly::New();
>> vtkPolyDataMapper* mapper1= vtkPolyDataMapper::New();
>> vtkPolyDataMapper* mapper2= vtkPolyDataMapper::New();
>> vtkActor* principalActor= vtkActor::New();
>> vtkActor* contourActor= vtkActor::New();
>> principalActor->SetMapper( mapper1 );
>> principalActor->SetMapper( mapper2 );
>> mapper1->SetInput( mesh );
>> mapper2->SetInput( contour );
>>
>> assem->AddPart( principalActor );
>> assem->AddPart( contourActor );
>>
>> contour->SetPoints( mesh->GetPoints() );
>> contour->SetLines( lines );
>>
>> vtkCellPicker* picker= vtkCellPicker::New();
>> int is_pick= picker->Pick( (float)x, (float)y, 1.0, renderer );
>>
>> if( ! is_pick )
>> {
>> return;
>> }
>>
>> vtkIdType cell_id= picker->GetCellId();
>>
>> vtkIdType npts, *pts;
>> mesh->BuildLinks();
>> mesh->GetCellPoints( cell_id, npts, pts );
>>
>> assert( npts == 3 ):
>>
>> vtkIdType ids[2];
>> ids[0]= pts[0]; ids[1]= pts[1];
>> lines->InsertNextCell( 2, ids );
>>
>> ids[0]= pts[1]; ids[1]= pts[2];
>> lines->InsertNextCell( 2, ids );
>>
>> ids[0]= pts[2]; ids[1]= pts[0];
>> lines->InsertNextCell( 2, ids );
>>
>> renderer->AddActor( assem );
>>
>> // then the ...->Delete() stuff
>>
>> HTH.
>> Bob.
>> _______________________________________________
>> This is the private VTK discussion list.
>> Please keep messages on-topic. Check the FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Show-edges-with-picker-tp14803681p15019121.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list