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>
30 #include "AliStrLine.h"
34 //________________________________________________________
35 AliStrLine::AliStrLine() :
40 // Default constructor
41 for(Int_t i=0;i<3;i++) {
46 SetIdPoints(65535,65535);
49 //________________________________________________________
50 AliStrLine::AliStrLine(Double_t *point, Double_t *cd,Bool_t twopoints, UShort_t id1, UShort_t id2) :
55 // Standard constructor
56 // if twopoints is true: point and cd are the 3D coordinates of
57 // two points defininig the straight line
58 // if twopoint is false: point represents the 3D coordinates of a point
59 // belonging to the straight line and cd is the
61 for(Int_t i=0;i<3;i++){
65 InitTwoPoints(point,cd);
68 InitDirection(point,cd);
73 //________________________________________________________
74 AliStrLine::AliStrLine(Float_t *pointf, Float_t *cdf,Bool_t twopoints, UShort_t id1, UShort_t id2) :
79 // Standard constructor - with float arguments
80 // if twopoints is true: point and cd are the 3D coordinates of
81 // two points defininig the straight line
82 // if twopoint is false: point represents the 3D coordinates of a point
83 // belonging to the straight line and cd is the
87 for(Int_t i=0;i<3;i++){
93 InitTwoPoints(point,cd);
96 InitDirection(point,cd);
101 //________________________________________________________
102 AliStrLine::AliStrLine(Double_t *point, Double_t *sig2point, Double_t *cd,Bool_t twopoints, UShort_t id1, UShort_t id2) :
107 // Standard constructor
108 // if twopoints is true: point and cd are the 3D coordinates of
109 // two points defininig the straight line
110 // if twopoint is false: point represents the 3D coordinates of a point
111 // belonging to the straight line and cd is the
112 // direction in space
113 for(Int_t i=0;i<3;i++){
114 fSigma2P0[i] = sig2point[i];
117 InitTwoPoints(point,cd);
120 InitDirection(point,cd);
122 SetIdPoints(id1,id2);
125 //________________________________________________________
126 AliStrLine::AliStrLine(Float_t *pointf, Float_t *sig2point, Float_t *cdf,Bool_t twopoints, UShort_t id1, UShort_t id2) :
131 // Standard constructor - with float arguments
132 // if twopoints is true: point and cd are the 3D coordinates of
133 // two points defininig the straight line
134 // if twopoint is false: point represents the 3D coordinates of a point
135 // belonging to the straight line and cd is the
136 // direction in space
139 for(Int_t i=0;i<3;i++){
140 point[i] = pointf[i];
142 fSigma2P0[i] = sig2point[i];
145 InitTwoPoints(point,cd);
148 InitDirection(point,cd);
150 SetIdPoints(id1,id2);
152 //________________________________________________________
153 AliStrLine::AliStrLine(Double_t *point, Double_t *sig2point, Double_t *wmat, Double_t *cd,Bool_t twopoints, UShort_t id1, UShort_t id2) :
158 // Standard constructor
159 // if twopoints is true: point and cd are the 3D coordinates of
160 // two points defininig the straight line
161 // if twopoint is false: point represents the 3D coordinates of a point
162 // belonging to the straight line and cd is the
163 // direction in space
165 fWMatrix = new Double_t [6];
166 for(Int_t i=0;i<3;i++){
167 fSigma2P0[i] = sig2point[i];
168 for(Int_t j=0;j<3;j++)if(j>=i)fWMatrix[k++]=wmat[3*i+j];
171 InitTwoPoints(point,cd);
174 InitDirection(point,cd);
176 SetIdPoints(id1,id2);
179 //________________________________________________________
180 AliStrLine::AliStrLine(Float_t *pointf, Float_t *sig2point, Float_t *wmat, Float_t *cdf,Bool_t twopoints, UShort_t id1, UShort_t id2) :
185 // Standard constructor - with float arguments
186 // if twopoints is true: point and cd are the 3D coordinates of
187 // two points defininig the straight line
188 // if twopoint is false: point represents the 3D coordinates of a point
189 // belonging to the straight line and cd is the
190 // direction in space
193 fWMatrix = new Double_t [6];
195 for(Int_t i=0;i<3;i++){
196 point[i] = pointf[i];
198 fSigma2P0[i] = sig2point[i];
199 for(Int_t j=0;j<3;j++)if(j>=i)fWMatrix[k++]=wmat[3*i+j];
202 InitTwoPoints(point,cd);
205 InitDirection(point,cd);
207 SetIdPoints(id1,id2);
210 //________________________________________________________
211 AliStrLine::AliStrLine(const AliStrLine &source):TObject(source),
215 for(Int_t i=0;i<3;i++){
216 fP0[i]=source.fP0[i];
217 fSigma2P0[i]=source.fSigma2P0[i];
218 fCd[i]=source.fCd[i];
221 fWMatrix = new Double_t [6];
222 for(Int_t i=0;i<6;i++)fWMatrix[i]=source.fWMatrix[i];
224 for(Int_t i=0;i<2;i++) fIdPoint[i]=source.fIdPoint[i];
227 //________________________________________________________
228 AliStrLine& AliStrLine::operator=(const AliStrLine& source){
229 // Assignment operator
232 new(this)AliStrLine(source);
237 //________________________________________________________
238 void AliStrLine::GetWMatrix(Double_t *wmat)const {
239 // Getter for weighting matrix, as a [9] dim. array
242 for(Int_t i=0;i<3;i++){
243 for(Int_t j=0;j<3;j++){
245 wmat[3*i+j]=fWMatrix[k++];
248 wmat[3*i+j]=wmat[3*j+i];
254 //________________________________________________________
255 void AliStrLine::SetWMatrix(const Double_t *wmat) {
256 // Setter for weighting matrix, strating from a [9] dim. array
257 if(fWMatrix)delete [] fWMatrix;
258 fWMatrix = new Double_t [6];
260 for(Int_t i=0;i<3;i++){
261 for(Int_t j=0;j<3;j++)if(j>=i)fWMatrix[k++]=wmat[3*i+j];
265 //________________________________________________________
266 void AliStrLine::InitDirection(Double_t *point, Double_t *cd){
267 // Initialization from a point and a direction
269 for(Int_t i=0;i<3;i++)norm+=cd[i]*cd[i];
271 norm = TMath::Sqrt(norm);
272 for(Int_t i=0;i<3;i++) cd[i]/=norm;
275 Error("AliStrLine","Null direction cosines!!!");
282 //________________________________________________________
283 void AliStrLine::InitTwoPoints(Double_t *pA, Double_t *pB){
284 // Initialization from the coordinates of two
285 // points in the space
287 for(Int_t i=0;i<3;i++)cd[i] = pB[i]-pA[i];
288 InitDirection(pA,cd);
291 //________________________________________________________
292 AliStrLine::~AliStrLine() {
294 if(fWMatrix)delete [] fWMatrix;
297 //________________________________________________________
298 void AliStrLine::PrintStatus() const {
299 // Print current status
300 cout <<"=======================================================\n";
301 cout <<"Direction cosines: ";
302 for(Int_t i=0;i<3;i++)cout <<fCd[i]<<"; ";
304 cout <<"Known point: ";
305 for(Int_t i=0;i<3;i++)cout <<fP0[i]<<"; ";
307 cout <<"Error on known point: ";
308 for(Int_t i=0;i<3;i++)cout <<TMath::Sqrt(fSigma2P0[i])<<"; ";
310 cout <<"Current value for the parameter: "<<fTpar<<endl;
313 //________________________________________________________
314 Int_t AliStrLine::IsParallelTo(AliStrLine *line) const {
315 // returns 1 if lines are parallel, 0 if not paralel
316 const Double_t prec=1e-14;
319 Double_t vecpx=fCd[1]*cd2[2]-fCd[2]*cd2[1];
320 if(TMath::Abs(vecpx) > prec) return 0;
321 Double_t vecpy=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
322 if(TMath::Abs(vecpy) > prec) return 0;
323 Double_t vecpz=fCd[0]*cd2[1]-fCd[1]*cd2[0];
324 if(TMath::Abs(vecpz) > prec) return 0;
327 //________________________________________________________
328 Int_t AliStrLine::Crossrphi(AliStrLine *line){
329 // Cross 2 lines in the X-Y plane
330 const Double_t prec=1e-14;
331 const Double_t big=1e20;
338 Double_t c=p2[0]-fP0[0];
341 Double_t f=p2[1]-fP0[1];
342 Double_t deno = a*e-b*d;
344 if(TMath::Abs(deno) > prec) {
345 fTpar = (c*e-b*f)/deno;
354 //________________________________________________________
355 Int_t AliStrLine::CrossPoints(AliStrLine *line, Double_t *point1, Double_t *point2){
356 // Looks for the crossing point estimated starting from the
358 const Double_t prec=1e-14;
368 k1+=(fP0[i]-p2[i])*fCd[i];
369 k2+=(fP0[i]-p2[i])*cd2[i];
379 Double_t deno = a11*a22-a21*a12;
380 if(TMath::Abs(deno) < prec) return -1;
381 fTpar = (a11*k2-a21*k1) / deno;
382 Double_t par2 = (k1*a22-k2*a12) / deno;
384 GetCurrentPoint(point1);
385 line->GetCurrentPoint(point2);
388 //________________________________________________________________
389 Int_t AliStrLine::Cross(AliStrLine *line, Double_t *point){
391 //Finds intersection between lines
394 Int_t retcod=CrossPoints(line,point1,point2);
396 for(Int_t i=0;i<3;i++)point[i]=(point1[i]+point2[i])/2.;
403 //___________________________________________________________
404 Double_t AliStrLine::GetDCA(AliStrLine *line) const{
405 //Returns the distance of closest approach between two lines
406 const Double_t prec=1e-14;
412 Int_t ispar=IsParallelTo(line);
414 Double_t dist1q=0,dist2=0,mod=0;
416 dist1q+=(fP0[i]-p2[i])*(fP0[i]-p2[i]);
417 dist2+=(fP0[i]-p2[i])*fCd[i];
420 if(TMath::Abs(mod) > prec){
422 return TMath::Sqrt(dist1q-dist2*dist2);
426 perp[0]=fCd[1]*cd2[2]-fCd[2]*cd2[1];
427 perp[1]=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
428 perp[2]=fCd[0]*cd2[1]-fCd[1]*cd2[0];
429 Double_t mod=0,dist=0;
431 mod+=perp[i]*perp[i];
432 dist+=(fP0[i]-p2[i])*perp[i];
435 if(TMath::Abs(mod) > prec){
437 return TMath::Abs(dist);
441 //________________________________________________________
442 void AliStrLine::GetCurrentPoint(Double_t *point) const {
443 // Fills the array point with the current value on the line
444 for(Int_t i=0;i<3;i++)point[i]=fP0[i]+fCd[i]*fTpar;
447 //________________________________________________________
448 Double_t AliStrLine::GetDistFromPoint(Double_t *point) const {
449 // computes distance from point
450 AliStrLine tmp(point,(Double_t *)fCd,kFALSE);
451 return this->GetDCA(&tmp);