Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REveGeoShape.cxx
Go to the documentation of this file.
1// @(#)root/eve7:$Id$
2// Author: Matevz Tadel 2007, 2018
3
4/*************************************************************************
5 * Copyright (C) 1995-2019, 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 <ROOT/REveGeoShape.hxx>
13#include <ROOT/REveUtil.hxx>
14#include <ROOT/REveTrans.hxx>
15#include <ROOT/REveManager.hxx>
19
23
24#include "TROOT.h"
25#include "TBuffer3D.h"
26#include "TBuffer3DTypes.h"
27#include "TColor.h"
28#include "TFile.h"
29
30#include "TGeoShape.h"
31#include "TGeoVolume.h"
32#include "TGeoNode.h"
33#include "TGeoShapeAssembly.h"
34#include "TGeoCompositeShape.h"
35#include "TGeoBoolNode.h"
36#include "TGeoManager.h"
37#include "TGeoMatrix.h"
38#include "TVirtualGeoPainter.h"
39
40
41namespace
42{
43 TGeoManager* init_geo_mangeur()
44 {
45 // Create a phony geo manager that can be used for storing free
46 // shapes. Otherwise shapes register themselves to current
47 // geo-manager (or even create one).
48
50 TGeoIdentity *old_id = gGeoIdentity;
51 gGeoManager = nullptr;
52 TGeoManager* mgr = new TGeoManager();
53 mgr->SetNameTitle("REveGeoShape::fgGeoManager",
54 "Static geo manager used for wrapped TGeoShapes.");
55 gGeoIdentity = new TGeoIdentity("Identity");
56 gGeoManager = old;
57 gGeoIdentity = old_id;
58 return mgr;
59 }
60
61 TGeoHMatrix localGeoHMatrixIdentity;
62}
63
64using namespace ROOT::Experimental;
65namespace REX = ROOT::Experimental;
66
67/** \class REveGeoShape
68\ingroup REve
69Wrapper for TGeoShape with absolute positioning and color
70attributes allowing display of extracted TGeoShape's (without an
71active TGeoManager) and simplified geometries (needed for non-linear
72projections).
73
74TGeoCompositeShapes and TGeoAssemblies are supported.
75
76If fNSegments data-member is < 2 (0 by default), the default number of
77segments is used for tesselation and special GL objects are
78instantiated for selected shapes (spheres, tubes). If fNSegments is > 2,
79it gets forwarded to geo-manager and this tesselation detail is
80used when creating the buffer passed to GL.
81
82The render data is the shape tessellated into triangles, written by
83REveGeoPolyShape::FillRenderData(): the entries `pos`, the vertices, and
84`idx`, three indices per triangle, plus `nrm`, one normal per triangle, when
85REveGeoPolyShape::SetAutoCalculateNormals() is on. The client draws one mesh
86of the triangles.
87*/
88
89TGeoManager *REveGeoShape::fgGeoManager = init_geo_mangeur();
90
91////////////////////////////////////////////////////////////////////////////////
92/// Return static geo-manager that is used internally to make shapes
93/// lead a happy life.
94/// Set gGeoManager to this object when creating TGeoShapes to be
95/// passed into EveGeoShapes.
96
97TGeoManager *REveGeoShape::GetGeoManager()
98{
99 return fgGeoManager;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// Return static identity matrix in homogeneous representation.
104/// This is needed because TGeoCompositeShape::PaintComposite()
105/// assumes TGeoShape::fgTransform is a TGeoHMatrix and we need to pass in
106/// an identity matrix when painting a composite shape.
107
109{
110 return &localGeoHMatrixIdentity;
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Constructor.
115
116REveGeoShape::REveGeoShape(const std::string &name, const std::string &title)
117 : REveShape(name, title), fNSegments(0), fShape(nullptr), fCompositeShape(nullptr)
118{
120}
121
122////////////////////////////////////////////////////////////////////////////////
123/// Destructor.
124
126{
127 SetShape(nullptr);
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Create derived REveGeoShape form a TGeoCompositeShape.
132
134{
135 auto poly = new REveGeoPolyShape();
136 poly->BuildFromComposite(fCompositeShape, fNSegments);
137 return poly;
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Fill core part of JSON representation.
142
143Int_t REveGeoShape::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
144{
145 return REveShape::WriteCoreJson(j, rnr_offset);
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// Write the render data entries, see the class description.
150
152{
153 if (!fShape) return;
154
155 // Tesselate first, a shape without faces has nothing that can be rendered
156 // as a mesh. Composite shapes are already converted in SetShape().
157 REveGeoPolyShape *egps = nullptr;
158 std::unique_ptr<REveGeoPolyShape> tmp_egps;
159
160 if (fCompositeShape) {
161 egps = dynamic_cast<REveGeoPolyShape *>(fShape);
162 } else {
164 tmp_egps = std::make_unique<REveGeoPolyShape>();
165 tmp_egps->BuildFromShape(fShape, fNSegments);
166 egps = tmp_egps.get();
167 }
168
169 if (!egps || egps->GetNumFaces() == 0) return;
170
171 fRenderData = std::make_unique<REveRenderData>("makeEveGeoShape");
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// Set number of segments.
177
179{
180 if (s != fNSegments && fCompositeShape != nullptr) {
181 delete fShape;
183 }
184 fNSegments = s;
185}
186
187////////////////////////////////////////////////////////////////////////////////
188/// Set TGeoShape shown by this object.
189///
190/// The shape is owned by REveGeoShape but TGeoShape::fUniqueID is
191/// used for reference counting so you can pass the same shape to
192/// several EveGeoShapes.
193///
194/// If it if is taken from an existing TGeoManager, manually
195/// increase the fUniqueID before passing it to REveGeoShape.
196
198{
200
201 if (fCompositeShape) {
202 delete fShape;
204 }
205
206 if (fShape) {
208 if (fShape->GetUniqueID() == 0) {
209 delete fShape;
210 }
211 }
212
213 fShape = s;
214
215 if (fShape) {
217 fCompositeShape = dynamic_cast<TGeoCompositeShape *>(fShape);
218 if (fCompositeShape) {
220 }
221 }
222}
223
224////////////////////////////////////////////////////////////////////////////////
225/// Compute bounding-box.
226
228{
229 BBoxInit();
230 TGeoBBox *bb = dynamic_cast<TGeoBBox *>(fShape);
231 if (bb) {
232 const Double_t *o = bb->GetOrigin();
233 BBoxCheckBox(REveVector(o[0], o[1], o[2]), REveVector(bb->GetDX(), bb->GetDY(), bb->GetDZ()));
234 }
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Save the shape tree as REveGeoShapeExtract.
239/// File is always recreated.
240
241void REveGeoShape::SaveExtract(const char* file, const char* name)
242{
243 // FIXME: ownership
244 REveGeoShapeExtract* gse = DumpShapeTree(this, nullptr);
245
246 TFile f(file, "RECREATE");
247 gse->Write(name);
248 f.Close();
249}
250
251////////////////////////////////////////////////////////////////////////////////
252/// Write the shape tree as REveGeoShapeExtract to current directory.
253/// FIXME: SL: no any write into gDirectory
254
256{
257 // FIXME: ownership
258 REveGeoShapeExtract* gse = DumpShapeTree(this, nullptr);
259 gse->Write(name);
260}
261
262////////////////////////////////////////////////////////////////////////////////
263/// Export this shape and its descendants into a geoshape-extract.
264
266 REveGeoShapeExtract* parent)
267{
268 REveGeoShapeExtract* she = new REveGeoShapeExtract(gsre->GetCName(), gsre->GetCTitle());
269 she->SetTrans(gsre->RefMainTrans().Array());
270 {
271 Int_t ci = gsre->GetFillColor();
272 TColor *c = gROOT->GetColor(ci);
273 Float_t rgba[4] = { 1, 0, 0, Float_t(1 - gsre->GetMainTransparency()/100.) };
274 if (c)
275 {
276 rgba[0] = c->GetRed();
277 rgba[1] = c->GetGreen();
278 rgba[2] = c->GetBlue();
279 }
280 she->SetRGBA(rgba);
281 }
282 {
283 Int_t ci = gsre->GetLineColor();
284 TColor *c = gROOT->GetColor(ci);
285 Float_t rgba[4] = { 1, 0, 0, 1 };
286 if (c)
287 {
288 rgba[0] = c->GetRed();
289 rgba[1] = c->GetGreen();
290 rgba[2] = c->GetBlue();
291 }
292 she->SetRGBALine(rgba);
293 }
294 she->SetRnrSelf(gsre->GetRnrSelf());
295 she->SetRnrElements(gsre->GetRnrChildren());
296 she->SetRnrFrame(gsre->GetDrawFrame());
297 she->SetMiniFrame(gsre->GetMiniFrame());
298 she->SetShape(gsre->GetShape());
299 if (gsre->HasChildren())
300 {
301 TList* ele = new TList();
302 she->SetElements(ele);
303 she->GetElements()->SetOwner(true);
304
305 for (auto &c: gsre->RefChildren())
306 DumpShapeTree(dynamic_cast<REveGeoShape *>(c), she);
307 }
308 if (parent)
309 parent->GetElements()->Add(she);
310
311 return she;
312}
313
314////////////////////////////////////////////////////////////////////////////////
315/// Import a shape extract 'gse' under element 'parent'.
316
318 REveElement* parent)
319{
321 REveGeoShape* gsre = SubImportShapeExtract(gse, parent);
322 gsre->StampObjProps();
323 return gsre;
324}
325
326////////////////////////////////////////////////////////////////////////////////
327/// Recursive version for importing a shape extract tree.
328
330 REveElement* parent)
331{
332 REveGeoShape* gsre = new REveGeoShape(gse->GetName(), gse->GetTitle());
333 gsre->RefMainTrans().SetFromArray(gse->GetTrans());
334 const Float_t* rgba = gse->GetRGBA();
335 gsre->SetMainColorRGB(rgba[0], rgba[1], rgba[2]);
336 gsre->SetMainAlpha(rgba[3]);
337 rgba = gse->GetRGBALine();
338 gsre->SetLineColor(TColor::GetColor(rgba[0], rgba[1], rgba[2]));
339 gsre->SetRnrSelf(gse->GetRnrSelf());
340 gsre->SetRnrChildren(gse->GetRnrElements());
341 gsre->SetDrawFrame(gse->GetRnrFrame());
342 gsre->SetMiniFrame(gse->GetMiniFrame());
343 gsre->SetShape(gse->GetShape());
344
345 if (parent)
346 parent->AddElement(gsre);
347
348 if (gse->HasElements())
349 {
350 TIter next(gse->GetElements());
352 while ((chld = (REveGeoShapeExtract*) next()) != nullptr)
353 SubImportShapeExtract(chld, gsre);
354 }
355
356 return gsre;
357}
358
359////////////////////////////////////////////////////////////////////////////////
360/// Return class for projected objects:
361/// - 2D projections: REvePolygonSetProjected,
362/// - 3D projections: REveGeoShapeProjected.
363/// Virtual from REveProjectable.
364
366{
367 if (p->Is2D())
368 return TClass::GetClass<REvePolygonSetProjected>();
369 else
370 return TClass::GetClass<REveGeoShapeProjected>();
371}
372
373////////////////////////////////////////////////////////////////////////////////
374/// Create a TBuffer3D suitable for presentation of the shape, in the shape's
375/// own frame. The projected classes apply the shape's matrix and those of its
376/// ancestors, through REveProjectionManager::GetModelTrans().
377
378std::unique_ptr<TBuffer3D> REveGeoShape::MakeBuffer3D()
379{
380 std::unique_ptr<TBuffer3D> buff;
381
382 if (!fShape) return buff;
383
384 if (dynamic_cast<TGeoShapeAssembly*>(fShape)) {
385 // TGeoShapeAssembly makes a bad TBuffer3D.
386 return buff;
387 }
388
390
391 buff.reset(fShape->MakeBuffer3D());
392 return buff;
393}
394
395
396//==============================================================================
397// REveGeoShapeProjected
398//==============================================================================
399
400/** \class REveGeoShapeProjected
401\ingroup REve
402A 3D projected REveGeoShape.
403*/
404
405////////////////////////////////////////////////////////////////////////////////
406/// Constructor.
407
409 REveShape("REveGeoShapeProjected"),
410 fBuff()
411{
412}
413
414////////////////////////////////////////////////////////////////////////////////
415/// Destructor.
416
418{
419 /// should be here because of TBuffer3D destructor
420}
421
422
423////////////////////////////////////////////////////////////////////////////////
424/// This should never be called as this class is only used for 3D
425/// projections.
426/// The implementation is required as this metod is abstract.
427/// Just emits a warning if called.
428
430{
431 Warning("SetDepthLocal", "This function only exists to fulfill an abstract interface.");
432}
433
434////////////////////////////////////////////////////////////////////////////////
435/// This is virtual method from base-class REveProjected.
436
445
446////////////////////////////////////////////////////////////////////////////////
447/// This is virtual method from base-class REveProjected.
448
450{
451 REveGeoShape *gre = dynamic_cast<REveGeoShape*>(fProjectable);
453
454 fBuff = gre->MakeBuffer3D();
455
456 if (fBuff)
457 {
459
460 REveTrans mt;
461 const REveTrans *trans = fManager->GetModelTrans(gre, mt);
462 Double_t *p = fBuff->fPnts;
463 for (UInt_t i = 0; i < fBuff->NbPnts(); ++i, p+=3)
464 {
465 prj->ProjectPointdv(trans, p, p, 0);
466 }
467 }
468
469 ResetBBox();
470}
471
472////////////////////////////////////////////////////////////////////////////////
473/// Fill the bounding box (virtual from REveElement).
474
476{
477 BBoxInit();
478 if (fBuff && fBuff->NbPnts() > 0)
479 {
480 Double_t *p = fBuff->fPnts;
481 for (UInt_t i = 0; i < fBuff->NbPnts(); ++i, p+=3)
482 {
483 BBoxCheckPoint(p[0], p[1], p[2]);
484 }
485 }
486}
dims_t fShape
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
winID h TVirtualViewer3D TVirtualGLPainter p
char name[80]
Definition TGX11.cxx:142
R__EXTERN TGeoManager * gGeoManager
R__EXTERN TGeoIdentity * gGeoIdentity
Definition TGeoMatrix.h:538
#define gROOT
Definition TROOT.h:417
const char * GetCTitle() const
virtual REveTrans & RefMainTrans()
Return reference to main transformation.
void SetMainColorRGB(UChar_t r, UChar_t g, UChar_t b)
Convert RGB values to Color_t and call SetMainColor.
void SetMainAlpha(Float_t alpha)
Set main-transparency via float alpha variable.
void BBoxCheckPoint(const REveVector &v)
const char * GetCName() const
virtual void AddElement(REveElement *el)
Add el to the list of children.
virtual Bool_t GetRnrSelf() const
virtual Bool_t SetRnrChildren(Bool_t rnr)
Set render state of this element's children, i.e.
virtual void InitMainTrans(Bool_t can_edit=kTRUE)
Initialize the main transformation to identity matrix, without motion.
std::unique_ptr< REveRenderData > fRenderData
! Render data entries for the client's maker, see REveRenderData.
virtual Char_t GetMainTransparency() const
virtual Bool_t SetRnrSelf(Bool_t rnr)
Set render state of this element, i.e.
virtual Bool_t GetRnrChildren() const
void BBoxInit()
Start filling the bounding box with BBoxCheckPoint(): min at +infinity, max at -infinity,...
void BBoxCheckBox(const REveVector &c, const REveVector &h)
Grow the box to hold the axis-aligned box with centre c and half sizes h.
REveGeoManagerHolder Exception-safe global variable holders.
Definition REveUtil.hxx:95
void FillRenderData(REveRenderData &rd)
Write the render data entries of the tessellated shape into rd:
void SetRGBA(const Float_t arr[4])
Set RGBA color.
void SetTrans(const Double_t arr[16])
Set transformation matrix.
Bool_t HasElements()
True if has at least one element.
void SetRGBALine(const Float_t arr[4])
Set RGBA color for line.
void ComputeBBox() override
Fill the bounding box (virtual from REveElement).
std::unique_ptr< TBuffer3D > fBuff
! 3d buffer
void SetProjection(REveProjectionManager *proj, REveProjectable *model) override
This is virtual method from base-class REveProjected.
void UpdateProjection() override
This is virtual method from base-class REveProjected.
void SetDepthLocal(Float_t d) override
This should never be called as this class is only used for 3D projections.
static REveGeoShape * ImportShapeExtract(REveGeoShapeExtract *gse, REveElement *parent=nullptr)
Import a shape extract 'gse' under element 'parent'.
TGeoShape * MakePolyShape()
Create derived REveGeoShape form a TGeoCompositeShape.
~REveGeoShape() override
Destructor.
static TGeoManager * fgGeoManager
void SetNSegments(Int_t s)
Set number of segments.
void SaveExtract(const char *file, const char *name)
Save the shape tree as REveGeoShapeExtract.
TClass * ProjectedClass(const REveProjection *p) const override
Return class for projected objects:
static REveGeoShape * SubImportShapeExtract(REveGeoShapeExtract *gse, REveElement *parent)
Recursive version for importing a shape extract tree.
Int_t WriteCoreJson(nlohmann::json &j, Int_t rnr_offset) override
Fill core part of JSON representation.
void ComputeBBox() override
Compute bounding-box.
void WriteExtract(const char *name)
Write the shape tree as REveGeoShapeExtract to current directory.
void SetShape(TGeoShape *s)
Set TGeoShape shown by this object.
virtual std::unique_ptr< TBuffer3D > MakeBuffer3D()
Create a TBuffer3D suitable for presentation of the shape, in the shape's own frame.
void BuildRenderData() override
Write the render data entries, see the class description.
static TGeoHMatrix * GetGeoHMatrixIdentity()
Return static identity matrix in homogeneous representation.
REveGeoShape(const REveGeoShape &)=delete
TGeoCompositeShape * fCompositeShape
! Temporary holder (if passed shape is composite shape).
REveGeoShapeExtract * DumpShapeTree(REveGeoShape *geon, REveGeoShapeExtract *parent=nullptr)
Export this shape and its descendants into a geoshape-extract.
virtual void SetProjection(REveProjectionManager *mng, REveProjectable *model)
Sets projection manager and reference in the projectable object.
REveProjectionManager Manager class for steering of projections and managing projected objects.
const REveTrans * GetModelTrans(REveElement *model, REveTrans &t)
Return the matrix that takes the coordinates of model into the frame of this manager,...
REveProjection Base for specific classes that implement non-linear projections.
void ProjectPointdv(Double_t *v, Float_t d)
Project double array.
void CopyVizParams(const REveElement *el) override
Copy visualization parameters from element el.
Definition REveShape.cxx:82
Int_t WriteCoreJson(nlohmann::json &j, Int_t rnr_offset) override
Fill core part of JSON representation.
Definition REveShape.cxx:51
void SetFromArray(const Double_t arr[16])
Set matrix from Double_t array.
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
The color creation and management class.
Definition TColor.h:23
static Int_t GetColor(const char *hexcolor)
Static method returning color number for color specified by hex color string of form: "#rrggbb",...
Definition TColor.cxx:1941
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
Box class.
Definition TGeoBBox.h:18
virtual const Double_t * GetOrigin() const
Definition TGeoBBox.h:81
virtual Double_t GetDX() const
Definition TGeoBBox.h:78
virtual Double_t GetDZ() const
Definition TGeoBBox.h:80
virtual Double_t GetDY() const
Definition TGeoBBox.h:79
Composite shapes are Boolean combinations of two or more shape components.
Matrix class used for computing global transformations Should NOT be used for node definition.
Definition TGeoMatrix.h:459
An identity transformation.
Definition TGeoMatrix.h:407
The manager class for any TGeo geometry.
Definition TGeoManager.h:46
The shape encapsulating an assembly (union) of volumes.
Base abstract class for all shapes.
Definition TGeoShape.h:25
virtual TBuffer3D * MakeBuffer3D() const
Definition TGeoShape.h:156
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
virtual void SetNameTitle(const char *name, const char *title)
Set all the TNamed parameters (name and title).
Definition TNamed.cxx:163
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition TObject.cxx:479
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TObject.cxx:987
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition TObject.cxx:897
Namespace for ROOT features in testing.
Definition TROOT.h:100
REveVectorT< Float_t > REveVector