TShape.cxx

Go to the documentation of this file.
00001 // @(#)root/g3d:$Id: TShape.cxx 31722 2009-12-09 10:55:00Z couet $
00002 // Author: Nenad Buncic   17/09/95
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TNode.h"
00013 #include "TShape.h"
00014 #include "TView.h"
00015 #include "TVirtualPad.h"
00016 #include "TGeometry.h"
00017 #include "TMaterial.h"
00018 #include "TBuffer3D.h"
00019 #include "TBuffer3DTypes.h"
00020 #include "TVirtualViewer3D.h"
00021 #include "TClass.h"
00022 #include "TMath.h"
00023 
00024 #include <assert.h>
00025 
00026 ClassImp(TShape)
00027 
00028 
00029 //______________________________________________________________________________
00030 //
00031 //  This is the base class for all geometry shapes.
00032 //  The list of shapes currently supported correspond to the shapes
00033 //  in Geant version 3:
00034 //     TBRIK,TCONE,TCONS,TGTRA,TPARA,TPCON,TPGON
00035 //    ,TTRAP,TTRD1,TTRD2,THYPE, TTUBE and TTUBS.
00036 //
00037 //  The figure below shows instances of all these shapes. This figure
00038 //  is generated by the ROOT 3-D viewer.
00039 //Begin_Html
00040 /*
00041 <img src="gif/tshape_classtree.gif">
00042 */
00043 //End_Html
00044 //Begin_Html
00045 /*
00046 <img src="gif/shapes.gif">
00047 */
00048 //End_Html
00049 //
00050 
00051 
00052 //______________________________________________________________________________
00053 TShape::TShape()
00054 {
00055    // Shape default constructor
00056 
00057    fVisibility = 1;
00058    fMaterial   = 0;
00059    fNumber     = 0;
00060 }
00061 
00062 
00063 //______________________________________________________________________________
00064 TShape::TShape(const char *name,const char *title, const char *materialname)
00065        : TNamed (name, title), TAttLine(), TAttFill()
00066 {
00067    // Shape normal constructor
00068 
00069    fVisibility = 1;
00070    if (!gGeometry) gGeometry = new TGeometry("Geometry","Default Geometry");
00071    fMaterial   = gGeometry->GetMaterial(materialname);
00072    fNumber     = gGeometry->GetListOfShapes()->GetSize();
00073    gGeometry->GetListOfShapes()->Add(this);
00074 #ifdef WIN32
00075    // The color "1" - default produces a very bad 3D image with OpenGL
00076    Color_t lcolor = 16;
00077    SetLineColor(lcolor);
00078 #endif
00079 }
00080 
00081 //______________________________________________________________________________
00082 TShape::TShape(const TShape& ts) : 
00083   TNamed(ts), 
00084   TAttLine(ts),
00085   TAttFill(ts),
00086   TAtt3D(ts),
00087   fNumber(ts.fNumber),
00088   fVisibility(ts.fVisibility),
00089   fMaterial(ts.fMaterial) 
00090 { 
00091    //copy constructor
00092 }
00093 
00094 //______________________________________________________________________________
00095 TShape& TShape::operator=(const TShape& ts) 
00096 { 
00097    //assignement operator
00098    if (this!=&ts) {
00099       TNamed::operator=(ts);
00100       TAttLine::operator=(ts);
00101       TAttFill::operator=(ts);
00102       TAtt3D::operator=(ts);
00103       fNumber=ts.fNumber;
00104       fVisibility=ts.fVisibility;
00105       fMaterial=ts.fMaterial;
00106    } 
00107    return *this;
00108 }
00109 
00110 //______________________________________________________________________________
00111 TShape::~TShape()
00112 {
00113    // Shape default destructor
00114 
00115    if (gGeometry) gGeometry->GetListOfShapes()->Remove(this);
00116 }
00117 
00118 
00119 //______________________________________________________________________________
00120 Int_t TShape::ShapeDistancetoPrimitive(Int_t numPoints, Int_t px, Int_t py)
00121 {
00122    // Distance to primitive.
00123 
00124    Int_t dist = 9999;
00125 
00126    TView *view = gPad->GetView();
00127    if (!(numPoints && view)) return dist;
00128 
00129    Double_t *points =  new Double_t[3*numPoints];
00130    SetPoints(points);
00131    Double_t dpoint2, x1, y1, xndc[3];
00132    for (Int_t i = 0; i < numPoints; i++) {
00133       if (gGeometry) gGeometry->Local2Master(&points[3*i],&points[3*i]);
00134       view->WCtoNDC(&points[3*i], xndc);
00135       x1     = gPad->XtoAbsPixel(xndc[0]);
00136       y1     = gPad->YtoAbsPixel(xndc[1]);
00137       dpoint2= (px-x1)*(px-x1) + (py-y1)*(py-y1);
00138       if (dpoint2 < dist) dist = (Int_t)dpoint2;
00139    }
00140    delete [] points;
00141    return Int_t(TMath::Sqrt(Float_t(dist)));
00142 }
00143 
00144 
00145 //______________________________________________________________________________
00146 void TShape::Paint(Option_t *)
00147 {
00148    // This method is used only when a shape is painted outside a TNode.
00149 
00150    TVirtualViewer3D * viewer3D = gPad->GetViewer3D();
00151    if (viewer3D) {
00152       const TBuffer3D & buffer = GetBuffer3D(TBuffer3D::kAll);
00153       viewer3D->AddObject(buffer);
00154    }
00155 }
00156 
00157 //______________________________________________________________________________
00158 void TShape::SetPoints(Double_t *) const 
00159 {
00160    // Set points.
00161 
00162    AbstractMethod("SetPoints(Double_t *buffer) const");
00163 }
00164 
00165 
00166 //______________________________________________________________________________
00167 void TShape::Streamer(TBuffer &R__b)
00168 {
00169    // Stream an object of class TShape.
00170 
00171    if (R__b.IsReading()) {
00172       UInt_t R__s, R__c;
00173       Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
00174       if (R__v > 1) {
00175          R__b.ReadClassBuffer(TShape::Class(), this, R__v, R__s, R__c);
00176          return;
00177       }
00178       //====process old versions before automatic schema evolution
00179       TNamed::Streamer(R__b);
00180       TAttLine::Streamer(R__b);
00181       TAttFill::Streamer(R__b);
00182       TAtt3D::Streamer(R__b);
00183       R__b >> fNumber;
00184       R__b >> fVisibility;
00185       R__b >> fMaterial;
00186       R__b.CheckByteCount(R__s, R__c, TShape::IsA());
00187       //====end of old versions
00188       
00189    } else {
00190       R__b.WriteClassBuffer(TShape::Class(),this);
00191    }
00192 }
00193 
00194 
00195 //______________________________________________________________________________
00196 void TShape::TransformPoints(Double_t *points, UInt_t NbPnts) const
00197 {
00198    // Tranform points (LocalToMaster)
00199 
00200    if (gGeometry && points) {
00201       Double_t dlocal[3];
00202       Double_t dmaster[3];
00203       for (UInt_t j=0; j<NbPnts; j++) {
00204          dlocal[0] = points[3*j];
00205          dlocal[1] = points[3*j+1];
00206          dlocal[2] = points[3*j+2];
00207          gGeometry->Local2Master(&dlocal[0],&dmaster[0]);
00208          points[3*j]   = dmaster[0];
00209          points[3*j+1] = dmaster[1];
00210          points[3*j+2] = dmaster[2];
00211       }
00212    }
00213 }
00214 
00215 
00216 //______________________________________________________________________________
00217 void TShape::FillBuffer3D(TBuffer3D & buffer, Int_t reqSections) const
00218 {
00219    // We have to set kRawSize (unless already done) to allocate buffer space 
00220    // before kRaw can be filled
00221 
00222    if (reqSections & TBuffer3D::kRaw)
00223    {
00224       if (!(reqSections & TBuffer3D::kRawSizes) && !buffer.SectionsValid(TBuffer3D::kRawSizes))
00225       {
00226          assert(kFALSE);
00227       }
00228    }
00229 
00230    if (reqSections & TBuffer3D::kCore) {
00231       buffer.ClearSectionsValid();
00232 
00233       // We are only filling TBuffer3D in the master frame. Therefore the shape 
00234       // described in buffer is a specific placement - and this needs to be
00235       // identified uniquely. Use the current node set in TNode::Paint which calls us
00236       buffer.fID = gNode;
00237       buffer.fColor = GetLineColor();
00238       buffer.fTransparency = 0;    
00239       buffer.fLocalFrame = kFALSE; // Only support master frame for these shapes
00240       buffer.fReflection = kFALSE;
00241 
00242       buffer.SetLocalMasterIdentity();
00243       buffer.SetSectionsValid(TBuffer3D::kCore);
00244    }
00245 }
00246 
00247 
00248 //______________________________________________________________________________
00249 Int_t TShape::GetBasicColor() const
00250 {
00251    // Get basic solor.
00252 
00253    Int_t basicColor = ((GetLineColor() %8) -1) * 4;
00254    if (basicColor < 0) basicColor = 0;
00255 
00256    return basicColor;
00257 }
00258 
00259 
00260 //______________________________________________________________________________
00261 const TBuffer3D &TShape::GetBuffer3D(Int_t /* reqSections */ ) const
00262 {
00263    // Stub to avoid forcing implementation at this stage
00264 
00265    static TBuffer3D buffer(TBuffer3DTypes::kGeneric);
00266    Warning("GetBuffer3D", "this must be implemented for shapes in a TNode::Paint hierarchy. This will become a pure virtual fn eventually.");
00267    return buffer;
00268 }

Generated on Tue Jul 5 14:16:41 2011 for ROOT_528-00b_version by  doxygen 1.5.1