File: Form1.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 (240 lines) | stat: -rw-r--r-- 9,353 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Kitware.VTK;
namespace VolumeRendering
{
    /// <summary>
    /// Application to load and display a volume
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool MouseDown1 = false;
        private vtkRenderWindowInteractor Interactor = null;
        private vtkRenderWindow RenderWindow = null;
        private vtkRenderer Renderer = null;
        private vtkImageActor ImageActor = null;
        private vtkImageClip Clip = null;
        string fileName = "";

        /// <summary>
        /// Tell the application when the mouse is being dragged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void iren_LeftButtonReleaseEvt(vtkObject sender, vtkObjectEventArgs e)
        {
            this.MouseDown1 = false;
        }
        /// <summary>
        /// Tell the application when the mouse is being dragged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void iren_LeftButtonPressEvt(vtkObject sender, vtkObjectEventArgs e)
        {
            this.MouseDown1 = true;
        }
        
        /// <summary>
        /// Display the render window with the 3D Volume in it
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void renderWindowControl2_Load(object sender, EventArgs e)
        {
            //Create all the objects for the pipeline
            vtkRenderer renderer = renderWindowControl2.RenderWindow.GetRenderers().GetFirstRenderer();
            vtkXMLImageDataReader reader = vtkXMLImageDataReader.New();
            vtkFixedPointVolumeRayCastMapper texMapper = vtkFixedPointVolumeRayCastMapper.New();
            vtkVolume vol = vtkVolume.New();
            vtkColorTransferFunction ctf = vtkColorTransferFunction.New();
            vtkPiecewiseFunction spwf = vtkPiecewiseFunction.New();
            vtkPiecewiseFunction gpwf = vtkPiecewiseFunction.New();
            
            //Read in the file
            reader.SetFileName(fileName);
            reader.Update();
            
            //Go through the visulizatin pipeline
            texMapper.SetInputConnection(reader.GetOutputPort());
            
            //Set the color curve for the volume
            ctf.AddHSVPoint(0, .67, .07, 1);
            ctf.AddHSVPoint(94, .67, .07, 1);
            ctf.AddHSVPoint(139, 0, 0, 0);
            ctf.AddHSVPoint(160, .28, .047, 1);
            ctf.AddHSVPoint(254, .38, .013, 1);
            
            //Set the opacity curve for the volume
            spwf.AddPoint(84, 0);
            spwf.AddPoint(151, .1);
            spwf.AddPoint(255, 1);
            
            //Set the gradient curve for the volume
            gpwf.AddPoint(0, .2);
            gpwf.AddPoint(10, .2);
            gpwf.AddPoint(25, 1);
            
            vol.GetProperty().SetColor(ctf);
            vol.GetProperty().SetScalarOpacity(spwf);
            vol.GetProperty().SetGradientOpacity(gpwf);
            
            vol.SetMapper(texMapper);

            //Go through the Graphics Pipeline
            renderer.AddVolume(vol);
        }
        
        
        /// <summary>
        /// Display the render window with the slice in it
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void renderWindowControl1_Load(object sender, EventArgs e)
        {
            //Get the name of the Unsigned Char volume that you want to load
            fileName = "../../../head.vti";
  
            //Create all the objects for the pipeline
            vtkXMLImageDataReader reader = vtkXMLImageDataReader.New();
            vtkImageActor iactor = vtkImageActor.New();
            vtkImageClip clip = vtkImageClip.New();
            vtkContourFilter contour = vtkContourFilter.New();
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
            vtkActor actor = vtkActor.New();
            vtkInteractorStyleImage style = vtkInteractorStyleImage.New();
            
            vtkRenderer renderer = renderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer();

            //Read the Image
            reader.SetFileName(fileName);
            
            //Go through the visulization pipeline
            iactor.SetInput(reader.GetOutput());
            renderer.AddActor(iactor);
            reader.Update();
            int[] extent = reader.GetOutput().GetWholeExtent();
            iactor.SetDisplayExtent(extent[0], extent[1], extent[2], extent[3],
                        (extent[4] + extent[5]) / 2,
                        (extent[4] + extent[5]) / 2);
            
            clip.SetInputConnection(reader.GetOutputPort());
            clip.SetOutputWholeExtent(extent[0], extent[1], extent[2], extent[3],
                        (extent[4] + extent[5]) / 2,
                        (extent[4] + extent[5]) / 2);
            
            contour.SetInputConnection(clip.GetOutputPort());
            contour.SetValue(0, 100);
           
            mapper.SetInputConnection(contour.GetOutputPort());
            mapper.SetScalarVisibility(1);
   
            //Go through the graphics pipeline
            actor.SetMapper(mapper);
            actor.GetProperty().SetColor(0, 1, 0);

            renderer.AddActor(actor);
            
            //Give a new style to the interactor
            vtkRenderWindowInteractor iren = renderWindowControl1.RenderWindow.GetInteractor();
            iren.SetInteractorStyle(style);

            //Add new events to the interactor style
            style.LeftButtonPressEvt += new vtkObject.vtkObjectEventHandler(iren_LeftButtonPressEvt);
            style.LeftButtonReleaseEvt += new vtkObject.vtkObjectEventHandler(iren_LeftButtonReleaseEvt);
            style.MouseMoveEvt += new vtkObject.vtkObjectEventHandler(iren_MouseMoveEvt);

            //Update global variables
            this.trackBar1.Maximum = extent[5];
            this.trackBar1.Minimum = extent[4];
            this.Interactor = iren;
            this.RenderWindow = renderWindowControl1.RenderWindow;
            this.Renderer = renderer;
            this.Clip = clip;
            this.ImageActor = iactor;
        }
        
