private void VectorFieldNonZeroExtraction() { // Create an image vtkImageData image = vtkImageData.New(); CreateVectorField(ref image); // This filter produces a vtkImageData with an array named "Magnitude" vtkImageMagnitude magnitudeFilter = vtkImageMagnitude.New(); magnitudeFilter.SetInputConnection(image.GetProducerPort()); magnitudeFilter.Update(); image.GetPointData().AddArray(magnitudeFilter.GetOutput().GetPointData().GetScalars()); image.GetPointData().SetActiveScalars("Magnitude"); vtkThresholdPoints thresholdVector = vtkThresholdPoints.New(); thresholdVector.SetInput(image); thresholdVector.SetInputArrayToProcess( 0, 0, (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS, (int)vtkDataSetAttributes.AttributeTypes.SCALARS, "Magnitude"); thresholdVector.ThresholdByUpper(0.00001); thresholdVector.Update(); // in case you want to save imageData //vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New(); //writer.SetFileName("output.vtp"); //writer.SetInputConnection(thresholdPoints.GetOutputPort()); //writer.Write(); // repesents the pixels vtkCubeSource cubeSource = vtkCubeSource.New(); cubeSource.SetXLength(2.0); cubeSource.SetYLength(2.0); cubeSource.SetZLength(2.0); vtkGlyph3D glyph = vtkGlyph3D.New(); glyph.SetInput(image); glyph.SetSourceConnection(cubeSource.GetOutputPort()); // don't scale glyphs according to any scalar data glyph.SetScaleModeToDataScalingOff(); vtkPolyDataMapper glyphMapper = vtkPolyDataMapper.New(); glyphMapper.SetInputConnection(glyph.GetOutputPort()); // don't color glyphs according to scalar data glyphMapper.ScalarVisibilityOff(); glyphMapper.SetScalarModeToDefault(); vtkActor actor = vtkActor.New(); actor.SetMapper(glyphMapper); // represent vector field vtkGlyph3D vectorGlyph = vtkGlyph3D.New(); vtkArrowSource arrowSource = vtkArrowSource.New(); vtkPolyDataMapper vectorGlyphMapper = vtkPolyDataMapper.New(); int n = image.GetPointData().GetNumberOfArrays(); for (int i = 0; i < n; i++) { Debug.WriteLine("name of array[" + i + "]: " + image.GetPointData().GetArrayName(i)); } vtkPolyData tmp = thresholdVector.GetOutput(); Debug.WriteLine("number of thresholded points: " + tmp.GetNumberOfPoints()); vectorGlyph.SetInputConnection(thresholdVector.GetOutputPort()); // in case you want the point glyphs to be oriented according to // scalar values in array "ImageScalars" uncomment the following line image.GetPointData().SetActiveVectors("ImageScalars"); vectorGlyph.SetSourceConnection(arrowSource.GetOutputPort()); vectorGlyph.SetScaleModeToScaleByVector(); vectorGlyph.SetVectorModeToUseVector(); vectorGlyph.ScalingOn(); vectorGlyph.OrientOn(); vectorGlyph.SetInputArrayToProcess( 1, 0, (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS, (int)vtkDataSetAttributes.AttributeTypes.SCALARS, "ImageScalars"); vectorGlyph.Update(); vectorGlyphMapper.SetInputConnection(vectorGlyph.GetOutputPort()); vectorGlyphMapper.Update(); vtkActor vectorActor = vtkActor.New(); vectorActor.SetMapper(vectorGlyphMapper); // get a reference to the renderwindow of our renderWindowControl1 vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; // renderer vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); // set background color renderer.SetBackground(.2, .6, .3); //Add the actors to the renderer, set the background and size renderer.AddActor(actor); renderer.AddActor(vectorActor); }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVgaussian(String [] argv) { //Prefix Content is: "" ren1 = vtkRenderer.New(); renWin = vtkRenderWindow.New(); renWin.AddRenderer((vtkRenderer)ren1); renWin.SetSize((int)300,(int)300); iren = new vtkRenderWindowInteractor(); iren.SetRenderWindow((vtkRenderWindow)renWin); camera = new vtkCamera(); camera.ParallelProjectionOn(); camera.SetViewUp((double)0,(double)1,(double)0); camera.SetFocalPoint((double)12,(double)10.5,(double)15); camera.SetPosition((double)-70,(double)15,(double)34); camera.ComputeViewPlaneNormal(); ren1.SetActiveCamera((vtkCamera)camera); // Create the reader for the data[] //vtkStructuredPointsReader reader[] reader = new vtkGaussianCubeReader(); reader.SetFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/m4_TotalDensity.cube"); reader.SetHBScale((double)1.1); reader.SetBScale((double)10); reader.Update(); range = reader.GetGridOutput().GetPointData().GetScalars().GetRange(); min = (double)(lindex(range,0)); max = (double)(lindex(range,1)); readerSS = new vtkImageShiftScale(); readerSS.SetInput((vtkDataObject)reader.GetGridOutput()); readerSS.SetShift((double)min*-1); readerSS.SetScale((double)255/(max-min)); readerSS.SetOutputScalarTypeToUnsignedChar(); bounds = new vtkOutlineFilter(); bounds.SetInput((vtkDataObject)reader.GetGridOutput()); boundsMapper = vtkPolyDataMapper.New(); boundsMapper.SetInputConnection((vtkAlgorithmOutput)bounds.GetOutputPort()); boundsActor = new vtkActor(); boundsActor.SetMapper((vtkMapper)boundsMapper); boundsActor.GetProperty().SetColor((double)0,(double)0,(double)0); contour = new vtkContourFilter(); contour.SetInput((vtkDataObject)reader.GetGridOutput()); contour.GenerateValues((int)5,(double)0,(double).05); contourMapper = vtkPolyDataMapper.New(); contourMapper.SetInputConnection((vtkAlgorithmOutput)contour.GetOutputPort()); contourMapper.SetScalarRange((double)0,(double).1); ((vtkLookupTable)contourMapper.GetLookupTable()).SetHueRange(0.32,0); contourActor = new vtkActor(); contourActor.SetMapper((vtkMapper)contourMapper); contourActor.GetProperty().SetOpacity((double).5); // Create transfer mapping scalar value to opacity[] opacityTransferFunction = new vtkPiecewiseFunction(); opacityTransferFunction.AddPoint((double)0,(double)0.01); opacityTransferFunction.AddPoint((double)255,(double)0.35); opacityTransferFunction.ClampingOn(); // Create transfer mapping scalar value to color[] colorTransferFunction = new vtkColorTransferFunction(); colorTransferFunction.AddHSVPoint((double)0.0,(double)0.66,(double)1.0,(double)1.0); colorTransferFunction.AddHSVPoint((double)50.0,(double)0.33,(double)1.0,(double)1.0); colorTransferFunction.AddHSVPoint((double)100.0,(double)0.00,(double)1.0,(double)1.0); // The property describes how the data will look[] volumeProperty = new vtkVolumeProperty(); volumeProperty.SetColor((vtkColorTransferFunction)colorTransferFunction); volumeProperty.SetScalarOpacity((vtkPiecewiseFunction)opacityTransferFunction); volumeProperty.SetInterpolationTypeToLinear(); // The mapper / ray cast function know how to render the data[] compositeFunction = new vtkVolumeRayCastCompositeFunction(); volumeMapper = new vtkVolumeRayCastMapper(); //vtkVolumeTextureMapper2D volumeMapper[] volumeMapper.SetVolumeRayCastFunction((vtkVolumeRayCastFunction)compositeFunction); volumeMapper.SetInputConnection((vtkAlgorithmOutput)readerSS.GetOutputPort()); // The volume holds the mapper and the property and[] // can be used to position/orient the volume[] volume = new vtkVolume(); volume.SetMapper((vtkAbstractVolumeMapper)volumeMapper); volume.SetProperty((vtkVolumeProperty)volumeProperty); ren1.AddVolume((vtkProp)volume); //ren1 AddActor contourActor[] ren1.AddActor((vtkProp)boundsActor); //#####################################################################[] Sphere = new vtkSphereSource(); Sphere.SetCenter((double)0,(double)0,(double)0); Sphere.SetRadius((double)1); Sphere.SetThetaResolution((int)16); Sphere.SetStartTheta((double)0); Sphere.SetEndTheta((double)360); Sphere.SetPhiResolution((int)16); Sphere.SetStartPhi((double)0); Sphere.SetEndPhi((double)180); Glyph = new vtkGlyph3D(); Glyph.SetInputConnection((vtkAlgorithmOutput)reader.GetOutputPort()); Glyph.SetOrient((int)1); Glyph.SetColorMode((int)1); //Glyph ScalingOn[] Glyph.SetScaleMode((int)2); Glyph.SetScaleFactor((double).6); Glyph.SetSource((vtkPolyData)Sphere.GetOutput()); AtomsMapper = vtkPolyDataMapper.New(); AtomsMapper.SetInputConnection((vtkAlgorithmOutput)Glyph.GetOutputPort()); AtomsMapper.SetImmediateModeRendering((int)1); AtomsMapper.UseLookupTableScalarRangeOff(); AtomsMapper.SetScalarVisibility((int)1); AtomsMapper.SetScalarModeToDefault(); Atoms = new vtkActor(); Atoms.SetMapper((vtkMapper)AtomsMapper); Atoms.GetProperty().SetRepresentationToSurface(); Atoms.GetProperty().SetInterpolationToGouraud(); Atoms.GetProperty().SetAmbient((double)0.15); Atoms.GetProperty().SetDiffuse((double)0.85); Atoms.GetProperty().SetSpecular((double)0.1); Atoms.GetProperty().SetSpecularPower((double)100); Atoms.GetProperty().SetSpecularColor((double)1,(double)1,(double)1); Atoms.GetProperty().SetColor((double)1,(double)1,(double)1); Tube = new vtkTubeFilter(); Tube.SetInputConnection((vtkAlgorithmOutput)reader.GetOutputPort()); Tube.SetNumberOfSides((int)16); Tube.SetCapping((int)0); Tube.SetRadius((double)0.2); Tube.SetVaryRadius((int)0); Tube.SetRadiusFactor((double)10); BondsMapper = vtkPolyDataMapper.New(); BondsMapper.SetInputConnection((vtkAlgorithmOutput)Tube.GetOutputPort()); BondsMapper.SetImmediateModeRendering((int)1); BondsMapper.UseLookupTableScalarRangeOff(); BondsMapper.SetScalarVisibility((int)1); BondsMapper.SetScalarModeToDefault(); Bonds = new vtkActor(); Bonds.SetMapper((vtkMapper)BondsMapper); Bonds.GetProperty().SetRepresentationToSurface(); Bonds.GetProperty().SetInterpolationToGouraud(); Bonds.GetProperty().SetAmbient((double)0.15); Bonds.GetProperty().SetDiffuse((double)0.85); Bonds.GetProperty().SetSpecular((double)0.1); Bonds.GetProperty().SetSpecularPower((double)100); Bonds.GetProperty().SetSpecularColor((double)1,(double)1,(double)1); Bonds.GetProperty().SetColor((double)1,(double)1,(double)1); ren1.AddActor((vtkProp)Bonds); ren1.AddActor((vtkProp)Atoms); //###################################################[] ren1.SetBackground((double)1,(double)1,(double)1); ren1.ResetCamera(); renWin.Render(); //method moved renWin.AbortCheckEvt += new Kitware.VTK.vtkObject.vtkObjectEventHandler(TkCheckAbort_Command.Execute); iren.Initialize(); //deleteAllVTKObjects(); }
private void ReadPDB() { // Path to vtk data must be set as an environment variable // VTK_DATA_ROOT = "C:\VTK\vtkdata-5.8.0" vtkTesting test = vtkTesting.New(); string root = test.GetDataRoot(); string filePath = System.IO.Path.Combine(root, @"Data\caffeine.pdb"); vtkPDBReader pdb = vtkPDBReader.New(); pdb.SetFileName(filePath); pdb.SetHBScale(1.0); pdb.SetBScale(1.0); pdb.Update(); Debug.WriteLine("# of atoms is: " + pdb.GetNumberOfAtoms()); // if molecule contains a lot of atoms, reduce the resolution of the sphere (represents an atom) for faster rendering int resolution = (int)Math.Floor(Math.Sqrt(300000.0 / pdb.GetNumberOfAtoms())); // 300000.0 is an empriric value if (resolution > 20) { resolution = 20; } else if (resolution < 4) { resolution = 4; } Debug.WriteLine("Resolution is: " + resolution); vtkSphereSource sphere = vtkSphereSource.New(); sphere.SetCenter(0, 0, 0); sphere.SetRadius(1); sphere.SetThetaResolution(resolution); sphere.SetPhiResolution(resolution); vtkGlyph3D glyph = vtkGlyph3D.New(); glyph.SetInputConnection(pdb.GetOutputPort()); glyph.SetOrient(1); glyph.SetColorMode(1); // glyph.ScalingOn(); glyph.SetScaleMode(2); glyph.SetScaleFactor(.25); glyph.SetSourceConnection(sphere.GetOutputPort()); vtkPolyDataMapper atomMapper = vtkPolyDataMapper.New(); atomMapper.SetInputConnection(glyph.GetOutputPort()); atomMapper.UseLookupTableScalarRangeOff(); atomMapper.ScalarVisibilityOn(); atomMapper.SetScalarModeToDefault(); vtkLODActor atom = vtkLODActor.New(); atom.SetMapper(atomMapper); atom.GetProperty().SetRepresentationToSurface(); atom.GetProperty().SetInterpolationToGouraud(); atom.GetProperty().SetAmbient(0.15); atom.GetProperty().SetDiffuse(0.85); atom.GetProperty().SetSpecular(0.1); atom.GetProperty().SetSpecularPower(30); atom.GetProperty().SetSpecularColor(1, 1, 1); atom.SetNumberOfCloudPoints(30000); vtkTubeFilter tube = vtkTubeFilter.New(); tube.SetInputConnection(pdb.GetOutputPort()); tube.SetNumberOfSides(resolution); tube.CappingOff(); tube.SetRadius(0.2); // turn off variation of tube radius with scalar values tube.SetVaryRadius(0); tube.SetRadiusFactor(10); vtkPolyDataMapper bondMapper = vtkPolyDataMapper.New(); bondMapper.SetInputConnection(tube.GetOutputPort()); bondMapper.UseLookupTableScalarRangeOff(); bondMapper.ScalarVisibilityOff(); bondMapper.SetScalarModeToDefault(); vtkLODActor bond = vtkLODActor.New(); bond.SetMapper(bondMapper); bond.GetProperty().SetRepresentationToSurface(); bond.GetProperty().SetInterpolationToGouraud(); bond.GetProperty().SetAmbient(0.15); bond.GetProperty().SetDiffuse(0.85); bond.GetProperty().SetSpecular(0.1); bond.GetProperty().SetSpecularPower(30); bond.GetProperty().SetSpecularColor(1, 1, 1); bond.GetProperty().SetDiffuseColor(1.0000, 0.8941, 0.70981); // get a reference to the renderwindow of our renderWindowControl1 vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; // renderer vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); // set background color renderer.SetBackground(0.2, 0.3, 0.4); // add our actor to the renderer renderer.AddActor(atom); renderer.AddActor(bond); }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVTestMultiBlockStreamer(String [] argv) { //Prefix Content is: "" // we need to use composite data pipeline with multiblock datasets[] alg = new vtkAlgorithm(); pip = new vtkCompositeDataPipeline(); vtkAlgorithm.SetDefaultExecutivePrototype((vtkExecutive)pip); //skipping Delete pip Ren1 = vtkRenderer.New(); Ren1.SetBackground((double)0.33,(double)0.35,(double)0.43); renWin = vtkRenderWindow.New(); renWin.AddRenderer((vtkRenderer)Ren1); iren = new vtkRenderWindowInteractor(); iren.SetRenderWindow((vtkRenderWindow)renWin); Plot3D0 = new vtkPLOT3DReader(); Plot3D0.SetFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/combxyz.bin"); Plot3D0.SetQFileName((string)"" + (VTK_DATA_ROOT.ToString()) + "/Data/combq.bin"); Plot3D0.SetBinaryFile((int)1); Plot3D0.SetMultiGrid((int)0); Plot3D0.SetHasByteCount((int)0); Plot3D0.SetIBlanking((int)0); Plot3D0.SetTwoDimensionalGeometry((int)0); Plot3D0.SetForceRead((int)0); Plot3D0.SetByteOrder((int)0); Geometry5 = new vtkStructuredGridOutlineFilter(); Geometry5.SetInputConnection((vtkAlgorithmOutput)Plot3D0.GetOutputPort()); Mapper5 = vtkPolyDataMapper.New(); Mapper5.SetInputConnection((vtkAlgorithmOutput)Geometry5.GetOutputPort()); Mapper5.SetImmediateModeRendering((int)1); Mapper5.UseLookupTableScalarRangeOn(); Mapper5.SetScalarVisibility((int)0); Mapper5.SetScalarModeToDefault(); Actor5 = new vtkActor(); Actor5.SetMapper((vtkMapper)Mapper5); Actor5.GetProperty().SetRepresentationToSurface(); Actor5.GetProperty().SetInterpolationToGouraud(); Actor5.GetProperty().SetAmbient((double)0.15); Actor5.GetProperty().SetDiffuse((double)0.85); Actor5.GetProperty().SetSpecular((double)0.1); Actor5.GetProperty().SetSpecularPower((double)100); Actor5.GetProperty().SetSpecularColor((double)1,(double)1,(double)1); Actor5.GetProperty().SetColor((double)1,(double)1,(double)1); Ren1.AddActor((vtkProp)Actor5); ExtractGrid[0] = new vtkExtractGrid(); ExtractGrid[0].SetInputConnection((vtkAlgorithmOutput)Plot3D0.GetOutputPort()); ExtractGrid[0].SetVOI((int)0,(int)14,(int)0,(int)32,(int)0,(int)24); ExtractGrid[0].SetSampleRate((int)1,(int)1,(int)1); ExtractGrid[0].SetIncludeBoundary((int)0); ExtractGrid[1] = new vtkExtractGrid(); ExtractGrid[1].SetInputConnection((vtkAlgorithmOutput)Plot3D0.GetOutputPort()); ExtractGrid[1].SetVOI((int)14,(int)29,(int)0,(int)32,(int)0,(int)24); ExtractGrid[1].SetSampleRate((int)1,(int)1,(int)1); ExtractGrid[1].SetIncludeBoundary((int)0); ExtractGrid[2] = new vtkExtractGrid(); ExtractGrid[2].SetInputConnection((vtkAlgorithmOutput)Plot3D0.GetOutputPort()); ExtractGrid[2].SetVOI((int)29,(int)56,(int)0,(int)32,(int)0,(int)24); ExtractGrid[2].SetSampleRate((int)1,(int)1,(int)1); ExtractGrid[2].SetIncludeBoundary((int)0); LineSourceWidget0 = new vtkLineSource(); LineSourceWidget0.SetPoint1((double)3.05638,(double)-3.00497,(double)28.2211); LineSourceWidget0.SetPoint2((double)3.05638,(double)3.95916,(double)28.2211); LineSourceWidget0.SetResolution((int)20); mbds = new vtkMultiBlockDataSet(); mbds.SetNumberOfBlocks((uint)3); i = 0; while((i) < 3) { ExtractGrid[i].Update(); sg[i] = vtkStructuredGrid.New(); sg[i].ShallowCopy(ExtractGrid[i].GetOutput()); mbds.SetBlock((uint)i, sg[i]); //skipping Delete sg[i] i = i + 1; } Stream0 = new vtkStreamTracer(); Stream0.SetInput((vtkDataObject)mbds); Stream0.SetSource((vtkDataSet)LineSourceWidget0.GetOutput()); Stream0.SetIntegrationStepUnit(2); Stream0.SetMaximumPropagation((double)20); Stream0.SetInitialIntegrationStep((double)0.5); Stream0.SetIntegrationDirection((int)0); Stream0.SetIntegratorType((int)0); Stream0.SetMaximumNumberOfSteps((int)2000); Stream0.SetTerminalSpeed((double)1e-12); //skipping Delete mbds aa = new vtkAssignAttribute(); aa.SetInputConnection((vtkAlgorithmOutput)Stream0.GetOutputPort()); aa.Assign((string)"Normals",(string)"NORMALS",(string)"POINT_DATA"); Ribbon0 = new vtkRibbonFilter(); Ribbon0.SetInputConnection((vtkAlgorithmOutput)aa.GetOutputPort()); Ribbon0.SetWidth((double)0.1); Ribbon0.SetAngle((double)0); Ribbon0.SetDefaultNormal((double)0,(double)0,(double)1); Ribbon0.SetVaryWidth((int)0); LookupTable1 = new vtkLookupTable(); LookupTable1.SetNumberOfTableValues((int)256); LookupTable1.SetHueRange((double)0,(double)0.66667); LookupTable1.SetSaturationRange((double)1,(double)1); LookupTable1.SetValueRange((double)1,(double)1); LookupTable1.SetTableRange((double)0.197813,(double)0.710419); LookupTable1.SetVectorComponent((int)0); LookupTable1.Build(); Mapper10 = vtkPolyDataMapper.New(); Mapper10.SetInputConnection((vtkAlgorithmOutput)Ribbon0.GetOutputPort()); Mapper10.SetImmediateModeRendering((int)1); Mapper10.UseLookupTableScalarRangeOn(); Mapper10.SetScalarVisibility((int)1); Mapper10.SetScalarModeToUsePointFieldData(); Mapper10.SelectColorArray((string)"Density"); Mapper10.SetLookupTable((vtkScalarsToColors)LookupTable1); Actor10 = new vtkActor(); Actor10.SetMapper((vtkMapper)Mapper10); Actor10.GetProperty().SetRepresentationToSurface(); Actor10.GetProperty().SetInterpolationToGouraud(); Actor10.GetProperty().SetAmbient((double)0.15); Actor10.GetProperty().SetDiffuse((double)0.85); Actor10.GetProperty().SetSpecular((double)0); Actor10.GetProperty().SetSpecularPower((double)1); Actor10.GetProperty().SetSpecularColor((double)1,(double)1,(double)1); Ren1.AddActor((vtkProp)Actor10); // enable user interface interactor[] iren.Initialize(); // prevent the tk window from showing up then start the event loop[] vtkAlgorithm.SetDefaultExecutivePrototype(null); //skipping Delete alg //deleteAllVTKObjects(); }