void v3dDataFibers::generateThumbnails() const { d->thumbnails.clear(); // we must use a parent to prevent infinte recursion of the QVTKWidget // see: http://vtk.1045678.n5.nabble.com/qtimageviewer-example-crashes-with-vtk-5-6-0-td1249263.html // QWidget *parent = new QWidget; QVTKWidget *widget = new QVTKWidget (parent); QVBoxLayout *l = new QVBoxLayout(parent); l->setContentsMargins(0,0,0,0); l->addWidget(widget); vtkPolyDataMapper *mapper = vtkPolyDataMapper::New(); vtkActor *actor = vtkActor::New(); vtkRenderer *renderer = vtkRenderer::New(); vtkRenderWindow *window = vtkRenderWindow::New(); widget->SetRenderWindow(window); parent->setFixedSize(128,128); parent->show(); mapper->SetInput (d->data->GetFibers()); actor->SetMapper (mapper); renderer->AddViewProp(actor); window->SetSize (128,128); window->AddRenderer (renderer); //window->OffScreenRenderingOn(); renderer->ResetCamera(); //window->Render(); parent->update(); // make sure widget is shown and that openGL context is initialized // otherwise, an infinite loop happens when calling GetRGBACharPixelData qApp->processEvents(); unsigned int w=128, h=128; d->thumbnail = QImage(w, h, QImage::Format_ARGB32); vtkUnsignedCharArray* pixels = vtkUnsignedCharArray::New(); pixels->SetArray(d->thumbnail.bits(), w*h*4, 1); window->GetRGBACharPixelData(0, 0, w-1, h-1, 1, pixels); parent->hide(); d->thumbnails.append (d->thumbnail); pixels->Delete(); mapper->Delete(); actor->Delete(); renderer->Delete(); window->Delete(); // crash if window is deleted parent->deleteLater(); }
void vtkDataMesh::createThumbnails() { vtkPointSet * mesh = vtkPointSet::SafeDownCast(d->mesh->GetDataSet()); unsigned int w=128, h=128; QImage img(w, h, QImage::Format_RGB32); img.fill(0); vtkSmartPointer <vtkDataSetSurfaceFilter> geometryextractor = vtkDataSetSurfaceFilter::New(); vtkSmartPointer <vtkPolyDataMapper> mapper = vtkPolyDataMapper::New(); vtkSmartPointer <vtkActor> actor = vtkActor::New(); vtkSmartPointer <vtkProperty> prop = vtkProperty::New(); vtkSmartPointer <vtkRenderer> renderer = vtkRenderer::New(); vtkSmartPointer <vtkRenderWindow> window = vtkRenderWindow::New(); //ugly trick of doom to have no popups QVTKWidget widget; widget.resize(w,h); geometryextractor->SetInput (mesh); mapper->SetInput (geometryextractor->GetOutput()); actor->SetMapper (mapper); actor->SetProperty (prop); renderer->AddViewProp(actor); window->SetSize (w,h); window->SetOffScreenRendering(1); window->AddRenderer (renderer); renderer->ResetCamera(); widget.SetRenderWindow(window); window->Render(); vtkSmartPointer <vtkUnsignedCharArray> pixels = vtkUnsignedCharArray::New(); pixels->SetArray(img.bits(), w*h*4, 1); window->GetRGBACharPixelData(0, 0, w-1, h-1, 1, pixels); d->thumbnails.push_back (img); }
int main(int argc, char** argv) { QApplication app(argc, argv); QVTKWidget widget; widget.resize(256,256); // Setup sphere vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New(); sphereSource->Update(); vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); sphereMapper->SetInputConnection(sphereSource->GetOutputPort()); vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New(); sphereActor->SetMapper(sphereMapper); // Setup window vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); // Setup renderer vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); renderWindow->AddRenderer(renderer); renderer->AddActor(sphereActor); renderer->ResetCamera(); widget.SetRenderWindow(renderWindow); widget.show(); app.exec(); return EXIT_SUCCESS; }
void KWidget_2D_left:: SetupRenderWindow() { kvImageRenderer = vtkSmartPointer<vtkRenderer>::New(); // This is where multiple actors are added / multiple label maps here kvImageRenderer->AddActor( imageActor ); if( bNoInputLabelFiles ) kvImageRenderer->AddActor( multiLabelMaps[0]->labelActor2D ); kvImageRenderer->SetBackground(0,0,0); renderWindowLeft = vtkSmartPointer<vtkRenderWindow>::New(); renderWindowLeft->AddRenderer( kvImageRenderer ); sliceViewPicker = vtkSmartPointer<vtkPropPicker>::New(); sliceViewPicker->PickFromListOn(); sliceViewPicker->AddPickList( imageActor ); image_interactor_style = vtkSmartPointer<vtkInteractorStyleImage>::New(); vtkRenderWindow* window_alias = renderWindowLeft; QVTKWidget* qvtk = qVTK_widget_left; qvtk->GetRenderWindow()->GetInteractor( )->SetInteractorStyle( image_interactor_style ); qvtk->SetRenderWindow( window_alias ); window_alias->GetInteractor( )->SetInteractorStyle( image_interactor_style ); }
int main(int argc, char** argv) { QApplication app(argc, argv); Tr tr; // 3D-Delaunay triangulation C2t3 c2t3 (tr); // 2D-complex in 3D-Delaunay triangulation // the 'function' is a 3D gray level image Gray_level_image image("../../../examples/Surface_mesher/data/skull_2.9.inr", 2.9); // Carefully choosen bounding sphere: the center must be inside the // surface defined by 'image' and the radius must be high enough so that // the sphere actually bounds the whole image. GT::Point_3 bounding_sphere_center(122., 102., 117.); GT::FT bounding_sphere_squared_radius = 200.*200.*2.; GT::Sphere_3 bounding_sphere(bounding_sphere_center, bounding_sphere_squared_radius); // definition of the surface, with 10^-2 as relative precision Surface_3 surface(image, bounding_sphere, 1e-5); // defining meshing criteria CGAL::Surface_mesh_default_criteria_3<Tr> criteria(30., 5., 1.); // meshing surface, with the "manifold without boundary" algorithm CGAL::make_surface_mesh(c2t3, surface, criteria, CGAL::Manifold_tag()); QVTKWidget widget; widget.resize(256,256); // vtkImageData* vtk_image = CGAL::vtk_image_sharing_same_data_pointer(image); vtkRenderer *aRenderer = vtkRenderer::New(); vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer(aRenderer); widget.SetRenderWindow(renWin); // vtkContourFilter *skinExtractor = vtkContourFilter::New(); // skinExtractor->SetInput(vtk_image); // skinExtractor->SetValue(0, isovalue); // skinExtractor->SetComputeNormals(0); vtkPolyDataNormals *skinNormals = vtkPolyDataNormals::New(); // skinNormals->SetInputConnection(skinExtractor->GetOutputPort()); vtkPolyData* polydata = CGAL::output_c2t3_to_vtk_polydata(c2t3); skinNormals->SetInput(polydata); skinNormals->SetFeatureAngle(60.0); vtkPolyDataMapper *skinMapper = vtkPolyDataMapper::New(); // skinMapper->SetInputConnection(skinExtractor->GetOutputPort()); skinMapper->SetInput(polydata); skinMapper->ScalarVisibilityOff(); vtkActor *skin = vtkActor::New(); skin->SetMapper(skinMapper); // An outline provides context around the data. // // vtkOutlineFilter *outlineData = vtkOutlineFilter::New(); // outlineData->SetInput(vtk_image); // vtkPolyDataMapper *mapOutline = vtkPolyDataMapper::New(); // mapOutline->SetInputConnection(outlineData->GetOutputPort()); // vtkActor *outline = vtkActor::New(); // outline->SetMapper(mapOutline); // outline->GetProperty()->SetColor(0,0,0); // It is convenient to create an initial view of the data. The FocalPoint // and Position form a vector direction. Later on (ResetCamera() method) // this vector is used to position the camera to look at the data in // this direction. vtkCamera *aCamera = vtkCamera::New(); aCamera->SetViewUp (0, 0, -1); aCamera->SetPosition (0, 1, 0); aCamera->SetFocalPoint (0, 0, 0); aCamera->ComputeViewPlaneNormal(); // Actors are added to the renderer. An initial camera view is created. // The Dolly() method moves the camera towards the FocalPoint, // thereby enlarging the image. // aRenderer->AddActor(outline); aRenderer->AddActor(skin); aRenderer->SetActiveCamera(aCamera); aRenderer->ResetCamera (); aCamera->Dolly(1.5); // Set a background color for the renderer and set the size of the // render window (expressed in pixels). aRenderer->SetBackground(1,1,1); renWin->SetSize(640, 480); // Note that when camera movement occurs (as it does in the Dolly() // method), the clipping planes often need adjusting. Clipping planes // consist of two planes: near and far along the view direction. The // near plane clips out objects in front of the plane; the far plane // clips out objects behind the plane. This way only what is drawn // between the planes is actually rendered. aRenderer->ResetCameraClippingRange (); // Initialize the event loop and then start it. // iren->Initialize(); // iren->Start(); // It is important to delete all objects created previously to prevent // memory leaks. In this case, since the program is on its way to // exiting, it is not so important. But in applications it is // essential. // vtk_image->Delete(); // skinExtractor->Delete(); skinNormals->Delete(); skinMapper->Delete(); skin->Delete(); // outlineData->Delete(); // mapOutline->Delete(); // outline->Delete(); aCamera->Delete(); // iren->Delete(); renWin->Delete(); aRenderer->Delete(); polydata->Delete(); widget.show(); app.exec(); return 0; }
int main(int argc, char** argv) { QApplication app(argc, argv); if(argc != 3) usage_and_exit(argv[0]); QVTKWidget widget; widget.resize(256,256); #if QT_VERSION < 0x040000 app.setMainWidget(&widget); #endif CGAL::Image_3 image; if(!image.read(argv[1])) { std::cerr << "Cannot read image file \"" << argv[1] << "\"!\n"; usage_and_exit(argv[0]); } std::stringstream argv2; argv2 << argv[2]; double isovalue; if(!(argv2 >> isovalue)) { std::cerr << "Invalid iso-value \"" << argv[2] << "\"!\n"; usage_and_exit(argv[0]); } vtkImageData* vtk_image = CGAL::vtk_image_sharing_same_data_pointer(image); vtkRenderer *aRenderer = vtkRenderer::New(); vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer(aRenderer); widget.SetRenderWindow(renWin); vtkContourFilter *skinExtractor = vtkContourFilter::New(); skinExtractor->SetInputData(vtk_image); skinExtractor->SetValue(0, isovalue); // skinExtractor->SetComputeNormals(0); vtkPolyDataNormals *skinNormals = vtkPolyDataNormals::New(); skinNormals->SetInputConnection(skinExtractor->GetOutputPort()); skinNormals->SetFeatureAngle(60.0); vtkPolyDataMapper *skinMapper = vtkPolyDataMapper::New(); skinMapper->SetInputConnection(skinExtractor->GetOutputPort()); skinMapper->ScalarVisibilityOff(); vtkActor *skin = vtkActor::New(); skin->SetMapper(skinMapper); // An outline provides context around the data. // vtkOutlineFilter *outlineData = vtkOutlineFilter::New(); outlineData->SetInputData(vtk_image); vtkPolyDataMapper *mapOutline = vtkPolyDataMapper::New(); mapOutline->SetInputConnection(outlineData->GetOutputPort()); vtkActor *outline = vtkActor::New(); outline->SetMapper(mapOutline); outline->GetProperty()->SetColor(0,0,0); // It is convenient to create an initial view of the data. The FocalPoint // and Position form a vector direction. Later on (ResetCamera() method) // this vector is used to position the camera to look at the data in // this direction. vtkCamera *aCamera = vtkCamera::New(); aCamera->SetViewUp (0, 0, -1); aCamera->SetPosition (0, 1, 0); aCamera->SetFocalPoint (0, 0, 0); aCamera->ComputeViewPlaneNormal(); // Actors are added to the renderer. An initial camera view is created. // The Dolly() method moves the camera towards the FocalPoint, // thereby enlarging the image. aRenderer->AddActor(outline); aRenderer->AddActor(skin); aRenderer->SetActiveCamera(aCamera); aRenderer->ResetCamera (); aCamera->Dolly(1.5); // Set a background color for the renderer and set the size of the // render window (expressed in pixels). aRenderer->SetBackground(1,1,1); renWin->SetSize(640, 480); // Note that when camera movement occurs (as it does in the Dolly() // method), the clipping planes often need adjusting. Clipping planes // consist of two planes: near and far along the view direction. The // near plane clips out objects in front of the plane; the far plane // clips out objects behind the plane. This way only what is drawn // between the planes is actually rendered. aRenderer->ResetCameraClippingRange (); // Initialize the event loop and then start it. // iren->Initialize(); // iren->Start(); // It is important to delete all objects created previously to prevent // memory leaks. In this case, since the program is on its way to // exiting, it is not so important. But in applications it is // essential. vtk_image->Delete(); skinExtractor->Delete(); skinNormals->Delete(); skinMapper->Delete(); skin->Delete(); outlineData->Delete(); mapOutline->Delete(); outline->Delete(); aCamera->Delete(); // iren->Delete(); renWin->Delete(); aRenderer->Delete(); widget.show(); app.exec(); return 0; }
void Ui::openComponentDialog() { addComponentDialog = new QDialog(this); // set as child of Ui, to be sure that it will be deleted in the end. QVBoxLayout *dialogLayout = new QVBoxLayout; // create vertical layout QVTKWidget *dialogVisualizer = new QVTKWidget; // create qvtk widget dialogViewer = new pcl::visualization::PCLVisualizer("Dialog Viewer", false); dialogVisualizer->SetRenderWindow(dialogViewer->getRenderWindow()); // set as render window the render window of the dialog visualizer dialogViewer->setupInteractor(dialogVisualizer->GetInteractor(), dialogVisualizer->GetRenderWindow()); // tells the visualizer what interactor is using now and for what window dialogViewer->getInteractorStyle()->setKeyboardModifier(pcl::visualization::INTERACTOR_KB_MOD_SHIFT); // ripristina input system of original visualizer (shift+click for points) pcl::PointCloud<pcl::PointXYZRGB>::Ptr temp = motor->getTargetCloud(); pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(temp); dialogViewer->addPointCloud<pcl::PointXYZRGB>(temp, rgb, "cloud"); dialogViewer->setBackgroundColor(0.5, 0.5, 0.5); dialogViewer->initCameraParameters(); dialogViewer->resetCamera(); componentCallbackConnection = dialogViewer->registerPointPickingCallback(&pointPickCallback, this); // callback standard non segmenta nulla QLineEdit *addComponentDialogName = new QLineEdit("Insert Component Name"); addComponentDialogName->setObjectName("componentname"); QHBoxLayout *dialogControlsLayout = new QHBoxLayout; QVBoxLayout *buttonsBox = new QVBoxLayout; QPushButton *selectPointButton = new QPushButton("Select Component"); connect(selectPointButton, SIGNAL(clicked()), this, SLOT(setComponentDialogCallback())); QPushButton *resetButton = new QPushButton("Reset Selection"); connect(resetButton, SIGNAL(clicked()), this, SLOT(resetComponentDialogCallback())); buttonsBox->addWidget(selectPointButton); buttonsBox->addWidget(resetButton); QVBoxLayout *numbersBox = new QVBoxLayout; QColor *selectedColor = new QColor(0, 0, 0, 255); // initialize color at black QPushButton *colorBox = new QPushButton("+-100"); colorBox->setObjectName("colorbox"); colorBox->setStyleSheet(colorToStyleSheet(selectedColor)); colorBox->setMaximumWidth(50); QLineEdit *clusterBox = new QLineEdit("1"); clusterBox->setReadOnly(true); clusterBox->setObjectName("clusterbox"); clusterBox->setMaxLength(5); clusterBox->setMaximumWidth(50); numbersBox->addWidget(clusterBox); numbersBox->addWidget(colorBox); QVBoxLayout *slidersBox = new QVBoxLayout; QSlider *setCluThresholdBar = new QSlider(Qt::Horizontal); setCluThresholdBar->setRange(0,5000); setCluThresholdBar->setValue(1000); motor->setClusterSegThreshold(1000); setCluThresholdBar->setObjectName("sliderCluster"); connect(setCluThresholdBar, SIGNAL(sliderReleased()), this, SLOT(setClusterThreshold())); QSlider *setColThresholdBar = new QSlider(Qt::Horizontal); setColThresholdBar->setRange(0,255); setColThresholdBar->setValue(100); motor->setColorSegThreshold(100); setColThresholdBar->setObjectName("sliderColor"); connect(setColThresholdBar, SIGNAL(sliderReleased()), this, SLOT(setColorThreshold())); slidersBox->addWidget(setCluThresholdBar); slidersBox->addWidget(setColThresholdBar); QPushButton *showSegButton = new QPushButton("Segment!"); connect(showSegButton, SIGNAL(clicked()), this, SLOT(segmentComponent())); dialogControlsLayout->addLayout(buttonsBox); dialogControlsLayout->addLayout(numbersBox); dialogControlsLayout->addLayout(slidersBox); dialogControlsLayout->addWidget(showSegButton); QPushButton *saveComponent = new QPushButton("Save to component list"); saveComponent->setDefault(true); // default button, pressed if enter is pressed connect(saveComponent, SIGNAL(clicked()), this, SLOT(saveComponent())); dialogLayout->addWidget(dialogVisualizer); dialogLayout->addWidget(addComponentDialogName); dialogLayout->addLayout(dialogControlsLayout); dialogLayout->addWidget(saveComponent); addComponentDialog->setLayout(dialogLayout); // DIALOG EXECUTION // addComponentDialog->deleteLater(); // delete dialog when the control returns to the event loop from which deleteLater() was called (after exec i guess) // causa seg fault se viene attivata la callback dialogLayout->deleteLater(); // delete dialog layout when the control returns to the event loop from which deleteLater() was called (after exec i guess) addComponentDialog->resize(800,600); addComponentDialog->exec(); componentCallbackConnection.disconnect(); // disconnect the callback function from the viewer delete dialogViewer; // finita l'esecuzione, deallocare il viewer (deallocare altra eventuale memoria non indirizzata nel QObject tree). }
void Ui::openDiffDialog() { diffDialog = new QDialog(this); // set as child of Ui, to be sure that it will be deleted in the end. QVBoxLayout *diffDialogLayout = new QVBoxLayout; // create vertical layout QVTKWidget *dialogVisualizer = new QVTKWidget; // create qvtk widget dialogViewer = new pcl::visualization::PCLVisualizer("Dialog Viewer", false); dialogVisualizer->SetRenderWindow(dialogViewer->getRenderWindow()); // set as render window the render window of the dialog visualizer dialogViewer->setupInteractor(dialogVisualizer->GetInteractor(), dialogVisualizer->GetRenderWindow()); // tells the visualizer what interactor is using now and for what window dialogViewer->getInteractorStyle()->setKeyboardModifier(pcl::visualization::INTERACTOR_KB_MOD_SHIFT); // ripristina input system of original visualizer (shift+click for points) pcl::PointCloud<pcl::PointXYZRGB>::Ptr regCloud = motor->getRegisteredCloud(); pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(regCloud); pcl::PointCloud<pcl::PointXYZRGB>::Ptr diffCloud = motor->getSourceDiffCloud(); pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> green(diffCloud, 0, 255, 0); pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> red(diffCloud, 255, 0, 0); int v1(0); int v2(0); dialogViewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1); dialogViewer->setBackgroundColor(0.5, 0.5, 0.5, v1); dialogViewer->addText("TARGET CLOUD", 30, 20,"TARGET CLOUD",v1); dialogViewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2); dialogViewer->setBackgroundColor(0.5, 0.5, 0.5, v2); dialogViewer->addText("SOURCE CLOUD", 30, 20,"SOURCE CLOUD",v2); dialogViewer->addPointCloud<pcl::PointXYZRGB>(motor->getTargetCloud(), rgb, "targetCloud", v1); dialogViewer->addPointCloud<pcl::PointXYZRGB>(diffCloud, red, "targetDiffCloud", v1); dialogViewer->addPointCloud<pcl::PointXYZRGB>(regCloud, rgb, "sourceCloud", v2); dialogViewer->addPointCloud<pcl::PointXYZRGB>(diffCloud, green, "sourceDiffCloud", v2); dialogViewer->initCameraParameters(); dialogViewer->resetCamera(); componentCallbackConnection = dialogViewer->registerPointPickingCallback(&pointPickCallback, this); // callback standard non segmenta nulla QHBoxLayout *dialogControlsLayout = new QHBoxLayout; QVBoxLayout *numbersBox = new QVBoxLayout; QLineEdit *segDiffBox = new QLineEdit("0.5"); segDiffBox->setReadOnly(true); segDiffBox->setObjectName("segdiffbox"); segDiffBox->setMaxLength(5); segDiffBox->setMaximumWidth(50); numbersBox->addWidget(segDiffBox); QVBoxLayout *slidersBox = new QVBoxLayout; QSlider *setSegDiffThresholdBar = new QSlider(Qt::Horizontal); setSegDiffThresholdBar->setRange(0, 5000); setSegDiffThresholdBar->setValue(500); motor->setSegDiffThreshold(500); setSegDiffThresholdBar->setObjectName("sliderSegDiff"); connect(setSegDiffThresholdBar, SIGNAL(sliderReleased()), this, SLOT(setSegDiffThreshold())); slidersBox->addWidget(setSegDiffThresholdBar); dialogControlsLayout->addLayout(numbersBox); dialogControlsLayout->addLayout(slidersBox); QPushButton *segmentDiffButton = new QPushButton("Segment differences"); segmentDiffButton->setDefault(true); // default button, pressed if enter is pressed connect(segmentDiffButton, SIGNAL(clicked()), this, SLOT(segmentDiff())); diffDialogLayout->addWidget(dialogVisualizer); diffDialogLayout->addLayout(dialogControlsLayout); diffDialogLayout->addWidget(segmentDiffButton); diffDialog->setLayout(diffDialogLayout); diffDialogLayout->deleteLater(); // delete dialog layout when the control returns to the event loop from which deleteLater() was called (after exec i guess) diffDialog->resize(800,600); diffDialog->exec(); componentCallbackConnection.disconnect(); // disconnect the callback function from the viewer delete dialogViewer; // finita l'esecuzione, deallocare il viewer (deallocare altra eventuale memoria non indirizzata nel QObject tree). }