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(); }
public static void GetRectData(vtkPolyData data, Rectangle rect) { var points = vtkPoints.New(); var colorArray = vtkUnsignedCharArray.New(); colorArray.SetNumberOfComponents(3); RectImageData = vtkImageData.New(); RectImageData.SetExtent(0, rect.Size.Width - 1, 0, rect.Size.Height - 1, 0, 0); RectImageData.SetNumberOfScalarComponents(1); RectImageData.SetScalarTypeToUnsignedChar(); for (var i = rect.Top; i < rect.Bottom; i++) { for (var j = rect.Left; j < rect.Right; j++) { double[] p = data.GetPoint(i * ImageWidth + j); points.InsertNextPoint(j - rect.Left, i - rect.Top, p[2]); double[] c = data.GetPointData().GetScalars().GetTuple3(i * ImageWidth + j); colorArray.InsertNextTuple3(c[0], c[1], c[2]); RectImageData.SetScalarComponentFromDouble(j - rect.Left, i - rect.Top, 0, 0, c[0]); } } RectPolyData = vtkPolyData.New(); RectPolyData.SetPoints(points); RectPolyData.GetPointData().SetScalars(colorArray); RectPolyData.Update(); }
/// <summary> /// Converts 3D byte array to vtkImageData. /// </summary> /// <param name="data">Input array.</param> /// <param name="dims">Input array dimensions. Give the begin and end extent for each dimension.</param> /// <returns>Converted array.</returns> public static vtkImageData byteToVTK1D(byte[] data, int[] dims) { int h = dims[1] - dims[0] + 1; int w = dims[3] - dims[2] + 1; int d = dims[5] - dims[4] + 1; //Create VTK data for putput vtkImageData vtkdata = vtkImageData.New(); //Character array for conversion vtkUnsignedCharArray charArray = vtkUnsignedCharArray.New(); //Pin byte array GCHandle pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned); //Set character array input charArray.SetArray(pinnedArray.AddrOfPinnedObject(), h * w * d, 1); //Set vtkdata properties and connect array //Data from char array vtkdata.GetPointData().SetScalars(charArray); //Number of scalars/pixel vtkdata.SetNumberOfScalarComponents(1); //Set data extent vtkdata.SetExtent(dims[0], dims[1], dims[2], dims[3], dims[4], dims[5]); //Scalar type vtkdata.SetScalarTypeToUnsignedChar(); vtkdata.Update(); return(vtkdata); }
public static void ReadDataFromFile(string filename) { try { SimplePointFile.GetImageInfo(filename); ImageWidth = SimplePointFile.ImageWidth; ImageHeight = SimplePointFile.ImageHeight; //Console.WriteLine("ImageWidth:" + ImageWidth); //Console.WriteLine("ImageHeight:" + ImageHeight); var points = vtkPoints.New(); var colorArray = vtkUnsignedCharArray.New(); colorArray.SetNumberOfComponents(3); ImageData = vtkImageData.New(); ImageData.SetExtent(0, ImageWidth - 1, 0, ImageHeight - 1, 0, 0); ImageData.SetNumberOfScalarComponents(1); ImageData.SetScalarTypeToUnsignedChar(); SimplePointFile.OpenReadFile(filename); double[] data; while ((data = SimplePointFile.ReadLine()) != null) { points.InsertNextPoint(data[0], data[1], data[2]); colorArray.InsertNextTuple3(data[3], data[4], data[5]); ImageData.SetScalarComponentFromDouble((int)data[0], (int)data[1], 0, 0, data[3]); } PolyData = vtkPolyData.New(); PolyData.SetPoints(points); PolyData.GetPointData().SetScalars(colorArray); PolyData.Update(); //Console.WriteLine("PolyData & ImageData Load."); } catch (Exception e) { MessageBox.Show(e.Message); } finally { SimplePointFile.CloseReadFile(); } }
/// <summary> /// Create scalar copy of vtkImageData. /// </summary> /// <param name="data">Input data.</param> /// <returns>Copied data.</returns> public static vtkImageData scalarCopy(vtkImageData data) { /*DEPRECATED!!*/ //Get data extent int[] dims = data.GetExtent(); vtkImageData newdata = vtkImageData.New(); newdata.SetExtent(dims[0], dims[1], dims[2], dims[3], dims[4], dims[5]); for (int h = dims[0]; h <= dims[1]; h++) { for (int w = dims[2]; w <= dims[3]; w++) { for (int d = dims[4]; d <= dims[5]; d++) { double scalar = data.GetScalarComponentAsDouble(h, w, d, 0); newdata.SetScalarComponentFromDouble(h, w, d, 0, scalar); } } } return(newdata); }
/// <summary> /// Converts 3D byte array to vtkImageData. /// </summary> /// <param name="data">Input array.</param> /// <param name="orientation">Data orientation as a list of axes (0-2)</param> /// <returns>Converted array.</returns> public static vtkImageData byteToVTK(byte[,,] data, int[] orientation = null) { //Get input dimensions int[] dims = new int[] { data.GetLength(0), data.GetLength(1), data.GetLength(2) }; //Create VTK data for putput vtkImageData vtkdata = vtkImageData.New(); //Character array for conversion vtkUnsignedCharArray charArray = vtkUnsignedCharArray.New(); //Pin byte array GCHandle pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned); //Set character array input charArray.SetArray(pinnedArray.AddrOfPinnedObject(), dims[0] * dims[1] * dims[2], 1); //Set vtkdata properties and connect array //Data from char array vtkdata.GetPointData().SetScalars(charArray); //Number of scalars/pixel vtkdata.SetNumberOfScalarComponents(1); //Data extent, 1st and last axis are swapped from the char array //Data is converted back to original orientation vtkdata.SetExtent(0, dims[2] - 1, 0, dims[1] - 1, 0, dims[0] - 1); //Scalar type vtkdata.SetScalarTypeToUnsignedChar(); vtkdata.SetSpacing(1.0, 1.0, 1.0); vtkdata.Update(); pinnedArray.Free(); //Return vtk data if (orientation == null) { return(vtkdata); } else { vtkImagePermute permuter = vtkImagePermute.New(); permuter.SetInput(vtkdata); permuter.SetFilteredAxes(orientation[0], orientation[1], orientation[2]); permuter.Update(); return(permuter.GetOutput()); } }
/// <summary> /// Calculates mean and standard deviation images from volume-of-interest. Obsolete. /// </summary> /// <param name="output"></param> /// <param name="mu"></param> /// <param name="std"></param> /// <param name="input"></param> /// <param name="depth"></param> /// <param name="threshold"></param> public static void get_voi_mu_std(out vtkImageData output, out double[,] mu, out double[,] std, vtkImageData input, int depth, double threshold = 70.0) { //Get data extent int[] dims = input.GetExtent(); int h = dims[1] - dims[0] + 1; int w = dims[3] - dims[2] + 1; int d = dims[5] - dims[4] + 1; //Compute strides int stridew = 1; int strideh = w; int strided = h * w; byte[] bytedata = DataTypes.vtkToByte(input); byte[] voidata = new byte[bytedata.Length]; double[,] _mu = new double[h, w]; double[,] _std = new double[h, w]; //Get voi indices Parallel.For(24, h - 24, (int y) => { Parallel.For(24, w - 24, (int x) => { int start = d - 1; int stop = 0; //Compute mean for (int z = d - 1; z > 0; z -= 1) { int pos = z * strided + y * strideh + x * stridew; double val = (double)bytedata[pos]; if (val >= threshold) { start = z; stop = Math.Max(z - depth, 0); //Compute mean and std for (int zz = start; zz > stop; zz -= 1) { int newpos = zz * strided + y * strideh + x * stridew; double newval = (double)bytedata[newpos]; voidata[newpos] = (byte)newval; _mu[y, x] = newval / (double)depth; } for (int zz = start; zz > stop; zz -= 1) { int newpos = zz * strided + y * strideh + x * stridew; double newval = (double)bytedata[newpos]; _std[y, x] += ((double)newval - _mu[y, x]) * ((double)newval - _mu[y, x]); } _std[y, x] = Math.Pow(_std[y, x] / (depth - 1), 0.5); break; } } }); }); mu = _mu; std = _std; output = vtkImageData.New(); //Copy voi data to input array vtkUnsignedCharArray charArray = vtkUnsignedCharArray.New(); //Pin byte array GCHandle pinnedArray = GCHandle.Alloc(voidata, GCHandleType.Pinned); //Set character array input charArray.SetArray(pinnedArray.AddrOfPinnedObject(), h * w * d, 1); //Set vtkdata properties and connect array //Data from char array output.GetPointData().SetScalars(charArray); //Number of scalars/pixel output.SetNumberOfScalarComponents(1); //Data extent, 1st and last axis are swapped from the char array //Data is converted back to original orientation output.SetExtent(dims[0], dims[1], dims[2], dims[3], dims[4], dims[5]); //Scalar type output.SetScalarTypeToUnsignedChar(); output.Update(); }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVTestCutMaterial(String [] argv) { //Prefix Content is: "" // Lets create a data set.[] data = new vtkImageData(); data.SetExtent((int)0,(int)31,(int)0,(int)31,(int)0,(int)31); data.SetScalarTypeToFloat(); // First the data array:[] gauss = new vtkImageGaussianSource(); gauss.SetWholeExtent((int)0,(int)30,(int)0,(int)30,(int)0,(int)30); gauss.SetCenter((double)18,(double)12,(double)20); gauss.SetMaximum((double)1.0); gauss.SetStandardDeviation((double)10.0); gauss.Update(); a = gauss.GetOutput().GetPointData().GetScalars(); a.SetName((string)"Gauss"); data.GetCellData().SetScalars((vtkDataArray)a); //skipping Delete gauss // Now the material array:[] ellipse = new vtkImageEllipsoidSource(); ellipse.SetWholeExtent((int)0,(int)30,(int)0,(int)30,(int)0,(int)30); ellipse.SetCenter((double)11,(double)12,(double)13); ellipse.SetRadius((double)5,(double)9,(double)13); ellipse.SetInValue((double)1); ellipse.SetOutValue((double)0); ellipse.SetOutputScalarTypeToInt(); ellipse.Update(); m = ellipse.GetOutput().GetPointData().GetScalars(); m.SetName((string)"Material"); data.GetCellData().AddArray((vtkAbstractArray)m); //skipping Delete ellipse cut = new vtkCutMaterial(); cut.SetInput((vtkDataObject)data); cut.SetMaterialArrayName((string)"Material"); cut.SetMaterial((int)1); cut.SetArrayName((string)"Gauss"); cut.SetUpVector((double)1,(double)0,(double)0); cut.Update(); mapper2 = vtkPolyDataMapper.New(); mapper2.SetInputConnection((vtkAlgorithmOutput)cut.GetOutputPort()); mapper2.SetScalarRange((double)0,(double)1); //apper2 SetScalarModeToUseCellFieldData[] //apper2 SetColorModeToMapScalars [] //apper2 ColorByArrayComponent "vtkGhostLevels" 0[] actor2 = new vtkActor(); actor2.SetMapper((vtkMapper)mapper2); actor2.SetPosition((double)1.5,(double)0,(double)0); ren = vtkRenderer.New(); ren.AddActor((vtkProp)actor2); renWin = vtkRenderWindow.New(); renWin.AddRenderer((vtkRenderer)ren); p = cut.GetCenterPoint(); n = cut.GetNormal(); cam = ren.GetActiveCamera(); cam.SetFocalPoint(p[0],p[1],p[2]); cam.SetViewUp(cut.GetUpVector()[0], cut.GetUpVector()[1], cut.GetUpVector()[2]); cam.SetPosition( (double)(lindex(n,0))+(double)(lindex(p,0)), (double)(lindex(n,1))+(double)(lindex(p,1)), (double)(lindex(n,2))+(double)(lindex(p,2))); ren.ResetCamera(); iren = vtkRenderWindowInteractor.New(); iren.SetRenderWindow(renWin); iren.Initialize(); //deleteAllVTKObjects(); }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVTestCutMaterial(String [] argv) { //Prefix Content is: "" // Lets create a data set.[] data = new vtkImageData(); data.SetExtent((int)0, (int)31, (int)0, (int)31, (int)0, (int)31); data.SetScalarTypeToFloat(); // First the data array:[] gauss = new vtkImageGaussianSource(); gauss.SetWholeExtent((int)0, (int)30, (int)0, (int)30, (int)0, (int)30); gauss.SetCenter((double)18, (double)12, (double)20); gauss.SetMaximum((double)1.0); gauss.SetStandardDeviation((double)10.0); gauss.Update(); a = gauss.GetOutput().GetPointData().GetScalars(); a.SetName((string)"Gauss"); data.GetCellData().SetScalars((vtkDataArray)a); //skipping Delete gauss // Now the material array:[] ellipse = new vtkImageEllipsoidSource(); ellipse.SetWholeExtent((int)0, (int)30, (int)0, (int)30, (int)0, (int)30); ellipse.SetCenter((double)11, (double)12, (double)13); ellipse.SetRadius((double)5, (double)9, (double)13); ellipse.SetInValue((double)1); ellipse.SetOutValue((double)0); ellipse.SetOutputScalarTypeToInt(); ellipse.Update(); m = ellipse.GetOutput().GetPointData().GetScalars(); m.SetName((string)"Material"); data.GetCellData().AddArray((vtkAbstractArray)m); //skipping Delete ellipse cut = new vtkCutMaterial(); cut.SetInput((vtkDataObject)data); cut.SetMaterialArrayName((string)"Material"); cut.SetMaterial((int)1); cut.SetArrayName((string)"Gauss"); cut.SetUpVector((double)1, (double)0, (double)0); cut.Update(); mapper2 = vtkPolyDataMapper.New(); mapper2.SetInputConnection((vtkAlgorithmOutput)cut.GetOutputPort()); mapper2.SetScalarRange((double)0, (double)1); //apper2 SetScalarModeToUseCellFieldData[] //apper2 SetColorModeToMapScalars [] //apper2 ColorByArrayComponent "vtkGhostLevels" 0[] actor2 = new vtkActor(); actor2.SetMapper((vtkMapper)mapper2); actor2.SetPosition((double)1.5, (double)0, (double)0); ren = vtkRenderer.New(); ren.AddActor((vtkProp)actor2); renWin = vtkRenderWindow.New(); renWin.AddRenderer((vtkRenderer)ren); p = cut.GetCenterPoint(); n = cut.GetNormal(); cam = ren.GetActiveCamera(); cam.SetFocalPoint(p[0], p[1], p[2]); cam.SetViewUp(cut.GetUpVector()[0], cut.GetUpVector()[1], cut.GetUpVector()[2]); cam.SetPosition( (double)(lindex(n, 0)) + (double)(lindex(p, 0)), (double)(lindex(n, 1)) + (double)(lindex(p, 1)), (double)(lindex(n, 2)) + (double)(lindex(p, 2))); ren.ResetCamera(); iren = vtkRenderWindowInteractor.New(); iren.SetRenderWindow(renWin); iren.Initialize(); //deleteAllVTKObjects(); }