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