ROOT  6.06/09
Reference Guide
TPointsArray3D.cxx
Go to the documentation of this file.
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 
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 ///*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine default constructor*-*-*-*-*-*-*-*-*-*-*
62 ///*-* ================================
63 
65 {
66  fN = 0;
67  fP = 0;
68  fLastPoint = -1;
69  fGLList = 0;
70  fLastPoint = 0;
71 }
72 
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 ///*-*-*-*-*-*3-D PolyLine normal constructor without initialisation*-*-*-*-*-*-*
76 ///*-* ======================================================
77 ///*-* If n < 0 the default size (2 points) is set
78 ///*-*
79 
81 {
82  fLastPoint = -1;
83  if (n < 1) fN = 2; // Set the default size for this object
84  else fN = n;
85 
86  fP = new Float_t[3*fN];
87  memset(fP,0,3*fN*sizeof(Float_t));
88  fOption = option;
89 
90  fGLList = 0;
91  fLastPoint = 0;
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 ///*-*-*-*-*-*-*-*-*-*-*-*-*3-D Point3D normal constructor*-*-*-*-*-*-*-*-*-*-*-*
96 ///*-* ===============================
97 ///*-* If n < 0 the default size (2 points) is set
98 ///*-*
99 
101 {
102  if (n < 1) fN = 2; // Set the default size for this object
103  else fN = n;
104 
105  fP = new Float_t[3*fN];
106  if (n > 0) {
107  memcpy(fP,p,3*fN*sizeof(Float_t));
108  fLastPoint = fN-1;
109  } else {
110  memset(fP,0,3*fN*sizeof(Float_t));
111  fLastPoint = -1;
112  }
113  fOption = option;
114 
115  fGLList = 0;
116  fLastPoint = 0;
117 }
118 
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 ///*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine normal constructor*-*-*-*-*-*-*-*-*-*-*-*
122 ///*-* ===============================
123 ///*-* If n < 0 the default size (2 points) is set
124 ///*-*
125 
127 {
128  fLastPoint = -1;
129  if (n < 1) fN = 2; // Set the default size for this object
130  else fN = n;
131 
132  fP = new Float_t[3*fN];
133  Int_t j = 0;
134  if (n > 0) {
135  for (Int_t i=0; i<n;i++) {
136  fP[j++] = x[i];
137  fP[j++] = y[i];
138  fP[j++] = z[i];
139  }
140  fLastPoint = fN-1;
141  } else {
142  memset(fP,0,3*fN*sizeof(Float_t));
143  }
144  fOption = option;
145 
146  fGLList = 0;
147  fLastPoint = 0;
148 }
149 
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 ///*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine default destructor*-*-*-*-*-*-*-*-*-*-*-*
153 ///*-* ===============================
154 
156 {
157  if (fP) delete [] fP;
158 
159 }
160 
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 ///to be documented
164 
166  fN(point.fN),fP(0),fGLList(point.fGLList),fLastPoint(point.fLastPoint)
167 {
168  ((TPointsArray3D&)point).Copy(*this);
169 }
170 
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 ///*-*-*-*-*-*-*-*-*-*-*-*-*Copy this TPointsArray3D to another *-*-*-*-*-*-*-*-*-*-*-*
174 ///*-* ==============================
175 
177 {
178  TObject::Copy(obj);
179  ((TPointsArray3D&)obj).fN = fN;
180  if (((TPointsArray3D&)obj).fP)
181  delete [] ((TPointsArray3D&)obj).fP;
182  ((TPointsArray3D&)obj).fP = new Float_t[3*fN];
183  for (Int_t i=0; i<3*fN;i++) {((TPointsArray3D&)obj).fP[i] = fP[i];}
184  ((TPointsArray3D&)obj).fOption = fOption;
185  ((TPointsArray3D&)obj).fLastPoint = fLastPoint;
186 }
187 
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 ///*-*-*-*-*-*-*Compute distance from point px,py to a 3-D points *-*-*-*-*-*-*
191 ///*-* =====================================================
192 ///*-*
193 ///*-* Compute the closest distance of approach from point px,py to each segment
194 ///*-* of the polyline.
195 ///*-* Returns when the distance found is below DistanceMaximum.
196 ///*-* The distance is computed in pixels units.
197 ///*-*
198 ///*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
199 
201 {
202  const Int_t inaxis = 7;
203  Float_t dist = 9999;
204 
205  Int_t puxmin = gPad->XtoAbsPixel(gPad->GetUxmin());
206  Int_t puymin = gPad->YtoAbsPixel(gPad->GetUymin());
207  Int_t puxmax = gPad->XtoAbsPixel(gPad->GetUxmax());
208  Int_t puymax = gPad->YtoAbsPixel(gPad->GetUymax());
209 
210 //*-*- return if point is not in the user area
211  if (px < puxmin - inaxis) return Int_t (dist);
212  if (py > puymin + inaxis) return Int_t (dist);
213  if (px > puxmax + inaxis) return Int_t (dist);
214  if (py < puymax - inaxis) return Int_t (dist);
215 
216  TView *view = gPad->GetView();
217  if (!view) return Int_t(dist);
218  Int_t i;
219  Float_t dpoint;
220  Float_t xndc[3];
221  Int_t x1,y1;
222  Int_t size = Size();
223  for (i=0;i<size;i++) {
224  view->WCtoNDC(&fP[3*i], xndc);
225  x1 = gPad->XtoAbsPixel(xndc[0]);
226  y1 = gPad->YtoAbsPixel(xndc[1]);
227  dpoint = (px-x1)*(px-x1) + (py-y1)*(py-y1);
228  if (dpoint < dist) dist = dpoint;
229  }
230  return Int_t(TMath::Sqrt(dist));
231 }
232 
233 
234 ////////////////////////////////////////////////////////////////////////////////
235 ///*-*-*-*-*-*-*-*-*-*Execute action corresponding to one event*-*-*-*-*-*-*-*-*-*
236 ///*-* =========================================
237 
239 {
240  if (gPad->GetView())
241  gPad->GetView()->ExecuteRotateView(event, px, py);
242 }
243 
244 ////////////////////////////////////////////////////////////////////////////////
245 ///*-*-*-*-*-*-*-*-*-*List this 3-D polyline with its attributes*-*-*-*-*-*-*
246 ///*-* ==========================================
247 
248 void TPointsArray3D::ls(Option_t *option) const
249 {
251  std::cout << IsA()->GetName() << " N=" <<fN<<" Option="<<option<<std::endl;
252 
253 }
254 ////////////////////////////////////////////////////////////////////////////////
255 ///*-*-*-*-*-*-*-*-*-*Dump this 3-D polyline with its attributes*-*-*-*-*-*-*-*-*
256 ///*-* ==========================================
257 
258 void TPointsArray3D::Print(Option_t *option) const
259 {
260  std::cout <<" " << IsA()->GetName() <<" Printing N=" <<fN<<" Option="<<option<<std::endl;
261 }
262 ////////////////////////////////////////////////////////////////////////////////
263 ///to be documented
264 
266 {
267  fLastPoint = TMath::Min(idx,GetN()-1);
268  return idx;
269 }
270 
271 ////////////////////////////////////////////////////////////////////////////////
272 ///*-*-*-*-*-*-*-*-*-*Initialize one point of the 3-D polyline*-*-*-*-*-*-*-*-*-*
273 ///*-* ========================================
274 ///*-* if n is more then the current TPointsArray3D size (n > fN) - re-allocate this
275 ///*-* The new size of the object will be fN += min(10,fN/4)
276 ///*-*
277 ///*-* return the total number of points introduced
278 ///*-*
279 
281 {
282  if (n < 0) return n;
283  if (!fP || n >= fN) {
284  // re-allocate the object
285  Int_t step = TMath::Max(10, fN/4);
286  Float_t *savepoint = new Float_t [3*(fN+step)];
287  if (fP && fN){
288  memcpy(savepoint,fP,3*fN*sizeof(Float_t));
289  delete [] fP;
290  }
291  fP = savepoint;
292  fN += step;
293  }
294  fP[3*n ] = x;
295  fP[3*n+1] = y;
296  fP[3*n+2] = z;
298  return fLastPoint;
299 }
300 
301 ////////////////////////////////////////////////////////////////////////////////
302 ///*-*-*-*-*-*-*-*-*-*-*Set new values for this 3-D polyline*-*-*-*-*-*-*-*-*-*-*
303 ///*-* ====================================
304 ///*-* return the total number of points introduced
305 ///*-*
306 
308 {
309  if (n < 0) return n;
310  fN = n;
311  if (fP) delete [] fP;
312  fP = new Float_t[3*fN];
313  for (Int_t i=0; i<3*fN;i++) {
314  if (p) fP[i] = p[i];
315  else memset(fP,0,3*fN*sizeof(Float_t));
316  }
317  fOption = option;
318  fLastPoint = fN-1;
319  return fLastPoint;
320 }
321 
322 ////////////////////////////////////////////////////////////////////////////////
323 ///*-*-*-*-*-*-*-*-*Stream a class object*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
324 ///*-* =========================================
325 
326 void TPointsArray3D::Streamer(TBuffer &b)
327 {
328  if (b.IsReading()) {
329  b.ReadVersion(); //Version_t v = b.ReadVersion();
330  TObject::Streamer(b);
331  b >> fN;
332  if (fN) {
333  fP = new Float_t[3*fN];
334  b.ReadFastArray(fP,3*fN);
335  }
336  fOption.Streamer(b);
337  fLastPoint = fN;
338  } else {
340  TObject::Streamer(b);
341  Int_t size = Size();
342  b << size;
343  if (size) b.WriteFastArray(fP, 3*size);
344  fOption.Streamer(b);
345  }
346 }
virtual Int_t SetLastPosition(Int_t idx)
to be documented
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
virtual Int_t Size() const
Bool_t IsReading() const
Definition: TBuffer.h:81
virtual void WCtoNDC(const Float_t *pw, Float_t *pn)=0
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
virtual ~TPointsArray3D()
*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine default destructor*-*-*-*-*-*-*-*-*-*-*-* *-* =================...
virtual void ls(Option_t *option="") const
*-*-*-*-*-*-*-*-*-*List this 3-D polyline with its attributes*-*-*-*-*-*-* *-* ======================...
ClassImp(TPointsArray3D) TPointsArray3D
*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine default constructor*-*-*-*-*-*-*-*-*-*-* *-* ==================...
Abstract class to define Arrays of 3D points.
Definition: TPoints3DABC.h:27
See TView3D.
Definition: TView.h:36
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py)
*-*-*-*-*-*-*-*-*-*Execute action corresponding to one event*-*-*-*-*-*-*-*-*-* *-* =================...
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:170
int Int_t
Definition: RtypesCore.h:41
virtual Int_t SetPoints(Int_t n, Float_t *p=0, Option_t *option="")
*-*-*-*-*-*-*-*-*-*-*Set new values for this 3-D polyline*-*-*-*-*-*-*-*-*-*-* *-* ==================...
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
virtual void Print(Option_t *option="") const
*-*-*-*-*-*-*-*-*-*Dump this 3-D polyline with its attributes*-*-*-*-*-*-*-*-* *-* ==================...
Double_t x[n]
Definition: legend1.C:17
virtual Int_t DistancetoPrimitive(Int_t px, Int_t py)
*-*-*-*-*-*-*Compute distance from point px,py to a 3-D points *-*-*-*-*-*-* *-* ====================...
virtual void Copy(TObject &object) const
Copy this to obj.
Definition: TObject.cxx:122
TClass * IsA() const
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual void ReadFastArray(Bool_t *b, Int_t n)=0
virtual void WriteFastArray(const Bool_t *b, Int_t n)=0
virtual Int_t GetN() const
GetN() returns the number of allocated cells if any.
static const double x1[5]
Double_t y[n]
Definition: legend1.C:17
Mother of all ROOT objects.
Definition: TObject.h:58
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
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)
*-*-*-*-*-*-*-*-*-*Initialize one point of the 3-D polyline*-*-*-*-*-*-*-*-*-* *-* ==================...
#define gPad
Definition: TVirtualPad.h:288
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition: TROOT.cxx:2461
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
TObject * obj
const Int_t n
Definition: legend1.C:16
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0