Logo ROOT   6.16/01
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
64{
65 fN = 0;
66 fP = 0;
67 fLastPoint = -1;
68 fGLList = 0;
69 fLastPoint = 0;
70}
71
72
73////////////////////////////////////////////////////////////////////////////////
74/// 3-D PolyLine normal constructor without initialisation.
75/// If n < 0 the default size (2 points) is set.
76
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
91////////////////////////////////////////////////////////////////////////////////
92/// 3-D Point3D normal constructor.
93/// If n < 0 the default size (2 points) is set.
94
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
115////////////////////////////////////////////////////////////////////////////////
116/// 3-D PolyLine normal constructor.
117/// If n < 0 the default size (2 points) is set.
118
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
144////////////////////////////////////////////////////////////////////////////////
145/// 3-D PolyLine default destructor.
146
148{
149 if (fP) delete [] fP;
150
151}
152
153
154////////////////////////////////////////////////////////////////////////////////
155///to be documented
156
158 fN(point.fN),fP(0),fGLList(point.fGLList),fLastPoint(point.fLastPoint)
159{
160 ((TPointsArray3D&)point).Copy(*this);
161}
162
163
164////////////////////////////////////////////////////////////////////////////////
165/// Copy this TPointsArray3D to another.
166
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
180////////////////////////////////////////////////////////////////////////////////
181/// Compute distance from point px,py to a 3-D points.
182///
183/// Compute the closest distance of approach from point px,py to each segment
184/// of the polyline.
185/// Returns when the distance found is below DistanceMaximum.
186/// The distance is computed in pixels units.
187
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
222////////////////////////////////////////////////////////////////////////////////
223/// Execute action corresponding to one event.
224
226{
227 if (gPad->GetView())
228 gPad->GetView()->ExecuteRotateView(event, px, py);
229}
230
231////////////////////////////////////////////////////////////////////////////////
232/// List this 3-D polyline with its attributes.
233
234void TPointsArray3D::ls(Option_t *option) const
235{
237 std::cout << IsA()->GetName() << " N=" <<fN<<" Option="<<option<<std::endl;
238
239}
240////////////////////////////////////////////////////////////////////////////////
241/// Dump this 3-D polyline with its attributes.
242
244{
245 std::cout <<" " << IsA()->GetName() <<" Printing N=" <<fN<<" Option="<<option<<std::endl;
246}
247////////////////////////////////////////////////////////////////////////////////
248///to be documented
249
251{
252 fLastPoint = TMath::Min(idx,GetN()-1);
253 return idx;
254}
255
256////////////////////////////////////////////////////////////////////////////////
257/// Initialize one point of the 3-D polyline.
258/// if n is more then the current TPointsArray3D size (n > fN) - re-allocate this.
259/// The new size of the object will be fN += min(10,fN/4).
260///
261/// return the total number of points introduced.
262
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;
281 return fLastPoint;
282}
283
284////////////////////////////////////////////////////////////////////////////////
285/// Set new values for this 3-D polyline.
286/// return the total number of points introduced.
287
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
303////////////////////////////////////////////////////////////////////////////////
304/// Stream a class object.
305
306void 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}
#define b(i)
Definition: RSha256.hxx:100
static const double x1[5]
int Int_t
Definition: RtypesCore.h:41
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:363
#define gPad
Definition: TVirtualPad.h:286
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Copy(TObject &object) const
Copy this to obj.
Definition: TObject.cxx:61
Abstract class to define Arrays of 3D points.
Definition: TPoints3DABC.h:25
virtual Int_t Size() const
virtual Int_t SetLastPosition(Int_t idx)
to be documented
virtual void Copy(TObject &points) const
Copy this TPointsArray3D to another.
virtual ~TPointsArray3D()
3-D PolyLine default destructor.
virtual Int_t SetPoint(Int_t point, Float_t x, Float_t y, Float_t z)
Initialize one point of the 3-D polyline.
virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py)
Execute action corresponding to one event.
virtual Int_t GetN() const
GetN() returns the number of allocated cells if any.
virtual void ls(Option_t *option="") const
List this 3-D polyline with its attributes.
virtual void Print(Option_t *option="") const
Dump this 3-D polyline with its attributes.
virtual Int_t SetPoints(Int_t n, Float_t *p=0, Option_t *option="")
Set new values for this 3-D polyline.
TPointsArray3D()
3-D PolyLine default constructor.
virtual Int_t DistancetoPrimitive(Int_t px, Int_t py)
Compute distance from point px,py to a 3-D points.
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition: TROOT.cxx:2851
See TView3D.
Definition: TView.h:25
virtual void WCtoNDC(const Float_t *pw, Float_t *pn)=0
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:212
Double_t Sqrt(Double_t x)
Definition: TMath.h:679
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:180