CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Fireworks/Core/src/FW3DViewBase.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FW3DViewBase
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Thu Feb 21 11:22:41 EST 2008
00011 // $Id: FW3DViewBase.cc,v 1.33 2013/05/01 05:00:09 amraktad Exp $
00012 //
00013 #include <boost/bind.hpp>
00014 
00015 // user include files
00016 
00017 #include "TGButton.h"
00018 #include "TGLScenePad.h"
00019 #include "TGLViewer.h"
00020 #include "TGLClip.h"
00021 #include "TGLPerspectiveCamera.h"
00022 #include "TEveManager.h"
00023 #include "TEveElement.h"
00024 #include "TEveLine.h"
00025 #include "TEveScene.h"
00026 #include "TGLLogicalShape.h"
00027 
00028 #include "Fireworks/Core/interface/FW3DViewBase.h"
00029 #include "Fireworks/Core/interface/FW3DViewGeometry.h"
00030 #include "Fireworks/Core/interface/Context.h"
00031 #include "Fireworks/Core/interface/FWViewContext.h"
00032 #include "Fireworks/Core/interface/FWViewEnergyScale.h"
00033 #include "Fireworks/Core/interface/CmsShowViewPopup.h"
00034 #include "Fireworks/Core/src/FW3DViewDistanceMeasureTool.h"
00035 
00036 namespace {
00037 class TGLClipsiLogical : public TGLLogicalShape
00038 {
00039 protected:
00040    virtual void DirectDraw(TGLRnrCtx & rnrCtx) const{}
00041 
00042 public:
00043    TGLClipsiLogical() : TGLLogicalShape() {}
00044    virtual ~TGLClipsiLogical() {}
00045    void Resize(Double_t ext){}
00046 };
00047 
00048 const float fgColor[4] = { 1.0, 0.6, 0.2, 0.5 };
00049 
00050 class Clipsi : public TGLClip
00051 {
00052 private:
00053    TGLRnrCtx* m_rnrCtx;
00054    Clipsi(const Clipsi&);            // Not implemented
00055    Clipsi& operator=(const Clipsi&); // Not implemented
00056 
00057 public:
00058    Clipsi(TGLRnrCtx* ctx):TGLClip(* new TGLClipsiLogical, TGLMatrix(), fgColor), m_rnrCtx(ctx){}
00059    virtual ~Clipsi() {}
00060    virtual void Setup(const TGLBoundingBox & bbox) {}
00061    virtual void PlaneSet(TGLPlaneSet_t & planeSet) const
00062    {
00063       TGLCamera& cam = m_rnrCtx->RefCamera();
00064 
00065       TGLVertex3 f[4];
00066 
00067       f[0] =  Intersection(cam.FrustumPlane(TGLCamera::kFar),
00068                            cam.FrustumPlane(TGLCamera::kBottom),
00069                            cam.FrustumPlane(TGLCamera::kLeft)).second;
00070          
00071       f[1] =  Intersection(cam.FrustumPlane(TGLCamera::kFar),
00072                            cam.FrustumPlane(TGLCamera::kBottom),
00073                            cam.FrustumPlane(TGLCamera::kRight)).second;
00074          
00075       f[2] =  Intersection(cam.FrustumPlane(TGLCamera::kFar),
00076                            cam.FrustumPlane(TGLCamera::kTop),
00077                            cam.FrustumPlane(TGLCamera::kRight)).second;
00078 
00079       f[3] =  Intersection(cam.FrustumPlane(TGLCamera::kFar),
00080                            cam.FrustumPlane(TGLCamera::kTop),
00081                            cam.FrustumPlane(TGLCamera::kLeft)).second;
00082 
00083       TGLVector3 dd  =  cam.FrustumPlane(TGLCamera::kNear).Norm();
00084       dd *= (cam.GetFarClip() -cam.GetNearClip() );
00085 
00086       f[0] -= dd;
00087       f[1] -= dd;
00088       f[2] -= dd;
00089       f[3] -= dd;
00090 
00091       TGLVertex3 c;//(cam.GetCenterVec());
00092       planeSet.push_back(TGLPlane(c, f[0], f[1]));
00093       planeSet.push_back(TGLPlane(c, f[1], f[2]));
00094       planeSet.push_back(TGLPlane(c, f[2], f[3]));
00095       planeSet.push_back(TGLPlane(c, f[3], f[0]));
00096    }
00097 };
00098 }
00099 
00101 // 
00102 //
00103 //                  FW3DViewBase
00104 //
00105 // 
00106 //
00108 FW3DViewBase::FW3DViewBase(TEveWindowSlot* iParent, FWViewType::EType typeId):
00109    FWEveView(iParent, typeId, 8),
00110    m_geometry(0),
00111    m_glClip(0),
00112    m_showMuonBarrel(this, "Show Muon Barrel",  0l, 0l, 2l ),
00113    m_showMuonEndcap(this, "Show Muon Endcap", false ),
00114    m_showPixelBarrel(this, "Show Pixel Barrel", false ),
00115    m_showPixelEndcap(this, "Show Pixel Endcap", false),
00116    m_showTrackerBarrel(this, "Show Tracker Barrel", false ),
00117    m_showTrackerEndcap(this, "Show Tracker Endcap", false),
00118    m_rnrStyle(this, "Render Style", 0l, 0l, 2l),
00119    m_clipParam(this, "View dependent Clip", false),
00120    m_selectable(this, "Enable Tooltips", false),
00121    m_cameraType(this, "Camera Type", 0l, 0l, 5l),
00122    m_DMT(0),
00123    m_DMTline(0)
00124 {
00125    viewerGL()->SetCurrentCamera(TGLViewer::kCameraPerspXOZ);
00126    m_DMT = new FW3DViewDistanceMeasureTool();
00127 
00128    m_showMuonBarrel.addEntry(0, "Hide");
00129    m_showMuonBarrel.addEntry(1, "Simplified");
00130    m_showMuonBarrel.addEntry(2, "Full");
00131    m_showMuonBarrel.changed_.connect(boost::bind(&FW3DViewBase::showMuonBarrel,this,_1));
00132 
00133    m_rnrStyle.addEntry(TGLRnrCtx::kFill, "Fill");
00134    m_rnrStyle.addEntry(TGLRnrCtx::kOutline, "Outline");
00135    m_rnrStyle.addEntry(TGLRnrCtx::kWireFrame, "WireFrame");
00136    m_rnrStyle.changed_.connect(boost::bind(&FW3DViewBase::rnrStyle,this, _1));
00137    m_clipParam.changed_.connect(boost::bind(&FW3DViewBase::sceneClip,this, _1));
00138 
00139    m_selectable.changed_.connect(boost::bind(&FW3DViewBase::selectable,this, _1));
00140 
00141 
00142    m_cameraType.addEntry(TGLViewer::kCameraPerspXOZ,"PerspXOZ" );
00143    m_cameraType.addEntry(TGLViewer::kCameraOrthoXOY,"OrthoXOY");
00144    m_cameraType.addEntry(TGLViewer::kCameraOrthoXOZ,"OrthoXOZ");
00145    m_cameraType.addEntry(TGLViewer::kCameraOrthoZOY,"OrthoZOY" );  
00146    m_cameraType.addEntry(TGLViewer::kCameraOrthoXnOY,"OrthoXnOY");
00147    m_cameraType.addEntry(TGLViewer::kCameraOrthoXnOZ,"OrthoXnOZ");
00148    m_cameraType.addEntry(TGLViewer::kCameraOrthoZnOY,"OrthoZnOY" );  
00149    m_cameraType.changed_.connect(boost::bind(&FW3DViewBase::setCameraType,this, _1));
00150 }
00151 
00152 FW3DViewBase::~FW3DViewBase()
00153 {
00154   delete m_glClip;
00155 }
00156 
00157 void FW3DViewBase::setContext(const fireworks::Context& context)
00158 {
00159    FWEveView::setContext(context);
00160 
00161    m_geometry = new FW3DViewGeometry(context);
00162    geoScene()->AddElement(m_geometry);
00163    
00164    m_showPixelBarrel.changed_.connect(boost::bind(&FW3DViewGeometry::showPixelBarrel,m_geometry,_1));
00165    m_showPixelEndcap.changed_.connect(boost::bind(&FW3DViewGeometry::showPixelEndcap,m_geometry,_1));
00166    m_showTrackerBarrel.changed_.connect(boost::bind(&FW3DViewGeometry::showTrackerBarrel,m_geometry,_1));
00167    m_showTrackerEndcap.changed_.connect(boost::bind(&FW3DViewGeometry::showTrackerEndcap,m_geometry,_1));
00168    m_showMuonEndcap.changed_.connect(boost::bind(&FW3DViewGeometry::showMuonEndcap,m_geometry,_1));
00169    
00170    // don't clip event scene --  ideally, would have TGLClipNoClip in root
00171    TGLClipPlane* c=new TGLClipPlane();
00172    c->Setup(TGLVector3(1e10,0,0), TGLVector3(-1,0,0));
00173    eventScene()->GetGLScene()->SetClip(c);
00174 
00175    m_DMTline = new TEveLine();
00176    m_DMTline->SetLineColor(1016);
00177    m_DMTline->SetLineStyle(5);
00178 
00179 
00180    m_DMTline->SetPoint(0, 0, 0, 0);
00181    m_DMTline->SetPoint(1, 0, 0, 0);
00182    eventScene()->AddElement(m_DMTline);
00183 }
00184 
00185 void FW3DViewBase::showMuonBarrel(long x)
00186 {
00187    if (m_geometry)
00188    {
00189       m_geometry->showMuonBarrel(x == 1);
00190       m_geometry->showMuonBarrelFull(x == 2);
00191    }
00192 }
00193 
00194 void FW3DViewBase::setCameraType(long x)
00195 {
00196    viewerGL()->RefCamera(TGLViewer::ECameraType(x)).IncTimeStamp();
00197    viewerGL()->SetCurrentCamera(TGLViewer::ECameraType(x));
00198    
00199    //if (viewerGL()->CurrentCamera().IsOrthographic())
00200    //   ((TGLOrthoCamera*)(&viewerGL()->CurrentCamera()))->SetEnableRotate(1);
00201 }
00202 
00203 void
00204 FW3DViewBase::rnrStyle( long x)
00205 {
00206    geoScene()->GetGLScene()->SetStyle(x);  
00207    viewerGL()->Changed();
00208    gEve->Redraw3D();
00209 }
00210 
00211 void
00212 FW3DViewBase::selectable( bool x)
00213 {
00214       geoScene()->GetGLScene()->SetSelectable(x);
00215 }
00216 void
00217 FW3DViewBase::sceneClip( bool x)
00218 {
00219    if (m_glClip == 0)  {
00220       m_glClip = new Clipsi(viewerGL()->GetRnrCtx());
00221    }
00222 
00223    geoScene()->GetGLScene()->SetClip(x ? m_glClip : 0);
00224    for (TEveElement::List_i it =gEve->GetScenes()->BeginChildren(); it != gEve->GetScenes()->EndChildren(); ++it )
00225    {
00226       if (strncmp((*it)->GetElementName(), "TopGeoNodeScene", 15) == 0)
00227          ((TEveScene*)(*it))->GetGLScene()->SetClip(x ? m_glClip : 0);
00228    }
00229    viewerGL()->RequestDraw();
00230 }
00231 
00232 //______________________________________________________________________________
00233 void
00234 FW3DViewBase::addTo(FWConfiguration& iTo) const
00235 {
00236    // take care of parameters
00237    FWEveView::addTo(iTo);
00238    TGLPerspectiveCamera* camera = dynamic_cast<TGLPerspectiveCamera*>(&(viewerGL()->CurrentCamera()));
00239    if (camera)
00240       addToPerspectiveCamera(camera, "Plain3D", iTo);   
00241 }
00242 
00243 //______________________________________________________________________________
00244 void
00245 FW3DViewBase::setFrom(const FWConfiguration& iFrom)
00246 {
00247    // take care of parameters
00248    FWEveView::setFrom(iFrom);
00249 
00250    TGLPerspectiveCamera* camera = dynamic_cast<TGLPerspectiveCamera*>(&(viewerGL()->CurrentCamera()));
00251    if (camera)
00252       setFromPerspectiveCamera(camera, "Plain3D", iFrom);
00253 
00254    if (iFrom.version() < 5)
00255    {
00256       // transparency moved to common preferences in FWEveView version 5
00257       std::string tName("Detector Transparency");
00258       std::istringstream s(iFrom.valueForKey(tName)->value());
00259       int transp;
00260       s>> transp;
00261       context().colorManager()->setGeomTransparency(transp, false);
00262    }
00263 }
00264 
00265 bool FW3DViewBase::requestGLHandlerPick() const
00266 {
00267    return m_DMT->m_action != FW3DViewDistanceMeasureTool::kNone;
00268 }
00269 
00270 void FW3DViewBase::setCurrentDMTVertex(double x, double y, double z)
00271 {
00272   if (m_DMT->m_action == FW3DViewDistanceMeasureTool::kNone)
00273     printf( "ERROR!!!! FW3DViewBase::setCurrentDMTVertex \n");
00274 
00275    m_DMTline->SetPoint(m_DMT->m_action, x, y, z);
00276    m_DMTline->ElementChanged();
00277    viewerGL()->RequestDraw();
00278 
00279    m_DMT->refCurrentVertex().Set(x, y,z);
00280    m_DMT->resetAction();
00281 }
00282 
00283 void 
00284 FW3DViewBase::populateController(ViewerParameterGUI& gui) const
00285 {
00286    FWEveView::populateController(gui);
00287 
00288    gui.requestTab("Detector").
00289       addParam(&m_showMuonBarrel).
00290       addParam(&m_showMuonEndcap).
00291       addParam(&m_showTrackerBarrel).
00292       addParam(&m_showTrackerEndcap).
00293       addParam(&m_showPixelBarrel).
00294       addParam(&m_showPixelEndcap).  
00295       separator().
00296       addParam(&m_rnrStyle).
00297       addParam(&m_clipParam).
00298       addParam(&m_selectable);
00299 
00300 
00301    gui.requestTab("Style").separator();
00302    gui.getTabContainer()->AddFrame(new TGTextButton(gui.getTabContainer(), "Root controls",
00303                                                     Form("TEveGedEditor::SpawnNewEditor((TGLViewer*)0x%lx)", (unsigned long)viewerGL())));
00304 
00305    gui.requestTab("Tools").addParam(&m_cameraType).separator();
00306    gui.getTabContainer()->AddFrame(m_DMT->buildGUI( gui.getTabContainer()), new TGLayoutHints(kLHintsExpandX, 2, 2, 2, 2));
00307 
00308 }
00309 
00310 
00311