Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TEveGeoNode.cxx
Go to the documentation of this file.
1 // @(#)root/eve:$Id$
2 // Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, 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 "TEveGeoNode.h"
13 #include "TEveTrans.h"
14 #include "TEveManager.h"
16 
17 #include "TEveGeoShapeExtract.h"
18 #include "TEvePad.h"
19 #include "TEveGeoPolyShape.h"
20 #include "TGLScenePad.h"
21 #include "TGLFaceSet.h"
22 
23 #include "TROOT.h"
24 #include "TBuffer3D.h"
25 #include "TVirtualViewer3D.h"
26 #include "TColor.h"
27 #include "TFile.h"
28 
29 #include "TGeoShape.h"
30 #include "TGeoVolume.h"
31 #include "TGeoNode.h"
32 #include "TGeoShapeAssembly.h"
33 #include "TGeoCompositeShape.h"
34 #include "TGeoManager.h"
35 #include "TGeoMatrix.h"
36 #include "TVirtualGeoPainter.h"
37 
38 /** \class TEveGeoNode
39 \ingroup TEve
40 Wrapper for TGeoNode that allows it to be shown in GUI and controlled as a TEveElement.
41 */
42 
43 ClassImp(TEveGeoNode);
44 
45 Int_t TEveGeoNode::fgCSGExportNSeg = 64;
46 std::list<TGeoShape*> TEveGeoNode::fgTemporaryStore;
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Returns number of segments used for CSG export.
50 
51 Int_t TEveGeoNode::GetCSGExportNSeg()
52 {
53  return fgCSGExportNSeg;
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Sets number of segments used for CSG export.
58 
59 void TEveGeoNode::SetCSGExportNSeg(Int_t nseg)
60 {
61  fgCSGExportNSeg = nseg;
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Constructor.
66 
67 TEveGeoNode::TEveGeoNode(TGeoNode* node) :
68  TEveElement(),
69  TObject(),
70  fNode(node)
71 {
72  // Hack!! Should use cint to retrieve TAttLine::fLineColor offset.
73  char* l = (char*) dynamic_cast<TAttLine*>(node->GetVolume());
74  SetMainColorPtr((Color_t*)(l + sizeof(void*)));
75  SetMainTransparency(fNode->GetVolume()->GetTransparency());
76 
77  SetRnrSelfChildren(fNode->IsVisible(), fNode->IsVisDaughters());
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// Return name, taken from geo-node. Used via TObject.
82 
83 const char* TEveGeoNode::GetName() const
84 {
85  return fNode->GetName();
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// Return title, taken from geo-node. Used via TObject.
90 
91 const char* TEveGeoNode::GetTitle() const
92 {
93  return fNode->GetTitle();
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Return name, taken from geo-node. Used via TEveElement.
98 
99 const char* TEveGeoNode::GetElementName() const
100 {
101  return fNode->GetName();
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// Return title, taken from geo-node. Used via TEveElement.
106 
107 const char* TEveGeoNode::GetElementTitle() const
108 {
109  return fNode->GetTitle();
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Checks if child-nodes have been imported ... imports them if not.
114 /// Then calls TEveElement::ExpandIntoListTree.
115 
116 void TEveGeoNode::ExpandIntoListTree(TGListTree* ltree,
117  TGListTreeItem* parent)
118 {
119  if ( ! HasChildren() && fNode->GetVolume()->GetNdaughters() > 0)
120  {
121  TIter next(fNode->GetVolume()->GetNodes());
122  TGeoNode* dnode;
123  while ((dnode = (TGeoNode*) next()) != 0)
124  {
125  TEveGeoNode* node_re = new TEveGeoNode(dnode);
126  AddElement(node_re);
127  }
128  }
129  TEveElement::ExpandIntoListTree(ltree, parent);
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Expand children into all list-trees.
134 
135 void TEveGeoNode::ExpandIntoListTrees()
136 {
137  for (sLTI_i i = fItems.begin(); i != fItems.end(); ++i)
138  {
139  ExpandIntoListTree(i->fTree, i->fItem);
140  }
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Expand children into all list-trees recursively.
145 /// This is useful if one wants to export extracted shapes.
146 
147 void TEveGeoNode::ExpandIntoListTreesRecursively()
148 {
149  ExpandIntoListTrees();
150  for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
151  {
152  TEveGeoNode *egn = dynamic_cast<TEveGeoNode*>(*i);
153  if (egn)
154  egn->ExpandIntoListTreesRecursively();
155  }
156 }
157 
158 ////////////////////////////////////////////////////////////////////////////////
159 /// Override from TEveElement.
160 /// Process visibility changes and forward them to fNode.
161 
162 void TEveGeoNode::AddStamp(UChar_t bits)
163 {
164  TEveElement::AddStamp(bits);
165  if (bits & kCBVisibility)
166  {
167  fNode->SetVisibility(fRnrSelf);
168  fNode->VisibleDaughters(fRnrChildren);
169  }
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Can edit main-color -- not available for assemblies.
174 
175 Bool_t TEveGeoNode::CanEditMainColor() const
176 {
177  return ! fNode->GetVolume()->IsAssembly();
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// Set color, propagate to volume's line color.
182 
183 void TEveGeoNode::SetMainColor(Color_t color)
184 {
185  TEveElement::SetMainColor(color);
186  fNode->GetVolume()->SetLineColor(color);
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Can edit main transparency -- not available for assemblies.
191 
192 Bool_t TEveGeoNode::CanEditMainTransparency() const
193 {
194  return ! fNode->GetVolume()->IsAssembly();
195 }
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// Get transparency -- it is taken from the geo node.
199 
200 Char_t TEveGeoNode::GetMainTransparency() const
201 {
202  return fNode->GetVolume()->GetTransparency();
203 }
204 
205 ////////////////////////////////////////////////////////////////////////////////
206 /// Set transparency, propagate to volume's transparency.
207 
208 void TEveGeoNode::SetMainTransparency(Char_t t)
209 {
210  TEveElement::SetMainTransparency(t);
211  fNode->GetVolume()->SetTransparency(t);
212 }
213 
214 ////////////////////////////////////////////////////////////////////////////////
215 /// Updates all reve-browsers having the node in their contents.
216 /// All 3D-pads updated if any change found.
217 ///
218 /// Should (could?) be optimized with some assumptions about
219 /// volume/node structure (search for parent, know the same node can not
220 /// reoccur on lower level once found).
221 
222 void TEveGeoNode::UpdateNode(TGeoNode* node)
223 {
224  static const TEveException eH("TEveGeoNode::UpdateNode ");
225 
226  // printf("%s node %s %p\n", eH.Data(), node->GetName(), node);
227 
228  if (fNode == node)
229  StampColorSelection();
230 
231  for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
232  ((TEveGeoNode*)(*i))->UpdateNode(node);
233  }
234 }
235 
236 ////////////////////////////////////////////////////////////////////////////////
237 /// Updates all reve-browsers having the volume in their contents.
238 /// All 3D-pads updated if any change found.
239 ///
240 /// Should (could?) be optimized with some assumptions about
241 /// volume/node structure (search for parent, know the same node can not
242 /// reoccur on lower level once found).
243 
244 void TEveGeoNode::UpdateVolume(TGeoVolume* volume)
245 {
246  static const TEveException eH("TEveGeoNode::UpdateVolume ");
247 
248  // printf("%s volume %s %p\n", eH.Data(), volume->GetName(), volume);
249 
250  if(fNode->GetVolume() == volume)
251  StampColorSelection();
252 
253  for(List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
254  ((TEveGeoNode*)(*i))->UpdateVolume(volume);
255  }
256 }
257 
258 ////////////////////////////////////////////////////////////////////////////////
259 /// Draw the object.
260 
261 void TEveGeoNode::Draw(Option_t* option)
262 {
263  TString opt("SAME");
264  opt += option;
265  fNode->GetVolume()->Draw(opt);
266 }
267 
268 ////////////////////////////////////////////////////////////////////////////////
269 /// Save TEveGeoShapeExtract tree starting at this node.
270 /// This function is obsolete, use SaveExtract() instead.
271 
272 void TEveGeoNode::Save(const char* file, const char* name, Bool_t leafs_only)
273 {
274  Warning("Save()", "This function is deprecated, use SaveExtract() instead.");
275  SaveExtract(file, name, leafs_only);
276 }
277 
278 ////////////////////////////////////////////////////////////////////////////////
279 /// Save the shape tree as TEveGeoShapeExtract.
280 /// File is always recreated.
281 
282 void TEveGeoNode::SaveExtract(const char* file, const char* name, Bool_t leafs_only)
283 {
284  TEveGeoShapeExtract* gse = DumpShapeTree(this, 0, leafs_only);
285  if (gse)
286  {
287  TFile f(file, "RECREATE");
288  gse->Write(name);
289  f.Close();
290  }
291 
292  for (std::list<TGeoShape*>::iterator i = fgTemporaryStore.begin(); i != fgTemporaryStore.end(); ++i)
293  delete *i;
294  fgTemporaryStore.clear();
295 }
296 
297 ////////////////////////////////////////////////////////////////////////////////
298 /// Write the shape tree as TEveGeoShapeExtract to current directory.
299 
300 void TEveGeoNode::WriteExtract(const char* name, Bool_t leafs_only)
301 {
302  TEveGeoShapeExtract* gse = DumpShapeTree(this, 0, leafs_only);
303  if (gse)
304  {
305  gse->Write(name);
306  }
307 }
308 
309 ////////////////////////////////////////////////////////////////////////////////
310 /// Export the node hierarchy into tree of TEveGeoShapeExtract objects.
311 
312 TEveGeoShapeExtract* TEveGeoNode::DumpShapeTree(TEveGeoNode* geon,
313  TEveGeoShapeExtract* parent,
314  Bool_t leafs_only)
315 {
316  static const TEveException eh("TEveGeoNode::DumpShapeTree ");
317 
318  TGeoNode* tnode = 0;
319  TGeoVolume* tvolume = 0;
320  TGeoShape* tshape = 0;
321 
322  tnode = geon->GetNode();
323  if (tnode == 0)
324  {
325  Info(eh, "Null TGeoNode for TEveGeoNode '%s': assuming it's a holder and descending.", geon->GetName());
326  }
327  else
328  {
329  tvolume = tnode->GetVolume();
330  if (tvolume == 0) {
331  Warning(eh, "Null TGeoVolume for TEveGeoNode '%s'; skipping its sub-tree.\n", geon->GetName());
332  return 0;
333  }
334  tshape = tvolume->GetShape();
335  if (tshape->IsComposite())
336  {
337  TEvePad pad;
338  TEvePadHolder gpad(kFALSE, &pad);
339  pad.GetListOfPrimitives()->Add(tshape);
340  TGLScenePad scene_pad(&pad);
341  pad.SetViewer3D(&scene_pad);
342 
343  {
344  TEveGeoManagerHolder gmgr(tvolume->GetGeoManager(), fgCSGExportNSeg);
345  gGeoManager->SetPaintVolume(tvolume);
346 
347  TGeoMatrix *gst = TGeoShape::GetTransform();
348  TGeoShape::SetTransform(TEveGeoShape::GetGeoHMatrixIdentity());
349 
350  scene_pad.BeginScene();
351  dynamic_cast<TGeoCompositeShape*>(tshape)->PaintComposite();
352  scene_pad.EndScene();
353 
354  TGeoShape::SetTransform(gst);
355  }
356 
357  pad.SetViewer3D(0);
358 
359  TGLFaceSet* fs = dynamic_cast<TGLFaceSet*>(scene_pad.FindLogical(tvolume));
360  if (!fs) {
361  Warning(eh, "Failed extracting CSG tesselation TEveGeoNode '%s'; skipping its sub-tree.\n", geon->GetName());
362  return 0;
363  }
364 
365  TEveGeoPolyShape* egps = new TEveGeoPolyShape();
366  egps->SetFromFaceSet(fs);
367  tshape = egps;
368  fgTemporaryStore.push_back(egps);
369  }
370  }
371 
372  // transformation
373  TEveTrans trans;
374  if (parent)
375  trans.SetFromArray(parent->GetTrans());
376  if (tnode)
377  {
378  TGeoMatrix *gm = tnode->GetMatrix();
379  const Double_t *rm = gm->GetRotationMatrix();
380  const Double_t *tv = gm->GetTranslation();
381  TEveTrans t;
382  t(1,1) = rm[0]; t(1,2) = rm[1]; t(1,3) = rm[2];
383  t(2,1) = rm[3]; t(2,2) = rm[4]; t(2,3) = rm[5];
384  t(3,1) = rm[6]; t(3,2) = rm[7]; t(3,3) = rm[8];
385  t(1,4) = tv[0]; t(2,4) = tv[1]; t(3,4) = tv[2];
386  trans *= t;
387  }
388 
389  TEveGeoShapeExtract* gse = new TEveGeoShapeExtract(geon->GetName(), geon->GetTitle());
390  gse->SetTrans(trans.Array());
391  Int_t ci = 0;
392  Char_t transp = 0;
393  if (tvolume) {
394  ci = tvolume->GetLineColor();
395  transp = tvolume->GetTransparency();
396  }
397  TColor* c = gROOT->GetColor(ci);
398  Float_t rgba[4] = {1, 0, 0, 1.0f - transp/100.0f};
399  if (c) {
400  rgba[0] = c->GetRed();
401  rgba[1] = c->GetGreen();
402  rgba[2] = c->GetBlue();
403  }
404  gse->SetRGBA(rgba);
405  rgba[3] = 1;
406  c = gROOT->GetColor(TColor::GetColorDark(ci));
407  if (c) {
408  rgba[0] = c->GetRed();
409  rgba[1] = c->GetGreen();
410  rgba[2] = c->GetBlue();
411  }
412  gse->SetRGBALine(rgba);
413 
414  // Keep default extract line color --> black.
415  Bool_t rnr = tnode ? tnode->IsVisible() : geon->GetRnrSelf();
416  Bool_t rnr_els = tnode ? tnode->IsVisDaughters() : geon->GetRnrChildren();
417  if (tvolume) {
418  rnr = rnr && tvolume->IsVisible();
419  rnr_els = rnr_els && tvolume->IsVisDaughters();
420  }
421  gse->SetRnrSelf (rnr);
422  gse->SetRnrElements(rnr_els);
423  gse->SetRnrFrame (kTRUE);
424  gse->SetMiniFrame (kTRUE);
425 
426  gse->SetShape((leafs_only && geon->HasChildren()) ? 0 : tshape);
427 
428  if (geon->HasChildren())
429  {
430  TList* ele = new TList();
431  gse->SetElements(ele);
432  gse->GetElements()->SetOwner(true);
433 
434  TEveElement::List_i i = geon->BeginChildren();
435  while (i != geon->EndChildren())
436  {
437  TEveGeoNode* l = dynamic_cast<TEveGeoNode*>(*i);
438  DumpShapeTree(l, gse, leafs_only);
439  ++i;
440  }
441  }
442 
443  if (parent)
444  parent->GetElements()->Add(gse);
445 
446  return gse;
447 }
448 
449 
450 
451 /** \class TEveGeoTopNode
452 \ingroup TEve
453 A wrapper over a TGeoNode, possibly displaced with a global
454 trasformation stored in TEveElement.
455 
456 It holds a pointer to TGeoManager and controls for steering of
457 TGeoPainter, fVisOption, fVisLevel and fMaxVisNodes. They have the
458 same meaning as in TGeoManager/TGeoPainter.
459 */
460 
461 ClassImp(TEveGeoTopNode);
462 
463 ////////////////////////////////////////////////////////////////////////////////
464 /// Constructor.
465 
466 TEveGeoTopNode::TEveGeoTopNode(TGeoManager* manager, TGeoNode* node,
467  Int_t visopt, Int_t vislvl, Int_t maxvisnds) :
468  TEveGeoNode (node),
469  fManager (manager),
470  fVisOption (visopt),
471  fVisLevel (vislvl),
472  fMaxVisNodes (maxvisnds)
473 {
474  InitMainTrans();
475  fRnrSelf = kTRUE; // Override back from TEveGeoNode.
476 }
477 
478 ////////////////////////////////////////////////////////////////////////////////
479 /// Use transformation matrix from the TGeoNode.
480 /// Warning: this is local transformation of the node!
481 
482 void TEveGeoTopNode::UseNodeTrans()
483 {
484  RefMainTrans().SetFrom(*fNode->GetMatrix());
485 }
486 
487 ////////////////////////////////////////////////////////////////////////////////
488 /// Revert from TEveGeoNode back to standard behaviour, that is,
489 /// do not pass visibility changes to fNode as they are honoured
490 /// in Paint() method.
491 
492 void TEveGeoTopNode::AddStamp(UChar_t bits)
493 {
494  TEveElement::AddStamp(bits);
495 }
496 
497 ////////////////////////////////////////////////////////////////////////////////
498 /// Draw the top-node.
499 
500 void TEveGeoTopNode::Draw(Option_t* option)
501 {
502  AppendPad(option);
503 }
504 
505 ////////////////////////////////////////////////////////////////////////////////
506 /// Paint the enclosed TGeo hierarchy with visibility level and
507 /// option given in data-members.
508 /// Uses TGeoPainter internally.
509 
510 void TEveGeoTopNode::Paint(Option_t* option)
511 {
512  if (fRnrSelf)
513  {
514  TEveGeoManagerHolder geo_holder(fManager);
515  TVirtualPad *pad = gPad;
516  gPad = 0;
517  TGeoVolume* top_volume = fManager->GetTopVolume();
518  if (fVisLevel > 0)
519  fManager->SetVisLevel(fVisLevel);
520  else
521  fManager->SetMaxVisNodes(fMaxVisNodes);
522  TVirtualGeoPainter* vgp = fManager->GetGeomPainter();
523  fManager->SetTopVolume(fNode->GetVolume());
524  switch (fVisOption)
525  {
526  case 0:
527  fNode->GetVolume()->SetVisContainers(kTRUE);
528  fManager->SetTopVisible(kTRUE);
529  break;
530  case 1:
531  fNode->GetVolume()->SetVisLeaves(kTRUE);
532  fManager->SetTopVisible(kFALSE);
533  break;
534  case 2:
535  fNode->GetVolume()->SetVisOnly(kTRUE);
536  break;
537  }
538  gPad = pad;
539  if(vgp != 0) {
540  vgp->SetVisOption(fVisOption);
541  TGeoHMatrix geomat;
542  if (HasMainTrans()) RefMainTrans().SetGeoHMatrix(geomat);
543  vgp->PaintNode(fNode, option, &geomat);
544  }
545  fManager->SetTopVolume(top_volume);
546  }
547 }
548 
549 ////////////////////////////////////////////////////////////////////////////////
550 /// Callback for propagating volume visibility changes.
551 
552 void TEveGeoTopNode::VolumeVisChanged(TGeoVolume* volume)
553 {
554  static const TEveException eh("TEveGeoTopNode::VolumeVisChanged ");
555  printf("%s volume %s %p\n", eh.Data(), volume->GetName(), (void*)volume);
556  UpdateVolume(volume);
557 }
558 
559 ////////////////////////////////////////////////////////////////////////////////
560 /// Callback for propagating volume parameter changes.
561 
562 void TEveGeoTopNode::VolumeColChanged(TGeoVolume* volume)
563 {
564  static const TEveException eh("TEveGeoTopNode::VolumeColChanged ");
565  printf("%s volume %s %p\n", eh.Data(), volume->GetName(), (void*)volume);
566  UpdateVolume(volume);
567 }
568 
569 ////////////////////////////////////////////////////////////////////////////////
570 /// Callback for propagating node visibility changes.
571 
572 void TEveGeoTopNode::NodeVisChanged(TGeoNode* node)
573 {
574  static const TEveException eh("TEveGeoTopNode::NodeVisChanged ");
575  printf("%s node %s %p\n", eh.Data(), node->GetName(), (void*)node);
576  UpdateNode(node);
577 }