ROOT logo
// @(#)root/table:$Id: TPointsArray3D.cxx 21414 2007-12-17 14:15:59Z brun $
// Author: Valery Fine(fine@mail.cern.ch)   24/04/99

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"

#include "TPointsArray3D.h"
#include "TVirtualPad.h"
#include "TView.h"
#include "TClass.h"
#include "TROOT.h"
#include "TMath.h"

//______________________________________________________________________________
//
// TPointsArray3D is an abstract class of the array of 3-dimensional points.
// It has 4 different constructors.
//
// This class has no implementation for Paint, Draw, and SavePrimitive methods
//
//   First one, without any parameters TPointsArray3D(), we call 'default
// constructor' and it's used in a case that just an initialisation is
// needed (i.e. pointer declaration).
//
//       Example:
//                 TPointsArray3D *pl1 = new TPointsArray3D;
//
//
//   Second one is 'normal constructor' with, usually, one parameter
// n (number of points), and it just allocates a space for the points.
//
//       Example:
//                 TPointsArray3D pl1(150);
//
//
//   Third one allocates a space for the points, and also makes
// initialisation from the given array.
//
//       Example:
//                 TPointsArray3D pl1(150, pointerToAnArray);
//
//
//   Fourth one is, almost, similar to the constructor above, except
// initialisation is provided with three independent arrays (array of
// x coordinates, y coordinates and z coordinates).
//
//       Example:
//                 TPointsArray3D pl1(150, xArray, yArray, zArray);
//

ClassImp(TPointsArray3D)

//______________________________________________________________________________
TPointsArray3D::TPointsArray3D()
{
//*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine default constructor*-*-*-*-*-*-*-*-*-*-*
//*-*                      ================================

   fP = 0;
   fLastPoint = -1;
}


//______________________________________________________________________________
TPointsArray3D::TPointsArray3D(Int_t n, Option_t *option)
{
//*-*-*-*-*-*3-D PolyLine normal constructor without initialisation*-*-*-*-*-*-*
//*-*        ======================================================
//*-*  If n < 0 the default size (2 points) is set
//*-*
   fLastPoint = -1;
   if (n < 1) fN = 2;  // Set the default size for this object
   else fN = n;

   fP = new Float_t[3*fN];
   memset(fP,0,3*fN*sizeof(Float_t));
   fOption = option;
}

//______________________________________________________________________________
TPointsArray3D::TPointsArray3D(Int_t n, Float_t *p, Option_t *option)
{
//*-*-*-*-*-*-*-*-*-*-*-*-*3-D Point3D normal constructor*-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ===============================
//*-*  If n < 0 the default size (2 points) is set
//*-*

   if (n < 1) fN = 2;  // Set the default size for this object
   else fN = n;

   fP = new Float_t[3*fN];
   if (n > 0) {
      memcpy(fP,p,3*fN*sizeof(Float_t));
      fLastPoint = fN-1;
   } else {
      memset(fP,0,3*fN*sizeof(Float_t));
      fLastPoint = -1;
   }
   fOption = option;
}


//______________________________________________________________________________
TPointsArray3D::TPointsArray3D(Int_t n, Float_t *x, Float_t *y, Float_t *z, Option_t *option)
{
//*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine normal constructor*-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ===============================
//*-*  If n < 0 the default size (2 points) is set
//*-*

   fLastPoint = -1;
   if (n < 1) fN = 2;  // Set the default size for this object
   else fN = n;

   fP = new Float_t[3*fN];
   Int_t j = 0;
   if (n > 0) {
      for (Int_t i=0; i<n;i++) {
         fP[j++] = x[i];
         fP[j++] = y[i];
         fP[j++] = z[i];
      }
      fLastPoint = fN-1;
   } else {
      memset(fP,0,3*fN*sizeof(Float_t));
   }
   fOption = option;
}


//______________________________________________________________________________
TPointsArray3D::~TPointsArray3D()
{
//*-*-*-*-*-*-*-*-*-*-*-*-*3-D PolyLine default destructor*-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ===============================

   if (fP) delete [] fP;

}


