core/vgui/vgui_soview2D.h
Go to the documentation of this file.
00001 // This is core/vgui/vgui_soview2D.h
00002 #ifndef vgui_soview2D_h_
00003 #define vgui_soview2D_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author Philip C. Pritchett, Robotics Research Group, University of Oxford
00010 // \date   24 Mar 99
00011 // \brief  2-dimensional spatial object view.
00012 //
00013 //  Contains classes:  vgui_soview2D  vgui_soview2D_point
00014 //                     vgui_soview2D_lineseg  vgui_soview2D_group
00015 //                     vgui_soview2D_infinite_line  vgui_soview2D_circle
00016 //                     vgui_soview2D_ellipse  vgui_soview2D_linestrip
00017 //                     vgui_soview2D_polygon
00018 //
00019 // Notes:  We use floats instead of doubles as size is a speed issue (sic.)
00020 //
00021 // \verbatim
00022 //  Modifications
00023 //   10 Feb 2000 fsm - removed dependency on MultiView
00024 //   04 Jul 2000 Marko Bacic - Fixed vgui_soview2D_circle
00025 // \endverbatim
00026 
00027 #include "vgui_soview.h"
00028 #include <vcl_iosfwd.h>
00029 
00030 #include "vgui_gl.h"
00031 
00032 class vil1_image;
00033 class vil_image_view_base;
00034 class vgui_section_buffer;
00035 
00036 //-----------------------------------------------------------------------------
00037 //: 2-dimensional spatial object view.
00038 class vgui_soview2D : public vgui_soview
00039 {
00040  public:
00041   //: Constructor - create a default soview2D.
00042   vgui_soview2D() {}
00043 
00044   //: Destructor - delete this soview2D.
00045   virtual ~vgui_soview2D() {}
00046 
00047   //: Returns the distance squared of this soview2D from the given position.
00048   virtual float distance_squared(float x, float y) const = 0;
00049 
00050   //: Returns the centroid of this soview2D.
00051   virtual void get_centroid(float* x, float* y) const = 0;
00052 
00053   //: Translate this soview2D by the given x and y distances.
00054   virtual void translate(float x, float y) = 0;
00055 };
00056 
00057 //-----------------------------------------------------------------------------
00058 //: 2-dimensional point.
00059 class vgui_soview2D_point : public vgui_soview2D
00060 {
00061  public:
00062   //: Constructor - create a default 2D-point.
00063   vgui_soview2D_point() : x(0), y(0) {}
00064 
00065   //: Constructor - create a 2D-point with the given coordinates.
00066   vgui_soview2D_point(float x_, float y_) : x(x_), y(y_) {}
00067 
00068   //: Render this 2D-point on the display.
00069   virtual void draw() const;
00070 
00071   // inherit documentation from base class
00072   virtual void draw_select() const;
00073 
00074   //: Print details about this 2D-point to the given stream.
00075   virtual vcl_ostream& print(vcl_ostream&) const;
00076 
00077   //: Distance of this 2D-point from the given position.
00078   virtual float distance_squared(float x, float y) const;
00079 
00080   //: Returns the type of this class ('vgui_soview2D_point').
00081   virtual vcl_string type_name() const { return "vgui_soview2D_point"; }
00082 
00083   //: Returns the centroid of this 2D-point.
00084   void get_centroid(float* x, float* y) const;
00085 
00086   //: Translates this 2D-point by the given x and y distances.
00087   void translate(float x, float y);
00088 
00089   //: x-coordinate of this 2D-point.
00090   float x;
00091   //: y-coordinate of this 2D-point.
00092   float y;
00093 };
00094 
00095 //-----------------------------------------------------------------------------
00096 //: 2-dimensional line segment (finite line).
00097 class vgui_soview2D_lineseg : public vgui_soview2D
00098 {
00099  public:
00100   //: Constructor - create a default 2D line segment.
00101   vgui_soview2D_lineseg() : x0(0), y0(0), x1(0), y1(0) {}
00102 
00103   //: Constructor - create a 2D line segment with given start and end points.
00104   vgui_soview2D_lineseg(float x0_, float y0_, float x1_, float y1_)
00105     : x0(x0_), y0(y0_), x1(x1_), y1(y1_) {}
00106 
00107   //: Constructor - create a 2D line segment same as the given 2D line segment.
00108   vgui_soview2D_lineseg( vgui_soview2D_lineseg &l_) : vgui_soview2D(), x0(l_.x0), y0(l_.y0), x1(l_.x1), y1(l_.y1) {}
00109 
00110   //: Render this 2D line segment on the display.
00111   virtual void draw() const;
00112 
00113   //: Print details about this 2D line segment to the given stream.
00114   virtual vcl_ostream& print(vcl_ostream&) const;
00115 
00116   //: Returns the distance squared to this 2D line segment.
00117   virtual float distance_squared(float x, float y) const;
00118 
00119   //: Returns the type of this class ('vgui_soview2D_lineseg').
00120   vcl_string type_name() const { return "vgui_soview2D_lineseg"; }
00121 
00122   //: Returns the centroid of this 2D line segment.
00123   void get_centroid(float* x, float* y) const;
00124 
00125   //: Translate this 2D line segment by the given x and y distances.
00126   void translate(float x, float y);
00127 
00128   //: Start point x coordinate.
00129   float x0;
00130   //: Start point y coordinate.
00131   float y0;
00132   //: End point x coordinate.
00133   float x1;
00134   //: End point y coordinate.
00135   float y1;
00136 };
00137 
00138 //-----------------------------------------------------------------------------
00139 //: Group of vgui_soview2D's.
00140 class vgui_soview2D_group : public vgui_soview2D
00141 {
00142  public:
00143   //: Constructor - creates an empty 2D soview group.
00144   vgui_soview2D_group() {}
00145 
00146   //: Constructor - creates a 2D soview group containing the given 2D soviews.
00147   vgui_soview2D_group( vcl_vector<vgui_soview2D *> ls_) : ls(ls_) {}
00148 
00149   //: Destructor - responsible for deleting 2D soview objects
00150   //  In current design, Easy2D tableau is responsible for cleaning up
00151   //  soview objects. To avoid memory leak when using this group class,
00152   //  clean up the soview objects in the destructor.
00153   //  It is hard to be nice & clean, unless smart ptr is introduced.
00154   //  GY
00155   virtual ~vgui_soview2D_group();
00156 
00157   //: Set the style (colour, line width, etc) for this 2D soview group.
00158   virtual void set_style(const vgui_style_sptr&);
00159 
00160   //: Render this 2D soview group on the display.
00161   virtual void draw() const;
00162 
00163   //: for selection purpose
00164   //  see comments in base class
00165   virtual void draw_select() const;
00166 
00167   //: Print details about this 2D soview group to the given stream.
00168   virtual vcl_ostream& print(vcl_ostream&) const;
00169 
00170   //: Returns distance squared of this 2D soview group from the given position.
00171   virtual float distance_squared(float x, float y) const;
00172 
00173   //: Returns the type of this class ('vgui_soview2D_group').
00174   vcl_string type_name() const { return "vgui_soview2D_group"; }
00175 
00176   //: Returns the centroid of this 2D soview group.
00177   void get_centroid(float* x, float* y) const;
00178 
00179   //: Translate this 2D soview group by the given x and y distances.
00180   void translate(float x, float y);
00181 
00182   //: List of 2D soviews in this group.
00183   vcl_vector<vgui_soview2D *> ls;
00184 };
00185 
00186 //-----------------------------------------------------------------------------
00187 //: 2-dimensional infinite line.
00188 class vgui_soview2D_infinite_line : public vgui_soview2D
00189 {
00190  public:
00191   //: Constructor - create a default 2D infinite line.
00192   vgui_soview2D_infinite_line() {}
00193 
00194   //: Constructor - create a 2D infinite line ax + by + c = 0.
00195   vgui_soview2D_infinite_line( float a_, float b_, float c_)
00196     : a(a_), b(b_), c(c_) {}
00197 
00198   //: Render this 2D infinite line on the display.
00199   virtual void draw() const;
00200 
00201   //: Print details about this 2D infinite line to the given stream.
00202   virtual vcl_ostream& print(vcl_ostream&) const;
00203 
00204   //: Returns distance squared of this 2D infinite line from the given position.
00205   virtual float distance_squared(float x, float y) const;
00206 
00207   //: Returns the type of this class ('vgui_soview2D_infinite_line').
00208   vcl_string type_name() const { return "vgui_soview2D_infinite_line"; }
00209 
00210   //: Returns (0,0) - centroid does not make sense for 2D infinite line.
00211   void get_centroid(float* x, float* y) const;
00212 
00213   //: Translate this 2D infinite line by the given x and y distances.
00214   void translate(float x, float y);
00215 
00216   //: Parameters of the 2D infinite line ax + by + c = 0.
00217   float a,b,c;
00218 };
00219 
00220 
00221 //-----------------------------------------------------------------------------
00222 //: 2-dimensional circle.
00223 class vgui_soview2D_circle : public vgui_soview2D
00224 {
00225  public:
00226   //: Constructor - creates a default 2D circle.
00227   vgui_soview2D_circle() {}
00228 
00229   //: Constructor - creates a circle with radius r, centered at (x,y)
00230   vgui_soview2D_circle( float x_, float y_, float r_ )
00231     : r(r_), x(x_), y(y_)  {  }
00232 
00233   //: Render this 2D circle on the display.
00234   virtual void draw() const;
00235 
00236   //: Print details about this 2D circle to the given stream.
00237   virtual vcl_ostream& print(vcl_ostream&) const;
00238 
00239   //: Returns the distance squared of this 2D circle from the given position.
00240   virtual float distance_squared(float x, float y) const;
00241 
00242   //: Returns the type of this class ('vgui_soview2D_circle').
00243   vcl_string type_name() const { return "vgui_soview2D_circle"; }
00244 
00245   //: Returns the centroid of this 2D circle (same as centre).
00246   void get_centroid(float* x, float* y) const;
00247 
00248   //: Translate this 2D circle by the given x and y distances.
00249   void translate(float x, float y);
00250 
00251   //: Radius of circle.
00252   float r;
00253   //: x-coordinate of the centre of the circle.
00254   float x;
00255   //: y-coordinate of the centre of the circle.
00256   float y;
00257 
00258   //: Compile the vcl_list
00259   static void compile();
00260 };
00261 
00262 
00263 //-----------------------------------------------------------------------------
00264 //: 2-dimensional ellipse.
00265 class vgui_soview2D_ellipse : public vgui_soview2D
00266 {
00267  public:
00268   //: Constructor - create a default 2D ellipse.
00269   vgui_soview2D_ellipse() {}
00270 
00271   //: Render this 2D ellipse on the display.
00272   virtual void draw() const;
00273 
00274   //: Print details about this 2D ellipse to the given stream.
00275   virtual vcl_ostream& print(vcl_ostream&) const;
00276 
00277   //: Returns the distance squared of this 2D ellipse from the given position.
00278   virtual float distance_squared(float x, float y) const;
00279 
00280   //: Returns the type of this class ('vgui_soview2D_ellipse').
00281   vcl_string type_name() const {return "vgui_soview2D_ellipse"; }
00282 
00283   //: Returns the centroid of this 2D ellipse.
00284   void get_centroid(float* x, float* y) const;
00285 
00286   //: Translate this 2D ellipse by the given x and y distances.
00287   void translate(float x, float y);
00288 
00289   //: Centre, width, height and angle of this 2D ellipse.
00290   float x, y, w, h, phi;
00291 
00292   //: Compile the vcl_list
00293   static void compile();
00294 };
00295 
00296 
00297 //-----------------------------------------------------------------------------
00298 //: 2-dimensional linestrip.
00299 class vgui_soview2D_linestrip : public vgui_soview2D
00300 {
00301  public:
00302   //: Constructor - create a 2D linestrip from the given coordinate list.
00303   vgui_soview2D_linestrip(unsigned, float const *, float const *);
00304 
00305   //: Constructor - create a default 2D linestrip.
00306   vgui_soview2D_linestrip() : n(0), x(0), y(0) {}
00307 
00308   //: Destructor - delete this 2D linestrip.
00309   ~vgui_soview2D_linestrip();
00310 
00311   //: Render this 2D linestrip on the display.
00312   virtual void draw() const;
00313 
00314   //: Print information about this 2D linestrip to the given stream.
00315   virtual vcl_ostream& print(vcl_ostream&) const;
00316 
00317   //: Returns the distance squared from this 2D linestrip to the given position.
00318   virtual float distance_squared(float x, float y) const;
00319 
00320   //: Returns the type of this class ('vgui_soview2D_linestrip').
00321   vcl_string type_name() const { return "vgui_soview2D_linestrip"; }
00322 
00323   //: Returns the centroid of this 2D linestrip.
00324   void get_centroid(float* x, float* y) const;
00325 
00326   //: Translate this 2D linestrip by the given x and y distances.
00327   void translate(float x, float y);
00328 
00329   unsigned n;
00330   float *x, *y;
00331   void set_size(unsigned );
00332 
00333   //static void compile();
00334 };
00335 
00336 
00337 //-----------------------------------------------------------------------------
00338 //: 2-dimensional polygon.
00339 class vgui_soview2D_polygon : public vgui_soview2D
00340 {
00341  public:
00342   //: Constructor - create a 2D polygon with the given vertices.
00343   vgui_soview2D_polygon(unsigned, float const *, float const *);
00344 
00345   //: Constructor - create a default 2D polygon.
00346   vgui_soview2D_polygon() : n(0), x(0), y(0) {}
00347 
00348   //: Destructor - delete this 2D polygon.
00349   ~vgui_soview2D_polygon();
00350 
00351   //: Render this 2D polygon on the display.
00352   virtual void draw() const;
00353 
00354   //: Print details about this 2D polygon to the given stream.
00355   virtual vcl_ostream& print(vcl_ostream&) const;
00356 
00357   //: Returns the distance squared from this 2D polygon to the given position.
00358   virtual float distance_squared(float x, float y) const;
00359 
00360   //: Returns the type of this class ('vgui_soview2D_polygon').
00361   vcl_string type_name() const { return "vgui_soview2D_polygon"; }
00362 
00363   //: Returns the centroid of this 2D polygon.
00364   void get_centroid(float* x, float* y) const;
00365 
00366   //: Translate this 2D polygon by the given x and y distances.
00367   void translate(float x, float y);
00368 
00369   unsigned n;
00370   float *x, *y;
00371   void set_size(unsigned );
00372 
00373   //static void compile();
00374 };
00375 
00376 //-----------------------------------------------------------------------------
00377 //: 2-dimensional image.
00378 //
00379 // This will store a GL pixel buffer of the input image and display
00380 // it, on request, with the top left corner at (x,y). It does not keep
00381 // a reference to the image. Rather, it will create the GL pixel
00382 // buffer at construction time and store that.
00383 //
00384 class vgui_soview2D_image : public vgui_soview2D
00385 {
00386  public:
00387   //: Create the sprite from a vil1_image.
00388   //
00389   // If format and type are not specified, the "best" one will be
00390   // automatically chosen.  \a blend will set the blend state for
00391   // rendering. See draw().
00392   //
00393   vgui_soview2D_image( float x, float y,
00394                        vil1_image const& img,
00395                        bool blend = false,
00396                        GLenum format = GL_NONE,
00397                        GLenum type = GL_NONE );
00398 
00399   //: Create the sprite from a vil_image_view.
00400   //
00401   // If format and type are not specified, the "best" one will be
00402   // automatically chosen.  \a blend will set the blend state for
00403   // rendering. See draw().
00404   //
00405   vgui_soview2D_image( float x, float y,
00406                        vil_image_view_base const& img,
00407                        bool blend = false,
00408                        GLenum format = GL_NONE,
00409                        GLenum type = GL_NONE );
00410 
00411   //: Destructor - delete this image.
00412   ~vgui_soview2D_image();
00413 
00414   //: Render this image on the display.
00415   //
00416   // If the blend state is on, then the image will be rendered with
00417   // GL_BLEND enabled, and with glBlendFunc(GL_SRC_ALPHA,
00418   // GL_ONE_MINUS_SRC_ALPHA)
00419   //
00420   virtual void draw() const;
00421 
00422   //: Print details about this image to the given stream.
00423   virtual vcl_ostream& print(vcl_ostream&) const;
00424 
00425   //: Returns the distance squared from the centroid
00426   virtual float distance_squared(float x, float y) const;
00427 
00428   //: Returns the type of this class ('vgui_soview2D_image').
00429   vcl_string type_name() const { return "vgui_soview2D_image"; }
00430 
00431   //: Returns the centroid of this 2D image.
00432   void get_centroid(float* x, float* y) const;
00433 
00434   //: Translate this 2D image by the given x and y distances.
00435   void translate(float x, float y);
00436 
00437  private:
00438   //: Coordinates of the upper lefthand corner of the image
00439   float x_, y_;
00440 
00441   //: Width and height of the image
00442   unsigned w_, h_;
00443 
00444   //: Render with or without blending?
00445   bool blend_;
00446 
00447   //: The OpenGL buffer corresponding to the image
00448   vgui_section_buffer* buffer_;
00449 };
00450 
00451 #endif // vgui_soview2D_h_