viewer3DMaster.C

Go to the documentation of this file.
00001 // Demonstrates 3D viewer architecture TVirtualViewer3D and TBuffer3D in the master frame.
00002 // Here each shape is described directly in a TBuffer3D
00003 // class, with identity translation matrix c.f. viewer3DLocal.C
00004 
00005 // Our abstract base shape class.
00006 // Author: Richard Maunder
00007 
00008 // As we overload TObject::Paint which is called directly from compiled
00009 // code, this script must also be compiled to work correctly.
00010 
00011 #if defined(__CINT__) && !defined(__MAKECINT__)
00012 {
00013    gSystem->CompileMacro("viewer3DMaster.C");
00014    viewer3DMaster();
00015 }
00016 #else
00017 
00018 #include "TVirtualViewer3D.h"
00019 #include "TBuffer3D.h"
00020 #include "TBuffer3DTypes.h"
00021 
00022 #include "TObject.h"
00023 #include "TVirtualPad.h"
00024 #include "TAtt3D.h"
00025 
00026 #include <vector>
00027 
00028 class Shape : public TObject
00029 {
00030 public:
00031    Shape(Int_t color, Double_t x, Double_t y, Double_t z);
00032    ~Shape() {};
00033    virtual TBuffer3D & GetBuffer3D(UInt_t reqSections) = 0;
00034 
00035 protected:
00036    Double_t fX, fY, fZ;    // Origin
00037    Int_t fColor;
00038 
00039    ClassDef(Shape,0);
00040 };
00041 
00042 ClassImp(Shape); 
00043 
00044 Shape::Shape(Int_t color, Double_t x, Double_t y, Double_t z) : 
00045    fX(x), fY(y), fZ(z), fColor(color) 
00046 {}
00047 
00048 class Box : public Shape
00049 {
00050 public:
00051    Box(Int_t color, Double_t x, Double_t y, Double_t z,
00052        Double_t dX, Double_t dY, Double_t dZ);
00053    ~Box() {};
00054 
00055    virtual TBuffer3D & GetBuffer3D(UInt_t reqSections);
00056 
00057 private:
00058    Double_t fDX, fDY, fDZ; // Half lengths
00059 
00060    ClassDef(Box,0);
00061 };
00062 
00063 ClassImp(Box); 
00064 
00065 Box::Box(Int_t color, Double_t x, Double_t y, Double_t z,
00066          Double_t dX, Double_t dY, Double_t dZ) : 
00067    Shape(color,x,y,z), 
00068    fDX(dX), fDY(dY), fDZ(dZ)
00069 {}
00070 
00071 TBuffer3D & Box::GetBuffer3D(UInt_t reqSections)
00072 {
00073    static TBuffer3D buffer(TBuffer3DTypes::kGeneric);
00074 
00075    // Complete kCore section - this could be moved to Shape base class
00076    if (reqSections & TBuffer3D::kCore) {      
00077       buffer.ClearSectionsValid();
00078       buffer.fID = this; 
00079       buffer.fColor = fColor;       // Color index - see gROOT->GetColor()
00080       buffer.fTransparency = 0;     // Transparency 0 (opaque) - 100 (fully transparent)
00081       buffer.fLocalFrame = kFALSE;
00082       buffer.SetLocalMasterIdentity();
00083       buffer.fReflection = kFALSE;
00084       buffer.SetSectionsValid(TBuffer3D::kCore);
00085    }
00086    // Complete kBoundingBox section
00087    if (reqSections & TBuffer3D::kBoundingBox) {
00088       Double_t origin[3] = { fX, fY, fZ };
00089       Double_t halfLength[3] =  { fDX, fDY, fDZ };
00090       buffer.SetAABoundingBox(origin, halfLength);
00091       buffer.SetSectionsValid(TBuffer3D::kBoundingBox);
00092    }
00093    // No kShapeSpecific section
00094 
00095    // Complete kRawSizes section
00096    if (reqSections & TBuffer3D::kRawSizes) {
00097       buffer.SetRawSizes(8, 3*8, 12, 3*12, 6, 6*6);
00098       buffer.SetSectionsValid(TBuffer3D::kRawSizes);
00099    }
00100    // Complete kRaw section
00101    if (reqSections & TBuffer3D::kRaw) {
00102       // Points (8)
00103       // 3 components: x,y,z
00104       buffer.fPnts[ 0] = fX - fDX; buffer.fPnts[ 1] = fY - fDY; buffer.fPnts[ 2] = fZ - fDZ; // 0
00105       buffer.fPnts[ 3] = fX + fDX; buffer.fPnts[ 4] = fY - fDY; buffer.fPnts[ 5] = fZ - fDZ; // 1
00106       buffer.fPnts[ 6] = fX + fDX; buffer.fPnts[ 7] = fY + fDY; buffer.fPnts[ 8] = fZ - fDZ; // 2
00107       buffer.fPnts[ 9] = fX - fDX; buffer.fPnts[10] = fY + fDY; buffer.fPnts[11] = fZ - fDZ; // 3
00108       buffer.fPnts[12] = fX - fDX; buffer.fPnts[13] = fY - fDY; buffer.fPnts[14] = fZ + fDZ; // 4
00109       buffer.fPnts[15] = fX + fDX; buffer.fPnts[16] = fY - fDY; buffer.fPnts[17] = fZ + fDZ; // 5
00110       buffer.fPnts[18] = fX + fDX; buffer.fPnts[19] = fY + fDY; buffer.fPnts[20] = fZ + fDZ; // 6
00111       buffer.fPnts[21] = fX - fDX; buffer.fPnts[22] = fY + fDY; buffer.fPnts[23] = fZ + fDZ; // 7
00112 
00113       // Segments (12)
00114       // 3 components: segment color(ignored), start point index, end point index
00115       // Indexes reference the above points
00116       buffer.fSegs[ 0] = fColor   ; buffer.fSegs[ 1] = 0   ; buffer.fSegs[ 2] = 1   ; // 0
00117       buffer.fSegs[ 3] = fColor   ; buffer.fSegs[ 4] = 1   ; buffer.fSegs[ 5] = 2   ; // 1
00118       buffer.fSegs[ 6] = fColor   ; buffer.fSegs[ 7] = 2   ; buffer.fSegs[ 8] = 3   ; // 2
00119       buffer.fSegs[ 9] = fColor   ; buffer.fSegs[10] = 3   ; buffer.fSegs[11] = 0   ; // 3
00120       buffer.fSegs[12] = fColor   ; buffer.fSegs[13] = 4   ; buffer.fSegs[14] = 5   ; // 4
00121       buffer.fSegs[15] = fColor   ; buffer.fSegs[16] = 5   ; buffer.fSegs[17] = 6   ; // 5
00122       buffer.fSegs[18] = fColor   ; buffer.fSegs[19] = 6   ; buffer.fSegs[20] = 7   ; // 6
00123       buffer.fSegs[21] = fColor   ; buffer.fSegs[22] = 7   ; buffer.fSegs[23] = 4   ; // 7
00124       buffer.fSegs[24] = fColor   ; buffer.fSegs[25] = 0   ; buffer.fSegs[26] = 4   ; // 8
00125       buffer.fSegs[27] = fColor   ; buffer.fSegs[28] = 1   ; buffer.fSegs[29] = 5   ; // 9
00126       buffer.fSegs[30] = fColor   ; buffer.fSegs[31] = 2   ; buffer.fSegs[32] = 6   ; // 10
00127       buffer.fSegs[33] = fColor   ; buffer.fSegs[34] = 3   ; buffer.fSegs[35] = 7   ; // 11
00128       
00129       // Polygons (6)
00130       // 5+ (2+n) components: polygon color (ignored), segment count(n=3+),
00131       // seg1, seg2 .... segn index
00132       // Segments indexes refer to the above 12 segments
00133       // Here n=4 - each polygon defines a rectangle - 4 sides.
00134       buffer.fPols[ 0] = fColor   ; buffer.fPols[ 1] = 4   ;  buffer.fPols[ 2] = 8  ; // 0
00135       buffer.fPols[ 3] = 4        ; buffer.fPols[ 4] = 9   ;  buffer.fPols[ 5] = 0  ;
00136       buffer.fPols[ 6] = fColor   ; buffer.fPols[ 7] = 4   ;  buffer.fPols[ 8] = 9  ; // 1
00137       buffer.fPols[ 9] = 5        ; buffer.fPols[10] = 10  ;  buffer.fPols[11] = 1  ;
00138       buffer.fPols[12] = fColor   ; buffer.fPols[13] = 4   ;  buffer.fPols[14] = 10  ; // 2
00139       buffer.fPols[15] = 6        ; buffer.fPols[16] = 11  ;  buffer.fPols[17] = 2  ;
00140       buffer.fPols[18] = fColor   ; buffer.fPols[19] = 4   ;  buffer.fPols[20] = 11 ; // 3
00141       buffer.fPols[21] = 7        ; buffer.fPols[22] = 8   ;  buffer.fPols[23] = 3 ;
00142       buffer.fPols[24] = fColor   ; buffer.fPols[25] = 4   ;  buffer.fPols[26] = 1  ; // 4
00143       buffer.fPols[27] = 2        ; buffer.fPols[28] = 3   ;  buffer.fPols[29] = 0  ;
00144       buffer.fPols[30] = fColor   ; buffer.fPols[31] = 4   ;  buffer.fPols[32] = 7  ; // 5
00145       buffer.fPols[33] = 6        ; buffer.fPols[34] = 5   ;  buffer.fPols[35] = 4  ;
00146       
00147       buffer.SetSectionsValid(TBuffer3D::kRaw);
00148   }
00149 
00150    return buffer;
00151 }
00152 
00153 class SBPyramid : public Shape
00154 {
00155 public:
00156    SBPyramid(Int_t color, Double_t d, Double_t y, Double_t z,
00157              Double_t dX, Double_t dY, Double_t dZ);
00158    ~SBPyramid() {};
00159 
00160    virtual TBuffer3D & GetBuffer3D(UInt_t reqSections);
00161 
00162 private:
00163    Double_t fDX, fDY, fDZ; // Base half lengths dX,dY
00164                            // Pyr. height dZ
00165 
00166    ClassDef(SBPyramid,0);
00167 };
00168 
00169 ClassImp(SBPyramid); 
00170 
00171 SBPyramid::SBPyramid(Int_t color, Double_t x, Double_t y, Double_t z,
00172          Double_t dX, Double_t dY, Double_t dZ) : 
00173    Shape(color,x,y,z), 
00174    fDX(dX), fDY(dY), fDZ(dZ)
00175 {}
00176 
00177 TBuffer3D & SBPyramid::GetBuffer3D(UInt_t reqSections)
00178 {
00179    static TBuffer3D buffer(TBuffer3DTypes::kGeneric);
00180 
00181    // Complete kCore section - this could be moved to Shape base class
00182    if (reqSections & TBuffer3D::kCore) {      
00183       buffer.ClearSectionsValid();
00184       buffer.fID = this; 
00185       buffer.fColor = fColor;       // Color index - see gROOT->GetColor()
00186       buffer.fTransparency = 0;     // Transparency 0 (opaque) - 100 (fully transparent)
00187       buffer.fLocalFrame = kFALSE;
00188       buffer.SetLocalMasterIdentity();
00189       buffer.fReflection = kFALSE;
00190       buffer.SetSectionsValid(TBuffer3D::kCore);
00191    }
00192    // Complete kBoundingBox section
00193    if (reqSections & TBuffer3D::kBoundingBox) {
00194       Double_t halfLength[3] =  { fDX, fDY, fDZ/2.0 };
00195       Double_t origin[3] = { fX , fY, fZ + halfLength[2]};
00196       buffer.SetAABoundingBox(origin, halfLength);
00197       buffer.SetSectionsValid(TBuffer3D::kBoundingBox);
00198    }
00199    // No kShapeSpecific section
00200 
00201    // Complete kRawSizes section
00202    if (reqSections & TBuffer3D::kRawSizes) {
00203       buffer.SetRawSizes(5, 3*5, 8, 3*8, 5, 6 + 4*5);
00204       buffer.SetSectionsValid(TBuffer3D::kRawSizes);
00205    }
00206    // Complete kRaw section
00207    if (reqSections & TBuffer3D::kRaw) {
00208       // Points (5)
00209       // 3 components: x,y,z
00210       buffer.fPnts[ 0] = fX - fDX; buffer.fPnts[ 1] = fY - fDY; buffer.fPnts[ 2] = fZ; // 0
00211       buffer.fPnts[ 3] = fX + fDX; buffer.fPnts[ 4] = fY - fDY; buffer.fPnts[ 5] = fZ; // 1
00212       buffer.fPnts[ 6] = fX + fDX; buffer.fPnts[ 7] = fY + fDY; buffer.fPnts[ 8] = fZ; // 2
00213       buffer.fPnts[ 9] = fX - fDX; buffer.fPnts[10] = fY + fDY; buffer.fPnts[11] = fZ; // 3
00214       buffer.fPnts[12] = fX;       buffer.fPnts[13] = fY      ; buffer.fPnts[14] = fZ + fDZ; // 4 (pyr top point)
00215 
00216       // Segments (8)
00217       // 3 components: segment color(ignored), start point index, end point index
00218       // Indexes reference the above points
00219 
00220       buffer.fSegs[ 0] = fColor   ; buffer.fSegs[ 1] = 0   ; buffer.fSegs[ 2] = 1   ; // 0 base
00221       buffer.fSegs[ 3] = fColor   ; buffer.fSegs[ 4] = 1   ; buffer.fSegs[ 5] = 2   ; // 1 base
00222       buffer.fSegs[ 6] = fColor   ; buffer.fSegs[ 7] = 2   ; buffer.fSegs[ 8] = 3   ; // 2 base
00223       buffer.fSegs[ 9] = fColor   ; buffer.fSegs[10] = 3   ; buffer.fSegs[11] = 0   ; // 3 base
00224       buffer.fSegs[12] = fColor   ; buffer.fSegs[13] = 0   ; buffer.fSegs[14] = 4   ; // 4 side
00225       buffer.fSegs[15] = fColor   ; buffer.fSegs[16] = 1   ; buffer.fSegs[17] = 4   ; // 5 side
00226       buffer.fSegs[18] = fColor   ; buffer.fSegs[19] = 2   ; buffer.fSegs[20] = 4   ; // 6 side
00227       buffer.fSegs[21] = fColor   ; buffer.fSegs[22] = 3   ; buffer.fSegs[23] = 4   ; // 7 side
00228       
00229       // Polygons (6)
00230       // 5+ (2+n) components: polygon color (ignored), segment count(n=3+),
00231       // seg1, seg2 .... segn index
00232       // Segments indexes refer to the above 12 segments
00233       // Here n=4 - each polygon defines a rectangle - 4 sides.
00234       buffer.fPols[ 0] = fColor  ; buffer.fPols[ 1] = 4   ;  buffer.fPols[ 2] = 0  ; // base
00235       buffer.fPols[ 3] = 1       ; buffer.fPols[ 4] = 2   ;  buffer.fPols[ 5] = 3  ;
00236 
00237       buffer.fPols[ 6] = fColor  ; buffer.fPols[ 7] = 3   ;  buffer.fPols[ 8] = 0  ; // side 0
00238       buffer.fPols[ 9] = 4       ; buffer.fPols[10] = 5   ;
00239       buffer.fPols[11] = fColor  ; buffer.fPols[12] = 3   ;  buffer.fPols[13] = 1  ; // side 1
00240       buffer.fPols[14] = 5       ; buffer.fPols[15] = 6   ;
00241       buffer.fPols[16] = fColor  ; buffer.fPols[17] = 3   ;  buffer.fPols[18] = 2  ; // side 2
00242       buffer.fPols[19] = 6       ; buffer.fPols[20] = 7   ;   
00243       buffer.fPols[21] = fColor  ; buffer.fPols[22] = 3   ;  buffer.fPols[23] = 3  ; // side 3
00244       buffer.fPols[24] = 7       ; buffer.fPols[25] = 4   ;
00245       
00246       buffer.SetSectionsValid(TBuffer3D::kRaw);
00247   }
00248 
00249    return buffer;
00250 }
00251 
00252 class MyGeom : public TObject, public TAtt3D
00253 {
00254 public:
00255    MyGeom();
00256    ~MyGeom();
00257 
00258    void Draw(Option_t *option);
00259    void Paint(Option_t *option);
00260 
00261 private:
00262    std::vector<Shape *> fShapes;
00263 
00264    ClassDef(MyGeom,0);
00265 };
00266 
00267 ClassImp(MyGeom); 
00268 
00269 MyGeom::MyGeom()
00270 {
00271    // Create our simple geometry - couple of boxes
00272    // and a square base pyramid
00273    Shape * aShape;
00274    aShape = new Box(kRed, 0.0, 0.0, 0.0, 20.0, 20.0, 20.0);
00275    fShapes.push_back(aShape);
00276    aShape = new Box(kBlue, 50.0, 100.0, 200.0, 5.0, 10.0, 15.0);
00277    fShapes.push_back(aShape);
00278    aShape = new SBPyramid(kGreen, 20.0, 25.0, 45.0, 30.0, 30.0, 90.0);
00279    fShapes.push_back(aShape);
00280 }
00281 
00282 MyGeom::~MyGeom()
00283 {
00284    // Clear out fShapes
00285 }
00286 
00287 void MyGeom::Draw(Option_t *option)
00288 {
00289    TObject::Draw(option);
00290 
00291    // Ask pad to create 3D viewer of type 'option'
00292    gPad->GetViewer3D(option);
00293 }
00294 
00295 void MyGeom::Paint(Option_t * /*option*/)
00296 {
00297    TVirtualViewer3D * viewer = gPad->GetViewer3D();
00298 
00299    // If MyGeom derives from TAtt3D then pad will recognise
00300    // that the object it is asking to paint is 3D, and open/close
00301    // the scene for us. If not Open/Close are required
00302    //viewer->BeginScene();
00303 
00304    // We are working in the master frame - so we don't bother
00305    // to ask the viewer if it prefers local. Viewer's must
00306    // always support master frame as minimum. c.f. with
00307    // viewer3DLocal.C
00308    std::vector<Shape *>::const_iterator ShapeIt = fShapes.begin();
00309    Shape * shape;
00310    while (ShapeIt != fShapes.end()) {
00311       shape = *ShapeIt;
00312 
00313       UInt_t reqSections = TBuffer3D::kCore|TBuffer3D::kBoundingBox|TBuffer3D::kShapeSpecific;
00314       TBuffer3D & buffer = shape->GetBuffer3D(reqSections);
00315       reqSections = viewer->AddObject(buffer);
00316 
00317       if (reqSections != TBuffer3D::kNone) {
00318          shape->GetBuffer3D(reqSections);
00319          viewer->AddObject(buffer);
00320       }
00321       ShapeIt++;
00322    }
00323    // Not required as we are TAtt3D subclass
00324    //viewer->EndScene();
00325 }
00326 
00327 void viewer3DMaster()
00328 {
00329    printf("\n\nviewer3DMaster: This frame demonstates master frame use of 3D viewer architecture.\n");
00330    printf("Creates two boxes and a square based pyramid, described in master frame.\n\n");
00331 
00332    MyGeom * myGeom = new MyGeom;
00333    myGeom->Draw("ogl");
00334 }
00335 
00336 #endif

Generated on Tue Jul 5 15:44:20 2011 for ROOT_528-00b_version by  doxygen 1.5.1