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

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