G4XXXStoredSceneHandler.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 // John Allison  7th March 2006
00031 // A template for a graphics driver with a store/database.
00032 //?? Lines beginning like this require specialisation for your driver.
00033 
00034 #include "G4XXXStoredSceneHandler.hh"
00035 
00036 #include "G4XXXStoredViewer.hh"
00037 #include "G4PhysicalVolumeModel.hh"
00038 #include "G4LogicalVolumeModel.hh"
00039 #include "G4VPhysicalVolume.hh"
00040 #include "G4LogicalVolume.hh"
00041 #include "G4Box.hh"
00042 #include "G4Polyline.hh"
00043 #include "G4Text.hh"
00044 #include "G4Circle.hh"
00045 #include "G4Square.hh"
00046 #include "G4Polyhedron.hh"
00047 #include "G4UnitsTable.hh"
00048 #include "G4AttDef.hh"
00049 #include "G4AttValue.hh"
00050 #include "G4AttCheck.hh"
00051 
00052 #include <sstream>
00053 
00054 G4int G4XXXStoredSceneHandler::fSceneIdCount = 0;
00055 // Counter for XXX scene handlers.
00056 
00057 G4XXXStoredSceneHandler::G4XXXStoredSceneHandler(G4VGraphicsSystem& system,
00058                                              const G4String& name):
00059   G4VSceneHandler(system, fSceneIdCount++, name)
00060 {}
00061 
00062 G4XXXStoredSceneHandler::~G4XXXStoredSceneHandler() {}
00063 
00064 #ifdef G4XXXStoredDEBUG
00065 // Useful function...
00066 void G4XXXStoredSceneHandler::PrintThings() {
00067   G4cout <<
00068     "  with transformation "
00069          << (void*)fpObjectTransformation;
00070   if (fpModel) {
00071     G4cout << " from " << fpModel->GetCurrentDescription()
00072            << " (tag " << fpModel->GetCurrentTag()
00073            << ')';
00074   } else {
00075     G4cout << "(not from a model)";
00076   }
00077   G4PhysicalVolumeModel* pPVModel =
00078     dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
00079   if (pPVModel) {
00080     G4cout <<
00081       "\n  current physical volume: "
00082            << pPVModel->GetCurrentPV()->GetName() <<
00083       "\n  current logical volume: "
00084 // There might be a problem with the LV pointer if this is a G4LogicalVolumeModel
00085            << pPVModel->GetCurrentLV()->GetName() <<
00086       "\n  current depth of geometry tree: "
00087            << pPVModel->GetCurrentDepth();
00088   }
00089   G4cout << G4endl;
00090 }
00091 #endif
00092 
00093 void G4XXXStoredSceneHandler::PreAddSolid
00094 (const G4Transform3D& objectTransformation,
00095  const G4VisAttributes& visAttribs)
00096 {
00097   G4VSceneHandler::PreAddSolid(objectTransformation, visAttribs);
00098 
00099   // Get user G4Atts...
00100   const std::map<G4String,G4AttDef>* userAttDefs = visAttribs.GetAttDefs();
00101   if (userAttDefs) {
00102 #ifdef G4XXXStoredDEBUG
00103     const std::vector<G4AttValue>* userAttValues =
00104       visAttribs.CreateAttValues();
00105     G4cout << "\nProvided G4Atts:\n"
00106            << G4AttCheck(userAttValues, userAttDefs);
00107     // Extra checks...
00108     G4AttCheck attCheck(userAttValues, userAttDefs);
00109     if (attCheck.Check()) G4cout << "Error" << G4endl;
00110     else {
00111       std::vector<G4AttValue> standardValues;
00112       std::map<G4String,G4AttDef> standardDefinitions;
00113       attCheck.Standard(&standardValues, &standardDefinitions);
00114       G4cout << "\nStandard G4Atts:\n"
00115              << G4AttCheck(&standardValues, &standardDefinitions);
00116     }
00117     // End of extra checks.
00118     delete userAttValues;  // (Must be deleted after use.)
00119 #endif
00120   }
00121 
00122   // Get solid's G4Atts created by G4PhysicalVolumeModel...
00123   G4PhysicalVolumeModel* pPVModel =
00124     dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
00125   if (pPVModel) {
00126     const std::map<G4String,G4AttDef>* solidAttDefs = pPVModel->GetAttDefs();
00127     if (solidAttDefs) {
00128 #ifdef G4XXXStoredDEBUG
00129       std::vector<G4AttValue>* solidAttValues =
00130         pPVModel->CreateCurrentAttValues();
00131       G4cout << "\nProvided G4Atts:\n"
00132              << G4AttCheck(solidAttValues, solidAttDefs);
00133       // Extra checks...
00134       G4AttCheck attCheck(solidAttValues,solidAttDefs);
00135       if (attCheck.Check()) G4cout << "Error" << G4endl;
00136       else {
00137         std::vector<G4AttValue> standardValues;
00138         std::map<G4String,G4AttDef> standardDefinitions;
00139         attCheck.Standard(&standardValues, &standardDefinitions);
00140         G4cout << "\nStandard G4Atts:\n"
00141                << G4AttCheck(&standardValues, &standardDefinitions);
00142       }
00143       // End of extra checks.
00144       delete solidAttValues;  // (Must be deleted after use.)
00145 #endif
00146     }
00147   }
00148 
00149   // Create a place for current solid...
00150   fCurrentItem = fStore.insert(fStore.end(), G4String("\nPreAddSolid:\n"));
00151   if (fReadyForTransients) {
00152     fTransients.push_back(fCurrentItem);
00153   } else {
00154     fPermanents.push_back(fCurrentItem);
00155   }
00156 }
00157 
00158 void G4XXXStoredSceneHandler::PostAddSolid()
00159 {
00160   *fCurrentItem += "\nEndSolid\n";
00161   G4VSceneHandler::PostAddSolid();
00162 }
00163 
00164 void G4XXXStoredSceneHandler::BeginPrimitives
00165 (const G4Transform3D& objectTransformation)
00166 {
00167   G4VSceneHandler::BeginPrimitives(objectTransformation);
00168 
00169   // If thread of control has already passed through PreAddSolid,
00170   // avoid opening a graphical data base component again.
00171   if (!fProcessingSolid) {
00172     // Create a place for current primitive...
00173     fCurrentItem = fStore.insert(fStore.end(),
00174                                  G4String("\nBeginPrimitives:\n"));
00175     if (fReadyForTransients) {
00176       fTransients.push_back(fCurrentItem);
00177     } else {
00178       fPermanents.push_back(fCurrentItem);
00179     }
00180   }
00181 }
00182 
00183 void G4XXXStoredSceneHandler::EndPrimitives ()
00184 {
00185   if (!fProcessingSolid) {  // Already done if so.
00186     *fCurrentItem += "\nEndPrimitives\n";
00187   }
00188   G4VSceneHandler::EndPrimitives ();
00189 }
00190 
00191 // Note: This function overrides G4VSceneHandler::AddSolid(const
00192 // G4Box&).  You may not want to do this, but this is how it's done if
00193 // you do.  Certain other specific solids may be treated this way -
00194 // see G4VSceneHandler.hh.  The simplest possible driver would *not*
00195 // implement these polymorphic functions, with the effect that the
00196 // default versions in G4VSceneHandler are used, which simply call
00197 // G4VSceneHandler::RequestPrimitives to turn the solid into a
00198 // G4Polyhedron usually.
00199 // Don't forget, solids can be transients too (e.g., representing a hit).
00200 void G4XXXStoredSceneHandler::AddSolid(const G4Box& box) {
00201 #ifdef G4XXXStoredDEBUG
00202   G4cout <<
00203     "G4XXXStoredSceneHandler::AddSolid(const G4Box& box) called for "
00204          << box.GetName()
00205          << G4endl;
00206 #endif
00207   //?? Process your box...
00208   std::ostringstream oss;
00209   oss << "G4Box(" <<
00210     G4String
00211     (G4BestUnit
00212      (G4ThreeVector
00213       (box.GetXHalfLength(), box.GetYHalfLength(), box.GetZHalfLength()),
00214       "Length")).strip() << ')';
00215   *fCurrentItem += oss.str();
00216 }
00217 
00218 void G4XXXStoredSceneHandler::AddPrimitive(const G4Polyline& polyline) {
00219 #ifdef G4XXXStoredDEBUG
00220   G4cout <<
00221  "G4XXXStoredSceneHandler::AddPrimitive(const G4Polyline& polyline) called.\n"
00222          << polyline
00223          << G4endl;
00224 #endif
00225   // Get vis attributes - pick up defaults if none.
00226   //const G4VisAttributes* pVA =
00227   //  fpViewer -> GetApplicableVisAttributes (polyline.GetVisAttributes ());
00228   //?? Process polyline.
00229   std::ostringstream oss;
00230   oss << polyline;
00231   *fCurrentItem += oss.str();
00232 }
00233 
00234 void G4XXXStoredSceneHandler::AddPrimitive(const G4Text& text) {
00235 #ifdef G4XXXStoredDEBUG
00236   G4cout <<
00237     "G4XXXStoredSceneHandler::AddPrimitive(const G4Text& text) called.|n"
00238          << text
00239          << G4endl;
00240 #endif
00241   // Get text colour - special method since default text colour is
00242   // determined by the default text vis attributes, which may be
00243   // specified independent of default vis attributes of other types of
00244   // visible objects.
00245   //const G4Colour& c = GetTextColour (text);  // Picks up default if none.
00246   //?? Process text.
00247   std::ostringstream oss;
00248   oss << text;
00249   *fCurrentItem += oss.str();
00250 }
00251 
00252 void G4XXXStoredSceneHandler::AddPrimitive(const G4Circle& circle) {
00253 #ifdef G4XXXStoredDEBUG
00254   G4cout <<
00255     "G4XXXStoredSceneHandler::AddPrimitive(const G4Circle& circle) called.\n"
00256          << circle
00257          << G4endl;
00258   MarkerSizeType sizeType;
00259   G4double size = GetMarkerSize (circle, sizeType);
00260   switch (sizeType) {
00261   default:
00262   case screen:
00263     // Draw in screen coordinates.
00264     G4cout << "screen";
00265     break;
00266   case world:
00267     // Draw in world coordinates.
00268     G4cout << "world";
00269     break;
00270   }
00271   G4cout << " size: " << size << G4endl;
00272 #endif
00273   // Get vis attributes - pick up defaults if none.
00274   //const G4VisAttributes* pVA =
00275   //  fpViewer -> GetApplicableVisAttributes (circle.GetVisAttributes ());
00276   //?? Process circle.
00277   std::ostringstream oss;
00278   oss << circle;
00279   *fCurrentItem += oss.str();
00280 }
00281 
00282 void G4XXXStoredSceneHandler::AddPrimitive(const G4Square& square) {
00283 #ifdef G4XXXStoredDEBUG
00284   G4cout <<
00285     "G4XXXStoredSceneHandler::AddPrimitive(const G4Square& square) called.\n"
00286          << square
00287          << G4endl;
00288   MarkerSizeType sizeType;
00289   G4double size = GetMarkerSize (square, sizeType);
00290   switch (sizeType) {
00291   default:
00292   case screen:
00293     // Draw in screen coordinates.
00294     G4cout << "screen";
00295     break;
00296   case world:
00297     // Draw in world coordinates.
00298     G4cout << "world";
00299     break;
00300   }
00301   G4cout << " size: " << size << G4endl;
00302 #endif
00303   // Get vis attributes - pick up defaults if none.
00304   //const G4VisAttributes* pVA =
00305   //  fpViewer -> GetApplicableVisAttributes (square.GetVisAttributes ());
00306   //?? Process square.
00307   std::ostringstream oss;
00308   oss << square;
00309   *fCurrentItem += oss.str();
00310 }
00311 
00312 void G4XXXStoredSceneHandler::AddPrimitive(const G4Polyhedron& polyhedron) {
00313 #ifdef G4XXXStoredDEBUG
00314   G4cout <<
00315     "G4XXXStoredSceneHandler::AddPrimitive(const G4Polyhedron&) called.\n"
00316          << polyhedron
00317          << G4endl;
00318 #endif
00319   //?? Process polyhedron.
00320   std::ostringstream oss;
00321   oss << polyhedron;
00322   *fCurrentItem += oss.str();
00323 
00324   //?? Or... here are some ideas for decomposing into polygons...
00325   //Assume all facets are convex quadrilaterals.
00326   //Draw each G4Facet individually
00327   
00328   //Get colour, etc..
00329   if (polyhedron.GetNoFacets() == 0) return;
00330 
00331   // Get vis attributes - pick up defaults if none.
00332   const G4VisAttributes* pVA =
00333     fpViewer -> GetApplicableVisAttributes (polyhedron.GetVisAttributes ());
00334 
00335   // Get view parameters that the user can force through the vis
00336   // attributes, thereby over-riding the current view parameter.
00337   G4ViewParameters::DrawingStyle drawing_style = GetDrawingStyle (pVA);
00338   //G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pVA);
00339   
00340   //Get colour, etc..
00341   //const G4Colour& c = pVA -> GetColour ();
00342   
00343   // Initial action depending on drawing style.
00344   switch (drawing_style) {
00345   case (G4ViewParameters::hsr):
00346     {
00347       break;
00348     }
00349   case (G4ViewParameters::hlr):
00350     {
00351       break;
00352     }
00353   case (G4ViewParameters::wireframe):
00354     {
00355       break;
00356     }
00357   default:
00358     {
00359       break;
00360     }     
00361   }
00362 
00363   // Loop through all the facets...
00364 
00365   // Look at G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&)
00366   // for an example of how to get facets out of a G4Polyhedron,
00367   // including how to cope with triangles if that's a problem.
00368 }
00369 
00370 void G4XXXStoredSceneHandler::AddPrimitive(const G4NURBS&) {
00371 #ifdef G4XXXStoredDEBUG
00372   G4cout <<
00373     "G4XXXStoredSceneHandler::AddPrimitive(const G4NURBS& nurbs) called."
00374          << G4endl;
00375 #endif
00376   //?? Don't bother implementing this.  NURBS are not functional.
00377 }
00378 
00379 void G4XXXStoredSceneHandler::ClearStore ()
00380 {
00381   fStore.clear();
00382   fPermanents.clear();
00383   fTransients.clear();
00384 }
00385 
00386 void G4XXXStoredSceneHandler::ClearTransientStore ()
00387 {
00388   typedef std::vector<StoreIterator>::iterator StoreIteratorIterator;
00389   for (StoreIteratorIterator i = fTransients.begin();
00390        i != fTransients.end(); ++i) {
00391     fStore.erase(*i);
00392   }
00393   fTransients.clear();
00394 
00395   // Make sure screen corresponds to graphical database...
00396   if (fpViewer) {
00397     fpViewer -> SetView ();
00398     fpViewer -> ClearView ();
00399     fpViewer -> DrawView ();
00400   }
00401 }

Generated on Mon May 27 17:50:28 2013 for Geant4 by  doxygen 1.4.7