]> git.uio.no Git - u/mrichter/AliRoot.git/blame - EVE/Reve/GeoNode.cxx
Implement load/save of GeoNodeRnrEl and GeoShapeRnrEl. GeoNodeRnrEl
[u/mrichter/AliRoot.git] / EVE / Reve / GeoNode.cxx
CommitLineData
5a5a1232 1// $Header$
2
3#include "GeoNode.h"
32e219c2 4#include <Reve/ReveManager.h>
712d9715 5#include <Reve/NLTPolygonSet.h>
5a5a1232 6
5b3adb7e 7#include "TGeoShapeExtract.h"
8
5a5a1232 9#include <TPad.h>
5b3adb7e 10#include <TBuffer3D.h>
11#include <TVirtualViewer3D.h>
5a5a1232 12#include <TColor.h>
01fa5a07 13#include <TFile.h>
5a5a1232 14
5b3adb7e 15#include <TGeoShape.h>
5a5a1232 16#include <TGeoVolume.h>
17#include <TGeoNode.h>
32e219c2 18#include <TGeoShapeAssembly.h>
5a5a1232 19#include <TGeoManager.h>
24e2ff4a 20#include <TVirtualGeoPainter.h>
5a5a1232 21
22using namespace Reve;
712d9715 23
5a5a1232 24
25//______________________________________________________________________
26// GeoNodeRnrEl
27//
28
29ClassImp(GeoNodeRnrEl)
30
31GeoNodeRnrEl::GeoNodeRnrEl(TGeoNode* node) :
01fa5a07 32 RenderElement(),
7d42b6c2 33 TObject(),
5a5a1232 34 fNode(node)
35{
36 // Hack!! Should use cint to retrieve TAttLine::fLineColor offset.
37 char* l = (char*) dynamic_cast<TAttLine*>(node->GetVolume());
38 SetMainColorPtr((Color_t*)(l + sizeof(void*)));
39
5b457dfa 40 fRnrSelf = fNode->TGeoAtt::IsVisible();
5a5a1232 41}
42
43const Text_t* GeoNodeRnrEl::GetName() const { return fNode->GetName(); }
44const Text_t* GeoNodeRnrEl::GetTitle() const { return fNode->GetTitle(); }
45
46/**************************************************************************/
47
48Int_t GeoNodeRnrEl::ExpandIntoListTree(TGListTree* ltree,
49 TGListTreeItem* parent)
50{
51 // Checks if child-nodes have been imported ... imports them if not.
5b457dfa 52 // Then calls RenderElement::ExpandIntoListTree.
5a5a1232 53
7d42b6c2 54 if(fChildren.empty() && fNode->GetVolume()->GetNdaughters() > 0) {
5a5a1232 55 TIter next(fNode->GetVolume()->GetNodes());
56 TGeoNode* dnode;
57 while((dnode = (TGeoNode*) next()) != 0) {
58 GeoNodeRnrEl* node_re = new GeoNodeRnrEl(dnode);
59 AddElement(node_re);
60 }
61 }
5b457dfa 62 return RenderElement::ExpandIntoListTree(ltree, parent);
5a5a1232 63}
64
65/**************************************************************************/
66
44f64ac9 67void GeoNodeRnrEl::SetRnrSelf(Bool_t rnr)
5a5a1232 68{
44f64ac9 69 RenderElement::SetRnrSelf(rnr);
70 fNode->SetVisibility(rnr);
5a5a1232 71}
72
44f64ac9 73void GeoNodeRnrEl::SetRnrChildren(Bool_t rnr)
74{
75 RenderElement::SetRnrChildren(rnr);
76 fNode->VisibleDaughters(rnr);
77}
5a5a1232 78
44f64ac9 79void GeoNodeRnrEl::SetRnrState(Bool_t rnr)
5a5a1232 80{
44f64ac9 81 RenderElement::SetRnrState(rnr);
5a5a1232 82 fNode->SetVisibility(rnr);
44f64ac9 83 fNode->VisibleDaughters(rnr);
5a5a1232 84}
85
86/**************************************************************************/
87
88void GeoNodeRnrEl::SetMainColor(Color_t color)
89{
90 fNode->GetVolume()->SetLineColor(color);
7d42b6c2 91 UpdateItems();
5a5a1232 92}
93
94void GeoNodeRnrEl::SetMainColor(Pixel_t pixel)
95{
96 // This one needed for proper calling via CINT (signals).
97
98 SetMainColor(Color_t(TColor::GetColor(pixel)));
99}
100
101/**************************************************************************/
102
103void GeoNodeRnrEl::UpdateNode(TGeoNode* node)
104{
105 // Updates all reve-browsers having the node in their contents.
106 // All 3D-pads updated if any change found.
107 //
108 // Should (could?) be optimized with some assumptions about
109 // volume/node structure (search for parent, know the same node can not
110 // reoccur on lower level once found).
111
112 static const Exc_t eH("GeoNodeRnrEl::UpdateNode ");
113
114 // printf("%s node %s %p\n", eH.Data(), node->GetName(), node);
115
116 if(fNode == node)
7d42b6c2 117 UpdateItems();
5a5a1232 118
e6ac3950 119 for(List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
5a5a1232 120 ((GeoNodeRnrEl*)(*i))->UpdateNode(node);
121 }
122
123}
124
125void GeoNodeRnrEl::UpdateVolume(TGeoVolume* volume)
126{
127 // Updates all reve-browsers having the volume in their contents.
128 // All 3D-pads updated if any change found.
129 //
130 // Should (could?) be optimized with some assumptions about
131 // volume/node structure (search for parent, know the same node can not
132 // reoccur on lower level once found).
133
134 static const Exc_t eH("GeoNodeRnrEl::UpdateVolume ");
135
136 // printf("%s volume %s %p\n", eH.Data(), volume->GetName(), volume);
137
138 if(fNode->GetVolume() == volume)
7d42b6c2 139 UpdateItems();
5a5a1232 140
e6ac3950 141 for(List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
5a5a1232 142 ((GeoNodeRnrEl*)(*i))->UpdateVolume(volume);
143 }
144}
145
146/**************************************************************************/
147
148void GeoNodeRnrEl::Draw(Option_t* option)
149{
150 TString opt("SAME");
151 opt += option;
152 fNode->GetVolume()->Draw(opt);
153}
154
712d9715 155/**************************************************************************/
156
01fa5a07 157void GeoNodeRnrEl::Save(const char* file, const char* name)
712d9715 158{
01fa5a07 159 TGeoShapeExtract* gse = DumpShapeTree(this, 0, 0);
160
161 TFile f(file, "RECREATE");
162 gse->Write(name);
163 f.Close();
164}
712d9715 165
01fa5a07 166/**************************************************************************/
167
168TGeoShapeExtract* GeoNodeRnrEl::DumpShapeTree(GeoNodeRnrEl* geon, TGeoShapeExtract* parent, Int_t level)
169{
170 printf("dump_shape_tree %s \n", geon->GetName());
171 TGeoNode* tnode = 0;
172 TGeoVolume* tvolume = 0;
173 TGeoShape* tshape = 0;
174
175 tnode = geon->GetNode();
176 if(tnode == 0) {
177 printf("Null node for %s; assuming it's a holder and descending.\n", geon->GetName());
178 goto do_dump;
179 }
180
181 tvolume = tnode->GetVolume();
182 if(tvolume == 0) {
183 printf("Null volume for %s; skipping.\n", geon->GetName());
712d9715 184 return 0;
185 }
186
01fa5a07 187 tshape = tvolume->GetShape();
188
189do_dump:
190 // transformation
191 ZTrans trans;
192 if (parent) if (parent) trans.SetFromArray(parent->GetTrans());
193 TGeoMatrix* gm = tnode->GetMatrix();
194 const Double_t* rm = gm->GetRotationMatrix();
195 const Double_t* tv = gm->GetTranslation();
196 ZTrans t;
197 t(1,1) = rm[0]; t(1,2) = rm[1]; t(1,3) = rm[2];
198 t(2,1) = rm[3]; t(2,2) = rm[4]; t(2,3) = rm[5];
199 t(3,1) = rm[6]; t(3,2) = rm[7]; t(3,3) = rm[8];
200 t(1,4) = tv[0]; t(2,4) = tv[1]; t(3,4) = tv[2];
201 trans *= t;
202
203 TGeoShapeExtract* gse = new TGeoShapeExtract(geon->GetName(), geon->GetTitle());
204 gse->SetTrans(trans.Array());
205 Int_t ci = 0;
206 if(tvolume) ci = tvolume->GetLineColor();
207 TColor* c = gROOT->GetColor(ci);
208 Float_t rgba[4] = {1, 0, 0, 1};
209 if (c) {
210 rgba[0] = c->GetRed();
211 rgba[1] = c->GetGreen();
212 rgba[2] = c->GetBlue();
213 }
214 gse->SetRGBA(rgba);
215 Bool_t rnr = geon->GetRnrSelf();
216 if(level > gGeoManager->GetVisLevel())
217 rnr = kFALSE;
218 gse->SetRnrSelf(rnr);
219 gse->SetRnrElements(geon->GetRnrChildren());
220
221 if(dynamic_cast<TGeoShapeAssembly*>(tshape)){
222 // printf("<TGeoShapeAssembly \n");
223 tshape = 0;
224 }
225 gse->SetShape(tshape);
226 level ++;
227 if ( geon->GetNChildren())
712d9715 228 {
01fa5a07 229 TList* ele = new TList();
230 gse->SetElements(ele);
231 gse->GetElements()->SetOwner(true);
232
233 RenderElement::List_i i = geon->BeginChildren();
234 while (i != geon->EndChildren()) {
235 GeoNodeRnrEl* l = dynamic_cast<GeoNodeRnrEl*>(*i);
236 DumpShapeTree(l, gse, level+1);
237 i++;
238 }
712d9715 239 }
01fa5a07 240
241 if(parent)
242 parent->GetElements()->Add(gse);
243
244 return gse;
712d9715 245}
5a5a1232 246/**************************************************************************/
247//______________________________________________________________________
248// GeoTopNodeRnrEl
249//
24e2ff4a 250// A wrapper over a TGeoNode, possibly displaced with a global
251// trasformation fGlobalTrans (the matrix is owned by this class).
5a5a1232 252/**************************************************************************/
253
254ClassImp(GeoTopNodeRnrEl)
255
256GeoTopNodeRnrEl::GeoTopNodeRnrEl(TGeoManager* manager, TGeoNode* node,
257 Int_t visopt, Int_t vislvl) :
57ad1faf 258 GeoNodeRnrEl (node),
259 fManager (manager),
32e219c2 260 fGlobalTrans (),
57ad1faf 261 fVisOption (visopt),
262 fVisLevel (vislvl)
5a5a1232 263{
5b457dfa 264 fRnrSelf = true;
5a5a1232 265}
266
24e2ff4a 267GeoTopNodeRnrEl::~GeoTopNodeRnrEl()
57ad1faf 268{}
24e2ff4a 269
270/**************************************************************************/
271
57ad1faf 272void GeoTopNodeRnrEl::SetGlobalTrans(const TGeoHMatrix* m)
24e2ff4a 273{
57ad1faf 274 fGlobalTrans.SetFrom(*m);
24e2ff4a 275}
276
57ad1faf 277void GeoTopNodeRnrEl::UseNodeTrans()
24e2ff4a 278{
57ad1faf 279 fGlobalTrans.SetFrom(*fNode->GetMatrix());
24e2ff4a 280}
281
5a5a1232 282/**************************************************************************/
283
284void GeoTopNodeRnrEl::SetVisOption(Int_t visopt)
285{
286 fVisOption = visopt;
287 gReve->Redraw3D();
288}
289
290void GeoTopNodeRnrEl::SetVisLevel(Int_t vislvl)
291{
292 fVisLevel = vislvl;
712d9715 293 gReve->Redraw3D();
5a5a1232 294}
295
296/**************************************************************************/
297
5b457dfa 298void GeoTopNodeRnrEl::SetRnrSelf(Bool_t rnr)
5a5a1232 299{
300 // Revert from GeoNode to back to standard behaviour.
5b457dfa 301 RenderElement::SetRnrSelf(rnr);
5a5a1232 302}
57ad1faf 303
5a5a1232 304/**************************************************************************/
305
306void GeoTopNodeRnrEl::Draw(Option_t* option)
307{
308 AppendPad(option);
309}
310
311void GeoTopNodeRnrEl::Paint(Option_t* option)
312{
5b457dfa 313 if(fRnrSelf) {
5a5a1232 314 gGeoManager = fManager;
315 TVirtualPad* pad = gPad;
316 gPad = 0;
317 TGeoVolume* top_volume = fManager->GetTopVolume();
318 fManager->SetVisOption(fVisOption);
319 fManager->SetVisLevel(fVisLevel);
320 fManager->SetTopVolume(fNode->GetVolume());
321 gPad = pad;
24e2ff4a 322 TVirtualGeoPainter* vgp = fManager->GetGeomPainter();
323 if(vgp != 0) {
57ad1faf 324 TGeoHMatrix geomat;
325 fGlobalTrans.SetGeoHMatrix(geomat);
326 vgp->PaintNode(fNode, option, &geomat);
24e2ff4a 327 }
5a5a1232 328 fManager->SetTopVolume(top_volume);
329 }
330}
331
332/**************************************************************************/
333
334void GeoTopNodeRnrEl::VolumeVisChanged(TGeoVolume* volume)
335{
336 static const Exc_t eH("GeoTopNodeRnrEl::VolumeVisChanged ");
337 printf("%s volume %s %p\n", eH.Data(), volume->GetName(), (void*)volume);
338 UpdateVolume(volume);
339}
340
341void GeoTopNodeRnrEl::VolumeColChanged(TGeoVolume* volume)
342{
343 static const Exc_t eH("GeoTopNodeRnrEl::VolumeColChanged ");
344 printf("%s volume %s %p\n", eH.Data(), volume->GetName(), (void*)volume);
345 UpdateVolume(volume);
346}
347
348void GeoTopNodeRnrEl::NodeVisChanged(TGeoNode* node)
349{
350 static const Exc_t eH("GeoTopNodeRnrEl::NodeVisChanged ");
351 printf("%s node %s %p\n", eH.Data(), node->GetName(), (void*)node);
352 UpdateNode(node);
353}
5b3adb7e 354
355
356/**************************************************************************/
357//______________________________________________________________________
358// GeoShapeRnrEl
359//
360// Minimal shape-wrapper allowing import of stuff from gled and retaining
361// user-set visibility, colors and transparency.
362/**************************************************************************/
363
364ClassImp(GeoShapeRnrEl)
365
366GeoShapeRnrEl::GeoShapeRnrEl(const Text_t* name, const Text_t* title) :
01fa5a07 367 RenderElement(),
5b3adb7e 368 TNamed (name, title),
f36b73bd 369 fHMTrans (),
5b3adb7e 370 fColor (0),
371 fTransparency (0),
372 fShape (0)
712d9715 373{
374 fMainColorPtr = &fColor;
375}
5b3adb7e 376
377GeoShapeRnrEl::~GeoShapeRnrEl()
378{
379 if (fShape) {
380 fShape->SetUniqueID(fShape->GetUniqueID() - 1);
381 if (fShape->GetUniqueID() == 0)
382 delete fShape;
383 }
384}
385
386/**************************************************************************/
387
388void GeoShapeRnrEl::Paint(Option_t* /*option*/)
389{
390 if (fShape == 0)
391 return;
392
393 TBuffer3D& buff = (TBuffer3D&) fShape->GetBuffer3D
394 (TBuffer3D::kCore, false);
395
5b3adb7e 396 buff.fID = this;
397 buff.fColor = fColor;
398 buff.fTransparency = fTransparency;
57ad1faf 399 fHMTrans.SetBuffer3D(buff);
32e219c2 400 buff.fLocalFrame = kTRUE; // Always enforce local frame (no geo manager).
5b3adb7e 401
402 fShape->GetBuffer3D(TBuffer3D::kBoundingBox | TBuffer3D::kShapeSpecific, true);
403
404 Int_t reqSec = gPad->GetViewer3D()->AddObject(buff);
405
406 if (reqSec != TBuffer3D::kNone) {
407 fShape->GetBuffer3D(reqSec, true);
408 reqSec = gPad->GetViewer3D()->AddObject(buff);
409 }
410
411 if (reqSec != TBuffer3D::kNone)
412 printf("spooky reqSec=%d for %s\n", reqSec, GetName());
413}
414
415/**************************************************************************/
416
01fa5a07 417void GeoShapeRnrEl::Save(const char* file, const char* name)
418{
419 TGeoShapeExtract* gse = DumpShapeTree(this, 0);
420
421 TFile f(file, "RECREATE");
422 gse->Write(name);
423 f.Close();
424}
425
426/**************************************************************************/
427
428TGeoShapeExtract* GeoShapeRnrEl::DumpShapeTree(GeoShapeRnrEl* gsre, TGeoShapeExtract* parent)
429{
430 // printf("dump_shape_tree %s \n", gsre->GetName());
431 TGeoShapeExtract* she = new TGeoShapeExtract(gsre->GetName(), gsre->GetTitle());
432 she->SetTrans(gsre->RefHMTrans().Array());
433 Int_t ci = gsre->GetColor();
434 TColor* c = gROOT->GetColor(ci);
435 Float_t rgba[4] = {1, 0, 0, 1 - gsre->GetMainTransparency()/100.};
436 if (c)
437 {
438 rgba[0] = c->GetRed();
439 rgba[1] = c->GetGreen();
440 rgba[2] = c->GetBlue();
441 }
442 she->SetRGBA(rgba);
443 she->SetRnrSelf(gsre->GetRnrSelf());
444 she->SetRnrElements(gsre->GetRnrChildren());
445 she->SetShape(gsre->GetShape());
446 if ( gsre->GetNChildren())
447 {
448 TList* ele = new TList();
449 she->SetElements(ele);
450 she->GetElements()->SetOwner(true);
451 RenderElement::List_i i = gsre->BeginChildren();
452 while (i != gsre->EndChildren()) {
453 GeoShapeRnrEl* l = dynamic_cast<GeoShapeRnrEl*>(*i);
454 DumpShapeTree(l, she);
455 i++;
456 }
457 }
458 if(parent)
459 parent->GetElements()->Add(she);
460
461 return she;
462}
463
32e219c2 464GeoShapeRnrEl* GeoShapeRnrEl::ImportShapeExtract(TGeoShapeExtract * gse,
5b3adb7e 465 RenderElement * parent)
466{
467 gReve->DisableRedraw();
32e219c2 468 GeoShapeRnrEl* gsre = SubImportShapeExtract(gse, parent);
469 gsre->ElementChanged();
5b3adb7e 470 gReve->EnableRedraw();
32e219c2 471 return gsre;
5b3adb7e 472}
473
32e219c2 474
475GeoShapeRnrEl* GeoShapeRnrEl::SubImportShapeExtract(TGeoShapeExtract * gse,
5b3adb7e 476 RenderElement * parent)
477{
5b3adb7e 478 GeoShapeRnrEl* gsre = new GeoShapeRnrEl(gse->GetName(), gse->GetTitle());
479 gsre->fHMTrans.SetFromArray(gse->GetTrans());
5b3adb7e 480 const Float_t* rgba = gse->GetRGBA();
481 gsre->fColor = TColor::GetColor(rgba[0], rgba[1], rgba[2]);
482 gsre->fTransparency = (UChar_t) (100.0f*(1.0f - rgba[3]));
483 gsre->SetRnrSelf(gse->GetRnrSelf());
484 gsre->SetRnrChildren(gse->GetRnrElements());
485 gsre->fShape = gse->GetShape();
486 if (gsre->fShape)
487 gsre->fShape->SetUniqueID(gsre->fShape->GetUniqueID() + 1);
488
32e219c2 489 gReve->AddGlobalRenderElement(gsre, parent);
5b3adb7e 490
491 if (gse->HasElements())
492 {
493 TIter next(gse->GetElements());
494 TGeoShapeExtract* chld;
495 while ((chld = (TGeoShapeExtract*) next()) != 0)
32e219c2 496 SubImportShapeExtract(chld, gsre);
5b3adb7e 497 }
498
32e219c2 499 return gsre;
500}
501
502/**************************************************************************/
503
01fa5a07 504TClass* GeoShapeRnrEl::ProjectedClass() const
505{
506 return NLTPolygonSet::Class();
507}
508
509/**************************************************************************/
510
32e219c2 511TBuffer3D* GeoShapeRnrEl::MakeBuffer3D()
512{
513 if(fShape == 0) return 0;
514
515 if(dynamic_cast<TGeoShapeAssembly*>(fShape)){
516 // !!!! TGeoShapeAssembly makes a bad TBuffer3D
517 return 0;
518 }
519
520 TBuffer3D* buff = fShape->MakeBuffer3D();
521 if (fHMTrans.GetUseTrans())
522 {
523 Reve::ZTrans& mx = RefHMTrans();
524 Int_t N = buff->NbPnts();
712d9715 525 Double_t* pnts = buff->fPnts;
526 for(Int_t k=0; k<N; k++)
32e219c2 527 {
528 mx.MultiplyIP(&pnts[3*k]);
529 }
530 }
531 return buff;
5b3adb7e 532}
01fa5a07 533
534
535