1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestContext.cxx
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 #include "vtkRenderer.h"
17 #include "vtkRenderWindow.h"
18 #include "vtkRenderWindowInteractor.h"
19 #include "vtkSmartPointer.h"
20 #include "vtkObjectFactory.h"
21 #include "vtkContext2D.h"
22 #include "vtkTransform2D.h"
23 #include "vtkContextItem.h"
24 #include "vtkContextView.h"
25 #include "vtkContextScene.h"
26 #include "vtkPen.h"
27 #include "vtkBrush.h"
28 #include "vtkTextProperty.h"
29 #include "vtkOpenGLContextDevice2D.h"
30 #include "vtkPoints2D.h"
31 #include "vtkNew.h"
32 
33 #include "vtkRegressionTestImage.h"
34 
35 //----------------------------------------------------------------------------
36 class ContextTest : public vtkContextItem
37 {
38 public:
39   static ContextTest *New();
40   vtkTypeMacro(ContextTest, vtkContextItem);
41   // Paint event for the chart, called whenever the chart needs to be drawn
42   virtual bool Paint(vtkContext2D *painter);
43 };
44 
45 //----------------------------------------------------------------------------
TestContext(int,char * [])46 int TestContext( int, char * [] )
47 {
48   // Set up a 2D context view, context test object and add it to the scene
49   vtkNew<vtkContextView> view;
50   view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
51   view->GetRenderWindow()->SetSize(800, 600);
52   vtkNew<ContextTest> test;
53   view->GetScene()->AddItem(test.GetPointer());
54 
55   // Force the use of the freetype based rendering strategy
56   vtkOpenGLContextDevice2D::SafeDownCast(view->GetContext()->GetDevice())
57       ->SetStringRendererToFreeType();
58 
59   view->GetRenderWindow()->SetMultiSamples(0);
60   view->GetInteractor()->Initialize();
61   view->GetInteractor()->Start();
62   return EXIT_SUCCESS;
63 }
64 
65 // Make our new derived class to draw a diagram
66 vtkStandardNewMacro(ContextTest);
67 // This function aims to test the primitives provided by the 2D API.
Paint(vtkContext2D * painter)68 bool ContextTest::Paint(vtkContext2D *painter)
69 {
70   // Test the string drawing functionality of the context
71   painter->GetTextProp()->SetVerticalJustificationToCentered();
72   painter->GetTextProp()->SetJustificationToCentered();
73   painter->GetTextProp()->SetColor(0.0, 0.0, 0.0);
74   painter->GetTextProp()->SetFontSize(24);
75   painter->GetTextProp()->SetFontFamilyToArial();
76   painter->GetPen()->SetColor(0, 0, 0, 255);
77   painter->GetBrush()->SetColor(0, 0, 0, 255);
78   painter->DrawString(400, 25, "OpenGL is used as a backend to the context.");
79 
80   // Draw some individual lines of different thicknesses.
81   for (int i = 0; i < 10; ++i)
82     {
83     painter->GetPen()->SetColor(255,
84                                 static_cast<unsigned char>(float(i)*25.0),
85                                 0);
86     painter->GetPen()->SetWidth(1.0 + float(i));
87     painter->DrawLine(10, 50 + float(i)*10, 60, 50 + float(i)*10);
88     }
89 
90   // Use the draw lines function now to draw a shape.
91   vtkSmartPointer<vtkPoints2D> points = vtkSmartPointer<vtkPoints2D>::New();
92   points->SetNumberOfPoints(30);
93   for (int i = 0; i < 30; ++i)
94     {
95     double point[2] = { float(i) * 25.0 + 10.0,
96                         sin(float(i) / 5.0) * 100.0 + 200.0 };
97     points->SetPoint(i, point);
98     }
99   painter->GetPen()->SetColor(0, 255, 0);
100   painter->GetPen()->SetWidth(5.0);
101   painter->DrawPoly(points);
102 
103   // Now to draw some points
104   painter->GetPen()->SetColor(0, 0, 255);
105   painter->GetPen()->SetWidth(5.0);
106   painter->DrawPoint(10, 10);
107   painter->DrawPoint(790, 10);
108   painter->DrawPoint(10, 590);
109   painter->DrawPoint(790, 590);
110 
111   // Test the markers
112   float markerPoints[10*2];
113   unsigned char markerColors[10*4];
114   for (int i = 0; i < 10; ++i)
115     {
116     markerPoints[2 * i]     = 500.0 + i * 30.0;
117     markerPoints[2 * i + 1] = 20 * sin(markerPoints[2 * i]) + 375.0;
118 
119     markerColors[4 * i]     = static_cast<unsigned char>(255 * i / 10.0);
120     markerColors[4 * i + 1] =
121         static_cast<unsigned char>(255 * (1.0 - i / 10.0));
122     markerColors[4 * i + 2] = static_cast<unsigned char>(255 * (0.3));
123     markerColors[4 * i + 3] =
124         static_cast<unsigned char>(255 * (1.0 - ((i / 10.0) * 0.25)));
125     }
126 
127   for (int style = VTK_MARKER_NONE + 1; style < VTK_MARKER_UNKNOWN; ++style)
128     {
129     // Increment the y values:
130     for (int i = 1; i < 20; i += 2)
131       {
132       markerPoints[i] += 35.0;
133       }
134     painter->GetPen()->SetWidth(style * 5 + 5);
135     // Not highlighted:
136     painter->DrawMarkers(style, false, markerPoints, 10, markerColors, 4);
137     // Highlight the middle 4 points
138     painter->GetPen()->SetColorF(0.9, 0.8, 0.1, 0.5);
139     painter->DrawMarkers(style, true, markerPoints + 3*2, 4);
140     }
141 
142   // Draw some individual lines of different thicknesses.
143   for (int i = 0; i < 10; ++i)
144     {
145     painter->GetPen()->SetColor(0,
146                                 static_cast<unsigned char>(float(i)*25.0),
147                                 255, 255);
148     painter->GetPen()->SetWidth(1.0 + float(i));
149     painter->DrawPoint(75, 50 + float(i)*10);
150     }
151 
152   painter->GetPen()->SetColor(0, 0, 255);
153   painter->GetPen()->SetWidth(3.0);
154   painter->DrawPoints(points);
155 
156   // Now draw a rectangle
157   painter->GetPen()->SetColor(100, 200, 255);
158   painter->GetPen()->SetWidth(3.0);
159   painter->GetBrush()->SetColor(100, 255, 100);
160   painter->DrawRect(100, 50, 200, 100);
161 
162   // Add in an arbitrary quad
163   painter->GetPen()->SetColor(159, 0, 255);
164   painter->GetPen()->SetWidth(1.0);
165   painter->GetBrush()->SetColor(100, 55, 0, 200);
166   painter->DrawQuad(350, 50, 375, 150,
167                     525, 199, 666, 45);
168 
169   // Now to test out the transform...
170   vtkNew<vtkTransform2D> transform;
171   transform->Translate(20, 200);
172   painter->GetDevice()->SetMatrix(transform->GetMatrix());
173   painter->GetPen()->SetColor(255, 0, 0);
174   painter->GetPen()->SetWidth(6.0);
175   painter->DrawPoly(points);
176 
177   transform->Translate(0, 10);
178   painter->GetDevice()->SetMatrix(transform->GetMatrix());
179   painter->GetPen()->SetColor(0, 0, 200);
180   painter->GetPen()->SetWidth(2.0);
181   painter->DrawPoints(points);
182 
183   transform->Translate(0, -20);
184   painter->GetDevice()->SetMatrix(transform->GetMatrix());
185   painter->GetPen()->SetColor(100, 0, 200);
186   painter->GetPen()->SetWidth(5.0);
187   painter->DrawPoints(points);
188 
189   // Now for an ellipse...
190   painter->GetPen()->SetColor(0, 0, 0);
191   painter->GetPen()->SetWidth(1.0);
192   painter->GetBrush()->SetColor(0, 0, 100, 69);
193   painter->DrawEllipse(110.0, 89.0, 20, 100);
194   painter->DrawEllipseWedge(250.0, 89.0, 100, 20, 50, 10, 0, 360);
195 
196   return true;
197 }
198