Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_box3 5 #define tools_box3 6 7 #include "../mnmx" 8 9 //#include <limits> 10 #include <ostream> 11 12 namespace tools { 13 14 template <class VEC3> 15 class box3 { 16 protected: 17 typedef typename VEC3::elem_t T_t; 18 //static T_t num_max() {return std::numeric_ 19 static T_t zero() {return T_t();} 20 protected: 21 box3(){ 22 //make_empty(); 23 } 24 public: 25 virtual ~box3() {} 26 public: 27 box3(const box3& a_from) 28 :m_min(a_from.m_min) 29 ,m_max(a_from.m_max) 30 {} 31 box3& operator=(const box3& a_from){ 32 m_min = a_from.m_min; 33 m_max = a_from.m_max; 34 return *this; 35 } 36 public: 37 bool center(VEC3& a_center) const { 38 if(is_empty()) {a_center.set_value(0,0,0); 39 a_center.set_value((m_max[0] + m_min[0])/T 40 (m_max[1] + m_min[1])/T 41 (m_max[2] + m_min[2])/T 42 return true; 43 } 44 45 bool set_bounds(const VEC3& a_mn,const VEC3& 46 if( a_mn[0]>a_mx[0] || a_mn[1]>a_mx[1] || 47 m_min = a_mn; 48 m_max = a_mx; 49 return true; 50 } 51 bool set_bounds(T_t a_mn_x,T_t a_mn_y,T_t a_ 52 T_t a_mx_x,T_t a_mx_y,T_t a_ 53 if( a_mn_x>a_mx_x || a_mn_y>a_mx_y || a_mn 54 m_min.set_value(a_mn_x,a_mn_y,a_mn_z); 55 m_max.set_value(a_mx_x,a_mx_y,a_mx_z); 56 return true; 57 } 58 59 bool get_size(T_t& a_dx,T_t& a_dy,T_t& a_dz) 60 if(is_empty()) {a_dx = 0;a_dy = 0;a_dz = 0 61 a_dx = m_max[0] - m_min[0]; 62 a_dy = m_max[1] - m_min[1]; 63 a_dz = m_max[2] - m_min[2]; 64 return true; 65 } 66 67 bool is_empty() const {return m_max[0] < m_m 68 69 const VEC3& mn() const {return m_min;} 70 const VEC3& mx() const {return m_max;} 71 72 bool back(VEC3& a_min,VEC3& a_min_y,VEC3& a_ 73 T_t dx,dy,dz; 74 if(!get_size(dx,dy,dz)) return false; //WA 75 // back (from m_min, clockwise order looki 76 a_min = m_min; 77 a_min_y.set_value (m_min.x() ,m_min.y()+ 78 a_min_xy.set_value(m_min.x()+dx,m_min.y()+ 79 a_min_x.set_value (m_min.x()+dx,m_min.y() 80 return true; 81 } 82 83 bool front(VEC3& a_max,VEC3& a_max_x,VEC3& a 84 T_t dx,dy,dz; 85 if(!get_size(dx,dy,dz)) return false; //WA 86 // front (from m_max, clockwise order look 87 a_max = m_max; 88 a_max_x.set_value (m_max.x()-dx,m_max.y() 89 a_max_xy.set_value(m_max.x()-dx,m_max.y()- 90 a_max_y.set_value (m_max.x() ,m_max.y()- 91 return true; 92 } 93 94 void extend_by(const VEC3& a_point) { 95 // Extend the boundaries of the box by the 96 // point fit inside the box if it isn't al 97 if(is_empty()) { 98 set_bounds(a_point,a_point); 99 } else { 100 m_min.set_value(min_of<T_t>(a_point[0],m 101 min_of<T_t>(a_point[1],m 102 min_of<T_t>(a_point[2],m 103 m_max.set_value(max_of<T_t>(a_point[0],m 104 max_of<T_t>(a_point[1],m 105 max_of<T_t>(a_point[2],m 106 } 107 } 108 109 void extend_by(T_t a_x,T_t a_y,T_t a_z) { 110 // Extend the boundaries of the box by the 111 // point fit inside the box if it isn't al 112 if(is_empty()) { 113 set_bounds(a_x,a_y,a_z,a_x,a_y,a_z); 114 } else { 115 m_min.set_value(min_of<T_t>(a_x,m_min[0] 116 min_of<T_t>(a_y,m_min[1] 117 min_of<T_t>(a_z,m_min[2] 118 m_max.set_value(max_of<T_t>(a_x,m_max[0] 119 max_of<T_t>(a_y,m_max[1] 120 max_of<T_t>(a_z,m_max[2] 121 } 122 } 123 124 bool get_cube_size(T_t& a_dx,T_t& a_dy,T_t& 125 if(!get_size(a_dx,a_dy,a_dz)) return false 126 if((a_dx<=zero())&&(a_dy<=zero())&&(a_dz<= 127 if((a_dx<=zero())&&(a_dy<=zero())) { //dz 128 a_dx = T_t(0.1)*a_dz; 129 a_dy = T_t(0.1)*a_dz; 130 } else if((a_dy<=zero())&&(a_dz<=zero())) 131 a_dy = T_t(0.1)*a_dx; 132 a_dz = T_t(0.1)*a_dx; 133 } else if((a_dz<=zero())&&(a_dx<=zero())) 134 a_dz = T_t(0.1)*a_dy; 135 a_dx = T_t(0.1)*a_dy; 136 137 } else if(a_dx<=zero()) { //dy,dz not 0 : 138 a_dx = T_t(0.1)*a_sqrt(a_dy*a_dy+a_dz*a_ 139 } else if(a_dy<=zero()) { //dx,dz not 0 : 140 a_dy = T_t(0.1)*a_sqrt(a_dx*a_dx+a_dz*a_ 141 } else if(a_dz<=zero()) { //dx,dy not 0 : 142 a_dz = T_t(0.1)*a_sqrt(a_dx*a_dx+a_dy*a_ 143 } 144 return true; 145 } 146 147 //NOTE : print is a Python keyword. 148 void dump(std::ostream& a_out) { 149 T_t dx,dy,dz; 150 if(!get_size(dx,dy,dz)) { 151 a_out << "box is empty." << std::endl; 152 } else { 153 a_out << " size " << dx << " " << dy << 154 } 155 a_out << " min " << m_min[0] << " " << m_m 156 a_out << " max " << m_max[0] << " " << m_m 157 VEC3 c; 158 center(c); 159 a_out << " center " << c[0] << " " << c[1] 160 } 161 162 protected: 163 VEC3 m_min; 164 VEC3 m_max; 165 }; 166 167 } 168 169 #endif