Introduction of the Copyright and cvs Log
[u/mrichter/AliRoot.git] / GEODB / AliGTransform.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 /*
17 $Log$
18 */
19
20 /**********************************************/
21 /*                                            */
22 /* FILE: AliGTransform.cxx                    */
23 /* PURPOSE: To define the relative positions  */
24 /*          of AliGNodes.                     */
25 /* LANGUAGE: C++                              */
26 /* COMPILER: CC for HP-UX 9.x and 10.         */
27 /* AUTHOR: Joana && David                     */
28 /* DATE: May 28, 1999                         */
29 /* ADDRESS: jesanto@cern.ch, dcollado@cern.ch */
30 /*                                            */
31 /**********************************************/
32
33
34 #include <TMath.h>
35 #include <TVector.h>
36 #include <iostream.h>
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <stdlib.h>
40 #include "AliGTransform.h"
41
42 ClassImp(AliGTransform)
43
44 //----------------------------------------------------------------------
45
46 AliGTransform::AliGTransform()
47 {
48     /* Default Constructor */
49     fExpression = "";
50     fMatrix     = NULL;
51     fName       = "";
52     fTitle      = "";
53     fX          = 0.;
54     fY          = 0.;
55     fZ          = 0.;
56     fTheta      = 0.;
57     fPsi        = 0.;
58     fPhi        = 0.;
59     
60 }
61
62 //----------------------------------------------------------------------
63
64 //----------------------------------------------------------------------
65
66 AliGTransform::AliGTransform(AliGTransform *tra)
67 {
68     /* Copy Constructor */
69     
70     fMatrix     = tra->fMatrix;
71     fName       = tra->fName;
72     fTitle      = tra->fTitle;
73     
74     
75  }   
76    
77 //----------------------------------------------------------------------}
78
79 AliGTransform::AliGTransform(Text_t* name, Text_t* title) : TNamed(name,title)
80 {
81     /* Constructor */
82     fExpression = "";
83     //float matrix[16] = {1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.}
84     fMatrix = new TVector(0,15,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1., "END");
85     fX          = 0.;
86     fY          = 0.;
87     fZ          = 0.;
88     fTheta      = 0.;
89     fPsi        = 0.;
90     fPhi        = 0.;
91 }
92
93 //----------------------------------------------------------------------
94
95 AliGTransform::AliGTransform(Text_t* name, Text_t* title, Text_t *expression): TNamed(name, title)
96 {
97     /* Constructor */
98     fExpression = expression;
99     
100     //float matrix[16] = {1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.};
101     //fMatrix     = new TArrayF(16,matrix);
102     fMatrix = new TVector(0,15,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.,0.,0.,0.,0.,1.,
103     "END");
104     CheckExpression();
105     BuildMatrix(fX,fY,fZ,fTheta,fPsi,fPhi);
106     
107 }
108
109 //----------------------------------------------------------------------
110
111 AliGTransform::AliGTransform(Text_t* name,Text_t* title, Text_t *axis, Float_t angle) : TNamed(name, title)
112 {
113     /* Constructor */
114     fX=fY=fZ=0.;
115    
116     
117         if (!strcmp(axis,"X")) {  
118                     
119             fTheta      = 90;
120             fPsi        = angle;
121             fPhi        = -90;
122             }
123         if (!strcmp(axis,"Y")) {   
124                     
125             fTheta      = 0.;
126             fPsi        = angle;
127             fPhi        = 0.;
128              }
129         if (!strcmp(axis,"Z")) {  
130                     
131             fTheta      = 0.;
132             fPsi        = 0.;
133             fPhi        = angle;
134              }
135
136  //cout << "fTheta" << fTheta << endl;
137  //cout << "fPsi" << fPsi << endl;
138  //cout << "fPhi" << fPhi << endl;
139
140         
141         BuildMatrix(fX,fY,fZ,fTheta,fPsi,fPhi);
142              
143         
144
145   
146 //----------------------------------------------------------------------
147   
148 AliGTransform::AliGTransform( Text_t* name, Text_t* title, Float_t theta1,Float_t phi1, 
149                                                     Float_t theta2,
150                                                     Float_t phi2, 
151                                                     Float_t theta3,Float_t phi3
152                                                     ) : TNamed(name,title)
153 {
154    const Double_t degrad = 0.0174532925199432958;
155    float a1=0.,a2=0.,a3=0.,b1=0.,b2=0.,b3=0.,c1=0.,c2=0.,c3=0.;
156    fX   = 0;
157    fY   = 0;
158    fZ   = 0;
159
160    a1 = TMath::Sin(theta1*degrad)*TMath::Cos(phi1*degrad);
161    a2 = TMath::Sin(theta1*degrad)*TMath::Sin(phi1*degrad);
162    a3 = TMath::Cos(theta1*degrad);
163    b1 = TMath::Sin(theta2*degrad)*TMath::Cos(phi2*degrad);
164    b2 = TMath::Sin(theta2*degrad)*TMath::Sin(phi2*degrad);
165    b3 = TMath::Cos(theta2*degrad);
166    c1 = TMath::Sin(theta3*degrad)*TMath::Cos(phi3*degrad);
167    c2 = TMath::Sin(theta3*degrad)*TMath::Sin(phi3*degrad);
168    c3 = TMath::Cos(theta3*degrad);
169    
170    // fMatrix = new TVector(0,15,a1,a2,a3,0.,b1,b2,b3,0.,c1,c2,c3,0.,0.,0.,0.,1., "END");
171    fMatrix = new TVector(0,15,a1,b1,c1,0.,a2,b2,c2,0.,a3,b3,c3,0.,0.,0.,0.,1., "END");
172 }                                                      
173 //----------------------------------------------------------------------
174 AliGTransform::AliGTransform( Text_t* name, Text_t* title, Float_t a1,Float_t a2,Float_t a3,Float_t b1,Float_t b2,
175         Float_t b3,Float_t c1,Float_t c2,Float_t c3,Float_t Dx,Float_t
176         Dy,Float_t Dz) : TNamed(name,title)
177 {
178
179
180 fMatrix = new TVector(0,15,a1,a2,a3,Dx,b1,b2,b3,Dy,c1,c2,c3,Dz,0.,0.,0.,1., "END");
181
182 }
183
184 //----------------------------------------------------------------------
185
186 AliGTransform::~AliGTransform() {
187     /* Destructor */
188     if(fMatrix)     delete fMatrix;
189 }
190
191 //----------------------------------------------------------------------
192
193 void AliGTransform::CheckExpression() 
194 /*Extracts the transformation arguments from the expression given in the
195 constructor*/
196
197 {
198     TString* string = new TString(fExpression);
199     string->ToUpper();
200     float Dx, Dy, Dz,theta, psi,phi;
201   
202     
203     if (strstr(*string, "+")) {
204          sscanf( *string, "TRA %f %f %f + ROT %f %f %f ", &Dx, &Dy, &Dz, &theta, &psi, &phi );
205         
206         if( sscanf(*string, "TRA %f %f %f", &Dx, &Dy, &Dz ) == EOF ) 
207             printf( "Error! Must introduce 3 distances\n" );
208
209         if( sscanf(*string, "ROT %f %f %f", &theta, &psi, &phi ) == EOF ) 
210             printf( "Error! Must introduce 3 angles\n" );
211          fX     = Dx;
212          fY     = Dy;
213          fZ     = Dz;
214          fTheta = theta;
215          fPsi   = psi;
216          fPhi   = phi;
217          
218     } else {
219   
220         if( strstr(*string,"TRA") ) {
221             sscanf( *string, "TRA %f %f %f", &Dx, &Dy, &Dz );
222             
223             if( sscanf(*string, "TRA %f %f %f", &Dx, &Dy, &Dz ) == EOF ) 
224                 printf( "Error! Must introduce 3 distances\n" );
225                 
226             fX          = Dx;
227             fY          = Dy;
228             fZ          = Dz;
229                 
230         } else {
231                 
232             if( strstr(*string,"ROT") ) {
233             sscanf( *string, "ROT %f %f %f", &theta, &psi, &phi );
234
235             if( sscanf(*string, "ROT %f %f %f", &theta, &psi, &phi ) == EOF ) 
236                 printf( "Error! Must introduce 3 angles\n" );
237                 
238              fTheta     = theta;
239              fPsi       = psi;
240              fPhi       = phi;
241                 
242              }
243         }
244      }
245  
246  
247 }
248
249 //----------------------------------------------------------------------
250
251 void AliGTransform::BuildMatrix(Float_t Dx=0., Float_t Dy=0., Float_t Dz=0., Float_t
252 theta=0., Float_t psi=0.,Float_t phi=0.  ) 
253 {
254 /* Builds the 4X4 matrix of a transformation */
255
256    
257     float a1=0.,a2=0.,a3=0.,b1=0.,b2=0.,b3=0.,c1=0.,c2=0.,c3=0.;
258     
259         const Double_t degrad = 0.0174532925199432958;
260         
261         a1 =
262         TMath::Cos(phi*degrad)*TMath::Cos(psi*degrad)*TMath::Cos(theta*degrad) -
263         TMath::Sin(phi*degrad)*TMath::Sin(theta*degrad);
264         a2 =
265         TMath::Cos(phi*degrad)*TMath::Cos(psi*degrad)*TMath::Sin(theta*degrad) +
266         TMath::Sin(phi*degrad)*TMath::Cos(theta*degrad);
267         a3 = - TMath::Cos(phi*degrad)*TMath::Sin(psi*degrad);
268         b1 = - TMath::Sin(phi*degrad)*TMath::Cos(psi*degrad)*TMath::Cos(theta*degrad) -
269         TMath::Cos(phi*degrad)*TMath::Sin(theta*degrad);
270         b2 = - TMath::Sin(phi*degrad)*TMath::Cos(psi*degrad)*TMath::Sin(theta*degrad) +
271         TMath::Cos(phi*degrad)*TMath::Cos(theta*degrad);
272         b3 = TMath::Sin(phi*degrad)*TMath::Sin(psi*degrad);
273         c1 = TMath::Sin(psi*degrad)*TMath::Cos(theta*degrad);
274         c2 = TMath::Sin(psi*degrad)*TMath::Sin(theta*degrad);
275         c3 = TMath::Cos(psi*degrad);
276         
277         /*
278         a1 =
279         TMath::Cos(psi*degrad)*TMath::Cos(phi*degrad);
280         a2 =
281         TMath::Cos(psi*degrad)*TMath::Sin(phi*degrad); 
282         a3 = - TMath::Sin(psi*degrad);
283         b1 = TMath::Sin(theta*degrad)*TMath::Sin(psi*degrad)*TMath::Cos(phi*degrad) -
284         TMath::Cos(theta*degrad)*TMath::Sin(phi*degrad);
285         b2 = TMath::Sin(theta*degrad)*TMath::Sin(psi*degrad)*TMath::Sin(phi*degrad) +
286         TMath::Cos(theta*degrad)*TMath::Cos(phi*degrad);
287         b3 = TMath::Sin(theta*degrad)*TMath::Cos(psi*degrad);
288         c1 =
289         TMath::Cos(theta*degrad)*TMath::Sin(psi*degrad)*TMath::Cos(phi*degrad) +
290         TMath::Sin(theta*degrad)*TMath::Sin(phi*degrad);
291         c2 =
292         TMath::Cos(theta*degrad)*TMath::Sin(psi*degrad)*TMath::Sin(phi*degrad) -
293         TMath::Sin(theta*degrad)*TMath::Cos(phi*degrad);
294         c3 = TMath::Cos(theta*degrad)*TMath::Sin(psi*degrad);
295         */
296         fMatrix = new TVector(0,15,a1,a2,a3,Dx,b1,b2,b3,Dy,c1,c2,c3,Dz,0.,0.,0.,1., "END");
297        
298 }
299
300 //----------------------------------------------------------------------
301
302 /*
303
304 void AliGTransform::CheckExpression( Text_t* expression ) 
305 {
306     char axis;
307     float theta, phi, psi,Dx, Dy, Dz;
308
309     TString* string = new TString( expression );
310     string->ToUpper();
311
312     if( strstr(*string,"ROT") ) {
313         sscanf( *string, "ROT%c", &axis );
314
315         switch (axis) {
316             case 'A':
317                 sscanf( *string, "ROTA%f %f %f", &phi, &psi, &theta );
318                 if( (!psi) || (!theta) || (!phi) )
319                     printf("Error! Must introduce 3 angles\n");
320                 break;
321
322             case 'X':
323                 sscanf( *string, "ROTX%f", &theta );
324                 if( !theta ) 
325                     printf("Error! Must introduce 1 angle\n");
326                 break;
327
328             case 'Y':
329                 sscanf( *string, "ROTY%f", &theta);
330                 if( !theta ) 
331                     printf("Error! Must introduce 1 angle\n");
332                 break;
333
334             case 'Z':
335                 sscanf( *string, "ROTZ%f", &theta );
336                 if( !theta )
337                     printf("Error! Must introduce 1 angle\n");
338                 break;
339
340             default:
341                 printf("Unrecognised rotation around axis %c\n",axis);
342         } 
343     }
344     else {
345         if( strstr(*string, "TRA") ) {
346             sscanf( *string, "TRA %f %f %f", &Dx, &Dy, &Dz );
347             if( sscanf(*string, "TRA %f %f %f", &Dx, &Dy, &Dz) == EOF )
348                 printf("Error! Must introduce 3 distances\n");
349             printf( "%f%f%f\n", Dx, Dy, Dz );
350         }
351         else 
352             printf( "ERROR!\n" );
353     }
354
355     delete string;
356 }
357
358 */