ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4OpenGLXmRotationCallbacks.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4OpenGLXmRotationCallbacks.cc
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 //
28 //
29 // Andrew Walkden 16th April 1997
30 // G4OpenGLXmRotationCallbacks :
31 // Several callback functions used by
32 // elements of the control panel to
33 // rotate the view.
34 
35 #ifdef G4VIS_BUILD_OPENGLXM_DRIVER
36 
37 #include "G4OpenGLXmViewer.hh"
38 
39 #include "G4PhysicalConstants.hh"
40 #include "G4Scene.hh"
41 #include "G4UImanager.hh"
42 #include "G4ios.hh"
43 
44 void G4OpenGLXmViewer::theta_rotation_callback (Widget w,
45  XtPointer clientData,
46  XtPointer callData)
47 {
48  XmArrowButtonCallbackStruct *cbs = (XmArrowButtonCallbackStruct*) callData;
49  G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData;
50 
51  pView->rotate_right = get_boolean_userData (w);
52 
53  if (cbs->reason == XmCR_ARM) {
54  rotate_in_theta (pView, NULL);
55  } else if (cbs->reason == XmCR_DISARM) {
56  XtRemoveTimeOut (pView->rotation_timer);
57  }
58 }
59 
60 void G4OpenGLXmViewer::rotate_in_theta (XtPointer clientData,
61  XtIntervalId* timer_id)
62 {
63  //theta spin stuff here
64  G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData;
65 
66  if (pView->rotate_right) {
67  pView->rotateScene(1,0);
68  } else {
69  pView->rotateScene(-1,0);
70  }
71  /*
72  G4double delta_theta;
73 
74  if (pView->fVP.GetLightsMoveWithCamera()) {
75  if (pView->rotate_right) {
76  delta_theta = -(pView->fRot_sens);
77  } else {
78  delta_theta = pView->fRot_sens;
79  }
80  } else {
81  if (pView->rotate_right) {
82  delta_theta = pView->fRot_sens;
83  } else {
84  delta_theta = -(pView->fRot_sens);
85  }
86  }
87  delta_theta *= deg;
88  // Rotates by fixed azimuthal angle delta_theta.
89 
90  const G4Vector3D& vp = pView->fVP.GetViewpointDirection ().unit ();
91  const G4Vector3D& up = pView->fVP.GetUpVector ().unit ();
92  const G4Vector3D& zprime = up;
93  G4double cosalpha = up.dot (vp);
94  G4double sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
95  G4Vector3D yprime = (zprime.cross (vp)).unit ();
96  G4Vector3D xprime = yprime.cross (zprime);
97  // Projection of vp on plane perpendicular to up...
98  G4Vector3D a1 = sinalpha * xprime;
99  // Required new projection...
100  G4Vector3D a2 =
101  sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
102  // Required Increment vector...
103  G4Vector3D delta = a2 - a1;
104  // So new viewpoint is...
105  G4Vector3D viewPoint = vp + delta;
106 
107  pView->fVP.SetViewAndLights (viewPoint);
108  */
109 
110  pView->SetView ();
111  pView->ClearView ();
112  pView->DrawView ();
113 
114  pView->rotation_timer = XtAppAddTimeOut
115  (pView->app,
116  timer_id == NULL ? 500 : 1,
117  rotate_in_theta,
118  pView);
119 }
120 
121 void G4OpenGLXmViewer::phi_rotation_callback (Widget w,
122  XtPointer clientData,
123  XtPointer callData)
124 {
125  XmArrowButtonCallbackStruct *cbs = (XmArrowButtonCallbackStruct*) callData;
126  G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData;
127 
128  pView->rotate_up = get_boolean_userData (w);
129 
130  if (cbs->reason == XmCR_ARM) {
131  rotate_in_phi (pView, NULL);
132  } else if (cbs->reason == XmCR_DISARM) {
133  XtRemoveTimeOut (pView->rotation_timer);
134  }
135 }
136 
137 void G4OpenGLXmViewer::rotate_in_phi (XtPointer clientData,
138  XtIntervalId* timer_id)
139 {
140  //phi spin stuff here
141  // G4double delta_alpha;
142  G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData;
143 
144  if (pView -> rotate_up) {
145  pView->rotateScene(0,-1);
146  } else {
147  pView->rotateScene(0,1);
148  }
149  /*
150  if (pView->fVP.GetLightsMoveWithCamera()) {
151  if (pView -> rotate_up) {
152  delta_alpha = -(pView->fRot_sens);
153  } else {
154  delta_alpha = pView->fRot_sens;
155  }
156  } else {
157  if (pView -> rotate_up) {
158  delta_alpha = pView->fRot_sens;
159  } else {
160  delta_alpha = -(pView->fRot_sens);
161  }
162  }
163 
164  delta_alpha *= deg;
165 
166  const G4Vector3D& vp = pView->fVP.GetViewpointDirection ().unit ();
167  const G4Vector3D& up = pView->fVP.GetUpVector ().unit ();
168 
169  const G4Vector3D& xprime = vp;
170  G4Vector3D yprime = (up.cross(xprime)).unit();
171  G4Vector3D zprime = (xprime.cross(yprime)).unit();
172 
173  G4Vector3D new_vp = std::cos(delta_alpha) * xprime + std::sin(delta_alpha) * zprime;
174 
175  pView->fVP.SetViewAndLights (new_vp.unit());
176 
177  if (pView->fVP.GetLightsMoveWithCamera()) {
178  G4Vector3D new_up = (new_vp.cross(yprime)).unit();
179  pView->fVP.SetUpVector(new_up);
180  }
181 
182  */
183  pView->SetView ();
184  pView->ClearView ();
185  pView->DrawView ();
186 
187  pView->rotation_timer = XtAppAddTimeOut
188  (pView->app,
189  timer_id == NULL ? 500 : 1,
190  rotate_in_phi,
191  pView);
192 }
193 
194 void G4OpenGLXmViewer::set_rot_sens_callback (Widget w,
195  XtPointer clientData,
196  XtPointer callData)
197 {
198  XmScaleCallbackStruct *cbs = (XmScaleCallbackStruct*) callData;
199  G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData;
200  short dp = -1;
201  G4float ten_to_the_dp = 10.;
202 
203  XtVaGetValues (w,
204  XmNdecimalPoints, &dp,
205  NULL);
206 
207  if (dp == 0) {
208  ten_to_the_dp = 1.;
209  } else if ( dp > 0) {
210  for (G4int i = 1; i < (G4int)dp; i++) {
211  ten_to_the_dp *= 10.;
212  }
213  } else {
215  ("G4OpenGLXmViewer::set_rot_sens_callback",
216  "opengl2004", FatalException,
217  "Bad value returned for dp in set_rot_sens_callback");
218  }
219 
220  pView->fRot_sens = (G4float)(cbs->value) / ten_to_the_dp;
221 }
222 
223 void G4OpenGLXmViewer::set_rot_subject_callback (Widget w,
224  XtPointer clientData,
225  XtPointer)
226 {
227  G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*)clientData;
228 
229  G4int choice = get_int_userData (w);
230 
231  switch (choice) {
232  case 0:
233  {
234  pView->fVP.SetLightsMoveWithCamera (true);
235  break;
236  }
237  case 1:
238  {
239  pView->fVP.SetLightsMoveWithCamera (false);
240  break;
241  }
242  default:
243  {
245  ("G4OpenGLXmViewer::set_rot_subject_callback",
246  "opengl2005", FatalException,
247  "Unrecognised choice made in set_rot_subject_callback");
248  }
249  }
250 }
251 
252 void G4OpenGLXmViewer::wobble_callback (Widget w,
253  XtPointer,
254  XtPointer)
255 {
256  G4OpenGLXmViewer* pView;
257 
258  XtVaGetValues (w,
259  XmNuserData, &pView,
260  NULL);
261 
262  pView->original_vp = pView->fVP.GetViewpointDirection();
263  pView->wobble_timer = XtAppAddTimeOut
264  (pView->app,
265  (long unsigned int) (1000. * (1. / pView->wob_sens)),
266  wobble_timer_callback,
267  pView);
268 }
269 
270 void G4OpenGLXmViewer::wobble_timer_callback (XtPointer clientData,
271  XtIntervalId*)
272 {
273  G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*)clientData;
274  const G4Vector3D& up = pView->fVP.GetUpVector();
275  G4Vector3D third_axis = up.cross(pView->original_vp);
276  G4double pi_div_by_ten = pi / 10.0;
277  G4Vector3D d_up = 0.1 * (std::sin ((G4double)pView->frameNo * pi_div_by_ten * 2.)) * up;
278  G4Vector3D d_third_axis = 0.1 * (std::sin ((G4double)pView->frameNo * (pi_div_by_ten))) * third_axis;
279 
280  pView->fVP.SetViewAndLights (pView->original_vp + d_up + d_third_axis);
281 
282  pView->SetView ();
283  pView->ClearView ();
284  pView->DrawView ();
285 
286  if (pView->frameNo++ == 20) {
287  if (pView->wobble_timer) {
288  XtRemoveTimeOut (pView->wobble_timer);
289  pView->frameNo = 0;
290  pView->fVP.SetViewAndLights (pView->original_vp);
291  pView->SetView ();
292  pView->ClearView ();
293  pView->DrawView ();
294  }
295  } else {
296  pView->wobble_timer = XtAppAddTimeOut
297  (pView->app,
298  (long unsigned int) (1000. * (1. / pView->wob_sens)),
299  wobble_timer_callback,
300  pView);
301  }
302 }
303 
304 void G4OpenGLXmViewer::reset_callback (Widget w,
305  XtPointer,
306  XtPointer)
307 {
308 
309  G4OpenGLXmViewer* pView;
310 
311  XtVaGetValues (w,
312  XmNuserData, &pView,
313  NULL);
314 
315  pView->fVP.SetCurrentTargetPoint(G4Point3D());
316  pView->fVP.SetZoomFactor(1.0);
317  pView->fVP.SetDolly(0.0);
318  pView->SetView ();
319  pView->ClearView ();
320  pView->DrawView ();
321  pView->zoom_low = 0.1;
322  pView->zoom_high = 10.0;
323 
324 }
325 #endif