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*/
82
83TGeoManager *REveGeoShape::fgGeoManager = init_geo_mangeur();
84
85////////////////////////////////////////////////////////////////////////////////
86/// Return static geo-manager that is used internally to make shapes
87/// lead a happy life.
88/// Set gGeoManager to this object when creating TGeoShapes to be
89/// passed into EveGeoShapes.
90
91TGeoManager *REveGeoShape::GetGeoManager()
92{
93 return fgGeoManager;
94}
95
96////////////////////////////////////////////////////////////////////////////////
97/// Return static identity matrix in homogeneous representation.
98/// This is needed because TGeoCompositeShape::PaintComposite()
99/// assumes TGeoShape::fgTransform is a TGeoHMatrix and we need to pass in
100/// an identity matrix when painting a composite shape.
101
103{
104 return &localGeoHMatrixIdentity;
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Constructor.
109
110REveGeoShape::REveGeoShape(const std::string &name, const std::string &title)
111 : REveShape(name, title), fNSegments(0), fShape(nullptr), fCompositeShape(nullptr)
112{
114}
115
116////////////////////////////////////////////////////////////////////////////////
117/// Destructor.
118
120{
121 SetShape(nullptr);
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Create derived REveGeoShape form a TGeoCompositeShape.
126
128{
129 auto poly = new REveGeoPolyShape();
130 poly->BuildFromComposite(fCompositeShape, fNSegments);
131 return poly;
132}
133
134////////////////////////////////////////////////////////////////////////////////
135/// Fill core part of JSON representation.
136
137Int_t REveGeoShape::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
138{
139 return REveShape::WriteCoreJson(j, rnr_offset);
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// Crates 3D point array for rendering.
144
146{
147 if (!fShape) return;
148
149 fRenderData = std::make_unique<REveRenderData>("makeEveGeoShape");
151
152 if (fCompositeShape) {
153 REveGeoPolyShape* egps = dynamic_cast<REveGeoPolyShape *>(fShape);
155 } else {
157 std::unique_ptr<REveGeoPolyShape> tmp_egps = std::make_unique<REveGeoPolyShape>();
158 tmp_egps->BuildFromShape(fShape, fNSegments);
159 tmp_egps->FillRenderData(*fRenderData);
160 }
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Set number of segments.
165
167{
168 if (s != fNSegments && fCompositeShape != nullptr) {
169 delete fShape;
171 }
172 fNSegments = s;
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// Set TGeoShape shown by this object.
177///
178/// The shape is owned by REveGeoShape but TGeoShape::fUniqueID is
179/// used for reference counting so you can pass the same shape to
180/// several EveGeoShapes.
181///
182/// If it if is taken from an existing TGeoManager, manually
183/// increase the fUniqueID before passing it to REveGeoShape.
184
186{
188
189 if (fCompositeShape) {
190 delete fShape;
192 }
193
194 if (fShape) {
196 if (fShape->GetUniqueID() == 0) {
197 delete fShape;
198 }
199 }
200
201 fShape = s;
202
203 if (fShape) {
205 fCompositeShape = dynamic_cast<TGeoCompositeShape *>(fShape);
206 if (fCompositeShape) {
208 }
209 }
210}
211
212////////////////////////////////////////////////////////////////////////////////
213/// Compute bounding-box.
214
216{
217 TGeoBBox *bb = dynamic_cast<TGeoBBox *>(fShape);
218 if (bb) {
219 BBoxInit();
220 const Double_t *o = bb->GetOrigin();
221 BBoxCheckPoint(o[0] - bb->GetDX(), o[0] - bb->GetDY(), o[0] - bb->GetDZ());
222 BBoxCheckPoint(o[0] + bb->GetDX(), o[0] + bb->GetDY(), o[0] + bb->GetDZ());
223 } else {
224 BBoxZero();
225 }
226}
227
228////////////////////////////////////////////////////////////////////////////////
229/// Save the shape tree as REveGeoShapeExtract.
230/// File is always recreated.
231
232void REveGeoShape::SaveExtract(const char* file, const char* name)
233{
234 // FIXME: ownership
235 REveGeoShapeExtract* gse = DumpShapeTree(this, nullptr);
236
237 TFile f(file, "RECREATE");
238 gse->Write(name);
239 f.Close();
240}
241
242////////////////////////////////////////////////////////////////////////////////
243/// Write the shape tree as REveGeoShapeExtract to current directory.
244/// FIXME: SL: no any write into gDirectory
245
247{
248 // FIXME: ownership
249 REveGeoShapeExtract* gse = DumpShapeTree(this, nullptr);
250 gse->Write(name);
251}
252
253////////////////////////////////////////////////////////////////////////////////
254/// Export this shape and its descendants into a geoshape-extract.
255
257 REveGeoShapeExtract* parent)
258{
259 REveGeoShapeExtract* she = new REveGeoShapeExtract(gsre->GetCName(), gsre->GetCTitle());
260 she->SetTrans(gsre->RefMainTrans().Array());
261 {
262 Int_t ci = gsre->GetFillColor();
263 TColor *c = gROOT->GetColor(ci);
264 Float_t rgba[4] = { 1, 0, 0, Float_t(1 - gsre->GetMainTransparency()/100.) };
265 if (c)
266 {
267 rgba[0] = c->GetRed();
268 rgba[1] = c->GetGreen();
269 rgba[2] = c->GetBlue();
270 }
271 she->SetRGBA(rgba);
272 }
273 {
274 Int_t ci = gsre->GetLineColor();
275 TColor *c = gROOT->GetColor(ci);
276 Float_t rgba[4] = { 1, 0, 0, 1 };
277 if (c)
278 {
279 rgba[0] = c->GetRed();
280 rgba[1] = c->GetGreen();
281 rgba[2] = c->GetBlue();
282 }
283 she->SetRGBALine(rgba);
284 }
285 she->SetRnrSelf(gsre->GetRnrSelf());
286 she->SetRnrElements(gsre->GetRnrChildren());
287 she->SetRnrFrame(gsre->GetDrawFrame());
288 she->SetMiniFrame(gsre->GetMiniFrame());
289 she->SetShape(gsre->GetShape());
290 if (gsre->HasChildren())
291 {
292 TList* ele = new TList();
293 she->SetElements(ele);
294 she->GetElements()->SetOwner(true);
295
296 for (auto &c: gsre->RefChildren())
297 DumpShapeTree(dynamic_cast<REveGeoShape *>(c), she);
298 }
299 if (parent)
300 parent->GetElements()->Add(she);
301
302 return she;
303}
304
305////////////////////////////////////////////////////////////////////////////////
306/// Import a shape extract 'gse' under element 'parent'.
307
309 REveElement* parent)
310{
312 REveGeoShape* gsre = SubImportShapeExtract(gse, parent);
313 gsre->StampObjProps();
314 return gsre;
315}
316
317////////////////////////////////////////////////////////////////////////////////
318/// Recursive version for importing a shape extract tree.
319
321 REveElement* parent)
322{
323 REveGeoShape* gsre = new REveGeoShape(gse->GetName(), gse->GetTitle());
324 gsre->RefMainTrans().SetFromArray(gse->GetTrans());
325 const Float_t* rgba = gse->GetRGBA();
326 gsre->SetMainColorRGB(rgba[0], rgba[1], rgba[2]);
327 gsre->SetMainAlpha(rgba[3]);
328 rgba = gse->GetRGBALine();
329 gsre->SetLineColor(TColor::GetColor(rgba[0], rgba[1], rgba[2]));
330 gsre->SetRnrSelf(gse->GetRnrSelf());
331 gsre->SetRnrChildren(gse->GetRnrElements());
332 gsre->SetDrawFrame(gse->GetRnrFrame());
333 gsre->SetMiniFrame(gse->GetMiniFrame());
334 gsre->SetShape(gse->GetShape());
335
336 if (parent)
337 parent->AddElement(gsre);
338
339 if (gse->HasElements())
340 {
341 TIter next(gse->GetElements());
343 while ((chld = (REveGeoShapeExtract*) next()) != nullptr)
344 SubImportShapeExtract(chld, gsre);
345 }
346
347 return gsre;
348}
349
350////////////////////////////////////////////////////////////////////////////////
351/// Return class for projected objects:
352/// - 2D projections: REvePolygonSetProjected,
353/// - 3D projections: REveGeoShapeProjected.
354/// Virtual from REveProjectable.
355
357{
358 if (p->Is2D())
359 return TClass::GetClass<REvePolygonSetProjected>();
360 else
361 return TClass::GetClass<REveGeoShapeProjected>();
362}
363
364////////////////////////////////////////////////////////////////////////////////
365/// Create a TBuffer3D suitable for presentation of the shape.
366/// Transformation matrix is also applied.
367
368std::unique_ptr<TBuffer3D> REveGeoShape::MakeBuffer3D()
369{
370 std::unique_ptr<TBuffer3D> buff;
371
372 if (!fShape) return buff;
373
374 if (dynamic_cast<TGeoShapeAssembly*>(fShape)) {
375 // TGeoShapeAssembly makes a bad TBuffer3D.
376 return buff;
377 }
378
380
381 buff.reset(fShape->MakeBuffer3D());
383 if (mx.GetUseTrans()) {
384 Int_t n = buff->NbPnts();
385 Double_t *pnts = buff->fPnts;
386 for(Int_t k = 0; k < n; ++k) {
387 mx.MultiplyIP(&pnts[3*k]);
388 }
389 }
390 return buff;
391}
392
393
394//==============================================================================
395// REveGeoShapeProjected
396//==============================================================================
397
398/** \class REveGeoShapeProjected
399\ingroup REve
400A 3D projected REveGeoShape.
401*/
402
403////////////////////////////////////////////////////////////////////////////////
404/// Constructor.
405
407 REveShape("REveGeoShapeProjected"),
408 fBuff()
409{
410}
411
412////////////////////////////////////////////////////////////////////////////////
413/// Destructor.
414
416{
417 /// should be here because of TBuffer3D destructor
418}
419
420
421////////////////////////////////////////////////////////////////////////////////
422/// This should never be called as this class is only used for 3D
423/// projections.
424/// The implementation is required as this metod is abstract.
425/// Just emits a warning if called.
426
428{
429 Warning("SetDepthLocal", "This function only exists to fulfill an abstract interface.");
430}
431
432////////////////////////////////////////////////////////////////////////////////
433/// This is virtual method from base-class REveProjected.
434
436 REveProjectable* model)
437{
439
440 REveGeoShape* gre = dynamic_cast<REveGeoShape*>(fProjectable);
441 CopyVizParams(gre);
442}
443
444////////////////////////////////////////////////////////////////////////////////
445/// This is virtual method from base-class REveProjected.
446
448{
449 REveGeoShape *gre = dynamic_cast<REveGeoShape*>(fProjectable);
451
452 fBuff = gre->MakeBuffer3D();
453
454 if (fBuff)
455 {
457
458 Double_t *p = fBuff->fPnts;
459 for (UInt_t i = 0; i < fBuff->NbPnts(); ++i, p+=3)
460 {
461 prj->ProjectPointdv(p, 0);
462 }
463 }
464
465 ResetBBox();
466}
467
468////////////////////////////////////////////////////////////////////////////////
469/// Override of virtual method from TAttBBox.
470
472{
473 if (fBuff && fBuff->NbPnts() > 0)
474 {
475 BBoxInit();
476
477 Double_t *p = fBuff->fPnts;
478 for (UInt_t i = 0; i < fBuff->NbPnts(); ++i, p+=3)
479 {
480 BBoxCheckPoint(p[0], p[1], p[2]);
481 }
482 }
483 else
484 {
485 BBoxZero();
486 }
487}
dims_t fShape
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
float Float_t
Definition RtypesCore.h:57
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char mx
char name[80]
Definition TGX11.cxx:110
R__EXTERN TGeoManager * gGeoManager
R__EXTERN TGeoIdentity * gGeoIdentity
Definition TGeoMatrix.h:537
#define gROOT
Definition TROOT.h:406
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:87
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.
~REveGeoShape() override
Destructor.
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:
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.
void BuildRenderData() override
Crates 3D point array for rendering.
static TGeoHMatrix * GetGeoHMatrixIdentity()
Return static identity matrix in homogeneous representation.
REveGeoShape(const REveGeoShape &)=delete
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.
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.
void BBoxCheckPoint(Float_t x, Float_t y, Float_t z)
Definition TAttBBox.h:69
void ResetBBox()
Definition TAttBBox.h:57
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
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
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:21
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:1920
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
virtual const Double_t * GetOrigin() const
Definition TGeoBBox.h:82
virtual Double_t GetDX() const
Definition TGeoBBox.h:79
virtual Double_t GetDZ() const
Definition TGeoBBox.h:81
virtual Double_t GetDY() const
Definition TGeoBBox.h:80
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:458
An identity transformation.
Definition TGeoMatrix.h:406
The manager class for any TGeo geometry.
Definition TGeoManager.h:44
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:146
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:83
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
virtual void SetNameTitle(const char *name, const char *title)
Set all the TNamed parameters (name and title).
Definition TNamed.cxx:154
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition TObject.cxx:462
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:886
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition TObject.cxx:797
const Int_t n
Definition legend1.C:16