Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGLAutoRotator.cxx
Go to the documentation of this file.
1 // @(#)root/eve:$Id$
2 // Author: Matevz Tadel 2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TGLAutoRotator.h"
13 
14 #include "TGLPhysicalShape.h"
15 #include "TGLLogicalShape.h"
16 #include "TGLViewer.h"
17 #include "TGLCamera.h"
18 #include "TGLScene.h"
19 
20 #include "TMath.h"
21 #include "TTimer.h"
22 #include "TStopwatch.h"
23 
24 /** \class TGLAutoRotator
25 \ingroup opengl
26 Automatically rotates GL camera.
27 
28 W's are angular velocities.
29  - ATheta -- Theta amplitude in units of Pi/2.
30  - ADolly -- In/out amplitude in units of initial distance.
31 
32 Can also save images automatically.
33 
34 fGUIOutMode is used internally between TGLAutoRotator and TGLViewerEditor,
35 allowed values are:
36  1. animated gif
37  2. a series of png images
38 */
39 
40 ClassImp(TGLAutoRotator);
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// Constructor.
44 
45 TGLAutoRotator::TGLAutoRotator(TGLViewer* v) :
46  fViewer(v), fCamera(0),
47  fTimer(new TTimer), fWatch(new TStopwatch),
48  fRotateScene(kFALSE),
49  fDeltaPhi(0.005),
50  fDt (0.01),
51  fWPhi (0.40),
52  fWTheta(0.15), fATheta(0.5),
53  fWDolly(0.30), fADolly(0.4),
54  fTimerRunning(kFALSE),
55  fImageCount(0), fImageAutoSave(kFALSE),
56  fImageGUIBaseName("animation"), fImageGUIOutMode(1)
57 {
58  fTimer->Connect("Timeout()", "TGLAutoRotator", this, "Timeout()");
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Destructor.
63 
64 TGLAutoRotator::~TGLAutoRotator()
65 {
66  delete fWatch;
67  delete fTimer;
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Set time between two redraws in seconds.
72 /// Range: 0.001 -> 1.
73 
74 void TGLAutoRotator::SetDt(Double_t dt)
75 {
76  fDt = TMath::Range(0.01, 1.0, dt);
77  if (fTimerRunning)
78  {
79  fTimer->SetTime(TMath::Nint(1000*fDt));
80  fTimer->Reset();
81  }
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Set relative amplitude of theta oscillation.
86 /// Value range: 0.01 -> 1.
87 
88 void TGLAutoRotator::SetATheta(Double_t a)
89 {
90  a = TMath::Range(0.01, 1.0, a);
91  if (fTimerRunning)
92  {
93  fThetaA0 = fThetaA0 * a / fATheta;
94  }
95  fATheta = a;
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Set relative amplitude of forward/backward oscillation.
100 /// Value range: 0.01 -> 1.
101 
102 void TGLAutoRotator::SetADolly(Double_t a)
103 {
104  a = TMath::Range(0.01, 1.0, a);
105  if (fTimerRunning)
106  {
107  fDollyA0 = fDollyA0 * a / fADolly;
108  }
109  fADolly = a;
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Start the auto-rotator.
114 
115 void TGLAutoRotator::Start()
116 {
117  if (fTimerRunning)
118  {
119  Stop();
120  }
121 
122  fCamera = & fViewer->CurrentCamera();
123 
124  fThetaA0 = fATheta * TMath::PiOver2();
125  fDollyA0 = fADolly * fCamera->GetCamTrans().GetBaseVec(4).Mag();
126 
127  fTimerRunning = kTRUE;
128  fTimer->SetTime(TMath::Nint(1000*fDt));
129  fTimer->Reset();
130  fTimer->TurnOn();
131  fWatch->Start();
132 }
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 /// Stop the auto-rotator.
136 
137 void TGLAutoRotator::Stop()
138 {
139  if (fTimerRunning)
140  {
141  fWatch->Stop();
142  fTimer->TurnOff();
143  fTimerRunning = kFALSE;
144  }
145 }
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// Called on every timer timeout. Moves / rotates the camera and optionally
149 /// produces a screenshot.
150 
151 void TGLAutoRotator::Timeout()
152 {
153  if (!fTimerRunning || gTQSender != fTimer)
154  {
155  Error("Timeout", "Not running or not called via timer.");
156  return;
157  }
158 
159  using namespace TMath;
160 
161  fWatch->Stop();
162  Double_t time = fWatch->RealTime();
163  fWatch->Continue();
164 
165  if (fRotateScene) {
166  RotateScene();
167  } else {
168  Double_t delta_p = fWPhi*fDt;
169  Double_t delta_t = fThetaA0*fWTheta*Cos(fWTheta*time)*fDt;
170  Double_t delta_d = fDollyA0*fWDolly*Cos(fWDolly*time)*fDt;
171  Double_t th = fCamera->GetTheta();
172 
173  if (th + delta_t > 3.0 || th + delta_t < 0.1416)
174  delta_t = 0;
175 
176  fCamera->RotateRad(delta_t, delta_p);
177  fCamera->RefCamTrans().MoveLF(1, -delta_d);
178  }
179 
180  fViewer->RequestDraw(TGLRnrCtx::kLODHigh);
181 
182  if (fImageAutoSave)
183  {
184  TString filename;
185  if (fImageName.Contains("%"))
186  {
187  filename.Form(fImageName, fImageCount);
188  }
189  else
190  {
191  filename = fImageName;
192  }
193  fViewer->SavePicture(filename);
194  ++fImageCount;
195  }
196 }
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// Start saving into animated gif. The provided name will be used as it is,
200 /// so make sure to end it with '.gif+'.
201 /// Use convert tool from ImageMagic if you want to set a different delay or
202 /// enable looping.
203 
204 void TGLAutoRotator::StartImageAutoSaveAnimatedGif(const TString& filename)
205 {
206  if ( ! filename.Contains(".gif+"))
207  {
208  Error("StartImageAutoSaveAnimatedGif", "Name should end with '.gif+'. Not starting.");
209  return;
210  }
211 
212  fImageName = filename;
213  fImageCount = 0;
214  fImageAutoSave = kTRUE;
215 }
216 
217 ////////////////////////////////////////////////////////////////////////////////
218 /// Start saving into a set of images. The provided name will be used as a
219 /// format to insert additional image sequence number so it should include
220 /// an '%' character. A good name would be something like:
221 /// "image-%04d.png"
222 /// On GNU/Linux use mencoder and/or ffmpeg to bundle images into a movie.
223 
224 void TGLAutoRotator::StartImageAutoSave(const TString& filename)
225 {
226  if ( ! filename.Contains("%"))
227  {
228  Error("StartImageAutoSave", "Name should include a '%%' character, like 'image-%%05d.png'. Not starting.");
229  return;
230  }
231 
232  fImageName = filename;
233  fImageCount = 0;
234  fImageAutoSave = kTRUE;
235 }
236 
237 ////////////////////////////////////////////////////////////////////////////////
238 /// Stops automatic saving of images.
239 
240 void TGLAutoRotator::StopImageAutoSave()
241 {
242  fImageAutoSave = kFALSE;
243 }
244 
245 ////////////////////////////////////////////////////////////////////////////////
246 /// Set output mode for GUI operation:
247 /// 1 - animated gif;
248 /// 2 - a series of pngs
249 
250 void TGLAutoRotator::SetImageGUIOutMode(Int_t m)
251 {
252  if (m < 1 || m > 2)
253  {
254  Warning("SetImageGUIOutMode", "Invalid value, ignoring");
255  return;
256  }
257  fImageGUIOutMode = m;
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// Start auto-saving images as set-up via GUI.
262 
263 void TGLAutoRotator::StartImageAutoSaveWithGUISettings()
264 {
265  if (fImageGUIOutMode == 1)
266  {
267  TString name = fImageGUIBaseName + ".gif+";
268  StartImageAutoSaveAnimatedGif(name);
269  }
270  else if (fImageGUIOutMode == 2)
271  {
272  TString name = fImageGUIBaseName + "-%05d.png";
273  StartImageAutoSave(name);
274  }
275  else
276  {
277  Warning("StartImageAutoSaveWithGUISettings", "Unsupported mode '%d'.", fImageGUIOutMode);
278  }
279 }
280 
281 ////////////////////////////////////////////////////////////////////////////////
282 ///"Scene rotation": either find a special object,
283 ///which will be an axis of rotation (it's Z actually)
284 ///or use a "global" Z axis.
285 
286 void TGLAutoRotator::RotateScene()
287 {
288  TGLViewer::SceneInfoList_t & scenes = fViewer->fScenes;
289  TGLViewer::SceneInfoList_i sceneIter = scenes.begin();
290 
291  for (; sceneIter != scenes.end(); ++sceneIter) {
292  TGLScene::TSceneInfo *sceneInfo = dynamic_cast<TGLScene::TSceneInfo *>(*sceneIter);
293  if (sceneInfo) {
294  TGLPhysicalShape *axisShape = 0;
295  TGLScene::ShapeVec_i shapeIter = sceneInfo->fShapesOfInterest.begin();
296  for (; shapeIter != sceneInfo->fShapesOfInterest.end(); ++shapeIter) {
297  TGLPhysicalShape * const testShape = const_cast<TGLPhysicalShape *>(*shapeIter);
298  if (testShape && testShape->GetLogical()->ID()->TestBit(13)) {
299  axisShape = testShape;
300  break;
301  }
302  }
303 
304  TGLVector3 axis;
305  TGLVertex3 center;
306 
307  if (!axisShape) {
308  const TGLBoundingBox &bbox = sceneInfo->GetTransformedBBox();
309  axis = bbox.Axis(2);
310  center = bbox.Center();
311  } else {
312  axis = axisShape->BoundingBox().Axis(2);
313  center = axisShape->BoundingBox().Center();
314  }
315 
316  shapeIter = sceneInfo->fShapesOfInterest.begin();
317  for (; shapeIter != sceneInfo->fShapesOfInterest.end(); ++shapeIter) {
318  if (TGLPhysicalShape * const shape = const_cast<TGLPhysicalShape *>(*shapeIter))
319  shape->Rotate(center, axis, fDeltaPhi);
320  }
321  }
322  }
323 }