[vtkusers] error message in subdividing a cell
Jochen
jochen.kling at email.de
Thu May 24 20:16:17 EDT 2012
Okay, here we go. Should we ever meet the beers go on you. ;-)
Most of the things were correct.
The main problem was you missed one important thing.
When you create a vtkPolyData by hand from an vtkUnstructuredGrid you must
copy the cells as well, not only the points.
So the reason no triangles could have been found was simply that no
triangles did exist.
// Catch mouse events
class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static MouseInteractorStyle* New();
MouseInteractorStyle() {
selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
selectedActor = vtkSmartPointer<vtkActor>::New();
selected = vtkSmartPointer<vtkUnstructuredGrid>::New();
// the next 5 lines can be done once here in the constructor.
// It's not necessary to do this with every pick
selectedMapper->SetInput(selected);
selectedActor->SetMapper(selectedMapper);
selectedActor->GetProperty()->EdgeVisibilityOn();
selectedActor->GetProperty()->SetEdgeColor(1,0,0);
selectedActor->GetProperty()->SetLineWidth(3);
}
virtual void OnLeftButtonDown()
{
int* pos = this->GetInteractor()->GetEventPosition();
vtkSmartPointer<vtkCellPicker> picker =
vtkSmartPointer<vtkCellPicker>::New();
picker->SetTolerance(0.0005);
picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
double* worldPosition = picker->GetPickPosition();
if(picker->GetCellId() != -1)
{
vtkSmartPointer<vtkIdTypeArray> ids =
vtkSmartPointer<vtkIdTypeArray>::New();
ids->SetNumberOfComponents(1);
ids->InsertNextValue(picker->GetCellId());
vtkSmartPointer<vtkSelectionNode> selectionNode =
vtkSmartPointer<vtkSelectionNode>::New();
selectionNode->SetFieldType(vtkSelectionNode::CELL);
selectionNode->SetContentType(vtkSelectionNode::INDICES);
selectionNode->SetSelectionList(ids);
vtkSmartPointer<vtkSelection> selection =
vtkSmartPointer<vtkSelection>::New();
selection->AddNode(selectionNode);
vtkSmartPointer<vtkExtractSelection> extractSelection =
vtkSmartPointer<vtkExtractSelection>::New();
extractSelection->SetInput(0, this->Data);
extractSelection->SetInput(1, selection);
extractSelection->Update();
selected->ShallowCopy(extractSelection->GetOutput());
vtkSmartPointer<vtkPolyData> newdata =
vtkSmartPointer<vtkPolyData>::New();
newdata->SetPoints(selected->GetPoints());
* // you missed to copy the cells
// it's not enough to copy points only
/**********************************************************************/
newdata->SetPolys(selected->GetCells());
/**********************************************************************/
*
// the triangle filter is not necessary
// but see my comment in the main function
//vtkSmartPointer< vtkTriangleFilter > triangleFilter =
// vtkSmartPointer<vtkTriangleFilter>::New();
//triangleFilter->SetInputConnection(newdata->GetProducerPort());
//triangleFilter->Update(); // not necessary
int numberOfSubdivisions = 1;
vtkSmartPointer< vtkLoopSubdivisionFilter >
subdivisionFilter =
vtkSmartPointer<vtkLoopSubdivisionFilter>::New();
subdivisionFilter->SetNumberOfSubdivisions(numberOfSubdivisions);
subdivisionFilter->SetInput(newdata);
//subdivisionFilter->SetInputConnection(triangleFilter->GetOutputPort());
subdivisionFilter->Update(); // not necessary
// show the subdivsions
selectedMapper.SetInputConnection(subdivisionFilter.GetOutputPort());
// comment the line above and uncomment the next code line if
you prefer
// to show the selected cell as a whole
// selectedMapper.SetInputConnection(selected.GetOutputPort());
// ???
//vtkSmartPointer<vtkIdTypeArray> id =
vtkSmartPointer<vtkIdTypeArray>::New();
//id->SetNumberOfComponents(1);
}
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
vtkSmartPointer<vtkPolyData> Data;
vtkSmartPointer<vtkDataSetMapper> selectedMapper;
vtkSmartPointer<vtkActor> selectedActor;
vtkSmartPointer<vtkUnstructuredGrid> selected;
};
vtkStandardNewMacro(MouseInteractorStyle);
int main(int, char *[])
{
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update(); // not necessary
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
// next commented lines are only necessary if you want to
// subdivide the complete sphere at once and not only the selected cell
/*
vtkSmartPointer<vtkTriangleFilter> triangleFilter =
vtkSmartPointer<vtkTriangleFilter>::New();
triangleFilter->SetInputConnection(sphereSource->GetOutputPort());
triangleFilter->Update();
mapper->SetInputConnection(triangleFilter->GetOutputPort());
*/
// instead use the spheresource directly as input
mapper->SetInputConnection(sphere->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor
=
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
vtkSmartPointer<MouseInteractorStyle> style =
vtkSmartPointer<MouseInteractorStyle>::New();
style->SetDefaultRenderer(renderer);
/* see my comment several lines above */
//style->Data = triangleFilter->GetOutput();
style->Data = sphere.GetOutput();
renderWindowInteractor->SetInteractorStyle(style);
renderer->AddActor(actor);
renderer->ResetCamera();
renderer->SetBackground(0,0,1); // Blue
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
with best regards
Jochen
--
View this message in context: http://vtk.1045678.n5.nabble.com/error-message-in-subdividing-a-cell-tp5713334p5713389.html
Sent from the VTK - Users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120524/26421eda/attachment.htm>
More information about the vtkusers
mailing list