Logo ROOT  
Reference Guide
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#include "json.hpp"
41
42
43namespace
44{
45 TGeoManager* init_geo_mangeur()
46 {
47 // Create a phony geo manager that can be used for storing free
48 // shapes. Otherwise shapes register themselves to current
49 // geo-manager (or even create one).
50
52 TGeoIdentity *old_id = gGeoIdentity;
53 gGeoManager = nullptr;
54 TGeoManager* mgr = new TGeoManager();
55 mgr->SetNameTitle("REveGeoShape::fgGeoManager",
56 "Static geo manager used for wrapped TGeoShapes.");
57 gGeoIdentity = new TGeoIdentity("Identity");
58 gGeoManager = old;
59 gGeoIdentity = old_id;
60 return mgr;
61 }
62
63 TGeoHMatrix localGeoHMatrixIdentity;
64}
65
66using namespace ROOT::Experimental;
67namespace REX = ROOT::Experimental;
68
69/** \class REveGeoShape
70\ingroup REve
71Wrapper for TGeoShape with absolute positioning and color
72attributes allowing display of extracted TGeoShape's (without an
73active TGeoManager) and simplified geometries (needed for non-linear
74projections).
75
76TGeoCompositeShapes and TGeoAssemblies are supported.
77
78If fNSegments data-member is < 2 (0 by default), the default number of
79segments is used for tesselation and special GL objects are
80instantiated for selected shapes (spheres, tubes). If fNSegments is > 2,
81it gets forwarded to geo-manager and this tesselation detail is
82used when creating the buffer passed to GL.
83*/
84
86
87////////////////////////////////////////////////////////////////////////////////
88/// Return static geo-manager that is used internally to make shapes
89/// lead a happy life.
90/// Set gGeoManager to this object when creating TGeoShapes to be
91/// passed into EveGeoShapes.
92
94{
95 return fgGeoManager;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Return static identity matrix in homogeneous representation.
100/// This is needed because TGeoCompositeShape::PaintComposite()
101/// assumes TGeoShape::fgTransform is a TGeoHMatrix and we need to pass in
102/// an identity matrix when painting a composite shape.
103
105{
106 return &localGeoHMatrixIdentity;
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// Constructor.
111
112REveGeoShape::REveGeoShape(const std::string &name, const std::string &title)
113 : REveShape(name, title), fNSegments(0), fShape(nullptr), fCompositeShape(nullptr)
114{
116}
117
118////////////////////////////////////////////////////////////////////////////////
119/// Destructor.
120
122{
123 SetShape(nullptr);
124}
125
126////////////////////////////////////////////////////////////////////////////////
127/// Create derived REveGeoShape form a TGeoCompositeShape.
128
130{
131 auto poly = new REveGeoPolyShape();
132 poly->BuildFromComposite(fCompositeShape, fNSegments);
133 return poly;
134}
135
136////////////////////////////////////////////////////////////////////////////////
137/// Fill core part of JSON representation.
138
140{
141 return REveShape::WriteCoreJson(j, rnr_offset);
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Crates 3D point array for rendering.
146
148{
149 if (!fShape) return;
150
151 REveGeoPolyShape *egps = nullptr;
152 std::unique_ptr<REveGeoPolyShape> tmp_egps;
153
154 if (fCompositeShape) {
155
156 egps = dynamic_cast<REveGeoPolyShape *>(fShape);
157
158 } else {
159
160 tmp_egps = std::make_unique<REveGeoPolyShape>();
161
162 tmp_egps->BuildFromShape(fShape, fNSegments);
163
164 egps = tmp_egps.get();
165 }
166
167 fRenderData = std::make_unique<REveRenderData>("makeEveGeoShape");
168
171}
172
173////////////////////////////////////////////////////////////////////////////////
174/// Set number of segments.
175
177{
178 if (s != fNSegments && fCompositeShape != nullptr) {
179 delete fShape;
181 }
182 fNSegments = s;
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Set TGeoShape shown by this object.
187///
188/// The shape is owned by REveGeoShape but TGeoShape::fUniqueID is
189/// used for reference counting so you can pass the same shape to
190/// several EveGeoShapes.
191///
192/// If it if is taken from an existing TGeoManager, manually
193/// increase the fUniqueID before passing it to REveGeoShape.
194
196{
198
199 if (fCompositeShape) {
200 delete fShape;
202 }
203
204 if (fShape) {
206 if (fShape->GetUniqueID() == 0) {
207 delete fShape;
208 }
209 }
210
211 fShape = s;
212
213 if (fShape) {
215 fCompositeShape = dynamic_cast<TGeoCompositeShape *>(fShape);
216 if (fCompositeShape) {
218 }
219 }
220}
221
222////////////////////////////////////////////////////////////////////////////////
223/// Compute bounding-box.
224
226{
227 TGeoBBox *bb = dynamic_cast<TGeoBBox *>(fShape);
228 if (bb) {
229 BBoxInit();
230 const Double_t *o = bb->GetOrigin();
231 BBoxCheckPoint(o[0] - bb->GetDX(), o[0] - bb->GetDY(), o[0] - bb->GetDZ());
232 BBoxCheckPoint(o[0] + bb->GetDX(), o[0] + bb->GetDY(), o[0] + bb->GetDZ());
233 } else {
234 BBoxZero();
235 }
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// Save the shape tree as REveGeoShapeExtract.
240/// File is always recreated.
241
242void REveGeoShape::SaveExtract(const char* file, const char* name)
243{
244 // FIXME: ownership
245 REveGeoShapeExtract* gse = DumpShapeTree(this, nullptr);
246
247 TFile f(file, "RECREATE");
248 gse->Write(name);
249 f.Close();
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// Write the shape tree as REveGeoShapeExtract to current directory.
254/// FIXME: SL: no any write into gDirectory
255
257{
258 // FIXME: ownership
259 REveGeoShapeExtract* gse = DumpShapeTree(this, nullptr);
260 gse->Write(name);
261}
262
263////////////////////////////////////////////////////////////////////////////////
264/// Export this shape and its descendants into a geoshape-extract.
265
267 REveGeoShapeExtract* parent)
268{
269 REveGeoShapeExtract* she = new REveGeoShapeExtract(gsre->GetCName(), gsre->GetCTitle());
270 she->SetTrans(gsre->RefMainTrans().Array());
271 {
272 Int_t ci = gsre->GetFillColor();
273 TColor *c = gROOT->GetColor(ci);
274 Float_t rgba[4] = { 1, 0, 0, Float_t(1 - gsre->GetMainTransparency()/100.) };
275 if (c)
276 {
277 rgba[0] = c->GetRed();
278 rgba[1] = c->GetGreen();
279 rgba[2] = c->GetBlue();
280 }
281 she->SetRGBA(rgba);
282 }
283 {
284 Int_t ci = gsre->GetLineColor();
285 TColor *c = gROOT->GetColor(ci);
286 Float_t rgba[4] = { 1, 0, 0, 1 };
287 if (c)
288 {
289 rgba[0] = c->GetRed();
290 rgba[1] = c->GetGreen();
291 rgba[2] = c->GetBlue();
292 }
293 she->SetRGBALine(rgba);
294 }
295 she->SetRnrSelf(gsre->GetRnrSelf());
296 she->SetRnrElements(gsre->GetRnrChildren());
297 she->SetRnrFrame(gsre->GetDrawFrame());
298 she->SetMiniFrame(gsre->GetMiniFrame());
299 she->SetShape(gsre->GetShape());
300 if (gsre->HasChildren())
301 {
302 TList* ele = new TList();
303 she->SetElements(ele);
304 she->GetElements()->SetOwner(true);
305
306 for (auto &c: gsre->RefChildren())
307 DumpShapeTree(dynamic_cast<REveGeoShape *>(c), she);
308 }
309 if (parent)
310 parent->GetElements()->Add(she);
311
312 return she;
313}
314
315////////////////////////////////////////////////////////////////////////////////
316/// Import a shape extract 'gse' under element 'parent'.
317
319 REveElement* parent)
320{
323 REveGeoShape* gsre = SubImportShapeExtract(gse, parent);
324 gsre->StampObjProps();
325 return gsre;
326}
327
328////////////////////////////////////////////////////////////////////////////////
329/// Recursive version for importing a shape extract tree.
330
332 REveElement* parent)
333{
334 REveGeoShape* gsre = new REveGeoShape(gse->GetName(), gse->GetTitle());
335 gsre->RefMainTrans().SetFromArray(gse->GetTrans());
336 const Float_t* rgba = gse->GetRGBA();
337 gsre->SetMainColorRGB(rgba[0], rgba[1], rgba[2]);
338 gsre->SetMainAlpha(rgba[3]);
339 rgba = gse->GetRGBALine();
340 gsre->SetLineColor(TColor::GetColor(rgba[0], rgba[1], rgba[2]));
341 gsre->SetRnrSelf(gse->GetRnrSelf());
342 gsre->SetRnrChildren(gse->GetRnrElements());
343 gsre->SetDrawFrame(gse->GetRnrFrame());
344 gsre->SetMiniFrame(gse->GetMiniFrame());
345 gsre->SetShape(gse->GetShape());
346
347 if (parent)
348 parent->AddElement(gsre);
349
350 if (gse->HasElements())
351 {
352 TIter next(gse->GetElements());
354 while ((chld = (REveGeoShapeExtract*) next()) != nullptr)
355 SubImportShapeExtract(chld, gsre);
356 }
357
358 return gsre;
359}
360
361////////////////////////////////////////////////////////////////////////////////
362/// Return class for projected objects:
363/// - 2D projections: REvePolygonSetProjected,
364/// - 3D projections: REveGeoShapeProjected.
365/// Virtual from REveProjectable.
366
368{
369 if (p->Is2D())
370 return TClass::GetClass<REvePolygonSetProjected>();
371 else
372 return TClass::GetClass<REveGeoShapeProjected>();
373}
374
375////////////////////////////////////////////////////////////////////////////////
376/// Create a TBuffer3D suitable for presentation of the shape.
377/// Transformation matrix is also applied.
378
379std::unique_ptr<TBuffer3D> REveGeoShape::MakeBuffer3D()
380{
381 std::unique_ptr<TBuffer3D> buff;
382
383 if (!fShape) return buff;
384
385 if (dynamic_cast<TGeoShapeAssembly*>(fShape)) {
386 // TGeoShapeAssembly makes a bad TBuffer3D.
387 return buff;
388 }
389
391
392 buff.reset(fShape->MakeBuffer3D());
393 REveTrans &mx = RefMainTrans();
394 if (mx.GetUseTrans()) {
395 Int_t n = buff->NbPnts();
396 Double_t *pnts = buff->fPnts;
397 for(Int_t k = 0; k < n; ++k) {
398 mx.MultiplyIP(&pnts[3*k]);
399 }
400 }
401 return buff;
402}
403
404
405//==============================================================================
406// REveGeoShapeProjected
407//==============================================================================
408
409/** \class REveGeoShapeProjected
410\ingroup REve
411A 3D projected REveGeoShape.
412*/
413
414////////////////////////////////////////////////////////////////////////////////
415/// Constructor.
416
418 REveShape("REveGeoShapeProjected"),
419 fBuff()
420{
421}
422
423////////////////////////////////////////////////////////////////////////////////
424/// Destructor.
425
427{
428 /// should be here because of TBuffer3D destructor
429}
430
431
432////////////////////////////////////////////////////////////////////////////////
433/// This should never be called as this class is only used for 3D
434/// projections.
435/// The implementation is required as this metod is abstract.
436/// Just emits a warning if called.
437
439{
440 Warning("SetDepthLocal", "This function only exists to fulfill an abstract interface.");
441}
442
443////////////////////////////////////////////////////////////////////////////////
444/// This is virtual method from base-class REveProjected.
445
447 REveProjectable* model)
448{
450
451 REveGeoShape* gre = dynamic_cast<REveGeoShape*>(fProjectable);
452 CopyVizParams(gre);
453}
454
455////////////////////////////////////////////////////////////////////////////////
456/// This is virtual method from base-class REveProjected.
457
459{
460 REveGeoShape *gre = dynamic_cast<REveGeoShape*>(fProjectable);
462
463 fBuff = gre->MakeBuffer3D();
464
465 if (fBuff)
466 {
468
469 Double_t *p = fBuff->fPnts;
470 for (UInt_t i = 0; i < fBuff->NbPnts(); ++i, p+=3)
471 {
472 prj->ProjectPointdv(p, 0);
473 }
474 }
475
476 ResetBBox();
477}
478
479////////////////////////////////////////////////////////////////////////////////
480/// Override of virtual method from TAttBBox.
481
483{
484 if (fBuff && fBuff->NbPnts() > 0)
485 {
486 BBoxInit();
487
488 Double_t *p = fBuff->fPnts;
489 for (UInt_t i = 0; i < fBuff->NbPnts(); ++i, p+=3)
490 {
491 BBoxCheckPoint(p[0], p[1], p[2]);
492 }
493 }
494 else
495 {
496 BBoxZero();
497 }
498}
#define f(i)
Definition: RSha256.hxx:104
#define c(i)
Definition: RSha256.hxx:101
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
double Double_t
Definition: RtypesCore.h:55
float Float_t
Definition: RtypesCore.h:53
void Warning(const char *location, const char *msgfmt,...)
R__EXTERN TEveManager * gEve
Definition: TEveManager.h:243
char name[80]
Definition: TGX11.cxx:109
R__EXTERN TGeoManager * gGeoManager
Definition: TGeoManager.h:601
R__EXTERN TGeoIdentity * gGeoIdentity
Definition: TGeoMatrix.h:478
#define gROOT
Definition: TROOT.h:415
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.
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.
std::unique_ptr< REveRenderData > fRenderData
Externally assigned and controlled user data.
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
virtual void BuildRenderData()
Write transformation Matrix to render data.
REveGeoManagerHolder Exception-safe global variable holders.
Definition: REveUtil.hxx:88
void FillRenderData(REveRenderData &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
Override of virtual method from TAttBBox.
std::unique_ptr< TBuffer3D > fBuff
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
3d buffer
static REveGeoShape * ImportShapeExtract(REveGeoShapeExtract *gse, REveElement *parent=nullptr)
Import a shape extract 'gse' under element 'parent'.
TGeoShape * MakePolyShape()
Create derived REveGeoShape form a TGeoCompositeShape.
static TGeoManager * fgGeoManager
Temporary holder (if passed shape is composite shape).
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:
REveGeoShape(const REveGeoShape &)
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 ~REveGeoShape()
Destructor.
virtual std::unique_ptr< TBuffer3D > MakeBuffer3D()
Create a TBuffer3D suitable for presentation of the shape.
void BuildRenderData() override
Crates 3D point array for rendering.
static TGeoHMatrix * GetGeoHMatrixIdentity()
Return static identity matrix in homogeneous representation.
static TGeoManager * GetGeoManager()
Return static geo-manager that is used internally to make shapes lead a happy life.
TGeoCompositeShape * fCompositeShape
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.
REveProjection Base for specific classes that implement non-linear projections.
void ProjectPointdv(Double_t *v, Float_t d)
Project double array.
virtual Bool_t Is2D() const =0
void CopyVizParams(const REveElement *el) override
Copy visualization parameters from element el.
Definition: REveShape.cxx:89
virtual void SetMiniFrame(Bool_t r)
Definition: REveShape.hxx:69
virtual void SetLineColor(Color_t c)
Definition: REveShape.hxx:65
virtual Color_t GetFillColor() const
Definition: REveShape.hxx:57
Int_t WriteCoreJson(nlohmann::json &j, Int_t rnr_offset) override
Fill core part of JSON representation.
Definition: REveShape.cxx:60
virtual Bool_t GetDrawFrame() const
Definition: REveShape.hxx:60
virtual Color_t GetLineColor() const
Definition: REveShape.hxx:58
virtual Bool_t GetMiniFrame() const
Definition: REveShape.hxx:62
virtual void SetDrawFrame(Bool_t f)
Definition: REveShape.hxx:67
void MultiplyIP(TVector3 &v, Double_t w=1) const
Multiply vector in-place.
Definition: REveTrans.cxx:730
void SetFromArray(const Double_t arr[16])
Set matrix from Double_t array.
Definition: REveTrans.cxx:179
void BBoxCheckPoint(Float_t x, Float_t y, Float_t z)
Definition: TAttBBox.h:58
void ResetBBox()
Definition: TAttBBox.h:46
void BBoxZero(Float_t epsilon=0, Float_t x=0, Float_t y=0, Float_t z=0)
Create cube of volume (2*epsilon)^3 at (x,y,z).
Definition: TAttBBox.cxx:42
void BBoxInit(Float_t infinity=1e6)
Dynamic Float_t[6] X(min,max), Y(min,max), Z(min,max)
Definition: TAttBBox.cxx:29
@ kRawSizes
Definition: TBuffer3D.h:53
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:75
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:19
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:1764
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
Box class.
Definition: TGeoBBox.h:18
virtual const Double_t * GetOrigin() const
Definition: TGeoBBox.h:73
virtual Double_t GetDX() const
Definition: TGeoBBox.h:70
virtual Double_t GetDZ() const
Definition: TGeoBBox.h:72
virtual Double_t GetDY() const
Definition: TGeoBBox.h:71
Class handling Boolean composition of shapes.
Matrix class used for computing global transformations Should NOT be used for node definition.
Definition: TGeoMatrix.h:421
An identity transformation.
Definition: TGeoMatrix.h:384
The manager class for any TGeo geometry.
Definition: TGeoManager.h:43
The shape encapsulating an assembly (union) of volumes.
Base abstract class for all shapes.
Definition: TGeoShape.h:26
virtual TBuffer3D * MakeBuffer3D() const
Definition: TGeoShape.h:143
A doubly linked list.
Definition: TList.h:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void SetNameTitle(const char *name, const char *title)
Set all the TNamed parameters (name and title).
Definition: TNamed.cxx:154
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:785
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition: TObject.cxx:375
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition: TObject.cxx:705
const Int_t n
Definition: legend1.C:16
static constexpr double s
Definition: file.py:1