File: Form1.vb

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 (226 lines) | stat: -rw-r--r-- 7,332 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
Imports Kitware.VTK
Public Class Form1

    Dim puzzle As vtkSpherePuzzle
    Dim mapper As vtkPolyDataMapper
    Dim actor As vtkActor
    Dim arrows As vtkSpherePuzzleArrows
    Dim mapper2 As vtkPolyDataMapper
    Dim actor2 As vtkActor
    Dim once As Boolean

    Dim in_piece_rotation As Boolean
    Dim LastVal As Double
    Dim LastValExists As Boolean


    ' <summary>
    ' Reset
    ' </summary>
    ' <param name="sender"></param>
    ' <param name="e"></param>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        puzzle.Reset()
        RenderWindowControl1.RenderWindow.Render()
    End Sub

    ' <summary>
    ' Quit
    ' </summary>
    ' <param name="sender"></param>
    ' <param name="e"></param>
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    ' <summary>
    ' Set up the dialog
    ' </summary>
    ' <param name="sender"></param>
    ' <param name="e"></param>
    Private Sub RenderWindowControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RenderWindowControl1.Load
        'Setup the variables and the background
        Dim ren1 As vtkRenderer
        ren1 = RenderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer()
        mapper.SetInputConnection(puzzle.GetOutputPort())
        mapper2.SetInputConnection(arrows.GetOutputPort())
        actor.SetMapper(mapper)
        actor2.SetMapper(mapper2)
        ren1.AddActor(actor)
        ren1.AddActor(actor2)
        ren1.SetBackground(0.1, 0.2, 0.4)

        'Set up the camera
        ren1.ResetCamera()
        Dim cam As vtkCamera
        cam = ren1.GetActiveCamera()
        cam.Elevation(-40)
        RenderWindowControl1.RenderWindow.Render()

        'Change the style to a trackball style
        'Equivalent of pressing 't'
        Dim iren As vtkRenderWindowInteractor
        Dim istyle As vtkInteractorStyleSwitch
        iren = RenderWindowControl1.RenderWindow.GetInteractor()
        istyle = vtkInteractorStyleSwitch.[New]
        iren.SetInteractorStyle(istyle)
        istyle.SetCurrentStyleToTrackballCamera()

        'Add events to the iren instead of Observers
        AddHandler iren.MouseMoveEvt, AddressOf MotionCallback
        AddHandler iren.CharEvt, AddressOf CharCallback
    End Sub

    ' <summary>
    ' Clean up globals
    ' </summary>
    Sub disposeAllVTKObjects()
        puzzle.Dispose()
        mapper.Dispose()
        actor.Dispose()
        arrows.Dispose()
        mapper2.Dispose()
        actor2.Dispose()
    End Sub


    ' <summary>
    ' Highlights pieces
    ' </summary>
    ' <param name="sender"></param>
    ' <param name="e"></param>
    Sub MotionCallback(ByVal sender As vtkObject, ByVal e As vtkObjectEventArgs)
        'Make sure the piece isn't in an animation
        'durring a click or bad things happen
        If in_piece_rotation = False Then
            Dim iren As vtkRenderWindowInteractor
            Dim istyle As vtkInteractorStyleSwitch
            iren = RenderWindowControl1.RenderWindow.GetInteractor()
            istyle = iren.GetInteractorStyle()

            'return if the user is performing interaction
            If istyle.GetState() <> 0 Then
                Return
            End If
            Dim pos() As Integer
            Dim x As Integer
            Dim y As Integer
            pos = iren.GetEventPosition()
            x = pos(0)
            y = pos(1)
            Dim ren1 As vtkRenderer
            ren1 = RenderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer()
            ren1.SetDisplayPoint(x, y, ren1.GetZ(x, y))
            ren1.DisplayToWorld()
            Dim pt() As Double
            Dim val As Double
            pt = ren1.GetWorldPoint()
            val = puzzle.SetPoint(pt(0), pt(1), pt(2))
            If (LastValExists = False Or val <> LastVal) Then
                RenderWindowControl1.RenderWindow.Render()
                LastVal = val
                LastValExists = True
            End If
        End If
    End Sub

    ' <summary>
    ' Called when a key is pressed
    ' </summary>
    ' <param name="sender"></param>
    ' <param name="e"></param>
    Sub CharCallback(byval sender as vtkObject, byval e as vtkObjectEventArgs)
        Dim iren As vtkRenderWindowInteractor
        Dim keycode As SByte
        iren = RenderWindowControl1.RenderWindow.GetInteractor()
        keycode = iren.GetKeyCode()
        'if the keycode is not M
        If (keycode <> 109 And keycode <> 77) Then

            Return
        End If
        Dim pos() As Integer
        pos = iren.GetEventPosition()
        ButtonCallback(pos(0), pos(1))
    End Sub

    ' <summary>
    ' Moves the sphere when the mouse is clicked in
    ' position (x,y)
    ' </summary>
    ' <param name="x"></param>
    ' <param name="y"></param>
    Sub ButtonCallback(ByVal x As Double, ByVal y As Double)
        If (in_piece_rotation = False) Then
            in_piece_rotation = True
            Dim ren1 As vtkRenderer
            ren1 = RenderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer()
            ren1.SetDisplayPoint(x, y, ren1.GetZ(x, y))
            ren1.DisplayToWorld()
            Dim pt() As Double
            pt = ren1.GetWorldPoint()
            x = pt(0)
            y = pt(1)
            Dim z As Double
            z = pt(2)
            Dim i As Integer
            i = 0
            While i <= 100
                puzzle.SetPoint(x, y, z)
                puzzle.MovePoint(i)
                RenderWindowControl1.RenderWindow.Render()
                Me.Update()
                i = i + 10
            End While
            in_piece_rotation = False
        End If
    End Sub

    ' <summary>
    ' Scrambles the puzzle when the form first becomes
    ' visible
    ' </summary>
    ' <param name="sender"></param>
    ' <param name="e"></param>
    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        Me.Timer1.Start()
    End Sub
    ' <summary>
    ' Clean up
    ' </summary>
    ' <param name="sender"></param>
    ' <param name="e"></param>
    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        disposeAllVTKObjects()
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        puzzle = vtkSpherePuzzle.[New]
        mapper = vtkPolyDataMapper.[New]
        actor = vtkActor.[New]
        arrows = vtkSpherePuzzleArrows.[New]
        mapper2 = vtkPolyDataMapper.[New]
        actor2 = vtkActor.[New]
        once = True

        in_piece_rotation = False
        LastVal = 0
        LastValExists = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (once = True) Then
            ButtonCallback(218, 195)
            ButtonCallback(261, 128)
            ButtonCallback(213, 107)
            ButtonCallback(203, 162)
            ButtonCallback(134, 186)
            once = False
        End If
    End Sub
End Class