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

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mgeom/MGeomPix.cc

    r9219 r9235  
    240240    return -1;
    241241}
     242
     243// ------------------------------------------------------------------------
     244//
     245// compute the distance of a point (px,py) to the Hexagon center in world
     246// coordinates. Return -1 if inside.
     247//
     248Float_t MGeomPix::DistanceToPrimitive(Float_t px, Float_t py) const
     249{
     250    //FIXME: Rotation phi missing!
     251
     252    static Double_t fgCos60 = 0.5;               //TMath::Cos(TMath::DegToRad()*60);
     253    static Double_t fgSin60 = TMath::Sqrt(3.)/2; //TMath::Sin(TMath::DegToRad()*60);
     254
     255    //
     256    //  compute the distance of the Point to the center of the Hexagon
     257    //
     258    //const Double_t dx = px-fX;
     259    //const Double_t dy = py-fY;
     260
     261    TVector2 v(px-fX, py-fY);
     262    // FIXME: fPhi or -fPhi?
     263    // v = v.Rotate(-fPhi);             // FIXME: Replace with a precalculates sin/cos vector
     264
     265    const Double_t dx = v.X();
     266    const Double_t dy = v.Y();
     267
     268    const Double_t disthex = TMath::Sqrt(dx*dx + dy*dy);
     269
     270    //
     271    // Now check if point is outside of hexagon; just check x coordinate
     272    // in three coordinate systems: the default one, in which two sides of
     273    // the hexagon are paralel to the y axis (see camera displays) and two
     274    // more, rotated with respect to that one by +- 60 degrees.
     275    //
     276
     277    if (TMath::Abs(dx) > fD/2.)
     278      return disthex;
     279
     280    const Double_t dx2 = dx*fgCos60 + dy*fgSin60;
     281
     282    if  (TMath::Abs(dx2) > fD/2.)
     283      return disthex;
     284
     285    const Double_t dx3 = dx*fgCos60 - dy*fgSin60;
     286
     287    if  (TMath::Abs(dx3) > fD/2.)
     288      return disthex;
     289
     290    return -1;
     291}
     292
     293// ==============================================================================
     294/*
     295#include <TOrdCollection.h>
     296#include "MMath.h"
     297#include "../mgui/MHexagon.h" // MHexagon::fgDx
     298
     299// ------------------------------------------------------------------------
     300//
     301// Small helper class to allow fast sorting of TVector2 by angle
     302//
     303class HVector2 : public TVector2
     304{
     305    Double_t fAngle;
     306public:
     307    HVector2() : TVector2()  { }
     308    HVector2(const TVector2 &v) : TVector2(v)  { }
     309    HVector2(Double_t x, Double_t y) : TVector2 (x, y) { }
     310    void CalcAngle() { fAngle = Phi(); }
     311    Bool_t IsSortable() const { return kTRUE; }
     312    Int_t Compare(const TObject *obj) const { return ((HVector2*)obj)->fAngle>fAngle ? 1 : -1; }
     313    Double_t GetAngle() const { return fAngle; }
     314};
     315
     316// ------------------------------------------------------------------------
     317//
     318// Calculate the edge points of the intersection area of two hexagons.
     319// The points are added as TVector2 to the TOrdCollection.
     320// The user is responsible of delete the objects.
     321//
     322void MGeomPix::GetIntersectionBorder(TOrdCollection &col, const MGeomPix &hex) const
     323{
     324//    if (fPhi!=0)
     325//    {
     326//        gLog << warn << "MGeomPix::GetIntersectionBorder: Only phi=0 supported yet." << endl;
     327//        return;
     328//    }
     329
     330    Bool_t inside0[6], inside1[6];
     331
     332    HVector2 v0[6];
     333    HVector2 v1[6];
     334
     335    Int_t cnt0=0;
     336    Int_t cnt1=0;
     337
     338    // Calculate teh edges of each hexagon and whether this edge
     339    // is inside the other hexgon or not
     340    for (int i=0; i<6; i++)
     341    {
     342        const Double_t x0 = fX+MHexagon::fgDx[i]*fD;
     343        const Double_t y0 = fY+MHexagon::fgDy[i]*fD;
     344
     345        const Double_t x1 = hex.fX+MHexagon::fgDx[i]*hex.fD;
     346        const Double_t y1 = hex.fY+MHexagon::fgDy[i]*hex.fD;
     347
     348        v0[i].Set(x0, y0);
     349        v1[i].Set(x1, y1);
     350
     351        inside0[i] = hex.DistanceToPrimitive(x0, y0) < 0;
     352        inside1[i] = DistanceToPrimitive(x1, y1)     < 0;
     353
     354        if (inside0[i])
     355        {
     356            col.Add(new HVector2(v0[i]));
     357            cnt0++;
     358        }
     359        if (inside1[i])
     360        {
     361            col.Add(new HVector2(v1[i]));
     362            cnt1++;
     363        }
     364    }
     365
     366    if (cnt0==0 || cnt1==0)
     367        return;
     368
     369    // No calculate which vorder lines intersect
     370    Bool_t iscross0[6], iscross1[6];
     371    for (int i=0; i<6; i++)
     372    {
     373        iscross0[i] = (inside0[i] && !inside0[(i+1)%6]) || (!inside0[i] && inside0[(i+1)%6]);
     374        iscross1[i] = (inside1[i] && !inside1[(i+1)%6]) || (!inside1[i] && inside1[(i+1)%6]);
     375    }
     376
     377    // Calculate the border points of our intersection area
     378    for (int i=0; i<6; i++)
     379    {
     380        // Skip non intersecting lines
     381        if (!iscross0[i])
     382            continue;
     383
     384        for (int j=0; j<6; j++)
     385        {
     386            // Skip non intersecting lines
     387            if (!iscross1[j])
     388                continue;
     389
     390            const TVector2 p = MMath::GetIntersectionPoint(v0[i], v0[(i+1)%6], v1[j], v1[(j+1)%6]);
     391            if (hex.DistanceToPrimitive(p.X(), p.Y())<1e-9)
     392                col.Add(new HVector2(p));
     393        }
     394    }
     395}
     396
     397// ------------------------------------------------------------------------
     398//
     399// Calculate the overlapping area of the two hexagons.
     400//
     401Double_t MGeomPix::CalcOverlapArea(const MGeomPix &cam) const
     402{
     403//    if (fPhi!=0)
     404//    {
     405//        gLog << warn << "MGeomPix::CalcOverlapArea: Only phi=0 supported yet." << endl;
     406//        return -1;
     407//    }
     408
     409    TOrdCollection col;
     410    col.SetOwner();
     411
     412    GetIntersectionBorder(col, cam);
     413
     414    // Check if there is an intersection to proceed with
     415    const Int_t n = col.GetEntries();
     416    if (n==0)
     417        return 0;
     418
     419    // Calculate the center of gravity
     420    TVector2 cog;
     421
     422    TIter Next(&col);
     423    HVector2 *v=0;
     424    while ((v=(HVector2*)Next()))
     425        cog += *v;
     426    cog /= n;
     427
     428    // Shift the figure to its center-og-gravity and
     429    // calculate the angle of the connection line between the
     430    // border points of our intersesction area and its cog
     431    Next.Reset();
     432    while ((v=(HVector2*)Next()))
     433    {
     434        *v -= cog;
     435        v->CalcAngle();
     436    }
     437
     438    // Sort these points by this angle
     439    col.Sort();
     440
     441    // Now sum up the area of all the triangles between two
     442    // following points and the cog.
     443    Double_t A = 0;
     444    for (int i=0; i<n; i++)
     445    {
     446        // Vectors from cog to two nearby border points
     447        const HVector2 *v1 = (HVector2*)col.At(i);
     448        const HVector2 *v2 = (HVector2*)col.At((i+1)%n);
     449
     450        // Angle between both vectors
     451        const Double_t a = fmod(v1->GetAngle()-v2->GetAngle()+TMath::TwoPi(), TMath::TwoPi());
     452
     453        // Length of both vectors
     454        const Double_t d1 = v1->Mod();
     455        const Double_t d2 = v2->Mod();
     456
     457        A += d1*d2/2*sin(a);
     458    }
     459    return A;
     460}
     461*/
     462
  • trunk/MagicSoft/Mars/mgeom/MGeomPix.h

    r9219 r9235  
    88class MGeomCam;
    99class TVector2;
     10class TOrdCollection;
    1011
    1112class MGeomPix : public MParContainer
     
    9697    Int_t  GetDirection(const MGeomPix &pix) const;
    9798
     99    virtual Float_t DistanceToPrimitive(Float_t px, Float_t py) const;
     100    //void GetIntersectionBorder(TOrdCollection &col, const MGeomPix &hex) const;
     101    //Double_t CalcOverlapArea(const MGeomPix &cam) const;
     102
    98103    ClassDef(MGeomPix, 4) // Geometry class describing the geometry of one pixel
    99104};
Note: See TracChangeset for help on using the changeset viewer.