diff --git a/DDRec/include/DDRec/Surface.h b/DDRec/include/DDRec/Surface.h index f5c3277fc2809fba16bae40d2ffbec01cf0eb9f4..57f46f5f1e47741e8e175415b464294465b65d67 100644 --- a/DDRec/include/DDRec/Surface.h +++ b/DDRec/include/DDRec/Surface.h @@ -161,7 +161,7 @@ namespace DD4hep { /** Convert the global position to the local position (u,v) on the surface */ virtual Vector2D globalToLocal( const Vector3D& point) const ; - /** Convert the global position to the local position (u,v) on the surface */ + /** Convert the local position (u,v) on the surface to the global position */ virtual Vector3D localToGlobal( const Vector2D& point) const ; /// Access to the material in opposite direction of the normal @@ -334,7 +334,12 @@ namespace DD4hep { /// Checks if the given point lies within the surface virtual bool insideBounds(const Vector3D& point, double epsilon=1.e-4) const ; - } ; + /** Convert the global position to the local position (u,v) on the surface - u runs along the axis of the cylinder, v is r*phi */ + virtual Vector2D globalToLocal( const Vector3D& point) const ; + + /** Convert the local position (u,v) on the surface to the global position - u runs along the axis of the cylinder, v is r*phi*/ + virtual Vector3D localToGlobal( const Vector2D& point) const ; + } ; //====================================================================================================== @@ -405,7 +410,7 @@ namespace DD4hep { /** Convert the global position to the local position (u,v) on the surface */ virtual Vector2D globalToLocal( const Vector3D& point) const ; - /** Convert the global position to the local position (u,v) on the surface */ + /** Convert the local position (u,v) on the surface to the global position*/ virtual Vector3D localToGlobal( const Vector2D& point) const ; /** Thickness of inner material */ @@ -471,6 +476,12 @@ namespace DD4hep { */ virtual Vector3D normal(const Vector3D& point = Vector3D() ) const ; + /** Convert the global position to the local position (u,v) on the surface - u runs along the axis of the cylinder, v is r*phi */ + virtual Vector2D globalToLocal( const Vector3D& point) const ; + + /** Convert the local position (u,v) on the surface to the global position - u runs along the axis of the cylinder, v is r*phi*/ + virtual Vector3D localToGlobal( const Vector2D& point) const ; + /// the radius of the cylinder (rho of the origin vector) virtual double radius() const { return _volSurf.origin().rho() ; diff --git a/DDRec/src/Surface.cpp b/DDRec/src/Surface.cpp index 604a840cd1c882df672596df82b8974a63983bed..885dfbfd2e401fab07e5f8e80d91691ef0aac14c 100644 --- a/DDRec/src/Surface.cpp +++ b/DDRec/src/Surface.cpp @@ -3,7 +3,7 @@ #include "DDRec/MaterialManager.h" -#include <math.h> +#include <cmath> #include <memory> #include <exception> #include <memory> @@ -145,6 +145,32 @@ namespace DD4hep { + + ISurface::Vector2D VolCylinder::globalToLocal( const Vector3D& point) const { + + // cylinder is parallel to here so u is dZ and v is r *dPhi + double phi = point.phi() - origin().phi() ; + + while( phi < -M_PI ) phi += 2.*M_PI ; + while( phi > M_PI ) phi -= 2.*M_PI ; + + return ISurface::Vector2D( point.z() - origin().z() , origin().rho() * phi ) ; + } + + + Vector3D VolCylinder::localToGlobal( const ISurface::Vector2D& point) const { + + double z = point.u() + origin().z() ; + double phi = point.v() / origin().rho() + origin().phi() ; + + while( phi < -M_PI ) phi += 2.*M_PI ; + while( phi > M_PI ) phi -= 2.*M_PI ; + + return Vector3D( origin().rho() , phi, z , Vector3D::cylindrical ) ; + } + + + VolCylinder::VolCylinder( Geometry::Volume vol, SurfaceType type, double thickness_inner ,double thickness_outer, Vector3D o ) : VolSurface( vol, type, thickness_inner, thickness_outer, Vector3D() , Vector3D() , Vector3D() , o ) { @@ -752,6 +778,7 @@ namespace DD4hep { } //================================================================================================================ + Vector3D CylinderSurface::u( const Vector3D& point ) const { Vector3D lp , u ; @@ -776,6 +803,25 @@ namespace DD4hep { _wtM->LocalToMasterVect( ln , n.array() ) ; return n ; } + + ISurface::Vector2D CylinderSurface::globalToLocal( const Vector3D& point) const { + + Vector3D lp , n ; + _wtM->MasterToLocal( point , lp.array() ) ; + + return _volSurf.globalToLocal( lp ) ; + } + + + Vector3D CylinderSurface::localToGlobal( const ISurface::Vector2D& point) const { + + Vector3D lp = _volSurf.localToGlobal( point ) ; + Vector3D p ; + _wtM->LocalToMasterVect( lp , p.array() ) ; + + return p ; + } + //================================================================================================================ diff --git a/DDSurfaces/include/DDSurfaces/ISurface.h b/DDSurfaces/include/DDSurfaces/ISurface.h index f33b54d112050154451c3f2524eff7b549cedc34..3c3bb03550c1ea4106fef74ad70d204ad56f7fd2 100644 --- a/DDSurfaces/include/DDSurfaces/ISurface.h +++ b/DDSurfaces/include/DDSurfaces/ISurface.h @@ -28,12 +28,12 @@ namespace DDSurfaces { /** Helper class for 2d vectors */ struct Vector2D{ - double _x,_y ; - Vector2D() : _x(0.),_y(0.) {} - Vector2D(double x, double y ) : _x(x),_y(y) {} - double operator[](unsigned i) const { return i==0 ? _x : _y ; } - double x() const { return _x ; } - double y() const { return _y ; } + double _u,_v ; + Vector2D() : _u(0.),_v(0.) {} + Vector2D(double u, double v ) : _u(u),_v(v) {} + double operator[](unsigned i) const { return i==0 ? _u : _v ; } + double u() const { return _u ; } + double v() const { return _v ; } }; @@ -61,7 +61,7 @@ namespace DDSurfaces { /** Convert the global position to the local position (u,v) on the surface */ virtual Vector2D globalToLocal( const Vector3D& point) const=0 ; - /** Convert the global position to the local position (u,v) on the surface */ + /** Convert the local position (u,v) on the surface to the global position*/ virtual Vector3D localToGlobal( const Vector2D& point) const=0 ; /** Get Origin of local coordinate system on surface */ diff --git a/DDTest/src/test_surface.cc b/DDTest/src/test_surface.cc index 05a28561c11fe756481279ddb629d5d429be52c3..06c9e3ac7311b35dd271a8b6fb047630c14f0f85 100644 --- a/DDTest/src/test_surface.cc +++ b/DDTest/src/test_surface.cc @@ -93,7 +93,7 @@ int main(int argc, char** argv ){ test( surf.insideBounds( Vector3D( 0.00003 , .23 , .42 ) ) , true , " insideBounds Vector3D( 0.00003 , .23 , .42 ) " ) ; - // === test global to local ===== + //=============== test global to local =================== Vector3D point = o + 34.3 * u - 42.7 * v ; @@ -220,6 +220,30 @@ int main(int argc, char** argv ){ std::cout << " ** yv = " << yv << std::endl ; + //=============== test global to local =================== + + Vector3D pointC( radius , -42.7/radius , 34.3 , Vector3D::cylindrical ) ; + + ISurface::Vector2D lpC = surfT.globalToLocal( pointC ) ; + + // std::cout << " --- local coordinates of " << pointC << " : (" << lpC[0] << "," << lpC[1] << ")" << std::endl ; + + test( STR( lpC[0] ) == STR( 34.3 ) , true , " local u coordinate is 34.4 " ) ; + test( STR( lpC[1] ) == STR( -42.7 ) , true , " local v coordinate is -42.7 " ) ; + + Vector3D pointPrimeC = surfT.localToGlobal( lpC ) ; + + // std::cout << " --- global coordinates of point after local to global ( " << pointC.rho() << ", " << pointC.phi() << ", " << pointC.z() + // << " ) : (" << lpC[0] << "," << lpC[1] << ")" << std::endl ; + + test( pointPrimeC.isEqual( pointC ) , true , " point after global to local to global is the same " ) ; + + //======================================================== + + + + + // test surface type: test( surfT.type().isSensitive() , true , " surface is sensitive " ) ;