//______________________________________________________________________________
TPointsArray3D::TPointsArray3D(const TPointsArray3D &point) : TPoints3DABC(point)
{
   //to be documented
   ((TPointsArray3D&)point).Copy(*this);
}


//______________________________________________________________________________
void TPointsArray3D::Copy(TObject &obj) const
{
//*-*-*-*-*-*-*-*-*-*-*-*-*Copy this TPointsArray3D to another *-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ==============================

   TObject::Copy(obj);
   ((TPointsArray3D&)obj).fN = fN;
   if (((TPointsArray3D&)obj).fP)
      delete [] ((TPointsArray3D&)obj).fP;
   ((TPointsArray3D&)obj).fP = new Float_t[3*fN];
   for (Int_t i=0; i<3*fN;i++)  {((TPointsArray3D&)obj).fP[i] = fP[i];}
   ((TPointsArray3D&)obj).fOption = fOption;
   ((TPointsArray3D&)obj).fLastPoint = fLastPoint;
}


//______________________________________________________________________________
Int_t TPointsArray3D::DistancetoPrimitive(Int_t px, Int_t py)
{
//*-*-*-*-*-*-*Compute distance from point px,py to a 3-D points *-*-*-*-*-*-*
//*-*          =====================================================
//*-*
//*-*  Compute the closest distance of approach from point px,py to each segment
//*-*  of the polyline.
//*-*  Returns when the distance found is below DistanceMaximum.
//*-*  The distance is computed in pixels units.
//*-*
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

   const Int_t inaxis = 7;
   Float_t dist = 9999;

   Int_t puxmin = gPad->XtoAbsPixel(gPad->GetUxmin());
   Int_t puymin = gPad->YtoAbsPixel(gPad->GetUymin());
   Int_t puxmax = gPad->XtoAbsPixel(gPad->GetUxmax());
   Int_t puymax = gPad->YtoAbsPixel(gPad->GetUymax());

//*-*- return if point is not in the user area
   if (px < puxmin - inaxis) return Int_t (dist);
   if (py > puymin + inaxis) return Int_t (dist);
   if (px > puxmax + inaxis) return Int_t (dist);
   if (py < puymax - inaxis) return Int_t (dist);

   TView *view = gPad->GetView();
   if (!view) return Int_t(dist);
   Int_t i;
   Float_t dpoint;
   Float_t xndc[3];
   Int_t x1,y1;
   Int_t size = Size();
   for (i=0;i<size;i++) {
      view->WCtoNDC(&fP[3*i], xndc);
      x1     = gPad->XtoAbsPixel(xndc[0]);
      y1     = gPad->YtoAbsPixel(xndc[1]);
      dpoint = (px-x1)*(px-x1) + (py-y1)*(py-y1);
      if (dpoint < dist) dist = dpoint;
   }
   return Int_t(TMath::Sqrt(dist));
}


//______________________________________________________________________________
void TPointsArray3D::ExecuteEvent(Int_t event, Int_t px, Int_t py)
{
//*-*-*-*-*-*-*-*-*-*Execute action corresponding to one event*-*-*-*-*-*-*-*-*-*
//*-*                =========================================
   if (gPad->GetView())
      gPad->GetView()->ExecuteRotateView(event, px, py);
}

//______________________________________________________________________________
void TPointsArray3D::ls(Option_t *option) const
{
//*-*-*-*-*-*-*-*-*-*List this 3-D polyline with its attributes*-*-*-*-*-*-*
//*-*                ==========================================

   TROOT::IndentLevel();
   cout << IsA()->GetName() << " N=" <<fN<<" Option="<<option<<endl;

}
//______________________________________________________________________________
void TPointsArray3D::Print(Option_t *option) const
{
//*-*-*-*-*-*-*-*-*-*Dump this 3-D polyline with its attributes*-*-*-*-*-*-*-*-*
//*-*                ==========================================

   cout <<"   " << IsA()->GetName() <<" Printing N=" <<fN<<" Option="<<option<<endl;
}
//______________________________________________________________________________
Int_t TPointsArray3D::SetLastPosition(Int_t idx)
{
   //to be documented
   fLastPoint = TMath::Min(idx,GetN()-1);
   return idx;
}

