ROOT  6.06/09
Reference Guide
TShape.cxx
Go to the documentation of this file.
1 // @(#)root/g3d:$Id$
2 // Author: Nenad Buncic 17/09/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TNode.h"
13 #include "TShape.h"
14 #include "TView.h"
15 #include "TVirtualPad.h"
16 #include "TGeometry.h"
17 #include "TMaterial.h"
18 #include "TBuffer3D.h"
19 #include "TBuffer3DTypes.h"
20 #include "TVirtualViewer3D.h"
21 #include "TClass.h"
22 #include "TMath.h"
23 
24 #include <assert.h>
25 
27 
28 /** \class TShape
29 \ingroup g3d
30 
31 This is the base class for all geometry shapes.
32 /The list of shapes currently supported correspond to the shapes
33 in Geant version 3:
34 
35 ~~~ {.cpp}
36  TBRIK,TCONE,TCONS,TGTRA,TPARA,TPCON,TPGON
37  TTRAP,TTRD1,TTRD2,THYPE, TTUBE and TTUBS.
38 ~~~
39 
40 The figure below shows instances of all these shapes. This figure
41 is generated by the ROOT 3-D viewer.
42 
43 \image html g3d_tshape_classtree.png
44 \image html g3d_shapes.png
45 */
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 /// Shape default constructor
49 
50 TShape::TShape()
51 {
52  fVisibility = 1;
53  fMaterial = 0;
54  fNumber = 0;
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Shape normal constructor
59 
60 TShape::TShape(const char *name,const char *title, const char *materialname)
61  : TNamed (name, title), TAttLine(), TAttFill()
62 {
63  fVisibility = 1;
64  if (!gGeometry) gGeometry = new TGeometry("Geometry","Default Geometry");
65  fMaterial = gGeometry->GetMaterial(materialname);
66  fNumber = gGeometry->GetListOfShapes()->GetSize();
67  gGeometry->GetListOfShapes()->Add(this);
68 #ifdef WIN32
69  // The color "1" - default produces a very bad 3D image with OpenGL
70  Color_t lcolor = 16;
71  SetLineColor(lcolor);
72 #endif
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// copy constructor
77 
78 TShape::TShape(const TShape& ts) :
79  TNamed(ts),
80  TAttLine(ts),
81  TAttFill(ts),
82  TAtt3D(ts),
83  fNumber(ts.fNumber),
84  fVisibility(ts.fVisibility),
85  fMaterial(ts.fMaterial)
86 {
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// assignment operator
91 
93 {
94  if (this!=&ts) {
99  fNumber=ts.fNumber;
100  fVisibility=ts.fVisibility;
101  fMaterial=ts.fMaterial;
102  }
103  return *this;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Shape default destructor
108 
110 {
111  if (gGeometry) gGeometry->GetListOfShapes()->Remove(this);
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// Distance to primitive.
116 
118 {
119  Int_t dist = 9999;
120 
121  TView *view = gPad->GetView();
122  if (!(numPoints && view)) return dist;
123 
124  Double_t *points = new Double_t[3*numPoints];
125  SetPoints(points);
126  Double_t dpoint2, x1, y1, xndc[3];
127  for (Int_t i = 0; i < numPoints; i++) {
128  if (gGeometry) gGeometry->Local2Master(&points[3*i],&points[3*i]);
129  view->WCtoNDC(&points[3*i], xndc);
130  x1 = gPad->XtoAbsPixel(xndc[0]);
131  y1 = gPad->YtoAbsPixel(xndc[1]);
132  dpoint2= (px-x1)*(px-x1) + (py-y1)*(py-y1);
133  if (dpoint2 < dist) dist = (Int_t)dpoint2;
134  }
135  delete [] points;
136  return Int_t(TMath::Sqrt(Float_t(dist)));
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// This method is used only when a shape is painted outside a TNode.
141 
143 {
144  TVirtualViewer3D * viewer3D = gPad->GetViewer3D();
145  if (viewer3D) {
146  const TBuffer3D & buffer = GetBuffer3D(TBuffer3D::kAll);
147  viewer3D->AddObject(buffer);
148  }
149 }
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// Set points.
153 
155 {
156  AbstractMethod("SetPoints(Double_t *buffer) const");
157 }
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Stream an object of class TShape.
161 
162 void TShape::Streamer(TBuffer &R__b)
163 {
164  if (R__b.IsReading()) {
165  UInt_t R__s, R__c;
166  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
167  if (R__v > 1) {
168  R__b.ReadClassBuffer(TShape::Class(), this, R__v, R__s, R__c);
169  return;
170  }
171  //====process old versions before automatic schema evolution
172  TNamed::Streamer(R__b);
173  TAttLine::Streamer(R__b);
174  TAttFill::Streamer(R__b);
175  TAtt3D::Streamer(R__b);
176  R__b >> fNumber;
177  R__b >> fVisibility;
178  R__b >> fMaterial;
179  R__b.CheckByteCount(R__s, R__c, TShape::IsA());
180  //====end of old versions
181 
182  } else {
183  R__b.WriteClassBuffer(TShape::Class(),this);
184  }
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// Transform points (LocalToMaster)
189 
191 {
192  if (gGeometry && points) {
193  Double_t dlocal[3];
194  Double_t dmaster[3];
195  for (UInt_t j=0; j<NbPnts; j++) {
196  dlocal[0] = points[3*j];
197  dlocal[1] = points[3*j+1];
198  dlocal[2] = points[3*j+2];
199  gGeometry->Local2Master(&dlocal[0],&dmaster[0]);
200  points[3*j] = dmaster[0];
201  points[3*j+1] = dmaster[1];
202  points[3*j+2] = dmaster[2];
203  }
204  }
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// We have to set kRawSize (unless already done) to allocate buffer space
209 /// before kRaw can be filled
210 
211 void TShape::FillBuffer3D(TBuffer3D & buffer, Int_t reqSections) const
212 {
213  if (reqSections & TBuffer3D::kRaw)
214  {
215  if (!(reqSections & TBuffer3D::kRawSizes) && !buffer.SectionsValid(TBuffer3D::kRawSizes))
216  {
217  assert(kFALSE);
218  }
219  }
220 
221  if (reqSections & TBuffer3D::kCore) {
222  buffer.ClearSectionsValid();
223 
224  // We are only filling TBuffer3D in the master frame. Therefore the shape
225  // described in buffer is a specific placement - and this needs to be
226  // identified uniquely. Use the current node set in TNode::Paint which calls us
227  buffer.fID = gNode;
228  buffer.fColor = GetLineColor();
229  buffer.fTransparency = 0;
230  buffer.fLocalFrame = kFALSE; // Only support master frame for these shapes
231  buffer.fReflection = kFALSE;
232 
233  buffer.SetLocalMasterIdentity();
234  buffer.SetSectionsValid(TBuffer3D::kCore);
235  }
236 }
237 
238 ////////////////////////////////////////////////////////////////////////////////
239 /// Get basic color.
240 
242 {
243  Int_t basicColor = ((GetLineColor() %8) -1) * 4;
244  if (basicColor < 0) basicColor = 0;
245 
246  return basicColor;
247 }
248 
249 ////////////////////////////////////////////////////////////////////////////////
250 /// Stub to avoid forcing implementation at this stage
251 
252 const TBuffer3D &TShape::GetBuffer3D(Int_t /* reqSections */ ) const
253 {
254  static TBuffer3D buffer(TBuffer3DTypes::kGeneric);
255  Warning("GetBuffer3D", "this must be implemented for shapes in a TNode::Paint hierarchy. This will become a pure virtual fn eventually.");
256  return buffer;
257 }
virtual ~TShape()
Shape default destructor.
Definition: TShape.cxx:109
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
Bool_t IsReading() const
Definition: TBuffer.h:81
short Version_t
Definition: RtypesCore.h:61
virtual void WCtoNDC(const Float_t *pw, Float_t *pn)=0
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
THashList * GetListOfShapes() const
Definition: TGeometry.h:79
#define assert(cond)
Definition: unittest.h:542
See TView3D.
Definition: TView.h:36
Use this attribute class when an object should have 3D capabilities.
Definition: TAtt3D.h:29
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
void SetLocalMasterIdentity()
Set kRaw tessellation section of buffer with supplied sizes.
Definition: TBuffer3D.cxx:294
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
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 FillBuffer3D(TBuffer3D &buffer, Int_t reqSections) const
We have to set kRawSize (unless already done) to allocate buffer space before kRaw can be filled...
Definition: TShape.cxx:211
Int_t fNumber
Definition: TShape.h:50
Fill Area Attributes class.
Definition: TAttFill.h:32
void Class()
Definition: Class.C:29
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
virtual void Local2Master(Double_t *local, Double_t *master)
Convert one point from local system to master reference system.
Definition: TGeometry.cxx:407
virtual void SetPoints(Double_t *points) const
Set points.
Definition: TShape.cxx:154
Abstract 3D shapes viewer.
void SetSectionsValid(UInt_t mask)
Definition: TBuffer3D.h:67
short Color_t
Definition: RtypesCore.h:79
virtual const TBuffer3D & GetBuffer3D(Int_t reqSections) const
Stub to avoid forcing implementation at this stage.
Definition: TShape.cxx:252
Int_t ShapeDistancetoPrimitive(Int_t numPoints, Int_t px, Int_t py)
Distance to primitive.
Definition: TShape.cxx:117
Bool_t fLocalFrame
Definition: TBuffer3D.h:92
TGeometry description.
Definition: TGeometry.h:43
point * points
Definition: X3DBuffer.c:20
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
R__EXTERN TNode * gNode
Definition: TShape.h:80
This is the base class for all geometry shapes.
Definition: TShape.h:47
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:40
void TransformPoints(Double_t *points, UInt_t NbPnts) const
Transform points (LocalToMaster)
Definition: TShape.cxx:190
TMaterial * GetMaterial(const char *name) const
Return pointer to Material with name.
Definition: TGeometry.cxx:322
TObject * Remove(TObject *obj)
Remove object from the list.
Definition: THashList.cxx:284
TClass * IsA() const
virtual Int_t AddObject(const TBuffer3D &buffer, Bool_t *addChildren=0)=0
Int_t GetBasicColor() const
Get basic color.
Definition: TShape.cxx:241
unsigned int UInt_t
Definition: RtypesCore.h:42
Generic 3D primitive description class.
Definition: TBuffer3D.h:19
void AbstractMethod(const char *method) const
Use this method to implement an "abstract" method that you don't want to leave purely abstract...
Definition: TObject.cxx:960
TMaterial * fMaterial
Definition: TShape.h:52
TObject * fID
Definition: TBuffer3D.h:89
virtual Color_t GetLineColor() const
Definition: TAttLine.h:47
virtual void Paint(Option_t *option="")
This method is used only when a shape is painted outside a TNode.
Definition: TShape.cxx:142
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
Bool_t fReflection
Definition: TBuffer3D.h:93
virtual Int_t GetSize() const
Definition: TCollection.h:95
static const double x1[5]
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
Int_t fColor
Definition: TBuffer3D.h:90
#define name(a, b)
Definition: linkTestLib0.cpp:5
Binding & operator=(OUT(*fun)(void))
Int_t fVisibility
Definition: TShape.h:51
virtual void Add(TObject *obj)
Definition: TList.h:81
#define gPad
Definition: TVirtualPad.h:288
Short_t fTransparency
Definition: TBuffer3D.h:91
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
TShape & operator=(const TShape &)
assignment operator
Definition: TShape.cxx:92
R__EXTERN TGeometry * gGeometry
Definition: TGeometry.h:162
Line Attributes class.
Definition: TAttLine.h:32
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
Bool_t SectionsValid(UInt_t mask) const
Definition: TBuffer3D.h:69
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904