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{
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
51 gGeoManager = nullptr;
53 mgr->SetNameTitle("REveGeoShape::fgGeoManager",
54 "Static geo manager used for wrapped TGeoShapes.");
55 gGeoIdentity = new TGeoIdentity("Identity");
56 gGeoManager = old;
58 return mgr;
59 }
60
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
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
141
142////////////////////////////////////////////////////////////////////////////////
143/// Crates 3D point array for rendering.
144
146{
147 if (!fShape) return;
148
149 // Tesselate first, a shape without faces has nothing that can be rendered
150 // as a mesh. Composite shapes are already converted in SetShape().
151 REveGeoPolyShape *egps = nullptr;
152 std::unique_ptr<REveGeoPolyShape> tmp_egps;
153
154 if (fCompositeShape) {
155 egps = dynamic_cast<REveGeoPolyShape *>(fShape);
156 } else {
158 tmp_egps = std::make_unique<REveGeoPolyShape>();
159 tmp_egps->BuildFromShape(fShape, fNSegments);
160 egps = tmp_egps.get();
161 }
162
163 if (!egps || egps->GetNumFaces() == 0) return;
164
165 fRenderData = std::make_unique<REveRenderData>("makeEveGeoShape");
167
168 egps->FillRenderData(*fRenderData);
169}
170
171////////////////////////////////////////////////////////////////////////////////
172/// Set number of segments.
173
175{
176 if (s != fNSegments && fCompositeShape != nullptr) {
177 delete fShape;
179 }
180 fNSegments = s;
181}
182
183////////////////////////////////////////////////////////////////////////////////
184/// Set TGeoShape shown by this object.
185///
186/// The shape is owned by REveGeoShape but TGeoShape::fUniqueID is
187/// used for reference counting so you can pass the same shape to
188/// several EveGeoShapes.
189///
190/// If it if is taken from an existing TGeoManager, manually
191/// increase the fUniqueID before passing it to REveGeoShape.
192
194{
196
197 if (fCompositeShape) {
198 delete fShape;
200 }
201
202 if (fShape) {
204 if (fShape->GetUniqueID() == 0) {
205 delete fShape;
206 }
207 }
208
209 fShape = s;
210
211 if (fShape) {
213 fCompositeShape = dynamic_cast<TGeoCompositeShape *>(fShape);
214 if (fCompositeShape) {
216 }
217 }
218}
219
220////////////////////////////////////////////////////////////////////////////////
221/// Compute bounding-box.
222
224{
225 TGeoBBox *bb = dynamic_cast<TGeoBBox *>(fShape);
226 if (bb) {
227 BBoxInit();
228 const Double_t *o = bb->GetOrigin();
229 BBoxCheckPoint(o[0] - bb->GetDX(), o[0] - bb->GetDY(), o[0] - bb->GetDZ());
230 BBoxCheckPoint(o[0] + bb->GetDX(), o[0] + bb->GetDY(), o[0] + bb->GetDZ());
231 } else {
232 BBoxZero();
233 }
234}
235
236////////////////////////////////////////////////////////////////////////////////
237/// Save the shape tree as REveGeoShapeExtract.
238/// File is always recreated.
239
240void REveGeoShape::SaveExtract(const char* file, const char* name)
241{
242 // FIXME: ownership
243 REveGeoShapeExtract* gse = DumpShapeTree(this, nullptr);
244
245 TFile f(file, "RECREATE");
246 gse->Write(name);
247 f.Close();
248}
249
250////////////////////////////////////////////////////////////////////////////////
251/// Write the shape tree as REveGeoShapeExtract to current directory.
252/// FIXME: SL: no any write into gDirectory
253
255{
256 // FIXME: ownership
257 REveGeoShapeExtract* gse = DumpShapeTree(this, nullptr);
258 gse->Write(name);
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// Export this shape and its descendants into a geoshape-extract.
263
265 REveGeoShapeExtract* parent)
266{
267 REveGeoShapeExtract* she = new REveGeoShapeExtract(gsre->GetCName(), gsre->GetCTitle());
268 she->SetTrans(gsre->RefMainTrans().Array());
269 {
270 Int_t ci = gsre->GetFillColor();
271 TColor *c = gROOT->GetColor(ci);
272 Float_t rgba[4] = { 1, 0, 0, Float_t(1 - gsre->GetMainTransparency()/100.) };
273 if (c)
274 {
275 rgba[0] = c->GetRed();
276 rgba[1] = c->GetGreen();
277 rgba[2] = c->GetBlue();
278 }
279 she->SetRGBA(rgba);
280 }
281 {
282 Int_t ci = gsre->GetLineColor();
283 TColor *c = gROOT->GetColor(ci);
284 Float_t rgba[4] = { 1, 0, 0, 1 };
285 if (c)
286 {
287 rgba[0] = c->GetRed();
288 rgba[1] = c->GetGreen();
289 rgba[2] = c->GetBlue();
290 }
291 she->SetRGBALine(rgba);
292 }
293 she->SetRnrSelf(gsre->GetRnrSelf());
294 she->SetRnrElements(gsre->GetRnrChildren());
295 she->SetRnrFrame(gsre->GetDrawFrame());
296 she->SetMiniFrame(gsre->GetMiniFrame());
297 she->SetShape(gsre->GetShape());
298 if (gsre->HasChildren())
299 {
300 TList* ele = new TList();
301 she->SetElements(ele);
302 she->GetElements()->SetOwner(true);
303
304 for (auto &c: gsre->RefChildren())
305 DumpShapeTree(dynamic_cast<REveGeoShape *>(c), she);
306 }
307 if (parent)
308 parent->GetElements()->Add(she);
309
310 return she;
311}
312
313////////////////////////////////////////////////////////////////////////////////
314/// Import a shape extract 'gse' under element 'parent'.
315
324
325////////////////////////////////////////////////////////////////////////////////
326/// Recursive version for importing a shape extract tree.
327
329 REveElement* parent)
330{
331 REveGeoShape* gsre = new REveGeoShape(gse->GetName(), gse->GetTitle());
332 gsre->RefMainTrans().SetFromArray(gse->GetTrans());
333 const Float_t* rgba = gse->GetRGBA();
334 gsre->SetMainColorRGB(rgba[0], rgba[1], rgba[2]);
335 gsre->SetMainAlpha(rgba[3]);
336 rgba = gse->GetRGBALine();
337 gsre->SetLineColor(TColor::GetColor(rgba[0], rgba[1], rgba[2]));
338 gsre->SetRnrSelf(gse->GetRnrSelf());
339 gsre->SetRnrChildren(gse->GetRnrElements());
340 gsre->SetDrawFrame(gse->GetRnrFrame());
341 gsre->SetMiniFrame(gse->GetMiniFrame());
342 gsre->SetShape(gse->GetShape());
343
344 if (parent)
345 parent->AddElement(gsre);
346
347 if (gse->HasElements())
348 {
349 TIter next(gse->GetElements());
351 while ((chld = (REveGeoShapeExtract*) next()) != nullptr)
353 }
354
355 return gsre;
356}
357
358////////////////////////////////////////////////////////////////////////////////
359/// Return class for projected objects:
360/// - 2D projections: REvePolygonSetProjected,
361/// - 3D projections: REveGeoShapeProjected.
362/// Virtual from REveProjectable.
363
365{
366 if (p->Is2D())
367 return TClass::GetClass<REvePolygonSetProjected>();
368 else
369 return TClass::GetClass<REveGeoShapeProjected>();
370}
371
372////////////////////////////////////////////////////////////////////////////////
373/// Create a TBuffer3D suitable for presentation of the shape.
374/// Transformation matrix is also applied.
375
376std::unique_ptr<TBuffer3D> REveGeoShape::MakeBuffer3D()
377{
378 std::unique_ptr<TBuffer3D> buff;
379
380 if (!fShape) return buff;
381
382 if (dynamic_cast<TGeoShapeAssembly*>(fShape)) {
383 // TGeoShapeAssembly makes a bad TBuffer3D.
384 return buff;
385 }
386
388
389 buff.reset(fShape->MakeBuffer3D());
391 if (mx.GetUseTrans()) {
392 Int_t n = buff->NbPnts();
393 Double_t *pnts = buff->fPnts;
394 for(Int_t k = 0; k < n; ++k) {
395 mx.MultiplyIP(&pnts[3*k]);
396 }
397 }
398 return buff;
399}
400
401
402//==============================================================================
403// REveGeoShapeProjected
404//==============================================================================
405
406/** \class REveGeoShapeProjected
407\ingroup REve
408A 3D projected REveGeoShape.
409*/
410
411////////////////////////////////////////////////////////////////////////////////
412/// Constructor.
413
415 REveShape("REveGeoShapeProjected"),
416 fBuff()
417{
418}
419
420////////////////////////////////////////////////////////////////////////////////
421/// Destructor.
422
424{
425 /// should be here because of TBuffer3D destructor
426}
427
428
429////////////////////////////////////////////////////////////////////////////////
430/// This should never be called as this class is only used for 3D
431/// projections.
432/// The implementation is required as this metod is abstract.
433/// Just emits a warning if called.
434
436{
437 Warning("SetDepthLocal", "This function only exists to fulfill an abstract interface.");
438}
439
440////////////////////////////////////////////////////////////////////////////////
441/// This is virtual method from base-class REveProjected.
442
451
452////////////////////////////////////////////////////////////////////////////////
453/// This is virtual method from base-class REveProjected.
454
456{
457 REveGeoShape *gre = dynamic_cast<REveGeoShape*>(fProjectable);
459
460 fBuff = gre->MakeBuffer3D();
461
462 if (fBuff)
463 {
465
466 Double_t *p = fBuff->fPnts;
467 for (UInt_t i = 0; i < fBuff->NbPnts(); ++i, p+=3)
468 {
469 prj->ProjectPointdv(p, 0);
470 }
471 }
472
473 ResetBBox();
474}
475
476////////////////////////////////////////////////////////////////////////////////
477/// Override of virtual method from TAttBBox.
478
480{
481 if (fBuff && fBuff->NbPnts() > 0)
482 {
483 BBoxInit();
484
485 Double_t *p = fBuff->fPnts;
486 for (UInt_t i = 0; i < fBuff->NbPnts(); ++i, p+=3)
487 {
488 BBoxCheckPoint(p[0], p[1], p[2]);
489 }
490 }
491 else
492 {
493 BBoxZero();
494 }
495}
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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
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:142
R__EXTERN TGeoManager * gGeoManager
R__EXTERN TGeoIdentity * gGeoIdentity
Definition TGeoMatrix.h:538
#define gROOT
Definition TROOT.h:417
virtual REveTrans & RefMainTrans()
Return reference to main transformation.
virtual void AddElement(REveElement *el)
Add el to the list of children.
virtual void InitMainTrans(Bool_t can_edit=kTRUE)
Initialize the main transformation to identity matrix.
std::unique_ptr< REveRenderData > fRenderData
! Vertex / normal / triangle index information for rendering.
virtual void BuildRenderData()
Write transformation Matrix to render data.
REveGeoManagerHolder Exception-safe global variable holders.
Definition REveUtil.hxx:90
void ComputeBBox() override
Override of virtual method from TAttBBox.
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.
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
! 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.
REveProjection Base for specific classes that implement non-linear projections.
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 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:41
void BBoxInit(Float_t infinity=1e6)
Allocate and prepare for incremental filling.
Definition TAttBBox.cxx:28
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
The color creation and management class.
Definition TColor.h:22
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:1939
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
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
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition TObject.cxx:479
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition TObject.cxx:897
const Int_t n
Definition legend1.C:16
Namespace for ROOT features in testing.
Definition TROOT.h:100