ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
polytest2.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 /// This macro is testing new "compacting" algorithm in TPadPainter
9 /// (it reduces the number of polygon's vertices using actual pixel coordinates).
10 /// In principle, this test case is what our histograms (fringe cases) are:
11 /// "saw-like" polygon (bins == teeth).
12 ///
13 /// \macro_code
14 ///
15 /// \author Timur Pocheptsov
16 
17 //Includes for ACLiC.
18 #include <cassert>
19 #include <vector>
20 
21 #include "TRandom.h"
22 #include "TCanvas.h"
23 #include "Rtypes.h"
24 #include "TNamed.h"
25 
26 class PolyTest2 : public TNamed, public TAttLine, public TAttFill {
27 public:
28  PolyTest2();
29 
30  void Paint(const Option_t *notUsed);
31 
32 private:
33  enum TestSize {
34  kNSawPoints = 10000
35  };
36 
37  //Part 1.
38  std::vector<Double_t> fXs1;
39  std::vector<Double_t> fYs1;
40  //Part 2.
41 
42  std::vector<Double_t> fXs2;
43  std::vector<Double_t> fYs2;
44 };
45 
46 //_____________________________________________________________
47 PolyTest2::PolyTest2()
48  : TNamed("polygon_compression_test2", "polygon_compression_test2")
49 {
50  //Polygon 1, n of points is 10003, after 'compression' : 1897
51  //Polygon 2, n of points is 10003, after 'compression' : 2093
52 
53  //Some canvas must already exist by this point.
54  assert(gPad != 0 && "PolyTest2, gPad is null");
55  //We need a gRandom to exist.
56  assert(gRandom != 0 && "PolyTest2, gRandom is null");
57 
58  Double_t xMin = 0., xMax = 0., yMin = 0., yMax = 0.;
59  gPad->GetRange(xMin, yMin, xMax, yMax);
60  assert(xMax - xMin > 0 && yMax - yMin > 0 && "PolyTest2, invalid canvas' ranges");
61 
62 
63  // .(0/the last)--------.(1)
64  // | /
65  // | \
66  // | /
67  // .(kNSawPoints + 1)--.(kNSawPoints)
68 
69  const unsigned nVertices = 3 + kNSawPoints;
70 
71  {
72  //Polygon 1, "vertical saw":
73  fXs1.resize(nVertices);
74  fYs1.resize(nVertices);
75 
76  fXs1[0] = 0.;
77  fYs1[0] = 0.;
78 
79  const Double_t w1 = 0.2 * (xMax - xMin);
80  const Double_t saw1ToothSize = 0.1 * w1;
81  const Double_t yStep = (yMax - yMin) / (kNSawPoints - 1);
82 
83  for (unsigned i = 1; i <= kNSawPoints; ++i) {
84  fXs1[i] = w1 + gRandom->Rndm() * saw1ToothSize;
85  fYs1[i] = yMin + yStep * (i - 1);
86  }
87 
88  fXs1[nVertices - 2] = 0.;
89  fYs1[nVertices - 2] = yMax;
90  //Let's close it.
91  fXs1[nVertices - 1] = fXs1[0];
92  fYs1[nVertices - 1] = fYs1[0];
93 
94  }
95 
96  //Polygon 2, "horizontal saw":
97 
98  {
99  const Double_t x2Min = xMin + 0.25 * (xMax - xMin);
100  const Double_t h2 = 0.1 * (yMax - yMin);
101  const Double_t saw2ToothSize = 0.1 * h2;
102  const Double_t xStep = (xMax - x2Min) / (kNSawPoints - 1);
103 
104  fXs2.resize(nVertices);
105  fYs2.resize(nVertices);
106 
107  fXs2[0] = x2Min;
108  fYs2[0] = 0.;
109 
110  for (unsigned i = 1; i <= kNSawPoints; ++i) {
111  fXs2[i] = x2Min + xStep * i;
112  fYs2[i] = h2 + gRandom->Rndm() * saw2ToothSize;
113  }
114 
115  fXs2[nVertices - 2] = xMax;
116  fYs2[nVertices - 2] = 0.;
117  fXs2[nVertices - 1] = fXs2[0];
118  fYs2[nVertices - 1] = fYs2[0];
119  }
120 }
121 
122 //_____________________________________________________________
123 void PolyTest2::Paint(const Option_t * /*notUsed*/)
124 {
125  assert(gPad != 0 && "Paint, gPad is null");
126 
129  gPad->PaintFillArea((Int_t)fXs1.size(), &fXs1[0], &fYs1[0]);
130 
133  gPad->PaintPolyLine((Int_t)fXs1.size(), &fXs1[0], &fYs1[0]);
134 
137  gPad->PaintFillArea((Int_t)fXs2.size(), &fXs2[0], &fYs2[0]);
138 
141  gPad->PaintPolyLine((Int_t)fXs2.size(), &fXs2[0], &fYs2[0]);
142 }
143 
144 void polytest2()
145 {
146  TCanvas * const cnv = new TCanvas;
147  cnv->cd();
148 
149  PolyTest2 * polygon = new PolyTest2;
150  polygon->Draw();//Attach a polygon to a canvas.
151 }
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
#define assert(cond)
Definition: unittest.h:542
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
int Int_t
Definition: RtypesCore.h:41
Definition: Rtypes.h:61
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
TH2D * h2
Definition: fit2dHist.C:45
lv SetLineColor(kBlue)
h1 SetFillColor(kGreen)
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
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
#define gPad
Definition: TVirtualPad.h:288
Definition: Rtypes.h:61
Line Attributes class.
Definition: TAttLine.h:32
Definition: Rtypes.h:62