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, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2009
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | //////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MGeomPix
|
---|
28 | //
|
---|
29 | // This container stores the geometry (position) information of
|
---|
30 | // a single pixel together with the information about next neighbors.
|
---|
31 | //
|
---|
32 | //
|
---|
33 | // Version 1:
|
---|
34 | // ----------
|
---|
35 | // - first implementation
|
---|
36 | //
|
---|
37 | // Version 2:
|
---|
38 | // ----------
|
---|
39 | // - added fA
|
---|
40 | //
|
---|
41 | // Version 3:
|
---|
42 | // ----------
|
---|
43 | // - added fAidx
|
---|
44 | //
|
---|
45 | // Version 4:
|
---|
46 | // ----------
|
---|
47 | // - added fUserBits
|
---|
48 | //
|
---|
49 | // Version 5:
|
---|
50 | // ----------
|
---|
51 | // - now derives from MGeom to which some data members have been moved
|
---|
52 | //
|
---|
53 | ////////////////////////////////////////////////////////////////////////////
|
---|
54 | #include "MGeomPix.h"
|
---|
55 |
|
---|
56 | #include <TMath.h>
|
---|
57 | #include <TVector2.h>
|
---|
58 | #include <TVirtualPad.h>
|
---|
59 |
|
---|
60 | #include "MLog.h"
|
---|
61 | #include "MLogManip.h"
|
---|
62 |
|
---|
63 | #include "MGeomCam.h"
|
---|
64 | #include "MHexagon.h"
|
---|
65 |
|
---|
66 | ClassImp(MGeomPix);
|
---|
67 |
|
---|
68 | using namespace std;
|
---|
69 |
|
---|
70 | const Float_t MGeomPix::gsTan30 = TMath::Sqrt(3)/3; //TMath::Tan(TMath::DegToRad()*30);
|
---|
71 | const Float_t MGeomPix::gsTan60 = TMath::Sqrt(3); //TMath::Tan(TMath::DegToRad()*60);
|
---|
72 |
|
---|
73 | const Float_t MGeomPix::gsCos60 = 0.5; //TMath::Cos(TMath::DegToRad()*60);
|
---|
74 | const Float_t MGeomPix::gsSin60 = TMath::Sqrt(3)/2; //TMath::Sin(TMath::DegToRad()*60);
|
---|
75 |
|
---|
76 | // --------------------------------------------------------------------------
|
---|
77 | //
|
---|
78 | // Initializes one pixel
|
---|
79 | //
|
---|
80 | MGeomPix::MGeomPix(Float_t x, Float_t y, Float_t r, UInt_t s, UInt_t a) : MGeom(x, y, s, a)
|
---|
81 | {
|
---|
82 | SetD(r);
|
---|
83 | SetNeighbors();
|
---|
84 | }
|
---|
85 |
|
---|
86 | // --------------------------------------------------------------------------
|
---|
87 | //
|
---|
88 | // Print the geometry information of one pixel.
|
---|
89 | //
|
---|
90 | void MGeomPix::Print(Option_t *opt) const
|
---|
91 | {
|
---|
92 | MGeom::Print(opt);
|
---|
93 | gLog << " d=" << fD << "mm" << endl;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // ------------------------------------------------------------------------
|
---|
97 | //
|
---|
98 | // compute the distance of a point (px,py) to the Hexagon center in world
|
---|
99 | // coordinates. Return -1 if inside.
|
---|
100 | //
|
---|
101 | Float_t MGeomPix::DistanceToPrimitive(Float_t px, Float_t py) const
|
---|
102 | {
|
---|
103 | //FIXME: Rotation phi missing!
|
---|
104 |
|
---|
105 | //
|
---|
106 | // compute the distance of the Point to the center of the Hexagon
|
---|
107 | //
|
---|
108 | //const Double_t dx = px-fX;
|
---|
109 | //const Double_t dy = py-fY;
|
---|
110 |
|
---|
111 | TVector2 v(px-fX, py-fY);
|
---|
112 | // FIXME: fPhi or -fPhi?
|
---|
113 | // v = v.Rotate(-fPhi); // FIXME: Replace with a precalculates sin/cos vector
|
---|
114 |
|
---|
115 | const Double_t dx = v.X();
|
---|
116 | const Double_t dy = v.Y();
|
---|
117 |
|
---|
118 | const Double_t disthex = TMath::Sqrt(dx*dx + dy*dy);
|
---|
119 |
|
---|
120 | //
|
---|
121 | // Now check if point is outside of hexagon; just check x coordinate
|
---|
122 | // in three coordinate systems: the default one, in which two sides of
|
---|
123 | // the hexagon are paralel to the y axis (see camera displays) and two
|
---|
124 | // more, rotated with respect to that one by +- 60 degrees.
|
---|
125 | //
|
---|
126 |
|
---|
127 | if (TMath::Abs(dx) > fD/2)
|
---|
128 | return disthex;
|
---|
129 |
|
---|
130 | const Double_t dx2 = dx*gsCos60 + dy*gsSin60;
|
---|
131 |
|
---|
132 | if (TMath::Abs(dx2) > fD/2)
|
---|
133 | return disthex;
|
---|
134 |
|
---|
135 | const Double_t dx3 = dx*gsCos60 - dy*gsSin60;
|
---|
136 |
|
---|
137 | if (TMath::Abs(dx3) > fD/2)
|
---|
138 | return disthex;
|
---|
139 |
|
---|
140 | return -1;
|
---|
141 | }
|
---|
142 |
|
---|
143 | // ------------------------------------------------------------------------
|
---|
144 | //
|
---|
145 | // Implementation of PaintPrimitive drwaing a hexagonal pixel
|
---|
146 | //
|
---|
147 | void MGeomPix::PaintPrimitive(const TAttLine &fill, const TAttFill &line, Double_t scalexy, Double_t scaled) const
|
---|
148 | {
|
---|
149 | MHexagon hex;
|
---|
150 |
|
---|
151 | fill.Copy(hex);
|
---|
152 | line.Copy(hex);
|
---|
153 |
|
---|
154 | hex.PaintHexagon(fX*scalexy, fY*scalexy, fD*scaled);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | // ------------------------------------------------------------------------
|
---|
159 | //
|
---|
160 | // compute the distance of a point (px,py) to the Hexagon center in
|
---|
161 | // MGeomPix coordinates. Return kTRUE if inside.
|
---|
162 | //
|
---|
163 | Bool_t MGeomPix::IsInside(Float_t px, Float_t py) const
|
---|
164 | {
|
---|
165 | //
|
---|
166 | // compute the distance of the Point to the center of the Hexagon
|
---|
167 | //
|
---|
168 | const Double_t dx = px-fX;
|
---|
169 |
|
---|
170 | //
|
---|
171 | // Now check if point is outside of hexagon; just check x coordinate
|
---|
172 | // in three coordinate systems: the default one, in which two sides of
|
---|
173 | // the hexagon are paralel to the y axis (see camera displays) and two
|
---|
174 | // more, rotated with respect to that one by +- 60 degrees.
|
---|
175 | //
|
---|
176 | if (TMath::Abs(dx)>fD/2)
|
---|
177 | return kFALSE;
|
---|
178 |
|
---|
179 | const Double_t dy = py-fY;
|
---|
180 |
|
---|
181 | const static Double_t cos60 = TMath::Cos(60/kRad2Deg);
|
---|
182 | const static Double_t sin60 = TMath::Sin(60/kRad2Deg);
|
---|
183 |
|
---|
184 | const Double_t dxc = dx*cos60;
|
---|
185 | const Double_t dys = dy*sin60;
|
---|
186 |
|
---|
187 | if (TMath::Abs(dxc + dys)>fD/2)
|
---|
188 | return kFALSE;
|
---|
189 |
|
---|
190 | if (TMath::Abs(dxc - dys)>fD/2)
|
---|
191 | return kFALSE;
|
---|
192 |
|
---|
193 | return kTRUE;
|
---|
194 | }
|
---|
195 |
|
---|
196 | // ==============================================================================
|
---|
197 | /*
|
---|
198 | #include <TOrdCollection.h>
|
---|
199 | #include "MMath.h"
|
---|
200 | #include "../mgui/MHexagon.h" // MHexagon::fgDx
|
---|
201 |
|
---|
202 | // ------------------------------------------------------------------------
|
---|
203 | //
|
---|
204 | // Small helper class to allow fast sorting of TVector2 by angle
|
---|
205 | //
|
---|
206 | class HVector2 : public TVector2
|
---|
207 | {
|
---|
208 | Double_t fAngle;
|
---|
209 | public:
|
---|
210 | HVector2() : TVector2() { }
|
---|
211 | HVector2(const TVector2 &v) : TVector2(v) { }
|
---|
212 | HVector2(Double_t x, Double_t y) : TVector2 (x, y) { }
|
---|
213 | void CalcAngle() { fAngle = Phi(); }
|
---|
214 | Bool_t IsSortable() const { return kTRUE; }
|
---|
215 | Int_t Compare(const TObject *obj) const { return ((HVector2*)obj)->fAngle>fAngle ? 1 : -1; }
|
---|
216 | Double_t GetAngle() const { return fAngle; }
|
---|
217 | };
|
---|
218 |
|
---|
219 | // ------------------------------------------------------------------------
|
---|
220 | //
|
---|
221 | // Calculate the edge points of the intersection area of two hexagons.
|
---|
222 | // The points are added as TVector2 to the TOrdCollection.
|
---|
223 | // The user is responsible of delete the objects.
|
---|
224 | //
|
---|
225 | void MGeomPix::GetIntersectionBorder(TOrdCollection &col, const MGeomPix &hex) const
|
---|
226 | {
|
---|
227 | // if (fPhi!=0)
|
---|
228 | // {
|
---|
229 | // gLog << warn << "MGeomPix::GetIntersectionBorder: Only phi=0 supported yet." << endl;
|
---|
230 | // return;
|
---|
231 | // }
|
---|
232 |
|
---|
233 | Bool_t inside0[6], inside1[6];
|
---|
234 |
|
---|
235 | HVector2 v0[6];
|
---|
236 | HVector2 v1[6];
|
---|
237 |
|
---|
238 | Int_t cnt0=0;
|
---|
239 | Int_t cnt1=0;
|
---|
240 |
|
---|
241 | // Calculate teh edges of each hexagon and whether this edge
|
---|
242 | // is inside the other hexgon or not
|
---|
243 | for (int i=0; i<6; i++)
|
---|
244 | {
|
---|
245 | const Double_t x0 = fX+MHexagon::fgDx[i]*fD;
|
---|
246 | const Double_t y0 = fY+MHexagon::fgDy[i]*fD;
|
---|
247 |
|
---|
248 | const Double_t x1 = hex.fX+MHexagon::fgDx[i]*hex.fD;
|
---|
249 | const Double_t y1 = hex.fY+MHexagon::fgDy[i]*hex.fD;
|
---|
250 |
|
---|
251 | v0[i].Set(x0, y0);
|
---|
252 | v1[i].Set(x1, y1);
|
---|
253 |
|
---|
254 | inside0[i] = hex.DistanceToPrimitive(x0, y0) < 0;
|
---|
255 | inside1[i] = DistanceToPrimitive(x1, y1) < 0;
|
---|
256 |
|
---|
257 | if (inside0[i])
|
---|
258 | {
|
---|
259 | col.Add(new HVector2(v0[i]));
|
---|
260 | cnt0++;
|
---|
261 | }
|
---|
262 | if (inside1[i])
|
---|
263 | {
|
---|
264 | col.Add(new HVector2(v1[i]));
|
---|
265 | cnt1++;
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | if (cnt0==0 || cnt1==0)
|
---|
270 | return;
|
---|
271 |
|
---|
272 | // No calculate which vorder lines intersect
|
---|
273 | Bool_t iscross0[6], iscross1[6];
|
---|
274 | for (int i=0; i<6; i++)
|
---|
275 | {
|
---|
276 | iscross0[i] = (inside0[i] && !inside0[(i+1)%6]) || (!inside0[i] && inside0[(i+1)%6]);
|
---|
277 | iscross1[i] = (inside1[i] && !inside1[(i+1)%6]) || (!inside1[i] && inside1[(i+1)%6]);
|
---|
278 | }
|
---|
279 |
|
---|
280 | // Calculate the border points of our intersection area
|
---|
281 | for (int i=0; i<6; i++)
|
---|
282 | {
|
---|
283 | // Skip non intersecting lines
|
---|
284 | if (!iscross0[i])
|
---|
285 | continue;
|
---|
286 |
|
---|
287 | for (int j=0; j<6; j++)
|
---|
288 | {
|
---|
289 | // Skip non intersecting lines
|
---|
290 | if (!iscross1[j])
|
---|
291 | continue;
|
---|
292 |
|
---|
293 | const TVector2 p = MMath::GetIntersectionPoint(v0[i], v0[(i+1)%6], v1[j], v1[(j+1)%6]);
|
---|
294 | if (hex.DistanceToPrimitive(p.X(), p.Y())<1e-9)
|
---|
295 | col.Add(new HVector2(p));
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | // ------------------------------------------------------------------------
|
---|
301 | //
|
---|
302 | // Calculate the overlapping area of the two hexagons.
|
---|
303 | //
|
---|
304 | Double_t MGeomPix::CalcOverlapArea(const MGeomPix &cam) const
|
---|
305 | {
|
---|
306 | // if (fPhi!=0)
|
---|
307 | // {
|
---|
308 | // gLog << warn << "MGeomPix::CalcOverlapArea: Only phi=0 supported yet." << endl;
|
---|
309 | // return -1;
|
---|
310 | // }
|
---|
311 |
|
---|
312 | TOrdCollection col;
|
---|
313 | col.SetOwner();
|
---|
314 |
|
---|
315 | GetIntersectionBorder(col, cam);
|
---|
316 |
|
---|
317 | // Check if there is an intersection to proceed with
|
---|
318 | const Int_t n = col.GetEntries();
|
---|
319 | if (n==0)
|
---|
320 | return 0;
|
---|
321 |
|
---|
322 | // Calculate the center of gravity
|
---|
323 | TVector2 cog;
|
---|
324 |
|
---|
325 | TIter Next(&col);
|
---|
326 | HVector2 *v=0;
|
---|
327 | while ((v=(HVector2*)Next()))
|
---|
328 | cog += *v;
|
---|
329 | cog /= n;
|
---|
330 |
|
---|
331 | // Shift the figure to its center-og-gravity and
|
---|
332 | // calculate the angle of the connection line between the
|
---|
333 | // border points of our intersesction area and its cog
|
---|
334 | Next.Reset();
|
---|
335 | while ((v=(HVector2*)Next()))
|
---|
336 | {
|
---|
337 | *v -= cog;
|
---|
338 | v->CalcAngle();
|
---|
339 | }
|
---|
340 |
|
---|
341 | // Sort these points by this angle
|
---|
342 | col.Sort();
|
---|
343 |
|
---|
344 | // Now sum up the area of all the triangles between two
|
---|
345 | // following points and the cog.
|
---|
346 | Double_t A = 0;
|
---|
347 | for (int i=0; i<n; i++)
|
---|
348 | {
|
---|
349 | // Vectors from cog to two nearby border points
|
---|
350 | const HVector2 *v1 = (HVector2*)col.At(i);
|
---|
351 | const HVector2 *v2 = (HVector2*)col.At((i+1)%n);
|
---|
352 |
|
---|
353 | // Angle between both vectors
|
---|
354 | const Double_t a = fmod(v1->GetAngle()-v2->GetAngle()+TMath::TwoPi(), TMath::TwoPi());
|
---|
355 |
|
---|
356 | // Length of both vectors
|
---|
357 | const Double_t d1 = v1->Mod();
|
---|
358 | const Double_t d2 = v2->Mod();
|
---|
359 |
|
---|
360 | A += d1*d2/2*sin(a);
|
---|
361 | }
|
---|
362 | return A;
|
---|
363 | }
|
---|
364 | */
|
---|