TPolyMarker.cxx

Go to the documentation of this file.
00001 // @(#)root/hist:$Id: TPolyMarker.cxx 35398 2010-09-18 06:02:18Z brun $
00002 // Author: Rene Brun   12/12/94
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "Riostream.h"
00013 #include "TROOT.h"
00014 #include "TVirtualPad.h"
00015 #include "TPolyMarker.h"
00016 #include "TClass.h"
00017 #include "TMath.h"
00018 
00019 ClassImp(TPolyMarker)
00020 
00021 
00022 //______________________________________________________________________________
00023 //
00024 //  a PolyMarker is defined by an array on N points in a 2-D space.
00025 // At each point x[i], y[i] a marker is drawn.
00026 // Marker attributes are managed by TAttMarker.
00027 // See TMarker for the list of possible marker types.
00028 //
00029 
00030 
00031 //______________________________________________________________________________
00032 TPolyMarker::TPolyMarker(): TObject()
00033 {
00034    // Default constructor.
00035 
00036    fN = 0;
00037    fX = fY = 0;
00038    fLastPoint = -1;
00039 }
00040 
00041 
00042 //______________________________________________________________________________
00043 TPolyMarker::TPolyMarker(Int_t n, Option_t *option)
00044       :TObject(), TAttMarker()
00045 {
00046    // Constructor.
00047 
00048    fOption = option;
00049    SetBit(kCanDelete);
00050    fLastPoint = -1;
00051    if (n <= 0) {
00052       fN = 0;
00053       fLastPoint = -1;
00054       fX = fY = 0;
00055       return;
00056    }
00057    fN = n;
00058    fX = new Double_t [fN];
00059    fY = new Double_t [fN];
00060 }
00061 
00062 
00063 //______________________________________________________________________________
00064 TPolyMarker::TPolyMarker(Int_t n, Float_t *x, Float_t *y, Option_t *option)
00065       :TObject(), TAttMarker()
00066 {
00067    // Constructor.
00068 
00069    fOption = option;
00070    SetBit(kCanDelete);
00071    fLastPoint = -1;
00072    if (n <= 0) {
00073       fN = 0;
00074       fLastPoint = -1;
00075       fX = fY = 0;
00076       return;
00077    }
00078    fN = n;
00079    fX = new Double_t [fN];
00080    fY = new Double_t [fN];
00081    if (!x || !y) return;
00082    for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i]; }
00083    fLastPoint = fN-1;
00084 }
00085 
00086 
00087 //______________________________________________________________________________
00088 TPolyMarker::TPolyMarker(Int_t n, Double_t *x, Double_t *y, Option_t *option)
00089       :TObject(), TAttMarker()
00090 {
00091    // Constructor.
00092 
00093    fOption = option;
00094    SetBit(kCanDelete);
00095    fLastPoint = -1;
00096    if (n <= 0) {
00097       fN = 0;
00098       fLastPoint = -1;
00099       fX = fY = 0;
00100       return;
00101    }
00102    fN = n;
00103    fX = new Double_t [fN];
00104    fY = new Double_t [fN];
00105    if (!x || !y) return;
00106    for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i]; }
00107    fLastPoint = fN-1;
00108 }
00109 
00110 //______________________________________________________________________________
00111 TPolyMarker& TPolyMarker::operator=(const TPolyMarker& pm)
00112 {
00113    //assignment operator
00114    if(this!=&pm) {
00115       TObject::operator=(pm);
00116       TAttMarker::operator=(pm);
00117       fN=pm.fN;
00118       fLastPoint=pm.fLastPoint;
00119       fX=pm.fX;
00120       fY=pm.fY;
00121       fOption=pm.fOption;
00122    }
00123    return *this;
00124 }
00125 
00126 //______________________________________________________________________________
00127 TPolyMarker::~TPolyMarker()
00128 {
00129    // Desctructor.
00130 
00131    if (fX) delete [] fX;
00132    if (fY) delete [] fY;
00133    fLastPoint = -1;
00134 }
00135 
00136 
00137 //______________________________________________________________________________
00138 TPolyMarker::TPolyMarker(const TPolyMarker &polymarker) : TObject(polymarker), TAttMarker(polymarker)
00139 {
00140    // Copy constructor.
00141 
00142    fN = 0;
00143    fLastPoint = -1;
00144    ((TPolyMarker&)polymarker).Copy(*this);
00145 }
00146 
00147 
00148 //______________________________________________________________________________
00149 void TPolyMarker::Copy(TObject &obj) const
00150 {
00151 
00152    // Copy.
00153 
00154    TObject::Copy(obj);
00155    TAttMarker::Copy(((TPolyMarker&)obj));
00156    ((TPolyMarker&)obj).fN = fN;
00157    if (fN > 0) {
00158       ((TPolyMarker&)obj).fX = new Double_t [fN];
00159       ((TPolyMarker&)obj).fY = new Double_t [fN];
00160       for (Int_t i=0; i<fN;i++) { ((TPolyMarker&)obj).fX[i] = fX[i], ((TPolyMarker&)obj).fY[i] = fY[i]; }
00161    } else {
00162       ((TPolyMarker&)obj).fX = 0;
00163       ((TPolyMarker&)obj).fY = 0;
00164    }
00165    ((TPolyMarker&)obj).fOption = fOption;
00166    ((TPolyMarker&)obj).fLastPoint = fLastPoint;
00167 }
00168 
00169 
00170 //______________________________________________________________________________
00171 Int_t TPolyMarker::DistancetoPrimitive(Int_t px, Int_t py)
00172 {
00173    // Compute distance from point px,py to a polymarker.
00174    //
00175    //  Compute the closest distance of approach from point px,py to each point
00176    //  of the polymarker.
00177    //  Returns when the distance found is below DistanceMaximum.
00178    //  The distance is computed in pixels units.
00179 
00180    const Int_t big = 9999;
00181 
00182    // check if point is near one of the points
00183    Int_t i, pxp, pyp, d;
00184    Int_t distance = big;
00185 
00186    for (i=0;i<Size();i++) {
00187       pxp = gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
00188       pyp = gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
00189       d   = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
00190       if (d < distance) distance = d;
00191    }
00192    return distance;
00193 }
00194 
00195 
00196 //______________________________________________________________________________
00197 void TPolyMarker::Draw(Option_t *option)
00198 {
00199    // Draw.
00200 
00201    AppendPad(option);
00202 }
00203 
00204 
00205 //______________________________________________________________________________
00206 void TPolyMarker::DrawPolyMarker(Int_t n, Double_t *x, Double_t *y, Option_t *)
00207 {
00208    // Draw polymarker.
00209 
00210    TPolyMarker *newpolymarker = new TPolyMarker(n,x,y);
00211    TAttMarker::Copy(*newpolymarker);
00212    newpolymarker->fOption = fOption;
00213    newpolymarker->SetBit(kCanDelete);
00214    newpolymarker->AppendPad();
00215 }
00216 
00217 
00218 //______________________________________________________________________________
00219 void TPolyMarker::ExecuteEvent(Int_t, Int_t, Int_t)
00220 {
00221    // Execute action corresponding to one event.
00222    //
00223    //  This member function must be implemented to realize the action
00224    //  corresponding to the mouse click on the object in the window
00225 }
00226 
00227 
00228 //______________________________________________________________________________
00229 void TPolyMarker::ls(Option_t *) const
00230 {
00231    // ls.
00232 
00233    TROOT::IndentLevel();
00234    printf("TPolyMarker  N=%d\n",fN);
00235 }
00236 
00237 
00238 //______________________________________________________________________________
00239 Int_t TPolyMarker::Merge(TCollection *li)
00240 {
00241    // Merge polymarkers in the collection in this polymarker.
00242 
00243    if (!li) return 0;
00244    TIter next(li);
00245 
00246    //first loop to count the number of entries
00247    TPolyMarker *pm;
00248    Int_t npoints = 0;
00249    while ((pm = (TPolyMarker*)next())) {
00250       if (!pm->InheritsFrom(TPolyMarker::Class())) {
00251          Error("Add","Attempt to add object of class: %s to a %s",pm->ClassName(),this->ClassName());
00252          return -1;
00253       }
00254       npoints += pm->Size();
00255    }
00256 
00257    //extend this polymarker to hold npoints
00258    SetPoint(npoints-1,0,0);
00259 
00260    //merge all polymarkers
00261    next.Reset();
00262    while ((pm = (TPolyMarker*)next())) {
00263       Int_t np = pm->Size();
00264       Double_t *x = pm->GetX();
00265       Double_t *y = pm->GetY();
00266       for (Int_t i=0;i<np;i++) {
00267          SetPoint(i,x[i],y[i]);
00268       }
00269    }
00270 
00271    return npoints;
00272 }
00273 
00274 
00275 //______________________________________________________________________________
00276 void TPolyMarker::Paint(Option_t *option)
00277 {
00278    // Paint.
00279 
00280    PaintPolyMarker(fLastPoint+1, fX, fY, option);
00281 }
00282 
00283 
00284 //______________________________________________________________________________
00285 void TPolyMarker::PaintPolyMarker(Int_t n, Double_t *x, Double_t *y, Option_t *option)
00286 {
00287    // Paint polymarker.
00288 
00289    if (n <= 0) return;
00290    TAttMarker::Modify();  //Change marker attributes only if necessary
00291    Double_t *xx = x;
00292    Double_t *yy = y;
00293    if (gPad->GetLogx()) {
00294       xx = new Double_t[n];
00295       for (Int_t ix=0;ix<n;ix++) xx[ix] = gPad->XtoPad(x[ix]);
00296    }
00297    if (gPad->GetLogy()) {
00298       yy = new Double_t[n];
00299       for (Int_t iy=0;iy<n;iy++) yy[iy] = gPad->YtoPad(y[iy]);
00300    }
00301    gPad->PaintPolyMarker(n,xx,yy,option);
00302    if (x != xx) delete [] xx;
00303    if (y != yy) delete [] yy;
00304 }
00305 
00306 
00307 //______________________________________________________________________________
00308 void TPolyMarker::Print(Option_t *) const
00309 {
00310    // Print polymarker.
00311 
00312    printf("TPolyMarker  N=%d\n",fN);
00313 }
00314 
00315 
00316 //______________________________________________________________________________
00317 void TPolyMarker::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
00318 {
00319    // Save primitive as a C++ statement(s) on output stream out.
00320 
00321    char quote = '"';
00322    out<<"   "<<endl;
00323    out<<"   Double_t *dum = 0;"<<endl;
00324    if (gROOT->ClassSaved(TPolyMarker::Class())) {
00325       out<<"   ";
00326    } else {
00327       out<<"   TPolyMarker *";
00328    }
00329    out<<"pmarker = new TPolyMarker("<<fN<<",dum,dum,"<<quote<<fOption<<quote<<");"<<endl;
00330 
00331    SaveMarkerAttributes(out,"pmarker",1,1,1);
00332 
00333    for (Int_t i=0;i<Size();i++) {
00334       out<<"   pmarker->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<");"<<endl;
00335    }
00336    out<<"   pmarker->Draw("
00337       <<quote<<option<<quote<<");"<<endl;
00338 }
00339 
00340 
00341 //______________________________________________________________________________
00342 Int_t TPolyMarker::SetNextPoint(Double_t x, Double_t y)
00343 {
00344    // Set point following LastPoint to x, y.
00345    // Returns index of the point (new last point).
00346 
00347    fLastPoint++;
00348    SetPoint(fLastPoint, x, y);
00349    return fLastPoint;
00350 }
00351 
00352 
00353 //______________________________________________________________________________
00354 void TPolyMarker::SetPoint(Int_t n, Double_t x, Double_t y)
00355 {
00356    // Set point number n.
00357    // if n is greater than the current size, the arrays are automatically
00358    // extended
00359 
00360    if (n < 0) return;
00361    if (!fX || !fY || n >= fN) {
00362       // re-allocate the object
00363       Int_t newN = TMath::Max(2*fN,n+1);
00364       Double_t *savex = new Double_t [newN];
00365       Double_t *savey = new Double_t [newN];
00366       if (fX && fN){
00367          memcpy(savex,fX,fN*sizeof(Double_t));
00368          memset(&savex[fN],0,(newN-fN)*sizeof(Double_t));
00369          delete [] fX;
00370       }
00371       if (fY && fN){
00372          memcpy(savey,fY,fN*sizeof(Double_t));
00373          memset(&savey[fN],0,(newN-fN)*sizeof(Double_t));
00374          delete [] fY;
00375       }
00376       fX = savex;
00377       fY = savey;
00378       fN = newN;
00379    }
00380    fX[n] = x;
00381    fY[n] = y;
00382    fLastPoint = TMath::Max(fLastPoint,n);
00383 }
00384 
00385 
00386 //______________________________________________________________________________
00387 void TPolyMarker::SetPolyMarker(Int_t n)
00388 {
00389    // If n <= 0 the current arrays of points are deleted.
00390 
00391    if (n <= 0) {
00392       fN = 0;
00393       fLastPoint = -1;
00394       delete [] fX;
00395       delete [] fY;
00396       fX = fY = 0;
00397       return;
00398    }
00399    SetPoint(n-1,0,0);
00400 }
00401 
00402 
00403 //______________________________________________________________________________
00404 void TPolyMarker::SetPolyMarker(Int_t n, Float_t *x, Float_t *y, Option_t *option)
00405 {
00406    // If n <= 0 the current arrays of points are deleted.
00407 
00408    if (n <= 0) {
00409       fN = 0;
00410       fLastPoint = -1;
00411       delete [] fX;
00412       delete [] fY;
00413       fX = fY = 0;
00414       return;
00415    }
00416    fN =n;
00417    if (fX) delete [] fX;
00418    if (fY) delete [] fY;
00419    fX = new Double_t[fN];
00420    fY = new Double_t[fN];
00421    for (Int_t i=0; i<fN;i++) {
00422       if (x) fX[i] = (Double_t)x[i];
00423       if (y) fY[i] = (Double_t)y[i];
00424    }
00425    fOption = option;
00426    fLastPoint = fN-1;
00427 }
00428 
00429 
00430 //______________________________________________________________________________
00431 void TPolyMarker::SetPolyMarker(Int_t n, Double_t *x, Double_t *y, Option_t *option)
00432 {
00433    // If n <= 0 the current arrays of points are deleted.
00434 
00435    if (n <= 0) {
00436       fN = 0;
00437       fLastPoint = -1;
00438       delete [] fX;
00439       delete [] fY;
00440       fX = fY = 0;
00441       return;
00442    }
00443    fN =n;
00444    if (fX) delete [] fX;
00445    if (fY) delete [] fY;
00446    fX = new Double_t[fN];
00447    fY = new Double_t[fN];
00448    for (Int_t i=0; i<fN;i++) {
00449       if (x) fX[i] = x[i];
00450       if (y) fY[i] = y[i];
00451    }
00452    fOption = option;
00453    fLastPoint = fN-1;
00454 }
00455 
00456 
00457 //_______________________________________________________________________
00458 void TPolyMarker::Streamer(TBuffer &R__b)
00459 {
00460    // Stream a class object.
00461 
00462    if (R__b.IsReading()) {
00463       UInt_t R__s, R__c;
00464       Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
00465       if (R__v > 1) {
00466          R__b.ReadClassBuffer(TPolyMarker::Class(), this, R__v, R__s, R__c);
00467          return;
00468       }
00469       //====process old versions before automatic schema evolution
00470       TObject::Streamer(R__b);
00471       TAttMarker::Streamer(R__b);
00472       R__b >> fN;
00473       fX = new Double_t[fN];
00474       fY = new Double_t[fN];
00475       Int_t i;
00476       Float_t xold,yold;
00477       for (i=0;i<fN;i++) {R__b >> xold; fX[i] = xold;}
00478       for (i=0;i<fN;i++) {R__b >> yold; fY[i] = yold;}
00479       fOption.Streamer(R__b);
00480       R__b.CheckByteCount(R__s, R__c, TPolyMarker::IsA());
00481       //====end of old versions
00482 
00483    } else {
00484       R__b.WriteClassBuffer(TPolyMarker::Class(),this);
00485    }
00486 }

Generated on Tue Jul 5 14:24:21 2011 for ROOT_528-00b_version by  doxygen 1.5.1