public void WriteSimpleVTUExample() { string filePath = "C:\\DatenE\\02Studium\\02WiSe1718\\06IndividualProjekt\\03Daten\\testData\\simpleTest4Points.vtu"; vtkPoints points = vtkPoints.New(); points.InsertNextPoint(0, 0, 0); points.InsertNextPoint(1, 0, 0); points.InsertNextPoint(1, 1, 0); points.InsertNextPoint(0, 1, 1); points.InsertNextPoint(2, 0, 0); //points.InsertNextPoint(2, 2, 0); vtkTetra tetra = vtkTetra.New(); tetra.GetPointIds().SetId(0, 0); tetra.GetPointIds().SetId(1, 1); tetra.GetPointIds().SetId(2, 2); tetra.GetPointIds().SetId(3, 3); vtkCellArray cellArray = vtkCellArray.New(); cellArray.InsertNextCell(tetra); tetra.GetPointIds().SetId(0, 1); tetra.GetPointIds().SetId(1, 0); tetra.GetPointIds().SetId(2, 3); tetra.GetPointIds().SetId(3, 2); cellArray.InsertNextCell(tetra); tetra.GetPointIds().SetId(0, 0); tetra.GetPointIds().SetId(1, 2); tetra.GetPointIds().SetId(2, 3); tetra.GetPointIds().SetId(3, 1); cellArray.InsertNextCell(tetra); tetra.GetPointIds().SetId(0, 3); tetra.GetPointIds().SetId(1, 1); tetra.GetPointIds().SetId(2, 0); tetra.GetPointIds().SetId(3, 2); cellArray.InsertNextCell(tetra); tetra.GetPointIds().SetId(0, 3); tetra.GetPointIds().SetId(1, 0); tetra.GetPointIds().SetId(2, 2); tetra.GetPointIds().SetId(3, 4); cellArray.InsertNextCell(tetra); //tetra.GetPointIds().SetId(0, 4); //tetra.GetPointIds().SetId(1, 3); //tetra.GetPointIds().SetId(2, 0); //tetra.GetPointIds().SetId(3, 2); //cellArray.InsertNextCell(tetra); vtkUnstructuredGrid unstructuredGrid = vtkUnstructuredGrid.New(); unstructuredGrid.SetPoints(points); const int VTK_TETRA = 10; unstructuredGrid.SetCells(VTK_TETRA, cellArray); // vX vtkDoubleArray scalarsX = new vtkDoubleArray(); scalarsX.SetNumberOfValues(5); scalarsX.SetValue(0, 4); scalarsX.SetValue(1, 1); scalarsX.SetValue(2, 2); scalarsX.SetValue(3, 3); scalarsX.SetValue(4, 1); //scalarsX.SetValue(5, 2); scalarsX.SetName("Vx"); unstructuredGrid.GetPointData().AddArray(scalarsX); // vY vtkDoubleArray scalarsY = new vtkDoubleArray(); scalarsY.SetNumberOfValues(5); scalarsY.SetValue(0, 1); scalarsY.SetValue(1, 2); scalarsY.SetValue(2, 3); scalarsY.SetValue(3, 4); scalarsY.SetValue(4, 1); //scalarsY.SetValue(5, 2); scalarsY.SetName("Vy"); unstructuredGrid.GetPointData().AddArray(scalarsY); // vZ vtkDoubleArray scalarsZ = new vtkDoubleArray(); scalarsZ.SetNumberOfValues(5); scalarsZ.SetValue(0, 3); scalarsZ.SetValue(1, 1); scalarsZ.SetValue(2, 4); scalarsZ.SetValue(3, 2); scalarsZ.SetValue(4, 1); //scalarsZ.SetValue(5, 2); scalarsZ.SetName("Vz"); unstructuredGrid.GetPointData().AddArray(scalarsZ); // Write file vtkXMLUnstructuredGridWriter writer = vtkXMLUnstructuredGridWriter.New(); writer.SetFileName(filePath); writer.SetInput(unstructuredGrid); writer.Write(); }
protected virtual void PlotInternal(CodeContext context, ndarray array, IList <string> names = null, Type type = null) { VtkChartXyModel model = this.Model as VtkChartXyModel; if (model == null) { return; } int ndim = array.ndim; if (type == null || !type.IsSubclassOf(typeof(VtkPlotPointsModel))) { type = typeof(VtkPlotPointsModel); } ConstructorInfo cInfo = type.GetConstructor(new Type[] { }); if (cInfo == null) { return; } switch (ndim) { case 1: { // Treat array as y-values VtkPlotPointsModel plot = cInfo.Invoke(new object[] {}) as VtkPlotPointsModel; if (plot == null) { return; } int len = (int)array.Dims[0]; ndarray linspace = Math.General.LinSpace(context, len, 0.0, 1.0); vtkDoubleArray arrX = NumpyVtk.NumpyToVtk(linspace) as vtkDoubleArray; vtkDoubleArray arrY = NumpyVtk.NumpyToVtk(array) as vtkDoubleArray; if (arrX == null || arrY == null) { this.Log.Error("Plot(): Could not cast ndarrays to vtkDoubleArrays"); return; } numPlots++; arrX.SetName(DEFAULT_X_ARRAY + '_' + numPlots); if (names != null && !String.IsNullOrEmpty(names[0])) { arrY.SetName(names[0]); } else { numPlots++; arrY.SetName(DEFAULT_Y_ARRAY + '_' + numPlots); } this.SetXArray(arrX, plot); this.SetYArray(arrY, plot); model.Plots.Add(plot); } break; case 2: { // Treat 1st array as x-values, rest as y-values // No need to check for dimensional conformity, ndarray won't allow asymmetric arrays to be created ndarray arrayX = array[0] as ndarray; if (arrayX == null) { this.Log.Error("Plot(): Could not cast X-data to ndarray"); return; } vtkDoubleArray arrX = NumpyVtk.NumpyToVtk(arrayX) as vtkDoubleArray; if (arrX == null) { this.Log.Error("Plot(): Could not cast X-ndarray to vtkDoubleArray"); return; } if (names != null && !String.IsNullOrEmpty(names[0])) { arrX.SetName(names[0]); } else { numPlots++; arrX.SetName(DEFAULT_X_ARRAY + '_' + numPlots); } int numArrays = (int)array.Dims[0]; for (int i = 1; i < numArrays; i++) { VtkPlotPointsModel plot = cInfo.Invoke(new object[] { }) as VtkPlotPointsModel; if (plot == null) { return; } ndarray arrayY = array[i] as ndarray; if (arrayY == null) { this.Log.Error("Plot(): Could not cast Y-data to ndarray"); return; } vtkDoubleArray arrY = NumpyVtk.NumpyToVtk(arrayY) as vtkDoubleArray; if (arrY == null) { this.Log.Error("Plot(): Could not cast Y-ndarray to vtkDoubleArray"); return; } if (names != null && names.Count > i && !String.IsNullOrEmpty(names[i])) { arrY.SetName(names[i]); } else { numPlots++; arrY.SetName(DEFAULT_Y_ARRAY + '_' + numPlots); } this.SetXArray(arrX, plot); this.SetYArray(arrY, plot); model.Plots.Add(plot); } } break; default: this.Log.Error("Can only plot 1D and 2D arrays"); break; } vtkChartXY chart = this._chartItem as vtkChartXY; if (chart == null) { return; } foreach (IVtkPlotItemModel plotItemModel in model.Plots) { plotItemModel.PropertyChanged += OnPlotItemChanged; vtkPlot plot = plotItemModel.PlotItem as vtkPlot; if (plot != null) { chart.AddPlot(plot); } } this.RenderInternal(); }
private void WarpVector() { vtkPoints points = vtkPoints.New(); points.InsertNextPoint(0.0, 0.0, 0.0); points.InsertNextPoint(1.0, 0.0, 0.0); points.InsertNextPoint(2.0, 0.0, 0.0); points.InsertNextPoint(3.0, 0.0, 0.0); points.InsertNextPoint(4.0, 0.0, 0.0); vtkCellArray lines = vtkCellArray.New(); vtkLine line = vtkLine.New(); line.GetPointIds().SetId(0, 0); line.GetPointIds().SetId(1, 1); lines.InsertNextCell(line); line.GetPointIds().SetId(0, 1); line.GetPointIds().SetId(1, 2); lines.InsertNextCell(line); line.GetPointIds().SetId(0, 2); line.GetPointIds().SetId(1, 3); lines.InsertNextCell(line); line.GetPointIds().SetId(0, 3); line.GetPointIds().SetId(1, 4); lines.InsertNextCell(line); vtkDoubleArray warpData = vtkDoubleArray.New(); warpData.SetNumberOfComponents(3); warpData.SetName("warpData"); double[] warp = new double[] { 0.0, 0.0, 0.0 }; warp[1] = 0.0; warpData.InsertNextTuple3(warp[0], warp[1], warp[2]); warp[1] = 0.1; warpData.InsertNextTuple3(warp[0], warp[1], warp[2]); warp[1] = 0.3; warpData.InsertNextTuple3(warp[0], warp[1], warp[2]); warp[1] = 0.0; warpData.InsertNextTuple3(warp[0], warp[1], warp[2]); warp[1] = 0.1; warpData.InsertNextTuple3(warp[0], warp[1], warp[2]); vtkPolyData polydata = vtkPolyData.New(); polydata.SetPoints(points); polydata.SetLines(lines); polydata.GetPointData().AddArray(warpData); polydata.GetPointData().SetActiveVectors(warpData.GetName()); //WarpVector will use the array marked as active vector in polydata //it has to be a 3 component array //with the same number of tuples as points in polydata vtkWarpVector warpVector = vtkWarpVector.New(); #if VTK_MAJOR_VERSION_5 warpVector.SetInput(polydata); #else warpVector.SetInputData(polydata); #endif warpVector.Update(); vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); #if VTK_MAJOR_VERSION_5 mapper.SetInput(warpVector.GetPolyDataOutput()); #else mapper.SetInputData(warpVector.GetPolyDataOutput()); #endif vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); // 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); renderer.AddActor(actor); }