Logo ROOT   6.10/09
Reference Guide
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 
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// Constructor.
44 
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 
65 {
66  delete fWatch;
67  delete fTimer;
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Set time between two redraws in seconds.
72 /// Range: 0.001 -> 1.
73 
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 
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 
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 
116 {
117  if (fTimerRunning)
118  {
119  Stop();
120  }
121 
123 
126 
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 
138 {
139  if (fTimerRunning)
140  {
141  fWatch->Stop();
142  fTimer->TurnOff();
144  }
145 }
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// Called on every timer timeout. Moves / rotates the camera and optionally
149 /// produces a screenshot.
150 
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 
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 
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;
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;
235 }
236 
237 ////////////////////////////////////////////////////////////////////////////////
238 /// Stops automatic saving of images.
239 
241 {
243 }
244 
245 ////////////////////////////////////////////////////////////////////////////////
246 /// Set output mode for GUI operation:
247 /// 1 - animated gif;
248 /// 2 - a series of pngs
249 
251 {
252  if (m < 1 || m > 2)
253  {
254  Warning("SetImageGUIOutMode", "Invalid value, ignoring");
255  return;
256  }
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// Start auto-saving images as set-up via GUI.
262 
264 {
265  if (fImageGUIOutMode == 1)
266  {
267  TString name = fImageGUIBaseName + ".gif+";
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 
287 {
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 }
ShapeVec_t::iterator ShapeVec_i
Definition: TGLScene.h:75
TGLVertex3 Center() const
Double_t fADolly
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:110
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
void StopImageAutoSave()
Stops automatic saving of images.
void Reset()
Reset the timer.
Definition: TTimer.cxx:157
R__EXTERN void * gTQSender
Definition: TQObject.h:45
std::list< TGLSceneInfo * > SceneInfoList_t
Definition: TGLViewerBase.h:43
Double_t fThetaA0
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:159
TGLCamera * fCamera
void SetImageGUIOutMode(Int_t m)
Set output mode for GUI operation: 1 - animated gif; 2 - a series of pngs.
const TGLMatrix & GetCamTrans() const
Definition: TGLCamera.h:167
TString fImageName
Short_t Range(Short_t lb, Short_t ub, Short_t x)
Definition: TMathBase.h:232
int Int_t
Definition: RtypesCore.h:41
virtual Bool_t RotateRad(Double_t hRotate, Double_t vRotate)
Rotate camera around center.
Definition: TGLCamera.cxx:927
TArc * a
Definition: textangle.C:12
TString fImageGUIBaseName
Double_t fDollyA0
void Timeout()
Called on every timer timeout.
Concrete physical shape - a GL drawable.
TGLCamera & CurrentCamera() const
Definition: TGLViewer.h:267
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
void StartImageAutoSaveAnimatedGif(const TString &filename)
Start saving into animated gif.
3 component (x/y/z) vertex class.
Definition: TGLUtil.h:82
const TGLBoundingBox & BoundingBox() const
3 component (x/y/z) vector class.
Definition: TGLUtil.h:246
void Continue()
Resume a stopped stopwatch.
Definition: TStopwatch.cxx:93
void RotateScene()
"Scene rotation": either find a special object, which will be an axis of rotation (it&#39;s Z actually) o...
TGLMatrix & RefCamTrans()
Definition: TGLCamera.h:170
void StartImageAutoSave(const TString &filename)
Start saving into a set of images.
void SetADolly(Double_t a)
Set relative amplitude of forward/backward oscillation.
Double_t fWTheta
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot...
Definition: TQObject.cxx:867
Automatically rotates GL camera.
void RequestDraw(Short_t LOD=TGLRnrCtx::kLODMed)
Post request for redraw of viewer at level of detail &#39;LOD&#39; Request is directed via cross thread gVirt...
Definition: TGLViewer.cxx:436
Double_t Mag() const
Definition: TGLUtil.h:299
SVector< double, 2 > v
Definition: Dict.h:5
void SetATheta(Double_t a)
Set relative amplitude of theta oscillation.
TMarker * m
Definition: textangle.C:8
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:873
virtual void TurnOff()
Remove timer from system timer list.
Definition: TTimer.cxx:229
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
Double_t fATheta
const TGLBoundingBox & GetTransformedBBox()
Definition: TGLSceneInfo.h:91
void StartImageAutoSaveWithGUISettings()
Start auto-saving images as set-up via GUI.
Base GL viewer object - used by both standalone and embedded (in pad) GL.
Definition: TGLViewer.h:53
Double_t Cos(Double_t)
Definition: TMath.h:551
const Bool_t kFALSE
Definition: RtypesCore.h:92
Bool_t fTimerRunning
void Stop()
Stop the auto-rotator.
#define ClassImp(name)
Definition: Rtypes.h:336
double Double_t
Definition: RtypesCore.h:55
Double_t fDeltaPhi
TGLViewer * fViewer
Concrete class describing an orientated (free) or axis aligned box of 8 vertices. ...
void SetDt(Double_t dt)
Set time between two redraws in seconds.
TGLAutoRotator(const TGLAutoRotator &)
SceneInfoList_t fScenes
Definition: TGLViewerBase.h:74
Double_t GetTheta() const
Get angle between camera up axis.
Definition: TGLCamera.cxx:882
const TGLVector3 & Axis(UInt_t i, Bool_t normalised=kTRUE) const
virtual ~TGLAutoRotator()
Destructor.
SceneInfoList_t::iterator SceneInfoList_i
Definition: TGLViewerBase.h:44
void Start()
Start the auto-rotator.
virtual void TurnOn()
Add the timer to the system timer list.
Definition: TTimer.cxx:241
const Bool_t kTRUE
Definition: RtypesCore.h:91
Int_t Nint(T x)
Definition: TMath.h:607
TObject * ID() const
Int_t fImageGUIOutMode
Double_t fWDolly
constexpr Double_t PiOver2()
Definition: TMath.h:48
const TGLLogicalShape * GetLogical() const
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:859
void MoveLF(Int_t ai, Double_t amount)
Translate in local frame.
Definition: TGLUtil.cxx:841
Bool_t SavePicture()
Save current image using the default file name which can be set via SetPictureFileName() and defaults...
Definition: TGLViewer.cxx:781
TGLVector3 GetBaseVec(Int_t b) const
Definition: TGLUtil.h:753
Bool_t fImageAutoSave
TStopwatch * fWatch
void SetTime(Long_t milliSec)
Definition: TTimer.h:90
ShapeVec_t fShapesOfInterest
Definition: TGLScene.h:90
Stopwatch class.
Definition: TStopwatch.h:28