Ignore:
Timestamp:
05/15/04 20:05:18 (20 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mbase/MGMap.cc

    r4078 r4081  
    217217// Convert root colors to arbitrary bitmap coordinates
    218218//
    219 Int_t MGMap::Color(int col)
     219UChar_t MGMap::Color(int col)
    220220{
    221221    switch (col)
     
    234234// --------------------------------------------------------------------------
    235235//
     236// Draw a line into the buffer (size w*h) from (x1, y1) to (x2, y2) with
     237// the color col and the line style style (default: solid)
     238//
     239void MGMap::DrawLine(UChar_t *buf, int w, int h, Float_t x1, Float_t y1, Float_t x2, Float_t y2, UChar_t col, Int_t style)
     240{
     241    const Int_t    step = style==kSolid?1:3;
     242    const Double_t len  = TMath::Hypot(x2-x1, y2-y1);
     243    const Double_t dx   = (x2-x1)/len*step;
     244    const Double_t dy   = (y2-y1)/len*step;
     245
     246    Double_t x = x1;
     247    Double_t y = y1;
     248
     249    for (int i=0; i<len; i+=step)
     250    {
     251        x+= dx;
     252        y+= dy;
     253
     254        const Int_t iy = (int)rint(y);
     255        if (iy<0 || iy>=h)
     256            continue;
     257
     258        const Int_t ix = (int)rint(x);
     259        if (ix<0 || ix>=w)
     260            continue;
     261
     262        buf[ix+iy*w] = col;
     263    }
     264}
     265
     266// --------------------------------------------------------------------------
     267//
     268// Draw a box into the buffer (size w*h) from (x1, y1) to (x2, y2) with
     269// the color col and the line style style (default: solid)
     270//
     271void MGMap::DrawBox(UChar_t *buf, int w, int h, Float_t x1, Float_t y1, Float_t x2, Float_t y2, UChar_t col, Int_t style)
     272{
     273    DrawLine(buf, w, h, x1, y1, x2, y1, col, style);
     274    DrawLine(buf, w, h, x1, y2, x2, y1, col, style);
     275    DrawLine(buf, w, h, x1, y1, x1, y2, col, style);
     276    DrawLine(buf, w, h, x2, y1, x2, y2, col, style);
     277}
     278
     279// --------------------------------------------------------------------------
     280//
     281// Draw a circle into the buffer (size w*h) around (x, y) with radius r and
     282// the color col.
     283//
     284void MGMap::DrawCircle(UChar_t *buf, int w, int h, Float_t x, Float_t y, Float_t r, UChar_t col)
     285{
     286    const Int_t n = (int)rint(sqrt(2.)*r*TMath::Pi()/2);
     287    for (int i=0; i<n-1; i++)
     288    {
     289        const Double_t angle = TMath::TwoPi()*i/n;
     290
     291        const Double_t dx = r*cos(angle);
     292        const Double_t dy = r*sin(angle);
     293
     294        const Int_t x1 = (int)rint(x+dx);
     295        const Int_t x2 = (int)rint(x-dx);
     296
     297        const Int_t y1 = (int)rint(y+dy);
     298        if (y1>=0 && y1<h)
     299        {
     300            if (x1>=0 && x1<w)
     301                buf[x1+y1*w] = col;
     302
     303            if (x2>=0 && x2<w)
     304                buf[x2+y1*w] = col;
     305        }
     306
     307        const Int_t y2 = (int)rint(y-dy);
     308        if (y2>=0 && y2<h)
     309        {
     310            if (x1>=0 && x1<w)
     311                buf[x1+y2*w] = col;
     312
     313            if (x2>=0 && x2<w)
     314                buf[x2+y2*w] = col;
     315        }
     316    }
     317}
     318
     319// --------------------------------------------------------------------------
     320//
     321// Draw a dot into the buffer (size w*h) at (x, y) with color col.
     322//
     323void MGMap::DrawDot(UChar_t *buf, int w, int h, Float_t cx, Float_t cy, UChar_t col)
     324{
     325    const Int_t x1 = (int)rint(cx);
     326    const Int_t y1 = (int)rint(cy);
     327
     328    if (x1>=0 && y1>=0 && x1<w && y1<h)
     329        buf[x1+y1*w] = col;
     330}
     331
     332// --------------------------------------------------------------------------
     333//
    236334// Draw a line into the buffer. The TObject must be a TLine.
    237335// Currently only solid and non sloid line are supported.
    238336//
    239 void MGMap::DrawLine(TObject *o, unsigned char *buf, int w, int h, Double_t scale)
     337void MGMap::DrawLine(TObject *o, UChar_t *buf, int w, int h, Double_t scale)
    240338{
    241339    TLine *l = dynamic_cast<TLine*>(o);
     
    243341        return;
    244342
    245     Double_t x1 = 0.5*w-(l->GetX1()/scale);
    246     Double_t x2 = 0.5*w-(l->GetX2()/scale);
    247     Double_t y1 = 0.5*h-(l->GetY1()/scale);
    248     Double_t y2 = 0.5*h-(l->GetY2()/scale);
    249 
    250     Double_t len = TMath::Hypot(x2-x1, y2-y1);
    251 
    252     Double_t x = x1;
    253     Double_t y = y1;
    254 
    255     Int_t step = l->GetLineStyle()==kSolid?1:3;
    256 
    257     Double_t dx = (x2-x1)/len*step;
    258     Double_t dy = (y2-y1)/len*step;
    259 
    260     for (int i=0; i<len; i+=step)
    261     {
    262         x+= dx;
    263         y+= dy;
    264 
    265         if (x<0 || y<0 || x>w-1 || y>h-1)
    266             continue;
    267 
    268         buf[(int)x+(int)y*w] = Color(l->GetLineColor());
    269     }
     343    const Double_t x1 = 0.5*w-(l->GetX1()/scale);
     344    const Double_t x2 = 0.5*w-(l->GetX2()/scale);
     345    const Double_t y1 = 0.5*h-(l->GetY1()/scale);
     346    const Double_t y2 = 0.5*h-(l->GetY2()/scale);
     347
     348    const Int_t col = Color(l->GetLineColor());
     349    DrawLine(buf, w, h, x1, y1, x2, y2, col, l->GetLineStyle());
     350}
     351
     352void MGMap::DrawMultiply(UChar_t *buf, int w, int h, Float_t cx, Float_t cy, Float_t size, UChar_t col)
     353{
     354    DrawLine(buf, w, h, cx-size, cy-size, cx+size, cy+size, col);
     355    DrawLine(buf, w, h, cx+size, cy-size, cx-size, cy+size, col);
     356}
     357
     358void MGMap::DrawCross(UChar_t *buf, int w, int h, Float_t cx, Float_t cy, Float_t size, UChar_t col)
     359{
     360    DrawLine(buf, w, h, cx-size, cy, cx+size, cy, col);
     361    DrawLine(buf, w, h, cx, cy-size, cx, cy+size, col);
    270362}
    271363
     
    275367// Currently kCircle, kMultiply and KDot are supported.
    276368//
    277 void MGMap::DrawMarker(TObject *o, unsigned char *buf, int w, int h, Double_t scale)
     369void MGMap::DrawMarker(TObject *o, UChar_t *buf, int w, int h, Double_t scale)
    278370{
    279371    TMarker *m = dynamic_cast<TMarker*>(o);
     
    289381    {
    290382    case kCircle:
    291         {
    292             Int_t step = 32;
    293 
    294             const Float_t l = (m->GetMarkerSize()*2)+1;
    295             for (int i=0; i<step; i++)
    296             {
    297                 const Double_t angle = i*TMath::TwoPi()/step;
    298 
    299                 const Double_t x1 = x+l*cos(angle);
    300                 const Double_t y1 = y+l*sin(angle);
    301 
    302                 if (x1<0 || y1<0 || x1>w-1 || y1>h-1)
    303                     continue;
    304 
    305                 buf[(int)x1+(int)y1*w] = col;
    306             }
    307         }
     383        DrawCircle(buf, w, h, x, y, m->GetMarkerSize()*2+1, col);
    308384        break;
    309385    case kDot:
    310         if (x>=0 && y>=0 && x<w && y<h)
    311             buf[(int)x+(int)y*w] = col;
    312         break;
    313 
     386        DrawDot(buf, w, h, x, y, col);
     387        break;
    314388    case kMultiply:
    315         {
    316             const Int_t l = (int)(m->GetMarkerSize()+1);
    317 
    318             for (int i=-l; i<l+1; i++)
    319             {
    320                 Double_t x1 = x+i;
    321                 Double_t y1 = y+i;
    322                 if (x1>=0 && y>=0 && x1<w && y<h)
    323                     buf[(int)x1+(int)y*w] = col;
    324                 if (x>=0 && y1>=0 && x<w && y1<h)
    325                     buf[(int)x+(int)y1*w] = col;
    326             }
    327         }
    328         break;
    329 
     389        DrawMultiply(buf, w, h, x, y, m->GetMarkerSize()*2+1, col);
     390        break;
     391    case kCross:
     392        DrawCross(buf, w, h, x, y, m->GetMarkerSize()*2+1, col);
     393        break;
    330394    }
    331395}
     
    345409// --------------------------------------------------------------------------
    346410//
    347 // Paint all objects into a buffer of w*h unsigned chars. The scale
     411// Paint all objects into a buffer of w*h UChar_ts. The scale
    348412// gives you the conversio factor to convert pad coordinates into
    349413// buffer pixels - it is the distance from the center of the buffer
    350414// to one of its edges.
    351415//
    352 void MGMap::Paint(unsigned char *buf, int w, int h, Float_t scale)
     416void MGMap::Paint(UChar_t *buf, int w, int h, Float_t scale)
    353417{
    354418    scale /= TMath::Hypot((float)w, (float)h)/2;
Note: See TracChangeset for help on using the changeset viewer.