VTK  9.3.1
vtkContext2D.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
21 #ifndef vtkContext2D_h
22 #define vtkContext2D_h
23 
24 #include "vtkObject.h"
25 #include "vtkRenderingContext2DModule.h" // For export macro
26 
27 #include <cstdint> // For std::uintptr_t
28 
29 VTK_ABI_NAMESPACE_BEGIN
30 class vtkWindow;
31 
32 class vtkContext3D;
33 class vtkStdString;
34 class vtkTextProperty;
35 
36 class vtkPoints2D;
37 class vtkDataArray;
38 class vtkVector2f;
39 class vtkRectf;
41 class vtkContextDevice2D;
42 class vtkPen;
43 class vtkBrush;
44 class vtkImageData;
45 class vtkPolyData;
46 class vtkTransform2D;
48 
49 class VTKRENDERINGCONTEXT2D_EXPORT vtkContext2D : public vtkObject
50 {
51 public:
52  vtkTypeMacro(vtkContext2D, vtkObject);
53  void PrintSelf(ostream& os, vtkIndent indent) override;
54 
58  static vtkContext2D* New();
59 
65  bool Begin(vtkContextDevice2D* device);
66 
67  vtkGetObjectMacro(Device, vtkContextDevice2D);
68 
74  bool End();
75 
79  bool GetBufferIdMode() const;
80 
87  void BufferIdModeBegin(vtkAbstractContextBufferId* bufferId);
88 
95  void BufferIdModeEnd();
96 
100  void DrawLine(float x1, float y1, float x2, float y2);
101 
105  void DrawLine(float p[4]);
106 
111  void DrawLine(vtkPoints2D* points);
112 
116  void DrawPoly(float* x, float* y, int n);
117 
122  void DrawPoly(vtkPoints2D* points);
123 
129  void DrawPoly(float* points, int n);
130 
137  void DrawPoly(float* points, int n, unsigned char* colors, int nc_comps);
138 
143  void DrawLines(vtkPoints2D* points);
144 
149  void DrawLines(float* points, int n);
150 
154  void DrawPoint(float x, float y);
155 
159  void DrawPoints(float* x, float* y, int n);
160 
165  void DrawPoints(vtkPoints2D* points);
166  void DrawPoints(
167  vtkDataArray* positions, vtkUnsignedCharArray* colors, std::uintptr_t cacheIdentifier);
168 
174  void DrawPoints(float* points, int n);
175 
181  void DrawPointSprites(vtkImageData* sprite, vtkPoints2D* points);
182 
184 
190  void DrawPointSprites(vtkImageData* sprite, vtkPoints2D* points, vtkUnsignedCharArray* colors);
191  void DrawPointSprites(
192  vtkImageData* sprite, float* points, int n, unsigned char* colors, int nc_comps);
193  void DrawPointSprites(vtkImageData* sprite, vtkDataArray* positions, vtkUnsignedCharArray* colors,
194  std::uintptr_t cacheIdentifier);
196 
202  void DrawPointSprites(vtkImageData* sprite, float* points, int n);
203 
205 
221  virtual void DrawMarkers(
222  int shape, bool highlight, float* points, int n, unsigned char* colors, int nc_comps);
223  virtual void DrawMarkers(int shape, bool highlight, float* points, int n);
224  virtual void DrawMarkers(int shape, bool highlight, vtkPoints2D* points);
225  virtual void DrawMarkers(
226  int shape, bool highlight, vtkPoints2D* points, vtkUnsignedCharArray* colors);
228 
232  virtual void DrawMarkers(int shape, bool highlight, vtkDataArray* positions,
233  vtkUnsignedCharArray* colors, std::uintptr_t cacheIdentifier);
234 
238  void DrawRect(float x, float y, float w, float h);
239 
241 
244  void DrawQuad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4);
245  void DrawQuad(float* p);
247 
249 
252  void DrawQuadStrip(vtkPoints2D* points);
253  void DrawQuadStrip(float* p, int n);
255 
260  void DrawPolygon(float* x, float* y, int n);
261 
266  void DrawPolygon(vtkPoints2D* points);
267 
273  void DrawPolygon(float* points, int n);
274 
279  void DrawPolygon(float* x, float* y, int n, unsigned char* color, int nc_comps);
280 
285  void DrawPolygon(vtkPoints2D* points, unsigned char* color, int nc_comps);
286 
292  void DrawPolygon(float* points, int n, unsigned char* color, int nc_comps);
293 
299  void DrawEllipse(float x, float y, float rx, float ry);
300 
309  void DrawWedge(
310  float x, float y, float outRadius, float inRadius, float startAngle, float stopAngle);
311 
323  void DrawEllipseWedge(float x, float y, float outRx, float outRy, float inRx, float inRy,
324  float startAngle, float stopAngle);
325 
331  void DrawArc(float x, float y, float r, float startAngle, float stopAngle);
332 
339  void DrawEllipticArc(float x, float y, float rX, float rY, float startAngle, float stopAngle);
340 
344  void DrawImage(float x, float y, vtkImageData* image);
345 
350  void DrawImage(float x, float y, float scale, vtkImageData* image);
351 
357  void DrawImage(const vtkRectf& pos, vtkImageData* image);
358 
363  void DrawPolyData(
364  float x, float y, vtkPolyData* polyData, vtkUnsignedCharArray* colors, int scalarMode);
365 
367 
372  void DrawStringRect(vtkPoints2D* rect, const vtkStdString& string);
373  void DrawStringRect(vtkPoints2D* rect, const char* string);
374  void DrawStringRect(const float rect[4], const vtkStdString& string);
375  void DrawStringRect(const float rect[4], const char* string);
377 
379 
382  void DrawString(vtkPoints2D* point, const vtkStdString& string);
383  void DrawString(float x, float y, const vtkStdString& string);
384  void DrawString(vtkPoints2D* point, const char* string);
385  void DrawString(float x, float y, const char* string);
387 
389 
398  void ComputeStringBounds(const vtkStdString& string, vtkPoints2D* bounds);
399  void ComputeStringBounds(const vtkStdString& string, float bounds[4]);
400  void ComputeStringBounds(const char* string, vtkPoints2D* bounds);
401  void ComputeStringBounds(const char* string, float bounds[4]);
403 
408  void ComputeJustifiedStringBounds(const char* string, float bounds[4]);
409 
416  int ComputeFontSizeForBoundedString(const vtkStdString& string, float width, float height);
417 
419 
426  void DrawMathTextString(vtkPoints2D* point, const vtkStdString& string);
427  void DrawMathTextString(float x, float y, const vtkStdString& string);
428  void DrawMathTextString(vtkPoints2D* point, const char* string);
429  void DrawMathTextString(float x, float y, const char* string);
431 
433 
441  void DrawMathTextString(
442  vtkPoints2D* point, const vtkStdString& string, const vtkStdString& fallback);
443  void DrawMathTextString(
444  float x, float y, const vtkStdString& string, const vtkStdString& fallback);
445  void DrawMathTextString(vtkPoints2D* point, const char* string, const char* fallback);
446  void DrawMathTextString(float x, float y, const char* string, const char* fallback);
448 
452  bool MathTextIsSupported();
453 
459  void ApplyPen(vtkPen* pen);
460 
466  vtkPen* GetPen();
467 
473  void ApplyBrush(vtkBrush* brush);
474 
479  vtkBrush* GetBrush();
480 
486  void ApplyTextProp(vtkTextProperty* prop);
487 
491  vtkTextProperty* GetTextProp();
492 
498  void SetTransform(vtkTransform2D* transform);
499 
503  vtkTransform2D* GetTransform();
504 
511  void AppendTransform(vtkTransform2D* transform);
512 
514 
518  void PushMatrix();
519  void PopMatrix();
521 
525  void ApplyId(vtkIdType id);
526 
532  static int FloatToInt(float x);
533 
535 
539  vtkGetObjectMacro(Context3D, vtkContext3D);
540  virtual void SetContext3D(vtkContext3D* context);
542 
543 protected:
544  vtkContext2D();
545  ~vtkContext2D() override;
546 
547  vtkContextDevice2D* Device; // The underlying device
548  vtkTransform2D* Transform; // Current transform
549 
551  vtkContext3D* Context3D; // May be very temporary - get at a 3D version.
552 
553 private:
554  vtkContext2D(const vtkContext2D&) = delete;
555  void operator=(const vtkContext2D&) = delete;
556 
563  vtkVector2f CalculateTextPosition(vtkPoints2D* rect);
564 
571  vtkVector2f CalculateTextPosition(const float rect[4]);
572 };
573 
574 inline int vtkContext2D::FloatToInt(float x)
575 {
576  // Use a tolerance of 1/256 of a pixel when converting.
577  // A float has only 24 bits of precision, so we cannot
578  // make the tolerance too small. For example, a tolerance
579  // of 2^-8 means that the tolerance will be significant
580  // for float values up to 2^16 or 65536.0. But a
581  // tolerance of 2^-16 would only be significant for
582  // float values up to 2^8 or 256.0. A small tolerance
583  // disappears into insignificance when added to a large float.
584  float tol = 0.00390625; // 1.0/256.0
585  tol = (x >= 0 ? tol : -tol);
586  return static_cast<int>(x + tol);
587 }
588 
589 VTK_ABI_NAMESPACE_END
590 #endif // vtkContext2D_h
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:28
vtkAbstractContextBufferId * BufferId
Definition: vtkContext2D.h:550
abstract base class for most VTK objects
Definition: vtkObject.h:51
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
int vtkIdType
Definition: vtkType.h:315
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:79
static int FloatToInt(float x)
Float to int conversion, performs truncation but with a rounding tolerance for float values that are ...
Definition: vtkContext2D.h:574
window superclass for vtkRenderWindow
Definition: vtkWindow.h:27
Class for drawing 2D primitives to a graphical context.
Definition: vtkContext2D.h:49
provides a brush that fills shapes drawn by vtkContext2D.
Definition: vtkBrush.h:29
a simple class to control print indentation
Definition: vtkIndent.h:28
represent and manipulate 2D points
Definition: vtkPoints2D.h:25
topologically and geometrically regular array of data
Definition: vtkImageData.h:42
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
Abstract class for drawing 2D primitives.
provides a pen that draws the outlines of shapes drawn by vtkContext2D.
Definition: vtkPen.h:28
describes linear transformations via a 3x3 matrix
represent text properties.
vtkContextDevice2D * Device
Definition: vtkContext2D.h:547
dynamic, self-adjusting array of unsigned char
2D array of ids, used for picking.
vtkTransform2D * Transform
Definition: vtkContext2D.h:548
vtkContext3D * Context3D
Definition: vtkContext2D.h:551
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
Class for drawing 3D primitives to a graphical context.
Definition: vtkContext3D.h:33