ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
polytest1.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphics
3 /// This macro is testing the "compacting" algorithm in TPadPainter.
4 /// It reduces the number of polygon's vertices using actual pixel coordinates.
5 ///
6 /// \macro_image
7 ///
8 /// It's not really useful, but just to test that the resulting polygon
9 /// is still reasonable. Initial number of points is 1000000, after "compression"
10 /// it's 523904 (with default canvas size, before you tried to resize it) - so almost half of
11 /// vertices were removed but you can still see the reasonable shape. If you resize
12 /// a canvas to a smaller size, the number of vertices after compression can be something like 5000 and even less.
13 /// It's easy to 'fool' this algorithm though in this particular case (ellipse is a kind of fringe case,
14 /// you can easily have a sequence of almost unique vertices (at a pixel level).
15 ///
16 /// \macro_code
17 ///
18 /// \author Timur Pocheptsov
19 
20 //Includes for ACLiC.
21 #include <cassert>
22 #include <vector>
23 
24 #include "TRandom.h"
25 #include "TCanvas.h"
26 #include "TError.h"
27 #include "Rtypes.h"
28 #include "TNamed.h"
29 #include "TMath.h"
30 
31 class PolyTest1 : public TNamed, public TAttLine, public TAttFill {
32 public:
33  PolyTest1(unsigned nVertices);
34 
35  void Paint(const Option_t *notUsed);
36  void Reset(unsigned nVertices);
37 
38 private:
39  enum {
40  kNPointsDefault = 10000//minimal number of points.
41  };
42 
43  std::vector<Double_t> fXs;
44  std::vector<Double_t> fYs;
45 };
46 
47 //_____________________________________________________________
48 PolyTest1::PolyTest1(unsigned nVertices)
49  : TNamed("polygon_compression_test1", "polygon_compression_test1")
50 {
51  Reset(nVertices);
52 }
53 
54 //_____________________________________________________________
55 void PolyTest1::Reset(unsigned nVertices)
56 {
57  //Some canvas must already exist by this point.
58  assert(gPad != 0 && "Reset, gPad is null");
59  //We need a gRandom to exist.
60  assert(gRandom != 0 && "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 //_____________________________________________________________
91 void PolyTest1::Paint(const Option_t * /*notUsed*/)
92 {
93  assert(gPad != 0 && "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 
102 void 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 }
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
const char Option_t
Definition: RtypesCore.h:62
Definition: Rtypes.h:61
#define assert(cond)
Definition: unittest.h:542
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:170
int Int_t
Definition: RtypesCore.h:41
virtual void Modify()
Change current line attributes if necessary.
Definition: TAttLine.cxx:229
Fill Area Attributes class.
Definition: TAttFill.h:32
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
virtual void Modify()
Change current fill area attributes if necessary.
Definition: TAttFill.cxx:206
Double_t TwoPi()
Definition: TMath.h:45
void Reset(Detail::TBranchProxy *x)
void Warning(const char *location, const char *msgfmt,...)
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
Double_t Cos(Double_t)
Definition: TMath.h:424
The Canvas class.
Definition: TCanvas.h:48
virtual void Paint(Option_t *option="")
This method must be overridden if a class wants to paint itself.
Definition: TObject.cxx:563
double Double_t
Definition: RtypesCore.h:55
Double_t Sin(Double_t)
Definition: TMath.h:421
#define gPad
Definition: TVirtualPad.h:288
Definition: Rtypes.h:61
Line Attributes class.
Definition: TAttLine.h:32