moab
|
00001 00017 #ifndef MB_AXIS_BOX_HPP 00018 #define MB_AXIS_BOX_HPP 00019 00020 #include <limits> 00021 #include "moab/Interface.hpp" 00022 00023 namespace moab { 00024 00030 class AxisBox { 00031 public: 00032 00033 inline AxisBox(); 00034 00035 inline AxisBox( const double* min, const double* max ); 00036 00037 inline AxisBox( const double* point ); 00038 00039 static ErrorCode get_tag( Tag& tag_handle_out, 00040 Interface* interface, 00041 const char* tag_name = 0 ); 00042 00044 static ErrorCode calculate( AxisBox& box_out, 00045 EntityHandle set, 00046 Interface* interface ); 00047 00049 static ErrorCode calculate( AxisBox& box_out, 00050 const Range& elements, 00051 Interface* interface ); 00052 00054 inline AxisBox& operator &=( const AxisBox& other ); 00055 00057 inline AxisBox& operator |=( const AxisBox& other ); 00058 00060 inline AxisBox& operator |=( const double* point ); 00061 00062 inline const double* minimum() const { return minVect; } 00063 00064 inline const double* maximum() const { return maxVect; } 00065 00066 inline double* minimum() { return minVect; } 00067 00068 inline double* maximum() { return maxVect; } 00069 00070 inline void center( double* center_out ) const; 00071 00072 inline void diagonal( double* diagonal_out ) const; 00073 00081 inline bool intersects( const AxisBox& other, 00082 double tolerance ) const; 00083 00088 inline bool intersects( const double* point, 00089 double tolerance ) const; 00090 00095 inline bool valid() const; 00096 00105 inline void closest_position_within_box( const double* input_position, 00106 double* output_position ) const; 00107 00108 private: 00109 00110 double minVect[3], maxVect[3]; 00111 }; 00112 00114 inline AxisBox operator&( const AxisBox& a, const AxisBox& b ) 00115 { return AxisBox(a) &= b; } 00116 00118 inline AxisBox operator|( const AxisBox& a, const AxisBox& b ) 00119 { return AxisBox(a) |= b; } 00120 00122 inline bool operator||( const AxisBox& a, const AxisBox& b ) 00123 { 00124 return a.minimum()[0] <= b.maximum()[0] 00125 && a.minimum()[1] <= b.maximum()[1] 00126 && a.minimum()[2] <= b.maximum()[2] 00127 && a.maximum()[0] >= b.minimum()[0] 00128 && a.maximum()[1] >= b.minimum()[1] 00129 && a.maximum()[2] >= b.minimum()[2]; 00130 } 00131 00132 00133 inline AxisBox::AxisBox() 00134 { 00135 minVect[0] = minVect[1] = minVect[2] = std::numeric_limits<double>::max(); 00136 maxVect[0] = maxVect[1] = maxVect[2] = -std::numeric_limits<double>::max(); 00137 } 00138 00139 inline AxisBox::AxisBox( const double* min, const double* max ) 00140 { 00141 minVect[0] = min[0]; 00142 minVect[1] = min[1]; 00143 minVect[2] = min[2]; 00144 maxVect[0] = max[0]; 00145 maxVect[1] = max[1]; 00146 maxVect[2] = max[2]; 00147 } 00148 00149 inline AxisBox::AxisBox( const double* point ) 00150 { 00151 minVect[0] = maxVect[0] = point[0]; 00152 minVect[1] = maxVect[1] = point[1]; 00153 minVect[2] = maxVect[2] = point[2]; 00154 } 00155 00156 inline AxisBox& AxisBox::operator &=( const AxisBox& other ) 00157 { 00158 for (int i = 0; i < 3; ++i) { 00159 if (minVect[i] < other.minVect[i]) 00160 minVect[i] = other.minVect[i]; 00161 if (maxVect[i] > other.maxVect[i]) 00162 maxVect[i] = other.maxVect[i]; 00163 } 00164 return *this; 00165 } 00166 00167 inline AxisBox& AxisBox::operator |=( const AxisBox& other ) 00168 { 00169 for (int i = 0; i < 3; ++i) { 00170 if (minVect[i] > other.minVect[i]) 00171 minVect[i] = other.minVect[i]; 00172 if (maxVect[i] < other.maxVect[i]) 00173 maxVect[i] = other.maxVect[i]; 00174 } 00175 return *this; 00176 } 00177 00178 inline AxisBox& AxisBox::operator |=( const double* point ) 00179 { 00180 for (int i = 0; i < 3; ++i) { 00181 if (minVect[i] > point[i]) 00182 minVect[i] = point[i]; 00183 if (maxVect[i] < point[i]) 00184 maxVect[i] = point[i]; 00185 } 00186 return *this; 00187 } 00188 00189 inline void AxisBox::center( double* center_out ) const 00190 { 00191 center_out[0] = 0.5 * (minVect[0] + maxVect[0]); 00192 center_out[1] = 0.5 * (minVect[1] + maxVect[1]); 00193 center_out[2] = 0.5 * (minVect[2] + maxVect[2]); 00194 } 00195 00196 inline void AxisBox::diagonal( double* diagonal_out ) const 00197 { 00198 diagonal_out[0] = maxVect[0] - minVect[0]; 00199 diagonal_out[1] = maxVect[1] - minVect[1]; 00200 diagonal_out[2] = maxVect[2] - minVect[2]; 00201 } 00202 00203 inline bool AxisBox::intersects( const AxisBox& other, 00204 double tolerance ) const 00205 { 00206 return minVect[0] - other.maxVect[0] <= tolerance && 00207 minVect[1] - other.maxVect[1] <= tolerance && 00208 minVect[2] - other.maxVect[2] <= tolerance && 00209 other.minVect[0] - maxVect[0] <= tolerance && 00210 other.minVect[1] - maxVect[1] <= tolerance && 00211 other.minVect[2] - maxVect[2] <= tolerance; 00212 } 00213 00214 inline bool AxisBox::intersects( const double* point, 00215 double tolerance ) const 00216 { 00217 return minVect[0] - point[0] <= tolerance && 00218 minVect[1] - point[1] <= tolerance && 00219 minVect[2] - point[2] <= tolerance && 00220 maxVect[0] - point[0] <= tolerance && 00221 maxVect[1] - point[1] <= tolerance && 00222 maxVect[2] - point[2] <= tolerance; 00223 } 00224 00225 00226 inline bool AxisBox::valid() const 00227 { 00228 return minVect[0] <= maxVect[0] 00229 && minVect[1] <= maxVect[1] 00230 && minVect[2] <= maxVect[2]; 00231 } 00232 00233 inline void AxisBox::closest_position_within_box( 00234 const double* input_position, 00235 double* output_position ) const 00236 { 00237 for (int i = 0; i < 3; ++i) { 00238 if (input_position[i] < minVect[i]) 00239 output_position[i] = minVect[i]; 00240 else if (input_position[i] > maxVect[i]) 00241 output_position[i] = maxVect[i]; 00242 else 00243 output_position[i] = input_position[i]; 00244 } 00245 } 00246 00247 } // namespace moab 00248 00249 #endif 00250