TPointsArray3D.cxx

Go to the documentation of this file.
00001 // @(#)root/table:$Id: TPointsArray3D.cxx 34976 2010-08-25 04:11:07Z pcanal $
00002 // Author: Valery Fine(fine@mail.cern.ch)   24/04/99
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 
00014 #include "TPointsArray3D.h"
00015 #include "TVirtualPad.h"
00016 #include "TView.h"
00017 #include "TClass.h"
00018 #include "TROOT.h"
00019 #include "TMath.h"
00020 
00021 //______________________________________________________________________________
00022 //
00023 // TPointsArray3D is an abstract class of the array of 3-dimensional points.
00024 // It has 4 different constructors.
00025 //
00026 // This class has no implementation for Paint, Draw, and SavePrimitive methods
00027 //
00028 //   First one, without any parameters TPointsArray3D(), we call 'default
00029 // constructor' and it's used in a case that just an initialisation is
00030 // needed (i.e. pointer declaration).
00031 //
00032 //       Example:
00033 //                 TPointsArray3D *pl1 = new TPointsArray3D;
00034 //
00035 //
00036 //   Second one is 'normal constructor' with, usually, one parameter
00037 // n (number of points), and it just allocates a space for the points.
00038 //
00039 //       Example:
00040 //                 TPointsArray3D pl1(150);
00041 //
00042 //
00043 //   Third one allocates a space for the points, and also makes
00044 // initialisation from the given array.
00045 //
00046 //       Example:
00047 //                 TPointsArray3D pl1(150, pointerToAnArray);
00048 //
00049 //
00050 //   Fourth one is, almost, similar to the constructor above, except
00051 // initialisation is provided with three independent arrays (array of
00052 // x coordinates, y coordinates and z coordinates).
00053 //
00054 //       Example:
00055 //                 TPointsArray3D pl1(150, xArray, yArray, zArray);
00056 //
00057 
00058 ClassImp(TPointsArray3D)
00059 
00060 //______________________________________________________________________________
00061 TPointsArray3D::TPointsArray3D()
00062 {
00063 //*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine default constructor*-*-*-*-*-*-*-*-*-*-*
00064 //*-*                      ================================
00065 
00066    fN = 0;
00067    fP = 0;
00068    fLastPoint = -1;
00069    fGLList = 0;
00070    fLastPoint = 0;
00071 }
00072 
00073 
00074 //______________________________________________________________________________
00075 TPointsArray3D::TPointsArray3D(Int_t n, Option_t *option)
00076 {
00077 //*-*-*-*-*-*3-D PolyLine normal constructor without initialisation*-*-*-*-*-*-*
00078 //*-*        ======================================================
00079 //*-*  If n < 0 the default size (2 points) is set
00080 //*-*
00081    fLastPoint = -1;
00082    if (n < 1) fN = 2;  // Set the default size for this object
00083    else fN = n;
00084 
00085    fP = new Float_t[3*fN];
00086    memset(fP,0,3*fN*sizeof(Float_t));
00087    fOption = option;
00088 
00089    fGLList = 0;
00090    fLastPoint = 0;
00091 }
00092 
00093 //______________________________________________________________________________
00094 TPointsArray3D::TPointsArray3D(Int_t n, Float_t *p, Option_t *option)
00095 {
00096 //*-*-*-*-*-*-*-*-*-*-*-*-*3-D Point3D normal constructor*-*-*-*-*-*-*-*-*-*-*-*
00097 //*-*                      ===============================
00098 //*-*  If n < 0 the default size (2 points) is set
00099 //*-*
00100 
00101    if (n < 1) fN = 2;  // Set the default size for this object
00102    else fN = n;
00103 
00104    fP = new Float_t[3*fN];
00105    if (n > 0) {
00106       memcpy(fP,p,3*fN*sizeof(Float_t));
00107       fLastPoint = fN-1;
00108    } else {
00109       memset(fP,0,3*fN*sizeof(Float_t));
00110       fLastPoint = -1;
00111    }
00112    fOption = option;
00113 
00114    fGLList = 0;
00115    fLastPoint = 0;
00116 }
00117 
00118 
00119 //______________________________________________________________________________
00120 TPointsArray3D::TPointsArray3D(Int_t n, Float_t *x, Float_t *y, Float_t *z, Option_t *option)
00121 {
00122 //*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine normal constructor*-*-*-*-*-*-*-*-*-*-*-*
00123 //*-*                      ===============================
00124 //*-*  If n < 0 the default size (2 points) is set
00125 //*-*
00126 
00127    fLastPoint = -1;
00128    if (n < 1) fN = 2;  // Set the default size for this object
00129    else fN = n;
00130 
00131    fP = new Float_t[3*fN];
00132    Int_t j = 0;
00133    if (n > 0) {
00134       for (Int_t i=0; i<n;i++) {
00135          fP[j++] = x[i];
00136          fP[j++] = y[i];
00137          fP[j++] = z[i];
00138       }
00139       fLastPoint = fN-1;
00140    } else {
00141       memset(fP,0,3*fN*sizeof(Float_t));
00142    }
00143    fOption = option;
00144 
00145    fGLList = 0;
00146    fLastPoint = 0;
00147 }
00148 
00149 
00150 //______________________________________________________________________________
00151 TPointsArray3D::~TPointsArray3D()
00152 {
00153 //*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine default destructor*-*-*-*-*-*-*-*-*-*-*-*
00154 //*-*                      ===============================
00155 
00156    if (fP) delete [] fP;
00157 
00158 }
00159 
00160 
00161 //______________________________________________________________________________
00162 TPointsArray3D::TPointsArray3D(const TPointsArray3D &point) : TPoints3DABC(point),
00163                                                               fN(point.fN),fP(0),fGLList(point.fGLList),fLastPoint(point.fLastPoint)
00164 {
00165    //to be documented
00166    ((TPointsArray3D&)point).Copy(*this);
00167 }
00168 
00169 
00170 //______________________________________________________________________________
00171 void TPointsArray3D::Copy(TObject &obj) const
00172 {
00173 //*-*-*-*-*-*-*-*-*-*-*-*-*Copy this TPointsArray3D to another *-*-*-*-*-*-*-*-*-*-*-*
00174 //*-*                      ==============================
00175 
00176    TObject::Copy(obj);
00177    ((TPointsArray3D&)obj).fN = fN;
00178    if (((TPointsArray3D&)obj).fP)
00179       delete [] ((TPointsArray3D&)obj).fP;
00180    ((TPointsArray3D&)obj).fP = new Float_t[3*fN];
00181    for (Int_t i=0; i<3*fN;i++)  {((TPointsArray3D&)obj).fP[i] = fP[i];}
00182    ((TPointsArray3D&)obj).fOption = fOption;
00183    ((TPointsArray3D&)obj).fLastPoint = fLastPoint;
00184 }
00185 
00186 
00187 //______________________________________________________________________________
00188 Int_t TPointsArray3D::DistancetoPrimitive(Int_t px, Int_t py)
00189 {
00190 //*-*-*-*-*-*-*Compute distance from point px,py to a 3-D points *-*-*-*-*-*-*
00191 //*-*          =====================================================
00192 //*-*
00193 //*-*  Compute the closest distance of approach from point px,py to each segment
00194 //*-*  of the polyline.
00195 //*-*  Returns when the distance found is below DistanceMaximum.
00196 //*-*  The distance is computed in pixels units.
00197 //*-*
00198 //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00199 
00200    const Int_t inaxis = 7;
00201    Float_t dist = 9999;
00202 
00203    Int_t puxmin = gPad->XtoAbsPixel(gPad->GetUxmin());
00204    Int_t puymin = gPad->YtoAbsPixel(gPad->GetUymin());
00205    Int_t puxmax = gPad->XtoAbsPixel(gPad->GetUxmax());
00206    Int_t puymax = gPad->YtoAbsPixel(gPad->GetUymax());
00207 
00208 //*-*- return if point is not in the user area
00209    if (px < puxmin - inaxis) return Int_t (dist);
00210    if (py > puymin + inaxis) return Int_t (dist);
00211    if (px > puxmax + inaxis) return Int_t (dist);
00212    if (py < puymax - inaxis) return Int_t (dist);
00213 
00214    TView *view = gPad->GetView();
00215    if (!view) return Int_t(dist);
00216    Int_t i;
00217    Float_t dpoint;
00218    Float_t xndc[3];
00219    Int_t x1,y1;
00220    Int_t size = Size();
00221    for (i=0;i<size;i++) {
00222       view->WCtoNDC(&fP[3*i], xndc);
00223       x1     = gPad->XtoAbsPixel(xndc[0]);
00224       y1     = gPad->YtoAbsPixel(xndc[1]);
00225       dpoint = (px-x1)*(px-x1) + (py-y1)*(py-y1);
00226       if (dpoint < dist) dist = dpoint;
00227    }
00228    return Int_t(TMath::Sqrt(dist));
00229 }
00230 
00231 
00232 //______________________________________________________________________________
00233 void TPointsArray3D::ExecuteEvent(Int_t event, Int_t px, Int_t py)
00234 {
00235 //*-*-*-*-*-*-*-*-*-*Execute action corresponding to one event*-*-*-*-*-*-*-*-*-*
00236 //*-*                =========================================
00237    if (gPad->GetView())
00238       gPad->GetView()->ExecuteRotateView(event, px, py);
00239 }
00240 
00241 //______________________________________________________________________________
00242 void TPointsArray3D::ls(Option_t *option) const
00243 {
00244 //*-*-*-*-*-*-*-*-*-*List this 3-D polyline with its attributes*-*-*-*-*-*-*
00245 //*-*                ==========================================
00246 
00247    TROOT::IndentLevel();
00248    cout << IsA()->GetName() << " N=" <<fN<<" Option="<<option<<endl;
00249 
00250 }
00251 //______________________________________________________________________________
00252 void TPointsArray3D::Print(Option_t *option) const
00253 {
00254 //*-*-*-*-*-*-*-*-*-*Dump this 3-D polyline with its attributes*-*-*-*-*-*-*-*-*
00255 //*-*                ==========================================
00256 
00257    cout <<"   " << IsA()->GetName() <<" Printing N=" <<fN<<" Option="<<option<<endl;
00258 }
00259 //______________________________________________________________________________
00260 Int_t TPointsArray3D::SetLastPosition(Int_t idx)
00261 {
00262    //to be documented
00263    fLastPoint = TMath::Min(idx,GetN()-1);
00264    return idx;
00265 }
00266 
00267 //______________________________________________________________________________
00268 Int_t TPointsArray3D::SetPoint(Int_t n, Float_t x, Float_t y, Float_t z)
00269 {
00270 //*-*-*-*-*-*-*-*-*-*Initialize one point of the 3-D polyline*-*-*-*-*-*-*-*-*-*
00271 //*-*                ========================================
00272 //*-*  if n is more then the current TPointsArray3D size (n > fN) - re-allocate this
00273 //*-*  The new size of the object will be fN += min(10,fN/4)
00274 //*-*
00275 //*-*  return the total number of points introduced
00276 //*-*
00277 
00278    if (n < 0) return n;
00279    if (!fP || n >= fN) {
00280    // re-allocate the object
00281       Int_t step = TMath::Max(10, fN/4);
00282       Float_t *savepoint = new Float_t [3*(fN+step)];
00283       if (fP && fN){
00284          memcpy(savepoint,fP,3*fN*sizeof(Float_t));
00285          delete [] fP;
00286       }
00287       fP = savepoint;
00288       fN += step;
00289    }
00290    fP[3*n  ] = x;
00291    fP[3*n+1] = y;
00292    fP[3*n+2] = z;
00293    fLastPoint = TMath::Max(fLastPoint,n);
00294    return fLastPoint;
00295 }
00296 
00297 //______________________________________________________________________________
00298 Int_t TPointsArray3D::SetPoints(Int_t n, Float_t *p, Option_t *option)
00299 {
00300 //*-*-*-*-*-*-*-*-*-*-*Set new values for this 3-D polyline*-*-*-*-*-*-*-*-*-*-*
00301 //*-*                  ====================================
00302 //*-* return the total number of points introduced
00303 //*-*
00304 
00305    if (n < 0) return n;
00306    fN = n;
00307    if (fP) delete [] fP;
00308    fP = new Float_t[3*fN];
00309    for (Int_t i=0; i<3*fN;i++) {
00310       if (p) fP[i] = p[i];
00311       else   memset(fP,0,3*fN*sizeof(Float_t));
00312    }
00313    fOption = option;
00314    fLastPoint = fN-1;
00315    return fLastPoint;
00316 }
00317 
00318 //_______________________________________________________________________
00319 void TPointsArray3D::Streamer(TBuffer &b)
00320 {
00321 //*-*-*-*-*-*-*-*-*Stream a class object*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00322 //*-*              =========================================
00323    if (b.IsReading()) {
00324       b.ReadVersion();  //Version_t v = b.ReadVersion();
00325       TObject::Streamer(b);
00326       b >> fN;
00327       if (fN) {
00328          fP = new Float_t[3*fN];
00329          b.ReadFastArray(fP,3*fN);
00330       }
00331       fOption.Streamer(b);
00332       fLastPoint = fN;
00333    } else {
00334       b.WriteVersion(TPointsArray3D::IsA());
00335       TObject::Streamer(b);
00336       Int_t size = Size();
00337       b << size;
00338       if (size) b.WriteFastArray(fP, 3*size);
00339       fOption.Streamer(b);
00340    }
00341 }

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