00001
00002
00003
00004
00005
00006
00007 #ifndef BALL_MATHS_BOX3_H
00008 #define BALL_MATHS_BOX3_H
00009
00010 #ifndef BALL_COMMON_H
00011 # include <BALL/common.h>
00012 #endif
00013
00014 #ifndef BALL_MATHS_VECTOR3_H
00015 # include <BALL/MATHS/vector3.h>
00016 #endif
00017
00018 namespace BALL
00019 {
00025
00031 template <typename T>
00032 class TBox3
00033 {
00034 public:
00035
00036 BALL_CREATE(TBox3<T>)
00037
00038
00041
00044 TBox3();
00045
00049 TBox3(const TBox3& box);
00050
00057 TBox3(const TVector3<T>& point,
00058 const TVector3<T>& right_vector,
00059 const TVector3<T>& height_vector,
00060 const T& depth = 1);
00061
00068 TBox3(const TVector3<T>& point,
00069 const TVector3<T>& right_vector = TVector3<T>((T) 0, (T)1, (T)0),
00070 const TVector3<T>& height_vector = TVector3<T>((T) 1, (T)0, (T)0),
00071 const TVector3<T>& depth_vector = TVector3<T>((T) 0, (T)0, (T)1));
00072
00075 virtual ~TBox3()
00076 {
00077 }
00078
00082 virtual void clear();
00083
00085
00087
00091 void set(const TBox3& box);
00092
00097 const TBox3& operator = (const TBox3& box);
00098
00102 void swap(TBox3& box);
00103
00105
00107
00109 void setWidth(T width);
00110
00112 T getWidth() const { return width_;}
00113
00115 void setHeight(T height);
00116
00118 T getHeight() const { return height_;}
00119
00121 void setDepth(T depth);
00122
00124 T getDepth() const { return depth_;}
00125
00127 const TVector3<T>& getPoint() const { return point_;}
00128
00130 void setPoint(const TVector3<T>& point) { point_ = point;}
00131
00133 const TVector3<T>& getRightVector() const { return right_vector_;}
00134
00136 void setRightVector(const TVector3<T>& v);
00137
00139 const TVector3<T>& getHeightVector() const { return height_vector_;}
00140
00142 void setHeightVector(const TVector3<T>& v);
00143
00145 const TVector3<T>& getDepthVector() const
00146 { return depth_vector_;}
00147
00149 void setDepthVector(const TVector3<T>& v);
00150
00154 T getSurface() const;
00155
00159 T getVolume() const;
00160
00162 TVector3<T> getDiagonalVector() const;
00163
00165
00167
00171 bool operator == (const TBox3& box) const;
00172
00176 bool operator != (const TBox3& box) const;
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00193
00195
00200 bool isValid() const;
00201
00208 void dump(std::ostream& s = std::cout, Size depth = 0) const;
00210
00211 protected:
00212
00213
00214 TVector3<T> point_;
00215 TVector3<T> right_vector_;
00216 TVector3<T> height_vector_;
00217 TVector3<T> depth_vector_;
00218
00219 T width_;
00220 T height_;
00221 T depth_;
00222 };
00224
00225
00226 template <typename T>
00227 TBox3<T>::TBox3(const TVector3<T>& point, const TVector3<T>& right_vector,const TVector3<T>& height_vector,const T& depth)
00228 : point_(point),
00229 right_vector_(right_vector),
00230 height_vector_(height_vector),
00231 depth_vector_((right_vector % height_vector).normalize() * depth),
00232 width_(right_vector.getLength()),
00233 height_(height_vector.getLength()),
00234 depth_(depth)
00235 {}
00236
00237 template <typename T>
00238 TBox3<T>::TBox3(const TVector3<T>& point,
00239 const TVector3<T>& right_vector,
00240 const TVector3<T>& height_vector,
00241 const TVector3<T>& depth_vector)
00242 : point_(point),
00243 right_vector_(right_vector),
00244 height_vector_(height_vector),
00245 depth_vector_(depth_vector),
00246 width_(right_vector.getLength()),
00247 height_(height_vector.getLength()),
00248 depth_(depth_vector.getLength())
00249 {}
00250
00251 template <typename T>
00252 TBox3<T>::TBox3()
00253 : point_((T)0, (T)0, (T)0),
00254 right_vector_((T)0, (T)1, (T)0),
00255 height_vector_((T)1, (T)0, (T)0),
00256 depth_vector_((T)0, (T)0, (T)1),
00257 width_((T)1),
00258 height_((T)1),
00259 depth_((T)1)
00260 {
00261 }
00262
00263 template <typename T>
00264 TBox3<T>::TBox3(const TBox3<T>& box)
00265 : point_(box.point_),
00266 right_vector_(box.right_vector_),
00267 height_vector_(box.height_vector_),
00268 depth_vector_(box.depth_vector_),
00269 width_(box.width_),
00270 height_(box.height_),
00271 depth_(box.depth_)
00272 {
00273 }
00274
00275 template <typename T>
00276 void TBox3<T>::set(const TBox3<T>& box)
00277 {
00278 point_ = box.point_;
00279 right_vector_ = box.right_vector_;
00280 height_vector_ = box.height_vector_;
00281 depth_vector_ = box.depth_vector_;
00282 width_ = box.width_;
00283 height_ = box.height_;
00284 depth_ = box.depth_;
00285 }
00286
00287 template <typename T>
00288 BALL_INLINE
00289 const TBox3<T>& TBox3<T>::operator = (const TBox3<T> &box)
00290 {
00291 set(box);
00292 return *this;
00293 }
00294
00295 template <typename T>
00296 void TBox3<T>::swap(TBox3<T>& box)
00297 {
00298 point_.swap(box.point_);
00299 right_vector_.swap(box.right_vector_);
00300 height_vector_.swap(box.height_vector_);
00301 depth_vector_.swap(box.depth_vector_);
00302
00303 std::swap(width_, box.width_);
00304 std::swap(height_, box.height_);
00305 std::swap(depth_, box.depth_);
00306 }
00307
00308 template <typename T>
00309 void TBox3<T>::clear()
00310 {
00311 point_.clear();
00312 right_vector_ = TVector3<T>((T)0, (T)1, (T)0);
00313 height_vector_ = TVector3<T>((T)-1, (T)0, (T)0);
00314 depth_vector_ = TVector3<T>((T)0, (T)0, (T)1);
00315 width_ = (T) 1;
00316 height_ = (T) 1;
00317 depth_ = (T) 1;
00318 }
00319
00320
00321 template <typename T>
00322 BALL_INLINE
00323 void TBox3<T>::setWidth(T width)
00324 {
00325 right_vector_.normalize();
00326 right_vector_ *= width;
00327 width_ = width;
00328 }
00329
00330 template <typename T>
00331 BALL_INLINE
00332 void TBox3<T>::setHeight(T height)
00333 {
00334 height_vector_.normalize();
00335 height_vector_ *= height;
00336 height_ = height;
00337 }
00338
00339 template <typename T>
00340 BALL_INLINE
00341 void TBox3<T>::setDepth(T depth)
00342 {
00343 depth_vector_.normalize();
00344 depth_vector_ *= depth;
00345 depth_ = depth;
00346 }
00347
00348 template <typename T>
00349 BALL_INLINE
00350 void TBox3<T>::setRightVector(const TVector3<T>& v)
00351 {
00352 right_vector_ = v;
00353 width_ = right_vector_.getLength();
00354 }
00355
00356 template <typename T>
00357 BALL_INLINE
00358 void TBox3<T>::setHeightVector(const TVector3<T>& v)
00359 {
00360 height_vector_ = v;
00361 height_ = height_vector_.getLength();
00362 }
00363
00364 template <typename T>
00365 BALL_INLINE
00366 void TBox3<T>::setDepthVector(const TVector3<T>& v)
00367 {
00368 depth_vector_ = v;
00369 depth_ = depth_vector_.getLength();
00370 }
00371
00372 template <typename T>
00373 BALL_INLINE
00374 T TBox3<T>::getSurface() const
00375 {
00376 return ((width_ * height_ + width_ * depth_ + height_ * depth_) * 2);
00377 }
00378
00379 template <typename T>
00380 TVector3<T> TBox3<T>::getDiagonalVector() const
00381 {
00382 return (right_vector_ + height_vector_ + depth_vector_);
00383 }
00384
00385 template <typename T>
00386 BALL_INLINE
00387 T TBox3<T>::getVolume() const
00388 {
00389 return (width_ * height_ * depth_);
00390 }
00391
00392 template <typename T>
00393 bool TBox3<T>::operator == (const TBox3<T>& box) const
00394 {
00395 return (point_ == box.point_ &&
00396 right_vector_ == box.right_vector_ &&
00397 height_vector_ == box.height_vector_ &&
00398 depth_vector_ == box.depth_vector_ &&
00399 width_ == box.width_ &&
00400 height_ == box.height_ &&
00401 depth_ == box.depth_);
00402 }
00403
00404 template <typename T>
00405 BALL_INLINE
00406 bool TBox3<T>::operator != (const TBox3<T> &box) const
00407 {
00408 return !(*this == box);
00409 }
00410
00411 template <typename T>
00412 BALL_INLINE
00413 bool TBox3<T>::isValid() const
00414 {
00415 return (point_.isValid() &&
00416 right_vector_.isValid() &&
00417 height_vector_.isValid() &&
00418 depth_vector_.isValid() &&
00419 !right_vector_.isZero() &&
00420 !height_vector_.isZero() &&
00421 !depth_vector_.isZero());
00422
00423 }
00424
00425 template <typename T>
00426 void TBox3<T>::dump(std::ostream& s, Size depth) const
00427 {
00428 BALL_DUMP_STREAM_PREFIX(s);
00429
00430 BALL_DUMP_HEADER(s, this, this);
00431
00432 BALL_DUMP_DEPTH(s, depth);
00433 s << "point: " << point_ << std::endl;
00434
00435 BALL_DUMP_DEPTH(s, depth);
00436 s << "right_vector: " << right_vector_ << std::endl;
00437
00438 BALL_DUMP_DEPTH(s, depth);
00439 s << "height_vector: " << height_vector_ << std::endl;
00440
00441 BALL_DUMP_DEPTH(s, depth);
00442 s << "depth_vector: " << depth_vector_ << std::endl;
00443
00444 BALL_DUMP_DEPTH(s, depth);
00445 s << "width: " << width_ << std::endl;
00446
00447 BALL_DUMP_DEPTH(s, depth);
00448 s << "height: " << height_ << std::endl;
00449
00450 BALL_DUMP_DEPTH(s, depth);
00451 s << "depth: " << depth_ << std::endl;
00452
00453 BALL_DUMP_STREAM_SUFFIX(s);
00454 }
00455
00463 template <typename T>
00464 std::istream& operator >> (std::istream& s, TBox3<T>& box)
00465 {
00466 TVector3<T> point, right, height, depth;
00467 s >> point >> right >> height >> depth;
00468 box.setPoint(point);
00469 box.setRightVector(right);
00470 box.setHeightVector(height);
00471 box.setDepthVector(depth);
00472 return s;
00473 }
00474
00483 template <typename T>
00484 std::ostream& operator << (std::ostream& s, const TBox3<T>& box)
00485 {
00486 return s << box.getPoint() << " "
00487 << box.getRightVector() << " "
00488 << box.getHeightVector() << " "
00489 << box.getDepthVector();
00490 }
00492
00496 typedef TBox3<float> Box3;
00497
00498 }
00499
00500 #endif // BALL_MATHS_BOX3_H