//______________________________________________________________________________
Int_t TPointsArray3D::SetPoint(Int_t n, Float_t x, Float_t y, Float_t z)
{
//*-*-*-*-*-*-*-*-*-*Initialize one point of the 3-D polyline*-*-*-*-*-*-*-*-*-*
//*-*                ========================================
//*-*  if n is more then the current TPointsArray3D size (n > fN) - re-allocate this
//*-*  The new size of the object will be fN += min(10,fN/4)
//*-*
//*-*  return the total number of points introduced
//*-*

   if (n < 0) return n;
   if (!fP || n >= fN) {
   // re-allocate the object
      Int_t step = TMath::Max(10, fN/4);
      Float_t *savepoint = new Float_t [3*(fN+step)];
      if (fP && fN){
         memcpy(savepoint,fP,3*fN*sizeof(Float_t));
         delete [] fP;
      }
      fP = savepoint;
      fN += step;
   }
   fP[3*n  ] = x;
   fP[3*n+1] = y;
   fP[3*n+2] = z;
   fLastPoint = TMath::Max(fLastPoint,n);
   return fLastPoint;
}

//______________________________________________________________________________
Int_t TPointsArray3D::SetPoints(Int_t n, Float_t *p, Option_t *option)
{
//*-*-*-*-*-*-*-*-*-*-*Set new values for this 3-D polyline*-*-*-*-*-*-*-*-*-*-*
//*-*                  ====================================
//*-* return the total number of points introduced
//*-*

   if (n < 0) return n;
   fN = n;
   if (fP) delete [] fP;
   fP = new Float_t[3*fN];
   for (Int_t i=0; i<3*fN;i++) {
      if (p) fP[i] = p[i];
      else   memset(fP,0,3*fN*sizeof(Float_t));
   }
   fOption = option;
   fLastPoint = fN-1;
   return fLastPoint;
}

