1 // This is core/vgui/vgui_projection_inspector.h
2 #ifndef vgui_projection_inspector_h_
3 #define vgui_projection_inspector_h_
4 //:
5 // \file
6 // \brief contains class vgui_projection_inspector
7 // \author fsm
8 //
9 // \verbatim
10 //  Modifications
11 //   14-Aug-2002 K.Y.McGaul - Converted to Doxygen style comments.
12 // \endverbatim
13 
14 #include <iosfwd>
15 #include <vnl/vnl_double_2.h>
16 #include <vnl/vnl_double_3.h>
17 #include <vnl/vnl_double_4.h>
18 #include <vnl/vnl_double_4x4.h>
19 #ifdef _MSC_VER
20 #  include <vcl_msvc_warnings.h>
21 #endif
22 
23 //:
24 class vgui_projection_inspector
25 {
26  public:
27   //: Constructor - with default projection and modelview matrices.
vgui_projection_inspector()28   vgui_projection_inspector() { inspect(); }
29 
30   //: Destructor.
~vgui_projection_inspector()31   ~vgui_projection_inspector() {}
32 
33   //: Returns the projection matrix.
projection_matrix()34   vnl_double_4x4 const& projection_matrix() const { return P; }
35 
36   //: Returns the modelview matrix.
modelview_matrix()37   vnl_double_4x4 const& modelview_matrix() const { return M; }
38 
39   //: Returns the viewport.
viewport()40   int const *viewport() const { return vp; }
41 
42   //: Send info on this projection_inspector to the given stream.
43   void print(std::ostream&) const;
44 
45   //: Returns projection matrix multiplied by modelview matrix.
total_transformation()46   vnl_double_4x4 total_transformation() const { return P*M; }
47 
48   //: Returns true if the projection matrix has a special form.
49   // True iff the current total projection matrix has the form
50   // \verbatim
51   // s0       t0
52   //    s1    t1
53   //       s2 t2
54   //          1
55   // \endverbatim
56   // in which case x1,y1, x2,y2 will contain the corners of the
57   // backprojection of the viewport onto the plane z=0 in the
58   // world and s,t will contain the nonzero entries.
59   bool diagonal_scale_3d;
60 
61   // Bottom left of viewport - x coord.
62   float x1;
63   // Bottom left of viewport - y coord.
64   float y1;
65   // Top right of viewport - x coord.
66   float x2;
67   // Top right of viewport - y coord.
68   float y2;
69 
70   float s[3], t[3];
71 
72   //: Convert window coords (eg. from vgui_event) to image coords.
73   void window_to_image_coordinates(int, int, float &,float &) const;
74 
75   //: Convert image coords to window coords.
76   void image_to_window_coordinates(float, float, float &,float &) const;
77 
78   //: Returns the corners of the backprojection of the viewport onto z=0.
79   bool image_viewport(float& bottom_left_x, float& bottom_left_y,
80                       float& top_right_x, float& top_right_y);
81 
82   //: Offset and scaling to transform window (x,y) to image (ix, iy) coords.
83   //        ix = (x - token.offsetX) / token.scaleX;
84   //        iy = (y - token.offsetY) / token.scaleY;
85   bool compute_as_2d_affine(int width, int height,
86                             float* offsetX, float* offsetY,
87                             float* scaleX, float* scaleY);
88 
89   //: Convert window coords (eg. from vgui_event) to image coords.
90   //  Some people prefer to put &s on "return value" parameters.
window_to_image_coordinates(int wx,int wy,float * ix,float * iy)91   void window_to_image_coordinates(int wx, int wy, float *ix,float *iy) const
92   { window_to_image_coordinates(wx, wy, *ix, *iy); }
93 
94   //: Convert image coords to window coords.
95   //  Some people prefer to put &s on "return value" parameters.
image_to_window_coordinates(float ix,float iy,float * wx,float * wy)96   void image_to_window_coordinates(float ix,float iy,float *wx,float *wy) const
97   { image_to_window_coordinates(ix, iy, *wx, *wy); }
98 
99   //: Back-projection of a given point onto a given plane p.
100   // From homogeneous viewport coordinates to homogeneous world coordinates.
101   bool back_project(double const x[3], double const p[4], double X[4]) const;
102 
103   //: Back-projection of a given point onto a given plane p.
104   //  Returns a 3-std::vector.
105   vnl_vector<double> back_project(double x,double y, vnl_double_4 const &p) const;
106 
107   //: Back-projection of a given point onto a given plane p.
108   //  Returns a 4-std::vector.
109   vnl_vector<double> back_project(double x,double y,double z,vnl_double_4 const &p) const;
110 
111   //: Back-projection of a given point onto a given plane p.
112   //  x is a 2-vector (Euclidean coordinates), and the returned 3-vector is also Euclidean.
113   vnl_vector<double> back_project(vnl_double_2 const &x,vnl_double_4 const &p) const;
114 
115   //: Back-projection of a given point onto a given plane p.
116   //  x is a 3-vector (projective coordinates), and the returned 4-vector is also projective.
117   vnl_vector<double> back_project(vnl_double_3 const &x,vnl_double_4 const &p) const;
118 
119  private:
120   int vp[4]; // viewport
121   vnl_double_4x4 P; // projection matrix
122   vnl_double_4x4 M; // modelview matrix
123   void inspect();
124 };
125 
126 #endif // vgui_projection_inspector_h_
127