Logo ROOT  
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 "TMath.h"
23 
24 #include <cassert>
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 
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);
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;
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();
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 }
TBuffer3D::SectionsValid
Bool_t SectionsValid(UInt_t mask) const
Definition: TBuffer3D.h:73
Version_t
short Version_t
Definition: RtypesCore.h:65
TShape::TShape
TShape()
Shape default constructor.
Definition: TShape.cxx:50
TBuffer3D::SetSectionsValid
void SetSectionsValid(UInt_t mask)
Definition: TBuffer3D.h:71
TGeometry::GetMaterial
TMaterial * GetMaterial(const char *name) const
Return pointer to Material with name.
Definition: TGeometry.cxx:322
TMaterial.h
gNode
R__EXTERN TNode * gNode
Definition: TShape.h:68
TNamed::operator=
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:51
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TNode.h
TShape::fNumber
Int_t fNumber
Definition: TShape.h:38
TBuffer::ReadClassBuffer
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
TView.h
TShape::fMaterial
TMaterial * fMaterial
Definition: TShape.h:40
TShape::Paint
virtual void Paint(Option_t *option="")
This method is used only when a shape is painted outside a TNode.
Definition: TShape.cxx:142
TMath::Sqrt
Double_t Sqrt(Double_t x)
Definition: TMath.h:691
operator=
Binding & operator=(OUT(*fun)(void))
Definition: TRInterface_Binding.h:11
Float_t
float Float_t
Definition: RtypesCore.h:57
Int_t
int Int_t
Definition: RtypesCore.h:45
TGeometry::Local2Master
virtual void Local2Master(Double_t *local, Double_t *master)
Convert one point from local system to master reference system.
Definition: TGeometry.cxx:407
gGeometry
R__EXTERN TGeometry * gGeometry
Definition: TGeometry.h:158
TShape::ShapeDistancetoPrimitive
Int_t ShapeDistancetoPrimitive(Int_t numPoints, Int_t px, Int_t py)
Distance to primitive.
Definition: TShape.cxx:117
TBuffer
Definition: TBuffer.h:43
TBuffer3D::SetLocalMasterIdentity
void SetLocalMasterIdentity()
Set kRaw tessellation section of buffer with supplied sizes.
Definition: TBuffer3D.cxx:296
TShape::GetBasicColor
Int_t GetBasicColor() const
Get basic color.
Definition: TShape.cxx:241
TBuffer::CheckByteCount
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
TGeometry
Definition: TGeometry.h:39
TAtt3D
Definition: TAtt3D.h:19
Color_t
short Color_t
Definition: RtypesCore.h:83
TShape::fVisibility
Int_t fVisibility
Definition: TShape.h:39
TGeometry.h
x1
static const double x1[5]
Definition: RooGaussKronrodIntegrator1D.cxx:346
ROOT::Math::gv_detail::dist
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:63
TShape::~TShape
virtual ~TShape()
Shape default destructor.
Definition: TShape.cxx:109
TShape::SetPoints
virtual void SetPoints(Double_t *points) const
Set points.
Definition: TShape.cxx:154
TShape::operator=
TShape & operator=(const TShape &)
assignment operator
Definition: TShape.cxx:92
TBuffer3D
Definition: TBuffer3D.h:17
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TShape.h
TBuffer.h
TBuffer3D::fColor
Int_t fColor
Definition: TBuffer3D.h:94
TAttLine
Definition: TAttLine.h:18
TView
Definition: TView.h:25
TBuffer3DTypes::kGeneric
@ kGeneric
Definition: TBuffer3DTypes.h:36
TShape::TransformPoints
void TransformPoints(Double_t *points, UInt_t NbPnts) const
Transform points (LocalToMaster)
Definition: TShape.cxx:190
TVirtualViewer3D.h
TNamed
Definition: TNamed.h:29
TBuffer::WriteClassBuffer
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
TBuffer3D.h
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TBuffer3DTypes.h
TVirtualPad.h
TBuffer3D::kRaw
@ kRaw
Definition: TBuffer3D.h:60
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TBuffer3D::fReflection
Bool_t fReflection
Definition: TBuffer3D.h:97
TObject::AbstractMethod
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:932
TBuffer3D::fTransparency
Short_t fTransparency
Definition: TBuffer3D.h:95
TGeometry::GetListOfShapes
THashList * GetListOfShapes() const
Definition: TGeometry.h:75
TShape
Definition: TShape.h:35
THashList::Remove
TObject * Remove(TObject *obj)
Remove object from the list.
Definition: THashList.cxx:378
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:876
TBuffer::ReadVersion
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
unsigned int
TBuffer3D::ClearSectionsValid
void ClearSectionsValid()
Clear any sections marked valid.
Definition: TBuffer3D.cxx:286
TBuffer::IsReading
Bool_t IsReading() const
Definition: TBuffer.h:86
Double_t
double Double_t
Definition: RtypesCore.h:59
TBuffer3D::fLocalFrame
Bool_t fLocalFrame
Definition: TBuffer3D.h:96
TBuffer3D::fID
TObject * fID
Definition: TBuffer3D.h:93
TBuffer3D::kAll
@ kAll
Definition: TBuffer3D.h:61
TCollection::GetSize
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
points
point * points
Definition: X3DBuffer.c:22
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TVirtualViewer3D
Definition: TVirtualViewer3D.h:33
TBuffer3D::kRawSizes
@ kRawSizes
Definition: TBuffer3D.h:59
name
char name[80]
Definition: TGX11.cxx:110
gPad
#define gPad
Definition: TVirtualPad.h:287
TVirtualViewer3D::AddObject
virtual Int_t AddObject(const TBuffer3D &buffer, Bool_t *addChildren=0)=0
TShape::GetBuffer3D
virtual const TBuffer3D & GetBuffer3D(Int_t reqSections) const
Stub to avoid forcing implementation at this stage.
Definition: TShape.cxx:252
TAttFill
Definition: TAttFill.h:19
SetLineColor
lv SetLineColor(kBlue)
TShape::FillBuffer3D
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
Class
void Class()
Definition: Class.C:29
TView::WCtoNDC
virtual void WCtoNDC(const Float_t *pw, Float_t *pn)=0
TMath.h
int
TBuffer3D::kCore
@ kCore
Definition: TBuffer3D.h:56