//_______________________________________________________________________
void TPointsArray3D::Streamer(TBuffer &b)
{
//*-*-*-*-*-*-*-*-*Stream a class object*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*              =========================================
   if (b.IsReading()) {
      b.ReadVersion();  //Version_t v = b.ReadVersion();
      TObject::Streamer(b);
      b >> fN;
      if (fN) {
         fP = new Float_t[3*fN];
         b.ReadFastArray(fP,3*fN);
      }
      fOption.Streamer(b);
      fLastPoint = fN;
   } else {
      b.WriteVersion(TPointsArray3D::IsA());
      TObject::Streamer(b);
      Int_t size = Size();
      b << size;
      if (size) b.WriteFastArray(fP, 3*size);
      fOption.Streamer(b);
   }
}
 TPointsArray3D.cxx:1
 TPointsArray3D.cxx:2
 TPointsArray3D.cxx:3
 TPointsArray3D.cxx:4
 TPointsArray3D.cxx:5
 TPointsArray3D.cxx:6
 TPointsArray3D.cxx:7
 TPointsArray3D.cxx:8
 TPointsArray3D.cxx:9
 TPointsArray3D.cxx:10
 TPointsArray3D.cxx:11
 TPointsArray3D.cxx:12
 TPointsArray3D.cxx:13
 TPointsArray3D.cxx:14
 TPointsArray3D.cxx:15
 TPointsArray3D.cxx:16
 TPointsArray3D.cxx:17
 TPointsArray3D.cxx:18
 TPointsArray3D.cxx:19
 TPointsArray3D.cxx:20
 TPointsArray3D.cxx:21
 TPointsArray3D.cxx:22
 TPointsArray3D.cxx:23
 TPointsArray3D.cxx:24
 TPointsArray3D.cxx:25
 TPointsArray3D.cxx:26
 TPointsArray3D.cxx:27
 TPointsArray3D.cxx:28
 TPointsArray3D.cxx:29
 TPointsArray3D.cxx:30
 TPointsArray3D.cxx:31
 TPointsArray3D.cxx:32
 TPointsArray3D.cxx:33
 TPointsArray3D.cxx:34
 TPointsArray3D.cxx:35
 TPointsArray3D.cxx:36
 TPointsArray3D.cxx:37
 TPointsArray3D.cxx:38
 TPointsArray3D.cxx:39
 TPointsArray3D.cxx:40
 TPointsArray3D.cxx:41
 TPointsArray3D.cxx:42
 TPointsArray3D.cxx:43
 TPointsArray3D.cxx:44
 TPointsArray3D.cxx:45
 TPointsArray3D.cxx:46
 TPointsArray3D.cxx:47
 TPointsArray3D.cxx:48
 TPointsArray3D.cxx:49
 TPointsArray3D.cxx:50
 TPointsArray3D.cxx:51
 TPointsArray3D.cxx:52
 TPointsArray3D.cxx:53
 TPointsArray3D.cxx:54
 TPointsArray3D.cxx:55
 TPointsArray3D.cxx:56
 TPointsArray3D.cxx:57
 TPointsArray3D.cxx:58
 TPointsArray3D.cxx:59
 TPointsArray3D.cxx:60
 TPointsArray3D.cxx:61
 TPointsArray3D.cxx:62
 TPointsArray3D.cxx:63
 TPointsArray3D.cxx:64
 TPointsArray3D.cxx:65
 TPointsArray3D.cxx:66
 TPointsArray3D.cxx:67
 TPointsArray3D.cxx:68
 TPointsArray3D.cxx:69
 TPointsArray3D.cxx:70
 TPointsArray3D.cxx:71
 TPointsArray3D.cxx:72
 TPointsArray3D.cxx:73
 TPointsArray3D.cxx:74
 TPointsArray3D.cxx:75
 TPointsArray3D.cxx:76
 TPointsArray3D.cxx:77
 TPointsArray3D.cxx:78
 TPointsArray3D.cxx:79
 TPointsArray3D.cxx:80
 TPointsArray3D.cxx:81
 TPointsArray3D.cxx:82
 TPointsArray3D.cxx:83
 TPointsArray3D.cxx:84
 TPointsArray3D.cxx:85
 TPointsArray3D.cxx:86
 TPointsArray3D.cxx:87
 TPointsArray3D.cxx:88
 TPointsArray3D.cxx:89
 TPointsArray3D.cxx:90
 TPointsArray3D.cxx:91
 TPointsArray3D.cxx:92
 TPointsArray3D.cxx:93
 TPointsArray3D.cxx:94
 TPointsArray3D.cxx:95
 TPointsArray3D.cxx:96
 TPointsArray3D.cxx:97
 TPointsArray3D.cxx:98
 TPointsArray3D.cxx:99
 TPointsArray3D.cxx:100
 TPointsArray3D.cxx:101
 TPointsArray3D.cxx:102
 TPointsArray3D.cxx:103
 TPointsArray3D.cxx:104
 TPointsArray3D.cxx:105
 TPointsArray3D.cxx:106
 TPointsArray3D.cxx:107
 TPointsArray3D.cxx:108
 TPointsArray3D.cxx:109
 TPointsArray3D.cxx:110
 TPointsArray3D.cxx:111
 TPointsArray3D.cxx:112
 TPointsArray3D.cxx:113
 TPointsArray3D.cxx:114
 TPointsArray3D.cxx:115
 TPointsArray3D.cxx:116
 TPointsArray3D.cxx:117
 TPointsArray3D.cxx:118
 TPointsArray3D.cxx:119
 TPointsArray3D.cxx:120
 TPointsArray3D.cxx:121
 TPointsArray3D.cxx:122
 TPointsArray3D.cxx:123
 TPointsArray3D.cxx:124
 TPointsArray3D.cxx:125
 TPointsArray3D.cxx:126
 TPointsArray3D.cxx:127
 TPointsArray3D.cxx:128
 TPointsArray3D.cxx:129
 TPointsArray3D.cxx:130
 TPointsArray3D.cxx:131
 TPointsArray3D.cxx:132
 TPointsArray3D.cxx:133
 TPointsArray3D.cxx:134
 TPointsArray3D.cxx:135
 TPointsArray3D.cxx:136
 TPointsArray3D.cxx:137
 TPointsArray3D.cxx:138
 TPointsArray3D.cxx:139
 TPointsArray3D.cxx:140
 TPointsArray3D.cxx:141
 TPointsArray3D.cxx:142
 TPointsArray3D.cxx:143
 TPointsArray3D.cxx:144
 TPointsArray3D.cxx:145
 TPointsArray3D.cxx:146
 TPointsArray3D.cxx:147
 TPointsArray3D.cxx:148
 TPointsArray3D.cxx:149
 TPointsArray3D.cxx:150
 TPointsArray3D.cxx:151
 TPointsArray3D.cxx:152
 TPointsArray3D.cxx:153
 TPointsArray3D.cxx:154
 TPointsArray3D.cxx:155
 TPointsArray3D.cxx:156
 TPointsArray3D.cxx:157
 TPointsArray3D.cxx:158
 TPointsArray3D.cxx:159
 TPointsArray3D.cxx:160
 TPointsArray3D.cxx:161
 TPointsArray3D.cxx:162
 TPointsArray3D.cxx:163
 TPointsArray3D.cxx:164
 TPointsArray3D.cxx:165
 TPointsArray3D.cxx:166
 TPointsArray3D.cxx:167
 TPointsArray3D.cxx:168
 TPointsArray3D.cxx:169
 TPointsArray3D.cxx:170
 TPointsArray3D.cxx:171
 TPointsArray3D.cxx:172
 TPointsArray3D.cxx:173
 TPointsArray3D.cxx:174
 TPointsArray3D.cxx:175
 TPointsArray3D.cxx:176
 TPointsArray3D.cxx:177
 TPointsArray3D.cxx:178
 TPointsArray3D.cxx:179
 TPointsArray3D.cxx:180
 TPointsArray3D.cxx:181
 TPointsArray3D.cxx:182
 TPointsArray3D.cxx:183
 TPointsArray3D.cxx:184
 TPointsArray3D.cxx:185
 TPointsArray3D.cxx:186
 TPointsArray3D.cxx:187
 TPointsArray3D.cxx:188
 TPointsArray3D.cxx:189
 TPointsArray3D.cxx:190
 TPointsArray3D.cxx:191
 TPointsArray3D.cxx:192
 TPointsArray3D.cxx:193
 TPointsArray3D.cxx:194
 TPointsArray3D.cxx:195
 TPointsArray3D.cxx:196
 TPointsArray3D.cxx:197
 TPointsArray3D.cxx:198
 TPointsArray3D.cxx:199
 TPointsArray3D.cxx:200
 TPointsArray3D.cxx:201
 TPointsArray3D.cxx:202
 TPointsArray3D.cxx:203
 TPointsArray3D.cxx:204
 TPointsArray3D.cxx:205
 TPointsArray3D.cxx:206
 TPointsArray3D.cxx:207
 TPointsArray3D.cxx:208
 TPointsArray3D.cxx:209
 TPointsArray3D.cxx:210
 TPointsArray3D.cxx:211
 TPointsArray3D.cxx:212
 TPointsArray3D.cxx:213
 TPointsArray3D.cxx:214
 TPointsArray3D.cxx:215
 TPointsArray3D.cxx:216
 TPointsArray3D.cxx:217
 TPointsArray3D.cxx:218
 TPointsArray3D.cxx:219
 TPointsArray3D.cxx:220
 TPointsArray3D.cxx:221
 TPointsArray3D.cxx:222
 TPointsArray3D.cxx:223
 TPointsArray3D.cxx:224
 TPointsArray3D.cxx:225
 TPointsArray3D.cxx:226
 TPointsArray3D.cxx:227
 TPointsArray3D.cxx:228
 TPointsArray3D.cxx:229
 TPointsArray3D.cxx:230
 TPointsArray3D.cxx:231
 TPointsArray3D.cxx:232
 TPointsArray3D.cxx:233
 TPointsArray3D.cxx:234
 TPointsArray3D.cxx:235
 TPointsArray3D.cxx:236
 TPointsArray3D.cxx:237
 TPointsArray3D.cxx:238
 TPointsArray3D.cxx:239
 TPointsArray3D.cxx:240
 TPointsArray3D.cxx:241
 TPointsArray3D.cxx:242
 TPointsArray3D.cxx:243
 TPointsArray3D.cxx:244
 TPointsArray3D.cxx:245
 TPointsArray3D.cxx:246
 TPointsArray3D.cxx:247
 TPointsArray3D.cxx:248
 TPointsArray3D.cxx:249
 TPointsArray3D.cxx:250
 TPointsArray3D.cxx:251
 TPointsArray3D.cxx:252
 TPointsArray3D.cxx:253
 TPointsArray3D.cxx:254
 TPointsArray3D.cxx:255
 TPointsArray3D.cxx:256
 TPointsArray3D.cxx:257
 TPointsArray3D.cxx:258
 TPointsArray3D.cxx:259
 TPointsArray3D.cxx:260
 TPointsArray3D.cxx:261
 TPointsArray3D.cxx:262
 TPointsArray3D.cxx:263
 TPointsArray3D.cxx:264
 TPointsArray3D.cxx:265
 TPointsArray3D.cxx:266
 TPointsArray3D.cxx:267
 TPointsArray3D.cxx:268
 TPointsArray3D.cxx:269
 TPointsArray3D.cxx:270
 TPointsArray3D.cxx:271
 TPointsArray3D.cxx:272
 TPointsArray3D.cxx:273
 TPointsArray3D.cxx:274
 TPointsArray3D.cxx:275
 TPointsArray3D.cxx:276
 TPointsArray3D.cxx:277
 TPointsArray3D.cxx:278
 TPointsArray3D.cxx:279
 TPointsArray3D.cxx:280
 TPointsArray3D.cxx:281
 TPointsArray3D.cxx:282
 TPointsArray3D.cxx:283
 TPointsArray3D.cxx:284
 TPointsArray3D.cxx:285
 TPointsArray3D.cxx:286
 TPointsArray3D.cxx:287
 TPointsArray3D.cxx:288
 TPointsArray3D.cxx:289
 TPointsArray3D.cxx:290
 TPointsArray3D.cxx:291
 TPointsArray3D.cxx:292
 TPointsArray3D.cxx:293
 TPointsArray3D.cxx:294
 TPointsArray3D.cxx:295
 TPointsArray3D.cxx:296
 TPointsArray3D.cxx:297
 TPointsArray3D.cxx:298
 TPointsArray3D.cxx:299
 TPointsArray3D.cxx:300
 TPointsArray3D.cxx:301
 TPointsArray3D.cxx:302
 TPointsArray3D.cxx:303
 TPointsArray3D.cxx:304
 TPointsArray3D.cxx:305
 TPointsArray3D.cxx:306
 TPointsArray3D.cxx:307
 TPointsArray3D.cxx:308
 TPointsArray3D.cxx:309
 TPointsArray3D.cxx:310
 TPointsArray3D.cxx:311
 TPointsArray3D.cxx:312
 TPointsArray3D.cxx:313
 TPointsArray3D.cxx:314
 TPointsArray3D.cxx:315
 TPointsArray3D.cxx:316
 TPointsArray3D.cxx:317
 TPointsArray3D.cxx:318
 TPointsArray3D.cxx:319
 TPointsArray3D.cxx:320
 TPointsArray3D.cxx:321
 TPointsArray3D.cxx:322
 TPointsArray3D.cxx:323
 TPointsArray3D.cxx:324
 TPointsArray3D.cxx:325
 TPointsArray3D.cxx:326
 TPointsArray3D.cxx:327
 TPointsArray3D.cxx:328