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) : TNamed("polygon_compression_test1", "polygon_compression_test1")
50{
51 Reset(nVertices);
52}
53
54//_____________________________________________________________
55void PolyTest1::Reset(unsigned nVertices)
56{
57 // Some canvas must already exist by this point.
58 assert(gPad != nullptr && "Reset, gPad is null");
59 // We need a gRandom to exist.
60 assert(gRandom != nullptr && "Reset, gRandom is null");
61
62 if (nVertices < kNPointsDefault) {
63 Warning("Reset", "resetting nVertices parameter to %u", unsigned(kNPointsDefault));
64 nVertices = kNPointsDefault;
65 }
66
67 fXs.resize(nVertices);
68 fYs.resize(nVertices);
69
70 Double_t xMin = 0., xMax = 0., yMin = 0., yMax = 0.;
71 gPad->GetRange(xMin, yMin, xMax, yMax);
72 assert(xMax - xMin > 0 && yMax - yMin > 0 && "Reset, invalid canvas' ranges");
73
74 const Double_t xCentre = xMin + 0.5 * (xMax - xMin);
75 const Double_t yCentre = yMin + 0.5 * (yMax - yMin);
76
77 const Double_t r = TMath::Min(xMax - xMin, yMax - yMin) * 0.8 / 2;
78 const Double_t angle = TMath::TwoPi() / (nVertices - 1);
79
80 for (unsigned i = 0; i < nVertices - 1; ++i) {
81 const Double_t currR = r + gRandom->Rndm() * r * 0.01;
82 fXs[i] = xCentre + currR * TMath::Cos(angle * i);
83 fYs[i] = yCentre + currR * TMath::Sin(angle * i);
84 }
85
86 fXs[nVertices - 1] = fXs[0];
87 fYs[nVertices - 1] = fYs[0];
88}
89
90//_____________________________________________________________
91void PolyTest1::Paint(const Option_t * /*notUsed*/)
92{
93 assert(gPad != nullptr && "Paint, gPad is null");
94
96 gPad->PaintFillArea((Int_t)fXs.size(), &fXs[0], &fYs[0]);
97
99 gPad->PaintPolyLine((Int_t)fXs.size(), &fXs[0], &fYs[0]);
100}
101
102void polytest1()
103{
104 TCanvas *const cnv = new TCanvas;
105 cnv->cd();
106
107 PolyTest1 *polygon = new PolyTest1(1000000);
108 polygon->SetLineColor(kBlue);
109 polygon->SetFillColor(kRed);
110 polygon->SetLineWidth(1);
111 polygon->Draw(); // Attach a polygon to a canvas.
112}
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:216
Line Attributes class.
Definition TAttLine.h:18
virtual void Modify()
Change current line attributes if necessary.
Definition TAttLine.cxx:247
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:719
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:624
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
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:598
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:592
constexpr Double_t TwoPi()
Definition TMath.h:44