src/BDSBeamlineNavigator.cc

00001 #include "BDSBeamlineNavigator.hh"
00002 #include "BDSGlobalConstants.hh"
00003 #include "G4AffineTransform.hh"
00004 #include "G4RotationMatrix.hh"
00005 
00006 BDSBeamlineNavigator::BDSBeamlineNavigator(){
00007   _localX = new G4ThreeVector(1,0,0);
00008   _localY = new G4ThreeVector(0,1,0);
00009   _localZ = new G4ThreeVector(0,0,1);
00010 
00011   _rotationLocal = new G4RotationMatrix();
00012   _rotationGlobal = new G4RotationMatrix();
00013   _rotation = new G4RotationMatrix();
00014   
00015   _position = new G4ThreeVector(0,0,0);
00016   _positionStart = new G4ThreeVector(0,0,0);
00017   _positionEnd = new G4ThreeVector(0,0,0);
00018   _positionFromCurrentCenter = new G4ThreeVector(0,0,0);
00019   _zHalfAngle = new G4ThreeVector(0,0,0); 
00020     
00021 }
00022 
00023 BDSBeamlineNavigator::~BDSBeamlineNavigator(){
00024   delete _localX;
00025   delete _localY;
00026   delete _localZ;
00027   delete _rotationLocal;
00028   delete _rotationGlobal;
00029   delete _rotation;
00030   delete _position;
00031   delete _positionStart;
00032   delete _positionEnd;
00033   delete _positionFromCurrentCenter;
00034   delete _zHalfAngle;
00035 }
00036 
00037 void BDSBeamlineNavigator::addComponent(BDSAcceleratorComponent* var){
00038   //Reset the local rotation matrix
00039   G4cout << "BDSBeamlineNavigator::addComponent: resetting rotation matrix" << G4endl;
00040   *_rotationLocal = G4RotationMatrix();
00041 
00042   G4cout << "BDSBeamlineNavigator::addComponent: component variables" << G4endl;
00043   _s_local   = var->GetArcLength();
00044   _s_total += _s_local;
00045   G4double angle=var->GetAngle();
00046   G4double theta=var->GetTheta();
00047   G4double phi=var->GetPhi();
00048   G4double psi=var->GetPsi();
00049   G4double length=var->GetZLength();
00050   
00051   if( var->GetType() == "transform3d"){
00052     G4cout << "BDSBeamlineNavigator::addComponent: doing transform3d" << G4endl;
00053     _rotationGlobal->rotate(psi,_localZ);
00054     _rotationLocal->rotate(psi,_localZ);
00055     _rotationGlobal->rotate(phi,_localY);   
00056     _rotationLocal->rotate(phi,_localY);
00057     _rotationGlobal->rotate(theta,_localX);
00058     _rotationLocal->rotate(theta,_localX);
00059     _localX->rotate(psi,*_localZ);
00060     _localY->rotate(psi,*_localZ);
00061     _localX->rotate(phi,*_localY);
00062     _localZ->rotate(phi,*_localY);
00063     _localY->rotate(theta,*_localX);
00064     _localZ->rotate(theta,*_localX);
00065   }
00066   
00067   // define center of bended elements from the previos coordinate frame
00068   _zHalfAngle->setX(_localZ->x());
00069   _zHalfAngle->setY(_localZ->y());
00070   _zHalfAngle->setZ(_localZ->z());
00071   G4cout << "BDSBeamlineNavigator::addComponent: doing _zHalfAngle" << G4endl;
00072   if( var->GetType() == "sbend" || var->GetType() == "rbend"  )
00073     G4cout << "BDSBeamlineNavigator::addComponent: bend half angle" << G4endl;
00074     _zHalfAngle->rotate(angle/2,*_localY);
00075 
00076   
00077   // target position - advance the coordinates
00078 
00079     G4cout << "BDSBeamlineNavigator::addComponent: advancing coordinates" << G4endl;
00080 
00081     *_positionStart = (*_positionEnd);
00082     *_position = (*_positionEnd) + (*_zHalfAngle) * ( length/2 );  // The target position is the centre of the component.
00083     *_positionEnd = (*_position) + (*_zHalfAngle) * ( length/2 );  // The end position of the component.
00084     *_positionFromCurrentCenter = (*_position) - (*_positionEnd)/2.0; //The position of the beam line component from the centre of the CURRENT beam line
00085   
00086 
00087   // rotate to the previous reference frame
00088  G4cout << "BDSBeamlineNavigator::addComponent: rotating" << G4endl;
00089   _rotation->transform(*_rotationGlobal);
00090   _rotation->invert();
00091   // recompute global rotation
00092   // define new coordinate system local frame     
00093 
00094 
00095   
00096   // bends transform the coordinate system
00097   if( var->GetType() == "sbend" || var->GetType() == "rbend"){
00098     G4cout << "BDSBeamlineNavigator::addComponent: bend transform" << G4endl;
00099     _rotationGlobal->rotate(angle,*_localY);
00100     _localX->rotate(angle,*_localY);
00101     _localZ->rotate(angle,*_localY);
00102     _rotationGlobal->rotate(theta,*_localX);
00103     _localY->rotate(theta,*_localX);
00104     _localZ->rotate(theta,*_localX);
00105     // bend trapezoids defined along z-axis
00106     _rotation->rotateY(-twopi/4-angle/2);                                               
00107   } else {
00108     if (var->GetMarkerLogicalVolume()->GetSolid()->GetName().contains("trapezoid") ){
00109       G4cout << "BDSBeamlineNavigator::addComponent: trapezoid rotate" << G4endl;
00110       _rotation->rotateY(-twopi/4); //Drift trapezoids defined along z axis 
00111     }
00112   }
00113 
00114 
00115 
00116   G4cout << "BDSBeamlineNavigator::addComponent: appending lists" << G4endl;
00117   _positionList.push_back(new G4ThreeVector(*_position));
00118   _positionStartList.push_back(new G4ThreeVector(*_positionStart));
00119   _positionEndList.push_back(new G4ThreeVector(*_positionEnd));
00120   _positionFromCurrentCenterList.push_back(new G4ThreeVector(*_positionFromCurrentCenter));
00121   _rotationList.push_back(new G4RotationMatrix(*_rotation));
00122   _rotationGlobalList.push_back(new G4RotationMatrix(*_rotationGlobal));
00123   G4cout << "BDSBeamlineNavigator::addComponent: finished." << G4endl;
00124 }
00125 
00126 
00127 void BDSBeamlineNavigator::print(){
00128   G4cout << "BDSBeamlineNavigator: _position = " << *_position << G4endl;
00129   G4cout << "BDSBeamlineNavigator: _rotation = " << *_rotation << G4endl;
00130 }
00131 
00132 void BDSBeamlineNavigator::first(){
00133   _iterRotation=_rotationList.begin();
00134   _iterRotationGlobal=_rotationGlobalList.begin();
00135   _iterPosition=_positionList.begin();
00136   _iterPositionStart=_positionStartList.begin();
00137   _iterPositionEnd=_positionEndList.begin();
00138   _iterPositionFromCurrentCenter=_positionFromCurrentCenterList.begin();
00139 }
00140 
00141 bool BDSBeamlineNavigator::isDone(){
00142   return (_iterRotation==_rotationList.end());
00143 }
00144 
00145 void BDSBeamlineNavigator::next(){
00146   _iterRotation++;
00147   _iterRotationGlobal++;
00148   _iterPosition++;
00149   _iterPositionStart++;
00150   _iterPositionEnd++;
00151   _iterPositionFromCurrentCenter++;
00152 }
00153 
00154 
00155 G4RotationMatrix* BDSBeamlineNavigator::rotation(){
00156   return *_iterRotation;
00157 }
00158 
00159 G4RotationMatrix* BDSBeamlineNavigator::rotationGlobal(){
00160   return *_iterRotationGlobal;
00161 }
00162 
00163 G4ThreeVector* BDSBeamlineNavigator::position(){
00164   return *_iterPosition;
00165 }
00166 
00167 G4ThreeVector* BDSBeamlineNavigator::positionStart(){
00168   return *_iterPositionStart;
00169 }
00170 
00171 G4ThreeVector* BDSBeamlineNavigator::positionEnd(){
00172   return *_iterPositionEnd;
00173 }
00174 
00175 G4ThreeVector* BDSBeamlineNavigator::positionFromCurrentCenter(){
00176   return *_iterPositionFromCurrentCenter;
00177 }
00178 
00179 G4double BDSBeamlineNavigator::s_total(){
00180   return _s_total;
00181 }

Generated on 27 Aug 2013 for BDSIM by  doxygen 1.4.7