private void SelectPointClick(vtkObject sender, vtkObjectEventArgs e) { if (ModelLoaded == true && SelectionMode == true) { int[] clickPos = Inter.GetEventPosition(); vtkPointPicker PointPicker = vtkPointPicker.New(); PointPicker.SetTolerance(0.05); PointPicker.Pick(clickPos[0], clickPos[1], 0, Viewport); vtkPoints points = Faces.GetPoints(); double[] PickPosition = PointPicker.GetPickPosition(); for (int j = 0; j < points.GetNumberOfPoints(); j++) { if (Math.Abs(points.GetPoint(j)[0] - PickPosition[0]) < 1e-6 && Math.Abs(points.GetPoint(j)[1] - PickPosition[1]) < 1e-6 && Math.Abs(points.GetPoint(j)[2] - PickPosition[2]) < 1e-6) { SelectionPoints.InsertNextPoint(PickPosition[0], PickPosition[1], PickPosition[2]); break; } } SelectionGlyph = vtkGlyph3D.New(); SelectionGlyph.SetInput(SelectionPolyData); SelectionGlyph.SetSourceConnection(SelectionSphere.GetOutputPort()); SelectionMapper.SetInputConnection(SelectionGlyph.GetOutputPort()); SelectionActor.SetMapper(SelectionMapper); // Refresh Viewport Refresh(); } }
public static DimensionDescription FromPoints(vtkPoints points) { if (points == null) { return(null); } var bounds = points.GetBounds(); var numPoints = points.GetNumberOfPoints(); var result = new DimensionDescription { MinX = bounds[0], MaxX = bounds[1], MinY = bounds[2], MaxY = bounds[3], Coordinates = new double[numPoints, 4] }; var xIndices = new List <double>(); var yIndices = new List <double>(); for (var i = 0; i < numPoints; i++) { var current = points.GetPoint(i); if (!xIndices.Contains(current[0])) { xIndices.Add(current[0]); } if (!yIndices.Contains(current[1])) { yIndices.Add(current[1]); } } xIndices.Sort(); yIndices.Sort(); if (xIndices.Count > 0) { result.DistanceX = xIndices[1] - xIndices[0]; } if (yIndices.Count > 0) { result.DistanceY = yIndices[1] - yIndices[0]; } for (var i = 0; i < numPoints; i++) { var current = points.GetPoint(i); result.Coordinates[i, 0] = current[0]; result.Coordinates[i, 1] = current[1]; result.Coordinates[i, 2] = xIndices.IndexOf(current[0]); result.Coordinates[i, 3] = yIndices.IndexOf(current[1]); } return(result); }
public void Read_MultiblockData_File(string filename) { vtkXMLMultiBlockDataReader reader = new vtkXMLMultiBlockDataReader(); reader.SetFileName(filename); reader.Update(); vtkMultiBlockDataSet data = (vtkMultiBlockDataSet)reader.GetOutput(); vtkInformation vtkInformation = data.GetMetaData(0); uint numBlocks = data.GetNumberOfBlocks(); if (numBlocks == 1) { vtkStructuredGrid structuredGrid = (vtkStructuredGrid)data.GetBlock(0); dimensions = structuredGrid.GetDimensions(); vtkPoints points = structuredGrid.GetPoints(); int numPoints = (int)structuredGrid.GetNumberOfPoints(); List <Vector3d> point_dat = new List <Vector3d>(); if (numPoints != 0) { // Read Point Data double[] pt; for (int i = 0; i < numPoints; i++) { pt = points.GetPoint(i); point_dat.Add(new Vector3d((float)pt[0], (float)pt[1], (float)pt[2])); } if (this.vertex_data.ContainsKey("vertices")) { this.vertex_data["vertices"] = point_dat; } else { this.vertex_data.Add("vertices", point_dat); } Console.WriteLine("All points read in correctly!"); } vtkPointData scalarValues = (vtkPointData)data.GetBlock(0).GetAttributes(0); // Load point attributes this.Load_Point_Attributes(scalarValues); } }
/// <summary> /// 将点做映射,按照xoz面来影射点,为2维的弹体做准备 /// </summary> /// <param name="points"></param> /// <returns></returns> private static vtkPoints ReflectPointsByXOZ(vtkPoints points) { vtkPoints tempPoints = vtkPoints.New(); int numPoints = points.GetNumberOfPoints(); //点数扩大两倍 tempPoints.SetNumberOfPoints(numPoints * 2); //构造相应的点 for (int i = 0; i < numPoints; i++) { double[] temp = points.GetPoint(i); tempPoints.SetPoint(numPoints * 2 - 1 - i, temp[0] * (-1), temp[1], temp[2]);//x坐标变为相反数 tempPoints.SetPoint(i, temp[0], temp[1], temp[2]); } return(tempPoints); }
/// <summary> /// 左键释放时获取选择的对象 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void m_interactor_LeftButtonReleaseEvt(vtkObject sender, vtkObjectEventArgs e) { if (m_pickTarget == ePickTarget.None) { return; } //if (m_iren.GetControlKey() != 0) // m_pickBoolean = ePickBollean.Add; //else if (m_iren.GetShiftKey() != 0) // m_pickBoolean = ePickBollean.Sub; m_iren.GetEventPosition(ref endx, ref endy); #region 点选对象 if (m_pickMode == ePickMode.DotPickMode) //如果是点选模式 { vtkCellPicker cellPicker = new vtkCellPicker(); cellPicker.SetTolerance(0.005); if (0 == cellPicker.Pick(endx, endy, 0, m_renderer)) { return; //什么都没有选到就返回 } FEMActor actor = cellPicker.GetActor() as FEMActor; if (actor == null) { return; } if (m_pickTarget == ePickTarget.Actor) { //如果选择对象为 actor.IsSelected = true; m_RWCtrl.Invalidate(); //貌似必须要用这个函数,update 不行 OnObjectsSelected(sender, e); } else if (m_pickTarget == ePickTarget.Cell) { int cellId = cellPicker.GetCellId(); actor.AddCellId(cellId, m_pickBoolean); OnObjectsSelected(sender, e); //if (cellId > 0) // Console.WriteLine("dotselect cellid:{0}", cellId); } else if (m_pickTarget == ePickTarget.Point) { int pointId = cellPicker.GetPointId(); if (pointId != -1) { actor.AddPointId(pointId, m_pickBoolean); OnObjectsSelected(sender, e); // Console.WriteLine("dotselect pointid:{0}", pointId); } } } #endregion else if (m_pickMode == ePickMode.RectPickMode) //如果是框选 { vtkAreaPicker areaPicker = new vtkAreaPicker(); if (0 == areaPicker.AreaPick(startx, starty, endx, endy, m_renderer)) { //如果什么也没选到就返回 //this.impactViewForm.ClearAllSelectCells(); return; } vtkProp3DCollection selectedActors = areaPicker.GetProp3Ds(); selectedActors.InitTraversal(); FEMActor selectedActor; while ((selectedActor = selectedActors.GetNextProp3D() as FEMActor) != null) { vtkSelection selection = new vtkSelection(); vtkSelectionNode selectionNode = new vtkSelectionNode(); selection.AddNode(selectionNode); vtkIdFilter idFilter = new vtkIdFilter(); idFilter.FieldDataOn(); idFilter.SetInput(selectedActor.GetMapper().GetInput()); idFilter.Update(); vtkExtractSelection frustum = new vtkExtractSelection(); frustum.SetInput(0, idFilter.GetOutput()); frustum.SetInput(1, selection); selectionNode.Initialize(); selectionNode.SetContentType((int)vtkSelectionNode.SelectionContent.FRUSTUM); if (m_pickTarget == ePickTarget.Cell)//如果选择的目标是cell { selectionNode.SetFieldType((int)vtkSelectionNode.SelectionField.CELL); } else if (m_pickTarget == ePickTarget.Point) //如果选择的目标是point { selectionNode.SetFieldType((int)vtkSelectionNode.SelectionField.POINT); } vtkPoints points = areaPicker.GetClipPoints(); vtkDoubleArray frustumcorners = new vtkDoubleArray(); frustumcorners.SetNumberOfComponents(4); frustumcorners.SetNumberOfTuples(8); for (int i = 0; i < 8; ++i) { double[] point = points.GetPoint(i); frustumcorners.SetTuple4(i, point[0], point[1], point[2], 0.0); } selectionNode.SetSelectionList(frustumcorners); frustum.Update(); vtkUnstructuredGrid grid = new vtkUnstructuredGrid(); grid = vtkUnstructuredGrid.SafeDownCast(frustum.GetOutput()); vtkIdTypeArray ids = null; if (m_pickTarget == ePickTarget.Cell) { ids = vtkIdTypeArray.SafeDownCast(grid.GetCellData().GetArray("vtkOriginalCellIds")); if (ids == null) { continue; } selectedActor.AddCellId(ids, m_pickBoolean); OnObjectsSelected(sender, e); } else if (m_pickTarget == ePickTarget.Point) { ids = vtkIdTypeArray.SafeDownCast(grid.GetPointData().GetArray("vtkOriginalPointIds")); if (ids == null) { continue; } selectedActor.AddPointId(ids, m_pickBoolean); OnObjectsSelected(sender, e); } } } }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVSelectionLoop(String [] argv) { //Prefix Content is: "" //[] // Demonstrate the use of implicit selection loop as well as closest point[] // connectivity[] //[] // create pipeline[] //[] sphere = new vtkSphereSource(); sphere.SetRadius((double)1); sphere.SetPhiResolution((int)100); sphere.SetThetaResolution((int)100); selectionPoints = new vtkPoints(); selectionPoints.InsertPoint((int)0,(double)0.07325,(double)0.8417,(double)0.5612); selectionPoints.InsertPoint((int)1,(double)0.07244,(double)0.6568,(double)0.7450); selectionPoints.InsertPoint((int)2,(double)0.1727,(double)0.4597,(double)0.8850); selectionPoints.InsertPoint((int)3,(double)0.3265,(double)0.6054,(double)0.7309); selectionPoints.InsertPoint((int)4,(double)0.5722,(double)0.5848,(double)0.5927); selectionPoints.InsertPoint((int)5,(double)0.4305,(double)0.8138,(double)0.4189); loop = new vtkImplicitSelectionLoop(); loop.SetLoop((vtkPoints)selectionPoints); extract = new vtkExtractGeometry(); extract.SetInputConnection((vtkAlgorithmOutput)sphere.GetOutputPort()); extract.SetImplicitFunction((vtkImplicitFunction)loop); connect = new vtkConnectivityFilter(); connect.SetInputConnection((vtkAlgorithmOutput)extract.GetOutputPort()); connect.SetExtractionModeToClosestPointRegion(); connect.SetClosestPoint((double)selectionPoints.GetPoint((int)0)[0], (double)selectionPoints.GetPoint((int)0)[1],(double)selectionPoints.GetPoint((int)0)[2]); clipMapper = new vtkDataSetMapper(); clipMapper.SetInputConnection((vtkAlgorithmOutput)connect.GetOutputPort()); backProp = new vtkProperty(); backProp.SetDiffuseColor((double) 1.0000, 0.3882, 0.2784 ); clipActor = new vtkActor(); clipActor.SetMapper((vtkMapper)clipMapper); clipActor.GetProperty().SetColor((double) 0.2000, 0.6300, 0.7900 ); clipActor.SetBackfaceProperty((vtkProperty)backProp); // Create graphics stuff[] //[] ren1 = vtkRenderer.New(); renWin = vtkRenderWindow.New(); renWin.AddRenderer((vtkRenderer)ren1); iren = new vtkRenderWindowInteractor(); iren.SetRenderWindow((vtkRenderWindow)renWin); // Add the actors to the renderer, set the background and size[] //[] ren1.AddActor((vtkProp)clipActor); ren1.SetBackground((double)1,(double)1,(double)1); ren1.ResetCamera(); ren1.GetActiveCamera().Azimuth((double)30); ren1.GetActiveCamera().Elevation((double)30); ren1.GetActiveCamera().Dolly((double)1.2); ren1.ResetCameraClippingRange(); renWin.SetSize((int)400,(int)400); renWin.Render(); // render the image[] //[] // prevent the tk window from showing up then start the event loop[] //deleteAllVTKObjects(); }
/// <summary> /// The main entry method called by the CSharp driver /// </summary> /// <param name="argv"></param> public static void AVSelectionLoop(String [] argv) { //Prefix Content is: "" //[] // Demonstrate the use of implicit selection loop as well as closest point[] // connectivity[] //[] // create pipeline[] //[] sphere = new vtkSphereSource(); sphere.SetRadius((double)1); sphere.SetPhiResolution((int)100); sphere.SetThetaResolution((int)100); selectionPoints = new vtkPoints(); selectionPoints.InsertPoint((int)0, (double)0.07325, (double)0.8417, (double)0.5612); selectionPoints.InsertPoint((int)1, (double)0.07244, (double)0.6568, (double)0.7450); selectionPoints.InsertPoint((int)2, (double)0.1727, (double)0.4597, (double)0.8850); selectionPoints.InsertPoint((int)3, (double)0.3265, (double)0.6054, (double)0.7309); selectionPoints.InsertPoint((int)4, (double)0.5722, (double)0.5848, (double)0.5927); selectionPoints.InsertPoint((int)5, (double)0.4305, (double)0.8138, (double)0.4189); loop = new vtkImplicitSelectionLoop(); loop.SetLoop((vtkPoints)selectionPoints); extract = new vtkExtractGeometry(); extract.SetInputConnection((vtkAlgorithmOutput)sphere.GetOutputPort()); extract.SetImplicitFunction((vtkImplicitFunction)loop); connect = new vtkConnectivityFilter(); connect.SetInputConnection((vtkAlgorithmOutput)extract.GetOutputPort()); connect.SetExtractionModeToClosestPointRegion(); connect.SetClosestPoint((double)selectionPoints.GetPoint((int)0)[0], (double)selectionPoints.GetPoint((int)0)[1], (double)selectionPoints.GetPoint((int)0)[2]); clipMapper = new vtkDataSetMapper(); clipMapper.SetInputConnection((vtkAlgorithmOutput)connect.GetOutputPort()); backProp = new vtkProperty(); backProp.SetDiffuseColor((double)1.0000, 0.3882, 0.2784); clipActor = new vtkActor(); clipActor.SetMapper((vtkMapper)clipMapper); clipActor.GetProperty().SetColor((double)0.2000, 0.6300, 0.7900); clipActor.SetBackfaceProperty((vtkProperty)backProp); // Create graphics stuff[] //[] ren1 = vtkRenderer.New(); renWin = vtkRenderWindow.New(); renWin.AddRenderer((vtkRenderer)ren1); iren = new vtkRenderWindowInteractor(); iren.SetRenderWindow((vtkRenderWindow)renWin); // Add the actors to the renderer, set the background and size[] //[] ren1.AddActor((vtkProp)clipActor); ren1.SetBackground((double)1, (double)1, (double)1); ren1.ResetCamera(); ren1.GetActiveCamera().Azimuth((double)30); ren1.GetActiveCamera().Elevation((double)30); ren1.GetActiveCamera().Dolly((double)1.2); ren1.ResetCameraClippingRange(); renWin.SetSize((int)400, (int)400); renWin.Render(); // render the image[] //[] // prevent the tk window from showing up then start the event loop[] //deleteAllVTKObjects(); }
public void Read_Unstructured_Grid_File(string filename) { // Initalize VTK Reader vtkXMLUnstructuredGridReader reader = new vtkXMLUnstructuredGridReader(); reader.SetFileName(filename); reader.Update(); vtkUnstructuredGrid grid = reader.GetOutput(); vtkCellArray data = grid.GetCells(); vtkIdList idList = vtkIdList.New(); int numCells = (int)data.GetNumberOfCells(); if (numCells != 0) { if (grid.GetCellType(0) == 10) { isTetra = true; Console.WriteLine("Celltype is tetra"); } else if (grid.GetCellType(0) == 5) { isTetra = false; Console.WriteLine("Celltype is triangle"); } else { Console.WriteLine("No valid celltype"); } for (int i = 0; i < numCells; i++) { long cellTypeID = grid.GetCellType(i); // alle punkte durchlaufen und in eine Variable für jeden Punkt die anderen drei Punkte speichern if (isTetra) { Vector4 tetraPoint = new Vector4(); grid.GetCellPoints(i, idList); // ueber alle vier punkte iterieren und diese in extra liste für jeden Punkt speichern for (int j = 0; j < 4; j++) { tetraPoint[j] = idList.GetId(j); } tetraPoints.Add(tetraPoint); } else if (!isTetra) { Vector3 triangle = new Vector3(); grid.GetCellPoints(i, idList); // ueber alle drei punkte iterieren und diese in extra liste für jeden Punkt speichern for (int j = 0; j < 3; j++) { triangle[j] = idList.GetId(j); } trianglePoints.Add(triangle); } } } // Read Point Coordinates vtkPoints points = grid.GetPoints(); int numPoints = (int)points.GetNumberOfPoints(); List <Vector3d> point_dat = new List <Vector3d>(); if (numPoints != 0) { // Read Point Data double[] pt; for (int i = 0; i < numPoints; i++) { pt = points.GetPoint(i); point_dat.Add(new Vector3d((float)pt[0], (float)pt[1], (float)pt[2])); } if (this.vertex_data.ContainsKey("vertices")) { this.vertex_data["vertices"] = point_dat; } else { this.vertex_data.Add("vertices", point_dat); } Console.WriteLine("All points read in correctly!"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("---------------No Points existent"); } vtkPointData pointData = grid.GetPointData(); // Load point attributes this.Load_Point_Attributes(pointData); }
private void RenderXYZColor() { FileStream fs = null; StreamReader sr = null; String sLineBuffer; String[] sXYZ; char[] chDelimiter = new char[] { ' ', '\t', ';' }; double[] xyz = new double[3]; double[] rgb = new double[3]; vtkPoints points = vtkPoints.New(); vtkPoints colors = vtkPoints.New(); int cnt = 0; try { // in case file must be open in another application too use "FileShare.ReadWrite" fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); sr = new StreamReader(fs); vtkDoubleArray colorScalor = new vtkDoubleArray(); int n = 1; while (!sr.EndOfStream) { sLineBuffer = sr.ReadLine(); cnt++; sXYZ = sLineBuffer.Split(chDelimiter, StringSplitOptions.RemoveEmptyEntries); if (sXYZ == null || sXYZ.Length != 6) { MessageBox.Show("data seems to be in wrong format at line " + cnt, "Format Exception", MessageBoxButtons.OK); return; } xyz[0] = double.Parse(sXYZ[0], CultureInfo.InvariantCulture) * 11100; xyz[1] = double.Parse(sXYZ[1], CultureInfo.InvariantCulture) * 11100; xyz[2] = double.Parse(sXYZ[2], CultureInfo.InvariantCulture); rgb[0] = double.Parse(sXYZ[0], CultureInfo.InvariantCulture); rgb[1] = double.Parse(sXYZ[1], CultureInfo.InvariantCulture); rgb[2] = double.Parse(sXYZ[2], CultureInfo.InvariantCulture); points.InsertNextPoint(xyz[0], xyz[1], xyz[2]); colors.InsertNextPoint(rgb[0], rgb[1], rgb[2]); colorScalor.InsertNextTuple1(n++); } vtkPolyData polydata = vtkPolyData.New(); polydata.SetPoints(points); polydata.GetPointData().SetScalars(colorScalor); //设置点的Scalar(标量)属性 vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New(); glyphFilter.SetInputConnection(polydata.GetProducerPort()); vtkLookupTable lookupTable = new vtkLookupTable(); lookupTable.SetNumberOfColors(n); // SetSetTableValue(vtkIdType indx, double r, double g, double b, double a); Random random = new Random(); for (int i = 0; i < n; i++) { double[] tmp = colors.GetPoint(i); double r = tmp[0]; double g = tmp[1]; double b = tmp[2]; lookupTable.SetTableValue(i, r, g, b, 1); } // Visualize vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection(glyphFilter.GetOutputPort()); mapper.SetLookupTable(lookupTable); mapper.SetScalarRange(1, n); vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); actor.GetProperty().SetPointSize(1); //actor.GetProperty().SetColor(1, 0.5, 0); // add our actor to the renderer m_Renderer.AddActor(actor); imgPropList.Add(actor); m_Renderer.ResetCamera(); //Rerender the screen m_RenderWindow.Render(); m_Renderer.Render(); } catch (IOException ex) { MessageBox.Show(ex.Message, "IOException", MessageBoxButtons.OK); } finally { if (sr != null) { sr.Close(); sr.Dispose(); sr = null; } } }