1 // This is core/vgui/vgui_soview2D.h
2 #ifndef vgui_soview2D_h_
3 #define vgui_soview2D_h_
4 //:
5 // \file
6 // \author Philip C. Pritchett, Robotics Research Group, University of Oxford
7 // \date   24 Mar 99
8 // \brief  2-dimensional spatial object view.
9 //
10 //  Contains classes:  vgui_soview2D  vgui_soview2D_point
11 //                     vgui_soview2D_lineseg  vgui_soview2D_group
12 //                     vgui_soview2D_infinite_line  vgui_soview2D_circle
13 //                     vgui_soview2D_ellipse  vgui_soview2D_linestrip
14 //                     vgui_soview2D_polygon
15 //
16 // Notes:  We use floats instead of doubles as size is a speed issue (sic.)
17 //
18 // \verbatim
19 //  Modifications
20 //   10 Feb 2000 fsm - removed dependency on MultiView
21 //   04 Jul 2000 Marko Bacic - Fixed vgui_soview2D_circle
22 // \endverbatim
23 
24 #include <iosfwd>
25 #include "vgui_soview.h"
26 #ifdef _MSC_VER
27 #  include <vcl_msvc_warnings.h>
28 #endif
29 
30 #include "vgui_gl.h"
31 
32 class vil1_image;
33 class vil_image_view_base;
34 class vgui_section_buffer;
35 
36 //-----------------------------------------------------------------------------
37 //: 2-dimensional spatial object view.
38 class vgui_soview2D : public vgui_soview
39 {
40  public:
41   //: Constructor - create a default soview2D.
vgui_soview2D()42   vgui_soview2D() {}
43 
44   //: Destructor - delete this soview2D.
~vgui_soview2D()45   virtual ~vgui_soview2D() {}
46 
47   //: Returns the distance squared of this soview2D from the given position.
48   virtual float distance_squared(float x, float y) const = 0;
49 
50   //: Returns the centroid of this soview2D.
51   virtual void get_centroid(float* x, float* y) const = 0;
52 
53   //: Translate this soview2D by the given x and y distances.
54   virtual void translate(float x, float y) = 0;
55 };
56 
57 //-----------------------------------------------------------------------------
58 //: 2-dimensional point.
59 class vgui_soview2D_point : public vgui_soview2D
60 {
61  public:
62   //: Constructor - create a default 2D-point.
vgui_soview2D_point()63   vgui_soview2D_point() : x(0), y(0) {}
64 
65   //: Constructor - create a 2D-point with the given coordinates.
vgui_soview2D_point(float x_,float y_)66   vgui_soview2D_point(float x_, float y_) : x(x_), y(y_) {}
67 
68   //: Render this 2D-point on the display.
69   virtual void draw() const;
70 
71   // inherit documentation from base class
72   virtual void draw_select() const;
73 
74   //: Print details about this 2D-point to the given stream.
75   virtual std::ostream& print(std::ostream&) const;
76 
77   //: Distance of this 2D-point from the given position.
78   virtual float distance_squared(float x, float y) const;
79 
80   //: Returns the type of this class ('vgui_soview2D_point').
type_name()81   virtual std::string type_name() const { return "vgui_soview2D_point"; }
82 
83   //: Returns the centroid of this 2D-point.
84   void get_centroid(float* x, float* y) const;
85 
86   //: Translates this 2D-point by the given x and y distances.
87   void translate(float x, float y);
88 
89   //: x-coordinate of this 2D-point.
90   float x;
91   //: y-coordinate of this 2D-point.
92   float y;
93 };
94 
95 //-----------------------------------------------------------------------------
96 //: 2-dimensional line segment (finite line).
97 class vgui_soview2D_lineseg : public vgui_soview2D
98 {
99  public:
100   //: Constructor - create a default 2D line segment.
vgui_soview2D_lineseg()101   vgui_soview2D_lineseg() : x0(0), y0(0), x1(0), y1(0) {}
102 
103   //: Constructor - create a 2D line segment with given start and end points.
vgui_soview2D_lineseg(float x0_,float y0_,float x1_,float y1_)104   vgui_soview2D_lineseg(float x0_, float y0_, float x1_, float y1_)
105     : x0(x0_), y0(y0_), x1(x1_), y1(y1_) {}
106 
107   //: Constructor - create a 2D line segment same as the given 2D line segment.
vgui_soview2D_lineseg(vgui_soview2D_lineseg & l_)108   vgui_soview2D_lineseg( vgui_soview2D_lineseg &l_) : vgui_soview2D(), x0(l_.x0), y0(l_.y0), x1(l_.x1), y1(l_.y1) {}
109 
110   //: Render this 2D line segment on the display.
111   virtual void draw() const;
112 
113   //: Print details about this 2D line segment to the given stream.
114   virtual std::ostream& print(std::ostream&) const;
115 
116   //: Returns the distance squared to this 2D line segment.
117   virtual float distance_squared(float x, float y) const;
118 
119   //: Returns the type of this class ('vgui_soview2D_lineseg').
type_name()120   std::string type_name() const { return "vgui_soview2D_lineseg"; }
121 
122   //: Returns the centroid of this 2D line segment.
123   void get_centroid(float* x, float* y) const;
124 
125   //: Translate this 2D line segment by the given x and y distances.
126   void translate(float x, float y);
127 
128   //: Start point x coordinate.
129   float x0;
130   //: Start point y coordinate.
131   float y0;
132   //: End point x coordinate.
133   float x1;
134   //: End point y coordinate.
135   float y1;
136 };
137 
138 //-----------------------------------------------------------------------------
139 //: Group of vgui_soview2D's.
140 class vgui_soview2D_group : public vgui_soview2D
141 {
142  public:
143   //: Constructor - creates an empty 2D soview group.
vgui_soview2D_group()144   vgui_soview2D_group() {}
145 
146   //: Constructor - creates a 2D soview group containing the given 2D soviews.
vgui_soview2D_group(std::vector<vgui_soview2D * > ls_)147   vgui_soview2D_group( std::vector<vgui_soview2D *> ls_) : ls(ls_) {}
148 
149   //: Destructor - responsible for deleting 2D soview objects
150   //  In current design, Easy2D tableau is responsible for cleaning up
151   //  soview objects. To avoid memory leak when using this group class,
152   //  clean up the soview objects in the destructor.
153   //  It is hard to be nice & clean, unless smart ptr is introduced.
154   //  GY
155   virtual ~vgui_soview2D_group();
156 
157   //: Set the style (colour, line width, etc) for this 2D soview group.
158   virtual void set_style(const vgui_style_sptr&);
159 
160   //: Render this 2D soview group on the display.
161   virtual void draw() const;
162 
163   //: for selection purpose
164   //  see comments in base class
165   virtual void draw_select() const;
166 
167   //: Print details about this 2D soview group to the given stream.
168   virtual std::ostream& print(std::ostream&) const;
169 
170   //: Returns distance squared of this 2D soview group from the given position.
171   virtual float distance_squared(float x, float y) const;
172 
173   //: Returns the type of this class ('vgui_soview2D_group').
type_name()174   std::string type_name() const { return "vgui_soview2D_group"; }
175 
176   //: Returns the centroid of this 2D soview group.
177   void get_centroid(float* x, float* y) const;
178 
179   //: Translate this 2D soview group by the given x and y distances.
180   void translate(float x, float y);
181 
182   //: List of 2D soviews in this group.
183   std::vector<vgui_soview2D *> ls;
184 };
185 
186 //-----------------------------------------------------------------------------
187 //: 2-dimensional infinite line.
188 class vgui_soview2D_infinite_line : public vgui_soview2D
189 {
190  public:
191   //: Constructor - create a default 2D infinite line.
vgui_soview2D_infinite_line()192   vgui_soview2D_infinite_line() {}
193 
194   //: Constructor - create a 2D infinite line ax + by + c = 0.
vgui_soview2D_infinite_line(float a_,float b_,float c_)195   vgui_soview2D_infinite_line( float a_, float b_, float c_)
196     : a(a_), b(b_), c(c_) {}
197 
198   //: Render this 2D infinite line on the display.
199   virtual void draw() const;
200 
201   //: Print details about this 2D infinite line to the given stream.
202   virtual std::ostream& print(std::ostream&) const;
203 
204   //: Returns distance squared of this 2D infinite line from the given position.
205   virtual float distance_squared(float x, float y) const;
206 
207   //: Returns the type of this class ('vgui_soview2D_infinite_line').
type_name()208   std::string type_name() const { return "vgui_soview2D_infinite_line"; }
209 
210   //: Returns (0,0) - centroid does not make sense for 2D infinite line.
211   void get_centroid(float* x, float* y) const;
212 
213   //: Translate this 2D infinite line by the given x and y distances.
214   void translate(float x, float y);
215 
216   //: Parameters of the 2D infinite line ax + by + c = 0.
217   float a,b,c;
218 };
219 
220 
221 //-----------------------------------------------------------------------------
222 //: 2-dimensional circle.
223 class vgui_soview2D_circle : public vgui_soview2D
224 {
225  public:
226   //: Constructor - creates a default 2D circle.
vgui_soview2D_circle()227   vgui_soview2D_circle() {}
228 
229   //: Constructor - creates a circle with radius r, centered at (x,y)
vgui_soview2D_circle(float x_,float y_,float r_)230   vgui_soview2D_circle( float x_, float y_, float r_ )
231     : r(r_), x(x_), y(y_)  {  }
232 
233   //: Render this 2D circle on the display.
234   virtual void draw() const;
235 
236   //: Print details about this 2D circle to the given stream.
237   virtual std::ostream& print(std::ostream&) const;
238 
239   //: Returns the distance squared of this 2D circle from the given position.
240   virtual float distance_squared(float x, float y) const;
241 
242   //: Returns the type of this class ('vgui_soview2D_circle').
type_name()243   std::string type_name() const { return "vgui_soview2D_circle"; }
244 
245   //: Returns the centroid of this 2D circle (same as centre).
246   void get_centroid(float* x, float* y) const;
247 
248   //: Translate this 2D circle by the given x and y distances.
249   void translate(float x, float y);
250 
251   //: Radius of circle.
252   float r;
253   //: x-coordinate of the centre of the circle.
254   float x;
255   //: y-coordinate of the centre of the circle.
256   float y;
257 
258   //: Compile the std::list
259   static void compile();
260 };
261 
262 
263 //-----------------------------------------------------------------------------
264 //: 2-dimensional ellipse.
265 class vgui_soview2D_ellipse : public vgui_soview2D
266 {
267  public:
268   //: Constructor - create a default 2D ellipse.
vgui_soview2D_ellipse()269   vgui_soview2D_ellipse() {}
270 
271   //: Render this 2D ellipse on the display.
272   virtual void draw() const;
273 
274   //: Print details about this 2D ellipse to the given stream.
275   virtual std::ostream& print(std::ostream&) const;
276 
277   //: Returns the distance squared of this 2D ellipse from the given position.
278   virtual float distance_squared(float x, float y) const;
279 
280   //: Returns the type of this class ('vgui_soview2D_ellipse').
type_name()281   std::string type_name() const {return "vgui_soview2D_ellipse"; }
282 
283   //: Returns the centroid of this 2D ellipse.
284   void get_centroid(float* x, float* y) const;
285 
286   //: Translate this 2D ellipse by the given x and y distances.
287   void translate(float x, float y);
288 
289   //: Centre, width, height and angle of this 2D ellipse.
290   float x, y, w, h, phi;
291 
292   //: Compile the std::list
293   static void compile();
294 };
295 
296 
297 //-----------------------------------------------------------------------------
298 //: 2-dimensional linestrip.
299 class vgui_soview2D_linestrip : public vgui_soview2D
300 {
301  public:
302   //: Constructor - create a 2D linestrip from the given coordinate list.
303   vgui_soview2D_linestrip(unsigned, float const *, float const *);
304 
305   //: Constructor - create a default 2D linestrip.
vgui_soview2D_linestrip()306   vgui_soview2D_linestrip() : n(0), x(nullptr), y(nullptr) {}
307 
308   //: Destructor - delete this 2D linestrip.
309   ~vgui_soview2D_linestrip();
310 
311   //: Render this 2D linestrip on the display.
312   virtual void draw() const;
313 
314   //: Print information about this 2D linestrip to the given stream.
315   virtual std::ostream& print(std::ostream&) const;
316 
317   //: Returns the distance squared from this 2D linestrip to the given position.
318   virtual float distance_squared(float x, float y) const;
319 
320   //: Returns the type of this class ('vgui_soview2D_linestrip').
type_name()321   std::string type_name() const { return "vgui_soview2D_linestrip"; }
322 
323   //: Returns the centroid of this 2D linestrip.
324   void get_centroid(float* x, float* y) const;
325 
326   //: Translate this 2D linestrip by the given x and y distances.
327   void translate(float x, float y);
328 
329   unsigned n;
330   float *x, *y;
331   void set_size(unsigned );
332 
333   //static void compile();
334 };
335 
336 
337 //-----------------------------------------------------------------------------
338 //: 2-dimensional polygon.
339 class vgui_soview2D_polygon : public vgui_soview2D
340 {
341  public:
342   //: Constructor - create a 2D polygon with the given vertices.
343   vgui_soview2D_polygon(unsigned, float const *, float const *, bool fill);
344 
345   //: Constructor - create a default 2D polygon.
vgui_soview2D_polygon()346   vgui_soview2D_polygon() : n(0), x(nullptr), y(nullptr) {}
347 
348   //: Destructor - delete this 2D polygon.
349   ~vgui_soview2D_polygon();
350 
351   //: Render this 2D polygon on the display.
352   virtual void draw() const;
353 
354   //: Print details about this 2D polygon to the given stream.
355   virtual std::ostream& print(std::ostream&) const;
356 
357   //: Returns the distance squared from this 2D polygon to the given position.
358   virtual float distance_squared(float x, float y) const;
359 
360   //: Returns the type of this class ('vgui_soview2D_polygon').
type_name()361   std::string type_name() const { return "vgui_soview2D_polygon"; }
362 
363   //: Returns the centroid of this 2D polygon.
364   void get_centroid(float* x, float* y) const;
365 
366   //: Translate this 2D polygon by the given x and y distances.
367   void translate(float x, float y);
368 
369   unsigned n;
370   float *x, *y;
371   void set_size(unsigned );
372   bool filled;
373   //static void compile();
374 };
375 
376 //-----------------------------------------------------------------------------
377 //: 2-dimensional image.
378 //
379 // This will store a GL pixel buffer of the input image and display
380 // it, on request, with the top left corner at (x,y). It does not keep
381 // a reference to the image. Rather, it will create the GL pixel
382 // buffer at construction time and store that.
383 //
384 class vgui_soview2D_image : public vgui_soview2D
385 {
386  public:
387   //: Create the sprite from a vil1_image.
388   //
389   // If format and type are not specified, the "best" one will be
390   // automatically chosen.  \a blend will set the blend state for
391   // rendering. See draw().
392   //
393   vgui_soview2D_image( float x, float y,
394                        vil1_image const& img,
395                        bool blend = false,
396                        GLenum format = GL_NONE,
397                        GLenum type = GL_NONE );
398 
399   //: Create the sprite from a vil_image_view.
400   //
401   // If format and type are not specified, the "best" one will be
402   // automatically chosen.  \a blend will set the blend state for
403   // rendering. See draw().
404   //
405   vgui_soview2D_image( float x, float y,
406                        vil_image_view_base const& img,
407                        bool blend = false,
408                        GLenum format = GL_NONE,
409                        GLenum type = GL_NONE );
410 
411   //: Destructor - delete this image.
412   ~vgui_soview2D_image();
413 
414   //: Render this image on the display.
415   //
416   // If the blend state is on, then the image will be rendered with
417   // GL_BLEND enabled, and with glBlendFunc(GL_SRC_ALPHA,
418   // GL_ONE_MINUS_SRC_ALPHA)
419   //
420   virtual void draw() const;
421 
422   //: Print details about this image to the given stream.
423   virtual std::ostream& print(std::ostream&) const;
424 
425   //: Returns the distance squared from the centroid
426   virtual float distance_squared(float x, float y) const;
427 
428   //: Returns the type of this class ('vgui_soview2D_image').
type_name()429   std::string type_name() const { return "vgui_soview2D_image"; }
430 
431   //: Returns the centroid of this 2D image.
432   void get_centroid(float* x, float* y) const;
433 
434   //: Translate this 2D image by the given x and y distances.
435   void translate(float x, float y);
436 
437  private:
438   //: Coordinates of the upper lefthand corner of the image
439   float x_, y_;
440 
441   //: Width and height of the image
442   unsigned w_, h_;
443 
444   //: Render with or without blending?
445   bool blend_;
446 
447   //: The OpenGL buffer corresponding to the image
448   vgui_section_buffer* buffer_;
449 };
450 
451 #endif // vgui_soview2D_h_
452