StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TPointsArray3D.cxx
1 // @(#)root/table:$Id$
2 // Author: Valery Fine(fine@mail.cern.ch) 24/04/99
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "Riostream.h"
13 
14 #include "TPointsArray3D.h"
15 #include "TVirtualPad.h"
16 #include "TView.h"
17 #include "TClass.h"
18 #include "TROOT.h"
19 #include "TMath.h"
20 
21 //______________________________________________________________________________
22 //
23 // TPointsArray3D is an abstract class of the array of 3-dimensional points.
24 // It has 4 different constructors.
25 //
26 // This class has no implementation for Paint, Draw, and SavePrimitive methods
27 //
28 // First one, without any parameters TPointsArray3D(), we call 'default
29 // constructor' and it's used in a case that just an initialisation is
30 // needed (i.e. pointer declaration).
31 //
32 // Example:
33 // TPointsArray3D *pl1 = new TPointsArray3D;
34 //
35 //
36 // Second one is 'normal constructor' with, usually, one parameter
37 // n (number of points), and it just allocates a space for the points.
38 //
39 // Example:
40 // TPointsArray3D pl1(150);
41 //
42 //
43 // Third one allocates a space for the points, and also makes
44 // initialisation from the given array.
45 //
46 // Example:
47 // TPointsArray3D pl1(150, pointerToAnArray);
48 //
49 //
50 // Fourth one is, almost, similar to the constructor above, except
51 // initialisation is provided with three independent arrays (array of
52 // x coordinates, y coordinates and z coordinates).
53 //
54 // Example:
55 // TPointsArray3D pl1(150, xArray, yArray, zArray);
56 //
57 
58 ClassImp(TPointsArray3D);
59 
62 
64 {
65  fN = 0;
66  fP = 0;
67  fLastPoint = -1;
68  fGLList = 0;
69  fLastPoint = 0;
70 }
71 
72 
76 
77 TPointsArray3D::TPointsArray3D(Int_t n, Option_t *option)
78 {
79  fLastPoint = -1;
80  if (n < 1) fN = 2; // Set the default size for this object
81  else fN = n;
82 
83  fP = new Float_t[3*fN];
84  memset(fP,0,3*fN*sizeof(Float_t));
85  fOption = option;
86 
87  fGLList = 0;
88  fLastPoint = 0;
89 }
90 
94 
95 TPointsArray3D::TPointsArray3D(Int_t n, Float_t *p, Option_t *option)
96 {
97  if (n < 1) fN = 2; // Set the default size for this object
98  else fN = n;
99 
100  fP = new Float_t[3*fN];
101  if (n > 0) {
102  memcpy(fP,p,3*fN*sizeof(Float_t));
103  fLastPoint = fN-1;
104  } else {
105  memset(fP,0,3*fN*sizeof(Float_t));
106  fLastPoint = -1;
107  }
108  fOption = option;
109 
110  fGLList = 0;
111  fLastPoint = 0;
112 }
113 
114 
118 
119 TPointsArray3D::TPointsArray3D(Int_t n, Float_t *x, Float_t *y, Float_t *z, Option_t *option)
120 {
121  fLastPoint = -1;
122  if (n < 1) fN = 2; // Set the default size for this object
123  else fN = n;
124 
125  fP = new Float_t[3*fN];
126  Int_t j = 0;
127  if (n > 0) {
128  for (Int_t i=0; i<n;i++) {
129  fP[j++] = x[i];
130  fP[j++] = y[i];
131  fP[j++] = z[i];
132  }
133  fLastPoint = fN-1;
134  } else {
135  memset(fP,0,3*fN*sizeof(Float_t));
136  }
137  fOption = option;
138 
139  fGLList = 0;
140  fLastPoint = 0;
141 }
142 
143 
146 
148 {
149  if (fP) delete [] fP;
150 
151 }
152 
153 
156 
157 TPointsArray3D::TPointsArray3D(const TPointsArray3D &point) : TPoints3DABC(point),
158  fN(point.fN),fP(0),fGLList(point.fGLList),fLastPoint(point.fLastPoint)
159 {
160  ((TPointsArray3D&)point).Copy(*this);
161 }
162 
163 
166 
167 void TPointsArray3D::Copy(TObject &obj) const
168 {
169  TObject::Copy(obj);
170  ((TPointsArray3D&)obj).fN = fN;
171  if (((TPointsArray3D&)obj).fP)
172  delete [] ((TPointsArray3D&)obj).fP;
173  ((TPointsArray3D&)obj).fP = new Float_t[3*fN];
174  for (Int_t i=0; i<3*fN;i++) {((TPointsArray3D&)obj).fP[i] = fP[i];}
175  ((TPointsArray3D&)obj).fOption = fOption;
176  ((TPointsArray3D&)obj).fLastPoint = fLastPoint;
177 }
178 
179 
187 
188 Int_t TPointsArray3D::DistancetoPrimitive(Int_t px, Int_t py)
189 {
190  const Int_t inaxis = 7;
191  Float_t dist = 9999;
192 
193  Int_t puxmin = gPad->XtoAbsPixel(gPad->GetUxmin());
194  Int_t puymin = gPad->YtoAbsPixel(gPad->GetUymin());
195  Int_t puxmax = gPad->XtoAbsPixel(gPad->GetUxmax());
196  Int_t puymax = gPad->YtoAbsPixel(gPad->GetUymax());
197 
198 //*-*- return if point is not in the user area
199  if (px < puxmin - inaxis) return Int_t (dist);
200  if (py > puymin + inaxis) return Int_t (dist);
201  if (px > puxmax + inaxis) return Int_t (dist);
202  if (py < puymax - inaxis) return Int_t (dist);
203 
204  TView *view = gPad->GetView();
205  if (!view) return Int_t(dist);
206  Int_t i;
207  Float_t dpoint;
208  Float_t xndc[3];
209  Int_t x1,y1;
210  Int_t size = Size();
211  for (i=0;i<size;i++) {
212  view->WCtoNDC(&fP[3*i], xndc);
213  x1 = gPad->XtoAbsPixel(xndc[0]);
214  y1 = gPad->YtoAbsPixel(xndc[1]);
215  dpoint = (px-x1)*(px-x1) + (py-y1)*(py-y1);
216  if (dpoint < dist) dist = dpoint;
217  }
218  return Int_t(TMath::Sqrt(dist));
219 }
220 
221 
224 
225 void TPointsArray3D::ExecuteEvent(Int_t event, Int_t px, Int_t py)
226 {
227  if (gPad->GetView())
228  gPad->GetView()->ExecuteRotateView(event, px, py);
229 }
230 
233 
234 void TPointsArray3D::ls(Option_t *option) const
235 {
236  TROOT::IndentLevel();
237  std::cout << IsA()->GetName() << " N=" <<fN<<" Option="<<option<<std::endl;
238 
239 }
242 
243 void TPointsArray3D::Print(Option_t *option) const
244 {
245  std::cout <<" " << IsA()->GetName() <<" Printing N=" <<fN<<" Option="<<option<<std::endl;
246 }
249 
251 {
252  fLastPoint = TMath::Min(idx,GetN()-1);
253  return idx;
254 }
255 
262 
263 Int_t TPointsArray3D::SetPoint(Int_t n, Float_t x, Float_t y, Float_t z)
264 {
265  if (n < 0) return n;
266  if (!fP || n >= fN) {
267  // re-allocate the object
268  Int_t step = TMath::Max(10, fN/4);
269  Float_t *savepoint = new Float_t [3*(fN+step)];
270  if (fP && fN){
271  memcpy(savepoint,fP,3*fN*sizeof(Float_t));
272  delete [] fP;
273  }
274  fP = savepoint;
275  fN += step;
276  }
277  fP[3*n ] = x;
278  fP[3*n+1] = y;
279  fP[3*n+2] = z;
280  fLastPoint = TMath::Max(fLastPoint,n);
281  return fLastPoint;
282 }
283 
287 
288 Int_t TPointsArray3D::SetPoints(Int_t n, Float_t *p, Option_t *option)
289 {
290  if (n < 0) return n;
291  fN = n;
292  if (fP) delete [] fP;
293  fP = new Float_t[3*fN];
294  for (Int_t i=0; i<3*fN;i++) {
295  if (p) fP[i] = p[i];
296  else memset(fP,0,3*fN*sizeof(Float_t));
297  }
298  fOption = option;
299  fLastPoint = fN-1;
300  return fLastPoint;
301 }
302 
305 
306 void TPointsArray3D::Streamer(TBuffer &b)
307 {
308  if (b.IsReading()) {
309  b.ReadVersion(); //Version_t v = b.ReadVersion();
310  TObject::Streamer(b);
311  b >> fN;
312  if (fN) {
313  fP = new Float_t[3*fN];
314  b.ReadFastArray(fP,3*fN);
315  }
316  fOption.Streamer(b);
317  fLastPoint = fN;
318  } else {
319  b.WriteVersion(TPointsArray3D::IsA());
320  TObject::Streamer(b);
321  Int_t size = Size();
322  b << size;
323  if (size) b.WriteFastArray(fP, 3*size);
324  fOption.Streamer(b);
325  }
326 }
virtual Int_t SetLastPosition(Int_t idx)
to be documented
virtual ~TPointsArray3D()
3-D PolyLine default destructor.
virtual void ls(Option_t *option="") const
List this 3-D polyline with its attributes.
virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py)
Execute action corresponding to one event.
virtual Int_t SetPoints(Int_t n, Float_t *p=0, Option_t *option="")
virtual void Print(Option_t *option="") const
Dump this 3-D polyline with its attributes.
virtual Int_t DistancetoPrimitive(Int_t px, Int_t py)
TPointsArray3D()
3-D PolyLine default constructor.
virtual void Copy(TObject &points) const
Copy this TPointsArray3D to another.
virtual Int_t SetPoint(Int_t point, Float_t x, Float_t y, Float_t z)