Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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
26Automatically rotates GL camera.
27
28W's are angular velocities.
29 - ATheta -- Theta amplitude in units of Pi/2.
30 - ADolly -- In/out amplitude in units of initial distance.
31
32Can also save images automatically.
33
34fGUIOutMode is used internally between TGLAutoRotator and TGLViewerEditor,
35allowed values are:
36 1. animated gif
37 2. a series of png images
38*/
39
40
41////////////////////////////////////////////////////////////////////////////////
42/// Constructor.
43
45 fViewer(v), fCamera(nullptr),
46 fTimer(new TTimer), fWatch(new TStopwatch),
48 fDeltaPhi(0.005),
49 fDt (0.01),
50 fWPhi (0.40),
51 fWTheta(0.15), fATheta(0.5),
52 fWDolly(0.30), fADolly(0.4),
55 fImageGUIBaseName("animation"), fImageGUIOutMode(1)
56{
57 fTimer->Connect("Timeout()", "TGLAutoRotator", this, "Timeout()");
58}
59
60////////////////////////////////////////////////////////////////////////////////
61/// Destructor.
62
64{
65 delete fWatch;
66 delete fTimer;
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Set time between two redraws in seconds.
71/// Range: 0.001 -> 1.
72
74{
75 fDt = TMath::Range(0.01, 1.0, dt);
76 if (fTimerRunning)
77 {
78 fTimer->SetTime(TMath::Nint(1000*fDt));
79 fTimer->Reset();
80 }
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// Set relative amplitude of theta oscillation.
85/// Value range: 0.01 -> 1.
86
88{
89 a = TMath::Range(0.01, 1.0, a);
90 if (fTimerRunning)
91 {
93 }
94 fATheta = a;
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Set relative amplitude of forward/backward oscillation.
99/// Value range: 0.01 -> 1.
100
102{
103 a = TMath::Range(0.01, 1.0, a);
104 if (fTimerRunning)
105 {
107 }
108 fADolly = a;
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Start the auto-rotator.
113
115{
116 if (fTimerRunning)
117 {
118 Stop();
119 }
120
121 fCamera = & fViewer->CurrentCamera();
122
124 fDollyA0 = fADolly * fCamera->GetCamTrans().GetBaseVec(4).Mag();
125
127 fTimer->SetTime(TMath::Nint(1000*fDt));
128 fTimer->Reset();
129 fTimer->TurnOn();
130 fWatch->Start();
131}
132
133////////////////////////////////////////////////////////////////////////////////
134/// Stop the auto-rotator.
135
137{
138 if (fTimerRunning)
139 {
140 fWatch->Stop();
141 fTimer->TurnOff();
143 }
144}
145
146////////////////////////////////////////////////////////////////////////////////
147/// Called on every timer timeout. Moves / rotates the camera and optionally
148/// produces a screenshot.
149
151{
152 if (!fTimerRunning || gTQSender != fTimer)
153 {
154 Error("Timeout", "Not running or not called via timer.");
155 return;
156 }
157
158 using namespace TMath;
159
160 fWatch->Stop();
161 Double_t time = fWatch->RealTime();
162 fWatch->Continue();
163
164 if (fRotateScene) {
165 RotateScene();
166 } else {
167 Double_t delta_p = fWPhi*fDt;
168 Double_t delta_t = fThetaA0*fWTheta*Cos(fWTheta*time)*fDt;
169 Double_t delta_d = fDollyA0*fWDolly*Cos(fWDolly*time)*fDt;
170 Double_t th = fCamera->GetTheta();
171
172 if (th + delta_t > 3.0 || th + delta_t < 0.1416)
173 delta_t = 0;
174
175 fCamera->RotateRad(delta_t, delta_p);
176 fCamera->RefCamTrans().MoveLF(1, -delta_d);
177 }
178
179 fViewer->RequestDraw(TGLRnrCtx::kLODHigh);
180
181 if (fImageAutoSave)
182 {
183 TString filename;
184 if (fImageName.Contains("%"))
185 {
186 filename.Form(fImageName, fImageCount);
187 }
188 else
189 {
190 filename = fImageName;
191 }
192 fViewer->SavePicture(filename);
193 ++fImageCount;
194 }
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// Start saving into animated gif. The provided name will be used as it is,
199/// so make sure to end it with '.gif+'.
200/// Use convert tool from ImageMagic if you want to set a different delay or
201/// enable looping.
202
204{
205 if ( ! filename.Contains(".gif+"))
206 {
207 Error("StartImageAutoSaveAnimatedGif", "Name should end with '.gif+'. Not starting.");
208 return;
209 }
210
211 fImageName = filename;
212 fImageCount = 0;
214}
215
216////////////////////////////////////////////////////////////////////////////////
217/// Start saving into a set of images. The provided name will be used as a
218/// format to insert additional image sequence number so it should include
219/// an '%' character. A good name would be something like:
220/// "image-%04d.png"
221/// On GNU/Linux use mencoder and/or ffmpeg to bundle images into a movie.
222
224{
225 if ( ! filename.Contains("%"))
226 {
227 Error("StartImageAutoSave", "Name should include a '%%' character, like 'image-%%05d.png'. Not starting.");
228 return;
229 }
230
231 fImageName = filename;
232 fImageCount = 0;
234}
235
236////////////////////////////////////////////////////////////////////////////////
237/// Stops automatic saving of images.
238
243
244////////////////////////////////////////////////////////////////////////////////
245/// Set output mode for GUI operation:
246/// 1 - animated gif;
247/// 2 - a series of pngs
248
250{
251 if (m < 1 || m > 2)
252 {
253 Warning("SetImageGUIOutMode", "Invalid value, ignoring");
254 return;
255 }
257}
258
259////////////////////////////////////////////////////////////////////////////////
260/// Start auto-saving images as set-up via GUI.
261
263{
264 if (fImageGUIOutMode == 1)
265 {
266 TString name = fImageGUIBaseName + ".gif+";
268 }
269 else if (fImageGUIOutMode == 2)
270 {
271 TString name = fImageGUIBaseName + "-%05d.png";
273 }
274 else
275 {
276 Warning("StartImageAutoSaveWithGUISettings", "Unsupported mode '%d'.", fImageGUIOutMode);
277 }
278}
279
280////////////////////////////////////////////////////////////////////////////////
281///"Scene rotation": either find a special object,
282///which will be an axis of rotation (it's Z actually)
283///or use a "global" Z axis.
284
286{
287 TGLViewer::SceneInfoList_t & scenes = fViewer->fScenes;
288 TGLViewer::SceneInfoList_i sceneIter = scenes.begin();
289
290 for (; sceneIter != scenes.end(); ++sceneIter) {
291 TGLScene::TSceneInfo *sceneInfo = dynamic_cast<TGLScene::TSceneInfo *>(*sceneIter);
292 if (sceneInfo) {
293 TGLPhysicalShape *axisShape = nullptr;
294 TGLScene::ShapeVec_i shapeIter = sceneInfo->fShapesOfInterest.begin();
295 for (; shapeIter != sceneInfo->fShapesOfInterest.end(); ++shapeIter) {
296 TGLPhysicalShape * const testShape = const_cast<TGLPhysicalShape *>(*shapeIter);
297 if (testShape && testShape->GetLogical()->ID()->TestBit(13)) {
298 axisShape = testShape;
299 break;
300 }
301 }
302
303 TGLVector3 axis;
304 TGLVertex3 center;
305
306 if (!axisShape) {
307 const TGLBoundingBox &bbox = sceneInfo->GetTransformedBBox();
308 axis = bbox.Axis(2);
309 center = bbox.Center();
310 } else {
311 axis = axisShape->BoundingBox().Axis(2);
312 center = axisShape->BoundingBox().Center();
313 }
314
315 shapeIter = sceneInfo->fShapesOfInterest.begin();
316 for (; shapeIter != sceneInfo->fShapesOfInterest.end(); ++shapeIter) {
317 if (TGLPhysicalShape * const shape = const_cast<TGLPhysicalShape *>(*shapeIter))
318 shape->Rotate(center, axis, fDeltaPhi);
319 }
320 }
321 }
322}
#define a(i)
Definition RSha256.hxx:99
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
char name[80]
Definition TGX11.cxx:148
externvoid * gTQSender
Definition TQObject.h:46
void Start()
Start the auto-rotator.
void RotateScene()
"Scene rotation": either find a special object, which will be an axis of rotation (it's Z actually) o...
void SetATheta(Double_t a)
Set relative amplitude of theta oscillation.
TGLAutoRotator(const TGLAutoRotator &)=delete
void Stop()
Stop the auto-rotator.
TGLViewer * fViewer
void SetImageGUIOutMode(Int_t m)
Set output mode for GUI operation: 1 - animated gif; 2 - a series of pngs.
TString fImageGUIBaseName
TStopwatch * fWatch
void StartImageAutoSaveWithGUISettings()
Start auto-saving images as set-up via GUI.
void StopImageAutoSave()
Stops automatic saving of images.
void StartImageAutoSaveAnimatedGif(const TString &filename)
Start saving into animated gif.
TGLCamera * fCamera
void SetDt(Double_t dt)
Set time between two redraws in seconds.
void StartImageAutoSave(const TString &filename)
Start saving into a set of images.
~TGLAutoRotator() override
Destructor.
void SetADolly(Double_t a)
Set relative amplitude of forward/backward oscillation.
void Timeout()
Called on every timer timeout.
Concrete class describing an orientated (free) or axis aligned box of 8 vertices.
TGLVertex3 Center() const
const TGLVector3 & Axis(UInt_t i, Bool_t normalised=kTRUE) const
TObject * ID() const
Concrete physical shape - a GL drawable.
const TGLBoundingBox & BoundingBox() const
const TGLLogicalShape * GetLogical() const
const TGLBoundingBox & GetTransformedBBox()
ShapeVec_t fShapesOfInterest
Definition TGLScene.h:88
ShapeVec_t::iterator ShapeVec_i
Definition TGLScene.h:73
3 component (x/y/z) vector class.
Definition TGLUtil.h:248
3 component (x/y/z) vertex class.
Definition TGLUtil.h:84
std::list< TGLSceneInfo * > SceneInfoList_t
SceneInfoList_t::iterator SceneInfoList_i
Base GL viewer object - used by both standalone and embedded (in pad) GL.
Definition TGLViewer.h:55
Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1084
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1098
Stopwatch class.
Definition TStopwatch.h:28
Basic string class.
Definition TString.h:138
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2363
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:641
Handles synchronous and a-synchronous timer events.
Definition TTimer.h:51
TMath.
Definition TMathBase.h:35
Int_t Nint(T x)
Round to nearest integer. Rounds half integers to the nearest even integer.
Definition TMath.h:704
constexpr Double_t PiOver2()
Definition TMath.h:54
Short_t Range(Short_t lb, Short_t ub, Short_t x)
Returns x if lb < x < up, lb if x < lb and ub if x > ub.
Definition TMathBase.h:301
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:605
TMarker m
Definition textangle.C:8