void CreateData(ref vtkImageData data) { data.SetExtent(-25, 25, -25, 25, 0, 0); #if VTK_MAJOR_VERSION_5 data.SetNumberOfScalarComponents(1); data.SetScalarTypeToDouble(); #else data.AllocateScalars(VTK_DOUBLE, 1); #endif int[] extent = data.GetExtent(); for (int y = extent[2]; y <= extent[3]; y++) { for (int x = extent[0]; x <= extent[1]; x++) { IntPtr ptr = data.GetScalarPointer(x, y, 0); double[] pixel = new double[] { Math.Sqrt(Math.Pow(x, 2.0) + Math.Pow(y, 2.0)) }; Marshal.Copy(pixel, 0, ptr, 1); } } vtkXMLImageDataWriter writer = vtkXMLImageDataWriter.New(); writer.SetFileName(@"c:\vtk\vtkdata-5.8.0\Data\testIsoContours.vti"); #if VTK_MAJOR_VERSION_5 writer.SetInputConnection(data.GetProducerPort()); #else writer.SetInputData(data); #endif writer.Write(); }
private static byte[] MipPixelDataFromVtkSlab(vtkImageData slabImageData) { #if true // Do our own MIP, albeit slowly int[] sliceDimensions = slabImageData.GetDimensions(); int sliceDataSize = sliceDimensions[0] * sliceDimensions[1]; IntPtr slabDataPtr = slabImageData.GetScalarPointer(); byte[] pixelData = MemoryManager.Allocate <byte>(sliceDataSize * sizeof(short)); // Init with first slice Marshal.Copy(slabDataPtr, pixelData, 0, sliceDataSize * sizeof(short)); // Walk through other slices, finding maximum unsafe { short *psSlab = (short *)slabDataPtr; fixed(byte *pbFrame = pixelData) { short *psFrame = (short *)pbFrame; for (int sliceIndex = 1; sliceIndex < sliceDimensions[2]; sliceIndex++) { for (int i = 0; i < sliceDataSize - 1; ++i) { int slabIndex = sliceIndex * sliceDataSize + i; if (psSlab[slabIndex] > psFrame[i]) { psFrame[i] = psSlab[slabIndex]; } } } } } return(pixelData); #else // Ideally we'd use VTK to do the MIP (MinIP, Average...) vtkVolumeRayCastMIPFunction mip = new vtkVolumeRayCastMIPFunction(); vtkVolumeRayCastMapper mapper = new vtkVolumeRayCastMapper(); mapper.SetVolumeRayCastFunction(mip); mapper.SetInput(slabImageData); //TODO: Need to figure out how to use mapper to output vtkImageData vtkImageAlgorithm algo = new vtkImageAlgorithm(); algo.SetInput(mapper.GetOutputDataObject(0)); using (vtkExecutive exec = mapper.GetExecutive()) { VtkHelper.RegisterVtkErrorEvents(exec); exec.Update(); // Note: These report no output port, must have to do something else to get mapper to give us data //return exec.GetOutputData(0); return(mapper.GetOutputDataObject(0)); } #endif }
private static byte[] CreatePixelDataFromVtkSlice(vtkImageData sliceImageData) { int[] sliceDimensions = sliceImageData.GetDimensions(); int sliceDataSize = sliceDimensions[0] * sliceDimensions[1]; IntPtr sliceDataPtr = sliceImageData.GetScalarPointer(); byte[] pixelData = MemoryManager.Allocate <byte>(sliceDataSize * sizeof(short)); Marshal.Copy(sliceDataPtr, pixelData, 0, sliceDataSize * sizeof(short)); return(pixelData); }
/// <summary> /// Creates byte array from vtkImageData /// </summary> /// <param name="idata"></param> /// <returns></returns> public static byte[] getArrayFromImageData(vtkImageData idata) { int width = (int)(idata.GetBounds()[1] - idata.GetBounds()[0]) + 1; int height = (int)(idata.GetBounds()[3] - idata.GetBounds()[2]) + 1; byte[] arr = new byte[width * height * idata.GetNumberOfScalarComponents()]; System.Runtime.InteropServices.Marshal.Copy( ((IntPtr)idata.GetScalarPointer()), arr, 0, width * height * idata.GetNumberOfScalarComponents()); return arr; }
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); }
/// <summary> /// Creates byte array from vtkImageData /// </summary> /// <param name="idata"></param> /// <returns></returns> static public byte[] getArrayFromImageData(vtkImageData idata) { int width = (int)(idata.GetBounds()[1] - idata.GetBounds()[0]) + 1; int height = (int)(idata.GetBounds()[3] - idata.GetBounds()[2]) + 1; byte[] arr = new byte[width * height * idata.GetNumberOfScalarComponents()]; System.Runtime.InteropServices.Marshal.Copy( ((IntPtr)idata.GetScalarPointer()), arr, 0, width * height * idata.GetNumberOfScalarComponents()); return(arr); }
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); }
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 byte[] ReadVtkImageData(vtkImageData imageData) { // get the pointer to the image data var pData = imageData.GetScalarPointer(); if (pData.Equals(IntPtr.Zero)) { return(null); } // get number of pixels in data var dataDimensions = imageData.GetDimensions(); var dataCount = dataDimensions[0] * dataDimensions[1] * dataDimensions[2]; // compute byte length of data var dataLength = dataCount * imageData.GetScalarSize(); // copy data to managed buffer var pixelData = MemoryManager.Allocate <byte>(dataLength); Marshal.Copy(pData, pixelData, 0, dataLength); return(pixelData); }
private static byte[] SlabVtkImageData(vtkImageData slabData, Action <IntPtr, byte[], int, int, int, bool> slabAggregator, int bitsPerVoxel, bool signed) { // get the pointer to the image data var pData = slabData.GetScalarPointer(); if (pData.Equals(IntPtr.Zero)) { return(null); } // get number of subsamples and pixels per subsample var dataDimensions = slabData.GetDimensions(); var subsamplePixels = dataDimensions[0] * dataDimensions[1]; var subsamples = dataDimensions[2]; // compute byte length of slabbed output data var dataLength = subsamplePixels * slabData.GetScalarSize(); // slab data to managed buffer var pixelData = MemoryManager.Allocate <byte>(dataLength); slabAggregator.Invoke(pData, pixelData, subsamples, subsamplePixels, bitsPerVoxel / 8, signed); return(pixelData); }
private static byte[] MipPixelDataFromVtkSlab(vtkImageData slabImageData) { VtkHelper.StaticInitializationHack(); #if true // Do our own MIP, albeit slowly int[] sliceDimensions = slabImageData.GetDimensions(); int sliceDataSize = sliceDimensions[0] * sliceDimensions[1]; IntPtr slabDataPtr = slabImageData.GetScalarPointer(); byte[] pixelData = MemoryManager.Allocate<byte>(sliceDataSize * sizeof(short)); // Init with first slice Marshal.Copy(slabDataPtr, pixelData, 0, sliceDataSize * sizeof(short)); // Walk through other slices, finding maximum unsafe { short* psSlab = (short*) slabDataPtr; fixed (byte* pbFrame = pixelData) { short* psFrame = (short*)pbFrame; for (int sliceIndex = 1; sliceIndex < sliceDimensions[2]; sliceIndex++) { for (int i = 0; i < sliceDataSize-1; ++i) { int slabIndex = sliceIndex * sliceDataSize + i; if (psSlab[slabIndex] > psFrame[i]) psFrame[i] = psSlab[slabIndex]; } } } } return pixelData; #else // Ideally we'd use VTK to do the MIP (MinIP, Average...) vtkVolumeRayCastMIPFunction mip = new vtkVolumeRayCastMIPFunction(); vtkVolumeRayCastMapper mapper = new vtkVolumeRayCastMapper(); mapper.SetVolumeRayCastFunction(mip); mapper.SetInput(slabImageData); //TODO: Need to figure out how to use mapper to output vtkImageData vtkImageAlgorithm algo = new vtkImageAlgorithm(); algo.SetInput(mapper.GetOutputDataObject(0)); using (vtkExecutive exec = mapper.GetExecutive()) { VtkHelper.RegisterVtkErrorEvents(exec); exec.Update(); // Note: These report no output port, must have to do something else to get mapper to give us data //return exec.GetOutputData(0); return mapper.GetOutputDataObject(0); } #endif }
private static byte[] CreatePixelDataFromVtkSlice(vtkImageData sliceImageData) { int[] sliceDimensions = sliceImageData.GetDimensions(); int sliceDataSize = sliceDimensions[0]*sliceDimensions[1]; IntPtr sliceDataPtr = sliceImageData.GetScalarPointer(); if (sliceDataPtr.Equals(IntPtr.Zero)) return null; byte[] pixelData = MemoryManager.Allocate<byte>(sliceDataSize*sizeof (short)); Marshal.Copy(sliceDataPtr, pixelData, 0, sliceDataSize*sizeof (short)); return pixelData; }
private static byte[] SlabVtkImageData(vtkImageData slabData, Action<IntPtr, byte[], int, int, int, bool> slabAggregator, int bitsPerVoxel, bool signed) { // get the pointer to the image data var pData = slabData.GetScalarPointer(); if (pData.Equals(IntPtr.Zero)) return null; // get number of subsamples and pixels per subsample var dataDimensions = slabData.GetDimensions(); var subsamplePixels = dataDimensions[0]*dataDimensions[1]; var subsamples = dataDimensions[2]; // compute byte length of slabbed output data var dataLength = subsamplePixels*slabData.GetScalarSize(); // slab data to managed buffer var pixelData = MemoryManager.Allocate<byte>(dataLength); slabAggregator.Invoke(pData, pixelData, subsamples, subsamplePixels, bitsPerVoxel/8, signed); return pixelData; }
private static byte[] ReadVtkImageData(vtkImageData imageData) { // get the pointer to the image data var pData = imageData.GetScalarPointer(); if (pData.Equals(IntPtr.Zero)) return null; // get number of pixels in data var dataDimensions = imageData.GetDimensions(); var dataCount = dataDimensions[0]*dataDimensions[1]*dataDimensions[2]; // compute byte length of data var dataLength = dataCount*imageData.GetScalarSize(); // copy data to managed buffer var pixelData = MemoryManager.Allocate<byte>(dataLength); Marshal.Copy(pData, pixelData, 0, dataLength); return pixelData; }
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); }
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); }