Logo ROOT  
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 
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";
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 }
TGLViewerBase::SceneInfoList_i
SceneInfoList_t::iterator SceneInfoList_i
Definition: TGLViewerBase.h:44
m
auto * m
Definition: textangle.C:8
TGLAutoRotator::Start
void Start()
Start the auto-rotator.
Definition: TGLAutoRotator.cxx:115
TGLAutoRotator::StopImageAutoSave
void StopImageAutoSave()
Stops automatic saving of images.
Definition: TGLAutoRotator.cxx:240
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TGLCamera.h
TObject::TestBit
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
TStopwatch::Continue
void Continue()
Resume a stopped stopwatch.
Definition: TStopwatch.cxx:93
TGLAutoRotator
Definition: TGLAutoRotator.h:23
TGLCamera::RotateRad
virtual Bool_t RotateRad(Double_t hRotate, Double_t vRotate)
Rotate camera around center.
Definition: TGLCamera.cxx:927
TGLAutoRotator::fWatch
TStopwatch * fWatch
Definition: TGLAutoRotator.h:33
TGLAutoRotator::fThetaA0
Double_t fThetaA0
Definition: TGLAutoRotator.h:44
TGLAutoRotator::fWDolly
Double_t fWDolly
Definition: TGLAutoRotator.h:42
TGLPhysicalShape.h
TGLScene::TSceneInfo
Definition: TGLScene.h:78
TMath::Cos
Double_t Cos(Double_t)
Definition: TMath.h:643
TGLAutoRotator::Stop
void Stop()
Stop the auto-rotator.
Definition: TGLAutoRotator.cxx:137
TGLRnrCtx::kLODHigh
@ kLODHigh
Definition: TGLRnrCtx.h:67
TGLAutoRotator::~TGLAutoRotator
virtual ~TGLAutoRotator()
Destructor.
Definition: TGLAutoRotator.cxx:64
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TGLCamera::GetTheta
Double_t GetTheta() const
Get angle between camera up axis.
Definition: TGLCamera.cxx:882
TGLAutoRotator::SetATheta
void SetATheta(Double_t a)
Set relative amplitude of theta oscillation.
Definition: TGLAutoRotator.cxx:88
TGLAutoRotator::fImageName
TString fImageName
Definition: TGLAutoRotator.h:47
TStopwatch.h
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TGLAutoRotator::SetDt
void SetDt(Double_t dt)
Set time between two redraws in seconds.
Definition: TGLAutoRotator.cxx:74
TGLAutoRotator::StartImageAutoSaveAnimatedGif
void StartImageAutoSaveAnimatedGif(const TString &filename)
Start saving into animated gif.
Definition: TGLAutoRotator.cxx:204
TGLMatrix::GetBaseVec
TGLVector3 GetBaseVec(Int_t b) const
Definition: TGLUtil.h:754
gTQSender
R__EXTERN void * gTQSender
Definition: TQObject.h:44
TGLViewer::CurrentCamera
TGLCamera & CurrentCamera() const
Definition: TGLViewer.h:267
TMath::PiOver2
constexpr Double_t PiOver2()
Definition: TMath.h:57
TGLLogicalShape::ID
TObject * ID() const
Definition: TGLLogicalShape.h:82
TGLCamera::GetCamTrans
const TGLMatrix & GetCamTrans() const
Definition: TGLCamera.h:173
TGLBoundingBox::Center
TGLVertex3 Center() const
Definition: TGLBoundingBox.h:174
TString::Contains
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
TTimer::TurnOff
virtual void TurnOff()
Remove timer from system timer list.
Definition: TTimer.cxx:229
TGLViewer::SavePicture
Bool_t SavePicture()
Save current image using the default file name which can be set via SetPictureFileName() and defaults...
Definition: TGLViewer.cxx:782
TGLAutoRotator::fDollyA0
Double_t fDollyA0
Definition: TGLAutoRotator.h:44
TTimer.h
TString
Definition: TString.h:136
TGLAutoRotator::SetADolly
void SetADolly(Double_t a)
Set relative amplitude of forward/backward oscillation.
Definition: TGLAutoRotator.cxx:102
TGLBoundingBox
Definition: TGLBoundingBox.h:32
v
@ v
Definition: rootcling_impl.cxx:3635
TGLAutoRotator::fTimer
TTimer * fTimer
Definition: TGLAutoRotator.h:32
TGLAutoRotator::StartImageAutoSave
void StartImageAutoSave(const TString &filename)
Start saving into a set of images.
Definition: TGLAutoRotator.cxx:224
TGLAutoRotator::Timeout
void Timeout()
Called on every timer timeout.
Definition: TGLAutoRotator.cxx:151
TGLAutoRotator::fImageGUIBaseName
TString fImageGUIBaseName
Definition: TGLAutoRotator.h:51
TGLAutoRotator::TGLAutoRotator
TGLAutoRotator(const TGLAutoRotator &)
TGLAutoRotator::fImageGUIOutMode
Int_t fImageGUIOutMode
Definition: TGLAutoRotator.h:52
TGLViewer::RequestDraw
void RequestDraw(Short_t LOD=TGLRnrCtx::kLODMed)
Post request for redraw of viewer at level of detail 'LOD' Request is directed via cross thread gVirt...
Definition: TGLViewer.cxx:437
TStopwatch::RealTime
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
TGLCamera::RefCamTrans
TGLMatrix & RefCamTrans()
Definition: TGLCamera.h:176
TGLAutoRotator::fTimerRunning
Bool_t fTimerRunning
Definition: TGLAutoRotator.h:45
TString::Form
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2289
TTimer
Definition: TTimer.h:51
TGLViewer
Definition: TGLViewer.h:51
TMath::Nint
Int_t Nint(T x)
Round to nearest integer. Rounds half integers to the nearest even integer.
Definition: TMath.h:713
TGLAutoRotator::fImageCount
Int_t fImageCount
Definition: TGLAutoRotator.h:48
TGLViewerBase::fScenes
SceneInfoList_t fScenes
Definition: TGLViewerBase.h:74
TGLVector3
Definition: TGLUtil.h:247
a
auto * a
Definition: textangle.C:12
TGLAutoRotator::StartImageAutoSaveWithGUISettings
void StartImageAutoSaveWithGUISettings()
Start auto-saving images as set-up via GUI.
Definition: TGLAutoRotator.cxx:263
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TGLMatrix::MoveLF
void MoveLF(Int_t ai, Double_t amount)
Translate in local frame.
Definition: TGLUtil.cxx:813
TGLAutoRotator.h
TGLAutoRotator::fADolly
Double_t fADolly
Definition: TGLAutoRotator.h:42
TStopwatch::Start
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
TGLPhysicalShape::GetLogical
const TGLLogicalShape * GetLogical() const
Definition: TGLPhysicalShape.h:96
TTimer::SetTime
void SetTime(Long_t milliSec)
Definition: TTimer.h:96
TGLViewer.h
TGLViewerBase::SceneInfoList_t
std::list< TGLSceneInfo * > SceneInfoList_t
Definition: TGLViewerBase.h:43
TGLScene::TSceneInfo::fShapesOfInterest
ShapeVec_t fShapesOfInterest
Definition: TGLScene.h:88
TGLVector3::Mag
Double_t Mag() const
Definition: TGLUtil.h:298
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:876
TMath::Range
Short_t Range(Short_t lb, Short_t ub, Short_t x)
Definition: TMathBase.h:244
TGLAutoRotator::fCamera
TGLCamera * fCamera
Definition: TGLAutoRotator.h:31
Double_t
double Double_t
Definition: RtypesCore.h:59
TGLPhysicalShape::BoundingBox
const TGLBoundingBox & BoundingBox() const
Definition: TGLPhysicalShape.h:88
TGLAutoRotator::fWTheta
Double_t fWTheta
Definition: TGLAutoRotator.h:41
TQObject::Connect
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:864
TGLAutoRotator::SetImageGUIOutMode
void SetImageGUIOutMode(Int_t m)
Set output mode for GUI operation: 1 - animated gif; 2 - a series of pngs.
Definition: TGLAutoRotator.cxx:250
TGLScene::ShapeVec_i
ShapeVec_t::iterator ShapeVec_i
Definition: TGLScene.h:73
TGLSceneInfo::GetTransformedBBox
const TGLBoundingBox & GetTransformedBBox()
Definition: TGLSceneInfo.h:92
TGLLogicalShape.h
TStopwatch
Definition: TStopwatch.h:28
TGLAutoRotator::RotateScene
void RotateScene()
"Scene rotation": either find a special object, which will be an axis of rotation (it's Z actually) o...
Definition: TGLAutoRotator.cxx:286
TGLAutoRotator::fImageAutoSave
Bool_t fImageAutoSave
Definition: TGLAutoRotator.h:49
TGLBoundingBox::Axis
const TGLVector3 & Axis(UInt_t i, Bool_t normalised=kTRUE) const
Definition: TGLBoundingBox.h:183
name
char name[80]
Definition: TGX11.cxx:110
TGLScene.h
TGLAutoRotator::fWPhi
Double_t fWPhi
Definition: TGLAutoRotator.h:40
TGLPhysicalShape
Definition: TGLPhysicalShape.h:31
TGLAutoRotator::fDeltaPhi
Double_t fDeltaPhi
Definition: TGLAutoRotator.h:37
TTimer::TurnOn
virtual void TurnOn()
Add the timer to the system timer list.
Definition: TTimer.cxx:241
TStopwatch::Stop
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
TGLAutoRotator::fATheta
Double_t fATheta
Definition: TGLAutoRotator.h:41
TGLAutoRotator::fRotateScene
Bool_t fRotateScene
Definition: TGLAutoRotator.h:35
TTimer::Reset
void Reset()
Reset the timer.
Definition: TTimer.cxx:157
TGLAutoRotator::fDt
Double_t fDt
Definition: TGLAutoRotator.h:39
TGLVertex3
Definition: TGLUtil.h:83
TGLAutoRotator::fViewer
TGLViewer * fViewer
Definition: TGLAutoRotator.h:30
TMath
TMath.
Definition: TMathBase.h:35
TMath.h
int