private void Frustum() { // Create a frustum. // in this example we need the renderer first to retrieve the active camera // in order to get camera's frustum planes vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer(); vtkCamera camera = renderer.GetActiveCamera(); double[] aspect = renderer.GetAspect(); double aspectRatio = aspect[0] / aspect[1]; // allocate memory for 24 unmanaged doubles int size = Marshal.SizeOf(typeof(double)) * 24; IntPtr ptr = Marshal.AllocHGlobal(size); camera.GetFrustumPlanes(aspectRatio, ptr); // in case we would need this values directly we could copy // the unmanaged double array to a managed array like so: // double[] planesArray = new double[24]; // Marshal.Copy(ptr, planesArray, 0, 24); // but fortunately we can forward the IntPtr directly to the function // SetFrustumPlanes() vtkPlanes planes = vtkPlanes.New(); planes.SetFrustumPlanes(ptr); // free unmanaged memory Marshal.FreeHGlobal(ptr); vtkFrustumSource frustumSource = vtkFrustumSource.New(); frustumSource.SetPlanes(planes); frustumSource.Update(); vtkPolyData frustum = frustumSource.GetOutput(); // Visualize vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInput(frustum); vtkActor actor = vtkActor.New(); actor.SetMapper(mapper); renderer.SetBackground(.2, .1, .3); // Background color dark purple renderer.AddActor(actor); renderer.ResetCamera(); }
private void Planes() { // in this example we need the renderer first to retrieve the active camera // in order to get camera's frustum planes and renderer's aspectratio vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow; vtkRenderer renderer = vtkRenderer.New(); renderer = renderWindow.GetRenderers().GetFirstRenderer(); vtkCamera camera = renderer.GetActiveCamera(); double[] aspect = renderer.GetAspect(); double aspectRatio = aspect[0] / aspect[1]; vtkPlanes planes = vtkPlanes.New(); // one way { // allocate memory for 24 unmanaged doubles int size = Marshal.SizeOf(typeof(double)) * 24; IntPtr ptr = Marshal.AllocHGlobal(size); camera.GetFrustumPlanes(aspectRatio, ptr); // in case we would need this values diectly we could copy // the unmanaged double array to a managed array like so: // double[] planesArray = new double[24]; // Marshal.Copy(ptr, planesArray, 0, 24); // but fortunately we can forward the IntPtr directly to the function // SetFrustumPlanes() planes.SetFrustumPlanes(ptr); // free unmanaged memory Marshal.FreeHGlobal(ptr); } // another way { vtkSphereSource sphereSource = vtkSphereSource.New(); sphereSource.Update(); double[] bounds = new double[6]; bounds = sphereSource.GetOutput().GetBounds(); planes.SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]); } // nothing to visualize }
/// <summary> /// A console application that creates a /// vtkRenderWindow without a Windows Form /// </summary> /// <param name="argv"></param> public static void Main(String[] argv) { // Demonstrate how to use the vtkBoxWidget 3D widget, // This script uses a 3D box widget to define a "clipping box" to clip some // simple geometry (a mace). Make sure that you hit the "W" key to activate the widget. // Create a mace out of filters. sphere = vtkSphereSource.New(); cone = vtkConeSource.New(); glyph = vtkGlyph3D.New(); glyph.SetInputConnection(sphere.GetOutputPort()); glyph.SetSource(cone.GetOutput()); glyph.SetVectorModeToUseNormal(); glyph.SetScaleModeToScaleByVector(); glyph.SetScaleFactor(0.25); // The sphere and spikes are appended into a single polydata. This just makes things // simpler to manage. apd = vtkAppendPolyData.New(); apd.AddInput(glyph.GetOutput()); apd.AddInput(sphere.GetOutput()); maceMapper = vtkPolyDataMapper.New(); maceMapper.SetInputConnection(apd.GetOutputPort()); maceActor = vtkLODActor.New(); maceActor.SetMapper(maceMapper); maceActor.VisibilityOn(); // This portion of the code clips the mace with the vtkPlanes implicit function. // The clipped region is colored green. planes = vtkPlanes.New(); clipper = vtkClipPolyData.New(); clipper.SetInputConnection(apd.GetOutputPort()); clipper.SetClipFunction(planes); clipper.InsideOutOn(); selectMapper = vtkPolyDataMapper.New(); selectMapper.SetInputConnection(clipper.GetOutputPort()); selectActor = vtkLODActor.New(); selectActor.SetMapper(selectMapper); selectActor.GetProperty().SetColor(0, 1, 0); selectActor.VisibilityOff(); selectActor.SetScale(1.01, 1.01, 1.01); // Create the RenderWindow, Renderer and both Actors ren1 = vtkRenderer.New(); renWin = vtkRenderWindow.New(); renWin.AddRenderer(ren1); iren = vtkRenderWindowInteractor.New(); iren.SetRenderWindow(renWin); // The SetInteractor method is how 3D widgets are associated with the render // window interactor. Internally, SetInteractor sets up a bunch of callbacks // using the Command/Observer mechanism (AddObserver()). boxWidget = vtkBoxWidget.New(); boxWidget.SetInteractor(iren); boxWidget.SetPlaceFactor(1.25); ren1.AddActor(maceActor); ren1.AddActor(selectActor); // Add the actors to the renderer, set the background and size ren1.SetBackground(0.1, 0.2, 0.4); renWin.SetSize(300, 300); // Place the interactor initially. The input to a 3D widget is used to // initially position and scale the widget. The EndInteractionEvent is // observed which invokes the SelectPolygons callback. boxWidget.SetInput(glyph.GetOutput()); boxWidget.PlaceWidget(); boxWidget.EndInteractionEvt += new vtkObject.vtkObjectEventHandler(SelectPolygons); // render the image iren.Initialize(); iren.Start(); //Clean up deleteAllVTKObjects(); }
private void SelectAreaClick(vtkObject sender, vtkObjectEventArgs e) { int[] clickPos = Inter.GetEventPosition(); vtkAreaPicker picker = vtkAreaPicker.New(); picker.AreaPick(clickPos[0], clickPos[1], clickPos[0] + 100, clickPos[1] + 100, Viewport); if (picker.GetActor() != null) { vtkPlanes Boundary = picker.GetFrustum(); vtkExtractGeometry Box = vtkExtractGeometry.New(); Box.SetImplicitFunction(Boundary); Box.SetInput(picker.GetActor().GetMapper().GetInput()); vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New(); glyphFilter.SetInputConnection(Box.GetOutputPort()); glyphFilter.Update(); vtkPolyData selected = glyphFilter.GetOutput(); vtkPoints points = vtkPoints.New(); vtkUnstructuredGrid grid = vtkUnstructuredGrid.New(); for (int i = 0; i < selected.GetNumberOfPoints(); i++) { points.InsertNextPoint(selected.GetPoint(i)[0], selected.GetPoint(i)[1], selected.GetPoint(i)[2]); } grid.SetPoints(points); vtkSphereSource sphere = vtkSphereSource.New(); sphere.SetPhiResolution(6); sphere.SetThetaResolution(6); sphere.SetRadius(0.1); vtkGlyph3D glyph3D = vtkGlyph3D.New(); glyph3D.SetInput(grid); glyph3D.SetSourceConnection(sphere.GetOutputPort()); vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); mapper.SetInputConnection(glyph3D.GetOutputPort()); //double[] P = new double[3]; //bool selected = false; //vtkPoints points = Faces.GetPoints(); //double[] ClickedPoint = PointPicker.GetActor().GetMapper().GetInput().GetPoint(PointPicker.GetPointId()); //for (int i = 0; i < points.GetNumberOfPoints(); i++) //{ // if (Math.Abs(points.GetPoint(i)[0] - ClickedPoint[0]) < 1e-6 && // Math.Abs(points.GetPoint(i)[1] - ClickedPoint[1]) < 1e-6 && // Math.Abs(points.GetPoint(i)[2] - ClickedPoint[2]) < 1e-6) // { // selected = true; // P = points.GetPoint(i); // break; // } //} // //if (selected == true) //{ // SelectionPoints.InsertNextPoint(P[0], P[1], P[2]); // // SelectionGlyph = vtkGlyph3D.New(); // SelectionGlyph.SetInput(SelectionPolyData); // SelectionGlyph.SetSourceConnection(SelectionSphere.GetOutputPort()); // SelectionMapper.SetInputConnection(SelectionGlyph.GetOutputPort()); // // // Refresh Viewport // Refresh(); //} } }