Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // >> 26 // $Id: G4TrajectoryDrawerUtils.cc,v 1.6 2006/06/29 21:33:12 gunter Exp $ >> 27 // GEANT4 tag $Name: geant4-08-01 $ 26 // 28 // 27 // Jane Tinslay, John Allison, Joseph Perl Nov 29 // Jane Tinslay, John Allison, Joseph Perl November 2005 28 // 30 // 29 #include "G4TrajectoryDrawerUtils.hh" 31 #include "G4TrajectoryDrawerUtils.hh" 30 #include "G4Colour.hh" 32 #include "G4Colour.hh" 31 #include "G4Polyline.hh" 33 #include "G4Polyline.hh" 32 #include "G4Polymarker.hh" 34 #include "G4Polymarker.hh" 33 #include "G4VTrajectory.hh" 35 #include "G4VTrajectory.hh" 34 #include "G4VTrajectoryPoint.hh" 36 #include "G4VTrajectoryPoint.hh" 35 #include "G4VisAttributes.hh" 37 #include "G4VisAttributes.hh" 36 #include "G4VisTrajContext.hh" 38 #include "G4VisTrajContext.hh" 37 #include "G4VVisManager.hh" 39 #include "G4VVisManager.hh" 38 #include "G4UIcommand.hh" << 39 #include "G4AttValue.hh" << 40 << 41 #include <utility> << 42 << 43 #define G4warn G4cout << 44 40 45 namespace G4TrajectoryDrawerUtils { 41 namespace G4TrajectoryDrawerUtils { 46 42 47 enum TimesValidity {InvalidTimes, ValidTimes << 43 48 << 44 void GetPoints(const G4VTrajectory& traj, G4Polyline& trajectoryLine, 49 TimesValidity GetPointsAndTimes << 45 G4Polymarker& auxiliaryPoints, G4Polymarker& stepPoints) 50 (const G4VTrajectory& traj, << 51 const G4VisTrajContext& context, << 52 G4Polyline& trajectoryLine, << 53 G4Polymarker& auxiliaryPoints, << 54 G4Polymarker& stepPoints, << 55 std::vector<G4double>& trajectoryLineTimes, << 56 std::vector<G4double>& auxiliaryPointTimes, << 57 std::vector<G4double>& stepPointTimes) << 58 { 46 { 59 TimesValidity validity = InvalidTimes; << 47 for (G4int i=0; i<traj.GetPointEntries(); i++) { 60 if (context.GetTimeSliceInterval()) validi << 48 G4VTrajectoryPoint* aTrajectoryPoint = traj.GetPoint(i); 61 << 49 62 // Memory for last trajectory point positi << 50 const std::vector<G4ThreeVector>* auxiliaries 63 // time interpolation algorithm. There ar << 51 = aTrajectoryPoint->GetAuxiliaryPoints(); 64 // for the first trajectory point, so its << 52 65 // immaterial. << 53 if (0 != auxiliaries) { 66 G4ThreeVector lastTrajectoryPointPosition; << 54 for (size_t iAux=0; iAux<auxiliaries->size(); ++iAux) { 67 << 55 const G4ThreeVector pos((*auxiliaries)[iAux]); 68 // Keep positions. Don't store unless fir << 56 trajectoryLine.push_back(pos); 69 std::vector<G4ThreeVector> positions; << 57 auxiliaryPoints.push_back(pos); 70 << 71 for (G4int iPoint=0; iPoint<traj.GetPointE << 72 << 73 G4VTrajectoryPoint* aTrajectoryPoint = t << 74 const G4ThreeVector& trajectoryPointPosi << 75 aTrajectoryPoint->GetPosition(); << 76 << 77 // Only store if first or if different << 78 if (positions.size() == 0 || << 79 trajectoryPointPosition != positions[posit << 80 << 81 // Pre- and Post-Point times from the trajec << 82 G4double trajectoryPointPreTime = -std::nume << 83 G4double trajectoryPointPostTime = std::nume << 84 << 85 if (context.GetTimeSliceInterval() && validi << 86 << 87 std::vector<G4AttValue>* trajectoryPointAt << 88 aTrajectoryPoint->CreateAttValues(); << 89 if (!trajectoryPointAttValues) { << 90 static G4bool warnedNoAttValues = false; << 91 if (!warnedNoAttValues) { << 92 G4warn << << 93 "******************************************* << 94 "\n* WARNING: G4TrajectoryDrawerUtils::GetP << 95 "\n***************************************** << 96 << G4endl; << 97 warnedNoAttValues = true; << 98 } << 99 validity = InvalidTimes; << 100 } else { << 101 G4bool foundPreTime = false, foundPostTi << 102 for (std::vector<G4AttValue>::iterator i << 103 trajectoryPointAttValues->begin(); << 104 i != trajectoryPointAttValues->end(); ++i << 105 if (i->GetName() == "PreT") { << 106 trajectoryPointPreTime = << 107 G4UIcommand::ConvertToDimensionedDouble( << 108 foundPreTime = true; << 109 } << 110 if (i->GetName() == "PostT") { << 111 trajectoryPointPostTime = << 112 G4UIcommand::ConvertToDimensionedDouble( << 113 foundPostTime = true; << 114 } << 115 } << 116 if (!foundPreTime || !foundPostTime) { << 117 static G4bool warnedTimesNotFound = fa << 118 if (!warnedTimesNotFound) { << 119 G4warn << << 120 "******************************************* << 121 "\n* WARNING: G4TrajectoryDrawerUtils::GetP << 122 "\n You need to specify \"/vis/scene/add/tra << 123 "\n***************************************** << 124 << G4endl; << 125 warnedTimesNotFound = true; << 126 } << 127 validity = InvalidTimes; << 128 } << 129 } << 130 delete trajectoryPointAttValues; // << 131 } 58 } 132 << 133 const std::vector<G4ThreeVector>* auxiliarie << 134 = aTrajectoryPoint->GetAuxiliaryPoints(); << 135 if (0 != auxiliaries) { << 136 for (size_t iAux=0; iAux<auxiliaries->size << 137 const G4ThreeVector& auxPointPosition = << 138 if (positions.size() == 0 || << 139 auxPointPosition != positions[positions.si << 140 // Only store if first or if different << 141 positions.push_back(trajectoryPointPos << 142 trajectoryLine.push_back(auxPointPosit << 143 auxiliaryPoints.push_back(auxPointPosi << 144 if (validity == ValidTimes) { << 145 // Interpolate time for auxiliary points.. << 146 G4double s1 = << 147 (auxPointPosition - lastTrajectoryPointP << 148 G4double s2 = << 149 (trajectoryPointPosition - auxPointPosit << 150 G4double t = trajectoryPointPreTime + << 151 (trajectoryPointPostTime - trajectoryPoi << 152 (s1 / (s1 + s2)); << 153 trajectoryLineTimes.push_back(t); << 154 auxiliaryPointTimes.push_back(t); << 155 } << 156 } << 157 } << 158 } << 159 << 160 positions.push_back(trajectoryPointPosition) << 161 trajectoryLine.push_back(trajectoryPointPosi << 162 stepPoints.push_back(trajectoryPointPosition << 163 if (validity == ValidTimes) { << 164 trajectoryLineTimes.push_back(trajectoryPo << 165 stepPointTimes.push_back(trajectoryPointPo << 166 } << 167 lastTrajectoryPointPosition = trajecto << 168 } 59 } 169 } << 60 const G4ThreeVector pos(aTrajectoryPoint->GetPosition()); 170 return validity; << 61 trajectoryLine.push_back(pos); >> 62 stepPoints.push_back(pos); >> 63 } 171 } 64 } 172 65 173 static void SliceLine(G4double timeIncrement << 66 void DrawLineAndPoints(const G4VTrajectory& traj, const G4int& i_mode, const G4Colour& colour, const G4bool& visible) { 174 G4Polyline& trajectoryLine, << 67 // If i_mode>=0, draws a trajectory as a polyline (default is blue for 175 std::vector<G4double>& trajectoryLineTim << 68 // positive, red for negative, green for neutral) and, if i_mode!=0, 176 { << 69 // adds markers - yellow circles for step points and magenta squares 177 // Assumes valid arguments from GetPoints << 70 // for auxiliary points, if any - whose screen size in pixels is 178 << 71 // given by std::abs(i_mode)/1000. E.g: i_mode = 5000 gives easily 179 G4Polyline newTrajectoryLine; << 72 // visible markers. 180 std::vector<G4double> newTrajectoryLineTim << 73 181 << 182 newTrajectoryLine.push_back(trajectoryLine << 183 newTrajectoryLineTimes.push_back(trajector << 184 size_t lineSize = trajectoryLine.size(); << 185 if (lineSize > 1) { << 186 for (size_t i = 1; i < trajectoryLine.si << 187 G4double deltaT = trajectoryLineTimes[i] - t << 188 if (deltaT > 0.) { << 189 G4double practicalTimeIncrement = << 190 std::max(timeIncrement, deltaT / 100.); << 191 for (G4double t = << 192 (int(trajectoryLineTimes[i - 1]/practical << 193 practicalTimeIncrement; << 194 t <= trajectoryLineTimes[i]; << 195 t += practicalTimeIncrement) { << 196 G4ThreeVector pos = trajectoryLine[i - 1 << 197 (trajectoryLine[i] - trajectoryLine[i << 198 ((t - trajectoryLineTimes[i - 1]) / de << 199 newTrajectoryLine.push_back(pos); << 200 newTrajectoryLineTimes.push_back(t); << 201 } << 202 } << 203 newTrajectoryLine.push_back(trajectoryLine[i << 204 newTrajectoryLineTimes.push_back(trajectoryL << 205 } << 206 } << 207 << 208 trajectoryLine = std::move(newTrajectoryLi << 209 trajectoryLineTimes = std::move(newTraject << 210 } << 211 << 212 static void DrawWithoutTime(const G4VisTrajC << 213 G4Polyline& trajectoryLine, << 214 G4Polymarker& auxiliaryPoints, << 215 G4Polymarker& stepPoints) << 216 { << 217 // Draw without time slice information << 218 << 219 G4VVisManager* pVVisManager = G4VVisManage 74 G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance(); 220 if (0 == pVVisManager) return; 75 if (0 == pVVisManager) return; 221 76 222 if (myContext.GetDrawLine() && myContext.G << 77 const G4double markerSize = std::abs(i_mode)/1000; 223 G4VisAttributes trajectoryLineAttribs(my << 78 G4bool lineRequired (i_mode >= 0); 224 trajectoryLineAttribs.SetLineWidth(myCon << 79 G4bool markersRequired (markerSize > 0.); >> 80 >> 81 // Return if don't need to do anything >> 82 if (!lineRequired && !markersRequired) return; >> 83 >> 84 // Get points to draw >> 85 G4Polyline trajectoryLine; >> 86 G4Polymarker stepPoints; >> 87 G4Polymarker auxiliaryPoints; >> 88 >> 89 GetPoints(traj, trajectoryLine, auxiliaryPoints, stepPoints); >> 90 >> 91 if (lineRequired) { >> 92 G4VisAttributes trajectoryLineAttribs(colour); >> 93 trajectoryLineAttribs.SetVisibility(visible); 225 trajectoryLine.SetVisAttributes(&traject 94 trajectoryLine.SetVisAttributes(&trajectoryLineAttribs); 226 << 227 pVVisManager->Draw(trajectoryLine); 95 pVVisManager->Draw(trajectoryLine); 228 } 96 } 229 << 97 230 if (myContext.GetDrawAuxPts() && myContext << 98 if (markersRequired) { 231 && (auxiliaryPoints.size() > 0)) { << 99 auxiliaryPoints.SetMarkerType(G4Polymarker::squares); 232 auxiliaryPoints.SetMarkerType(myContext. << 100 auxiliaryPoints.SetScreenSize(markerSize); 233 auxiliaryPoints.SetSize(myContext.GetAux << 101 auxiliaryPoints.SetFillStyle(G4VMarker::filled); 234 auxiliaryPoints.SetFillStyle(myContext.G << 102 G4VisAttributes auxiliaryPointsAttribs(G4Colour(0.,1.,1.)); // Magenta 235 << 103 auxiliaryPointsAttribs.SetVisibility(visible); 236 G4VisAttributes auxiliaryPointsAttribs(m << 237 auxiliaryPoints.SetVisAttributes(&auxili 104 auxiliaryPoints.SetVisAttributes(&auxiliaryPointsAttribs); 238 << 239 pVVisManager->Draw(auxiliaryPoints); 105 pVVisManager->Draw(auxiliaryPoints); 240 } << 106 241 << 107 stepPoints.SetMarkerType(G4Polymarker::circles); 242 if (myContext.GetDrawStepPts() && myContex << 108 stepPoints.SetScreenSize(markerSize); 243 && (stepPoints.size() > 0)) { << 109 stepPoints.SetFillStyle(G4VMarker::filled); 244 stepPoints.SetMarkerType(myContext.GetSt << 110 G4VisAttributes stepPointsAttribs(G4Colour(1.,1.,0.)); // Yellow. 245 stepPoints.SetSize(myContext.GetStepPtsS << 246 stepPoints.SetFillStyle(myContext.GetSte << 247 << 248 G4VisAttributes stepPointsAttribs(myCont << 249 stepPoints.SetVisAttributes(&stepPointsA 111 stepPoints.SetVisAttributes(&stepPointsAttribs); 250 << 251 pVVisManager->Draw(stepPoints); 112 pVVisManager->Draw(stepPoints); 252 } 113 } >> 114 253 } 115 } 254 << 116 255 static void DrawWithTime(const G4VisTrajCont << 117 void DrawLineAndPoints(const G4VTrajectory& traj, const G4VisTrajContext& context, const G4int& i_mode) 256 G4Polyline& trajectoryLine, << 257 G4Polymarker& auxiliaryPoints, << 258 G4Polymarker& stepPoints, << 259 std::vector<G4double>& trajectoryLine << 260 std::vector<G4double>& auxiliaryPoint << 261 std::vector<G4double>& stepPointTimes << 262 { 118 { 263 // Draw with time slice information << 264 << 265 G4VVisManager* pVVisManager = G4VVisManage 119 G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance(); 266 if (0 == pVVisManager) return; 120 if (0 == pVVisManager) return; 267 << 121 268 if (myContext.GetDrawLine() && myContext.G << 122 // Extra copy while i_mode is still around 269 G4VisAttributes trajectoryLineAttribs(my << 123 G4VisTrajContext myContext(context); 270 trajectoryLineAttribs.SetLineWidth(myCon << 124 271 << 125 if (i_mode != 0) { 272 for (size_t i = 1; i < trajectoryLine.si << 126 const G4double markerSize = std::abs(i_mode)/1000; 273 G4Polyline slice; << 127 G4bool lineRequired (i_mode >= 0); 274 slice.push_back(trajectoryLine[i -1]); << 128 G4bool markersRequired (markerSize > 0.); 275 slice.push_back(trajectoryLine[i]); << 129 276 trajectoryLineAttribs.SetStartTime(trajector << 130 myContext.SetDrawLine(lineRequired); 277 trajectoryLineAttribs.SetEndTime(trajectoryL << 131 myContext.SetDrawAuxPts(markersRequired); 278 slice.SetVisAttributes(&trajectoryLineAttrib << 132 myContext.SetDrawStepPts(markersRequired); 279 pVVisManager->Draw(slice); << 133 280 } << 134 myContext.SetAuxPtsSize(markerSize); 281 } << 135 myContext.SetStepPtsSize(markerSize); 282 << 136 283 if (myContext.GetDrawAuxPts() && myContext << 137 if (!warnedAboutIMode) { 284 && (auxiliaryPoints.size() > 0)) { << 138 G4cout<<"Trajectory drawing configuration will be based on imode value of "<<i_mode<<G4endl; 285 G4VisAttributes auxiliaryPointsAttribs(m << 139 warnedAboutIMode = true; 286 << 287 for (size_t i = 0; i < auxiliaryPoints.s << 288 G4Polymarker point; << 289 point.push_back(auxiliaryPoints[i]); << 290 point.SetMarkerType(myContext.GetAuxPtsType( << 291 point.SetSize(myContext.GetAuxPtsSizeType(), << 292 point.SetFillStyle(myContext.GetAuxPtsFillSt << 293 auxiliaryPointsAttribs.SetStartTime(auxiliar << 294 auxiliaryPointsAttribs.SetEndTime(auxiliaryP << 295 point.SetVisAttributes(&auxiliaryPointsAttri << 296 pVVisManager->Draw(point); << 297 } << 298 } << 299 << 300 if (myContext.GetDrawStepPts() && myContex << 301 && (stepPoints.size() > 0)) { << 302 G4VisAttributes stepPointsAttribs(myCont << 303 << 304 for (size_t i = 0; i < stepPoints.size() << 305 G4Polymarker point; << 306 point.push_back(stepPoints[i]); << 307 point.SetMarkerType(myContext.GetStepPtsType << 308 point.SetSize(myContext.GetStepPtsSizeType() << 309 point.SetFillStyle(myContext.GetStepPtsFillS << 310 stepPointsAttribs.SetStartTime(stepPointTime << 311 stepPointsAttribs.SetEndTime(stepPointTimes[ << 312 point.SetVisAttributes(&stepPointsAttribs); << 313 pVVisManager->Draw(point); << 314 } 140 } 315 } 141 } 316 } << 317 142 318 void DrawLineAndPoints(const G4VTrajectory& << 319 { << 320 // Return if don't need to do anything 143 // Return if don't need to do anything 321 if (!context.GetDrawLine() && !context.Get << 144 if (!myContext.GetDrawLine() && !myContext.GetDrawAuxPts() && !myContext.GetDrawStepPts()) return; 322 145 323 // Get points and times (times are returne << 146 // Get points to draw 324 // is requested). << 325 G4Polyline trajectoryLine; 147 G4Polyline trajectoryLine; 326 G4Polymarker stepPoints; 148 G4Polymarker stepPoints; 327 G4Polymarker auxiliaryPoints; 149 G4Polymarker auxiliaryPoints; 328 std::vector<G4double> trajectoryLineTimes; << 329 std::vector<G4double> stepPointTimes; << 330 std::vector<G4double> auxiliaryPointTimes; << 331 << 332 TimesValidity validity = GetPointsAndTimes << 333 (traj, context, << 334 trajectoryLine, auxiliaryPoints, stepPo << 335 trajectoryLineTimes, auxiliaryPointTime << 336 150 337 if (validity == ValidTimes) { << 151 GetPoints(traj, trajectoryLine, auxiliaryPoints, stepPoints); 338 << 152 339 SliceLine(context.GetTimeSliceInterval() << 153 if (myContext.GetDrawLine()) { 340 trajectoryLine, trajectoryLineTimes); << 154 G4VisAttributes trajectoryLineAttribs(myContext.GetLineColour()); 341 << 155 trajectoryLineAttribs.SetVisibility(myContext.GetLineVisible()); 342 DrawWithTime(context, << 156 trajectoryLine.SetVisAttributes(&trajectoryLineAttribs); 343 trajectoryLine, auxiliaryPoints, stepPo << 157 344 trajectoryLineTimes, auxiliaryPointTime << 158 pVVisManager->Draw(trajectoryLine); 345 << 159 } 346 } else { << 160 347 << 161 if (myContext.GetDrawAuxPts() && (auxiliaryPoints.size() > 0)) { 348 DrawWithoutTime(context, trajectoryLine, << 162 auxiliaryPoints.SetMarkerType(myContext.GetAuxPtsType()); 349 << 163 auxiliaryPoints.SetScreenSize(myContext.GetAuxPtsSize()); >> 164 auxiliaryPoints.SetFillStyle(myContext.GetAuxPtsFillStyle()); >> 165 >> 166 G4VisAttributes auxiliaryPointsAttribs(myContext.GetAuxPtsColour()); >> 167 auxiliaryPointsAttribs.SetVisibility(myContext.GetAuxPtsVisible()); >> 168 auxiliaryPoints.SetVisAttributes(&auxiliaryPointsAttribs); >> 169 >> 170 pVVisManager->Draw(auxiliaryPoints); >> 171 } >> 172 >> 173 if (myContext.GetDrawStepPts() && (stepPoints.size() > 0)) { >> 174 stepPoints.SetMarkerType(myContext.GetStepPtsType()); >> 175 stepPoints.SetScreenSize(myContext.GetStepPtsSize()); >> 176 stepPoints.SetFillStyle(myContext.GetStepPtsFillStyle()); >> 177 >> 178 G4VisAttributes stepPointsAttribs(myContext.GetStepPtsColour()); >> 179 stepPointsAttribs.SetVisibility(myContext.GetStepPtsVisible()); >> 180 stepPoints.SetVisAttributes(&stepPointsAttribs); >> 181 >> 182 pVVisManager->Draw(stepPoints); 350 } 183 } 351 } 184 } 352 } 185 } 353 186