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 "TClass.h"
23#include "TMath.h"
24
25#include <assert.h>
26
28
29/** \class TShape
30\ingroup g3d
31
32This is the base class for all geometry shapes.
33/The list of shapes currently supported correspond to the shapes
34in Geant version 3:
35
36~~~ {.cpp}
37 TBRIK,TCONE,TCONS,TGTRA,TPARA,TPCON,TPGON
38 TTRAP,TTRD1,TTRD2,THYPE, TTUBE and TTUBS.
39~~~
40
41The figure below shows instances of all these shapes. This figure
42is 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
61TShape::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);
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
80 TNamed(ts),
81 TAttLine(ts),
82 TAttFill(ts),
83 TAtt3D(ts),
84 fNumber(ts.fNumber),
85 fVisibility(ts.fVisibility),
86 fMaterial(ts.fMaterial)
87{
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// assignment operator
92
94{
95 if (this!=&ts) {
100 fNumber=ts.fNumber;
103 }
104 return *this;
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Shape default destructor
109
111{
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];
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
163void 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
212void 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();
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
253const 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}
void Class()
Definition: Class.C:29
static const double x1[5]
int Int_t
Definition: RtypesCore.h:41
short Version_t
Definition: RtypesCore.h:61
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
double Double_t
Definition: RtypesCore.h:55
short Color_t
Definition: RtypesCore.h:79
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:365
char name[80]
Definition: TGX11.cxx:109
R__EXTERN TGeometry * gGeometry
Definition: TGeometry.h:158
Binding & operator=(OUT(*fun)(void))
R__EXTERN TNode * gNode
Definition: TShape.h:68
#define gPad
Definition: TVirtualPad.h:286
point * points
Definition: X3DBuffer.c:22
Use this attribute class when an object should have 3D capabilities.
Definition: TAtt3D.h:19
Fill Area Attributes class.
Definition: TAttFill.h:19
Line Attributes class.
Definition: TAttLine.h:18
virtual Color_t GetLineColor() const
Return the line color.
Definition: TAttLine.h:33
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
Generic 3D primitive description class.
Definition: TBuffer3D.h:18
void SetLocalMasterIdentity()
Set kRaw tessellation section of buffer with supplied sizes.
Definition: TBuffer3D.cxx:296
Bool_t SectionsValid(UInt_t mask) const
Definition: TBuffer3D.h:67
@ kRawSizes
Definition: TBuffer3D.h:53
void ClearSectionsValid()
Clear any sections marked valid.
Definition: TBuffer3D.cxx:286
void SetSectionsValid(UInt_t mask)
Definition: TBuffer3D.h:65
Bool_t fLocalFrame
Definition: TBuffer3D.h:90
Int_t fColor
Definition: TBuffer3D.h:88
Short_t fTransparency
Definition: TBuffer3D.h:89
Bool_t fReflection
Definition: TBuffer3D.h:91
TObject * fID
Definition: TBuffer3D.h:87
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
Bool_t IsReading() const
Definition: TBuffer.h:85
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
TGeometry description.
Definition: TGeometry.h:39
virtual void Local2Master(Double_t *local, Double_t *master)
Convert one point from local system to master reference system.
Definition: TGeometry.cxx:407
TMaterial * GetMaterial(const char *name) const
Return pointer to Material with name.
Definition: TGeometry.cxx:322
THashList * GetListOfShapes() const
Definition: TGeometry.h:75
TObject * Remove(TObject *obj)
Remove object from the list.
Definition: THashList.cxx:378
virtual void Add(TObject *obj)
Definition: TList.h:87
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:51
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:922
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:866
This is the base class for all geometry shapes.
Definition: TShape.h:35
virtual void Paint(Option_t *option="")
This method is used only when a shape is painted outside a TNode.
Definition: TShape.cxx:143
TShape & operator=(const TShape &)
assignment operator
Definition: TShape.cxx:93
virtual void SetPoints(Double_t *points) const
Set points.
Definition: TShape.cxx:155
virtual ~TShape()
Shape default destructor.
Definition: TShape.cxx:110
Int_t GetBasicColor() const
Get basic color.
Definition: TShape.cxx:242
Int_t fVisibility
Definition: TShape.h:39
Int_t ShapeDistancetoPrimitive(Int_t numPoints, Int_t px, Int_t py)
Distance to primitive.
Definition: TShape.cxx:118
virtual const TBuffer3D & GetBuffer3D(Int_t reqSections) const
Stub to avoid forcing implementation at this stage.
Definition: TShape.cxx:253
TShape()
Shape default constructor.
Definition: TShape.cxx:51
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
Int_t fNumber
Definition: TShape.h:38
TMaterial * fMaterial
Definition: TShape.h:40
void TransformPoints(Double_t *points, UInt_t NbPnts) const
Transform points (LocalToMaster)
Definition: TShape.cxx:191
See TView3D.
Definition: TView.h:25
virtual void WCtoNDC(const Float_t *pw, Float_t *pn)=0
Abstract 3D shapes viewer.
virtual Int_t AddObject(const TBuffer3D &buffer, Bool_t *addChildren=0)=0
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
Double_t Sqrt(Double_t x)
Definition: TMath.h:681