4 added + 1 modified, total 5 files
slic/include
diff -N VRML2Writer.hh
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ VRML2Writer.hh 7 Jul 2009 21:39:19 -0000 1.1
@@ -0,0 +1,48 @@
+#ifndef SLIC_VRML2WRITER_HH
+#define SLIC_VRML2WRITER_HH 1
+
+#include <fstream>
+#include <iostream>
+#include <string>
+
+#include "G4ThreeVector.hh"
+#include "G4RotationMatrix.hh"
+
+class G4VPhysicalVolume;
+class G4Box;
+class G4VSolid;
+
+// TODO: correct indentation level
+// TODO: transforms are REVERSED; up the hierarchy instead of down!
+
+namespace slic
+{
+ class VRML2Writer
+ {
+ public:
+ VRML2Writer(const std::string& filename);
+ ~VRML2Writer();
+ void write();
+
+ private:
+
+ void processTopVolume(G4VPhysicalVolume*);
+ void processPhysicalVolume(G4VPhysicalVolume*);
+ void processVisualization(G4VPhysicalVolume*);
+ void processSolid(G4VSolid*);
+ void processBox(G4Box*);
+ void writeLine(const std::string&);
+ void indent();
+ void unindent();
+
+ // workaround for CLHEP Seg Fault
+ double getAngleAxis(G4RotationMatrix* rot, G4ThreeVector &axis);
+
+ private:
+
+ std::ofstream _out;
+ int _indentLevel;
+ };
+}
+
+#endif
slic/include
diff -N VRML2WriterMessenger.hh
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ VRML2WriterMessenger.hh 7 Jul 2009 21:39:19 -0000 1.1
@@ -0,0 +1,29 @@
+// $Header: /cvs/lcd/slic/include/VRML2WriterMessenger.hh,v 1.1 2009/07/07 21:39:19 jeremy Exp $
+
+#ifndef SLIC_VRML2WRITERMESSENGER_HH
+#define SLIC_VRML2WRITERMESSENGER_HH 1
+
+#include "G4UImessenger.hh"
+
+class G4UIcommand;
+class G4UIdirectory;
+class G4UIcmdWithAString;
+
+namespace slic
+{
+ class VRML2WriterMessenger : public G4UImessenger
+ {
+ public:
+ VRML2WriterMessenger();
+ virtual ~VRML2WriterMessenger();
+
+ public:
+ virtual void SetNewValue(G4UIcommand* cmd, G4String newVals);
+
+ private:
+ G4UIdirectory* m_vrmlDir;
+ G4UIcmdWithAString* m_writeVrmlCmd;
+ };
+}
+
+#endif
slic/src
diff -N VRML2Writer.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ VRML2Writer.cc 7 Jul 2009 21:39:19 -0000 1.1
@@ -0,0 +1,167 @@
+#include "VRML2Writer.hh"
+
+// stl
+#include <sstream>
+#include <iostream>
+
+// geant4
+#include "G4TransportationManager.hh"
+#include "G4VPhysicalVolume.hh"
+#include "G4LogicalVolume.hh"
+#include "G4VisAttributes.hh"
+#include "G4Box.hh"
+#include "G4RotationMatrix.hh"
+#include "G4ThreeVector.hh"
+#include "G4RotationMatrix.hh"
+#include "globals.hh"
+
+// clhep
+//#include <CLHEP/Vector/ThreeVector.h>
+
+namespace slic
+{
+
+ VRML2Writer::VRML2Writer(const::std::string& filename)
+ : _indentLevel(0)
+ {
+ _out.open(filename.c_str());
+ }
+
+ VRML2Writer::~VRML2Writer()
+ {
+ if (_out.is_open())
+ _out.close();
+ }
+
+ void VRML2Writer::write()
+ {
+ writeLine("#VRML V2.0 utf8");
+ processTopVolume( G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume() );
+ }
+
+ void VRML2Writer::processTopVolume(G4VPhysicalVolume* wv)
+ {
+ writeLine("Group {");
+ indent();
+ writeLine("children [");
+ indent();
+ processPhysicalVolume(wv);
+ unindent();
+ writeLine("]");
+ unindent();
+ writeLine("}"); // Group
+ }
+
+ void VRML2Writer::processPhysicalVolume(G4VPhysicalVolume* pv)
+ {
+ writeLine("Transform {");
+ indent();
+ writeLine("children [");
+ indent();
+
+ writeLine("Shape {");
+ indent();
+ processVisualization(pv);
+ processSolid(pv->GetLogicalVolume()->GetSolid());
+ unindent();
+ writeLine("}");
+
+ G4LogicalVolume* lv = pv->GetLogicalVolume();
+
+ // Recurse through daughter volumes
+ for (int i=0, ndau=lv->GetNoDaughters(); i < ndau; i++) {
+ processPhysicalVolume(lv->GetDaughter(i));
+ }
+
+ unindent();
+ writeLine("]"); // children
+
+ // translation
+ const G4ThreeVector& pos = pv->GetTranslation();
+ std::stringstream ss;
+ ss << "translation " << pos.x() / m << " " << pos.y() / m << " " << pos.z();
+ writeLine(ss.str());
+
+ // rotation
+
+ const G4RotationMatrix* rot = pv->GetRotation();
+ CLHEP::Hep3Vector axis(0,0,0);
+ double angle = 0;
+ if (rot != 0)
+ rot->getAngleAxis(angle, axis);
+ std::stringstream ss2;
+ ss2 << "rotation " << axis.x() << " " << axis.y() << " " << axis.z() << " " << angle;
+ rot = 0;
+ writeLine(ss2.str());
+
+ unindent();
+ writeLine("}"); // Transform
+ }
+
+ void VRML2Writer::processVisualization(G4VPhysicalVolume* pv)
+ {
+ const G4VisAttributes* vis = pv->GetLogicalVolume()->GetVisAttributes();
+ double r, g, b;
+ r = g = b = 1.0;
+ if ( vis != 0 )
+ {
+ const G4Colour& col = vis->GetColour();
+ r = col.GetRed();
+ g = col.GetGreen();
+ b = col.GetBlue();
+ }
+ writeLine("appearance Appearance {");
+ indent();
+ writeLine("material Material {");
+ indent();
+ std::stringstream ss;
+ ss << r << " " << g << " " << b;
+ writeLine("diffuseColor " + ss.str());
+ writeLine("transparency 0.7");
+ unindent();
+ writeLine("}");
+ unindent();
+ writeLine("}");
+ }
+
+ void VRML2Writer::processSolid(G4VSolid* solid)
+ {
+ if (dynamic_cast<G4Box*>(solid) != 0)
+ {
+ processBox((G4Box*)solid);
+ }
+ }
+
+ void VRML2Writer::processBox(G4Box* box)
+ {
+ writeLine("geometry Box {");
+ indent();
+ std::stringstream ss;
+ ss << "size "
+ << box->GetXHalfLength() / m << " "
+ << box->GetYHalfLength() / m << " "
+ << box->GetZHalfLength() / m;
+ writeLine(ss.str());
+ unindent();
+ writeLine("}");
+ }
+
+ void VRML2Writer::writeLine(const std::string& text)
+ {
+ for (int i=0; i<_indentLevel; i++)
+ {
+ _out << " ";
+ }
+ _out << text << std::endl;
+ }
+
+ void VRML2Writer::indent()
+ {
+ ++_indentLevel;
+ }
+
+ void VRML2Writer::unindent()
+ {
+ --_indentLevel;
+ }
+}
slic/src
diff -N VRML2WriterMessenger.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ VRML2WriterMessenger.cc 7 Jul 2009 21:39:19 -0000 1.1
@@ -0,0 +1,41 @@
+// $Header: /cvs/lcd/slic/src/VRML2WriterMessenger.cc,v 1.1 2009/07/07 21:39:19 jeremy Exp $
+
+#include "VRML2WriterMessenger.hh"
+
+// slic
+#include "VRML2Writer.hh"
+
+// geant4
+#include "G4UIcommand.hh"
+#include "G4UIdirectory.hh"
+#include "G4UIcmdWithAString.hh"
+
+namespace slic
+{
+
+ VRML2WriterMessenger::VRML2WriterMessenger()
+ {
+ m_vrmlDir = new G4UIdirectory("/vrml/");
+ m_vrmlDir->SetGuidance( "Test VRML2Writer. [SLIC]" );
+
+ m_writeVrmlCmd = new G4UIcmdWithAString( "/vrml/writeVRML", this );
+ m_writeVrmlCmd->SetGuidance( "Write VRML of current geometry to a file." );
+
+ }
+
+ VRML2WriterMessenger::~VRML2WriterMessenger()
+ {;}
+
+ void VRML2WriterMessenger::SetNewValue(G4UIcommand* cmd, G4String newVals)
+ {
+ G4String fname;
+ if (newVals == "")
+ fname = "geometry.wrl";
+ else
+ fname = newVals;
+ VRML2Writer* writer = new VRML2Writer(fname);
+ writer->write();
+ delete writer;
+ writer = 0;
+ }
+}
slic/src
diff -u -r1.47 -r1.48
--- SlicApplication.cc 10 Sep 2008 00:03:58 -0000 1.47
+++ SlicApplication.cc 7 Jul 2009 21:39:19 -0000 1.48
@@ -1,4 +1,4 @@
-// $Header: /cvs/lcd/slic/src/SlicApplication.cc,v 1.47 2008/09/10 00:03:58 jeremy Exp $
+// $Header: /cvs/lcd/slic/src/SlicApplication.cc,v 1.48 2009/07/07 21:39:19 jeremy Exp $
#include "SlicApplication.hh"
@@ -25,6 +25,9 @@
#include "TrajectoryManager.hh"
#include "HepPDTManager.hh"
+// test VRML writer
+#include "VRML2WriterMessenger.hh"
+
// lcdd
#include "LCDDDetectorConstruction.hh"
@@ -233,6 +236,9 @@
{
m_visManager = new VisManager();
m_visManager->Initialize();
+
+ // VRML test
+ new VRML2WriterMessenger();
}
#endif
CVSspam 0.2.8