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