forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TEveGeoShape.cxx
537 lines (449 loc) · 15.8 KB
/
TEveGeoShape.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// @(#)root/eve:$Id$
// Author: Matevz Tadel 2007
/*************************************************************************
* Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include "TEveGeoShape.h"
#include "TEveTrans.h"
#include "TEveManager.h"
#include "TEvePolygonSetProjected.h"
#include "TEveProjections.h"
#include "TEveProjectionManager.h"
#include "TEveGeoShapeExtract.h"
#include "TEveGeoPolyShape.h"
#include "TROOT.h"
#include "TBuffer3D.h"
#include "TBuffer3DTypes.h"
#include "TVirtualViewer3D.h"
#include "TColor.h"
#include "TFile.h"
#include "TGeoShape.h"
#include "TGeoVolume.h"
#include "TGeoNode.h"
#include "TGeoShapeAssembly.h"
#include "TGeoCompositeShape.h"
#include "TGeoBoolNode.h"
#include "TGeoManager.h"
#include "TGeoMatrix.h"
#include "TVirtualGeoPainter.h"
namespace
{
TGeoManager* init_geo_mangeur()
{
// Create a phony geo manager that can be used for storing free
// shapes. Otherwise shapes register themselves to current
// geo-manager (or even create one).
TGeoManager *old = gGeoManager;
TGeoIdentity *old_id = gGeoIdentity;
gGeoManager = 0;
TGeoManager* mgr = new TGeoManager();
mgr->SetNameTitle("TEveGeoShape::fgGeoMangeur",
"Static geo manager used for wrapped TGeoShapes.");
gGeoIdentity = new TGeoIdentity("Identity");
gGeoManager = old;
gGeoIdentity = old_id;
return mgr;
}
TGeoHMatrix localGeoHMatrixIdentity;
}
/** \class TEveGeoShape
\ingroup TEve
Wrapper for TGeoShape with absolute positioning and color
attributes allowing display of extracted TGeoShape's (without an
active TGeoManager) and simplified geometries (needed for non-linear
projections).
TGeoCompositeShapes and TGeoAssemblies are supported.
If fNSegments data-member is < 2 (0 by default), the default number of
segments is used for tesselation and special GL objects are
instantiated for selected shapes (spheres, tubes). If fNSegments is > 2,
it gets forwarded to geo-manager and this tesselation detail is
used when creating the buffer passed to GL.
*/
ClassImp(TEveGeoShape);
TGeoManager* TEveGeoShape::fgGeoMangeur = init_geo_mangeur();
////////////////////////////////////////////////////////////////////////////////
/// Return static geo-manager that is used internally to make shapes
/// lead a happy life.
/// Set gGeoManager to this object when creating TGeoShapes to be
/// passed into TEveGeoShapes.
TGeoManager* TEveGeoShape::GetGeoMangeur()
{
return fgGeoMangeur;
}
////////////////////////////////////////////////////////////////////////////////
/// Return static identity matrix in homogeneous representation.
/// This is needed because TGeoCompositeShape::PaintComposite()
/// assumes TGeoShape::fgTransform is a TGeoHMatrix and we need to pass in
/// an identity matrix when painting a composite shape.
TGeoHMatrix* TEveGeoShape::GetGeoHMatrixIdentity()
{
return &localGeoHMatrixIdentity;
}
////////////////////////////////////////////////////////////////////////////////
/// Constructor.
TEveGeoShape::TEveGeoShape(const char* name, const char* title) :
TEveShape (name, title),
fNSegments (0),
fShape (0),
fCompositeShape (0)
{
InitMainTrans();
}
////////////////////////////////////////////////////////////////////////////////
/// Destructor.
TEveGeoShape::~TEveGeoShape()
{
SetShape(0);
}
////////////////////////////////////////////////////////////////////////////////
/// Create derived TEveGeoShape form a TGeoCompositeShape.
TGeoShape* TEveGeoShape::MakePolyShape()
{
return TEveGeoPolyShape::Construct(fCompositeShape, fNSegments);
}
////////////////////////////////////////////////////////////////////////////////
/// Set number of segments.
void TEveGeoShape::SetNSegments(Int_t s)
{
if (s != fNSegments && fCompositeShape != 0)
{
delete fShape;
fShape = MakePolyShape();
}
fNSegments = s;
}
////////////////////////////////////////////////////////////////////////////////
/// Set TGeoShape shown by this object.
///
/// The shape is owned by TEveGeoShape but TGeoShape::fUniqueID is
/// used for reference counting so you can pass the same shape to
/// several TEveGeoShapes.
///
/// If it if is taken from an existing TGeoManager, manually
/// increase the fUniqueID before passing it to TEveGeoShape.
void TEveGeoShape::SetShape(TGeoShape* s)
{
TEveGeoManagerHolder gmgr(fgGeoMangeur);
if (fCompositeShape)
{
delete fShape;
fShape = fCompositeShape;
}
if (fShape)
{
fShape->SetUniqueID(fShape->GetUniqueID() - 1);
if (fShape->GetUniqueID() == 0)
{
delete fShape;
}
}
fShape = s;
if (fShape)
{
fShape->SetUniqueID(fShape->GetUniqueID() + 1);
fCompositeShape = dynamic_cast<TGeoCompositeShape*>(fShape);
if (fCompositeShape)
{
fShape = MakePolyShape();
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Compute bounding-box.
void TEveGeoShape::ComputeBBox()
{
TGeoBBox *bb = dynamic_cast<TGeoBBox*>(fShape);
if (bb)
{
BBoxInit();
const Double_t *o = bb->GetOrigin();
BBoxCheckPoint(o[0] - bb->GetDX(), o[0] - bb->GetDY(), o[0] - bb->GetDZ());
BBoxCheckPoint(o[0] + bb->GetDX(), o[0] + bb->GetDY(), o[0] + bb->GetDZ());
}
else
{
BBoxZero();
}
}
////////////////////////////////////////////////////////////////////////////////
/// Paint object.
void TEveGeoShape::Paint(Option_t* /*option*/)
{
static const TEveException eh("TEveGeoShape::Paint ");
if (fShape == 0)
return;
TEveGeoManagerHolder gmgr(fgGeoMangeur, fNSegments);
if (fCompositeShape)
{
Double_t halfLengths[3] = { fCompositeShape->GetDX(), fCompositeShape->GetDY(), fCompositeShape->GetDZ() };
TBuffer3D buff(TBuffer3DTypes::kComposite);
buff.fID = this;
buff.fColor = GetMainColor();
buff.fTransparency = GetMainTransparency();
RefMainTrans().SetBuffer3D(buff);
buff.fLocalFrame = kTRUE; // Always enforce local frame (no geo manager).
buff.SetAABoundingBox(fCompositeShape->GetOrigin(), halfLengths);
buff.SetSectionsValid(TBuffer3D::kCore|TBuffer3D::kBoundingBox);
Bool_t paintComponents = kTRUE;
// Start a composite shape, identified by this buffer
if (TBuffer3D::GetCSLevel() == 0)
paintComponents = gPad->GetViewer3D()->OpenComposite(buff);
TBuffer3D::IncCSLevel();
// Paint the boolean node - will add more buffers to viewer
TGeoMatrix *gst = TGeoShape::GetTransform();
TGeoShape::SetTransform(TEveGeoShape::GetGeoHMatrixIdentity());
if (paintComponents) fCompositeShape->GetBoolNode()->Paint("");
TGeoShape::SetTransform(gst);
// Close the composite shape
if (TBuffer3D::DecCSLevel() == 0)
gPad->GetViewer3D()->CloseComposite();
}
else
{
TBuffer3D& buff = (TBuffer3D&) fShape->GetBuffer3D
(TBuffer3D::kCore, kFALSE);
buff.fID = this;
buff.fColor = GetMainColor();
buff.fTransparency = GetMainTransparency();
RefMainTrans().SetBuffer3D(buff);
buff.fLocalFrame = kTRUE; // Always enforce local frame (no geo manager).
Int_t sections = TBuffer3D::kBoundingBox | TBuffer3D::kShapeSpecific;
if (fNSegments > 2)
sections |= TBuffer3D::kRawSizes | TBuffer3D::kRaw;
fShape->GetBuffer3D(sections, kTRUE);
Int_t reqSec = gPad->GetViewer3D()->AddObject(buff);
if (reqSec != TBuffer3D::kNone) {
// This shouldn't happen, but I suspect it does sometimes.
if (reqSec & TBuffer3D::kCore)
Warning(eh, "Core section required again for shape='%s'. This shouldn't happen.", GetName());
fShape->GetBuffer3D(reqSec, kTRUE);
reqSec = gPad->GetViewer3D()->AddObject(buff);
}
if (reqSec != TBuffer3D::kNone)
Warning(eh, "Extra section required: reqSec=%d, shape=%s.", reqSec, GetName());
}
}
////////////////////////////////////////////////////////////////////////////////
/// Save the shape tree as TEveGeoShapeExtract.
/// File is always recreated.
/// This function is obsolete, use SaveExtractInstead().
void TEveGeoShape::Save(const char* file, const char* name)
{
Warning("Save()", "This function is deprecated, use SaveExtract() instead.");
SaveExtract(file, name);
}
////////////////////////////////////////////////////////////////////////////////
/// Save the shape tree as TEveGeoShapeExtract.
/// File is always recreated.
void TEveGeoShape::SaveExtract(const char* file, const char* name)
{
TEveGeoShapeExtract* gse = DumpShapeTree(this, 0);
TFile f(file, "RECREATE");
gse->Write(name);
f.Close();
}
////////////////////////////////////////////////////////////////////////////////
/// Write the shape tree as TEveGeoShapeExtract to current directory.
void TEveGeoShape::WriteExtract(const char* name)
{
TEveGeoShapeExtract* gse = DumpShapeTree(this, 0);
gse->Write(name);
}
////////////////////////////////////////////////////////////////////////////////
/// Export this shape and its descendants into a geoshape-extract.
TEveGeoShapeExtract* TEveGeoShape::DumpShapeTree(TEveGeoShape* gsre,
TEveGeoShapeExtract* parent)
{
TEveGeoShapeExtract* she = new TEveGeoShapeExtract(gsre->GetName(), gsre->GetTitle());
she->SetTrans(gsre->RefMainTrans().Array());
{
Int_t ci = gsre->GetFillColor();
TColor *c = gROOT->GetColor(ci);
Float_t rgba[4] = { 1, 0, 0, Float_t(1 - gsre->GetMainTransparency()/100.) };
if (c)
{
rgba[0] = c->GetRed();
rgba[1] = c->GetGreen();
rgba[2] = c->GetBlue();
}
she->SetRGBA(rgba);
}
{
Int_t ci = gsre->GetLineColor();
TColor *c = gROOT->GetColor(ci);
Float_t rgba[4] = { 1, 0, 0, 1 };
if (c)
{
rgba[0] = c->GetRed();
rgba[1] = c->GetGreen();
rgba[2] = c->GetBlue();
}
she->SetRGBALine(rgba);
}
she->SetRnrSelf(gsre->GetRnrSelf());
she->SetRnrElements(gsre->GetRnrChildren());
she->SetRnrFrame(gsre->GetDrawFrame());
she->SetMiniFrame(gsre->GetMiniFrame());
she->SetShape(gsre->GetShape());
if (gsre->HasChildren())
{
TList* ele = new TList();
she->SetElements(ele);
she->GetElements()->SetOwner(true);
TEveElement::List_i i = gsre->BeginChildren();
while (i != gsre->EndChildren()) {
TEveGeoShape* l = dynamic_cast<TEveGeoShape*>(*i);
DumpShapeTree(l, she);
i++;
}
}
if (parent)
parent->GetElements()->Add(she);
return she;
}
////////////////////////////////////////////////////////////////////////////////
/// Import a shape extract 'gse' under element 'parent'.
TEveGeoShape* TEveGeoShape::ImportShapeExtract(TEveGeoShapeExtract* gse,
TEveElement* parent)
{
TEveGeoManagerHolder gmgr(fgGeoMangeur);
TEveManager::TRedrawDisabler redrawOff(gEve);
TEveGeoShape* gsre = SubImportShapeExtract(gse, parent);
gsre->ElementChanged();
return gsre;
}
////////////////////////////////////////////////////////////////////////////////
/// Recursive version for importing a shape extract tree.
TEveGeoShape* TEveGeoShape::SubImportShapeExtract(TEveGeoShapeExtract* gse,
TEveElement* parent)
{
TEveGeoShape* gsre = new TEveGeoShape(gse->GetName(), gse->GetTitle());
gsre->RefMainTrans().SetFromArray(gse->GetTrans());
const Float_t* rgba = gse->GetRGBA();
gsre->SetMainColorRGB(rgba[0], rgba[1], rgba[2]);
gsre->SetMainAlpha(rgba[3]);
rgba = gse->GetRGBALine();
gsre->SetLineColor(TColor::GetColor(rgba[0], rgba[1], rgba[2]));
gsre->SetRnrSelf(gse->GetRnrSelf());
gsre->SetRnrChildren(gse->GetRnrElements());
gsre->SetDrawFrame(gse->GetRnrFrame());
gsre->SetMiniFrame(gse->GetMiniFrame());
gsre->SetShape(gse->GetShape());
if (parent)
parent->AddElement(gsre);
if (gse->HasElements())
{
TIter next(gse->GetElements());
TEveGeoShapeExtract* chld;
while ((chld = (TEveGeoShapeExtract*) next()) != 0)
SubImportShapeExtract(chld, gsre);
}
return gsre;
}
////////////////////////////////////////////////////////////////////////////////
/// Return class for projected objects:
/// - 2D projections: TEvePolygonSetProjected,
/// - 3D projections: TEveGeoShapeProjected.
/// Virtual from TEveProjectable.
TClass* TEveGeoShape::ProjectedClass(const TEveProjection* p) const
{
if (p->Is2D())
return TEvePolygonSetProjected::Class();
else
return TEveGeoShapeProjected::Class();
}
////////////////////////////////////////////////////////////////////////////////
/// Create a TBuffer3D suitable for presentation of the shape.
/// Transformation matrix is also applied.
TBuffer3D* TEveGeoShape::MakeBuffer3D()
{
if (fShape == 0) return 0;
if (dynamic_cast<TGeoShapeAssembly*>(fShape)) {
// TGeoShapeAssembly makes a bad TBuffer3D.
return 0;
}
TEveGeoManagerHolder gmgr(fgGeoMangeur, fNSegments);
TBuffer3D* buff = fShape->MakeBuffer3D();
TEveTrans& mx = RefMainTrans();
if (mx.GetUseTrans())
{
Int_t n = buff->NbPnts();
Double_t* pnts = buff->fPnts;
for(Int_t k = 0; k < n; ++k)
{
mx.MultiplyIP(&pnts[3*k]);
}
}
return buff;
}
/** \class TEveGeoShapeProjected
\ingroup TEve
A 3D projected TEveGeoShape.
*/
ClassImp(TEveGeoShapeProjected);
////////////////////////////////////////////////////////////////////////////////
/// Constructor.
TEveGeoShapeProjected::TEveGeoShapeProjected() :
TEveShape("TEveGeoShapeProjected"),
fBuff(0)
{
}
////////////////////////////////////////////////////////////////////////////////
/// This should never be called as this class is only used for 3D
/// projections.
/// The implementation is required as this metod is abstract.
/// Just emits a warning if called.
void TEveGeoShapeProjected::SetDepthLocal(Float_t /*d*/)
{
Warning("SetDepthLocal", "This function only exists to fulfill an abstract interface.");
}
////////////////////////////////////////////////////////////////////////////////
/// This is virtual method from base-class TEveProjected.
void TEveGeoShapeProjected::SetProjection(TEveProjectionManager* mng,
TEveProjectable* model)
{
TEveProjected::SetProjection(mng, model);
TEveGeoShape* gre = dynamic_cast<TEveGeoShape*>(fProjectable);
CopyVizParams(gre);
}
////////////////////////////////////////////////////////////////////////////////
/// This is virtual method from base-class TEveProjected.
void TEveGeoShapeProjected::UpdateProjection()
{
TEveGeoShape *gre = dynamic_cast<TEveGeoShape*>(fProjectable);
TEveProjection *prj = fManager->GetProjection();
delete fBuff;
fBuff = gre->MakeBuffer3D();
if (fBuff)
{
fBuff->SetSectionsValid(TBuffer3D::kCore | TBuffer3D::kRawSizes | TBuffer3D::kRaw);
Double_t *p = fBuff->fPnts;
for (UInt_t i = 0; i < fBuff->NbPnts(); ++i, p+=3)
{
prj->ProjectPointdv(p, 0);
}
}
ResetBBox();
}
////////////////////////////////////////////////////////////////////////////////
/// Override of virtual method from TAttBBox.
void TEveGeoShapeProjected::ComputeBBox()
{
if (fBuff && fBuff->NbPnts() > 0)
{
BBoxInit();
Double_t *p = fBuff->fPnts;
for (UInt_t i = 0; i < fBuff->NbPnts(); ++i, p+=3)
{
BBoxCheckPoint(p[0], p[1], p[2]);
}
}
else
{
BBoxZero();
}
}