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