1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkContextDevice2D.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 /**
17  * @class   vtkContextDevice2D
18  * @brief   Abstract class for drawing 2D primitives.
19  *
20  *
21  * This defines the interface for a vtkContextDevice2D. In this sense a
22  * ContextDevice is a class used to paint 2D primitives onto a device, such as
23  * an OpenGL context or a QGraphicsView.
24 */
25 
26 #ifndef vtkContextDevice2D_h
27 #define vtkContextDevice2D_h
28 
29 #include "vtkRenderingContext2DModule.h" // For export macro
30 #include "vtkObject.h"
31 #include "vtkVector.h" // For vtkVector2i ivar
32 #include "vtkRect.h"   // For vtkRecti ivar
33 #include "vtkRenderingCoreEnums.h" // For marker enum
34 
35 class vtkWindow;
36 class vtkViewport;
37 class vtkStdString;
38 class vtkUnicodeString;
39 class vtkTextProperty;
40 class vtkPoints2D;
41 class vtkImageData;
42 class vtkMatrix3x3;
43 class vtkAbstractContextBufferId;
44 class vtkPen;
45 class vtkBrush;
46 class vtkRectf;
47 class vtkPolyData;
48 class vtkUnsignedCharArray;
49 
50 class VTKRENDERINGCONTEXT2D_EXPORT vtkContextDevice2D : public vtkObject
51 {
52 public:
53   vtkTypeMacro(vtkContextDevice2D, vtkObject);
54   void PrintSelf(ostream &os, vtkIndent indent) override;
55 
56   static vtkContextDevice2D * New();
57 
58   /**
59    * Draw a poly line using the points - fastest code path due to memory
60    * layout of the coordinates. The line will be colored by
61    * the colors array, which must be have nc_comps components (defining a single
62    * color).
63    * \sa DrawLines()
64    */
65   virtual void DrawPoly(float *points, int n,
66                         unsigned char *colors = nullptr, int nc_comps = 0) = 0;
67 
68   /**
69    * Draw lines using the points - memory layout is as follows:
70    * l1p1,l1p2,l2p1,l2p2... The lines will be colored by colors array
71    * which has nc_comps components (defining a single color).
72    * \sa DrawPoly()
73    */
74   virtual void DrawLines(float *f, int n, unsigned char *colors = nullptr,
75                          int nc_comps = 0) = 0;
76 
77   /**
78    * Draw a series of points - fastest code path due to memory layout of the
79    * coordinates. The colors and nc_comps are optional - color array.
80    */
81   virtual void DrawPoints(float *points, int n, unsigned char* colors = nullptr,
82                           int nc_comps = 0) = 0;
83 
84   /**
85    * Draw a series of point sprites, images centred at the points supplied.
86    * The supplied vtkImageData is the sprite to be drawn, only squares will be
87    * drawn and the size is set using SetPointSize.
88    * \param colors is an optional array of colors.
89    * \param nc_comps is the number of components for the color.
90    */
91   virtual void DrawPointSprites(vtkImageData *sprite, float *points, int n,
92                                 unsigned char *colors = nullptr, int nc_comps = 0) = 0;
93 
94   /**
95    * Draw a series of markers centered at the points supplied. The \a shape
96    * argument controls the marker shape, and can be one of
97    * - VTK_MARKER_CROSS
98    * - VTK_MARKER_PLUS
99    * - VTK_MARKER_SQUARE
100    * - VTK_MARKER_CIRCLE
101    * - VTK_MARKER_DIAMOND
102    * \param colors is an optional array of colors.
103    * \param nc_comps is the number of components for the color.
104    */
105   virtual void DrawMarkers(int shape, bool highlight, float *points, int n,
106                            unsigned char *colors = nullptr, int nc_comps = 0);
107 
108   /**
109    * Draw a quad using the specified number of points.
110    */
DrawQuad(float *,int)111   virtual void DrawQuad(float *, int) { ; }
112 
113   /**
114    * Draw a quad using the specified number of points.
115    */
DrawQuadStrip(float *,int)116   virtual void DrawQuadStrip(float *, int) { ; }
117 
118   /**
119    * Draw a polygon using the specified number of points.
120    * @{
121    */
DrawPolygon(float * p,int n)122   virtual void DrawPolygon(float *p, int n) { this->DrawColoredPolygon(p, n); }
123   virtual void DrawColoredPolygon(float *points, int numPoints,
124                                   unsigned char *colors = nullptr,
125                                   int nc_comps = 0);
126   /**@}*/
127 
128   /**
129    * Draw an elliptic wedge with center at x, y, outer radii outRx, outRy,
130    * inner radii inRx, inRy between angles startAngle and stopAngle
131    * (expressed in degrees).
132    * \pre positive_outRx: outRx>=0
133    * \pre positive_outRy: outRy>=0
134    * \pre positive_inRx: inRx>=0
135    * \pre positive_inRy: inRy>=0
136    * \pre ordered_rx: inRx<=outRx
137    * \pre ordered_ry: inRy<=outRy
138    */
139   virtual void DrawEllipseWedge(float x, float y, float outRx, float outRy,
140                                 float inRx, float inRy, float startAngle,
141                                 float stopAngle)=0;
142 
143   /**
144    * Draw an elliptic arc with center at x,y with radii rX and rY between
145    * angles startAngle and stopAngle (expressed in degrees).
146    * \pre positive_rX: rX>=0
147    * \pre positive_rY: rY>=0
148    */
149   virtual void DrawEllipticArc(float x, float y, float rX, float rY,
150                                float startAngle, float stopAngle)=0;
151 
152   /**
153    * Draw some text to the screen.
154    */
155   virtual void DrawString(float *point, const vtkStdString &string) = 0;
156 
157   /**
158    * Compute the bounds of the supplied string. The bounds will be copied to the
159    * supplied bounds variable, the first two elements are the bottom corner of
160    * the string, and the second two elements are the width and height of the
161    * bounding box.
162    * NOTE: This function does not take account of the text rotation or justification.
163    */
164   virtual void ComputeStringBounds(const vtkStdString &string,
165                                    float bounds[4]) = 0;
166 
167   /**
168    * Draw some text to the screen.
169    */
170   virtual void DrawString(float *point, const vtkUnicodeString &string) = 0;
171 
172   /**
173    * Compute the bounds of the supplied string. The bounds will be copied to the
174    * supplied bounds variable, the first two elements are the bottom corner of
175    * the string, and the second two elements are the width and height of the
176    * bounding box.
177    * NOTE: This function does not take account of the text rotation or justification.
178    */
179   virtual void ComputeStringBounds(const vtkUnicodeString &string,
180                                    float bounds[4]) = 0;
181 
182   /**
183    * Compute the bounds of the supplied string while taking into account the
184    * justification of the currently applied text property. Simple rotations
185    * (0, 90, 180, 270) are also correctly taken into account.
186    */
187   virtual void ComputeJustifiedStringBounds(const char* string, float bounds[4]) = 0;
188 
189   /**
190    * Draw text using MathText markup for mathematical equations. See
191    * http://matplotlib.sourceforge.net/users/mathtext.html for more information.
192    */
193   virtual void DrawMathTextString(float *point, const vtkStdString &string) = 0;
194 
195   /**
196    * Return true if MathText rendering available on this device.
197    */
198   virtual bool MathTextIsSupported();
199 
200   /**
201    * Draw the supplied image at the given x, y (p[0], p[1]) (bottom corner),
202    * scaled by scale (1.0 would match the image).
203    */
204   virtual void DrawImage(float p[2], float scale, vtkImageData *image) = 0;
205 
206   /**
207    * Draw the supplied image at the given position. The origin, width, and
208    * height are specified by the supplied vtkRectf variable pos. The image
209    * will be drawn scaled to that size.
210    */
211   virtual void DrawImage(const vtkRectf& pos, vtkImageData *image) = 0;
212 
213   /**
214    * Draw the supplied PolyData at the given x, y (p[0], p[1]) (bottom corner),
215    * scaled by scale (1.0 would match the actual dataset).
216    *
217    * Only lines and polys are rendered. Only the x/y coordinates of the
218    * polydata are used.
219    *
220    * @param p Offset to apply to polydata.
221    * @param scale Isotropic scale for polydata. Applied after offset.
222    * @param polyData Draw lines and polys from this dataset.
223    * @param colors RGBA for points or cells, depending on value of scalarMode.
224    * Must not be NULL.
225    * @param scalarMode Must be either VTK_SCALAR_MODE_USE_POINT_DATA or
226    * VTK_SCALAR_MODE_USE_CELL_DATA.
227    *
228    * The base implementation breaks the polydata apart and renders each polygon
229    * individually using the device API. Subclasses should override this method
230    * with a batch-drawing implementation if performance is a concern.
231    */
232   virtual void DrawPolyData(float p[2], float scale, vtkPolyData* polyData,
233     vtkUnsignedCharArray* colors, int scalarMode);
234 
235   /**
236    * Apply the supplied pen which controls the outlines of shapes, as well as
237    * lines, points and related primitives. This makes a deep copy of the vtkPen
238    * object in the vtkContext2D, it does not hold a pointer to the supplied object.
239    */
240   virtual void ApplyPen(vtkPen *pen);
241 
242   //@{
243   /**
244    * Get the pen which controls the outlines of shapes, as well as lines,
245    * points and related primitives. This object can be modified and the changes
246    * will be reflected in subsequent drawing operations.
247    */
248   vtkGetObjectMacro(Pen, vtkPen);
249   //@}
250 
251   /**
252    * Apply the supplied brush which controls the outlines of shapes, as well as
253    * lines, points and related primitives. This makes a deep copy of the vtkBrush
254    * object in the vtkContext2D, it does not hold a pointer to the supplied object.
255    */
256   virtual void ApplyBrush(vtkBrush *brush);
257 
258   //@{
259   /**
260    * Get the pen which controls the outlines of shapes as well as lines, points
261    * and related primitives.
262    */
263   vtkGetObjectMacro(Brush, vtkBrush);
264   //@}
265 
266   /**
267    * Apply the supplied text property which controls how text is rendered.
268    * This makes a deep copy of the vtkTextProperty object in the vtkContext2D,
269    * it does not hold a pointer to the supplied object.
270    */
271   virtual void ApplyTextProp(vtkTextProperty *prop);
272 
273   //@{
274   /**
275    * Get the text properties object for the vtkContext2D.
276    */
277   vtkGetObjectMacro(TextProp, vtkTextProperty);
278   //@}
279 
280   /**
281    * Set the color for the device using unsigned char of length 4, RGBA.
282    */
283   virtual void SetColor4(unsigned char color[4]) = 0;
284 
285   enum TextureProperty {
286     Nearest = 0x01,
287     Linear  = 0x02,
288     Stretch = 0x04,
289     Repeat  = 0x08
290   };
291   /**
292    * Set the texture for the device, it is used to fill the polygons
293    */
294   virtual void SetTexture(vtkImageData* image, int properties) = 0;
295 
296   /**
297    * Set the point size for glyphs/sprites.
298    */
299   virtual void SetPointSize(float size) = 0;
300 
301   /**
302    * Set the line width.
303    */
304   virtual void SetLineWidth(float width) = 0;
305 
306   /**
307    * Set the line type type (using anonymous enum in vtkPen).
308    */
309   virtual void SetLineType(int type) = 0;
310 
311   /**
312    * Get the width of the device in pixels.
313    */
GetWidth()314   virtual int GetWidth() { return this->Geometry[0]; }
315 
316   /**
317    * Get the width of the device in pixels.
318    */
GetHeight()319   virtual int GetHeight() { return this->Geometry[1]; }
320 
321   /**
322    * Set the model view matrix for the display
323    */
324   virtual void SetMatrix(vtkMatrix3x3 *m) = 0;
325 
326   /**
327    * Set the model view matrix for the display
328    */
329   virtual void GetMatrix(vtkMatrix3x3 *m) = 0;
330 
331   /**
332    * Multiply the current model view matrix by the supplied one
333    */
334   virtual void MultiplyMatrix(vtkMatrix3x3 *m) = 0;
335 
336   /**
337    * Push the current matrix onto the stack.
338    */
339   virtual void PushMatrix() = 0;
340 
341   /**
342    * Pop the current matrix off of the stack.
343    */
344   virtual void PopMatrix() = 0;
345 
346   /**
347    * Supply an int array of length 4 with x1, y1, width, height specifying
348    * clipping region for the device in pixels.
349    */
350   virtual void SetClipping(int *x) = 0;
351 
352   /**
353    * Disable clipping of the display.
354    * Remove in a future release - retained for API compatibility.
355    */
DisableClipping()356   virtual void DisableClipping() { this->EnableClipping(false); }
357 
358   /**
359    * Enable or disable the clipping of the scene.
360    */
361   virtual void EnableClipping(bool enable) = 0;
362 
363   /**
364    * Begin drawing, pass in the viewport to set up the view.
365    */
Begin(vtkViewport *)366   virtual void Begin(vtkViewport*) { }
367 
368   /**
369    * End drawing, clean up the view.
370    */
End()371   virtual void End() { }
372 
373   /**
374    * Tell if the device context is in BufferId creation mode.
375    * Initial value is false.
376    */
377   virtual bool GetBufferIdMode() const;
378 
379   /**
380    * Start BufferId creation Mode.
381    * The default implementation is empty.
382    * \pre not_yet: !GetBufferIdMode()
383    * \pre bufferId_exists: bufferId!=0
384    * \post started: GetBufferIdMode()
385    */
386   virtual void BufferIdModeBegin(vtkAbstractContextBufferId *bufferId);
387 
388   /**
389    * Finalize BufferId creation Mode. It makes sure that the content of the
390    * bufferId passed in argument of BufferIdModeBegin() is correctly set.
391    * The default implementation is empty.
392    * \pre started: GetBufferIdMode()
393    * \post done: !GetBufferIdMode()
394    */
395   virtual void BufferIdModeEnd();
396 
SetViewportSize(const vtkVector2i & size)397   virtual void SetViewportSize(const vtkVector2i &size)
398   {
399     this->ViewportSize = size;
400   }
vtkGetMacro(ViewportSize,vtkVector2i)401   vtkGetMacro(ViewportSize, vtkVector2i)
402 
403   virtual void SetViewportRect(const vtkRecti &rect)
404   {
405     this->ViewportRect = rect;
406   }
407   vtkGetMacro(ViewportRect, vtkRecti)
408 
409 protected:
410   vtkContextDevice2D();
411   ~vtkContextDevice2D() override;
412 
413   /**
414    * Store the width and height of the device in pixels.
415    */
416   int Geometry[2];
417 
418   /**
419    * Store the size of the total viewport.
420    */
421   vtkVector2i ViewportSize;
422 
423   /**
424    * Store our origin and size in the total viewport.
425    */
426   vtkRecti ViewportRect;
427 
428   vtkAbstractContextBufferId *BufferId;
429 
430   vtkPen *Pen;                // Outlining
431   vtkBrush *Brush;            // Fills
432   vtkTextProperty *TextProp;  // Text property
433 
434 private:
435   vtkContextDevice2D(const vtkContextDevice2D &) = delete;
436   void operator=(const vtkContextDevice2D &) = delete;
437 
438 };
439 
440 #endif //vtkContextDevice2D_h
441