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>
29 #include "AliStrLine.h"
33 //________________________________________________________
34 AliStrLine::AliStrLine() :
39 // Default constructor
40 for(Int_t i=0;i<3;i++) {
47 //________________________________________________________
48 AliStrLine::AliStrLine(Double_t *point, Double_t *cd,Bool_t twopoints) :
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++) fSigma2P0[i] = 0.;
61 InitTwoPoints(point,cd);
64 InitDirection(point,cd);
68 //________________________________________________________
69 AliStrLine::AliStrLine(Float_t *pointf, Float_t *cdf,Bool_t twopoints) :
74 // Standard constructor - with float arguments
75 // if twopoints is true: point and cd are the 3D coordinates of
76 // two points defininig the straight line
77 // if twopoint is false: point represents the 3D coordinates of a point
78 // belonging to the straight line and cd is the
82 for(Int_t i=0;i<3;i++){
88 InitTwoPoints(point,cd);
91 InitDirection(point,cd);
95 //________________________________________________________
96 AliStrLine::AliStrLine(Double_t *point, Double_t *sig2point, Double_t *cd,Bool_t twopoints) :
101 // Standard constructor
102 // if twopoints is true: point and cd are the 3D coordinates of
103 // two points defininig the straight line
104 // if twopoint is false: point represents the 3D coordinates of a point
105 // belonging to the straight line and cd is the
106 // direction in space
107 for(Int_t i=0;i<3;i++) fSigma2P0[i] = sig2point[i];
109 InitTwoPoints(point,cd);
112 InitDirection(point,cd);
116 //________________________________________________________
117 AliStrLine::AliStrLine(Float_t *pointf, Float_t *sig2point, Float_t *cdf,Bool_t twopoints) :
122 // Standard constructor - with float arguments
123 // if twopoints is true: point and cd are the 3D coordinates of
124 // two points defininig the straight line
125 // if twopoint is false: point represents the 3D coordinates of a point
126 // belonging to the straight line and cd is the
127 // direction in space
130 for(Int_t i=0;i<3;i++){
131 point[i] = pointf[i];
133 fSigma2P0[i] = sig2point[i];
136 InitTwoPoints(point,cd);
139 InitDirection(point,cd);
142 //________________________________________________________
143 void AliStrLine::InitDirection(Double_t *point, Double_t *cd){
144 // Initialization from a point and a direction
146 for(Int_t i=0;i<3;i++)norm+=cd[i]*cd[i];
148 norm = TMath::Sqrt(norm);
149 for(Int_t i=0;i<3;i++) cd[i]/=norm;
152 Error("AliStrLine","Null direction cosines!!!");
160 //________________________________________________________
161 void AliStrLine::InitTwoPoints(Double_t *pA, Double_t *pB){
162 // Initialization from the coordinates of two
163 // points in the space
165 for(Int_t i=0;i<3;i++)cd[i] = pB[i]-pA[i];
166 InitDirection(pA,cd);
169 //________________________________________________________
170 AliStrLine::~AliStrLine() {
174 //________________________________________________________
175 void AliStrLine::PrintStatus() const {
176 // Print current status
177 cout <<"=======================================================\n";
178 cout <<"Direction cosines: ";
179 for(Int_t i=0;i<3;i++)cout <<fCd[i]<<"; ";
181 cout <<"Known point: ";
182 for(Int_t i=0;i<3;i++)cout <<fP0[i]<<"; ";
184 cout <<"Error on known point: ";
185 for(Int_t i=0;i<3;i++)cout <<TMath::Sqrt(fSigma2P0[i])<<"; ";
187 cout <<"Current value for the parameter: "<<fTpar<<endl;
188 cout <<" Debug flag: "<<fDebug<<endl;
191 //________________________________________________________
192 Int_t AliStrLine::IsParallelTo(AliStrLine *line) const {
193 // returns 1 if lines are parallel, 0 if not paralel
196 Double_t vecpx=fCd[1]*cd2[2]-fCd[2]*cd2[1];
197 if(vecpx!=0) return 0;
198 Double_t vecpy=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
199 if(vecpy!=0) return 0;
200 Double_t vecpz=fCd[0]*cd2[1]-fCd[1]*cd2[0];
201 if(vecpz!=0) return 0;
204 //________________________________________________________
205 Int_t AliStrLine::Crossrphi(AliStrLine *line){
206 // Cross 2 lines in the X-Y plane
213 Double_t c=p2[0]-fP0[0];
216 Double_t f=p2[1]-fP0[1];
217 Double_t deno = a*e-b*d;
220 fTpar = (c*e-b*f)/deno;
228 //________________________________________________________
229 Int_t AliStrLine::CrossPoints(AliStrLine *line, Double_t *point1, Double_t *point2){
230 // Looks for the crossing point estimated starting from the
238 for(i=0;i<3;i++)k1+=(fP0[i]-p2[i])*fCd[i];
240 for(i=0;i<3;i++)k2+=(fP0[i]-p2[i])*cd2[i];
242 for(i=0;i<3;i++)a11+=fCd[i]*cd2[i];
245 for(i=0;i<3;i++)a21+=cd2[i]*cd2[i];
247 for(i=0;i<3;i++)a12-=fCd[i]*fCd[i];
248 Double_t deno = a11*a22-a21*a12;
249 if(deno == 0.) return -1;
250 fTpar = (a11*k2-a21*k1) / deno;
251 Double_t par2 = (k1*a22-k2*a12) / deno;
253 GetCurrentPoint(point1);
254 line->GetCurrentPoint(point2);
257 //________________________________________________________________
258 Int_t AliStrLine::Cross(AliStrLine *line, Double_t *point){
260 //Finds intersection between lines
263 Int_t retcod=CrossPoints(line,point1,point2);
265 for(Int_t i=0;i<3;i++)point[i]=(point1[i]+point2[i])/2.;
272 //___________________________________________________________
273 Double_t AliStrLine::GetDCA(AliStrLine *line) const{
274 //Returns the distance of closest approach between two lines
280 Int_t ispar=IsParallelTo(line);
282 Double_t dist1q=0,dist2=0,mod=0;
284 dist1q+=(fP0[i]-p2[i])*(fP0[i]-p2[i]);
285 dist2+=(fP0[i]-p2[i])*fCd[i];
290 return TMath::Sqrt(dist1q-dist2*dist2);
294 perp[0]=fCd[1]*cd2[2]-fCd[2]*cd2[1];
295 perp[1]=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
296 perp[2]=fCd[0]*cd2[1]-fCd[1]*cd2[0];
297 Double_t mod=0,dist=0;
299 mod+=perp[i]*perp[i];
300 dist+=(fP0[i]-p2[i])*perp[i];
305 return TMath::Abs(dist);
309 //________________________________________________________
310 void AliStrLine::GetCurrentPoint(Double_t *point) const {
311 // Fills the array point with the current value on the line
312 for(Int_t i=0;i<3;i++)point[i]=fP0[i]+fCd[i]*fTpar;
315 //________________________________________________________
316 Double_t AliStrLine::GetDistFromPoint(Double_t *point) const {
317 // computes distance from point
318 AliStrLine tmp(point,(Double_t *)fCd,kFALSE);
319 return this->GetDCA(&tmp);