1 | // |
---|
2 | // ******************************************************************** |
---|
3 | // * License and Disclaimer * |
---|
4 | // * * |
---|
5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
7 | // * conditions of the Geant4 Software License, included in the file * |
---|
8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
9 | // * include a list of copyright holders. * |
---|
10 | // * * |
---|
11 | // * Neither the authors of this software system, nor their employing * |
---|
12 | // * institutes,nor the agencies providing financial support for this * |
---|
13 | // * work make any representation or warranty, express or implied, * |
---|
14 | // * regarding this software system or assume any liability for its * |
---|
15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
16 | // * for the full disclaimer and the limitation of liability. * |
---|
17 | // * * |
---|
18 | // * This code implementation is the result of the scientific and * |
---|
19 | // * technical work of the GEANT4 collaboration. * |
---|
20 | // * By using, copying, modifying or distributing the software (or * |
---|
21 | // * any work based on the software) you agree to acknowledge its * |
---|
22 | // * use in resulting scientific publications, and indicate your * |
---|
23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
24 | // ******************************************************************** |
---|
25 | // |
---|
26 | // |
---|
27 | // $Id: G4OpenGLXmRotationCallbacks.cc,v 1.17 2009/01/19 16:53:42 lgarnier Exp $ |
---|
28 | // GEANT4 tag $Name: $ |
---|
29 | // |
---|
30 | // |
---|
31 | // Andrew Walkden 16th April 1997 |
---|
32 | // G4OpenGLXmRotationCallbacks : |
---|
33 | // Several callback functions used by |
---|
34 | // elements of the control panel to |
---|
35 | // rotate the view. |
---|
36 | |
---|
37 | #ifdef G4VIS_BUILD_OPENGLXM_DRIVER |
---|
38 | |
---|
39 | #include "G4OpenGLXmViewer.hh" |
---|
40 | |
---|
41 | #include "G4Scene.hh" |
---|
42 | #include "G4VSceneHandler.hh" |
---|
43 | #include "G4UImanager.hh" |
---|
44 | #include "G4ios.hh" |
---|
45 | |
---|
46 | void G4OpenGLXmViewer::theta_rotation_callback (Widget w, |
---|
47 | XtPointer clientData, |
---|
48 | XtPointer callData) |
---|
49 | { |
---|
50 | XmArrowButtonCallbackStruct *cbs = (XmArrowButtonCallbackStruct*) callData; |
---|
51 | G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData; |
---|
52 | |
---|
53 | pView->rotate_right = get_boolean_userData (w); |
---|
54 | |
---|
55 | if (cbs->reason == XmCR_ARM) { |
---|
56 | rotate_in_theta (pView, NULL); |
---|
57 | } else if (cbs->reason == XmCR_DISARM) { |
---|
58 | XtRemoveTimeOut (pView->rotation_timer); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | void G4OpenGLXmViewer::rotate_in_theta (XtPointer clientData, |
---|
63 | XtIntervalId* timer_id) |
---|
64 | { |
---|
65 | //theta spin stuff here |
---|
66 | G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData; |
---|
67 | // No callback allowed when no scene |
---|
68 | if (!pView->GetSceneHandler()->GetScene()) { |
---|
69 | return; |
---|
70 | } |
---|
71 | |
---|
72 | if (pView->rotate_right) { |
---|
73 | pView->rotateScene((G4double)pView->rot_sens,0,1); |
---|
74 | } else { |
---|
75 | pView->rotateScene(-(G4double)pView->rot_sens,0,1); |
---|
76 | } |
---|
77 | /* |
---|
78 | G4double delta_theta; |
---|
79 | |
---|
80 | if (pView->fVP.GetLightsMoveWithCamera()) { |
---|
81 | if (pView->rotate_right) { |
---|
82 | delta_theta = -((G4double)pView->rot_sens); |
---|
83 | } else { |
---|
84 | delta_theta = (G4double)pView->rot_sens; |
---|
85 | } |
---|
86 | } else { |
---|
87 | if (pView->rotate_right) { |
---|
88 | delta_theta = (G4double)pView->rot_sens; |
---|
89 | } else { |
---|
90 | delta_theta = -((G4double)pView->rot_sens); |
---|
91 | } |
---|
92 | } |
---|
93 | delta_theta *= deg; |
---|
94 | // Rotates by fixed azimuthal angle delta_theta. |
---|
95 | |
---|
96 | const G4Vector3D& vp = pView->fVP.GetViewpointDirection ().unit (); |
---|
97 | const G4Vector3D& up = pView->fVP.GetUpVector ().unit (); |
---|
98 | const G4Vector3D& zprime = up; |
---|
99 | G4double cosalpha = up.dot (vp); |
---|
100 | G4double sinalpha = std::sqrt (1. - std::pow (cosalpha, 2)); |
---|
101 | G4Vector3D yprime = (zprime.cross (vp)).unit (); |
---|
102 | G4Vector3D xprime = yprime.cross (zprime); |
---|
103 | // Projection of vp on plane perpendicular to up... |
---|
104 | G4Vector3D a1 = sinalpha * xprime; |
---|
105 | // Required new projection... |
---|
106 | G4Vector3D a2 = |
---|
107 | sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime); |
---|
108 | // Required Increment vector... |
---|
109 | G4Vector3D delta = a2 - a1; |
---|
110 | // So new viewpoint is... |
---|
111 | G4Vector3D viewPoint = vp + delta; |
---|
112 | |
---|
113 | pView->fVP.SetViewAndLights (viewPoint); |
---|
114 | */ |
---|
115 | |
---|
116 | pView->SetView (); |
---|
117 | pView->ClearView (); |
---|
118 | pView->DrawView (); |
---|
119 | |
---|
120 | pView->rotation_timer = XtAppAddTimeOut |
---|
121 | (pView->app, |
---|
122 | timer_id == NULL ? 500 : 1, |
---|
123 | rotate_in_theta, |
---|
124 | pView); |
---|
125 | } |
---|
126 | |
---|
127 | void G4OpenGLXmViewer::phi_rotation_callback (Widget w, |
---|
128 | XtPointer clientData, |
---|
129 | XtPointer callData) |
---|
130 | { |
---|
131 | XmArrowButtonCallbackStruct *cbs = (XmArrowButtonCallbackStruct*) callData; |
---|
132 | G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData; |
---|
133 | |
---|
134 | pView->rotate_up = get_boolean_userData (w); |
---|
135 | |
---|
136 | if (cbs->reason == XmCR_ARM) { |
---|
137 | rotate_in_phi (pView, NULL); |
---|
138 | } else if (cbs->reason == XmCR_DISARM) { |
---|
139 | XtRemoveTimeOut (pView->rotation_timer); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | void G4OpenGLXmViewer::rotate_in_phi (XtPointer clientData, |
---|
144 | XtIntervalId* timer_id) |
---|
145 | { |
---|
146 | |
---|
147 | //phi spin stuff here |
---|
148 | // G4double delta_alpha; |
---|
149 | G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData; |
---|
150 | |
---|
151 | // No callback allowed when no scene |
---|
152 | if (!pView->GetSceneHandler()->GetScene()) { |
---|
153 | return; |
---|
154 | } |
---|
155 | if (pView -> rotate_up) { |
---|
156 | pView->rotateScene(0,-(G4double)pView->rot_sens,1); |
---|
157 | } else { |
---|
158 | pView->rotateScene(0,(G4double)pView->rot_sens,1); |
---|
159 | } |
---|
160 | /* |
---|
161 | if (pView->fVP.GetLightsMoveWithCamera()) { |
---|
162 | if (pView -> rotate_up) { |
---|
163 | delta_alpha = -((G4double)pView->rot_sens); |
---|
164 | } else { |
---|
165 | delta_alpha = (G4double)pView->rot_sens; |
---|
166 | } |
---|
167 | } else { |
---|
168 | if (pView -> rotate_up) { |
---|
169 | delta_alpha = (G4double)pView->rot_sens; |
---|
170 | } else { |
---|
171 | delta_alpha = -((G4double)pView->rot_sens); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | delta_alpha *= deg; |
---|
176 | |
---|
177 | const G4Vector3D& vp = pView->fVP.GetViewpointDirection ().unit (); |
---|
178 | const G4Vector3D& up = pView->fVP.GetUpVector ().unit (); |
---|
179 | |
---|
180 | const G4Vector3D& xprime = vp; |
---|
181 | G4Vector3D yprime = (up.cross(xprime)).unit(); |
---|
182 | G4Vector3D zprime = (xprime.cross(yprime)).unit(); |
---|
183 | |
---|
184 | G4Vector3D new_vp = std::cos(delta_alpha) * xprime + std::sin(delta_alpha) * zprime; |
---|
185 | |
---|
186 | pView->fVP.SetViewAndLights (new_vp.unit()); |
---|
187 | |
---|
188 | if (pView->fVP.GetLightsMoveWithCamera()) { |
---|
189 | G4Vector3D new_up = (new_vp.cross(yprime)).unit(); |
---|
190 | pView->fVP.SetUpVector(new_up); |
---|
191 | } |
---|
192 | |
---|
193 | */ |
---|
194 | pView->SetView (); |
---|
195 | pView->ClearView (); |
---|
196 | pView->DrawView (); |
---|
197 | |
---|
198 | pView->rotation_timer = XtAppAddTimeOut |
---|
199 | (pView->app, |
---|
200 | timer_id == NULL ? 500 : 1, |
---|
201 | rotate_in_phi, |
---|
202 | pView); |
---|
203 | } |
---|
204 | |
---|
205 | void G4OpenGLXmViewer::set_rot_sens_callback (Widget w, |
---|
206 | XtPointer clientData, |
---|
207 | XtPointer callData) |
---|
208 | { |
---|
209 | XmScaleCallbackStruct *cbs = (XmScaleCallbackStruct*) callData; |
---|
210 | G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData; |
---|
211 | short dp = -1; |
---|
212 | G4float ten_to_the_dp = 10.; |
---|
213 | |
---|
214 | XtVaGetValues (w, |
---|
215 | XmNdecimalPoints, &dp, |
---|
216 | NULL); |
---|
217 | |
---|
218 | if (dp == 0) { |
---|
219 | ten_to_the_dp = 1.; |
---|
220 | } else if ( dp > 0) { |
---|
221 | for (G4int i = 1; i < (G4int)dp; i++) { |
---|
222 | ten_to_the_dp *= 10.; |
---|
223 | } |
---|
224 | } else { |
---|
225 | G4Exception("Bad value returned for dp in set_rot_sens_callback"); |
---|
226 | } |
---|
227 | |
---|
228 | pView->rot_sens = (G4float)(cbs->value) / ten_to_the_dp; |
---|
229 | } |
---|
230 | |
---|
231 | void G4OpenGLXmViewer::set_rot_subject_callback (Widget w, |
---|
232 | XtPointer clientData, |
---|
233 | XtPointer) |
---|
234 | { |
---|
235 | G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*)clientData; |
---|
236 | |
---|
237 | G4int choice = get_int_userData (w); |
---|
238 | |
---|
239 | switch (choice) { |
---|
240 | case 0: |
---|
241 | { |
---|
242 | pView->fVP.SetLightsMoveWithCamera (true); |
---|
243 | break; |
---|
244 | } |
---|
245 | case 1: |
---|
246 | { |
---|
247 | pView->fVP.SetLightsMoveWithCamera (false); |
---|
248 | break; |
---|
249 | } |
---|
250 | default: |
---|
251 | { |
---|
252 | G4Exception("Unrecognised choice made in set_rot_subject_callback"); |
---|
253 | } |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | void G4OpenGLXmViewer::wobble_callback (Widget w, |
---|
258 | XtPointer, |
---|
259 | XtPointer) |
---|
260 | { |
---|
261 | G4OpenGLXmViewer* pView; |
---|
262 | |
---|
263 | XtVaGetValues (w, |
---|
264 | XmNuserData, &pView, |
---|
265 | NULL); |
---|
266 | |
---|
267 | pView->original_vp = pView->fVP.GetViewpointDirection(); |
---|
268 | pView->wobble_timer = XtAppAddTimeOut |
---|
269 | (pView->app, |
---|
270 | (long unsigned int) (1000. * (1. / pView->wob_sens)), |
---|
271 | wobble_timer_callback, |
---|
272 | pView); |
---|
273 | } |
---|
274 | |
---|
275 | void G4OpenGLXmViewer::wobble_timer_callback (XtPointer clientData, |
---|
276 | XtIntervalId*) |
---|
277 | { |
---|
278 | |
---|
279 | G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*)clientData; |
---|
280 | // No callback allowed when no scene |
---|
281 | if (!pView->GetSceneHandler()->GetScene()) { |
---|
282 | return; |
---|
283 | } |
---|
284 | const G4Vector3D& up = pView->fVP.GetUpVector(); |
---|
285 | G4Vector3D third_axis = up.cross(pView->original_vp); |
---|
286 | G4double pi_div_by_ten = pi / 10.0; |
---|
287 | G4Vector3D d_up = 0.1 * (std::sin ((G4double)pView->frameNo * pi_div_by_ten * 2.)) * up; |
---|
288 | G4Vector3D d_third_axis = 0.1 * (std::sin ((G4double)pView->frameNo * (pi_div_by_ten))) * third_axis; |
---|
289 | |
---|
290 | pView->fVP.SetViewAndLights (pView->original_vp + d_up + d_third_axis); |
---|
291 | |
---|
292 | pView->SetView (); |
---|
293 | pView->ClearView (); |
---|
294 | pView->DrawView (); |
---|
295 | |
---|
296 | if (pView->frameNo++ == 20) { |
---|
297 | if (pView->wobble_timer) { |
---|
298 | XtRemoveTimeOut (pView->wobble_timer); |
---|
299 | pView->frameNo = 0; |
---|
300 | pView->fVP.SetViewAndLights (pView->original_vp); |
---|
301 | pView->SetView (); |
---|
302 | pView->ClearView (); |
---|
303 | pView->DrawView (); |
---|
304 | } |
---|
305 | } else { |
---|
306 | pView->wobble_timer = XtAppAddTimeOut |
---|
307 | (pView->app, |
---|
308 | (long unsigned int) (1000. * (1. / pView->wob_sens)), |
---|
309 | wobble_timer_callback, |
---|
310 | pView); |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | void G4OpenGLXmViewer::reset_callback (Widget w, |
---|
315 | XtPointer, |
---|
316 | XtPointer) |
---|
317 | { |
---|
318 | |
---|
319 | G4OpenGLXmViewer* pView; |
---|
320 | |
---|
321 | XtVaGetValues (w, |
---|
322 | XmNuserData, &pView, |
---|
323 | NULL); |
---|
324 | |
---|
325 | // No callback allowed when no scene |
---|
326 | if (!pView->GetSceneHandler()->GetScene()) { |
---|
327 | return; |
---|
328 | } |
---|
329 | pView->fVP.SetCurrentTargetPoint(G4Point3D()); |
---|
330 | pView->fVP.SetZoomFactor(1.0); |
---|
331 | pView->fVP.SetDolly(0.0); |
---|
332 | pView->SetView (); |
---|
333 | pView->ClearView (); |
---|
334 | pView->DrawView (); |
---|
335 | pView->zoom_low = 0.1; |
---|
336 | pView->zoom_high = 10.0; |
---|
337 | |
---|
338 | } |
---|
339 | #endif |
---|