private vtkImageData CreateVtkVolume() { vtkImageData vtkVolume = new vtkImageData(); VtkHelper.RegisterVtkErrorEvents(vtkVolume); vtkVolume.SetDimensions(ArrayDimensions.Width, ArrayDimensions.Height, ArrayDimensions.Depth); vtkVolume.SetOrigin(Origin.X, Origin.Y, Origin.Z); vtkVolume.SetSpacing(VoxelSpacing.X, VoxelSpacing.Y, VoxelSpacing.Z); if (!this.Signed) { vtkVolume.SetScalarTypeToUnsignedShort(); vtkVolume.GetPointData().SetScalars( VtkHelper.ConvertToVtkUnsignedShortArray(_volumeDataUInt16)); } else { vtkVolume.SetScalarTypeToShort(); vtkVolume.GetPointData().SetScalars( VtkHelper.ConvertToVtkShortArray(_volumeDataInt16)); } // This call is necessary to ensure vtkImageData data's info is correct (e.g. updates WholeExtent values) vtkVolume.UpdateInformation(); return(vtkVolume); }
private vtkImageData CreateVolumeImageData() { vtkImageData imageData = new vtkImageData(); imageData.SetDimensions(this.Width, this.Height, this.Depth); imageData.SetSpacing(GetFirstFrame().PixelSpacing.Column, GetFirstFrame().PixelSpacing.Row, GetSliceSpacing()); imageData.AllocateScalars(); imageData.SetScalarTypeToUnsignedShort(); imageData.GetPointData().SetScalars(BuildVolumeImageData()); return(imageData); }
private void Window_Activated(object sender, EventArgs e) { // 一定要先 vtkformhost.Child = vtkControl;下面三条顺序不能乱 否则报错 切记! vtkformhost.Child = vtkControl; vtkRenderWindow renWin = vtkControl.RenderWindow; vtkRenderer aRenderer = renWin.GetRenderers().GetFirstRenderer(); renWin.AddRenderer(aRenderer); Marshal.Copy(ys1, 0, temp128, imgLine * imgPixel * imgNum); temp.SetArray(temp128, imgLine * imgPixel * imgNum, 1); testimgdata.SetDimensions(imgLine, imgPixel, imgNum); testimgdata.SetSpacing(1, 1, 4); testimgdata.SetScalarTypeToUnsignedChar(); testimgdata.SetNumberOfScalarComponents(1); testimgdata.AllocateScalars(); testimgdata.GetPointData().SetScalars(temp); testimgdata.Modified(); VtkSetColor(); VtkSetOpacity(); volumeProperty.SetColor(colorTransferFunction); volumeProperty.SetScalarOpacity(opacityTransferFunction); volumeProperty.ShadeOn(); volumeProperty.SetInterpolationTypeToLinear(); volumeMapper.SetVolumeRayCastFunction(compositeFunction); volumeMapper.SetInput(testimgdata); volume.SetMapper(volumeMapper); volume.SetProperty(volumeProperty); aCamera.SetViewUp(0, -1, 0); aCamera.SetPosition(-1, 0, 1); aCamera.ComputeViewPlaneNormal(); aRenderer.AddVolume(volume); aRenderer.SetBackground(0, 0, 0.6); aRenderer.SetActiveCamera(aCamera); aRenderer.ResetCamera(); renWin.SetSize(800, 800); renWin.Render(); }
private void GreedyTerrainDecimation() { // Create an image vtkImageData image = vtkImageData.New(); image.SetDimensions(3, 3, 1); image.SetNumberOfScalarComponents(1); image.SetScalarTypeToUnsignedChar(); int[] dims = image.GetDimensions(); unsafe { for (int i = 0; i < dims[0]; i++) { for (int j = 0; j < dims[1]; j++) { byte *ptr = (byte *)image.GetScalarPointer(i, j, 0); * ptr = (byte)vtkMath.Round(vtkMath.Random(0, 1)); } } } vtkGreedyTerrainDecimation decimation = vtkGreedyTerrainDecimation.New(); decimation.SetInputConnection(image.GetProducerPort()); decimation.Update(); vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection(decimation.GetOutputPort()); // actor vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); actor.GetProperty().SetInterpolationToFlat(); actor.GetProperty().EdgeVisibilityOn(); actor.GetProperty().SetEdgeColor(1, 0, 0); // 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(actor); }
//把fyGrid转为vtkImageData private vtkImageData ConvertfyGrid2vtkImageData(ExGrid Grid) { vtkImageData ImageData = vtkImageData.New(); ImageData.SetScalarTypeToUnsignedChar(); ImageData.SetNumberOfScalarComponents(1); ImageData.SetDimensions(Grid.ICount, Grid.JCount, Grid.KCount); ImageData.SetSpacing(1, 1, 1); ImageData.AllocateScalars(); byte[] data = new byte[Grid.CellCount]; for (int i = 0; i < Grid.CellCount; i++) { data[i] = (byte)Grid.GetCell(i).Value; } System.Runtime.InteropServices.Marshal.Copy(data, 0, (IntPtr)ImageData.GetScalarPointer(), data.Length); return(ImageData); }
private static vtkImageData CreateVtkVolume(short[] data, int width, int height, int depth) { var vtkVolume = new vtkImageData(); RegisterVtkErrorEvents(vtkVolume); vtkVolume.SetDimensions(width, height, depth); vtkVolume.SetOrigin(0, 0, 0); vtkVolume.SetSpacing(1.0, 1.0, 1.0); using (var array = ConvertToVtkShortArray(data)) { vtkVolume.SetScalarTypeToShort(); vtkVolume.GetPointData().SetScalars(array); // This call is necessary to ensure vtkImageData data's info is correct (e.g. updates WholeExtent values) vtkVolume.UpdateInformation(); } return(vtkVolume); }
void CreateVectorField(ref vtkImageData image) { // Specify the size of the image data image.SetDimensions(3, 3, 3); image.SetNumberOfScalarComponents(3); image.SetScalarTypeToFloat(); image.AllocateScalars(); image.SetSpacing(10.0, 10.0, 10.0); int[] dims = image.GetDimensions(); float[] pixel = new float[] { 0.0f, 0.0f, 0.0f }; IntPtr pPixel; // Zero the vectors for (int z = 0; z < dims[2]; z++) { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { pPixel = image.GetScalarPointer(x, y, 0); Marshal.Copy(pixel, 0, pPixel, 3); } } } // Set two of the pixels to non zero values pixel[0] = 8.0f; pixel[1] = 8.0f; pixel[2] = -8.0f; pPixel = image.GetScalarPointer(0, 2, 0); Marshal.Copy(pixel, 0, pPixel, 3); pixel[0] = 8.0f; pixel[1] = -8.0f; pixel[2] = 8.0f; pPixel = image.GetScalarPointer(2, 0, 2); Marshal.Copy(pixel, 0, pPixel, 3); }
void CreateVectorField(ref vtkImageData image) { // Specify the size of the image data image.SetDimensions(3, 3, 3); image.SetNumberOfScalarComponents(3); image.SetScalarTypeToFloat(); image.AllocateScalars(); image.SetSpacing(10.0, 10.0, 10.0); int[] dims = image.GetDimensions(); float[] pixel = new float[] {0.0f, 0.0f, 0.0f}; IntPtr pPixel; // Zero the vectors for(int z = 0; z < dims[2]; z++) { for(int y = 0; y < dims[1]; y++) { for(int x = 0; x < dims[0]; x++) { pPixel = image.GetScalarPointer(x, y, 0); Marshal.Copy(pixel, 0, pPixel, 3); } } } // Set two of the pixels to non zero values pixel[0] = 8.0f; pixel[1] = 8.0f; pixel[2] = -8.0f; pPixel = image.GetScalarPointer(0, 2, 0); Marshal.Copy(pixel, 0, pPixel, 3); pixel[0] = 8.0f; pixel[1] = -8.0f; pixel[2] = 8.0f; pPixel = image.GetScalarPointer(2, 0, 2); Marshal.Copy(pixel, 0, pPixel, 3); }
public void Generate(CImage3D BinarySubImageSeq, Color Colour, cPoint3D Pos) { vtkImageData ImageData1 = new vtkImageData(); ImageData1.SetDimensions(BinarySubImageSeq.Width, BinarySubImageSeq.Height, BinarySubImageSeq.Depth); ImageData1.SetNumberOfScalarComponents(1); //ImageData1.SetSpacing(BinarySubImageSeq.XResolution, BinarySubImageSeq.YResolution, BinarySubImageSeq.ZResolution); ImageData1.SetScalarTypeToFloat(); vtkFloatArray array1 = new vtkFloatArray(); for (int i = 0; i < BinarySubImageSeq.ImageSize; i++) array1.InsertTuple1(i, BinarySubImageSeq.Data[i]); ImageData1.GetPointData().SetScalars(array1); vtkExtractVOI VOI = new vtkExtractVOI(); VOI.SetInput(ImageData1); VOI.SetSampleRate(1, 1, 1); vtkMarchingCubes ContourObject = vtkMarchingCubes.New(); vtk_PolyDataMapper = vtkPolyDataMapper.New(); //ContourActor = new vtkActor(); VOI.SetVOI(0, BinarySubImageSeq.Width - 1, 0, BinarySubImageSeq.Height - 1, 0, BinarySubImageSeq.Depth - 1); ContourObject.SetInput(VOI.GetOutput()); ContourObject.SetValue(0, 0.5); vtkAlgorithmOutput AlgoOutPut = null; vtk_PolyData = ContourObject.GetOutput(); AlgoOutPut = ContourObject.GetOutputPort(); vtk_PolyDataMapper.SetInput(vtk_PolyData); vtk_PolyDataMapper.ScalarVisibilityOn(); vtk_PolyDataMapper.SetScalarModeToUseFieldData(); vtkActor TmpActor = vtkActor.New(); TmpActor.SetMapper(vtk_PolyDataMapper); TmpActor.SetPosition(Pos.X, Pos.Y, Pos.Z); //Console.WriteLine("PosX"+Pos.X+" PosY"+Pos.Y+" PosZ"+Pos.Z); #region deal with the containers this.Position = new cPoint3D(Pos.X, Pos.Y, Pos.Z); this.Colour = Colour; CreateVTK3DObject(1); // vtk_PolyData = ContourObject.GetOutput(); // compute convex hull hullFilter = vtkHull.New(); hullFilter.SetInputConnection(AlgoOutPut); hullFilter.AddRecursiveSpherePlanes(1); hullFilter.Update(); // this.BackfaceCulling(false); Information = new cInformation(AlgoOutPut, this, hullFilter); #endregion }
private static vtkImageData CreateVtkVolume(short[] data, int width, int height, int depth) { var vtkVolume = new vtkImageData(); RegisterVtkErrorEvents(vtkVolume); vtkVolume.SetDimensions(width, height, depth); vtkVolume.SetOrigin(0, 0, 0); vtkVolume.SetSpacing(1.0, 1.0, 1.0); using (var array = ConvertToVtkShortArray(data)) { vtkVolume.SetScalarTypeToShort(); vtkVolume.GetPointData().SetScalars(array); // This call is necessary to ensure vtkImageData data's info is correct (e.g. updates WholeExtent values) vtkVolume.UpdateInformation(); } return vtkVolume; }
private static vtkImageData CreateVtkVolume(Volume volume) { var vtkVolume = new vtkImageData(); vtkVolume.RegisterVtkErrorEvents(); vtkVolume.SetDimensions(volume.ArrayDimensions.Width, volume.ArrayDimensions.Height, volume.ArrayDimensions.Depth); vtkVolume.SetOrigin(0, 0, 0); vtkVolume.SetSpacing(volume.VoxelSpacing.X, volume.VoxelSpacing.Y, volume.VoxelSpacing.Z); if (volume.BitsPerVoxel == 16) { if (!volume.Signed) { using (var array = new vtkUnsignedShortArray()) { array.SetArray((ushort[]) volume.Array, (VtkIdType) volume.ArrayLength, 1); vtkVolume.SetScalarTypeToUnsignedShort(); vtkVolume.GetPointData().SetScalars(array); } } else { using (var array = new vtkShortArray()) { array.SetArray((short[]) volume.Array, (VtkIdType) volume.ArrayLength, 1); vtkVolume.SetScalarTypeToShort(); vtkVolume.GetPointData().SetScalars(array); } } } else if (volume.BitsPerVoxel == 8) { if (!volume.Signed) { using (var array = new vtkUnsignedCharArray()) { array.SetArray((byte[]) volume.Array, (VtkIdType) volume.ArrayLength, 1); vtkVolume.SetScalarTypeToUnsignedChar(); vtkVolume.GetPointData().SetScalars(array); } } else { using (var array = new vtkSignedCharArray()) { array.SetArray((sbyte[]) volume.Array, (VtkIdType) volume.ArrayLength, 1); vtkVolume.SetScalarTypeToSignedChar(); vtkVolume.GetPointData().SetScalars(array); } } } else { throw new NotSupportedException("Unsupported volume scalar type."); } // This call is necessary to ensure vtkImageData data's info is correct (e.g. updates WholeExtent values) vtkVolume.UpdateInformation(); return vtkVolume; }
//private void CommonInit(Sequence BinarySubImageSeq) //{ // vtkImageData ImageData1 = new vtkImageData(); // ImageData1.SetDimensions(BinarySubImageSeq.Width, BinarySubImageSeq.Height, BinarySubImageSeq.Depth); // ImageData1.SetNumberOfScalarComponents(1); // ImageData1.SetSpacing(BinarySubImageSeq.XResolution, BinarySubImageSeq.YResolution, BinarySubImageSeq.ZResolution); // ImageData1.SetScalarTypeToFloat(); // vtkFloatArray array1 = new vtkFloatArray(); // for (int i = 0; i < BinarySubImageSeq.ImageSize; i++) // array1.InsertTuple1(i, BinarySubImageSeq[0].Data[0][i]); // ImageData1.GetPointData().SetScalars(array1); // vtkExtractVOI VOI = new vtkExtractVOI(); // VOI.SetInput(ImageData1); // VOI.SetSampleRate(1, 1, 1); // vtkMarchingCubes ContourObject = vtkMarchingCubes.New(); // vtk_PolyDataMapper = vtkPolyDataMapper.New(); // //ContourActor = new vtkActor(); // VOI.SetVOI(0, BinarySubImageSeq.Width - 1, 0, BinarySubImageSeq.Height - 1, 0, BinarySubImageSeq.Depth - 1); // ContourObject.SetInput(VOI.GetOutput()); // ContourObject.SetValue(0, 0.5); // vtk_PolyDataMapper.SetInput(ContourObject.GetOutput()); // vtk_PolyDataMapper.ScalarVisibilityOn(); // vtk_PolyDataMapper.SetScalarModeToUseFieldData(); //} ///// <summary> ///// Generate a 3D mesh using marching-cubes algorithm. If voxel value is lower than 1 it is consider as background, else as object ///// </summary> ///// <param name="BinarySubImageSeq">The binary image</param> ///// <param name="Colour">Mesh color</param> ///// <param name="Pos">Postion of the object in the world</param> //public void Generate(Sequence BinarySubImageSeq, Color Colour, cPoint3D Pos) //{ // vtkImageData ImageData1 = new vtkImageData(); // ImageData1.SetDimensions(BinarySubImageSeq.Width, BinarySubImageSeq.Height, BinarySubImageSeq.Depth); // ImageData1.SetNumberOfScalarComponents(1); // ImageData1.SetSpacing(BinarySubImageSeq.XResolution, BinarySubImageSeq.YResolution, BinarySubImageSeq.ZResolution); // ImageData1.SetScalarTypeToFloat(); // vtkFloatArray array1 = new vtkFloatArray(); // for (int i = 0; i < BinarySubImageSeq.ImageSize; i++) // array1.InsertTuple1(i, BinarySubImageSeq[0].Data[0][i]); // ImageData1.GetPointData().SetScalars(array1); // vtkExtractVOI VOI = new vtkExtractVOI(); // VOI.SetInput(ImageData1); // VOI.SetSampleRate(1, 1, 1); // vtkMarchingCubes ContourObject = vtkMarchingCubes.New(); // vtk_PolyDataMapper = vtkPolyDataMapper.New(); // //ContourActor = new vtkActor(); // VOI.SetVOI(0, BinarySubImageSeq.Width - 1, 0, BinarySubImageSeq.Height - 1, 0, BinarySubImageSeq.Depth - 1); // // perform the amrching cubes // ContourObject.SetInput(VOI.GetOutput()); // ContourObject.SetValue(0, 0.5); // //vtkDecimatePro deci // //deci SetInputConnection [fran GetOutputPort] // //deci SetTargetReduction 0.9 // //deci PreserveTopologyOn // if (MeshSmoother!=null) // { // vtkSmoothPolyDataFilter smoother = new vtkSmoothPolyDataFilter(); // smoother.SetInputConnection(ContourObject.GetOutputPort());// [deci GetOutputPort] // smoother.SetNumberOfIterations(50); // vtk_PolyData = smoother.GetOutput(); // } // else // { // vtk_PolyData = ContourObject.GetOutput(); // } // vtk_PolyDataMapper.SetInput(vtk_PolyData); // vtk_PolyDataMapper.ScalarVisibilityOn(); // vtk_PolyDataMapper.SetScalarModeToUseFieldData(); // this.Position = new cPoint3D(Pos.X, Pos.Y, Pos.Z); // this.Colour = Colour; // CreateVTK3DObject(1); // vtk_PolyData = ContourObject.GetOutput(); // // compute convex hull // hullFilter = vtkHull.New(); // hullFilter.SetInputConnection(ContourObject.GetOutputPort()); // hullFilter.AddRecursiveSpherePlanes(1); // hullFilter.Update(); // // this.BackfaceCulling(false); // Information = new cInformation(ContourObject, this, hullFilter); //} public void Generate(cImage BinarySubImageSeq, Color Colour, cPoint3D Pos/*, List<cBiological3DObject> Containers, int ContainerMode*/) { vtkImageData ImageData1 = new vtkImageData(); ImageData1.SetDimensions(BinarySubImageSeq.Width, BinarySubImageSeq.Height, BinarySubImageSeq.Depth); ImageData1.SetNumberOfScalarComponents(1); ImageData1.SetSpacing(BinarySubImageSeq.Resolution.X, BinarySubImageSeq.Resolution.Y, BinarySubImageSeq.Resolution.Z); ImageData1.SetScalarTypeToFloat(); vtkFloatArray array1 = new vtkFloatArray(); for (int i = 0; i < BinarySubImageSeq.ImageSize; i++) array1.InsertTuple1(i, BinarySubImageSeq.SingleChannelImage[0].Data[i]); ImageData1.GetPointData().SetScalars(array1); vtkExtractVOI VOI = new vtkExtractVOI(); VOI.SetInput(ImageData1); VOI.SetSampleRate(1, 1, 1); vtkMarchingCubes ContourObject = vtkMarchingCubes.New(); vtk_PolyDataMapper = vtkPolyDataMapper.New(); //ContourActor = new vtkActor(); VOI.SetVOI(0, BinarySubImageSeq.Width - 1, 0, BinarySubImageSeq.Height - 1, 0, BinarySubImageSeq.Depth - 1); ContourObject.SetInput(VOI.GetOutput()); ContourObject.SetValue(0, 0.5); vtkAlgorithmOutput AlgoOutPut = null; if (MeshSmoother != null) { //vtkSmoothPolyDataFilter smoother = new vtkSmoothPolyDataFilter(); vtkWindowedSincPolyDataFilter smoother = new vtkWindowedSincPolyDataFilter(); smoother.SetInputConnection(ContourObject.GetOutputPort());// [deci GetOutputPort] smoother.SetNumberOfIterations(MeshSmoother.NumberOfIterations); vtk_PolyData = smoother.GetOutput(); //smoother.GetOutputPort(); AlgoOutPut = smoother.GetOutputPort(); } else { vtk_PolyData = ContourObject.GetOutput(); AlgoOutPut = ContourObject.GetOutputPort(); } vtk_PolyDataMapper.SetInput(vtk_PolyData); vtk_PolyDataMapper.ScalarVisibilityOn(); vtk_PolyDataMapper.SetScalarModeToUseFieldData(); vtkActor TmpActor = vtkActor.New(); TmpActor.SetMapper(vtk_PolyDataMapper); TmpActor.SetPosition(Pos.X, Pos.Y, Pos.Z); //Console.WriteLine("PosX"+Pos.X+" PosY"+Pos.Y+" PosZ"+Pos.Z); #region deal with the containers if (this.Containers != null) { if (this.Containers.ContainerMode == 0) { cPoint3D Centroid = new cPoint3D((float)TmpActor.GetCenter()[0], (float)TmpActor.GetCenter()[1], (float)TmpActor.GetCenter()[2]); bool IsInside = false; for (int Idx = 0; Idx < Containers.Containers.Count; Idx++) { cBiological3DVolume CurrentContainer = (cBiological3DVolume)(Containers.Containers[Idx]); if (CurrentContainer.IsPointInside(Centroid)) { IsInside = true; ContainerIdx = Idx; break; } } if (IsInside) { this.SetPosition(new cPoint3D(Pos.X, Pos.Y, Pos.Z)); this.Colour = Colour; CreateVTK3DObject(1); // vtk_PolyData = ContourObject.GetOutput(); // compute convex hull hullFilter = vtkHull.New(); hullFilter.SetInputConnection(AlgoOutPut); hullFilter.AddRecursiveSpherePlanes(0); hullFilter.Update(); Information = new cInformation(AlgoOutPut, this, hullFilter); this.Detected = true; } else { this.Detected = false; } } else if (Containers.ContainerMode == 1) { this.Detected = true; //bool IsInside = false; for (int Idx = 0; Idx < Containers.Containers.Count; Idx++) { cBiological3DVolume CurrentContainer = (cBiological3DVolume)(Containers.Containers[Idx]); if (CurrentContainer.IsPointInside(Pos)) { //IsInside = false; this.Detected = false; return; } } this.SetPosition(new cPoint3D(Pos.X, Pos.Y, Pos.Z)); this.Colour = Colour; CreateVTK3DObject(1); // vtk_PolyData = ContourObject.GetOutput(); // compute convex hull hullFilter = vtkHull.New(); hullFilter.SetInputConnection(AlgoOutPut); hullFilter.AddRecursiveSpherePlanes(0); hullFilter.Update(); Information = new cInformation(AlgoOutPut, this, hullFilter); this.Detected = true; } } else { this.SetPosition(new cPoint3D(Pos.X, Pos.Y, Pos.Z)); this.Colour = Colour; CreateVTK3DObject(1); // vtk_PolyData = ContourObject.GetOutput(); // compute convex hull hullFilter = vtkHull.New(); hullFilter.SetInputConnection(AlgoOutPut); hullFilter.AddRecursiveSpherePlanes(1); hullFilter.Update(); // this.BackfaceCulling(false); Information = new cInformation(AlgoOutPut, this, hullFilter); } #endregion }
private void WriteVTI() { // 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\test_vti.vti"); vtkImageData imageData = vtkImageData.New(); imageData.SetDimensions(3, 4, 5); imageData.SetNumberOfScalarComponents(1); imageData.SetScalarTypeToDouble(); int[] dims = imageData.GetDimensions(); // Fill every entry of the image data with "2.0" /* we can do this in unsafe mode which looks pretty similar to the c++ version * but then you must declare at the very top of your file the "preprocessor" directive * #define UNSAFE * * or whatever name you choose for the following preprocessor #if statement */ #if UNSAFE unsafe { for (int z = 0; z < dims[2]; z++) { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { double *pixel = (double *)imageData.GetScalarPointer(x, y, z).ToPointer(); // c++ version: // double* pixel = static_cast<double*>(imageData->GetScalarPointer(x,y,z)); pixel[0] = 2.0; } } } } #else /* or we can do it in managed mode */ int size = imageData.GetScalarSize(); IntPtr ptr = Marshal.AllocHGlobal(size); for (int z = 0; z < dims[2]; z++) { for (int y = 0; y < dims[1]; y++) { for (int x = 0; x < dims[0]; x++) { ptr = imageData.GetScalarPointer(x, y, z); Marshal.Copy(new double[] { 2.0 }, 0, ptr, 1); } } } Marshal.FreeHGlobal(ptr); #endif vtkXMLImageDataWriter writer = vtkXMLImageDataWriter.New(); writer.SetFileName(filePath); writer.SetInputConnection(imageData.GetProducerPort()); writer.Write(); // Read and display file for verification that it was written correctly vtkXMLImageDataReader reader = vtkXMLImageDataReader.New(); if (reader.CanReadFile(filePath) == 0) { MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK); return; } reader.SetFileName(filePath); reader.Update(); // Convert the image to a polydata vtkImageDataGeometryFilter imageDataGeometryFilter = vtkImageDataGeometryFilter.New(); imageDataGeometryFilter.SetInputConnection(reader.GetOutputPort()); imageDataGeometryFilter.Update(); vtkDataSetMapper mapper = vtkDataSetMapper.New(); mapper.SetInputConnection(imageDataGeometryFilter.GetOutputPort()); // actor vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); actor.GetProperty().SetPointSize(4); // 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(actor); }
private vtkImageData CreateVolumeImageData() { vtkImageData imageData = new vtkImageData(); imageData.SetDimensions(this.Width, this.Height, this.Depth); imageData.SetSpacing(GetFirstFrame().PixelSpacing.Column, GetFirstFrame().PixelSpacing.Row, GetSliceSpacing()); imageData.AllocateScalars(); imageData.SetScalarTypeToUnsignedShort(); imageData.GetPointData().SetScalars(BuildVolumeImageData()); return imageData; }
private static vtkImageData CreateVtkVolume(Volume volume) { var vtkVolume = new vtkImageData(); vtkVolume.RegisterVtkErrorEvents(); vtkVolume.SetDimensions(volume.ArrayDimensions.Width, volume.ArrayDimensions.Height, volume.ArrayDimensions.Depth); vtkVolume.SetOrigin(0, 0, 0); vtkVolume.SetSpacing(volume.VoxelSpacing.X, volume.VoxelSpacing.Y, volume.VoxelSpacing.Z); if (volume.BitsPerVoxel == 16) { if (!volume.Signed) { using (var array = new vtkUnsignedShortArray()) { array.SetArray((ushort[])volume.Array, (VtkIdType)volume.ArrayLength, 1); vtkVolume.SetScalarTypeToUnsignedShort(); vtkVolume.GetPointData().SetScalars(array); } } else { using (var array = new vtkShortArray()) { array.SetArray((short[])volume.Array, (VtkIdType)volume.ArrayLength, 1); vtkVolume.SetScalarTypeToShort(); vtkVolume.GetPointData().SetScalars(array); } } } else if (volume.BitsPerVoxel == 8) { if (!volume.Signed) { using (var array = new vtkUnsignedCharArray()) { array.SetArray((byte[])volume.Array, (VtkIdType)volume.ArrayLength, 1); vtkVolume.SetScalarTypeToUnsignedChar(); vtkVolume.GetPointData().SetScalars(array); } } else { using (var array = new vtkSignedCharArray()) { array.SetArray((sbyte[])volume.Array, (VtkIdType)volume.ArrayLength, 1); vtkVolume.SetScalarTypeToSignedChar(); vtkVolume.GetPointData().SetScalars(array); } } } else { throw new NotSupportedException("Unsupported volume scalar type."); } // This call is necessary to ensure vtkImageData data's info is correct (e.g. updates WholeExtent values) vtkVolume.UpdateInformation(); return(vtkVolume); }
private vtkImageData CreateVtkVolume() { vtkImageData vtkVolume = new vtkImageData(); VtkHelper.RegisterVtkErrorEvents(vtkVolume); vtkVolume.SetDimensions(ArrayDimensions.Width, ArrayDimensions.Height, ArrayDimensions.Depth); vtkVolume.SetOrigin(Origin.X, Origin.Y, Origin.Z); vtkVolume.SetSpacing(VoxelSpacing.X, VoxelSpacing.Y, VoxelSpacing.Z); if (!this.Signed) { vtkVolume.SetScalarTypeToUnsignedShort(); vtkVolume.GetPointData().SetScalars( VtkHelper.ConvertToVtkUnsignedShortArray(_volumeDataUInt16)); } else { vtkVolume.SetScalarTypeToShort(); vtkVolume.GetPointData().SetScalars( VtkHelper.ConvertToVtkShortArray(_volumeDataInt16)); } // This call is necessary to ensure vtkImageData data's info is correct (e.g. updates WholeExtent values) vtkVolume.UpdateInformation(); return vtkVolume; }
static public vtkProp3D genFieldActor(FieldBase data) { int N_width = data.Ex.Count; int M_depth = data.Ex.Count; double ds = data.ds_x; double width = N_width * ds; double depth = M_depth * ds; vtkImageData img = vtkImageData.New(); img.SetDimensions(N_width, M_depth, 1); img.SetSpacing(0.01 * ds / 0.01, 0.01 * ds / 0.01, 1); img.SetScalarTypeToDouble(); img.SetNumberOfScalarComponents(1); double max = -100000000, min = 0; List <List <Complex> > tempEH = null; int content = 1; bool isPhs = false; bool isLinear = true; const double dB_RABNGE = 60; switch (content) { case 0: tempEH = data.Ex; break; case 1: tempEH = data.Ey; break; case 2: tempEH = data.Ez; break; case 3: tempEH = data.Hx; break; case 4: tempEH = data.Hy; break; case 5: tempEH = data.Hz; break; default: break; } double[] data_tmp = new double[M_depth * N_width]; int count = 0; for (int j = 0; j < M_depth; j++) { for (int i = 0; i < N_width; i++) { double tempD; Complex temp; temp = tempEH[i][j]; if (isPhs) { if (temp.real != 0) { tempD = Math.Atan2(temp.imag, temp.real); } else { tempD = 0; } } else { tempD = Math.Pow((temp.real * temp.real + temp.imag * temp.imag), 0.5); } if (!isLinear && !isPhs) { tempD = 20 * Math.Log(tempD + 0.000000001); if (min > tempD) { min = tempD; } if (max < tempD) { max = tempD; } } else { if (max < tempD) { max = tempD; } if (min > tempD) { min = tempD; } } data_tmp[count++] = tempD; } } //ptr = img.GetScalarPointer(); vtkLookupTable colorTable = vtkLookupTable.New(); if (!isLinear && !isPhs) { min = max - dB_RABNGE; } if (!isPhs) { for (int i = 0; i < N_width * M_depth * 1; i++) { data_tmp[i] = max - data_tmp[i]; } colorTable.SetRange(0, max - min); } else { colorTable.SetRange(min, max); } IntPtr ptr = img.GetScalarPointer(); System.Runtime.InteropServices.Marshal.Copy(data_tmp, 0, ptr, M_depth * N_width); colorTable.Build(); vtkImageMapToColors colorMap = vtkImageMapToColors.New(); colorMap.SetInput(img); colorMap.SetLookupTable(colorTable); colorMap.Update(); vtkTransform transform = vtkTransform.New(); transform.Translate(data.coordinate.pos.x, data.coordinate.pos.y, data.coordinate.pos.z); transform.RotateWXYZ(data.coordinate.rotate_theta, data.coordinate.rotate_axis.x, data.coordinate.rotate_axis.y, data.coordinate.rotate_axis.z); transform.Translate(-width / 2, -depth / 2, 0); vtkImageActor actor = vtkImageActor.New(); actor.SetInput(colorMap.GetOutput()); actor.SetUserTransform(transform); return(actor); }