1 /**************************************************************************
2 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////
20 // A straight line is coded as a point (3 Double_t) and //
21 // 3 direction cosines //
23 ///////////////////////////////////////////////////////////////////
25 #include <Riostream.h>
28 #include "AliStrLine.h"
32 //________________________________________________________
33 AliStrLine::AliStrLine() :
38 // Default constructor
39 for(Int_t i=0;i<3;i++) {
44 SetIdPoints(65535,65535);
47 //________________________________________________________
48 AliStrLine::AliStrLine(const Double_t *const point, const Double_t *const cd, Bool_t twopoints, UShort_t id1, UShort_t id2) :
53 // Standard constructor
54 // if twopoints is true: point and cd are the 3D coordinates of
55 // two points defininig the straight line
56 // if twopoint is false: point represents the 3D coordinates of a point
57 // belonging to the straight line and cd is the
59 for(Int_t i=0;i<3;i++)
63 InitTwoPoints(point,cd);
65 InitDirection(point,cd);
71 //________________________________________________________
72 AliStrLine::AliStrLine(const Double_t *const point, const Double_t *const sig2point, const Double_t *const cd, Bool_t twopoints, UShort_t id1, UShort_t id2) :
77 // Standard constructor
78 // if twopoints is true: point and cd are the 3D coordinates of
79 // two points defininig the straight line
80 // if twopoint is false: point represents the 3D coordinates of a point
81 // belonging to the straight line and cd is the
83 for(Int_t i=0;i<3;i++)
84 fSigma2P0[i] = sig2point[i];
87 InitTwoPoints(point,cd);
89 InitDirection(point,cd);
94 //________________________________________________________
95 AliStrLine::AliStrLine(const Double_t *const point, const Double_t *const sig2point, const Double_t *const wmat, const Double_t *const cd, Bool_t twopoints, UShort_t id1, UShort_t id2) :
100 // Standard constructor
101 // if twopoints is true: point and cd are the 3D coordinates of
102 // two points defininig the straight line
103 // if twopoint is false: point represents the 3D coordinates of a point
104 // belonging to the straight line and cd is the
105 // direction in space
107 fWMatrix = new Double_t [6];
108 for(Int_t i=0;i<3;i++){
109 fSigma2P0[i] = sig2point[i];
110 for(Int_t j=0;j<3;j++)if(j>=i)fWMatrix[k++]=wmat[3*i+j];
113 InitTwoPoints(point,cd);
115 InitDirection(point,cd);
117 SetIdPoints(id1,id2);
120 //________________________________________________________
121 AliStrLine::AliStrLine(const AliStrLine &source):TObject(source),
125 for(Int_t i=0;i<3;i++){
126 fP0[i]=source.fP0[i];
127 fSigma2P0[i]=source.fSigma2P0[i];
128 fCd[i]=source.fCd[i];
131 fWMatrix = new Double_t [6];
132 for(Int_t i=0;i<6;i++)fWMatrix[i]=source.fWMatrix[i];
134 for(Int_t i=0;i<2;i++) fIdPoint[i]=source.fIdPoint[i];
137 //________________________________________________________
138 AliStrLine& AliStrLine::operator=(const AliStrLine& source){
139 // Assignment operator
142 new(this)AliStrLine(source);
147 //________________________________________________________
148 void AliStrLine::GetWMatrix(Double_t *wmat)const {
149 // Getter for weighting matrix, as a [9] dim. array
152 for(Int_t i=0;i<3;i++){
153 for(Int_t j=0;j<3;j++){
155 wmat[3*i+j]=fWMatrix[k++];
158 wmat[3*i+j]=wmat[3*j+i];
164 //________________________________________________________
165 void AliStrLine::SetWMatrix(const Double_t *wmat) {
166 // Setter for weighting matrix, strating from a [9] dim. array
167 if(fWMatrix)delete [] fWMatrix;
168 fWMatrix = new Double_t [6];
170 for(Int_t i=0;i<3;i++){
171 for(Int_t j=0;j<3;j++)if(j>=i)fWMatrix[k++]=wmat[3*i+j];
175 //________________________________________________________
176 void AliStrLine::InitDirection(const Double_t *const point, const Double_t *const cd)
178 // Initialization from a point and a direction
179 Double_t norm = cd[0]*cd[0]+cd[1]*cd[1]+cd[2]*cd[2];
182 norm = TMath::Sqrt(1./norm);
183 for(Int_t i=0;i<3;++i) {
189 else AliFatal("Null direction cosines!!!");
192 //________________________________________________________
193 void AliStrLine::InitTwoPoints(const Double_t *const pA, const Double_t *const pB)
195 // Initialization from the coordinates of two
196 // points in the space
198 for(Int_t i=0;i<3;i++)cd[i] = pB[i]-pA[i];
199 InitDirection(pA,cd);
202 //________________________________________________________
203 AliStrLine::~AliStrLine() {
205 if(fWMatrix)delete [] fWMatrix;
208 //________________________________________________________
209 void AliStrLine::PrintStatus() const {
210 // Print current status
211 cout <<"=======================================================\n";
212 cout <<"Direction cosines: ";
213 for(Int_t i=0;i<3;i++)cout <<fCd[i]<<"; ";
215 cout <<"Known point: ";
216 for(Int_t i=0;i<3;i++)cout <<fP0[i]<<"; ";
218 cout <<"Error on known point: ";
219 for(Int_t i=0;i<3;i++)cout <<TMath::Sqrt(fSigma2P0[i])<<"; ";
221 cout <<"Current value for the parameter: "<<fTpar<<endl;
224 //________________________________________________________
225 Int_t AliStrLine::IsParallelTo(const AliStrLine *line) const {
226 // returns 1 if lines are parallel, 0 if not paralel
227 const Double_t prec=1e-14;
231 Double_t vecpx=fCd[1]*cd2[2]-fCd[2]*cd2[1];
232 Double_t mod=TMath::Abs(fCd[1]*cd2[2])+TMath::Abs(fCd[2]*cd2[1]);
233 if(TMath::Abs(vecpx) > prec*mod) return 0;
235 Double_t vecpy=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
236 mod=TMath::Abs(fCd[0]*cd2[2])+TMath::Abs(fCd[2]*cd2[0]);
237 if(TMath::Abs(vecpy) > prec*mod) return 0;
239 Double_t vecpz=fCd[0]*cd2[1]-fCd[1]*cd2[0];
240 mod=TMath::Abs(fCd[0]*cd2[1])+TMath::Abs(fCd[1]*cd2[0]);
241 if(TMath::Abs(vecpz) > prec) return 0;
245 //________________________________________________________
246 Int_t AliStrLine::Crossrphi(const AliStrLine *line)
248 // Cross 2 lines in the X-Y plane
249 const Double_t prec=1e-14;
250 const Double_t big=1e20;
257 Double_t c=p2[0]-fP0[0];
260 Double_t f=p2[1]-fP0[1];
261 Double_t deno = a*e-b*d;
262 Double_t mod=TMath::Abs(a*e)+TMath::Abs(b*d);
264 if(TMath::Abs(deno) > prec*mod) {
265 fTpar = (c*e-b*f)/deno;
274 //________________________________________________________
275 Int_t AliStrLine::CrossPoints(AliStrLine *line, Double_t *point1, Double_t *point2){
276 // Looks for the crossing point estimated starting from the
278 const Double_t prec=1e-14;
288 k1+=(fP0[i]-p2[i])*fCd[i];
289 k2+=(fP0[i]-p2[i])*cd2[i];
299 Double_t deno = a11*a22-a21*a12;
300 Double_t mod = TMath::Abs(a11*a22)+TMath::Abs(a21*a12);
301 if(TMath::Abs(deno) < prec*mod) return -1;
302 fTpar = (a11*k2-a21*k1) / deno;
303 Double_t par2 = (k1*a22-k2*a12) / deno;
305 GetCurrentPoint(point1);
306 line->GetCurrentPoint(point2);
309 //________________________________________________________________
310 Int_t AliStrLine::Cross(AliStrLine *line, Double_t *point)
313 //Finds intersection between lines
316 Int_t retcod=CrossPoints(line,point1,point2);
318 for(Int_t i=0;i<3;i++)point[i]=(point1[i]+point2[i])/2.;
325 //___________________________________________________________
326 Double_t AliStrLine::GetDCA(const AliStrLine *line) const
328 //Returns the distance of closest approach between two lines
329 const Double_t prec=1e-14;
335 Int_t ispar=IsParallelTo(line);
337 Double_t dist1q=0,dist2=0,mod=0;
339 dist1q+=(fP0[i]-p2[i])*(fP0[i]-p2[i]);
340 dist2+=(fP0[i]-p2[i])*fCd[i];
343 if(TMath::Abs(mod) > prec){
345 return TMath::Sqrt(dist1q-dist2*dist2);
349 perp[0]=fCd[1]*cd2[2]-fCd[2]*cd2[1];
350 perp[1]=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
351 perp[2]=fCd[0]*cd2[1]-fCd[1]*cd2[0];
352 Double_t mod=0,dist=0;
354 mod+=perp[i]*perp[i];
355 dist+=(fP0[i]-p2[i])*perp[i];
357 if(TMath::Abs(mod) > prec) {
358 return TMath::Abs(dist/TMath::Sqrt(mod));
362 //________________________________________________________
363 void AliStrLine::GetCurrentPoint(Double_t *point) const {
364 // Fills the array point with the current value on the line
365 for(Int_t i=0;i<3;i++)point[i]=fP0[i]+fCd[i]*fTpar;
368 //________________________________________________________
369 Double_t AliStrLine::GetDistFromPoint(const Double_t *point) const
371 // computes distance from point
372 AliStrLine tmpline(point, fCd, kFALSE);
373 return GetDCA(&tmpline);