source: snovis/trunk/source/G4Lab/cxx/DigitsCollectionAccessor.cxx@ 233

Last change on this file since 233 was 233, checked in by barrand, 19 years ago
  • Property svn:eol-style set to native
File size: 12.8 KB
Line 
1// this :
2#include <G4Lab/DigitsCollectionAccessor.h>
3
4// Inventor :
5#include <Inventor/nodes/SoSeparator.h>
6#include <Inventor/nodes/SoTransform.h>
7#include <Inventor/nodes/SoLightModel.h>
8#include <Inventor/nodes/SoDrawStyle.h>
9#include <Inventor/nodes/SoCoordinate3.h>
10
11// HEPVis :
12#include <HEPVis/misc/SoStyleCache.h>
13#include <HEPVis/nodes/SoPolyhedron.h>
14#include <HEPVis/nodes/SoHighlightMaterial.h>
15
16#ifdef WIN32
17#undef pascal // Clash between windef.h and Geant4/SystemOfUnits.hh
18#endif
19
20// Geant4 :
21#include <G4RunManager.hh>
22#include <G4Event.hh>
23#include <G4DigiManager.hh>
24#include <G4DCofThisEvent.hh>
25#include <G4LogicalVolume.hh>
26#include <G4Transform3D.hh>
27#include <G4AttDef.hh>
28#include <G4AttValue.hh>
29#include <G4Polyhedron.hh>
30#include <G4Colour.hh>
31#include <G4VDigi.hh>
32#include <G4VDigiCollection.hh>
33
34// Lib :
35#include <Slash/Core/ISession.h>
36#include <Slash/Data/IIterator.h>
37#include <Lib/Out.h>
38#include <Lib/Value.h>
39#include <Lib/smanip.h>
40#include <Lib/sout.h>
41
42// G4Lab :
43#include <G4Lab/Transform3D.h>
44#include <G4Lab/Polyhedron.h>
45
46//////////////////////////////////////////////////////////////////////////////
47G4Lab::DigitsCollectionBaseAccessor::DigitsCollectionBaseAccessor(
48 Slash::Core::ISession& aSession
49,const std::string& aDC
50)
51:OnX::InventorAccessor(aSession)
52,fType("DC")
53,fAttDefs(0)
54,fDC(aDC)
55//////////////////////////////////////////////////////////////////////////////
56//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
57{
58 if(fDC!="") fType = fDC;
59}
60//////////////////////////////////////////////////////////////////////////////
61G4Lab::DigitsCollectionBaseAccessor::~DigitsCollectionBaseAccessor(
62)
63//////////////////////////////////////////////////////////////////////////////
64//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
65{
66}
67//////////////////////////////////////////////////////////////////////////////
68std::string G4Lab::DigitsCollectionBaseAccessor::name(
69) const
70//////////////////////////////////////////////////////////////////////////////
71//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
72{
73 return fType;
74}
75//////////////////////////////////////////////////////////////////////////////
76const std::string& G4Lab::DigitsCollectionBaseAccessor::DCName(
77) const
78//////////////////////////////////////////////////////////////////////////////
79//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
80{
81 return fDC;
82}
83//////////////////////////////////////////////////////////////////////////////
84Slash::Core::IValue* G4Lab::DigitsCollectionBaseAccessor::findValue(
85 Slash::Data::IAccessor::Data aData
86,const std::string& aName
87,void*
88)
89//////////////////////////////////////////////////////////////////////////////
90//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
91{
92 if(!fAttDefs) {
93 Lib::Out out(printer());
94 out << "G4Lab::DigitsCollectionBaseAccessor::value :"
95 << " problem to get AttDefs fot digi collection " << Lib::sout(fDC)
96 << "."
97 << Lib::endl;
98 return new Lib::Value();
99 }
100
101 G4VDigi* obj = (G4VDigi*)aData;
102 std::vector<G4AttValue>* vec = obj->CreateAttValues();
103 if(!vec) {
104 Lib::Out out(printer());
105 out << "G4Lab::DigitsCollectionBaseAccessor::value :"
106 << " problem to get AttValues fot digi collection " << Lib::sout(fDC)
107 << "."
108 << Lib::endl;
109 return new Lib::Value();
110 }
111
112 unsigned int number = vec->size();
113 for(unsigned int index=0;index<number;index++) {
114 const G4AttValue& val = (*vec)[index];
115 if(aName==val.GetName()) {
116 const std::string& stype = (*fAttDefs)[val.GetName()].GetValueType();
117 if(stype=="G4int") {
118 int v;
119 if(!Lib::smanip::toint(val.GetValue(),v)) {
120 Lib::Out out(printer());
121 out << "G4Lab::DigitsCollectionBaseAccessor::value :"
122 << " " << Lib::sout(val.GetValue()) << " not a G4int."
123 << Lib::endl;
124 delete vec;
125 return new Lib::Value();
126 }
127 delete vec;
128 return new Lib::Value(v);
129 } else if((stype=="G4double")||(stype=="G4float")) {
130 double v;
131 if(!Lib::smanip::todouble(val.GetValue(),v)) {
132 Lib::Out out(printer());
133 out << "G4Lab::DigitsCollectionBaseAccessor::value :"
134 << " " << Lib::sout(val.GetValue()) << " not a double."
135 << Lib::endl;
136 delete vec;
137 return new Lib::Value();
138 }
139 delete vec;
140 return new Lib::Value(v);
141 } else {
142 delete vec;
143 return new Lib::Value(val.GetValue());
144 }
145 }
146 }
147 Lib::Out out(printer());
148 out << "G4Lab::DigitsCollectionBaseAccessor::value :"
149 << " AttValue not found for property " << Lib::sout(aName) << "."
150 << Lib::endl;
151 delete vec;
152 return new Lib::Value();
153}
154//////////////////////////////////////////////////////////////////////////////
155void G4Lab::DigitsCollectionBaseAccessor::beginVisualize(
156)
157//////////////////////////////////////////////////////////////////////////////
158//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
159{
160 OnX::InventorAccessor::beginVisualize();
161
162 std::string style = "DC("+fDC+")";
163 if(!isStyle(style)) style = "DC"; //Default.
164 fillSoGC(style);
165}
166//////////////////////////////////////////////////////////////////////////////
167void G4Lab::DigitsCollectionBaseAccessor::visualize(
168 Slash::Data::IAccessor::Data aData
169,void*
170)
171//////////////////////////////////////////////////////////////////////////////
172// The digi must have the "LV" and "TSF" AttDef.
173//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
174{
175 G4VDigi* obj = (G4VDigi*)aData;
176 G4LogicalVolume* lv = digiLogicalVolume(*obj);
177 if(!lv) return;
178 G4Transform3D* tsf = digiTransform3D(*obj);
179 if(!tsf) return;
180 G4VSolid* solid = lv->GetSolid();
181 if(!solid) return;
182 //G4Material* material = lv->GetMaterial();
183
184 int old_nstep = HepPolyhedron::GetNumberOfRotationSteps();
185 HepPolyhedron::SetNumberOfRotationSteps(fSoGC.getRotationSteps());
186 G4Polyhedron* g4Polyhedron = solid->CreatePolyhedron();
187 HepPolyhedron::SetNumberOfRotationSteps(old_nstep);
188 if(!g4Polyhedron) return;
189
190 G4Lab::Polyhedron* polyhedron = new G4Lab::Polyhedron(*g4Polyhedron);
191 delete g4Polyhedron;
192 if(!polyhedron) return;
193
194 SoSeparator* separator = new SoSeparator;
195 separator->setName("sceneGraph");
196
197 G4Colour color;
198 if(digiColor(*obj,color)) {
199 SbColor sbColor((float)color.GetRed(),
200 (float)color.GetGreen(),
201 (float)color.GetBlue());
202 float transp = 1.0F - (float)color.GetAlpha();
203 SoStyleCache* styleCache = fSoGC.getStyleCache();
204 separator->addChild(
205 styleCache->getHighlightMaterial
206 (sbColor,fSoGC.getHighlightColor(),transp));
207 } else {
208 separator->addChild(fSoGC.getHighlightMaterial());
209 }
210
211 separator->addChild(fSoGC.getDrawStyle());
212 separator->addChild(fSoGC.getLightModel());
213
214 SoTransform* transform = new SoTransform;
215 G4Lab::Transform3D* t = new G4Lab::Transform3D(*tsf);
216 SbMatrix* matrix = t->getMatrix();
217 transform->setMatrix(*matrix);
218 delete matrix;
219 delete t;
220 separator->addChild(transform);
221
222 // Build name (for picking) :
223 std::string s;
224 Lib::smanip::printf(s,128,"%s/0x%lx",fDC.c_str(),(unsigned long)obj);
225 SbName name(s.c_str());
226 //FIXME : can't cache due to the below setName for picking.
227 //FIXME SoPolyhedron* soPolyhedron = fSoGC.getPolyhedron(*polyhedron);
228 SoPolyhedron* soPolyhedron = new SoPolyhedron(*polyhedron);
229 if(fSoGC.getModeling()==SbModeling_wire_frame) {
230 soPolyhedron->solid.setValue(FALSE);
231 //FIXME : handle reduceWireFrame.
232 soPolyhedron->reducedWireFrame.setValue(TRUE);
233 } else {
234 soPolyhedron->solid.setValue(TRUE);
235 }
236 delete polyhedron;
237 soPolyhedron->setName(name);
238
239 separator->addChild(soPolyhedron);
240
241 fSoRegion->doIt(SbAddNode(separator,"dynamicScene"));
242}
243//////////////////////////////////////////////////////////////////////////////
244void G4Lab::DigitsCollectionBaseAccessor::setProperties(
245 G4VDigi& aDigi
246)
247//////////////////////////////////////////////////////////////////////////////
248//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
249{
250 fAttDefs = (std::map<G4String,G4AttDef>*)aDigi.GetAttDefs();
251 if(!fAttDefs) return;
252 std::map<G4String,G4AttDef>::const_iterator it;
253 for(it=fAttDefs->begin();it!=fAttDefs->end();++it) {
254 const std::string& sname = (*it).second.GetName();
255 if((sname!="LV") &&
256 (sname!="TSF") &&
257 (sname!="Color")) {
258 const std::string& stype = (*it).second.GetValueType();
259 if(stype=="G4int") {
260 addProperty((*it).first,Lib::Property::INTEGER);
261 } else if((stype=="G4double")||(stype=="G4float")) {
262 addProperty((*it).first,Lib::Property::DOUBLE);
263 } else {
264 addProperty((*it).first,Lib::Property::STRING);
265 }
266 }
267 }
268}
269//////////////////////////////////////////////////////////////////////////////
270//////////////////////////////////////////////////////////////////////////////
271//////////////////////////////////////////////////////////////////////////////
272G4VDigiCollection* G4Lab::DigitsCollectionBaseAccessor::getCollection(
273 Slash::Core::IWriter& aPrinter
274,const std::string& aName
275)
276//////////////////////////////////////////////////////////////////////////////
277//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
278{
279 G4RunManager* runManager = G4RunManager::GetRunManager();
280 if(!runManager) {
281 Lib::Out out(aPrinter);
282 out << "No G4RunManager." << Lib::endl;
283 return 0;
284 }
285 const G4Event* event = runManager->GetCurrentEvent();
286 if(!event) {
287 Lib::Out out(aPrinter);
288 out << "No event." << Lib::endl;
289 return 0;
290 }
291 G4DigiManager* digiManager = G4DigiManager::GetDMpointer();
292 if(!digiManager) {
293 Lib::Out out(aPrinter);
294 out << "No G4DigiManager." << Lib::endl;
295 return 0;
296 }
297
298 // Get Digi collection of this event
299 G4DCofThisEvent* dce = event->GetDCofThisEvent();
300 if(!dce) {
301 Lib::Out out(aPrinter);
302 out << "No G4DCofThisEvent." << Lib::endl;
303 return 0;
304 }
305 G4int cid = digiManager->GetDigiCollectionID(aName);
306 int nc = dce->GetNumberOfCollections();
307 if((cid<0)||(cid>=nc)) {
308 Lib::Out out(aPrinter);
309 out << "Collection id not found for " << Lib::sout(aName) << Lib::endl;
310 return 0;
311 }
312
313 G4VDigiCollection* dc = dce->GetDC(cid);
314 if(!dce) {
315 Lib::Out out(aPrinter);
316 out << "No G4VDigiCollection for " << Lib::sout(aName) << Lib::endl;
317 return 0;
318 }
319 return dc;
320}
321//////////////////////////////////////////////////////////////////////////////
322G4LogicalVolume* G4Lab::DigitsCollectionBaseAccessor::digiLogicalVolume(
323 const G4VDigi& aDigi
324)
325//////////////////////////////////////////////////////////////////////////////
326//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
327{
328 std::vector<G4AttValue>* vec = aDigi.CreateAttValues();
329 if(!vec) return 0;
330 std::string value;
331 unsigned int number = vec->size();
332 for(unsigned int index=0;index<number;index++) {
333 const G4AttValue& val = (*vec)[index];
334 if(val.GetName()=="LV") {
335 std::string s = val.GetValue();
336 void* p;
337 if(!Lib::smanip::topointer(s,p)) return 0;
338 return (G4LogicalVolume*)p; //Beurk
339 }
340 }
341 return 0;
342}
343//////////////////////////////////////////////////////////////////////////////
344G4Transform3D* G4Lab::DigitsCollectionBaseAccessor::digiTransform3D(
345 const G4VDigi& aDigi
346)
347//////////////////////////////////////////////////////////////////////////////
348//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
349{
350 std::vector<G4AttValue>* vec = aDigi.CreateAttValues();
351 if(!vec) return 0;
352 std::string value;
353 unsigned int number = vec->size();
354 for(unsigned int index=0;index<number;index++) {
355 const G4AttValue& val = (*vec)[index];
356 if(val.GetName()=="TSF") {
357 std::string s = val.GetValue();
358 void* p;
359 if(!Lib::smanip::topointer(s,p)) return 0;
360 return (G4Transform3D*)p; //Beurk
361 }
362 }
363 return 0;
364}
365//////////////////////////////////////////////////////////////////////////////
366bool G4Lab::DigitsCollectionBaseAccessor::digiColor(
367 const G4VDigi& aDigi
368,G4Colour& aColor
369)
370//////////////////////////////////////////////////////////////////////////////
371//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
372{
373 std::vector<G4AttValue>* vec = aDigi.CreateAttValues();
374 if(vec) {
375 std::string value;
376 unsigned int number = vec->size();
377 for(unsigned int index=0;index<number;index++) {
378 const G4AttValue& val = (*vec)[index];
379 if(val.GetName()=="Color") {
380 std::string s = val.GetValue();
381 double r,g,b,a;
382 if(!Lib::smanip::torgba(s,r,g,b,a)) break;
383 aColor = G4Colour(r,g,b,a);
384 return true;
385 }
386 }
387 }
388 aColor = G4Colour();
389 return false;
390}
Note: See TracBrowser for help on using the repository browser.