| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Types.cpp

Go to the documentation of this file.
00001 // This :
00002 #include "Types.h"
00003 
00004 // Lib :
00005 #include <Lib/Property.h>
00006 #include <Lib/Variable.h>
00007 #include <Lib/Manager.h>
00008 
00009 // DetDesc :
00010 #include <DetDesc/ILVolume.h>
00011 #include <DetDesc/IPVolume.h>
00012 #include <DetDesc/ISolid.h>
00013 #include <DetDesc/IGeometryInfo.h>
00014 #include <DetDesc/Material.h>
00015 
00016 // OnXSvc :
00017 #include <OnXSvc/Helpers.h>
00018 
00019 // HEPVis :
00020 #include <HEPVis/misc/SoStyleCache.h>
00021 
00022 #ifdef WIN32
00023 #include <sstream>
00024 #endif
00025 
00029 LVolumeType::LVolumeType(
00030  IPrinter& aPrinter
00031 )
00032 :BaseType(aPrinter)
00033 ,fType("LVolume")
00036 {
00037   addProperty("id",Lib::Property::POINTER);
00038   addProperty("name",Lib::Property::STRING,70);
00039   addProperty("material",Lib::Property::STRING,70);
00040   addProperty("solid",Lib::Property::STRING,70);
00041 }
00043 std::string LVolumeType::name() const
00046 {
00047   return fType;
00048 }
00050 Lib::Variable LVolumeType::value(
00051  Lib::Identifier aIdentifier
00052 ,const std::string& aName
00053 ,void*
00054 ) 
00055 
00056 
00057 {
00058   const ILVolume* obj = (const ILVolume*)aIdentifier;
00059   if(aName=="id") {
00060     return Lib::Variable(printer(),(void*)obj);
00061   } else if(aName=="name") {
00062     return Lib::Variable(printer(),obj->name());
00063   } else if(aName=="material") {
00064     const Material* material = obj->material();
00065     return Lib::Variable(printer(),material ? material->name() : "(nil)");
00066   } else if(aName=="solid") {
00067     const ISolid* solid = obj->solid();
00068     if(solid) {
00069 #if defined(__GNUC__) && (__GNUC__ <= 2)
00070       std::ostrstream os;
00071       os << solid << std::ends;
00072       const std::string s(os.str());
00073       return Lib::Variable(printer(),s);
00074 #else
00075       std::ostringstream os;
00076       os << solid << std::ends;
00077       return Lib::Variable(printer(),os.str());
00078 #endif
00079     } else {
00080       const std::string s = "(nil)";
00081       return Lib::Variable(printer(),s);
00082     }
00083   } else {
00084     return Lib::Variable(printer());
00085   }
00086 }
00090 // Inventor :
00091 #include <Inventor/nodes/SoSeparator.h>
00092 #include <Inventor/nodes/SoIndexedFaceSet.h>
00093 #include <Inventor/nodes/SoNormal.h>
00094 #include <Inventor/nodes/SoNormalBinding.h>
00095 #include <Inventor/nodes/SoCoordinate3.h>
00096 #include <Inventor/nodes/SoMaterial.h>
00097 #include <Inventor/nodes/SoDrawStyle.h>
00098 #include <Inventor/nodes/SoLightModel.h>
00099 
00100 // HEPVis :
00101 #include <HEPVis/nodes/SoHighlightMaterial.h>
00102 
00103 // Gaudi :
00104 #include <GaudiKernel/IMagneticFieldSvc.h>
00105 
00106 // OnXSvc :
00107 #include <OnXSvc/IUserInterfaceSvc.h>
00108 
00109 // Lib :
00110 #include <Lib/Interfaces/IIterator.h>
00111 #include <Lib/Interfaces/ISession.h>
00112 #include <Lib/smanip.h>
00113 
00114 // OnX :
00115 #include <OnX/Interfaces/IUI.h>
00116 
00117 #define MINIMUM(a,b) ((a)<(b)?a:b)
00118 #define MAXIMUM(a,b) ((a)>(b)?a:b)
00119 
00121 MagneticFieldType::MagneticFieldType(
00122  IUserInterfaceSvc* aUISvc
00123 ,IMagneticFieldSvc* aMagneticFieldSvc
00124 )
00125 :OnX::BaseType(aUISvc->printer())
00126 ,fName("MagneticField")
00127 ,fUISvc(aUISvc)
00128 ,fMagneticFieldSvc(aMagneticFieldSvc)
00131 {
00132   addProperty("id",Lib::Property::POINTER);
00133 }
00135 std::string MagneticFieldType::name() const
00138 {
00139   return fName;
00140 }
00142 Lib::IIterator* MagneticFieldType::iterator(
00143 ) 
00144 
00145 
00146 {
00147   // One shoot only iterator.
00148   class Iterator : public Lib::IIterator {
00149   public: //Lib::IIterator
00150     virtual Lib::Identifier object() { return fMagneticFieldSvc; }
00151     virtual void* tag() { return 0; }
00152     virtual void next() {fMagneticFieldSvc = 0;}
00153   public:
00154     Iterator(IMagneticFieldSvc* aMagneticFieldSvc)
00155       :fMagneticFieldSvc(aMagneticFieldSvc){}
00156   private:
00157     IMagneticFieldSvc* fMagneticFieldSvc;
00158   };
00159 
00160   return new Iterator(fMagneticFieldSvc);
00161 }
00163 Lib::Variable MagneticFieldType::value(
00164  Lib::Identifier aIdentifier
00165 ,const std::string& aName
00166 ,void*
00167 ) 
00168 
00169 
00170 {
00171   if(aName=="id") {
00172     return Lib::Variable(printer(),(void*)aIdentifier);
00173   } else {
00174     return Lib::Variable(printer());
00175   }
00176 }
00178 void MagneticFieldType::visualize(
00179  Lib::Identifier aIdentifier
00180 ,void*
00181 ) 
00182 
00183 // We subdivide a cutting plan into cells (then we have a cutting grid).
00184 // At each center of the cell we compute the magnetic field strength.
00185 // We colorize the cell according the field value.
00186 // Cells with a field value below "fieldMin" are not drawn.
00187 // See SoDet/scripts/Python/MagneticField.py for a triggering
00188 // of the representation from python.
00190 {
00191   if(!fMagneticFieldSvc) return;
00192   if(!fUISvc) return;
00193   if(!fUISvc->session()) return;
00194   ISession& session = *(fUISvc->session());
00195 
00196   SoRegion* soRegion = fUISvc->currentSoRegion();
00197   if(!soRegion) return;
00198 
00199   std::string value;
00200   double dvalue,d1,d2,d3;
00201   std::vector<double> ds;
00202   std::vector<int> ls;
00203 
00204   // Default values :
00205   float sx = 100;  // Size of a cell in x.
00206   float sy = 100;  // Size of a cell in y.
00207   unsigned int nx = 80; // Number of cell in x.
00208   unsigned int ny = 80; // Number of cell in y.
00209   SbVec3f gridStart(-(sx * nx)/2,-(sy * ny)/2,1000);
00210   SbVec3f gridX(1,0,0);
00211   SbVec3f gridY(0,1,0);
00212   float fieldMin = 0.0001F; // Below that, field is colored in grey.
00213   float fieldMax = 0.003F;
00214   float colorMin = 0.3F; // Color for fieldMin.
00215   float colorMax = 1;    // Color for fieldMax.
00216   float hr = 1, hg = 1, hb = 1;
00217   float transparency = 0;
00218 
00219   if(session.parameterValue("modeling.magneticField.gridPosition",value))
00220     if(Lib::smanip::todoubles(value,ds) && (ds.size()==3) ) {
00221       gridStart.setValue((float)ds[0],(float)ds[1],(float)ds[2]);
00222     }
00223   if(session.parameterValue("modeling.magneticField.gridX",value)) 
00224     if(Lib::smanip::todoubles(value,ds) && (ds.size()==3) ) {
00225       gridX.setValue((float)ds[0],(float)ds[1],(float)ds[2]);
00226     }
00227   if(session.parameterValue("modeling.magneticField.gridY",value))
00228     if(Lib::smanip::todoubles(value,ds) && (ds.size()==3) ) {
00229       gridY.setValue((float)ds[0],(float)ds[1],(float)ds[2]);
00230     }
00231   if(session.parameterValue("modeling.magneticField.gridCells",value))
00232     if(Lib::smanip::toints(value,ls) && (ls.size()==2) ) {
00233       nx = ls[0];
00234       ny = ls[1];
00235     }
00236   if(session.parameterValue("modeling.magneticField.gridCellSize",value))
00237     if(Lib::smanip::todoubles(value,ds) && (ds.size()==2) ) {
00238       sx = (float)ds[0];
00239       sy = (float)ds[1];
00240     }
00241   if(session.parameterValue("modeling.magneticField.min",value))
00242     if(Lib::smanip::todouble(value,dvalue)) fieldMin = (float)dvalue;
00243   if(session.parameterValue("modeling.magneticField.max",value))
00244     if(Lib::smanip::todouble(value,dvalue)) fieldMax = (float)dvalue;
00245   if(session.parameterValue("modeling.magneticField.colorMin",value))
00246     if(Lib::smanip::todouble(value,dvalue)) colorMin = (float)dvalue;
00247   if(session.parameterValue("modeling.magneticField.colorMax",value))
00248     if(Lib::smanip::todouble(value,dvalue)) colorMax = (float)dvalue;
00249   if(session.parameterValue("modeling.transparency",value))
00250     if(Lib::smanip::todouble(value,dvalue)) transparency = (float)dvalue;
00251   if(session.parameterValue("modeling.highlightColor",value))
00252     if(Lib::smanip::torgb(value,d1,d2,d3)) { 
00253       hr = (float)d1; 
00254       hg = (float)d2; 
00255       hb = (float)d3;
00256     }
00257 
00258   if(fieldMax<=fieldMin) return;
00259   if(colorMax<=colorMin) return;
00260 
00261   // Build name (for picking) :
00262   std::string s;
00263   Lib::smanip::printf(s,128,"MagneticField/0x%lx",(unsigned long)aIdentifier);
00264   SbName name(s.c_str());
00265     
00266   bool sceneEmpty = true;
00267   SoSeparator* cells = new SoSeparator;
00268 
00269   SoCoordinate3* coordinate3 = new SoCoordinate3;
00270   cells->addChild(coordinate3);
00271 
00272   int32_t coordIndex[5];
00273   int icoord = 0;
00274 
00275   float colorSlope = (colorMax-colorMin)/(fieldMax-fieldMin);
00276   for(unsigned int ix=0;ix<nx;ix++) {
00277     for(unsigned int iy=0;iy<ny;iy++) {
00278 
00279       SbVec3f center = 
00280         gridStart + gridX * (ix * sx + sx/2) + gridY * (iy * sy + sy/2);
00281       Gaudi::XYZPoint point(center[0],center[1],center[2]);
00282       Gaudi::XYZVector field;
00283       fMagneticFieldSvc->fieldVector( point, field);
00284       float value = (float)field.mag2();
00285       value = sqrt(value);
00286       
00287       if( (value<fieldMin) || (value>=fieldMax) ) continue;
00288 
00289       float r = colorSlope * (value - fieldMin) + colorMin;
00290       float g = 0;
00291       float b = 0;
00292 
00293       //FIXME : 
00294       //  A SoStyleCache for the below materials slows down the creation
00295       //  We should have a gradient table.
00296       // Material :
00297       SoHighlightMaterial* highlightMaterial = new SoHighlightMaterial;
00298       highlightMaterial->setName("highlight");
00299       highlightMaterial->diffuseColor.setValue(SbColor(r,g,b));
00300       highlightMaterial->highlightColor.setValue(SbColor(hr,hg,hb));
00301       highlightMaterial->transparency.setValue(transparency);
00302       cells->addChild(highlightMaterial);
00303 
00304       int32_t pointn = 4;
00305       SbVec3f line[4];
00306       line[0] = gridStart + gridX * (ix * sx) + gridY * (iy * sy);
00307       line[1] = gridStart + gridX * (ix * sx + sx) + gridY * (iy * sy);
00308       line[2] = gridStart + gridX * (ix * sx + sx) + gridY * (iy * sy + sy);
00309       line[3] = gridStart + gridX * (ix * sx) + gridY * (iy * sy + sy);
00310       coordIndex[0] = icoord + 0;
00311       coordIndex[1] = icoord + 1;
00312       coordIndex[2] = icoord + 2;
00313       coordIndex[3] = icoord + 3;
00314       coordIndex[4] = SO_END_FACE_INDEX;
00315 
00316       coordinate3->point.setValues(icoord,pointn,line);
00317       icoord += pointn;
00318 
00319       SoIndexedFaceSet* faceSet = new SoIndexedFaceSet;
00320       faceSet->coordIndex.setValues(0,pointn+1,coordIndex);
00321       faceSet->setName(name);
00322       cells->addChild(faceSet);
00323 
00324       sceneEmpty = false;
00325     }
00326   }
00327   
00328   if(sceneEmpty) {
00329 
00330     cells->unref();
00331 
00332   } else {
00333     // Build scene graph :
00334     SoSeparator* separator = new SoSeparator;
00335     separator->setName("sceneGraph");
00336 
00337     SoStyleCache* styleCache = soRegion->styleCache();
00338 
00339     separator->addChild(styleCache->getLightModelBaseColor());
00340     separator->addChild(styleCache->getFilled());
00341     separator->addChild(styleCache->getNormalBindingPerFace());
00342     separator->addChild(styleCache->getNormalZ());
00343 
00344     separator->addChild(cells);
00345 
00346     soRegion->resetUndo();    
00347     region_addToStaticScene(*soRegion,separator);
00348 
00349   }
00350 
00351 }
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:02:29 2011 for SoDet by doxygen 1.4.7