ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
viewer3DMaster.C
Go to the documentation of this file.
1 // Demonstrates 3D viewer architecture TVirtualViewer3D and TBuffer3D in the master frame.
2 // Here each shape is described directly in a TBuffer3D
3 // class, with identity translation matrix c.f. viewer3DLocal.C
4 
5 // Our abstract base shape class.
6 // Author: Richard Maunder
7 
8 // As we overload TObject::Paint which is called directly from compiled
9 // code, this script must also be compiled to work correctly.
10 
11 #if defined(__CINT__) && !defined(__MAKECINT__)
12 {
13  gSystem->CompileMacro("viewer3DMaster.C");
15 }
16 #else
17 
18 #include "TVirtualViewer3D.h"
19 #include "TBuffer3D.h"
20 #include "TBuffer3DTypes.h"
21 
22 #include "TObject.h"
23 #include "TVirtualPad.h"
24 #include "TAtt3D.h"
25 
26 #include <vector>
27 
28 class Shape : public TObject
29 {
30 public:
31  Shape(Int_t color, Double_t x, Double_t y, Double_t z);
32  ~Shape() {};
33  virtual TBuffer3D & GetBuffer3D(UInt_t reqSections) = 0;
34 
35 protected:
36  Double_t fX, fY, fZ; // Origin
37  Int_t fColor;
38 
39  ClassDef(Shape,0);
40 };
41 
42 ClassImp(Shape);
43 
44 Shape::Shape(Int_t color, Double_t x, Double_t y, Double_t z) :
45  fX(x), fY(y), fZ(z), fColor(color)
46 {}
47 
48 class Box : public Shape
49 {
50 public:
51  Box(Int_t color, Double_t x, Double_t y, Double_t z,
52  Double_t dX, Double_t dY, Double_t dZ);
53  ~Box() {};
54 
55  virtual TBuffer3D & GetBuffer3D(UInt_t reqSections);
56 
57 private:
58  Double_t fDX, fDY, fDZ; // Half lengths
59 
60  ClassDef(Box,0);
61 };
62 
63 ClassImp(Box);
64 
65 Box::Box(Int_t color, Double_t x, Double_t y, Double_t z,
66  Double_t dX, Double_t dY, Double_t dZ) :
67  Shape(color,x,y,z),
68  fDX(dX), fDY(dY), fDZ(dZ)
69 {}
70 
71 TBuffer3D & Box::GetBuffer3D(UInt_t reqSections)
72 {
74 
75  // Complete kCore section - this could be moved to Shape base class
76  if (reqSections & TBuffer3D::kCore) {
77  buffer.ClearSectionsValid();
78  buffer.fID = this;
79  buffer.fColor = fColor; // Color index - see gROOT->GetColor()
80  buffer.fTransparency = 0; // Transparency 0 (opaque) - 100 (fully transparent)
81  buffer.fLocalFrame = kFALSE;
82  buffer.SetLocalMasterIdentity();
83  buffer.fReflection = kFALSE;
84  buffer.SetSectionsValid(TBuffer3D::kCore);
85  }
86  // Complete kBoundingBox section
87  if (reqSections & TBuffer3D::kBoundingBox) {
88  Double_t origin[3] = { fX, fY, fZ };
89  Double_t halfLength[3] = { fDX, fDY, fDZ };
90  buffer.SetAABoundingBox(origin, halfLength);
91  buffer.SetSectionsValid(TBuffer3D::kBoundingBox);
92  }
93  // No kShapeSpecific section
94 
95  // Complete kRawSizes section
96  if (reqSections & TBuffer3D::kRawSizes) {
97  buffer.SetRawSizes(8, 3*8, 12, 3*12, 6, 6*6);
98  buffer.SetSectionsValid(TBuffer3D::kRawSizes);
99  }
100  // Complete kRaw section
101  if (reqSections & TBuffer3D::kRaw) {
102  // Points (8)
103  // 3 components: x,y,z
104  buffer.fPnts[ 0] = fX - fDX; buffer.fPnts[ 1] = fY - fDY; buffer.fPnts[ 2] = fZ - fDZ; // 0
105  buffer.fPnts[ 3] = fX + fDX; buffer.fPnts[ 4] = fY - fDY; buffer.fPnts[ 5] = fZ - fDZ; // 1
106  buffer.fPnts[ 6] = fX + fDX; buffer.fPnts[ 7] = fY + fDY; buffer.fPnts[ 8] = fZ - fDZ; // 2
107  buffer.fPnts[ 9] = fX - fDX; buffer.fPnts[10] = fY + fDY; buffer.fPnts[11] = fZ - fDZ; // 3
108  buffer.fPnts[12] = fX - fDX; buffer.fPnts[13] = fY - fDY; buffer.fPnts[14] = fZ + fDZ; // 4
109  buffer.fPnts[15] = fX + fDX; buffer.fPnts[16] = fY - fDY; buffer.fPnts[17] = fZ + fDZ; // 5
110  buffer.fPnts[18] = fX + fDX; buffer.fPnts[19] = fY + fDY; buffer.fPnts[20] = fZ + fDZ; // 6
111  buffer.fPnts[21] = fX - fDX; buffer.fPnts[22] = fY + fDY; buffer.fPnts[23] = fZ + fDZ; // 7
112 
113  // Segments (12)
114  // 3 components: segment color(ignored), start point index, end point index
115  // Indexes reference the above points
116  buffer.fSegs[ 0] = fColor ; buffer.fSegs[ 1] = 0 ; buffer.fSegs[ 2] = 1 ; // 0
117  buffer.fSegs[ 3] = fColor ; buffer.fSegs[ 4] = 1 ; buffer.fSegs[ 5] = 2 ; // 1
118  buffer.fSegs[ 6] = fColor ; buffer.fSegs[ 7] = 2 ; buffer.fSegs[ 8] = 3 ; // 2
119  buffer.fSegs[ 9] = fColor ; buffer.fSegs[10] = 3 ; buffer.fSegs[11] = 0 ; // 3
120  buffer.fSegs[12] = fColor ; buffer.fSegs[13] = 4 ; buffer.fSegs[14] = 5 ; // 4
121  buffer.fSegs[15] = fColor ; buffer.fSegs[16] = 5 ; buffer.fSegs[17] = 6 ; // 5
122  buffer.fSegs[18] = fColor ; buffer.fSegs[19] = 6 ; buffer.fSegs[20] = 7 ; // 6
123  buffer.fSegs[21] = fColor ; buffer.fSegs[22] = 7 ; buffer.fSegs[23] = 4 ; // 7
124  buffer.fSegs[24] = fColor ; buffer.fSegs[25] = 0 ; buffer.fSegs[26] = 4 ; // 8
125  buffer.fSegs[27] = fColor ; buffer.fSegs[28] = 1 ; buffer.fSegs[29] = 5 ; // 9
126  buffer.fSegs[30] = fColor ; buffer.fSegs[31] = 2 ; buffer.fSegs[32] = 6 ; // 10
127  buffer.fSegs[33] = fColor ; buffer.fSegs[34] = 3 ; buffer.fSegs[35] = 7 ; // 11
128 
129  // Polygons (6)
130  // 5+ (2+n) components: polygon color (ignored), segment count(n=3+),
131  // seg1, seg2 .... segn index
132  // Segments indexes refer to the above 12 segments
133  // Here n=4 - each polygon defines a rectangle - 4 sides.
134  buffer.fPols[ 0] = fColor ; buffer.fPols[ 1] = 4 ; buffer.fPols[ 2] = 8 ; // 0
135  buffer.fPols[ 3] = 4 ; buffer.fPols[ 4] = 9 ; buffer.fPols[ 5] = 0 ;
136  buffer.fPols[ 6] = fColor ; buffer.fPols[ 7] = 4 ; buffer.fPols[ 8] = 9 ; // 1
137  buffer.fPols[ 9] = 5 ; buffer.fPols[10] = 10 ; buffer.fPols[11] = 1 ;
138  buffer.fPols[12] = fColor ; buffer.fPols[13] = 4 ; buffer.fPols[14] = 10 ; // 2
139  buffer.fPols[15] = 6 ; buffer.fPols[16] = 11 ; buffer.fPols[17] = 2 ;
140  buffer.fPols[18] = fColor ; buffer.fPols[19] = 4 ; buffer.fPols[20] = 11 ; // 3
141  buffer.fPols[21] = 7 ; buffer.fPols[22] = 8 ; buffer.fPols[23] = 3 ;
142  buffer.fPols[24] = fColor ; buffer.fPols[25] = 4 ; buffer.fPols[26] = 1 ; // 4
143  buffer.fPols[27] = 2 ; buffer.fPols[28] = 3 ; buffer.fPols[29] = 0 ;
144  buffer.fPols[30] = fColor ; buffer.fPols[31] = 4 ; buffer.fPols[32] = 7 ; // 5
145  buffer.fPols[33] = 6 ; buffer.fPols[34] = 5 ; buffer.fPols[35] = 4 ;
146 
147  buffer.SetSectionsValid(TBuffer3D::kRaw);
148  }
149 
150  return buffer;
151 }
152 
153 class SBPyramid : public Shape
154 {
155 public:
156  SBPyramid(Int_t color, Double_t d, Double_t y, Double_t z,
157  Double_t dX, Double_t dY, Double_t dZ);
158  ~SBPyramid() {};
159 
160  virtual TBuffer3D & GetBuffer3D(UInt_t reqSections);
161 
162 private:
163  Double_t fDX, fDY, fDZ; // Base half lengths dX,dY
164  // Pyr. height dZ
165 
166  ClassDef(SBPyramid,0);
167 };
168 
169 ClassImp(SBPyramid);
170 
171 SBPyramid::SBPyramid(Int_t color, Double_t x, Double_t y, Double_t z,
172  Double_t dX, Double_t dY, Double_t dZ) :
173  Shape(color,x,y,z),
174  fDX(dX), fDY(dY), fDZ(dZ)
175 {}
176 
177 TBuffer3D & SBPyramid::GetBuffer3D(UInt_t reqSections)
178 {
180 
181  // Complete kCore section - this could be moved to Shape base class
182  if (reqSections & TBuffer3D::kCore) {
183  buffer.ClearSectionsValid();
184  buffer.fID = this;
185  buffer.fColor = fColor; // Color index - see gROOT->GetColor()
186  buffer.fTransparency = 0; // Transparency 0 (opaque) - 100 (fully transparent)
187  buffer.fLocalFrame = kFALSE;
188  buffer.SetLocalMasterIdentity();
189  buffer.fReflection = kFALSE;
190  buffer.SetSectionsValid(TBuffer3D::kCore);
191  }
192  // Complete kBoundingBox section
193  if (reqSections & TBuffer3D::kBoundingBox) {
194  Double_t halfLength[3] = { fDX, fDY, fDZ/2.0 };
195  Double_t origin[3] = { fX , fY, fZ + halfLength[2]};
196  buffer.SetAABoundingBox(origin, halfLength);
197  buffer.SetSectionsValid(TBuffer3D::kBoundingBox);
198  }
199  // No kShapeSpecific section
200 
201  // Complete kRawSizes section
202  if (reqSections & TBuffer3D::kRawSizes) {
203  buffer.SetRawSizes(5, 3*5, 8, 3*8, 5, 6 + 4*5);
204  buffer.SetSectionsValid(TBuffer3D::kRawSizes);
205  }
206  // Complete kRaw section
207  if (reqSections & TBuffer3D::kRaw) {
208  // Points (5)
209  // 3 components: x,y,z
210  buffer.fPnts[ 0] = fX - fDX; buffer.fPnts[ 1] = fY - fDY; buffer.fPnts[ 2] = fZ; // 0
211  buffer.fPnts[ 3] = fX + fDX; buffer.fPnts[ 4] = fY - fDY; buffer.fPnts[ 5] = fZ; // 1
212  buffer.fPnts[ 6] = fX + fDX; buffer.fPnts[ 7] = fY + fDY; buffer.fPnts[ 8] = fZ; // 2
213  buffer.fPnts[ 9] = fX - fDX; buffer.fPnts[10] = fY + fDY; buffer.fPnts[11] = fZ; // 3
214  buffer.fPnts[12] = fX; buffer.fPnts[13] = fY ; buffer.fPnts[14] = fZ + fDZ; // 4 (pyr top point)
215 
216  // Segments (8)
217  // 3 components: segment color(ignored), start point index, end point index
218  // Indexes reference the above points
219 
220  buffer.fSegs[ 0] = fColor ; buffer.fSegs[ 1] = 0 ; buffer.fSegs[ 2] = 1 ; // 0 base
221  buffer.fSegs[ 3] = fColor ; buffer.fSegs[ 4] = 1 ; buffer.fSegs[ 5] = 2 ; // 1 base
222  buffer.fSegs[ 6] = fColor ; buffer.fSegs[ 7] = 2 ; buffer.fSegs[ 8] = 3 ; // 2 base
223  buffer.fSegs[ 9] = fColor ; buffer.fSegs[10] = 3 ; buffer.fSegs[11] = 0 ; // 3 base
224  buffer.fSegs[12] = fColor ; buffer.fSegs[13] = 0 ; buffer.fSegs[14] = 4 ; // 4 side
225  buffer.fSegs[15] = fColor ; buffer.fSegs[16] = 1 ; buffer.fSegs[17] = 4 ; // 5 side
226  buffer.fSegs[18] = fColor ; buffer.fSegs[19] = 2 ; buffer.fSegs[20] = 4 ; // 6 side
227  buffer.fSegs[21] = fColor ; buffer.fSegs[22] = 3 ; buffer.fSegs[23] = 4 ; // 7 side
228 
229  // Polygons (6)
230  // 5+ (2+n) components: polygon color (ignored), segment count(n=3+),
231  // seg1, seg2 .... segn index
232  // Segments indexes refer to the above 12 segments
233  // Here n=4 - each polygon defines a rectangle - 4 sides.
234  buffer.fPols[ 0] = fColor ; buffer.fPols[ 1] = 4 ; buffer.fPols[ 2] = 0 ; // base
235  buffer.fPols[ 3] = 1 ; buffer.fPols[ 4] = 2 ; buffer.fPols[ 5] = 3 ;
236 
237  buffer.fPols[ 6] = fColor ; buffer.fPols[ 7] = 3 ; buffer.fPols[ 8] = 0 ; // side 0
238  buffer.fPols[ 9] = 4 ; buffer.fPols[10] = 5 ;
239  buffer.fPols[11] = fColor ; buffer.fPols[12] = 3 ; buffer.fPols[13] = 1 ; // side 1
240  buffer.fPols[14] = 5 ; buffer.fPols[15] = 6 ;
241  buffer.fPols[16] = fColor ; buffer.fPols[17] = 3 ; buffer.fPols[18] = 2 ; // side 2
242  buffer.fPols[19] = 6 ; buffer.fPols[20] = 7 ;
243  buffer.fPols[21] = fColor ; buffer.fPols[22] = 3 ; buffer.fPols[23] = 3 ; // side 3
244  buffer.fPols[24] = 7 ; buffer.fPols[25] = 4 ;
245 
246  buffer.SetSectionsValid(TBuffer3D::kRaw);
247  }
248 
249  return buffer;
250 }
251 
252 class MyGeom : public TObject, public TAtt3D
253 {
254 public:
255  MyGeom();
256  ~MyGeom();
257 
258  void Draw(Option_t *option);
259  void Paint(Option_t *option);
260 
261 private:
262  std::vector<Shape *> fShapes;
263 
264  ClassDef(MyGeom,0);
265 };
266 
267 ClassImp(MyGeom);
268 
269 MyGeom::MyGeom()
270 {
271  // Create our simple geometry - couple of boxes
272  // and a square base pyramid
273  Shape * aShape;
274  aShape = new Box(kRed, 0.0, 0.0, 0.0, 20.0, 20.0, 20.0);
275  fShapes.push_back(aShape);
276  aShape = new Box(kBlue, 50.0, 100.0, 200.0, 5.0, 10.0, 15.0);
277  fShapes.push_back(aShape);
278  aShape = new SBPyramid(kGreen, 20.0, 25.0, 45.0, 30.0, 30.0, 90.0);
279  fShapes.push_back(aShape);
280 }
281 
282 MyGeom::~MyGeom()
283 {
284  // Clear out fShapes
285 }
286 
287 void MyGeom::Draw(Option_t *option)
288 {
289  TObject::Draw(option);
290 
291  // Ask pad to create 3D viewer of type 'option'
292  gPad->GetViewer3D(option);
293 }
294 
295 void MyGeom::Paint(Option_t * /*option*/)
296 {
297  TVirtualViewer3D * viewer = gPad->GetViewer3D();
298 
299  // If MyGeom derives from TAtt3D then pad will recognise
300  // that the object it is asking to paint is 3D, and open/close
301  // the scene for us. If not Open/Close are required
302  //viewer->BeginScene();
303 
304  // We are working in the master frame - so we don't bother
305  // to ask the viewer if it prefers local. Viewer's must
306  // always support master frame as minimum. c.f. with
307  // viewer3DLocal.C
308  std::vector<Shape *>::const_iterator ShapeIt = fShapes.begin();
309  Shape * shape;
310  while (ShapeIt != fShapes.end()) {
311  shape = *ShapeIt;
312 
313  UInt_t reqSections = TBuffer3D::kCore|TBuffer3D::kBoundingBox|TBuffer3D::kShapeSpecific;
314  TBuffer3D & buffer = shape->GetBuffer3D(reqSections);
315  reqSections = viewer->AddObject(buffer);
316 
317  if (reqSections != TBuffer3D::kNone) {
318  shape->GetBuffer3D(reqSections);
319  viewer->AddObject(buffer);
320  }
321  ShapeIt++;
322  }
323  // Not required as we are TAtt3D subclass
324  //viewer->EndScene();
325 }
326 
328 {
329  printf("\n\nviewer3DMaster: This frame demonstates master frame use of 3D viewer architecture.\n");
330  printf("Creates two boxes and a square based pyramid, described in master frame.\n\n");
331 
332  MyGeom * myGeom = new MyGeom;
333  myGeom->Draw("ogl");
334 }
335 
336 #endif
tuple buffer
Definition: tree.py:99
void viewer3DMaster()
const char Option_t
Definition: RtypesCore.h:62
Definition: Rtypes.h:61
Use this attribute class when an object should have 3D capabilities.
Definition: TAtt3D.h:29
void SetLocalMasterIdentity()
Set kRaw tessellation section of buffer with supplied sizes.
Definition: TBuffer3D.cxx:294
void ClearSectionsValid()
Clear any sections marked valid.
Definition: TBuffer3D.cxx:284
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:254
th1 Draw()
Definition: Rtypes.h:61
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
int d
Definition: tornado.py:11
Double_t * fPnts
Definition: TBuffer3D.h:114
virtual int CompileMacro(const char *filename, Option_t *opt="", const char *library_name="", const char *build_dir="", UInt_t dirmode=0)
This method compiles and loads a shared library containing the code from the file "filename"...
Definition: TSystem.cxx:2736
Abstract 3D shapes viewer.
Float_t z[5]
Definition: Ifit.C:16
void SetSectionsValid(UInt_t mask)
Definition: TBuffer3D.h:67
Int_t * fPols
Definition: TBuffer3D.h:116
Bool_t fLocalFrame
Definition: TBuffer3D.h:92
ClassImp(Shape)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
void SetAABoundingBox(const Double_t origin[3], const Double_t halfLengths[3])
Set fBBVertex in kBoundingBox section to a axis aligned (local) BB using supplied origin and box half...
Definition: TBuffer3D.cxx:318
virtual Int_t AddObject(const TBuffer3D &buffer, Bool_t *addChildren=0)=0
unsigned int UInt_t
Definition: RtypesCore.h:42
Bool_t SetRawSizes(UInt_t reqPnts, UInt_t reqPntsCapacity, UInt_t reqSegs, UInt_t reqSegsCapacity, UInt_t reqPols, UInt_t reqPolsCapacity)
Set kRaw tessellation section of buffer with supplied sizes.
Definition: TBuffer3D.cxx:357
Generic 3D primitive description class.
Definition: TBuffer3D.h:19
TObject * fID
Definition: TBuffer3D.h:89
virtual void Paint(Option_t *option="")
This method must be overridden if a class wants to paint itself.
Definition: TObject.cxx:563
Bool_t fReflection
Definition: TBuffer3D.h:93
double Double_t
Definition: RtypesCore.h:55
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Int_t fColor
Definition: TBuffer3D.h:90
Double_t y[n]
Definition: legend1.C:17
Mother of all ROOT objects.
Definition: TObject.h:58
Int_t * fSegs
Definition: TBuffer3D.h:115
#define gPad
Definition: TVirtualPad.h:288
Short_t fTransparency
Definition: TBuffer3D.h:91
Definition: Rtypes.h:61