ATLAS Offline Software
VP1HEPVisUtils.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
7 // //
8 // Implementation of class VP1HEPVisUtils //
9 // //
10 // Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11 // Initial version: November 2008 //
12 // Update: Giorgi Gvaberidze (ggvaberi@cern.ch) //
13 // Update version: November 2010 //
14 // //
16 
18 
19 #include <Inventor/C/errors/debugerror.h>
20 #include <Inventor/nodes/SoSelection.h>
21 #include <Inventor/nodes/SoSeparator.h>
22 #include <Inventor/nodes/SoSwitch.h>
23 //---
24 #include <Inventor/nodes/SoSphere.h>
25 
26 #include "VP1HEPVis/nodes/SoCons.h"
28 #include "VP1HEPVis/nodes/SoLAr.h"
32 #include "VP1HEPVis/nodes/SoTubs.h"
34 
35 #include <Inventor/nodes/SoIndexedFaceSet.h>
36 #include <Inventor/nodes/SoVertexProperty.h>
37 #include <VP1HEPVis/SbPolyhedron.h>
38 
39 #include <iostream>//fixme
40 
41 //____________________________________________________________________
43 public:
44  template <class T>
45  static SoNode * convertToAlternateRep(SoNode *);
46 
47  template <class T>
48  static void clearAlternateRep(SoNode *);
49 
50  template <class T>
51  static void updateAlternateRepIfNull(SoNode *);
52 
53  static bool isGroup(SoNode* n);
54  static bool isNonCustomTree(SoGroup* g);
55  static SoGroup * convertToStandardGroupNode(SoGroup* g,bool transferChildren);
56  static SoGroup * convertToStandardScene(SoGroup* g);
57  static void updateAllNullAlternativeReps(SoGroup*g);
58 };
59 
60 //____________________________________________________________________
62 {
63  if (!g)
64  return true;//well...
65 
66  if (isCustomNode(g))
67  return false;
68 
69  for (int i = 0; i<g->getNumChildren();++i) {
70  SoNode * n = g->getChild(i);
71  if ( Imp::isGroup(n) ) {
72  if (!isNonCustomTree(static_cast<SoGroup*>(n)))
73  return false;
74  } else {
75  if (isCustomNode(n))
76  return false;
77  }
78  }
79  return true;
80 }
81 
82 //____________________________________________________________________
83 SoGroup * VP1HEPVisUtils::Imp::convertToStandardGroupNode(SoGroup* g,bool transferChildren)
84 {
85  //Create new non-custom node and transfer relevant fields:
86  SoGroup * newgroup(0);
87  if ( g->getTypeId().isDerivedFrom(SoSeparator::getClassTypeId()) &&
88  ! g->getTypeId().isDerivedFrom(SoSelection::getClassTypeId()) ) {
89  SoSeparator * sep = new SoSeparator;
90  SoSeparator * origsep = static_cast<SoSeparator*>(g);
91  sep->renderCaching.setValue(origsep->renderCaching.getValue());
92  sep->boundingBoxCaching.setValue(origsep->boundingBoxCaching.getValue());
93  sep->renderCulling.setValue(origsep->renderCulling.getValue());
94  sep->pickCulling.setValue(origsep->pickCulling.getValue());
95  newgroup = sep;
96  } else if ( g->getTypeId().isDerivedFrom(SoSelection::getClassTypeId()) ) {
97  SoSelection * sel = new SoSelection;
98  SoSelection * origsel = static_cast<SoSelection*>(g);
99  sel->renderCaching.setValue(origsel->renderCaching.getValue());
100  sel->boundingBoxCaching.setValue(origsel->boundingBoxCaching.getValue());
101  sel->renderCulling.setValue(origsel->renderCulling.getValue());
102  sel->pickCulling.setValue(origsel->pickCulling.getValue());
103  sel->policy.setValue(origsel->policy.getValue());
104  newgroup = sel;
105  } else if ( g->getTypeId().isDerivedFrom(SoSwitch::getClassTypeId()) ) {
106  SoSwitch *sw = new SoSwitch;
107  sw->whichChild.setValue(static_cast<SoSwitch*>(g)->whichChild.getValue());
108  newgroup = sw;
109  } else {
110  //Everything else is just treated like a group (we could add more specialisations):
111  newgroup = new SoGroup;
112  }
113 
114  //Transfer name:
115  newgroup->setName(g->getName());
116 
117  //Transfer children:
118  if (transferChildren)
119  for (int i = 0; i<g->getNumChildren();++i)
120  newgroup->addChild(g->getChild(i));
121 
122  return newgroup;
123 }
124 
125 //____________________________________________________________________
127 {
128  if (Imp::isNonCustomTree(g))
129  return g;
130 
131  //We have to change something underneath, so we must get a new (and non-custom) group node:
132  SoGroup * thegroup = convertToStandardGroupNode(g,false/*don't transfer children*/);
133  if (!thegroup)
134  std::cout <<"BAAAAAAAAAD!"<<std::endl;
135  for (int i = 0; i<g->getNumChildren();++i) {
136  SoNode * n = g->getChild(i);
137  if (isGroup(n)) {
138  SoGroup * standard = convertToStandardScene(static_cast<SoGroup*>(n));
139  if (standard)
140  thegroup->addChild(standard);
141  else
142  std::cout <<"BAD 1"<<std::endl;
143  } else {
144  SoNode * ntoadd = isCustomNode(n)?convertCustomNodeToAlternateRep(n):n;
145  if (ntoadd)
146  thegroup->addChild(isCustomNode(n)?convertCustomNodeToAlternateRep(n):n);
147  else
148  std::cout <<"BAD 2"<<std::endl;
149  }
150  }
151 
152  return thegroup;
153 }
154 
155 
156 //____________________________________________________________________
158 {
159  if (!g)
160  return 0;
161 
163 
164  if (Imp::isNonCustomTree(g))
165  return g;
166 
167 
168  g->ref();
169 
171 
172  SoGroup* thegroup = Imp::convertToStandardScene(g);
173  thegroup->ref();
174 
176 
177  g->unrefNoDelete();
178  thegroup->unrefNoDelete();
179  return thegroup;
180 }
181 
182 //____________________________________________________________________
184 {
185  if (!n)
186  return false;
187 
188  return n->getNodeType() & SoNode::EXTENSION;
189 }
190 
191 //____________________________________________________________________
192 template <class T>
194 {
195  if (!n)
196  return 0;
197  T * t = static_cast<T*>(n);
198  SoNode * n_alt = t->alternateRep.getValue();
199  if (n_alt) {
200  //Don't clear the alternateRep, since it was there before.
201  } else {
202  t->generateAlternateRep();
203  n_alt = t->alternateRep.getValue();
204  if (n_alt) {
205  //Clear the alternateRep, since it was not there before.
206  n_alt->ref();
207  t->clearAlternateRep();
208  n_alt->unrefNoDelete();
209  } else {
210  return 0;
211  }
212  }
213  n_alt->setName(n->getName());
214  return n_alt;
215 }
216 
217 //____________________________________________________________________
218 template <class T>
220 {
221  T * t = static_cast<T*>(n);
222  if (t&&t->alternateRep.getValue())
223  t->clearAlternateRep();
224 }
225 
226 //____________________________________________________________________
227 template <class T>
229 {
230  T * t = static_cast<T*>(n);
231  if (t&&!t->alternateRep.getValue()) {
232  t->generateAlternateRep();
233  t->clearAlternateRep();
234  }
235 }
236 
237 //____________________________________________________________________
239 {
240  if (!n)
241  return 0;
242 
244 
245  if ( n->getTypeId().isDerivedFrom(SoCons::getClassTypeId()) ) return Imp::convertToAlternateRep<SoCons>(n);
246  if ( n->getTypeId().isDerivedFrom(SoLAr::getClassTypeId()) ) return Imp::convertToAlternateRep<SoLAr>(n);
247 //NOT IMPLEMENTED if ( n->getTypeId().isDerivedFrom(SoPcons::getClassTypeId()) ) return Imp::convertToAlternateRep<SoPcons>(n);
248  if ( n->getTypeId().isDerivedFrom(SoPcons::getClassTypeId()) ) return Imp::convertToAlternateRep<SoPcons>(n);
249  if ( n->getTypeId().isDerivedFrom(SoGenericBox::getClassTypeId()) ) return Imp::convertToAlternateRep<SoGenericBox>(n);
250  if ( n->getTypeId().isDerivedFrom(SoTubs::getClassTypeId()) ) return Imp::convertToAlternateRep<SoTubs>(n);
251  if ( n->getTypeId().isDerivedFrom(SoPolyhedron::getClassTypeId()) ) return Imp::convertToAlternateRep<SoPolyhedron>(n);
252  if ( n->getTypeId().isDerivedFrom(SoTessellated::getClassTypeId()) ) return Imp::convertToAlternateRep<SoTessellated>(n);
253 
254  //A bit special. Probably should not get called here.
255  if ( Imp::isGroup(n) ) return Imp::convertToStandardGroupNode(static_cast<SoGroup*>(n),false /*transferChildren*/);
256  SoGroup * g = new SoGroup;
257  return g;//fixme: share this
258 }
259 
260 //____________________________________________________________________
262 {
263  return n->getTypeId().isDerivedFrom(SoGroup::getClassTypeId());
264 }
265 
266 //____________________________________________________________________
268 {
269  for (int i = 0; i<g->getNumChildren();++i) {
270  SoNode * n = g->getChild(i);
271  if ( Imp::isGroup(n) ) {
272  updateAllNullAlternativeReps(static_cast<SoGroup*>(n));
273  } else if (VP1HEPVisUtils::isCustomNode(n)) {
274  if ( n->getTypeId().isDerivedFrom(SoCons::getClassTypeId()) ) return Imp::updateAlternateRepIfNull<SoCons>(n);
275  if ( n->getTypeId().isDerivedFrom(SoLAr::getClassTypeId()) ) return Imp::updateAlternateRepIfNull<SoLAr>(n);
276  //NOT IMPLEMENTED if ( n->getTypeId().isDerivedFrom(SoPcons::getClassTypeId()) ) return Imp::updateAlternateRepIfNull<SoPcons>(n);
277  if ( n->getTypeId().isDerivedFrom(SoPcons::getClassTypeId()) ) return Imp::updateAlternateRepIfNull<SoPcons>(n);
278  if ( n->getTypeId().isDerivedFrom(SoGenericBox::getClassTypeId()) ) return Imp::updateAlternateRepIfNull<SoGenericBox>(n);
279  if ( n->getTypeId().isDerivedFrom(SoTubs::getClassTypeId()) ) return Imp::updateAlternateRepIfNull<SoTubs>(n);
280  if ( n->getTypeId().isDerivedFrom(SoPolyhedron::getClassTypeId()) ) return Imp::updateAlternateRepIfNull<SoPolyhedron>(n);
281  if ( n->getTypeId().isDerivedFrom(SoTessellated::getClassTypeId()) ) return Imp::updateAlternateRepIfNull<SoTessellated>(n);
282  }
283  }
284 }
285 
286 //____________________________________________________________________
288 {
289  clearAllAlternativeReps(g);//To make sure all altReps. are NULL
290  //(thus, we only update those with null,
291  //avoiding repeated updates on shared
292  //instances)
294 }
295 
296 //____________________________________________________________________
298 {
299  for (int i = 0; i<g->getNumChildren();++i) {
300  SoNode * n = g->getChild(i);
301  if ( Imp::isGroup(n) ) {
302  clearAllAlternativeReps(static_cast<SoGroup*>(n));
303  } else if (isCustomNode(n)) {
304  if ( n->getTypeId().isDerivedFrom(SoCons::getClassTypeId()) ) return Imp::clearAlternateRep<SoCons>(n);
305  if ( n->getTypeId().isDerivedFrom(SoLAr::getClassTypeId()) ) return Imp::clearAlternateRep<SoLAr>(n);
306  //NOT IMPLEMENTED if ( n->getTypeId().isDerivedFrom(SoPcons::getClassTypeId()) ) return Imp::clearAlternateRep<SoPcons>(n);
307  if ( n->getTypeId().isDerivedFrom(SoPcons::getClassTypeId()) ) return Imp::clearAlternateRep<SoPcons>(n);
308  if ( n->getTypeId().isDerivedFrom(SoGenericBox::getClassTypeId()) ) return Imp::clearAlternateRep<SoGenericBox>(n);
309  if ( n->getTypeId().isDerivedFrom(SoTubs::getClassTypeId()) ) return Imp::clearAlternateRep<SoTubs>(n);
310  if ( n->getTypeId().isDerivedFrom(SoPolyhedron::getClassTypeId()) ) return Imp::clearAlternateRep<SoPolyhedron>(n);
311  if ( n->getTypeId().isDerivedFrom(SoTessellated::getClassTypeId()) ) return Imp::clearAlternateRep<SoTessellated>(n);
312  }
313  }
314 }
315 
316 //____________________________________________________________________
318 {
327 }
VP1HEPVisUtils::Imp
Definition: VP1HEPVisUtils.cxx:42
VP1HEPVisUtils::Imp::convertToStandardScene
static SoGroup * convertToStandardScene(SoGroup *g)
Definition: VP1HEPVisUtils.cxx:126
SoCons.h
VP1HEPVisUtils::Imp::clearAlternateRep
static void clearAlternateRep(SoNode *)
Definition: VP1HEPVisUtils.cxx:219
VP1HEPVisUtils::Imp::convertToAlternateRep
static SoNode * convertToAlternateRep(SoNode *)
Definition: VP1HEPVisUtils.cxx:193
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
VP1HEPVisUtils::Imp::isNonCustomTree
static bool isNonCustomTree(SoGroup *g)
Definition: VP1HEPVisUtils.cxx:61
SoTransparency.h
VP1HEPVisUtils.h
SoTessellated::initClass
static void initClass()
Definition: SoTessellated.cxx:19
VP1HEPVisUtils::initAllCustomClasses
static void initAllCustomClasses()
Definition: VP1HEPVisUtils.cxx:317
VP1HEPVisUtils::isCustomNode
static bool isCustomNode(SoNode *)
Definition: VP1HEPVisUtils.cxx:183
SoPcons.h
SoGenericBox::initClass
static void initClass()
Definition: SoGenericBox.cxx:62
SoPolyhedron.h
VP1HEPVisUtils::clearAllAlternativeReps
static void clearAllAlternativeReps(SoGroup *)
Definition: VP1HEPVisUtils.cxx:297
VP1HEPVisUtils::Imp::updateAllNullAlternativeReps
static void updateAllNullAlternativeReps(SoGroup *g)
Definition: VP1HEPVisUtils.cxx:267
SoTransparency::initClass
static void initClass()
Definition: SoTransparency.cxx:29
lumiFormat.i
int i
Definition: lumiFormat.py:92
SoTessellated.h
python.CaloCondTools.g
g
Definition: CaloCondTools.py:15
beamspotman.n
n
Definition: beamspotman.py:731
sel
sel
Definition: SUSYToolsTester.cxx:92
SoPcons::initClass
static void initClass()
Class Initializer, required.
Definition: SoPcons.cxx:72
SoTubs::initClass
static void initClass()
Class Initializer, required.
Definition: SoTubs.cxx:68
VP1HEPVisUtils::Imp::updateAlternateRepIfNull
static void updateAlternateRepIfNull(SoNode *)
Definition: VP1HEPVisUtils.cxx:228
grepfile.sep
sep
Definition: grepfile.py:38
VP1HEPVisUtils::Imp::isGroup
static bool isGroup(SoNode *n)
Definition: VP1HEPVisUtils.cxx:261
VP1HEPVisUtils::Imp::convertToStandardGroupNode
static SoGroup * convertToStandardGroupNode(SoGroup *g, bool transferChildren)
Definition: VP1HEPVisUtils.cxx:83
VP1HEPVisUtils::convertToStandardScene
static SoGroup * convertToStandardScene(SoGroup *)
Definition: VP1HEPVisUtils.cxx:157
SoTubs.h
VP1HEPVisUtils::updateAllAlternativeReps
static void updateAllAlternativeReps(SoGroup *)
Definition: VP1HEPVisUtils.cxx:287
SoCons::initClass
static void initClass()
Class Initializer, required.
Definition: SoCons.cxx:63
SoLAr.h
SoPolyhedron::initClass
static void initClass()
Definition: SoPolyhedron.cxx:47
SbPolyhedron.h
SoGenericBox.h
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
SoLAr::initClass
static void initClass()
Class Initializer, required.
Definition: SoLAr.cxx:66
VP1HEPVisUtils::convertCustomNodeToAlternateRep
static SoNode * convertCustomNodeToAlternateRep(SoNode *)
Definition: VP1HEPVisUtils.cxx:238