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

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