core/vgui/vgui_projection_inspector.h
Go to the documentation of this file.
00001 // This is core/vgui/vgui_projection_inspector.h
00002 #ifndef vgui_projection_inspector_h_
00003 #define vgui_projection_inspector_h_
00004 //:
00005 // \file
00006 // \brief contains class vgui_projection_inspector
00007 // \author fsm
00008 //
00009 // \verbatim
00010 //  Modifications
00011 //   14-Aug-2002 K.Y.McGaul - Converted to Doxygen style comments.
00012 // \endverbatim
00013 
00014 #include <vnl/vnl_double_2.h>
00015 #include <vnl/vnl_double_3.h>
00016 #include <vnl/vnl_double_4.h>
00017 #include <vnl/vnl_double_4x4.h>
00018 #include <vcl_iosfwd.h>
00019 
00020 //:
00021 class vgui_projection_inspector
00022 {
00023  public:
00024   //: Constructor - with default projection and modelview matrices.
00025   vgui_projection_inspector() { inspect(); }
00026 
00027   //: Destructor.
00028   ~vgui_projection_inspector() {}
00029 
00030   //: Returns the projection matrix.
00031   vnl_double_4x4 const& projection_matrix() const { return P; }
00032 
00033   //: Returns the modelview matrix.
00034   vnl_double_4x4 const& modelview_matrix() const { return M; }
00035 
00036   //: Returns the viewport.
00037   int const *viewport() const { return vp; }
00038 
00039   //: Send info on this projection_inspector to the given stream.
00040   void print(vcl_ostream&) const;
00041 
00042   //: Returns projection matrix multiplied by modelview matrix.
00043   vnl_double_4x4 total_transformation() const { return P*M; }
00044 
00045   //: Returns true if the projection matrix has a special form.
00046   // True iff the current total projection matrix has the form
00047   // \verbatim
00048   // s0       t0
00049   //    s1    t1
00050   //       s2 t2
00051   //          1
00052   // \endverbatim
00053   // in which case x1,y1, x2,y2 will contain the corners of the
00054   // backprojection of the viewport onto the plane z=0 in the
00055   // world and s,t will contain the nonzero entries.
00056   bool diagonal_scale_3d;
00057 
00058   // Bottom left of viewport - x coord.
00059   float x1;
00060   // Bottom left of viewport - y coord.
00061   float y1;
00062   // Top right of viewport - x coord.
00063   float x2;
00064   // Top right of viewport - y coord.
00065   float y2;
00066 
00067   float s[3], t[3];
00068 
00069   //: Convert window coords (eg. from vgui_event) to image coords.
00070   void window_to_image_coordinates(int, int, float &,float &) const;
00071 
00072   //: Convert image coords to window coords.
00073   void image_to_window_coordinates(float, float, float &,float &) const;
00074 
00075   //: Returns the corners of the backprojection of the viewport onto z=0.
00076   bool image_viewport(float& bottom_left_x, float& bottom_left_y,
00077                       float& top_right_x, float& top_right_y);
00078 
00079   //: Offset and scaling to transform window (x,y) to image (ix, iy) coords.
00080   //        ix = (x - token.offsetX) / token.scaleX;
00081   //        iy = (y - token.offsetY) / token.scaleY;
00082   bool compute_as_2d_affine(int width, int height,
00083                             float* offsetX, float* offsetY,
00084                             float* scaleX, float* scaleY);
00085 
00086   //: Convert window coords (eg. from vgui_event) to image coords.
00087   //  Some people prefer to put &s on "return value" parameters.
00088   void window_to_image_coordinates(int wx, int wy, float *ix,float *iy) const
00089   { window_to_image_coordinates(wx, wy, *ix, *iy); }
00090 
00091   //: Convert image coords to window coords.
00092   //  Some people prefer to put &s on "return value" parameters.
00093   void image_to_window_coordinates(float ix,float iy,float *wx,float *wy) const
00094   { image_to_window_coordinates(ix, iy, *wx, *wy); }
00095 
00096   //: Back-projection of a given point onto a given plane p.
00097   // From homogeneous viewport coordinates to homogeneous world coordinates.
00098   bool back_project(double const x[3], double const p[4], double X[4]) const;
00099 
00100   //: Back-projection of a given point onto a given plane p.
00101   //  Returns a 3-vcl_vector.
00102   vnl_vector<double> back_project(double x,double y, vnl_double_4 const &p) const;
00103 
00104   //: Back-projection of a given point onto a given plane p.
00105   //  Returns a 4-vcl_vector.
00106   vnl_vector<double> back_project(double x,double y,double z,vnl_double_4 const &p) const;
00107 
00108   //: Back-projection of a given point onto a given plane p.
00109   //  x is a 2-vector (Euclidean coordinates), and the returned 3-vector is also Euclidean.
00110   vnl_vector<double> back_project(vnl_double_2 const &x,vnl_double_4 const &p) const;
00111 
00112   //: Back-projection of a given point onto a given plane p.
00113   //  x is a 3-vector (projective coordinates), and the returned 4-vector is also projective.
00114   vnl_vector<double> back_project(vnl_double_3 const &x,vnl_double_4 const &p) const;
00115 
00116  private:
00117   int vp[4]; // viewport
00118   vnl_double_4x4 P; // projection matrix
00119   vnl_double_4x4 M; // modelview matrix
00120   void inspect();
00121 };
00122 
00123 #endif // vgui_projection_inspector_h_