1 // This is core/vgl/algo/vgl_orient_box_3d.h
2 #ifndef vgl_orient_box_3d_h
3 #define vgl_orient_box_3d_h
4 //:
5 // \file
6 // \brief A bounding oriented box
7 //
8 //  This class mimics the properties of an oriented
9 //  box by keeping a regular axis aligned box and a
10 //  rotation direction. It keeps a bounding box of
11 //  the rotated box which is an axis aligned box.
12 //
13 // \verbatim
14 //  Modifications
15 //   2010-01-18 Peter Vanroose - added constructor from 4 corner points
16 // \endverbatim
17 
18 #include <vector>
19 #include <iosfwd>
20 #include <vgl/vgl_box_3d.h>
21 #include <vgl/vgl_point_3d.h>
22 #include <vnl/vnl_quaternion.h>
23 #ifdef _MSC_VER
24 #  include <vcl_msvc_warnings.h>
25 #endif
26 
27 template <class Type>
28 class vgl_orient_box_3d
29 {
30  public:
31   vgl_orient_box_3d() = default;
32 
33   //: constructor with only box definition, the direction will be set to (0,0,1) with no rotation
vgl_orient_box_3d(vgl_box_3d<Type> const & box)34   vgl_orient_box_3d(vgl_box_3d<Type> const& box)
35   : box_(box), orient_(vnl_quaternion<double>(vnl_vector_fixed<double,3>(0.0,0.0,1.0), 0.0)) {}
36 
37   //: constructor with box and the orientation
vgl_orient_box_3d(vgl_box_3d<Type> const & box,vnl_quaternion<double> const & orient)38   vgl_orient_box_3d(vgl_box_3d<Type> const& box, vnl_quaternion<double> const& orient)
39   : box_(box), orient_(orient) {}
40 
41 
42   //: constructor from four corner points.
43   //  The three directions from the first of these to the three other points must be mutually orthogonal.
44   vgl_orient_box_3d(vgl_point_3d<Type> const& p0, vgl_point_3d<Type> const& px, vgl_point_3d<Type> const& py, vgl_point_3d<Type> const& pz);
45 
46   virtual ~vgl_orient_box_3d() = default;
47 
48   inline bool operator==(vgl_orient_box_3d<Type> const& obb) const {
49     return obb.box_ == this->box_ && obb.orient_ == this->orient_;
50   }
51 
52   // dimension related Data access
width()53   Type width() const { return box_.width(); }
height()54   Type height() const { return box_.height(); }
depth()55   Type depth() const { return box_.depth(); }
volume()56   inline Type volume() const { return box_.width()*box_.height()*box_.depth(); }
57   std::vector<vgl_point_3d<Type> > corners() const;
centroid()58   vgl_point_3d<Type>  centroid() {return box_.centroid(); }
box()59   vgl_box_3d<Type> const box() {return box_; }
60 
61   //: The axis-aligned box that encloses the oriented box
62   vgl_box_3d<Type> enclosing_box() const;
63 
64   //: Return true if \a (x,y,z) is inside this box
65   bool contains(Type const& x, Type const& y, Type const& z) const;
66 
67   //: Return true if point is inside this box
contains(vgl_point_3d<Type> const & p)68   bool contains(vgl_point_3d<Type> const& p) const {return contains(p.x(), p.y(), p.z());}
69 
70   std::ostream& print(std::ostream& s) const;
71 
72   std::istream& read(std::istream& s);
73 
74  //private:
75   //: regular AABB(axis-aligned bounding box)
76   vgl_box_3d<Type> box_;
77 
78   //: orientation of the box as a quaternion
79   vnl_quaternion<double> orient_;
80 };
81 
82 //: Write box to stream
83 // \relatesalso vgl_box_3d
84 template <class Type>
85 std::ostream&  operator<<(std::ostream& s, vgl_orient_box_3d<Type> const& p);
86 
87 //: Read box from stream
88 // \relatesalso vgl_box_3d
89 template <class Type>
90 std::istream&  operator>>(std::istream& is,  vgl_orient_box_3d<Type>& p);
91 
92 #define VGL_ORIENT_BOX_3D_INSTANTIATE(T) extern "Please #include <vgl/vgl_orient_box_3d.hxx> instead"
93 
94 #endif // vgl_orient_box_3d_h
95