Logo ROOT   6.16/01
Reference Guide
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);
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 != 0 && "Reset, gPad is null");
60 //We need a gRandom to exist.
61 assert(gRandom != 0 && "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 != 0 && "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}
ROOT::R::TRInterface & r
Definition: Object.C:4
int Int_t
Definition: RtypesCore.h:41
double Double_t
Definition: RtypesCore.h:55
const char Option_t
Definition: RtypesCore.h:62
@ kRed
Definition: Rtypes.h:63
@ kBlue
Definition: Rtypes.h:63
void Warning(const char *location, const char *msgfmt,...)
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
#define gPad
Definition: TVirtualPad.h:286
Fill Area Attributes class.
Definition: TAttFill.h:19
virtual void Modify()
Change current fill area attributes if necessary.
Definition: TAttFill.cxx:210
Line Attributes class.
Definition: TAttLine.h:18
virtual void Modify()
Change current line attributes if necessary.
Definition: TAttLine.cxx:234
The Canvas class.
Definition: TCanvas.h:31
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:693
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:519
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:533
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:180
Double_t Cos(Double_t)
Definition: TMath.h:629
Double_t Sin(Double_t)
Definition: TMath.h:625
constexpr Double_t TwoPi()
Definition: TMath.h:45