Ignore:
Timestamp:
01/21/09 14:37:07 (16 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars/mgui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mgui/MHexagon.cc

    r8178 r9235  
    11/* ======================================================================== *\
    2 ! $Name: not supported by cvs2svn $:$Id: MHexagon.cc,v 1.30 2006-10-30 12:46:13 tbretz Exp $
     2! $Name: not supported by cvs2svn $:$Id: MHexagon.cc,v 1.31 2009-01-21 14:34:48 tbretz Exp $
    33! --------------------------------------------------------------------------
    44!
     
    2121!   Author(s): Harald Kornmayer 1/2001
    2222!
    23 !   Copyright: MAGIC Software Development, 2000-2006
     23!   Copyright: Software Development, 2000-2009
    2424!
    2525!
     
    2929//
    3030// MHexagon
     31//
     32// Class Version 2:
     33//  - added fPhi
    3134//
    3235//////////////////////////////////////////////////////////////////////////////
     
    4144#include <TOrdCollection.h> // TOrdCollection
    4245
     46#include "MLog.h"
     47#include "MLogManip.h"
     48
    4349#include "MMath.h"
    4450#include "MGeomPix.h"       // GetX
     
    6672//    normal constructor for MHexagon
    6773//
    68 MHexagon::MHexagon(Float_t x, Float_t y, Float_t d)
    69 : TAttLine(1, 1, 1), TAttFill(0, 1001), fX(x), fY(y), fD(d)
     74MHexagon::MHexagon(Float_t x, Float_t y, Float_t d, Float_t phi)
     75: TAttLine(1, 1, 1), TAttFill(0, 1001), fX(x), fY(y), fD(d), fPhi(phi)
    7076{
    7177}
     
    7682//
    7783MHexagon::MHexagon(const MGeomPix &pix)
    78 : TAttLine(1, 1, 1), TAttFill(0, 1001)
     84: TAttLine(1, 1, 1), TAttFill(0, 1001), fPhi(0)
    7985{
    8086    fX = pix.GetX();
    8187    fY = pix.GetY();
    8288    fD = pix.GetD();
     89
     90    // fPhi = pix.GetPhi();
    8391}
    8492
     
    8997MHexagon::MHexagon(const MHexagon &hexagon) : TObject(hexagon), TAttLine(hexagon), TAttFill(hexagon)
    9098{
    91     fX = hexagon.fX;
    92     fY = hexagon.fY;
    93     fD = hexagon.fD;
     99    fX   = hexagon.fX;
     100    fY   = hexagon.fY;
     101    fD   = hexagon.fD;
     102    fPhi = hexagon.fPhi;
    94103}
    95104
     
    109118    TAttFill::Copy(hex);
    110119
    111     hex.fX = fX;
    112     hex.fY = fY;
    113     hex.fD = fD;
     120    hex.fX   = fX;
     121    hex.fY   = fY;
     122    hex.fD   = fD;
     123    hex.fPhi = fPhi;
    114124}
    115125
     
    125135Int_t MHexagon::DistancetoPrimitive(Int_t px, Int_t py, Float_t conv)
    126136{
     137    //FIXME: Rotation phi missing!
     138
    127139    //
    128140    //  compute the distance of the Point to the center of the Hexagon
     
    131143    const Int_t pyhex = gPad->YtoAbsPixel(fY*conv);
    132144
    133     const Double_t x = TMath::Abs(px-pxhex);
    134     const Double_t y = TMath::Abs(py-pyhex);
     145    //const Double_t x = TMath::Abs(px-pxhex);
     146    //const Double_t y = TMath::Abs(py-pyhex);
     147
     148    TVector2 v(TMath::Abs(px-pxhex), TMath::Abs(py-pyhex));
     149    // FIXME: fPhi or -fPhi?
     150    v = v.Rotate(-fPhi);             // FIXME: Replace with a precalculates sin/cos vector
     151
     152    const Double_t x = TMath::Abs(v.X());
     153    const Double_t y = TMath::Abs(v.Y());
    135154
    136155    const Double_t disthex = TMath::Sqrt(x*x + y*y);
     
    162181    return distborder < disthex ? (int)((disthex-distborder)/conv+1) : 0;
    163182}
    164 
     183/*
    165184// ------------------------------------------------------------------------
    166185//
     
    170189Float_t MHexagon::DistanceToPrimitive(Float_t px, Float_t py) const
    171190{
     191    //FIXME: Rotation phi missing!
     192
    172193    //
    173194    //  compute the distance of the Point to the center of the Hexagon
    174195    //
    175     const Double_t dx = px-fX;
    176     const Double_t dy = py-fY;
     196    //const Double_t dx = px-fX;
     197    //const Double_t dy = py-fY;
     198
     199    TVector2 v(px-fX, py-fY);
     200    // FIXME: fPhi or -fPhi?
     201    v = v.Rotate(-fPhi);             // FIXME: Replace with a precalculates sin/cos vector
     202
     203    const Double_t dx = v.X();
     204    const Double_t dy = v.Y();
     205
    177206    const Double_t disthex = TMath::Sqrt(dx*dx + dy*dy);
    178207
     
    199228    return -1;
    200229}
    201 
     230*/
    202231/*
    203232Float_t MHexagon::DistanceToPrimitive(Float_t px, Float_t py)
     
    227256//  Draw this ellipse with new coordinate
    228257//
    229 void MHexagon::DrawHexagon(Float_t x, Float_t y, Float_t d)
     258void MHexagon::DrawHexagon(Float_t x, Float_t y, Float_t d, Float_t phi)
    230259{
    231     MHexagon *newhexagon = new MHexagon(x, y, d);
     260    MHexagon *newhexagon = new MHexagon(x, y, d, phi);
    232261
    233262    TAttLine::Copy(*newhexagon);
     
    281310void MHexagon::Paint(Option_t *)
    282311{
    283     PaintHexagon(fX, fY, fD);
     312    PaintHexagon(fX, fY, fD, fPhi);
    284313}
    285314
     
    288317//  draw this hexagon with the coordinates
    289318//
    290 void MHexagon::PaintHexagon(Float_t inX, Float_t inY, Float_t inD)
     319void MHexagon::PaintHexagon(Float_t inX, Float_t inY, Float_t inD, Float_t phi)
    291320{
    292321    //
     
    296325    for (Int_t i=0; i<7; i++)
    297326    {
    298         x[i] = inX + fgDx[i%6]*inD;
    299         y[i] = inY + fgDy[i%6]*inD;
     327        TVector2 v(fgDx[i%6], fgDy[i%6]);
     328
     329        v = v.Rotate(phi); // FIXME: Replace with a precalculates sin/cos vector
     330
     331        x[i] = inX + v.X()*inD;
     332        y[i] = inY + v.Y()*inD;
    300333    }
    301334
     
    320353{
    321354    cout << "MHexagon: ";
    322     cout << "x=" << fX << "mm y=" << fY << "mm r=" << fD << "mm" << endl;
     355    cout << "x=" << fX << "mm y=" << fY << "mm r=" << fD << "mm phi=" << TMath::RadToDeg() << "deg" << endl;
    323356
    324357    cout << " Line:";
     
    343376       out << "   MHexagon *";
    344377
    345     out << "hexagon = new MHexagon(" << fX << "," << fY << "," << fD << ");" << endl;
     378    out << "hexagon = new MHexagon(" << fX << "," << fY << "," << fD << "," << fPhi << ");" << endl;
    346379
    347380    SaveFillAttributes(out, "hexagon");
     
    360393       out << "   MHexagon *";
    361394
    362     out << "hexagon = new MHexagon(" << fX << "," << fY << "," << fD << ");" << endl;
     395    out << "hexagon = new MHexagon(" << fX << "," << fY << "," << fD << "," << fPhi << ");" << endl;
    363396
    364397    SaveFillAttributes(out, "hexagon");
     
    370403#endif
    371404}
    372 
    373 // ------------------------------------------------------------------------
    374 //
    375 // Small helper class to allow fast sorting of TVector2 by angle
    376 //
    377 class HVector2 : public TVector2
    378 {
    379     Double_t fAngle;
    380 public:
    381     HVector2() : TVector2()  { }
    382     HVector2(const TVector2 &v) : TVector2(v)  { }
    383     HVector2(Double_t x, Double_t y) : TVector2 (x, y) { }
    384     void CalcAngle() { fAngle = Phi(); }
    385     Bool_t IsSortable() const { return kTRUE; }
    386     Int_t Compare(const TObject *obj) const { return ((HVector2*)obj)->fAngle>fAngle ? 1 : -1; }
    387     Double_t GetAngle() const { return fAngle; }
    388 };
    389 
    390 // ------------------------------------------------------------------------
    391 //
    392 // Calculate the edge points of the intersection area of two hexagons.
    393 // The points are added as TVector2 to the TOrdCollection.
    394 // The user is responsible of delete the objects.
    395 //
    396 void MHexagon::GetIntersectionBorder(TOrdCollection &col, const MHexagon &hex) const
    397 {
    398     Bool_t inside0[6], inside1[6];
    399 
    400     HVector2 v0[6];
    401     HVector2 v1[6];
    402 
    403     Int_t cnt0=0;
    404     Int_t cnt1=0;
    405 
    406     // Calculate teh edges of each hexagon and whether this edge
    407     // is inside the other hexgon or not
    408     for (int i=0; i<6; i++)
    409     {
    410         const Double_t x0 = fX+fgDx[i]*fD;
    411         const Double_t y0 = fY+fgDy[i]*fD;
    412 
    413         const Double_t x1 = hex.fX+fgDx[i]*hex.fD;
    414         const Double_t y1 = hex.fY+fgDy[i]*hex.fD;
    415 
    416         v0[i].Set(x0, y0);
    417         v1[i].Set(x1, y1);
    418 
    419         inside0[i] = hex.DistanceToPrimitive(x0, y0) < 0;
    420         inside1[i] = DistanceToPrimitive(x1, y1)     < 0;
    421 
    422         if (inside0[i])
    423         {
    424             col.Add(new HVector2(v0[i]));
    425             cnt0++;
    426         }
    427         if (inside1[i])
    428         {
    429             col.Add(new HVector2(v1[i]));
    430             cnt1++;
    431         }
    432     }
    433 
    434     if (cnt0==0 || cnt1==0)
    435         return;
    436 
    437     // No calculate which vorder lines intersect
    438     Bool_t iscross0[6], iscross1[6];
    439     for (int i=0; i<6; i++)
    440     {
    441         iscross0[i] = (inside0[i] && !inside0[(i+1)%6]) || (!inside0[i] && inside0[(i+1)%6]);
    442         iscross1[i] = (inside1[i] && !inside1[(i+1)%6]) || (!inside1[i] && inside1[(i+1)%6]);
    443     }
    444 
    445     // Calculate the border points of our intersection area
    446     for (int i=0; i<6; i++)
    447     {
    448         // Skip non intersecting lines
    449         if (!iscross0[i])
    450             continue;
    451 
    452         for (int j=0; j<6; j++)
    453         {
    454             // Skip non intersecting lines
    455             if (!iscross1[j])
    456                 continue;
    457 
    458             const TVector2 p = MMath::GetIntersectionPoint(v0[i], v0[(i+1)%6], v1[j], v1[(j+1)%6]);
    459             if (hex.DistanceToPrimitive(p.X(), p.Y())<1e-9)
    460                 col.Add(new HVector2(p));
    461         }
    462     }
    463 }
    464 
    465 // ------------------------------------------------------------------------
    466 //
    467 // Calculate the overlapping area of the two hexagons.
    468 //
    469 Double_t MHexagon::CalcOverlapArea(const MHexagon &cam) const
    470 {
    471     TOrdCollection col;
    472     col.SetOwner();
    473 
    474     GetIntersectionBorder(col, cam);
    475 
    476     // Check if there is an intersection to proceed with
    477     const Int_t n = col.GetEntries();
    478     if (n==0)
    479         return 0;
    480 
    481     // Calculate the center of gravity
    482     TVector2 cog;
    483 
    484     TIter Next(&col);
    485     HVector2 *v=0;
    486     while ((v=(HVector2*)Next()))
    487         cog += *v;
    488     cog /= n;
    489 
    490     // Shift the figure to its center-og-gravity and
    491     // calculate the angle of the connection line between the
    492     // border points of our intersesction area and its cog
    493     Next.Reset();
    494     while ((v=(HVector2*)Next()))
    495     {
    496         *v -= cog;
    497         v->CalcAngle();
    498     }
    499 
    500     // Sort these points by this angle
    501     col.Sort();
    502 
    503     // Now sum up the area of all the triangles between two
    504     // following points and the cog.
    505     Double_t A = 0;
    506     for (int i=0; i<n; i++)
    507     {
    508         // Vectors from cog to two nearby border points
    509         const HVector2 *v1 = (HVector2*)col.At(i);
    510         const HVector2 *v2 = (HVector2*)col.At((i+1)%n);
    511 
    512         // Angle between both vectors
    513         const Double_t a = fmod(v1->GetAngle()-v2->GetAngle()+TMath::TwoPi(), TMath::TwoPi());
    514 
    515         // Length of both vectors
    516         const Double_t d1 = v1->Mod();
    517         const Double_t d2 = v2->Mod();
    518 
    519         A += d1*d2/2*sin(a);
    520     }
    521     return A;
    522 }
  • trunk/MagicSoft/Mars/mgui/MHexagon.h

    r8178 r9235  
    3535    static const Double_t fgSin60;
    3636
     37public:
    3738    static const Double_t fgDx[6];   // X coordinate of the six edges
    3839    static const Double_t fgDy[6];   // Y coordinate of the six edges
     
    4445    Float_t fD;  // diameter D or better distance between opposite sides
    4546
     47    Float_t fPhi; // Rotation angle
     48
    4649public:
    4750
    4851    MHexagon();
    49     MHexagon(Float_t x, Float_t y, Float_t d);
     52    MHexagon(Float_t x, Float_t y, Float_t d, Float_t phi=0);
    5053    MHexagon(const MGeomPix &pix);
    5154    MHexagon(const MHexagon &hexagon);
     
    6265        return DistancetoPrimitive(px, py, 1);
    6366    }
    64     virtual Float_t DistanceToPrimitive(Float_t px, Float_t py) const;
    65     virtual void  DrawHexagon(Float_t x, Float_t y, Float_t d);
     67
     68    virtual void  DrawHexagon(Float_t x, Float_t y, Float_t d, Float_t phi=0);
    6669
    6770    //virtual void  ExecuteEvent(Int_t event, Int_t px, Int_t py);
     
    6972    virtual void  ls(const Option_t *Option="") const;
    7073    virtual void  Paint(Option_t *Option="");
    71     virtual void  PaintHexagon(Float_t x, Float_t y, Float_t d);
     74    virtual void  PaintHexagon(Float_t x, Float_t y, Float_t d, Float_t phi=0);
    7275    virtual void  Print(Option_t *Option="") const; // *MENU*
    7376    virtual void  SavePrimitive(ostream &out, Option_t *);
     
    7780    Float_t GetY() const { return fY; }
    7881    Float_t GetD() const { return fD; }
     82    Float_t GetPhi() const { return fPhi; }
    7983
    80     void GetIntersectionBorder(TOrdCollection &col, const MHexagon &hex) const;
    81     Double_t CalcOverlapArea(const MHexagon &cam) const;
    82 
    83     ClassDef(MHexagon, 1)    // A hexagon for MAGIC
     84    ClassDef(MHexagon, 2)    // A hexagon for MAGIC
    8485};
    8586
Note: See TracChangeset for help on using the changeset viewer.