File: BoxWidget.cs

package info (click to toggle)
activiz.net 1%3A1.0~git20111214-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,280 kB
  • ctags: 5,957
  • sloc: cs: 28,767; python: 915; cpp: 130; makefile: 35; sh: 11
file content (141 lines) | stat: -rw-r--r-- 5,120 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using Kitware.VTK;
using System;

/// <summary>
/// Class containing main method
/// </summary>
public class BoxWidgetClass
{
    /// <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();
    }

    static vtkSphereSource sphere;
    static vtkConeSource cone;
    static vtkGlyph3D glyph;
    static vtkAppendPolyData apd;
    static vtkPolyDataMapper maceMapper;
    static vtkLODActor maceActor;
    static vtkPlanes planes;
    static vtkClipPolyData clipper;
    static vtkPolyDataMapper selectMapper;
    static vtkLODActor selectActor;
    static vtkRenderer ren1;
    static vtkRenderWindow renWin;
    static vtkRenderWindowInteractor iren;
    static vtkBoxWidget boxWidget;

    /// <summary>
    /// Callback function for boxWidget.EndInteractionEvt
    /// </summary>
    public static void SelectPolygons(vtkObject sender, vtkObjectEventArgs e)
    {
        boxWidget.GetPlanes(planes);
        selectActor.VisibilityOn();
    }

    ///<summary>
    ///Deletes all static objects created
    ///</summary>
    public static void deleteAllVTKObjects()
    {
        //clean up vtk objects
        if (sphere != null) { sphere.Dispose(); }
        if (cone != null) { cone.Dispose(); }
        if (glyph != null) { glyph.Dispose(); }
        if (apd != null) { apd.Dispose(); }
        if (maceMapper != null) { maceMapper.Dispose(); }
        if (maceActor != null) { maceActor.Dispose(); }
        if (planes != null) { planes.Dispose(); }
        if (clipper != null) { clipper.Dispose(); }
        if (selectMapper != null) { selectMapper.Dispose(); }
        if (selectActor != null) { selectActor.Dispose(); }
        if (ren1 != null) { ren1.Dispose(); }
        if (renWin != null) { renWin.Dispose(); }
        if (iren != null) { iren.Dispose(); }
        if (boxWidget != null) { boxWidget.Dispose(); }
    }
}