Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
polytest1.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphics
3/// \notebook
4/// This macro is testing the "compacting" algorithm in TPadPainter.
5/// It reduces the number of polygon's vertices using actual pixel coordinates.
6///
7/// \macro_image
8///
9/// It's not really useful, but just to test that the resulting polygon
10/// is still reasonable. Initial number of points is 1000000, after "compression"
11/// it's 523904 (with default canvas size, before you tried to resize it) - so almost half of
12/// vertices were removed but you can still see the reasonable shape. If you resize
13/// a canvas to a smaller size, the number of vertices after compression can be something like 5000 and even less.
14/// It's easy to 'fool' this algorithm though in this particular case (ellipse is a kind of fringe case,
15/// you can easily have a sequence of almost unique vertices (at a pixel level).
16///
17/// \macro_code
18///
19/// \author Timur Pocheptsov
20
21//Includes for ACLiC.
22#include <cassert>
23#include <vector>
24
25#include "TRandom.h"
26#include "TCanvas.h"
27#include "TError.h"
28#include "Rtypes.h"
29#include "TNamed.h"
30#include "TMath.h"
31
32class PolyTest1 : public TNamed, public TAttLine, public TAttFill {
33public:
34 PolyTest1(unsigned nVertices);
35
36 void Paint(const Option_t *notUsed) override;
37 void Reset(unsigned nVertices);
38
39private:
40 enum {
41 kNPointsDefault = 10000//minimal number of points.
42 };
43
44 std::vector<Double_t> fXs;
45 std::vector<Double_t> fYs;
46};
47
48//_____________________________________________________________
49PolyTest1::PolyTest1(unsigned nVertices)
50 : TNamed("polygon_compression_test1", "polygon_compression_test1")
51{
52 Reset(nVertices);
53}
54
55//_____________________________________________________________
56void PolyTest1::Reset(unsigned nVertices)
57{
58 //Some canvas must already exist by this point.
59 assert(gPad != nullptr && "Reset, gPad is null");
60 //We need a gRandom to exist.
61 assert(gRandom != nullptr && "Reset, gRandom is null");
62
63 if (nVertices < kNPointsDefault) {
64 Warning("Reset", "resetting nVertices parameter to %u", unsigned(kNPointsDefault));
65 nVertices = kNPointsDefault;
66 }
67
68 fXs.resize(nVertices);
69 fYs.resize(nVertices);
70
71 Double_t xMin = 0., xMax = 0., yMin = 0., yMax = 0.;
72 gPad->GetRange(xMin, yMin, xMax, yMax);
73 assert(xMax - xMin > 0 && yMax - yMin > 0 && "Reset, invalid canvas' ranges");
74
75 const Double_t xCentre = xMin + 0.5 * (xMax - xMin);
76 const Double_t yCentre = yMin + 0.5 * (yMax - yMin);
77
78 const Double_t r = TMath::Min(xMax - xMin, yMax - yMin) * 0.8 / 2;
79 const Double_t angle = TMath::TwoPi() / (nVertices - 1);
80
81 for (unsigned i = 0; i < nVertices - 1; ++i) {
82 const Double_t currR = r + gRandom->Rndm() * r * 0.01;
83 fXs[i] = xCentre + currR * TMath::Cos(angle * i);
84 fYs[i] = yCentre + currR * TMath::Sin(angle * i);
85 }
86
87 fXs[nVertices - 1] = fXs[0];
88 fYs[nVertices - 1] = fYs[0];
89}
90
91//_____________________________________________________________
92void PolyTest1::Paint(const Option_t * /*notUsed*/)
93{
94 assert(gPad != nullptr && "Paint, gPad is null");
95
97 gPad->PaintFillArea((Int_t)fXs.size(), &fXs[0], &fYs[0]);
98
100 gPad->PaintPolyLine((Int_t)fXs.size(), &fXs[0], &fYs[0]);
101}
102
103void polytest1()
104{
105 TCanvas * const cnv = new TCanvas;
106 cnv->cd();
107
108 PolyTest1 * polygon = new PolyTest1(1000000);
109 polygon->SetLineColor(kBlue);
110 polygon->SetFillColor(kRed);
111 polygon->SetLineWidth(1);
112 polygon->Draw();//Attach a polygon to a canvas.
113}
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
const char Option_t
Definition RtypesCore.h:66
@ kRed
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint angle
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#define gPad
Fill Area Attributes class.
Definition TAttFill.h:19
virtual void Modify()
Change current fill area attributes if necessary.
Definition TAttFill.cxx:213
Line Attributes class.
Definition TAttLine.h:18
virtual void Modify()
Change current line attributes if necessary.
Definition TAttLine.cxx:245
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:716
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
virtual void Paint(Option_t *option="")
This method must be overridden if a class wants to paint itself.
Definition TObject.cxx:607
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:552
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:594
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:588
constexpr Double_t TwoPi()
Definition TMath.h:44