G4Scene.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // Scene data  John Allison  19th July 1996.
00031 
00032 #include "G4Scene.hh"
00033 
00034 #include "G4Vector3D.hh"
00035 #include "G4BoundingSphereScene.hh"
00036 #include "G4VisAttributes.hh"
00037 #include "G4PhysicalVolumeModel.hh"
00038 #include "G4TransportationManager.hh"
00039 
00040 G4Scene::G4Scene (const G4String& name):
00041   fName (name),
00042   fRefreshAtEndOfEvent(true),
00043   fRefreshAtEndOfRun(true),
00044   fMaxNumberOfKeptEvents(0)
00045 {} // Note all other data members have default initial values.
00046 
00047 G4Scene::~G4Scene () {}
00048 
00049 G4bool G4Scene::AddRunDurationModel (G4VModel* pModel, G4bool warn) {
00050   std::vector<Model>::const_iterator i;
00051   for (i = fRunDurationModelList.begin ();
00052        i != fRunDurationModelList.end (); ++i) {
00053     if (pModel -> GetGlobalDescription () ==
00054         i->fpModel->GetGlobalDescription ()) break;
00055   }
00056   if (i != fRunDurationModelList.end ()) {
00057     if (warn) {
00058       G4cout << "G4Scene::AddRunDurationModel: model \""
00059              << pModel -> GetGlobalDescription ()
00060              << "\"\n  is already in the run-duration list of scene \""
00061              << fName
00062              << "\"."
00063              << G4endl;
00064     }
00065     return false;
00066   }
00067   fRunDurationModelList.push_back (Model(pModel));
00068   CalculateExtent ();
00069   return true;
00070 }
00071 
00072 void G4Scene::CalculateExtent ()
00073 {
00074   G4BoundingSphereScene boundingSphereScene;
00075 
00076   for (size_t i = 0; i < fRunDurationModelList.size(); i++) {
00077     if (fRunDurationModelList[i].fActive) {
00078       G4VModel* model = fRunDurationModelList[i].fpModel;
00079       if (model -> Validate()) {  // Validates and also recomputes extent.
00080         const G4VisExtent& thisExtent = model -> GetExtent ();
00081         G4double thisRadius = thisExtent.GetExtentRadius ();
00082         if (thisRadius > 0.) {
00083           G4Point3D thisCentre = thisExtent.GetExtentCentre ();
00084           thisCentre.transform (model -> GetTransformation ());
00085           boundingSphereScene.AccrueBoundingSphere (thisCentre, thisRadius);
00086         }
00087       } else {
00088         G4ExceptionDescription ed;
00089         ed << "Invalid model \"" << model->GetGlobalDescription()
00090            << "\".\n  Not included in extent calculation.";
00091         G4Exception
00092           ("G4Scene::CalculateExtent",
00093            "visman0201", JustWarning, ed);
00094       }
00095     }
00096   }
00097 
00098   for (size_t i = 0; i < fEndOfEventModelList.size(); i++) {
00099     if (fEndOfEventModelList[i].fActive) {
00100       G4VModel* model = fEndOfEventModelList[i].fpModel;
00101       if (model -> Validate()) {  // Validates and also recomputes extent.
00102         const G4VisExtent& thisExtent = model -> GetExtent ();
00103         G4double thisRadius = thisExtent.GetExtentRadius ();
00104         if (thisRadius > 0.) {
00105           G4Point3D thisCentre = thisExtent.GetExtentCentre ();
00106           thisCentre.transform (model -> GetTransformation ());
00107           boundingSphereScene.AccrueBoundingSphere (thisCentre, thisRadius);
00108         }
00109       } else {
00110         G4ExceptionDescription ed;
00111         ed << "Invalid model \"" << model->GetGlobalDescription()
00112            << "\".\n  Not included in extent calculation.";
00113         G4Exception
00114           ("G4Scene::CalculateExtent",
00115            "visman0201", JustWarning, ed);
00116       }
00117     }
00118   }
00119 
00120   for (size_t i = 0; i < fEndOfRunModelList.size(); i++) {
00121     if (fEndOfRunModelList[i].fActive) {
00122       G4VModel* model = fEndOfRunModelList[i].fpModel;
00123       if (model -> Validate()) {  // Validates and also recomputes extent.
00124         const G4VisExtent& thisExtent = model -> GetExtent ();
00125         G4double thisRadius = thisExtent.GetExtentRadius ();
00126         if (thisRadius > 0.) {
00127           G4Point3D thisCentre = thisExtent.GetExtentCentre ();
00128           thisCentre.transform (model -> GetTransformation ());
00129           boundingSphereScene.AccrueBoundingSphere (thisCentre, thisRadius);
00130         }
00131       } else {
00132         G4ExceptionDescription ed;
00133         ed << "Invalid model \"" << model->GetGlobalDescription()
00134            << "\".\n  Not included in extent calculation.";
00135         G4Exception
00136           ("G4Scene::CalculateExtent",
00137            "visman0201", JustWarning, ed);
00138       }
00139     }
00140   }
00141 
00142   fExtent = boundingSphereScene.GetBoundingSphereExtent ();
00143   fStandardTargetPoint = fExtent.GetExtentCentre ();
00144   if (fExtent.GetExtentRadius() <= 0.) {
00145         G4Exception
00146           ("G4Scene::CalculateExtent",
00147            "visman0202", JustWarning,
00148            "Scene has no extent.  Please activate or add something."
00149            "\nThe camera needs to have something to point at!"
00150            "\n\"/vis/scene/list\" to see list of models.");
00151   }
00152 }
00153 
00154 G4bool G4Scene::AddWorldIfEmpty (G4bool warn) {
00155   G4bool successful = true;
00156   if (IsEmpty ()) {
00157     successful = false;
00158     G4VPhysicalVolume* pWorld =
00159       G4TransportationManager::GetTransportationManager ()
00160       -> GetNavigatorForTracking () -> GetWorldVolume ();
00161     if (pWorld) {
00162       const G4VisAttributes* pVisAttribs =
00163         pWorld -> GetLogicalVolume () -> GetVisAttributes ();
00164       if (!pVisAttribs || pVisAttribs -> IsVisible ()) {
00165         if (warn) {
00166           G4cout << 
00167             "Your \"world\" has no vis attributes or is marked as visible."
00168             "\n  For a better view of the contents, mark the world as"
00169             " invisible, e.g.,"
00170             "\n  myWorldLogicalVol ->"
00171             " SetVisAttributes (G4VisAttributes::Invisible);"
00172                  << G4endl;
00173         }
00174       }
00175       successful = AddRunDurationModel (new G4PhysicalVolumeModel (pWorld));
00176       // Note: default depth and no modeling parameters.
00177       if (successful) {
00178         if (warn) {
00179           G4cout <<
00180     "G4Scene::AddWorldIfEmpty: The scene was empty of run-duration models."
00181     "\n  \"world\" has been added.";
00182           G4cout << G4endl;
00183         }
00184       }
00185     }
00186   }
00187   return successful;
00188 }
00189 
00190 G4bool G4Scene::AddEndOfEventModel (G4VModel* pModel, G4bool warn) {
00191   G4int i, nModels = fEndOfEventModelList.size ();
00192   for (i = 0; i < nModels; i++) {
00193     if (pModel -> GetGlobalDescription () ==
00194         fEndOfEventModelList[i].fpModel -> GetGlobalDescription ()) break;
00195   }
00196   if (i < nModels) {
00197     if (warn) {
00198       G4cout << "G4Scene::AddEndOfEventModel: a model \""
00199              << pModel -> GetGlobalDescription ()
00200              << "\"\n  is already in the end-of-event list of scene \""
00201              << fName << "\"."
00202              << G4endl;
00203     }
00204     return false;
00205   }
00206   fEndOfEventModelList.push_back (Model(pModel));
00207   return true;
00208 }
00209 
00210 G4bool G4Scene::AddEndOfRunModel (G4VModel* pModel, G4bool warn) {
00211   G4int i, nModels = fEndOfRunModelList.size ();
00212   for (i = 0; i < nModels; i++) {
00213     if (pModel -> GetGlobalDescription () ==
00214         fEndOfRunModelList[i].fpModel -> GetGlobalDescription ()) break;
00215   }
00216   if (i < nModels) {
00217     if (warn) {
00218       G4cout << "G4Scene::AddEndOfRunModel: a model \""
00219              << pModel -> GetGlobalDescription ()
00220              << "\"\n  is already in the end-of-run list of scene \""
00221              << fName << "\"."
00222              << G4endl;
00223     }
00224     return false;
00225   }
00226   fEndOfRunModelList.push_back (pModel);
00227   return true;
00228 }
00229 
00230 std::ostream& operator << (std::ostream& os, const G4Scene& scene) {
00231 
00232   size_t i;
00233 
00234   os << "Scene data:";
00235 
00236   os << "\n  Run-duration model list:";
00237   for (i = 0; i < scene.fRunDurationModelList.size (); i++) {
00238     if (scene.fRunDurationModelList[i].fActive) os << "\n  Active:   ";
00239     else os << "\n  Inactive: ";
00240     os << *(scene.fRunDurationModelList[i].fpModel);
00241   }
00242 
00243   os << "\n  End-of-event model list:";
00244   for (i = 0; i < scene.fEndOfEventModelList.size (); i++) {
00245     if (scene.fEndOfEventModelList[i].fActive) os << "\n  Active:   ";
00246     else os << "\n  Inactive: ";
00247     os << *(scene.fEndOfEventModelList[i].fpModel);
00248   }
00249 
00250   os << "\n  End-of-run model list:";
00251   for (i = 0; i < scene.fEndOfRunModelList.size (); i++) {
00252     if (scene.fEndOfRunModelList[i].fActive) os << "\n  Active:   ";
00253     else os << "\n  Inactive: ";
00254     os << *(scene.fEndOfRunModelList[i].fpModel);
00255   }
00256 
00257   os << "\n  Extent or bounding box: " << scene.fExtent;
00258 
00259   os << "\n  Standard target point:  " << scene.fStandardTargetPoint;
00260 
00261   os << "\n  End of event action set to \"";
00262   if (scene.fRefreshAtEndOfEvent) os << "refresh\"";
00263   else {
00264     os << "accumulate (maximum number of kept events: ";
00265     if (scene.fMaxNumberOfKeptEvents >= 0) os << scene.fMaxNumberOfKeptEvents;
00266     else os << "unlimited";
00267     os << ")";
00268   }
00269 
00270   os << "\n  End of run action set to \"";
00271   if (scene.fRefreshAtEndOfRun) os << "refresh";
00272   else os << "accumulate";
00273   os << "\"";
00274 
00275   return os;
00276 }
00277 
00278 G4bool G4Scene::operator != (const G4Scene& scene) const {
00279   if (
00280       (fRunDurationModelList.size () !=
00281        scene.fRunDurationModelList.size ())                 ||
00282       (fEndOfEventModelList.size () !=
00283        scene.fEndOfEventModelList.size ())                  ||
00284       (fEndOfRunModelList.size () !=
00285        scene.fEndOfRunModelList.size ())                    ||
00286       (fExtent               != scene.fExtent)              ||
00287       !(fStandardTargetPoint == scene.fStandardTargetPoint) ||
00288       fRefreshAtEndOfEvent   != scene.fRefreshAtEndOfEvent  ||
00289       fRefreshAtEndOfRun     != scene.fRefreshAtEndOfRun    ||
00290       fMaxNumberOfKeptEvents != scene.fMaxNumberOfKeptEvents
00291       ) return true;
00292 
00293   /* A complete comparison should, perhaps, include a comparison of
00294      individual models, but it is not easy to implement operator!= for
00295      all models.  Also, it would be unfeasible to ask users to
00296      implement opeerator!= if we ever get round to allowing
00297      user-defined models.  Moreover, there is no editing of G4Scene
00298      objects, apart from changing fRefreshAtEndOfEvent, etc; as far as
00299      models are concerned, all you can ever do is add them, so a test
00300      on size (above) is enough.
00301 
00302   for (size_t i = 0; i < fRunDurationModelList.size (); i++) {
00303     if (fRunDurationModelList[i] != scene.fRunDurationModelList[i])
00304       return true;
00305   }
00306 
00307   for (size_t i = 0; i < fEndOfEventModelList.size (); i++) {
00308     if (fEndOfEventModelList[i] != scene.fEndOfEventModelList[i])
00309       return true;
00310   }
00311 
00312   for (size_t i = 0; i < fEndOfRunModelList.size (); i++) {
00313     if (fEndOfRunModelList[i] != scene.fEndOfRunModelList[i])
00314       return true;
00315   }
00316   */
00317 
00318   return false;
00319 }

Generated on Mon May 27 17:49:48 2013 for Geant4 by  doxygen 1.4.7