TEST_F(ArrayChangeInformationFilter_test, vtkAssignAttribute_CorrectNumberOfComponentsPassedDownstream) { inAttr->SetNumberOfComponents(3); inAttr->SetNumberOfTuples(17); for (vtkIdType i = 0; i < inAttr->GetNumberOfValues(); ++i) { inAttr->SetValue(i, static_cast<float>(i)); } auto inImage = vtkSmartPointer<vtkImageData>::New(); inImage->GetPointData()->SetScalars(inAttr); inImage->SetExtent(0, 0, 2, 18, 3, 3); auto infoSource = vtkSmartPointer<InformationSource>::New(); infoSource->SetOutput(inImage); vtkDataObject::SetActiveAttributeInfo(infoSource->GetOutInfo(), vtkDataObject::FIELD_ASSOCIATION_POINTS, vtkDataSetAttributes::SCALARS, inAttr->GetName(), inAttr->GetDataType(), inAttr->GetNumberOfComponents(), static_cast<int>(inAttr->GetNumberOfTuples())); filter->SetAttributeLocation(ArrayChangeInformationFilter::POINT_DATA); filter->SetAttributeType(vtkDataSetAttributes::SCALARS); filter->SetInputConnection(infoSource->GetOutputPort()); filter->EnableRenameOff(); filter->EnableSetUnitOff(); auto assignToVectors = vtkSmartPointer<vtkAssignAttribute>::New(); assignToVectors->SetInputConnection(filter->GetOutputPort()); assignToVectors->Assign(vtkDataSetAttributes::SCALARS, vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA); auto reassignScalars = vtkSmartPointer<vtkAssignAttribute>::New(); reassignScalars->SetInputConnection(assignToVectors->GetOutputPort()); reassignScalars->Assign(vtkDataSetAttributes::VECTORS, vtkDataSetAttributes::SCALARS, vtkAssignAttribute::POINT_DATA); auto normalize = vtkSmartPointer<vtkImageNormalize>::New(); normalize->SetInputConnection(reassignScalars->GetOutputPort()); normalize->SetEnableSMP(false); normalize->SetNumberOfThreads(1); // before patching (VTK 7.1+) vtkAssignAttribute, this would cause segmentation faults normalize->Update(); auto outImg = normalize->GetOutput(); auto outScalars = outImg->GetPointData()->GetScalars(); ASSERT_EQ(inAttr->GetNumberOfComponents(), outScalars->GetNumberOfComponents()); ASSERT_EQ(inAttr->GetNumberOfTuples(), outScalars->GetNumberOfTuples()); }
static void SaveSliceOrImageAsPNG(mitk::Image::Pointer image, mitk::Image::Pointer mask, std::string path, int index) { // Create a Standalone Datastorage for the single purpose of saving screenshots.. mitk::StandaloneDataStorage::Pointer ds = mitk::StandaloneDataStorage::New(); QmitkRenderWindow renderWindow; renderWindow.GetRenderer()->SetDataStorage(ds); auto nodeI = mitk::DataNode::New(); nodeI->SetData(image); auto nodeM = mitk::DataNode::New(); nodeM->SetData(mask); ds->Add(nodeI); ds->Add(nodeM); auto geo = ds->ComputeBoundingGeometry3D(ds->GetAll()); mitk::RenderingManager::GetInstance()->InitializeViews(geo); mitk::SliceNavigationController::Pointer sliceNaviController = renderWindow.GetSliceNavigationController(); unsigned int numberOfSteps = 1; if (sliceNaviController) { numberOfSteps = sliceNaviController->GetSlice()->GetSteps(); sliceNaviController->GetSlice()->SetPos(0); } renderWindow.show(); renderWindow.resize(256, 256); for (unsigned int currentStep = 0; currentStep < numberOfSteps; ++currentStep) { if (sliceNaviController) { sliceNaviController->GetSlice()->SetPos(currentStep); } renderWindow.GetRenderer()->PrepareRender(); vtkRenderWindow* renderWindow2 = renderWindow.GetVtkRenderWindow(); mitk::BaseRenderer* baserenderer = mitk::BaseRenderer::GetInstance(renderWindow2); auto vtkRender = baserenderer->GetVtkRenderer(); vtkRender->GetRenderWindow()->WaitForCompletion(); vtkRenderLargeImage* magnifier = vtkRenderLargeImage::New(); magnifier->SetInput(vtkRender); magnifier->SetMagnification(3.0); std::stringstream ss; ss << path << "_Idx-" << index << "_Step-"<<currentStep<<".png"; std::string tmpImageName; ss >> tmpImageName; auto fileWriter = vtkPNGWriter::New(); fileWriter->SetInputConnection(magnifier->GetOutputPort()); fileWriter->SetFileName(tmpImageName.c_str()); fileWriter->Write(); fileWriter->Delete(); } }
vtkSmartPointer<vtkAlgorithm> createTransformPipeline( const CoordinateSystemSpecification & /*toSystem*/, vtkAlgorithmOutput * pipelineUpstream) const override { auto filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New(); filter->SetInputConnection(pipelineUpstream); filter->SetTransform(m_transform); return filter; }
vtkSmartPointer<vtkAlgorithm> GlyphMagnitudeColorMapping::createFilter(AbstractVisualizedData & visualizedData, unsigned int port) { if (port != 0) { return vtkSmartPointer<vtkPassThrough>::New(); } const auto filterIt = m_filters.find(&visualizedData); // Return already created filter (in this case, visualizedData is valid for this mapping) if (filterIt != m_filters.end()) { return filterIt->second; } // Check if the mapping can be applied to the provided visualization const auto glyphMappingDataIt = m_glyphMappingData.find(static_cast<RenderedData3D *>(&visualizedData)); if (glyphMappingDataIt == m_glyphMappingData.end()) { return vtkSmartPointer<vtkPassThrough>::New(); } // Create the mapping pipeline assert(glyphMappingDataIt->second); auto & glyphMappingData = *glyphMappingDataIt->second; auto norm = vtkSmartPointer<vtkVectorNorm>::New(); norm->SetInputConnection(glyphMappingData.vectorDataOutputPort()); auto assignVectors = vtkSmartPointer<vtkAssignAttribute>::New(); assignVectors->Assign(m_vectorName.toUtf8().data(), vtkDataSetAttributes::VECTORS, vtkAssignAttribute::POINT_DATA); assignVectors->SetInputConnection(norm->GetOutputPort()); auto setArrayName = vtkSmartPointer<ArrayChangeInformationFilter>::New(); setArrayName->EnableRenameOn(); setArrayName->SetArrayName(s_normScalarsName); setArrayName->SetAttributeType(vtkDataSetAttributes::SCALARS); setArrayName->SetAttributeLocation(IndexType::points); setArrayName->SetInputConnection(assignVectors->GetOutputPort()); m_filters[&visualizedData] = setArrayName; return setArrayName; }
vtkSmartPointer<vtkAlgorithm> PointCoordinateColorMapping::createFilter( AbstractVisualizedData & visualizedData, int connection) { auto coordsAttribute = vtkSmartPointer<AssignPointAttributeToCoordinatesFilter>::New(); coordsAttribute->CurrentCoordinatesAsScalarsOn(); coordsAttribute->SetInputConnection(visualizedData.colorMappingInput(connection)); return coordsAttribute; }
void StackedImageDataLIC3D::SimpleExecute(vtkImageData * input, vtkImageData * output) { vtkDataArray * vectors = input->GetPointData()->GetVectors(); if (!vectors) { vtkGenericWarningMacro("StackeImageDataLIC3D::SimpleExecute: missing input vectors"); return; } initialize(); auto appendTo3D = vtkSmartPointer<vtkImageAppend>::New(); appendTo3D->SetAppendAxis(2); int inputExtent[6]; input->GetExtent(inputExtent); for (int position = inputExtent[4]; position < inputExtent[5]; ++position) { int sliceExtent[6]; input->GetExtent(sliceExtent); sliceExtent[4] = sliceExtent[5] = position; auto slice = vtkSmartPointer<vtkExtractVOI>::New(); slice->SetVOI(sliceExtent); slice->SetInputData(input); auto lic2D = vtkSmartPointer<vtkImageDataLIC2D>::New(); lic2D->SetInputConnection(0, slice->GetOutputPort()); lic2D->SetInputConnection(1, m_noiseImage->GetOutputPort()); lic2D->SetSteps(50); lic2D->GlobalWarningDisplayOff(); lic2D->SetContext(m_glContext); appendTo3D->AddInputConnection(lic2D->GetOutputPort()); } appendTo3D->Update(); output->DeepCopy(appendTo3D->GetOutput()); }
vtkSmartPointer<vtkAlgorithm> ImageDataObject::createTransformPipeline( const CoordinateSystemSpecification & toSystem, vtkAlgorithmOutput * pipelineUpstream) const { if (!GeographicTransformationFilter::IsTransformationSupported( coordinateSystem(), toSystem)) { return{}; } auto filter = vtkSmartPointer<GeographicTransformationFilter>::New(); filter->SetInputConnection(pipelineUpstream); filter->SetTargetCoordinateSystem(toSystem); return filter; }
TEST_F(ArrayChangeInformationFilter_test, PassVectorInformation) { inAttr->SetNumberOfComponents(3); inAttr->SetNumberOfTuples(17); auto inImage = vtkSmartPointer<vtkImageData>::New(); inImage->GetPointData()->SetVectors(inAttr); inImage->SetExtent(0, 0, 2, 18, 3, 3); auto infoSource = vtkSmartPointer<InformationSource>::New(); infoSource->SetOutput(inImage); vtkDataObject::SetActiveAttributeInfo(infoSource->GetOutInfo(), vtkDataObject::FIELD_ASSOCIATION_POINTS, vtkDataSetAttributes::VECTORS, inAttr->GetName(), inAttr->GetDataType(), inAttr->GetNumberOfComponents(), static_cast<int>(inAttr->GetNumberOfTuples())); filter->SetAttributeLocation(ArrayChangeInformationFilter::POINT_DATA); filter->SetAttributeType(vtkDataSetAttributes::VECTORS); filter->SetInputConnection(infoSource->GetOutputPort()); filter->EnableRenameOff(); filter->EnableSetUnitOff(); auto getInfo = vtkSmartPointer<InformationSink>::New(); getInfo->SetInputConnection(filter->GetOutputPort()); getInfo->UpdateInformation(); auto outInfo = vtkDataObject::GetActiveFieldInformation(getInfo->GetInInfo(), vtkDataObject::FIELD_ASSOCIATION_POINTS, vtkDataSetAttributes::VECTORS); ASSERT_TRUE(outInfo); ASSERT_TRUE(outInfo->Has(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS())); ASSERT_EQ(inAttr->GetNumberOfComponents(), outInfo->Get(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS())); ASSERT_TRUE(outInfo->Has(vtkDataObject::FIELD_NUMBER_OF_TUPLES())); ASSERT_EQ(inAttr->GetNumberOfTuples(), outInfo->Get(vtkDataObject::FIELD_NUMBER_OF_TUPLES())); ASSERT_TRUE(outInfo->Has(vtkDataObject::FIELD_NAME())); ASSERT_STREQ(inAttr->GetName(), outInfo->Get(vtkDataObject::FIELD_NAME())); ASSERT_TRUE(outInfo->Has(vtkDataObject::FIELD_ARRAY_TYPE())); ASSERT_EQ(inAttr->GetDataType(), outInfo->Get(vtkDataObject::FIELD_ARRAY_TYPE())); }
int main(int, char *[]) { // Create a cube. vtkSmartPointer<vtkCubeSource> cubeSource = vtkSmartPointer<vtkCubeSource>::New(); // Create a mapper and actor. vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(cubeSource->GetOutputPort()); //save to ply std::string filename = "cube.ply"; auto plyWriter = vtkSmartPointer<vtkPLYWriter>::New(); plyWriter->SetFileName(filename.c_str()); plyWriter->SetInputConnection(cubeSource->GetOutputPort()); plyWriter->Write(); vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); // Create a renderer, render window, and interactor vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(renderWindow); // Add the actors to the scene renderer->AddActor(actor); renderer->SetBackground(.3, .2, .1); // Render and interact renderWindow->Render(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }
vtkSmartPointer<vtkPolyData> VoxelCarving::createVisualHull( const double isolevel) const { // create vtk visualization pipeline from voxel grid auto spoints = vtkSmartPointer<vtkStructuredPoints>::New(); auto vdim = static_cast<int>(voxel_dim_); spoints->SetDimensions(vdim, vdim, vdim); spoints->SetSpacing(params_.voxel_width, params_.voxel_height, params_.voxel_depth); spoints->SetOrigin(params_.start_x, params_.start_y, params_.start_z); auto farray = vtkSmartPointer<vtkFloatArray>::New(); auto vsize = static_cast<vtkIdType>(voxel_size_); farray->SetNumberOfValues(vsize); farray->SetArray(vox_array_.get(), vsize, 1); spoints->GetPointData()->SetScalars(farray); // create iso surface with marching cubes auto mc_source = vtkSmartPointer<vtkMarchingCubes>::New(); #if VTK_MAJOR_VERSION < 6 mc_source->SetInput(spoints); #else mc_source->SetInputData(spoints); #endif mc_source->SetNumberOfContours(1); mc_source->SetValue(0, isolevel); // calculate surface normals auto surface_normals = vtkSmartPointer<vtkPolyDataNormals>::New(); surface_normals->SetInputConnection(mc_source->GetOutputPort()); surface_normals->SetFeatureAngle(60.0); surface_normals->ComputePointNormalsOn(); surface_normals->Update(); return surface_normals->GetOutput(); }