source: trunk/MagicSoft/Mars/mgui/MHexagon.cc@ 3227

Last change on this file since 3227 was 3227, checked in by moralejo, 21 years ago
*** empty log message ***
File size: 9.3 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC 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 appear 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 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Harald Kornmayer 1/2001
20!
21! Copyright: MAGIC Software Development, 2000-2002
22!
23!
24\* ======================================================================== */
25
26//////////////////////////////////////////////////////////////////////////////
27// //
28// MHexagon //
29// //
30//////////////////////////////////////////////////////////////////////////////
31
32#include "MHexagon.h"
33
34#include <fstream>
35#include <iostream>
36
37#include <TVirtualPad.h> // gPad
38
39#include "MGeomPix.h" // GetX
40
41ClassImp(MHexagon);
42
43using namespace std;
44
45// ------------------------------------------------------------------------
46//
47// default constructor for MHexagon
48//
49MHexagon::MHexagon()
50{
51}
52
53// ------------------------------------------------------------------------
54//
55// normal constructor for MHexagon
56//
57MHexagon::MHexagon(Float_t x, Float_t y, Float_t d)
58: TAttLine(1, 1, 1), TAttFill(0, 1001), fX(x), fY(y), fD(d)
59{
60}
61
62// ------------------------------------------------------------------------
63//
64// normal constructor for MHexagon
65//
66MHexagon::MHexagon(const MGeomPix &pix)
67: TAttLine(1, 1, 1), TAttFill(0, 1001)
68{
69 fX = pix.GetX();
70 fY = pix.GetY();
71 fD = pix.GetD();
72}
73
74// ------------------------------------------------------------------------
75//
76// copy constructor for MHexagon
77//
78MHexagon::MHexagon(const MHexagon &hexagon) : TObject(hexagon), TAttLine(hexagon), TAttFill(hexagon)
79{
80 fX = hexagon.fX;
81 fY = hexagon.fY;
82 fD = hexagon.fD;
83}
84
85// ------------------------------------------------------------------------
86//
87// copy this hexagon to hexagon
88//
89void MHexagon::Copy(TObject &obj)
90#if ROOT_VERSION_CODE > ROOT_VERSION(3,04,01)
91const
92#endif
93{
94 MHexagon &hex = (MHexagon&) obj;
95
96 TObject::Copy(obj);
97 TAttLine::Copy(hex);
98 TAttFill::Copy(hex);
99
100 hex.fX = fX;
101 hex.fY = fY;
102 hex.fD = fD;
103}
104
105// ------------------------------------------------------------------------
106//
107// compute the distance of a point (px,py) to the Hexagon
108// this functions needed for graphical primitives, that
109// means without this function you are not able to interact
110// with the graphical primitive with the mouse!!!
111//
112// All calcutations are running in pixel coordinates
113//
114Int_t MHexagon::DistancetoPrimitive(Int_t px, Int_t py)
115{
116 //
117 // compute the distance of the Point to the center of the Hexagon
118 //
119 const Int_t pxhex = gPad->XtoAbsPixel(fX);
120 const Int_t pyhex = gPad->YtoAbsPixel(fY);
121
122 const Double_t x = TMath::Abs(px-pxhex);
123 const Double_t y = TMath::Abs(py-pyhex);
124
125 const Double_t disthex = TMath::Sqrt(x*x + y*y);
126
127 if (disthex==0)
128 return 0;
129
130 const Double_t cosa = x / disthex;
131 const Double_t sina = y / disthex;
132
133 //
134 // comput the distance of hexagon center to pixel border
135 //
136 const Double_t dx = fD/2 * cosa;
137 const Double_t dy = fD/2 * sina;
138
139 const Int_t pxborder = gPad->XtoAbsPixel(fX + dx);
140 const Int_t pyborder = gPad->YtoAbsPixel(fY + dy);
141
142 const Double_t bx = pxhex-pxborder;
143 const Double_t by = pyhex-pyborder;
144
145 const Double_t distborder = TMath::Sqrt(bx*bx + by*by);
146
147 //
148 // compute the distance from the border of Pixel
149 // here in the first implementation is just circle inside
150 //
151 return distborder < disthex ? (int)(disthex-distborder+1) : 0;
152}
153
154// ------------------------------------------------------------------------
155//
156// compute the distance of a point (px,py) to the Hexagon center in world
157// coordinates. Return -1 if inside.
158//
159Float_t MHexagon::DistanceToPrimitive(Float_t px, Float_t py)
160{
161 //
162 // compute the distance of the Point to the center of the Hexagon
163 //
164 const Double_t dx = px-fX;
165 const Double_t dy = py-fY;
166 const Double_t disthex = TMath::Sqrt(dx*dx + dy*dy);
167
168 //
169 // Now check if point is outside of hexagon; just check x coordinate
170 // in three coordinate systems: the default one, in which two sides of
171 // the hexagon are paralel to the y axis (see camera displays) and two
172 // more, rotated with respect to that one by +- 60 degrees. The values
173 // 0.5 and 0.866025403784438597 are the sine and cosine of 60 degrees.
174 //
175
176 if (TMath::Abs(dx) > fD/2.)
177 return disthex;
178
179 const Double_t dx2 = dx*0.5 + dy*0.866025403784438597;
180
181 if (TMath::Abs(dx2) > fD/2.)
182 return disthex;
183
184 const Double_t dx3 = dx*0.5 - dy*0.866025403784438597;
185
186 if (TMath::Abs(dx3) > fD/2.)
187 return disthex;
188
189 return -1;
190}
191
192/*
193Float_t MHexagon::DistanceToPrimitive(Float_t px, Float_t py)
194{
195 //
196 // compute the distance of the Point to the center of the Hexagon
197 //
198 const Double_t dx = px-fX;
199 const Double_t dy = py-fY;
200
201 const Double_t disthex = TMath::Sqrt(dx*dx + dy*dy);
202
203 //
204 // compute the distance from the border of Pixel
205 // here in the first implementation is just circle outside
206 //
207 return fD*0.5772 < disthex ? disthex-fD*0.5772 : -1;
208 //
209 // FIXME: this approximate method results in some photons being
210 // assigned to two or even three different pixels.
211 //
212}
213*/
214
215// ------------------------------------------------------------------------
216//
217// Draw this ellipse with new coordinate
218//
219void MHexagon::DrawHexagon(Float_t x, Float_t y, Float_t d)
220{
221 MHexagon *newhexagon = new MHexagon(x, y, d);
222
223 TAttLine::Copy(*newhexagon);
224 TAttFill::Copy(*newhexagon);
225
226 newhexagon->SetBit(kCanDelete);
227 newhexagon->AppendPad();
228}
229
230/*
231// ------------------------------------------------------------------------
232//
233// This is the first test of implementing a clickable interface
234// for one pixel
235//
236void MHexagon::ExecuteEvent(Int_t event, Int_t px, Int_t py)
237{
238 switch (event)
239 {
240 case kButton1Down:
241 cout << endl << "kButton1Down" << endl;
242 SetFillColor(2);
243 gPad->Modified();
244 break;
245
246 case kButton1Double:
247 SetFillColor(0);
248 gPad->Modified();
249 break;
250 // case kMouseEnter:
251 // printf ("\n Mouse inside object \n" ) ;
252 // break;
253 }
254}
255*/
256
257// ------------------------------------------------------------------------
258//
259// list this hexagon with its attributes
260//
261void MHexagon::ls(const Option_t *) const
262{
263 TROOT::IndentLevel();
264 Print();
265}
266
267// ------------------------------------------------------------------------
268//
269// paint this hexagon with its attribute
270//
271void MHexagon::Paint(Option_t *)
272{
273 PaintHexagon(fX, fY, fD);
274}
275
276// ------------------------------------------------------------------------
277//
278// draw this hexagon with the coordinates
279//
280void MHexagon::PaintHexagon(Float_t inX, Float_t inY, Float_t inD)
281{
282 const Int_t np = 6;
283
284 const Float_t dx[np+1] = { .5 , 0. , -.5 , -.5 , 0. , .5 , .5 };
285 const Float_t dy[np+1] = { .2886, .5772, .2886, -.2886, -.5772, -.2886, .2886 };
286
287 //
288 // calculate the positions of the pixel corners
289 //
290 Float_t x[np+1], y[np+1];
291 for (Int_t i=0; i<np+1; i++)
292 {
293 x[i] = inX + dx[i]*inD;
294 y[i] = inY + dy[i]*inD;
295 }
296
297 TAttLine::Modify(); // Change line attributes only if neccessary
298 TAttFill::Modify(); // Change fill attributes only if neccessary
299
300 //
301 // paint the pixel
302 //
303 if (GetFillColor())
304 gPad->PaintFillArea(np, x, y);
305
306 if (GetLineStyle())
307 gPad->PaintPolyLine(np+1, x, y);
308}
309
310// ------------------------------------------------------------------------
311//
312// print/dump this hexagon with its attributes
313//
314void MHexagon::Print(Option_t *) const
315{
316 cout << "MHexagon: ";
317 cout << "x=" << fX << "mm y=" << fY << "mm r=" << fD << "mm" << endl;
318
319 cout << " Line:";
320 cout << " Color=" << GetLineColor() << ",";
321 cout << " Style=" << GetLineStyle() << ",";
322 cout << " Width=" << GetLineWidth() << endl;
323 cout << " Fill:";
324 cout << " Color=" << GetFillColor() << ",";
325 cout << " Style=" << GetFillStyle() << endl;
326}
327
328// ------------------------------------------------------------------------
329//
330// Save primitive as a C++ statement(s) on output stream out
331//
332void MHexagon::SavePrimitive(ofstream &out, Option_t *)
333{
334 if (gROOT->ClassSaved(Class()))
335 out << " ";
336 else
337 out << " MHexagon *";
338
339 out << "hexagon = new MHexagon(" << fX << "," << fY << "," << fD << ");" << endl;
340
341 SaveFillAttributes(out, "hexagon");
342 SaveLineAttributes(out, "hexagon");
343
344 out << " hexagon->Draw();" << endl;
345}
346
Note: See TracBrowser for help on using the repository browser.