Geant4 VMC Version 6.6
Loading...
Searching...
No Matches
TG4VisManager.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Geant4 Virtual Monte Carlo package
3// Copyright (C) 2007 - 2014 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
31
32#include "TG4VisManager.h"
33#include "TG4Globals.h"
34
35#include <G4LogicalVolumeStore.hh>
36#include <G4Material.hh>
37#include <G4PhysicalVolumeModel.hh>
38#include <G4PhysicalVolumeStore.hh>
39#include <G4SystemOfUnits.hh>
40#include <G4TransportationManager.hh>
41#include <G4VVisManager.hh>
42
43//_____________________________________________________________________________
45 : fColourFlag(true), fVerboseLevel(verboseLevel)
46{
48}
49
50//_____________________________________________________________________________
55
56//
57// private methods
58//
59
60//_____________________________________________________________________________
62 const LogicalVolumesVector& lvVector, const G4LogicalVolume* lv) const
63{
65
66 LogicalVolumesVector::const_iterator i;
67
68 for (i = lvVector.begin(); i != lvVector.end(); i++)
69 if (*i == lv) return true;
70
71 return false;
72}
73
74//_____________________________________________________________________________
76 const PhysicalVolumesVector& pvVector, const G4VPhysicalVolume* pv) const
77{
79
80 PhysicalVolumesVector::const_iterator i;
81
82 for (i = pvVector.begin(); i != pvVector.end(); i++)
83 if (*i == pv) return true;
84
85 return false;
86}
87
88//_____________________________________________________________________________
90{
95
97 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
98 G4LogicalVolume* pLV = 0;
99 if (pLVStore) {
100 for (G4int i = 0; i < G4int(pLVStore->size()); i++) {
101 pLV = (*pLVStore)[i];
102 if (CaseInsensitiveEqual(name, pLV->GetName())) {
103 if (!Contains(lvList, pLV)) lvList.push_back(pLV);
104 }
105 }
106 }
107 if (lvList.size() > 0) return lvList;
108
109 G4PhysicalVolumeStore* pPVStore = G4PhysicalVolumeStore::GetInstance();
110 G4VPhysicalVolume* pPV = 0;
111 if (pPVStore) {
112 for (G4int i = 0; i < G4int(pPVStore->size()); i++) {
113 pPV = (*pPVStore)[i];
114 if (CaseInsensitiveEqual(name, pPV->GetName())) {
115 pLV = pPV->GetLogicalVolume();
116 if (!Contains(lvList, pLV)) lvList.push_back(pLV);
117 }
118 }
119 }
120 return lvList;
121}
122
123//_____________________________________________________________________________
125{
127
129 G4PhysicalVolumeStore* pPVStore = G4PhysicalVolumeStore::GetInstance();
130 G4VPhysicalVolume* pPV = 0;
131 for (G4int i = 0; i < G4int(pPVStore->size()); i++) {
132 pPV = (*pPVStore)[i];
133 if (CaseInsensitiveEqual(name, pPV->GetName())) {
134 if (!Contains(pvList, pPV)) pvList.push_back(pPV);
135 }
136 }
137 return pvList;
138}
139
140//_____________________________________________________________________________
142 const G4String string1, const G4String string2)
143{
145
146 G4String str1Cpy(string1);
147 G4String str2Cpy(string2);
148 std::transform(str1Cpy.begin(), str1Cpy.end(), str1Cpy.begin(), ::tolower);
149 std::transform(str2Cpy.begin(), str2Cpy.end(), str2Cpy.begin(), ::tolower);
150 return (str1Cpy == str2Cpy);
151}
152
153//_____________________________________________________________________________
155 G4LogicalVolume* const lv, const TG4G3Attribute att, const G4int val)
156{
158
159 SetG4Attribute(lv, att, val);
160
161 G4String lvName = lv->GetName();
162 G4int nOfDaughters = lv->GetNoDaughters();
163 if (nOfDaughters > 0) {
164 G4String previousName = "";
165 for (G4int i = 0; i < nOfDaughters; i++) {
166 G4LogicalVolume* lvd = lv->GetDaughter(i)->GetLogicalVolume();
167 G4String currentName = lvd->GetName();
168 if (currentName != lvName && currentName != previousName) {
169 SetAtt4Daughters(lvd, att, val);
170 previousName = currentName;
171 }
172 }
173 }
174}
175
176//_____________________________________________________________________________
177G4bool TG4VisManager::IsSharedVisAttributes(const G4LogicalVolume* pLV)
178{
181
182 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
183 G4LogicalVolume* pLVCurrent = 0;
184 const G4VisAttributes* pVisAtt = pLV->GetVisAttributes();
185 if (!pVisAtt) return false;
186 for (G4int i = 0; i < G4int(pLVStore->size()); i++) {
187 pLVCurrent = (*pLVStore)[i];
188 if (pLVCurrent != pLV) {
189 if (pLVCurrent->GetVisAttributes() == pVisAtt) {
190 return true;
191 }
192 }
193 }
194 return false;
195}
196
197//_____________________________________________________________________________
199 G4LogicalVolume* const lv, const TG4G3Attribute att, const G4int val)
200{
203 // --
204
205 if (!lv) return;
206 // Dupplicating old vis. attributes
207 const G4VisAttributes* visAttributes = lv->GetVisAttributes();
208 G4VisAttributes* newVisAttributes;
209 if (!visAttributes)
210 newVisAttributes = new G4VisAttributes(false);
211 else {
212 G4bool visibility = visAttributes->IsVisible();
213 G4Colour colour = visAttributes->GetColour();
214 newVisAttributes = new G4VisAttributes(visibility, colour);
215 }
216
217 const G4int kAbsVal = abs(val); // the functionality is given by the abs value
218
219 // Default visible attributes
220 G4double red(0), green(0), blue(0); // default is black
221 G4bool isVisible(false);
222 G4bool isDaughtersInvisible(false);
223 G4VisAttributes::LineStyle lineStyle = G4VisAttributes::unbroken;
224 G4double lineWidth = 1.0;
225 G4bool isForceDrawingStyle(false);
226 G4VisAttributes::ForcedDrawingStyle drawingStyle = G4VisAttributes::wireframe;
227
228 // a 'hardcopy' of old vis attributes is needed because the copy constructor
229 // resets to defaults some of the data members of G4VisAttributes class
230 if (visAttributes) {
231 isVisible = visAttributes->IsVisible();
232 isDaughtersInvisible = visAttributes->IsDaughtersInvisible();
233 red = visAttributes->GetColour().GetRed();
234 green = visAttributes->GetColour().GetGreen();
235 blue = visAttributes->GetColour().GetBlue(); // old RGB components
236 lineStyle = visAttributes->GetLineStyle();
237 lineWidth = visAttributes->GetLineWidth();
238 isForceDrawingStyle = visAttributes->IsForceDrawingStyle();
239 if (isForceDrawingStyle)
240 drawingStyle = visAttributes->GetForcedDrawingStyle();
241 }
242 G4double luminosityBin(0.04), // bin for luminosity
243 luminosity(0); // colour luminosity
244
245 // Delete old vis. attributes if they are not shared
246 if (visAttributes && !IsSharedVisAttributes(lv)) delete visAttributes;
247
248 // Set the required attribute
249 switch (att) {
250 case kSEEN:
251 switch (val) {
252 case 0:
253 isVisible = false;
254 break;
255 case 1:
256 isVisible = true;
257 break;
258 case -1:
259 isVisible = false;
260 break;
261 case -2:
262 isVisible = false;
263 break;
264 default:
265 isVisible = false;
266 }
267 break;
268 case kLSTY:
269 switch (kAbsVal) {
270 case 1:
271 lineStyle = G4VisAttributes::unbroken;
272 break;
273 case 2:
274 lineStyle = G4VisAttributes::dashed;
275 break;
276 case 3:
277 lineStyle = G4VisAttributes::dotted;
278 break;
279 default:
280 if (fVerboseLevel > 0)
281 G4cout << "TG4VisManager::Gsatt() Usage of LSTY :" << G4endl
282 << "ATT = 1,2,3 means line unbroken, dashed or dotted"
283 << G4endl
284 << "any other value resets to the default : unbroken"
285 << G4endl;
286 lineStyle = G4VisAttributes::unbroken;
287 }
288 break;
289 case kLWID:
290 lineWidth = kAbsVal;
291 if (lineWidth > 7) lineWidth = 7;
292 if (fVerboseLevel > 0)
293 G4cout << "TG4VisManager::Gsatt() Usage for LWID :" << G4endl
294 << " The VAL you supply means the width of lines in pixels "
295 << "for the screen and in 0.1*mm for paper." << G4endl
296 << " Negative values means the same, but for all daughters"
297 << G4endl;
298 break;
299 case kCOLO:
300 if (kAbsVal < 8) // G3 base colours
301 {
302 switch (kAbsVal) {
303 case 1:
304 red = 0;
305 green = 0;
306 blue = 0; // black
307 break;
308 case 2:
309 red = 1;
310 green = 0;
311 blue = 0; // red
312 break;
313 case 3:
314 red = 0;
315 green = 1;
316 blue = 0; // green
317 break;
318 case 4:
319 red = 0;
320 green = 0;
321 blue = 1; // blue
322 break;
323 case 5:
324 red = 1;
325 green = 1;
326 blue = 0; // yellow
327 break;
328 case 6:
329 red = 1;
330 green = 0;
331 blue = 1; // violet
332 break;
333 case 7:
334 red = 0;
335 green = 1;
336 blue = 1; // lightblue (almost !)
337 }
338 luminosity = 0.;
339 }
340 if (kAbsVal >= 8 && kAbsVal <= 16) {
341 red = 0;
342 green = 0;
343 blue = 0;
344 luminosity = (kAbsVal - 7) * luminosityBin;
345 }
346 if (kAbsVal >= 17 && kAbsVal <= 41) {
347 red = 1;
348 green = 0;
349 blue = 0;
350 luminosity = (kAbsVal - 16) * luminosityBin;
351 }
352 if (kAbsVal >= 67 && kAbsVal <= 91) {
353 red = 0;
354 green = 1;
355 blue = 0;
356 luminosity = (kAbsVal - 66) * luminosityBin;
357 }
358 if (kAbsVal >= 117 && kAbsVal <= 141) {
359 red = 0;
360 green = 0;
361 blue = 1;
362 luminosity = (kAbsVal - 116) * luminosityBin;
363 }
364 if (kAbsVal >= 42 && kAbsVal <= 66) {
365 red = 1;
366 green = 1;
367 blue = 0;
368 luminosity = (kAbsVal - 41) * luminosityBin;
369 }
370 if (kAbsVal >= 142 && kAbsVal <= 166) {
371 red = 1;
372 green = 0;
373 blue = 1;
374 luminosity = (kAbsVal - 141) * luminosityBin;
375 }
376 if (kAbsVal >= 92 && kAbsVal <= 116) {
377 red = 0;
378 green = 1;
379 blue = 1;
380 luminosity = (kAbsVal - 91) * luminosityBin;
381 }
382 if (red < luminosityBin) red += luminosity;
383 if (green < luminosityBin) green += luminosity;
384 if (blue < luminosityBin) blue += luminosity;
385 break;
386 case kFILL:
387 isForceDrawingStyle = true;
388 switch (kAbsVal) {
389 case 0:
390 drawingStyle = G4VisAttributes::wireframe;
391 break;
392 case 1:
393 drawingStyle = G4VisAttributes::solid;
394 break;
395 default:
396 if (fVerboseLevel > 0)
397 G4cout << "TG4VisManager::Gsatt() FILL usage :" << G4endl
398 << " The FILL values you can supply are only :" << G4endl
399 << "+/- 1 : forces wireframe drawing (default)" << G4endl
400 << "+/- 2 : forces solid drawing" << G4endl
401 << "other values sets the drawing style to solid" << G4endl;
402 drawingStyle = G4VisAttributes::solid;
403 }
404 default:;
405 ;
406 }
407 // Register vis. attributes
408 newVisAttributes->SetVisibility(isVisible);
409 newVisAttributes->SetDaughtersInvisible(isDaughtersInvisible);
410 newVisAttributes->SetColour(red, green, blue);
411 newVisAttributes->SetLineStyle(lineStyle);
412 newVisAttributes->SetLineWidth(lineWidth);
413 if (drawingStyle == G4VisAttributes::wireframe)
414 newVisAttributes->SetForceWireframe(isForceDrawingStyle);
415 if (drawingStyle == G4VisAttributes::solid)
416 newVisAttributes->SetForceSolid(isForceDrawingStyle);
417
418 lv->SetVisAttributes(newVisAttributes);
419}
420
421//
422// functions for drawing
423//
424
425//_____________________________________________________________________________
426void TG4VisManager::DrawOneSpec(const char* /*name*/)
427{
430
431 G4cout << "TG4VisManager::DrawOneSpec() Not yet implemented";
432}
433
434//_____________________________________________________________________________
436{
438
439 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
440 const G4LogicalVolume* pLV = 0;
441 // parse the LV tree and set colours according to material density
442 for (G4int i = 0; i < G4int(pLVStore->size()); i++) {
443 pLV = (*pLVStore)[i];
444 // G4cout << "VOLUME : " << pLV->GetName() << G4endl;
445 const G4Material* pMaterial = pLV->GetMaterial();
446 const G4State kState = pMaterial->GetState();
447 G4double density = (pMaterial->GetDensity()) * cm3 / g;
448 G4String nState = "Undefined";
449 G4int colour = 1; // black by default
450 G4double luminosity = 0.;
451 if (kState == kStateUndefined) {
452 nState = "Undefined";
453 }
454 if (kState == kStateSolid) {
455 nState = "Solid";
456 if (density < 2) {
457 colour = 17; // red
458 luminosity = 25 - 25 * density / 2;
459 }
460 else if (density < 3) {
461 colour = 117; // blue
462 luminosity = 25 - 25 * (density - 2);
463 }
464 else if (density < 10) {
465 colour = 67; // green
466 luminosity = 25 - 25 * (density - 5) / 5;
467 }
468 else if (density < 15) {
469 colour = 92; // cyan
470 luminosity = 25 - 25 * (density - 10) / 5;
471 }
472 else if (density < 20) {
473 colour = 8; // black
474 luminosity = 9 - 9 * (density - 15) / 5;
475 }
476 }
477 if (kState == kStateLiquid) {
478 nState = "Liquid";
479 colour = 142; // violet
480 luminosity = 25 - 25 * density / 2;
481 }
482 if (kState == kStateGas) {
483 nState = "Gas";
484 if (density < 0.001) {
485 colour = 42;
486 } // yellow
487 else if (density < 0.002) {
488 colour = 27;
489 } // light red
490 else if (density < 0.003) {
491 colour = 77;
492 } // light green
493 else {
494 colour = 102;
495 } // light cyan
496 luminosity = 0;
497 }
498 if (luminosity < 0) luminosity = 0;
499 colour += (G4int)luminosity;
500 // Setting the corresponding colour
501 Gsatt(pLV->GetName(), "COLO", colour);
502 }
503}
504
505//_____________________________________________________________________________
506void TG4VisManager::Gsatt(const char* name, const char* att, Int_t val)
507{
517
518 G4int ival = val;
519 G4LogicalVolume* lv = 0;
521 G4String sname(name), satt(att);
522
523 // seek for known attributes
524 TG4G3Attribute attribute = kUNKNOWN;
525 if (CaseInsensitiveEqual(att, "WORK")) {
526 TG4Globals::Warning("TG4VisManager", "Gsatt",
527 "G3Attribute " + TString(satt) + " not used in G4");
528 return;
529 }
530 if (CaseInsensitiveEqual(att, "SEEN")) attribute = kSEEN;
531 if (CaseInsensitiveEqual(att, "LSTY")) attribute = kLSTY;
532 if (CaseInsensitiveEqual(att, "LWID")) attribute = kLWID;
533 if (CaseInsensitiveEqual(att, "COLO")) attribute = kCOLO;
534 if (CaseInsensitiveEqual(att, "FILL")) attribute = kFILL;
535
536 if (CaseInsensitiveEqual(att, "SET") || CaseInsensitiveEqual(att, "DET") ||
537 CaseInsensitiveEqual(att, "DTYP")) {
538 TG4Globals::Warning("TG4VisManager", "Gsatt",
539 "G3Attribute " + TString(satt) + " not used in G4");
540 return;
541 }
542 if (attribute == kUNKNOWN) {
544 "TG4VisManager", "Gsatt", "G3Attribute " + TString(satt) + " unknown");
545 return;
546 }
547 G4bool doForDaughters(false), // tree iterator flag
548 doForAll(false), // activated if NAME is "*"
549 topVisible(false); // activated for kSEEN/-2
550 if (sname == "*") doForAll = true;
551 if (val < 0 && sname != "*") doForDaughters = true;
552 if (attribute == kSEEN && val == -2) topVisible = true;
553
554 // parse all the tree
555 if (doForAll) {
556 G4LogicalVolumeStore* pLVStore = G4LogicalVolumeStore::GetInstance();
557 for (G4int i = 0; i < G4int(pLVStore->size()); i++) {
558 lv = (*pLVStore)[i];
559 SetG4Attribute(lv, attribute, ival);
560 }
561 return;
562 }
563
564 // get the logical volume pointer corresponding to NAME
565 lvList = GetLVList(name);
566 if (lvList.size() == 0) {
567 TG4Globals::Warning("TG4VisManager", "Gsatt",
568 "Logical volume " + TString(sname) + " has not been found.");
569 return;
570 }
571 // set attribute for all descendents
572 if (doForDaughters) {
573 for (G4int i = 0; i < G4int(G4int(lvList.size())); i++) {
574 lv = lvList[i];
575 SetAtt4Daughters(lv, attribute, ival);
576 }
577 }
578 else {
579 for (G4int i = 0; i < G4int(G4int(lvList.size())); i++) {
580 lv = lvList[i];
581 SetG4Attribute(lv, attribute, ival);
582 }
583 }
584 if (topVisible) {
585 for (G4int i = 0; i < G4int(G4int(lvList.size())); i++) {
586 lv = lvList[i];
587 SetG4Attribute(lv, attribute, 1);
588 }
589 }
590}
591
592//_____________________________________________________________________________
593void TG4VisManager::Gdraw(const char* /*name*/, Float_t /*theta*/,
594 Float_t /*phi*/, Float_t /*psi*/, Float_t /*u0*/, Float_t /*v0*/,
595 Float_t /*ul*/, Float_t /*vl*/)
596{
610
611 /*
612 G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
613 if (!pVVisManager) {
614 TG4Globals::Warning(
615 "TG4VisManager", "Gdraw", "Ignored - No graphics driver is built.");
616 return;
617 }
618 if (NeedSetColours())
619 {
620 SetColors();
621 SetColourFlag(false);
622 }
623
624 const G4double kRad = M_PI/180.;
625 PhysicalVolumesVector pvList;
626 G4String sname(name);
627 G4bool successful = false;
628
629 pvList = GetPVList(name);
630 if (pvList.size()==0)
631 {
632 TG4Globals::Warning(
633 "TG4VisManager", "Gdraw",
634 "Volume " + TString(sname) + " not found.");
635 return;
636 }
637
638 G4VPhysicalVolume *pPV = 0;
639
640 // clear the current scene if not empty
641 // if (!fpScene->IsEmpty()) fpScene->Clear();
642 // G4Scene::Clear() function not available since Geant4 8.1
643
644 // create and add object's model list to the runtime-duration model
645 // list and draw it
646 // (it is deleted in the VisManager destructor within
647 // all the vectors of the scene)
648 for (G4int i=0; i<G4int(pvList.size()); i++)
649 {
650 pPV = pvList[i];
651 successful = fpScene->AddRunDurationModel(new G4PhysicalVolumeModel(pPV));
652 if (!successful)
653 {
654 TG4Globals::Warning(
655 "TG4VisManager", "Gdraw",
656 "Could not add " + TString(pPV->GetName()) + " to the drawing list."
657 + TG4Globals::Endl() +
658 "Probably it is already in the list.");
659 }
660 }
661 // get the standard target point of the scene
662 const G4Point3D kTargetPoint = fpScene->GetStandardTargetPoint();
663
664 // set the viewpoint and the rotation on the screen
665 G4Vector3D viewpointDirection(sin(theta*kRad)*cos(phi*kRad),
666 sin(theta*kRad)*sin(phi*kRad),
667 cos(theta*kRad)); G4Vector3D upVector(sin(psi*kRad), cos(psi*kRad),0);
668
669 // set and register view parameters to the viewer
670
671 G4ViewParameters vp;;
672 vp.SetLightsMoveWithCamera(true);
673 vp.SetViewGeom();
674 vp.UnsetViewHits();
675 vp.UnsetViewDigis();
676 vp.SetNoOfSides(48);
677 vp.SetCurrentTargetPoint(kTargetPoint);
678 vp.SetViewpointDirection(viewpointDirection);
679 vp.SetUpVector(upVector);
680 vp.SetDensityCulling(true);
681 fpViewer->SetViewParameters(vp);
682
683 if (IsValidView())
684 {
685 fpSceneHandler->SetScene(fpScene);
686 fpSceneHandler->SetCurrentViewer(fpViewer);
687 fpViewer->DrawView();
688 fpViewer->ShowView();
689 }
690 else
691 TG4Globals::Warning(
692 "TG4VisManager", "Gdraw", "Ignored - Failed to register volume");
693 */
694
695 TG4Globals::Warning("TG4VisManager", "Gdraw", "Not implemented");
696}
Definition of the TG4Globals class and basic container types.
Definition of the TG4VisManager class.
static void Warning(const TString &className, const TString &methodName, const TString &text)
std::vector< G4LogicalVolume * > LogicalVolumesVector
The vector of G4 logical volumes.
PhysicalVolumesVector GetPVList(G4String name)
LogicalVolumesVector GetLVList(G4String name)
void SetG4Attribute(G4LogicalVolume *const lv, const TG4G3Attribute att, const G4int val)
void SetAtt4Daughters(G4LogicalVolume *const lv, const TG4G3Attribute att, const G4int val)
void Gsatt(const char *name, const char *att, Int_t val)
G4bool IsSharedVisAttributes(const G4LogicalVolume *pLV)
void DrawOneSpec(const char *name)
G4bool Contains(const LogicalVolumesVector &lvVector, const G4LogicalVolume *lv) const
G4int fVerboseLevel
verbose level
void Gdraw(const char *name, Float_t theta, Float_t phi, Float_t psi, Float_t u0, Float_t v0, Float_t ul, Float_t vl)
std::vector< G4VPhysicalVolume * > PhysicalVolumesVector
The vector of G4 physical volumes.
G4bool CaseInsensitiveEqual(const G4String string1, const G4String string2)
TG4VisManager(G4int verboseLevel=0)
TG4G3Attribute
Enumeration type for G3 visualization attributes.
@ kUNKNOWN
Unknown attribute.
@ kLWID
Set line width.
@ kLSTY
@ kCOLO
@ kSEEN
@ kFILL