source: trunk/Mars/mtools/MagicCivilization.cc@ 17685

Last change on this file since 17685 was 9369, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 8.0 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of CheObs, the Modular Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appears in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 7/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: CheObs Software Development, 2000-2009
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MagicCivilization
28// ---------
29//
30// Tool to visualize Next Neighbours.
31//
32// Start the show by:
33// MagicCivilization show;
34//
35// Use the following keys:
36// -----------------------
37//
38// * Space:
39// Toggle between auto increment and manual increment
40//
41// * Right/Left:
42// Increment/Decrement pixel number by 1
43//
44// * Right/Left:
45// Increment/Decrement pixel number by 1
46//
47// * Up/Down:
48// Increment/Decrement pixel number by 10
49//
50// * PageUp/PageDown:
51// Increment/Decrement pixel number by 100
52//
53// * Home/End:
54// Jump to first/last pixel
55//
56////////////////////////////////////////////////////////////////////////////
57#include "MagicCivilization.h"
58
59#include <iostream>
60
61#include <KeySymbols.h>
62
63#include <TCanvas.h>
64#include <TRandom.h>
65#include <TInterpreter.h>
66
67#include "MH.h"
68
69#include "MGeomPix.h"
70#include "MGeomCamCT1.h"
71#include "MGeomCamMagic.h"
72
73ClassImp(MagicCivilization);
74
75using namespace std;
76
77void MagicCivilization::Free()
78{
79 if (!fGeomCam)
80 return;
81
82 delete fGeomCam;
83}
84
85void MagicCivilization::ChangeCamera()
86{
87 static Bool_t ct1=kFALSE;
88
89 cout << "Change to " << (ct1?"Magic":"CT1") << endl;
90
91 if (ct1)
92 SetNewCamera(new MGeomCamMagic);
93 else
94 SetNewCamera(new MGeomCamCT1);
95
96 ct1 = !ct1;
97
98 AppendPad();
99}
100
101void MagicCivilization::SetNewCamera(MGeomCam *geom)
102{
103 Free();
104
105 //
106 // Set new camera
107 //
108 fGeomCam = geom;
109
110 //
111 // create the hexagons of the display
112 //
113 fNumPixels = fGeomCam->GetNumPixels();
114 fRange = fGeomCam->GetMaxRadius();
115
116 fColors.Set(fNumPixels);
117}
118
119// ------------------------------------------------------------------------
120//
121// default constructor
122//
123MagicCivilization::MagicCivilization(Byte_t lim, UShort_t init)
124 : fTimer(this, 500, kTRUE), fGeomCam(NULL), fNumInit(init), fLimit(lim)
125{
126 SetNewCamera(new MGeomCamMagic);
127
128 //
129 // Make sure, that the object is destroyed when the canvas/pad is
130 // destroyed. Make also sure, that the interpreter doesn't try to
131 // delete it a second time.
132 //
133 SetBit(kCanDelete);
134 gInterpreter->DeleteGlobal(this);
135
136 Draw();
137
138 fTimer.TurnOn();
139}
140
141// ------------------------------------------------------------------------
142//
143// Destructor. Deletes TClonesArrays for hexagons and legend elements.
144//
145MagicCivilization::~MagicCivilization()
146{
147 fTimer.TurnOff();
148 Free();
149}
150
151// ------------------------------------------------------------------------
152//
153// This is called at any time the canvas should get repainted.
154// Here we maintain an aspect ratio of 5/4=1.15. This makes sure,
155// that the camera image doesn't get distorted by resizing the canvas.
156//
157void MagicCivilization::Paint(Option_t *opt)
158{
159 const Float_t r = fGeomCam->GetMaxRadius();
160
161 MH::SetPadRange(-r, -r, r, r*1.1);
162
163 TAttLine line;
164 TAttFill fill;
165
166 // FIXME:
167 for (UInt_t i=0; i<fNumPixels; i++)
168 {
169 const MGeom &pix = (*fGeomCam)[i];
170
171 fill.SetFillColor(fColors[i]);
172 pix.PaintPrimitive(line, fill);
173 }
174 /*
175 for (int i=0; i<6; i++)
176 fText[i]->Paint();
177 */
178}
179
180void MagicCivilization::Reset()
181{
182 if (fNumInit>=fNumPixels)
183 fNumInit = fNumPixels-1;
184 if (fNumInit<0)
185 fNumInit = 0;
186
187 if (fLimit<0)
188 fLimit=6;
189 if (fLimit>6)
190 fLimit=0;
191
192
193 TRandom rnd(0);
194 for (int i=0; i<fNumPixels; i++)
195 (*fGeomCam)[i].ResetBit(kUserBits);
196
197 for (int i=0; i<fNumInit; i++)
198 {
199 Int_t idx;
200
201 do idx = (Int_t)rnd.Uniform(fNumPixels);
202 while ((*fGeomCam)[idx].TestBit(kHasFlag));
203
204 if (idx>=fNumPixels)
205 cout << "!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
206
207 (*fGeomCam)[idx].SetBit(kHasFlag);
208 }
209
210 fAuto = kFALSE;
211 fStep = 0;
212
213 fColors.Reset();
214
215 Update();
216
217 gPad->Modified();
218 gPad->Update();
219}
220
221// ------------------------------------------------------------------------
222//
223// Call this function to draw the camera layout into your canvas.
224// Setup a drawing canvas. Add this object and all child objects
225// (hexagons, etc) to the current pad. If no pad exists a new one is
226// created.
227//
228void MagicCivilization::Draw(Option_t *option)
229{
230 //
231 // if no canvas is yet existing to draw into, create a new one
232 //
233 new TCanvas("MagicCivilization", "Magic Civilization", 0, 0, 800, 800);
234
235 gPad->SetBorderMode(0);
236 gPad->SetFillColor(22);
237
238 //
239 // Append this object, so that the aspect ratio is maintained
240 // (Paint-function is called)
241 //
242 AppendPad(option);
243
244 fCivilization.SetTextAlign(23); // centered/bottom
245#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
246 fCivilization.SetBit(kNoContextMenu|kCannotPick);
247#endif
248 fCivilization.Draw();
249
250 Reset();
251}
252
253void MagicCivilization::Update()
254{
255 TString txt = "Lim: ";
256 txt += (int)fLimit;
257 txt += " Init: ";
258 txt += fNumInit;
259 txt += " On: ";
260 txt += fNumCivilizations;
261 txt += " Step: ";
262 txt += fStep;
263
264 if (!fAuto)
265 txt += " (paused)";
266
267 fCivilization.SetText(0, fRange, txt);
268}
269
270
271
272// ------------------------------------------------------------------------
273//
274// Execute a mouse event on the camera
275//
276void MagicCivilization::ExecuteEvent(Int_t event, Int_t keycode, Int_t keysym)
277{
278 if (event!=kKeyPress)
279 return;
280
281 switch (keysym)
282 {
283 default:
284 return;
285
286 case kKey_Space:
287 if ((fNumCivilizations==0 || fNumCivilizations==fNumPixels) && !fAuto)
288 Reset();
289 fAuto = !fAuto;
290 Update();
291 gPad->Update();
292 return;
293
294 case kKey_Right:
295 fNumInit += 1;;
296 break;
297
298 case kKey_Left:
299 fNumInit -= 1;
300 break;
301
302 case kKey_Up:
303 fNumInit += 10;
304 break;
305
306 case kKey_Down:
307 fNumInit -= 10;
308 break;
309
310 case kKey_PageUp:
311 fNumInit += 100;
312 break;
313
314 case kKey_PageDown:
315 fNumInit -= 100;
316 break;
317
318 case kKey_Plus:
319 fLimit++;
320 break;
321
322 case kKey_Minus:
323 fLimit--;
324 break;
325 }
326
327 Reset();
328}
329
330Bool_t MagicCivilization::HandleTimer(TTimer *timer)
331{
332 if (!fAuto)
333 return kTRUE;
334
335 for (int i=0; i<fNumPixels; i++)
336 {
337 MGeom &pix = (*fGeomCam)[i];
338
339 Byte_t cnt=0;
340 for (int j=0; j<pix.GetNumNeighbors(); j++)
341 if ((*fGeomCam)[pix.GetNeighbor(j)].TestBit(kHasFlag))
342 cnt++;
343
344 cnt += (6-pix.GetNumNeighbors())*cnt/6;
345
346 if (cnt>fLimit)
347 pix.SetBit(kHasCreation);
348 }
349
350 fNumCivilizations = 0;
351 for (int i=0; i<fNumPixels; i++)
352 {
353 MGeom &pix = (*fGeomCam)[i];
354
355 if (pix.TestBit(kHasCreation))
356 {
357 pix.SetBit(kHasFlag);
358 fColors[i] = kBlack;
359 fNumCivilizations++;
360 }
361 else
362 {
363 pix.ResetBit(kHasFlag);
364 fColors[i] = kBackground;
365 }
366 pix.ResetBit(kHasCreation);
367 }
368
369 if (fNumCivilizations==0 || fNumCivilizations==fNumPixels)
370 fAuto = kFALSE;
371
372 fStep++;
373
374 Update();
375
376 gPad->Update();
377
378 return kTRUE;
379}
380
Note: See TracBrowser for help on using the repository browser.