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] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 //
  2 // ********************************************************************
  3 // * License and Disclaimer                                           *
  4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.                             *
 10 // *                                                                  *
 11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                                                  *
 18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // ********************************************************************
 25 //
 26 //
 27 //
 28 //
 29 //
 30 
 31 ///////////////////////
 32 //G4RTRun.cc
 33 ///////////////////////
 34 
 35 #include "G4RTRun.hh"
 36 #include "G4TheMTRayTracer.hh"
 37 
 38 #include "G4Colour.hh"
 39 #include "G4VisAttributes.hh"
 40 #include "G4Event.hh"
 41 #include "G4TrajectoryContainer.hh"
 42 
 43 #include "G4RayTrajectory.hh"
 44 #include "G4RayTrajectoryPoint.hh"
 45 
 46 G4RTRun::G4RTRun()
 47 {
 48   colorMap = new G4THitsMap<G4Colour>("G4RTRun","ColorMap");
 49 
 50   backgroundColour = G4TheMTRayTracer::theInstance->backgroundColour;
 51   lightDirection = G4TheMTRayTracer::theInstance->lightDirection;
 52   attenuationLength = G4TheMTRayTracer::theInstance->attenuationLength;
 53 }
 54 
 55 G4RTRun::~G4RTRun()
 56 { 
 57   colorMap->clear();
 58   delete colorMap;
 59 }
 60 
 61 void G4RTRun::RecordEvent(const G4Event* evt)
 62 {
 63   G4TrajectoryContainer * trajectoryContainer = evt->GetTrajectoryContainer();
 64   if(!trajectoryContainer) return;
 65   G4RayTrajectory* trajectory = static_cast<G4RayTrajectory*>( (*trajectoryContainer)[0] );
 66   if(!trajectory) return;
 67 
 68   G4int nPoint = trajectory->GetPointEntries();
 69   if(nPoint==0) return;
 70 
 71   G4int evId = evt->GetEventID();
 72   G4Colour initialCol(backgroundColour);
 73   if( trajectory->GetPointC(nPoint-1)->GetPostStepAtt() )
 74   { initialCol = GetSurfaceColour(trajectory->GetPointC(nPoint-1)); }
 75   G4Colour rayColour = Attenuate(trajectory->GetPointC(nPoint-1),initialCol);
 76 
 77   for(int i=nPoint-2;i>=0;i--)
 78   {
 79     G4Colour surfaceCol = GetSurfaceColour(trajectory->GetPointC(i));
 80     G4double weight = 1.0 - surfaceCol.GetAlpha();
 81     G4Colour mixedCol = GetMixedColour(rayColour,surfaceCol,weight);
 82     rayColour = Attenuate(trajectory->GetPointC(i),mixedCol);
 83   }
 84 
 85   colorMap->add(evId,rayColour);
 86 }
 87 
 88 void G4RTRun::Merge(const G4Run* aLocalRun)
 89 {
 90   const G4RTRun* theLocalRun = static_cast<const G4RTRun*>(aLocalRun);
 91   if(theLocalRun) *(colorMap) += *(theLocalRun->colorMap);
 92   G4Run::Merge(aLocalRun);
 93 }
 94 
 95 G4Colour G4RTRun::GetSurfaceColour(G4RayTrajectoryPoint* point)
 96 {
 97   const G4VisAttributes* preAtt = point->GetPreStepAtt();
 98   const G4VisAttributes* postAtt = point->GetPostStepAtt();
 99 
100   G4bool preVis = ValidColour(preAtt);
101   G4bool postVis = ValidColour(postAtt);
102 
103   G4Colour transparent(1.,1.,1.,0.);
104 
105   if(!preVis&&!postVis) return transparent;
106 
107   G4ThreeVector normal = point->GetSurfaceNormal();
108 
109   G4Colour preCol(1.,1.,1.);
110   G4Colour postCol(1.,1.,1.);
111 
112   if(preVis)
113   {
114     G4double brill = (1.0-(-lightDirection).dot(normal))/2.0;
115     G4double red   = preAtt->GetColour().GetRed();
116     G4double green = preAtt->GetColour().GetGreen();
117     G4double blue  = preAtt->GetColour().GetBlue();
118     preCol = G4Colour
119       (red*brill,green*brill,blue*brill,preAtt->GetColour().GetAlpha());
120   }
121   else
122   { preCol = transparent; }
123 
124   if(postVis)
125   {
126     G4double brill = (1.0-(-lightDirection).dot(-normal))/2.0;
127     G4double red   = postAtt->GetColour().GetRed();
128     G4double green = postAtt->GetColour().GetGreen();
129     G4double blue  = postAtt->GetColour().GetBlue();
130     postCol = G4Colour
131       (red*brill,green*brill,blue*brill,postAtt->GetColour().GetAlpha());
132   }
133   else
134   { postCol = transparent; }
135 
136   if(!preVis) return postCol;
137   if(!postVis) return preCol;
138 
139   G4double weight = 0.5;
140   return GetMixedColour(preCol,postCol,weight);
141 }
142 
143 G4Colour G4RTRun::GetMixedColour(G4Colour surfCol,G4Colour transCol,G4double weight)
144 {
145   G4double red   = weight*surfCol.GetRed() + (1.-weight)*transCol.GetRed();
146   G4double green = weight*surfCol.GetGreen() + (1.-weight)*transCol.GetGreen();
147   G4double blue  = weight*surfCol.GetBlue() + (1.-weight)*transCol.GetBlue();
148   G4double alpha = weight*surfCol.GetAlpha() + (1.-weight)*transCol.GetAlpha();
149   return G4Colour(red,green,blue,alpha);
150 }
151 
152 G4Colour G4RTRun::Attenuate(G4RayTrajectoryPoint* point, G4Colour sourceCol)
153 {
154   const G4VisAttributes* preAtt = point->GetPreStepAtt();
155 
156   G4bool visible = ValidColour(preAtt);
157   if(!visible) return sourceCol;
158 
159   G4Colour objCol = preAtt->GetColour();
160   G4double stepRed = objCol.GetRed();
161   G4double stepGreen = objCol.GetGreen();
162   G4double stepBlue = objCol.GetBlue();
163   G4double stepAlpha = objCol.GetAlpha();
164   G4double stepLength = point->GetStepLength();
165 
166   G4double attenuationFuctor;
167   if(stepAlpha > 0.9999999){ stepAlpha = 0.9999999; } // patch to the next line
168     attenuationFuctor = -stepAlpha/(1.0-stepAlpha)*stepLength/attenuationLength;
169 
170   G4double KtRed = std::exp((1.0-stepRed)*attenuationFuctor);
171   G4double KtGreen = std::exp((1.0-stepGreen)*attenuationFuctor);
172   G4double KtBlue = std::exp((1.0-stepBlue)*attenuationFuctor);
173   if(KtRed>1.0){KtRed=1.0;}
174   if(KtGreen>1.0){KtGreen=1.0;}
175   if(KtBlue>1.0){KtBlue=1.0;}
176   return G4Colour(sourceCol.GetRed()*KtRed,
177     sourceCol.GetGreen()*KtGreen,sourceCol.GetBlue()*KtBlue);
178 }
179 
180 G4bool G4RTRun::ValidColour(const G4VisAttributes* visAtt)
181 {
182   G4bool val = true;
183   if(!visAtt)
184   { val = false; }
185   else if(!(visAtt->IsVisible()))
186   { val = false; }
187   else if(visAtt->IsForceDrawingStyle()
188     &&(visAtt->GetForcedDrawingStyle()==G4VisAttributes::wireframe))
189   { val = false; }
190   return val;
191 }
192 
193 
194