1 // This is core/vgl/vgl_frustum_3d.h
2 #ifndef vgl_frustum_3d_h
3 #define vgl_frustum_3d_h
4 //:
5 // \file
6 // \brief A polygonal cone truncated by parallel planes
7 // \author J.L. Mundy
8 // \date   December 1, 2013
9 //
10 // \verbatim
11 //  Modifications
12 // None
13 // \endverbatim
14 
15 #include <iosfwd>
16 #include <vector>
17 #include <map>
18 #ifdef _MSC_VER
19 #  include <vcl_msvc_warnings.h>
20 #endif
21 #include "vgl_plane_3d.h"
22 #include "vgl_point_3d.h" // forward declare vgl datatypes
23 
24 //: A 3D frustum is the portion of a solid (normally a cone or pyramid)
25 // that lies between two parallel planes cutting it.
26 //
27 template <class Type>
28 class vgl_frustum_3d
29 {
30  public:
31   //: default constructor
32    vgl_frustum_3d() = default;
33 
34    //: Construct the frustum from rays
35    // the corner rays intersect in a common origin point, i.e., the apex
36    // of the frustum cone. It is assumed the rays are in sorted order
37    // around the cone surface, so that sequential rays are coplanar
38    // with a cone surface plane and the cross product r[i].dir X r[i+1].dir
39    // defines an outward-pointing normal. norm is the vector
40    // perpendicular to the parallel frustum faces. d0 is the distance in
41    // the norm vector dirction from the apex to the closest parallel face.
42    // d1 the distance in the norm direction from the apex to the far face.
43    //
44    vgl_frustum_3d(std::vector<vgl_ray_3d<Type>> const &corner_rays,
45                   vgl_vector_3d<Type> const &norm, Type d0, Type d1);
46 
47    //: Equality test
48    inline bool operator==(vgl_frustum_3d<Type> const &other) const;
49 
50    // Data Access---------------------------------------------------------------
apex()51    const vgl_point_3d<Type> &apex() const { return apex_; }
52 
surface_planes()53    const std::vector<vgl_plane_3d<Type>> &surface_planes() const {
54      return surface_planes_;}
55 
near_plane()56   const vgl_plane_3d<Type>& near_plane() const
57   {return surface_planes_[near_plane_];}
58 
far_plane()59   const vgl_plane_3d<Type>& far_plane() const
60   {return surface_planes_[far_plane_];}
61 
verts()62   const std::vector<vgl_point_3d<Type> >& verts() const
63   {return verts_;}
64 
faces()65   const std::map<int, std::vector<int> >& faces() const
66   {return faces_;}
67 
68   vgl_box_3d<Type> bounding_box() const;
69 
70   //: Get the centroid point
71   vgl_point_3d<Type> centroid() const;
72 
73   //: test if the frustum is convex
74   bool is_convex() const;
75 
76   // Data Control--------------------------------------------------------------
77 
78   //: Return true iff the point p is inside this frustum
79   // assumes that the frustum is a convex solid
80   bool contains(vgl_point_3d<Type> const& p) const;
81 
82   //: Return true if \a (x,y,z) is inside this frustum
83   // assumes that the frustum is a convex solid
84   bool contains(Type const& x, Type const& y, Type const& z) const;
85 
86   // I/O-----------------------------------------------------------------------
87 
88 
89   // INTERNALS-----------------------------------------------------------------
90  protected:
91   // Data Members--------------------------------------------------------------
92   vgl_point_3d<Type> apex_;
93   vgl_vector_3d<Type> norm_;
94   //: planes bounding the frustum volume including near and far plane
95   std::vector<vgl_plane_3d<Type> > surface_planes_;
96   int near_plane_{0};
97   int far_plane_{0};
98   std::vector<vgl_point_3d<Type> > verts_;
99   //: the number of verts in the top or bottom face
100   int n_top_bot_face_verts_{0};
101   // key corresponds to plane index, value is clockwise verts on face boundary
102   // clockwise with respect the the face normal
103   std::map<int, std::vector<int> > faces_;
104 };
105 
106 //: Write frustum to stream
107 // \relatesalso vgl_frustum_3d
108 template <class Type>
109 std::ostream&  operator<<(std::ostream& s, vgl_frustum_3d<Type> const& p);
110 
111 
112 
113 #define VGL_FRUSTUM_3D_INSTANTIATE(T) extern "please include vgl/vgl_frustum_3d.hxx first"
114 
115 #endif // vgl_frustum_3d_h
116