        /// <summary>
        /// Move the slice when the trackbar is moved
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (ImageActor != null)
            {
                int[] lastPos = this.Interactor.GetLastEventPosition();
                int[] size = this.RenderWindow.GetSize();
                int[] dim = this.ImageActor.GetInput().GetDimensions();

                int newSlice = (int)(trackBar1.Value);

                if (newSlice >= 0 && newSlice < dim[2])
                {
                    this.Clip.SetOutputWholeExtent(0, dim[0] - 1, 0, dim[1] - 1, newSlice, newSlice);
                    this.ImageActor.SetDisplayExtent(0, dim[0] - 1, 0, dim[1] - 1, newSlice, newSlice);
                    this.Renderer.ResetCameraClippingRange();
                    this.RenderWindow.Render();
                }
            }
        }
         
        /// <summary>
        /// Move the slice and the trackbar when the mouse is dragged on the render window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void iren_MouseMoveEvt(vtkObject sender, vtkObjectEventArgs e)
        {
            if (this.MouseDown1 && this.ImageActor != null)
            {
                int[] lastPos = this.Interactor.GetLastEventPosition();
                int[] size = this.RenderWindow.GetSize();
                int[] dim = this.ImageActor.GetInput().GetDimensions();

                int newSlice = (int)((double)(dim[2] - 1.0) * (double)(lastPos[1]) / (double)(size[1]));
               
                if (newSlice >= 0 && newSlice < dim[2])
                {
                    this.trackBar1.Value = newSlice;
                    this.Clip.SetOutputWholeExtent(0, dim[0] - 1, 0, dim[1] - 1, newSlice, newSlice);
                    this.ImageActor.SetDisplayExtent(0, dim[0] - 1, 0, dim[1] - 1, newSlice, newSlice);
                    this.Renderer.ResetCameraClippingRange();
                    this.RenderWindow.Render();
                }
            }
        }
        
        /// <summary>
        /// Clean Up any global variables that might still be around
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Closed(object sender, FormClosedEventArgs e)
        {
            if (this.Interactor != null)
            {
                this.Interactor.Dispose();
            }
            if (this.ImageActor != null)
            {
                this.ImageActor.Dispose();
            }
            if (this.Clip != null)
            {
                this.Clip.Dispose();
            }
        }
    }
}