Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/lina/box3 is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_box3
0005 #define tools_box3
0006 
0007 #include "../mnmx"
0008 
0009 //#include <limits>
0010 #include <ostream>
0011 
0012 namespace tools {
0013 
0014 template <class VEC3>
0015 class box3 {
0016 protected:
0017   typedef typename VEC3::elem_t T_t;
0018   //static T_t num_max() {return std::numeric_limits<T_t>::max();} //max is a forever pain on Windows.
0019   static T_t zero() {return T_t();}
0020 protected:
0021   box3(){
0022     //make_empty();
0023   }
0024 public:
0025   virtual ~box3() {}
0026 public:
0027   box3(const box3& a_from)
0028   :m_min(a_from.m_min)
0029   ,m_max(a_from.m_max)
0030   {}
0031   box3& operator=(const box3& a_from){
0032     m_min = a_from.m_min;
0033     m_max = a_from.m_max;
0034     return *this;
0035   }
0036 public:
0037   bool center(VEC3& a_center) const {
0038     if(is_empty()) {a_center.set_value(0,0,0);return false;} //??
0039     a_center.set_value((m_max[0] + m_min[0])/T_t(2),
0040                        (m_max[1] + m_min[1])/T_t(2),
0041                        (m_max[2] + m_min[2])/T_t(2));
0042     return true;
0043   }
0044 
0045   bool set_bounds(const VEC3& a_mn,const VEC3& a_mx){
0046     if( a_mn[0]>a_mx[0] || a_mn[1]>a_mx[1] || a_mn[2]>a_mx[2]) return false;
0047     m_min = a_mn;
0048     m_max = a_mx;
0049     return true;
0050   }
0051   bool set_bounds(T_t a_mn_x,T_t a_mn_y,T_t a_mn_z,
0052                   T_t a_mx_x,T_t a_mx_y,T_t a_mx_z){
0053     if( a_mn_x>a_mx_x || a_mn_y>a_mx_y || a_mn_z>a_mx_z ) return false;
0054     m_min.set_value(a_mn_x,a_mn_y,a_mn_z);
0055     m_max.set_value(a_mx_x,a_mx_y,a_mx_z);
0056     return true;
0057   }
0058 
0059   bool get_size(T_t& a_dx,T_t& a_dy,T_t& a_dz) const {
0060     if(is_empty()) {a_dx = 0;a_dy = 0;a_dz = 0;return false;}
0061     a_dx = m_max[0] - m_min[0];
0062     a_dy = m_max[1] - m_min[1];
0063     a_dz = m_max[2] - m_min[2];
0064     return true;
0065   }
0066 
0067   bool is_empty() const {return m_max[0] < m_min[0];}
0068 
0069   const VEC3& mn() const {return m_min;}
0070   const VEC3& mx() const {return m_max;}
0071 
0072   bool back(VEC3& a_min,VEC3& a_min_y,VEC3& a_min_xy,VEC3& a_min_x) const {
0073     T_t dx,dy,dz;
0074     if(!get_size(dx,dy,dz)) return false; //WARNING : a_vecs not touched.
0075     // back (from m_min, clockwise order looking toward +z) :
0076     a_min = m_min;
0077     a_min_y.set_value (m_min.x()   ,m_min.y()+dy,m_min.z());
0078     a_min_xy.set_value(m_min.x()+dx,m_min.y()+dy,m_min.z());
0079     a_min_x.set_value (m_min.x()+dx,m_min.y()   ,m_min.z());
0080     return true;
0081   }
0082 
0083   bool front(VEC3& a_max,VEC3& a_max_x,VEC3& a_max_xy,VEC3& a_max_y) const {
0084     T_t dx,dy,dz;
0085     if(!get_size(dx,dy,dz)) return false; //WARNING : a_vecs not touched.
0086     // front (from m_max, clockwise order looking toward -z) :
0087     a_max = m_max;
0088     a_max_x.set_value (m_max.x()-dx,m_max.y()   ,m_max.z());
0089     a_max_xy.set_value(m_max.x()-dx,m_max.y()-dy,m_max.z());
0090     a_max_y.set_value (m_max.x()   ,m_max.y()-dy,m_max.z());
0091     return true;
0092   }
0093 
0094   void extend_by(const VEC3& a_point) {
0095     // Extend the boundaries of the box by the given point, i.e. make the
0096     // point fit inside the box if it isn't already so.
0097     if(is_empty()) {
0098       set_bounds(a_point,a_point);
0099     } else {
0100       m_min.set_value(min_of<T_t>(a_point[0],m_min[0]),
0101                       min_of<T_t>(a_point[1],m_min[1]),
0102                       min_of<T_t>(a_point[2],m_min[2]));
0103       m_max.set_value(max_of<T_t>(a_point[0],m_max[0]),
0104                       max_of<T_t>(a_point[1],m_max[1]),
0105                       max_of<T_t>(a_point[2],m_max[2]));
0106     }
0107   }
0108 
0109   void extend_by(T_t a_x,T_t a_y,T_t a_z) {
0110     // Extend the boundaries of the box by the given point, i.e. make the
0111     // point fit inside the box if it isn't already so.
0112     if(is_empty()) {
0113       set_bounds(a_x,a_y,a_z,a_x,a_y,a_z);
0114     } else {
0115       m_min.set_value(min_of<T_t>(a_x,m_min[0]),
0116                       min_of<T_t>(a_y,m_min[1]),
0117                       min_of<T_t>(a_z,m_min[2]));
0118       m_max.set_value(max_of<T_t>(a_x,m_max[0]),
0119                       max_of<T_t>(a_y,m_max[1]),
0120                       max_of<T_t>(a_z,m_max[2]));
0121     }
0122   }
0123 
0124   bool get_cube_size(T_t& a_dx,T_t& a_dy,T_t& a_dz,T_t(*a_sqrt)(T_t)) const {
0125     if(!get_size(a_dx,a_dy,a_dz)) return false;
0126     if((a_dx<=zero())&&(a_dy<=zero())&&(a_dz<=zero())) return false;
0127     if((a_dx<=zero())&&(a_dy<=zero())) { //dz not 0 :
0128       a_dx = T_t(0.1)*a_dz;
0129       a_dy = T_t(0.1)*a_dz;
0130     } else if((a_dy<=zero())&&(a_dz<=zero())) { //dx not 0 :
0131       a_dy = T_t(0.1)*a_dx;
0132       a_dz = T_t(0.1)*a_dx;
0133     } else if((a_dz<=zero())&&(a_dx<=zero())) { //dy not 0 :
0134       a_dz = T_t(0.1)*a_dy;
0135       a_dx = T_t(0.1)*a_dy;
0136 
0137     } else if(a_dx<=zero()) { //dy,dz not 0 :
0138       a_dx = T_t(0.1)*a_sqrt(a_dy*a_dy+a_dz*a_dz);
0139     } else if(a_dy<=zero()) { //dx,dz not 0 :
0140       a_dy = T_t(0.1)*a_sqrt(a_dx*a_dx+a_dz*a_dz);
0141     } else if(a_dz<=zero()) { //dx,dy not 0 :
0142       a_dz = T_t(0.1)*a_sqrt(a_dx*a_dx+a_dy*a_dy);
0143     }
0144     return true;
0145   }
0146 
0147   //NOTE : print is a Python keyword.
0148   void dump(std::ostream& a_out) {
0149     T_t dx,dy,dz;
0150     if(!get_size(dx,dy,dz)) {
0151       a_out << "box is empty." << std::endl;
0152     } else {
0153       a_out << " size " << dx << " " << dy << " " << dz << std::endl;
0154     }
0155     a_out << " min " << m_min[0] << " " << m_min[1] << " " << m_min[2] << std::endl;
0156     a_out << " max " << m_max[0] << " " << m_max[1] << " " << m_max[2] << std::endl;
0157     VEC3 c;
0158     center(c);
0159     a_out << " center " << c[0] << " " << c[1] << " " << c[2] << std::endl;
0160   }
0161 
0162 protected:
0163   VEC3 m_min;
0164   VEC3 m_max;
0165 };
0166 
0167 }
0168 
0169 #endif