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