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

Last change on this file since 2812 was 2230, checked in by tbretz, 21 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>
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 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
167 const Double_t disthex = TMath::Sqrt(dx*dx + dy*dy);
168
169 //
170 // compute the distance from the border of Pixel
171 // here in the first implementation is just circle inside
172 //
173 return fD*0.5772 < disthex ? disthex-fD*0.5772 : -1;
174}
175
176// ------------------------------------------------------------------------
177//
178// Draw this ellipse with new coordinate
179//
180void MHexagon::DrawHexagon(Float_t x, Float_t y, Float_t d)
181{
182 MHexagon *newhexagon = new MHexagon(x, y, d);
183
184 TAttLine::Copy(*newhexagon);
185 TAttFill::Copy(*newhexagon);
186
187 newhexagon->SetBit(kCanDelete);
188 newhexagon->AppendPad();
189}
190
191/*
192// ------------------------------------------------------------------------
193//
194// This is the first test of implementing a clickable interface
195// for one pixel
196//
197void MHexagon::ExecuteEvent(Int_t event, Int_t px, Int_t py)
198{
199 switch (event)
200 {
201 case kButton1Down:
202 cout << endl << "kButton1Down" << endl;
203 SetFillColor(2);
204 gPad->Modified();
205 break;
206
207 case kButton1Double:
208 SetFillColor(0);
209 gPad->Modified();
210 break;
211 // case kMouseEnter:
212 // printf ("\n Mouse inside object \n" ) ;
213 // break;
214 }
215}
216*/
217
218// ------------------------------------------------------------------------
219//
220// list this hexagon with its attributes
221//
222void MHexagon::ls(const Option_t *) const
223{
224 TROOT::IndentLevel();
225 Print();
226}
227
228// ------------------------------------------------------------------------
229//
230// paint this hexagon with its attribute
231//
232void MHexagon::Paint(Option_t *)
233{
234 PaintHexagon(fX, fY, fD);
235}
236
237// ------------------------------------------------------------------------
238//
239// draw this hexagon with the coordinates
240//
241void MHexagon::PaintHexagon(Float_t inX, Float_t inY, Float_t inD)
242{
243 const Int_t np = 6;
244
245 const Float_t dx[np+1] = { .5 , 0. , -.5 , -.5 , 0. , .5 , .5 };
246 const Float_t dy[np+1] = { .2886, .5772, .2886, -.2886, -.5772, -.2886, .2886 };
247
248 //
249 // calculate the positions of the pixel corners
250 //
251 Float_t x[np+1], y[np+1];
252 for (Int_t i=0; i<np+1; i++)
253 {
254 x[i] = inX + dx[i]*inD;
255 y[i] = inY + dy[i]*inD;
256 }
257
258 TAttLine::Modify(); // Change line attributes only if neccessary
259 TAttFill::Modify(); // Change fill attributes only if neccessary
260
261 //
262 // paint the pixel
263 //
264 if (GetFillColor())
265 gPad->PaintFillArea(np, x, y);
266
267 if (GetLineStyle())
268 gPad->PaintPolyLine(np+1, x, y);
269}
270
271// ------------------------------------------------------------------------
272//
273// print/dump this hexagon with its attributes
274//
275void MHexagon::Print(Option_t *) const
276{
277 cout << "MHexagon: ";
278 cout << "x=" << fX << "mm y=" << fY << "mm r=" << fD << "mm" << endl;
279
280 cout << " Line:";
281 cout << " Color=" << GetLineColor() << ",";
282 cout << " Style=" << GetLineStyle() << ",";
283 cout << " Width=" << GetLineWidth() << endl;
284 cout << " Fill:";
285 cout << " Color=" << GetFillColor() << ",";
286 cout << " Style=" << GetFillStyle() << endl;
287}
288
289// ------------------------------------------------------------------------
290//
291// Save primitive as a C++ statement(s) on output stream out
292//
293void MHexagon::SavePrimitive(ofstream &out, Option_t *)
294{
295 if (gROOT->ClassSaved(Class()))
296 out << " ";
297 else
298 out << " MHexagon *";
299
300 out << "hexagon = new MHexagon(" << fX << "," << fY << "," << fD << ");" << endl;
301
302 SaveFillAttributes(out, "hexagon");
303 SaveLineAttributes(out, "hexagon");
304
305 out << " hexagon->Draw();" << endl;
306}
307
Note: See TracBrowser for help on using the repository browser.