// @(#)root/g3d:$Id$
// Author: Valery Fine(fine@mail.cern.ch)   04/05/99

// @(#)root/g3d:$Id$
// Author: Valery Fine(fine@mail.cern.ch)   24/04/99

#include "TPoints3DABC.h"
#include "TMath.h"

ClassImp(TPoints3DABC)

//______________________________________________________________________________
/* Begin_Html
<center><h2>The TPoints3DABC class</h2></center>
Abstract class to define Arrays of 3D points.
End_Html */



//______________________________________________________________________________
Int_t TPoints3DABC::Add(Float_t x, Float_t y, Float_t z)
{
   // Add one 3D point defined by x,y,z to the array of the points
   // as its last element

   return AddLast(x,y,z);
}


//______________________________________________________________________________
Int_t TPoints3DABC::AddLast(Float_t x, Float_t y, Float_t z)
{
   // Add one 3D point defined by x,y,z to the array of the points
   // as its last element

   return SetNextPoint(x,y,z);
}


//______________________________________________________________________________
Int_t TPoints3DABC::DistancetoLine(Int_t px, Int_t py, Float_t x1, Float_t y1, Float_t x2, Float_t y2, Int_t lineWidth )
{
   // Compute distance from point px,py to an axis of the band defined.
   //  by pair points  (x1,y1),(x2,y2) where lineWidth is the width of the band
   //
   //  Compute the closest distance of approach from point px,py to this line.
   //  The distance is computed in pixels units.
   //
   //
   //  Algorithm:
   //
   //    A(x1,y1)         P                             B(x2,y2)
   //    ------------------------------------------------
   //                     I
   //                     I
   //                     I
   //                     I
   //                    M(x,y)
   //
   //  Let us call  a = distance AM     a2=a**2
   //               b = distance BM     b2=b**2
   //               c = distance AB     c2=c**2
   //               d = distance PM     d2=d**2
   //               u = distance AP     u2=u**2
   //               v = distance BP     v2=v**2     c = u + v
   //
   //  d2 = a2 - u2
   //  d2 = b2 - v2  = b2 -(c-u)**2
   //     ==> u = (a2 -b2 +c2)/2c
   //
   //   Float_t x1    = gPad->XtoAbsPixel(xp1);
   //   Float_t y1    = gPad->YtoAbsPixel(yp1);
   //   Float_t x2    = gPad->XtoAbsPixel(xp2);
   //   Float_t y2    = gPad->YtoAbsPixel(yp2);

   Float_t xl, xt, yl, yt;
   Float_t x     = px;
   Float_t y     = py;
   if (x1 < x2) {xl = x1; xt = x2;}
   else         {xl = x2; xt = x1;}
   if (y1 < y2) {yl = y1; yt = y2;}
   else         {yl = y2; yt = y1;}
   if (x < xl-2 || x> xt+2) return 9999;  //following algorithm only valid in the box
   if (y < yl-2 || y> yt+2) return 9999;  //surrounding the line
   Float_t xx1   = x  - x1;
   Float_t xx2   = x  - x2;
   Float_t x1x2  = x1 - x2;
   Float_t yy1   = y  - y1;
   Float_t yy2   = y  - y2;
   Float_t y1y2  = y1 - y2;
   Float_t a2    = xx1*xx1   + yy1*yy1;
   Float_t b2    = xx2*xx2   + yy2*yy2;
   Float_t c2    = x1x2*x1x2 + y1y2*y1y2;
   if (c2 <= 0)  return 9999;
   Float_t c     = TMath::Sqrt(c2);
   Float_t u     = (a2 - b2 + c2)/(2*c);
   Float_t d2    = TMath::Abs(a2 - u*u);
   if (d2 < 0)   return 9999;

   return Int_t(TMath::Sqrt(d2) - 0.5*float(lineWidth));
}


//______________________________________________________________________________
Int_t TPoints3DABC::SetNextPoint(Float_t x, Float_t y, Float_t z)
{
   // Add one 3D point defined by x,y,z to the array of the points
   // as its last element

   return SetPoint(GetLastPosition()+1,x,y,z);
}


//______________________________________________________________________________
Int_t TPoints3DABC::GetN() const
{
   // GetN()  returns the number of allocated cells if any.
   //         GetN() > 0 shows how many cells
   //         can be available via GetP() method.
   //         GetN() == 0 then GetP() must return 0 as well

   return 0;
}


//______________________________________________________________________________
Float_t *TPoints3DABC::GetP() const
{
   // GetP()  returns the pointer to the float point array
   //         of points if available
   //         The number of the available celss can be found via
   //         GetN() method.
   //         GetN() > 0 shows how many cells

   return 0;
}


//______________________________________________________________________________
Float_t *TPoints3DABC::GetXYZ(Float_t *xyz,Int_t idx,Int_t num)  const
{
   // GetXYZ(Float_t *xyz,Int_t idx,Int_t num=1) fills the buffer supplied
   // by the calling code with the points information.
   //
   //  Input parameters:
   //
   //   Float_t *xyz - an external user supplied floating point array.
   //   Int_t    num - the total number of the points to be copied
   //                  the dimension of that array the size of the
   //                  array is num*sizeof(Float_t) at least
   //   Int_t    idx - The index of the first copy to be taken.
   //
   //  Return: The pointer to the buffer array supplied

   if (xyz) {
      Int_t size = TMath::Min(idx+num,Size());
      Int_t j=0;
      for (Int_t i=idx;i<size;i++) {
         xyz[j++] = GetX(i);
         xyz[j++] = GetY(i);
         xyz[j++] = GetZ(i);
      }
   }
   return xyz;
}
 TPoints3DABC.cxx:1
 TPoints3DABC.cxx:2
 TPoints3DABC.cxx:3
 TPoints3DABC.cxx:4
 TPoints3DABC.cxx:5
 TPoints3DABC.cxx:6
 TPoints3DABC.cxx:7
 TPoints3DABC.cxx:8
 TPoints3DABC.cxx:9
 TPoints3DABC.cxx:10
 TPoints3DABC.cxx:11
 TPoints3DABC.cxx:12
 TPoints3DABC.cxx:13
 TPoints3DABC.cxx:14
 TPoints3DABC.cxx:15
 TPoints3DABC.cxx:16
 TPoints3DABC.cxx:17
 TPoints3DABC.cxx:18
 TPoints3DABC.cxx:19
 TPoints3DABC.cxx:20
 TPoints3DABC.cxx:21
 TPoints3DABC.cxx:22
 TPoints3DABC.cxx:23
 TPoints3DABC.cxx:24
 TPoints3DABC.cxx:25
 TPoints3DABC.cxx:26
 TPoints3DABC.cxx:27
 TPoints3DABC.cxx:28
 TPoints3DABC.cxx:29
 TPoints3DABC.cxx:30
 TPoints3DABC.cxx:31
 TPoints3DABC.cxx:32
 TPoints3DABC.cxx:33
 TPoints3DABC.cxx:34
 TPoints3DABC.cxx:35
 TPoints3DABC.cxx:36
 TPoints3DABC.cxx:37
 TPoints3DABC.cxx:38
 TPoints3DABC.cxx:39
 TPoints3DABC.cxx:40
 TPoints3DABC.cxx:41
 TPoints3DABC.cxx:42
 TPoints3DABC.cxx:43
 TPoints3DABC.cxx:44
 TPoints3DABC.cxx:45
 TPoints3DABC.cxx:46
 TPoints3DABC.cxx:47
 TPoints3DABC.cxx:48
 TPoints3DABC.cxx:49
 TPoints3DABC.cxx:50
 TPoints3DABC.cxx:51
 TPoints3DABC.cxx:52
 TPoints3DABC.cxx:53
 TPoints3DABC.cxx:54
 TPoints3DABC.cxx:55
 TPoints3DABC.cxx:56
 TPoints3DABC.cxx:57
 TPoints3DABC.cxx:58
 TPoints3DABC.cxx:59
 TPoints3DABC.cxx:60
 TPoints3DABC.cxx:61
 TPoints3DABC.cxx:62
 TPoints3DABC.cxx:63
 TPoints3DABC.cxx:64
 TPoints3DABC.cxx:65
 TPoints3DABC.cxx:66
 TPoints3DABC.cxx:67
 TPoints3DABC.cxx:68
 TPoints3DABC.cxx:69
 TPoints3DABC.cxx:70
 TPoints3DABC.cxx:71
 TPoints3DABC.cxx:72
 TPoints3DABC.cxx:73
 TPoints3DABC.cxx:74
 TPoints3DABC.cxx:75
 TPoints3DABC.cxx:76
 TPoints3DABC.cxx:77
 TPoints3DABC.cxx:78
 TPoints3DABC.cxx:79
 TPoints3DABC.cxx:80
 TPoints3DABC.cxx:81
 TPoints3DABC.cxx:82
 TPoints3DABC.cxx:83
 TPoints3DABC.cxx:84
 TPoints3DABC.cxx:85
 TPoints3DABC.cxx:86
 TPoints3DABC.cxx:87
 TPoints3DABC.cxx:88
 TPoints3DABC.cxx:89
 TPoints3DABC.cxx:90
 TPoints3DABC.cxx:91
 TPoints3DABC.cxx:92
 TPoints3DABC.cxx:93
 TPoints3DABC.cxx:94
 TPoints3DABC.cxx:95
 TPoints3DABC.cxx:96
 TPoints3DABC.cxx:97
 TPoints3DABC.cxx:98
 TPoints3DABC.cxx:99
 TPoints3DABC.cxx:100
 TPoints3DABC.cxx:101
 TPoints3DABC.cxx:102
 TPoints3DABC.cxx:103
 TPoints3DABC.cxx:104
 TPoints3DABC.cxx:105
 TPoints3DABC.cxx:106
 TPoints3DABC.cxx:107
 TPoints3DABC.cxx:108
 TPoints3DABC.cxx:109
 TPoints3DABC.cxx:110
 TPoints3DABC.cxx:111
 TPoints3DABC.cxx:112
 TPoints3DABC.cxx:113
 TPoints3DABC.cxx:114
 TPoints3DABC.cxx:115
 TPoints3DABC.cxx:116
 TPoints3DABC.cxx:117
 TPoints3DABC.cxx:118
 TPoints3DABC.cxx:119
 TPoints3DABC.cxx:120
 TPoints3DABC.cxx:121
 TPoints3DABC.cxx:122
 TPoints3DABC.cxx:123
 TPoints3DABC.cxx:124
 TPoints3DABC.cxx:125
 TPoints3DABC.cxx:126
 TPoints3DABC.cxx:127
 TPoints3DABC.cxx:128
 TPoints3DABC.cxx:129
 TPoints3DABC.cxx:130
 TPoints3DABC.cxx:131
 TPoints3DABC.cxx:132
 TPoints3DABC.cxx:133
 TPoints3DABC.cxx:134
 TPoints3DABC.cxx:135
 TPoints3DABC.cxx:136
 TPoints3DABC.cxx:137
 TPoints3DABC.cxx:138
 TPoints3DABC.cxx:139
 TPoints3DABC.cxx:140
 TPoints3DABC.cxx:141
 TPoints3DABC.cxx:142
 TPoints3DABC.cxx:143
 TPoints3DABC.cxx:144
 TPoints3DABC.cxx:145
 TPoints3DABC.cxx:146
 TPoints3DABC.cxx:147
 TPoints3DABC.cxx:148
 TPoints3DABC.cxx:149
 TPoints3DABC.cxx:150
 TPoints3DABC.cxx:151
 TPoints3DABC.cxx:152
 TPoints3DABC.cxx:153
 TPoints3DABC.cxx:154
 TPoints3DABC.cxx:155
 TPoints3DABC.cxx:156
 TPoints3DABC.cxx:157
 TPoints3DABC.cxx:158
 TPoints3DABC.cxx:159
 TPoints3DABC.cxx:160
 TPoints3DABC.cxx:161
 TPoints3DABC.cxx:162
 TPoints3DABC.cxx:163
 TPoints3DABC.cxx:164
 TPoints3DABC.cxx:165