CMS 3D CMS Logo

FW3DViewBase.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FW3DViewBase
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Thu Feb 21 11:22:41 EST 2008
11 //
12 #include <boost/bind.hpp>
13 
14 // user include files
15 
16 #include "TGButton.h"
17 #include "TGLScenePad.h"
18 #include "TGLViewer.h"
19 #include "TGLClip.h"
20 #include "TGLPerspectiveCamera.h"
21 #include "TEveManager.h"
22 #include "TEveElement.h"
23 #include "TEveLine.h"
24 #include "TEveBoxSet.h"
25 #include "TEveScene.h"
26 #include "TGLLogicalShape.h"
27 #include "TEveCalo.h"
28 #include "TEveCaloData.h"
29 #include "TEveStraightLineSet.h"
30 
39 
40 namespace {
41 class TGLClipsiLogical : public TGLLogicalShape
42 {
43 protected:
44  virtual void DirectDraw(TGLRnrCtx & rnrCtx) const override{}
45 
46 public:
47  TGLClipsiLogical() : TGLLogicalShape() {}
48  virtual ~TGLClipsiLogical() {}
49  void Resize(Double_t ext){}
50 };
51 
52 const float fgColor[4] = { 1.0, 0.6, 0.2, 0.5 };
53 
54 class Clipsi : public TGLClip
55 {
56 private:
57  TGLRnrCtx* m_rnrCtx;
58  Clipsi(const Clipsi&); // Not implemented
59  Clipsi& operator=(const Clipsi&); // Not implemented
60 
61  TGLVertex3 vtx[4];
62  TGLVertex3 appexOffset;
63 
64 public:
65  Clipsi(TGLRnrCtx* ctx):TGLClip(* new TGLClipsiLogical, TGLMatrix(), fgColor), m_rnrCtx(ctx){}
66  virtual ~Clipsi() {}
67  using TGLClip::Setup;
68  virtual void Setup(const TGLBoundingBox & bbox) override {}
69 
70  void SetPlaneInfo(TEveVector* vec)
71  {
72  for (int i = 0; i < 4; ++i) {
73  // vec[i].Dump();
74  vtx[i].Set(vec[i].fX + appexOffset.X(), vec[i].fY + appexOffset.Y(), vec[i].fZ + appexOffset.Z());
75  }
76  }
77 
78  void SetAppexOffset(TEveVector& vec)
79  {
80  appexOffset.Set(vec.fX, vec.fY, vec.fZ);
81  }
82 
83 
84  using TGLClip::PlaneSet;
85  virtual void PlaneSet(TGLPlaneSet_t & planeSet) const override
86  {
87  TGLVertex3 o = appexOffset;
88 
89  planeSet.push_back(TGLPlane(o, vtx[0], vtx[1]));
90  planeSet.push_back(TGLPlane(o, vtx[1], vtx[2]));
91  planeSet.push_back(TGLPlane(o, vtx[2], vtx[3]));
92  planeSet.push_back(TGLPlane(o, vtx[3], vtx[0]));
93  }
94 };
95 }
96 
98 //
99 //
100 // FW3DViewBase
101 //
102 //
103 //
105 FW3DViewBase::FW3DViewBase(TEveWindowSlot* iParent, FWViewType::EType typeId, unsigned int version):
106  FWEveView(iParent, typeId, version ),
107  m_geometry(0),
108  m_glClip(0),
109  m_showMuonBarrel(this, "Show Muon Barrel", 0l, 0l, 2l ),
110  m_showMuonEndcap(this, "Show Muon Endcap", false ),
111  m_showPixelBarrel(this, "Show Pixel Barrel", false ),
112  m_showPixelEndcap(this, "Show Pixel Endcap", false),
113  m_showTrackerBarrel(this, "Show Tracker Barrel", false ),
114  m_showTrackerEndcap(this, "Show Tracker Endcap", false),
115  m_ecalBarrel(0),
116  m_showEcalBarrel(this, "Show Ecal Barrel", false),
117  m_rnrStyle(this, "Render Style", 0l, 0l, 2l),
118  m_selectable(this, "Enable Tooltips", false),
119  m_cameraType(this, "Camera Type", 0l, 0l, 5l),
120  m_clipEnable(this, "Enable Clip", false),
121  m_clipTheta(this, "Clip Theta", 0.0, -5.0, 5.0),
122  m_clipPhi(this, "Clip Phi", 0.0, -2.0, 2.0),
123  m_clipDelta1(this, "Clip Delta1", 0.2, 0.01, 2),
124  m_clipDelta2(this, "Clip Delta2", 0.2, 0.01, 2),
125  m_clipAppexOffset(this, "Appex Offset", 10l, 0l, 50l),
126  m_DMT(0),
127  m_DMTline(0)
128 {
129  viewerGL()->SetCurrentCamera(TGLViewer::kCameraPerspXOZ);
131 
132  m_showMuonBarrel.addEntry(0, "Hide");
133  m_showMuonBarrel.addEntry(1, "Simplified");
134  m_showMuonBarrel.addEntry(2, "Full");
135  m_showMuonBarrel.changed_.connect(boost::bind(&FW3DViewBase::showMuonBarrel,this,_1));
136 
137  m_rnrStyle.addEntry(TGLRnrCtx::kFill, "Fill");
138  m_rnrStyle.addEntry(TGLRnrCtx::kOutline, "Outline");
139  m_rnrStyle.addEntry(TGLRnrCtx::kWireFrame, "WireFrame");
140  m_rnrStyle.changed_.connect(boost::bind(&FW3DViewBase::rnrStyle,this, _1));
141 
142  m_selectable.changed_.connect(boost::bind(&FW3DViewBase::selectable,this, _1));
143 
144 
145  m_cameraType.addEntry(TGLViewer::kCameraPerspXOZ,"PerspXOZ" );
146  m_cameraType.addEntry(TGLViewer::kCameraOrthoXOY,"OrthoXOY");
147  m_cameraType.addEntry(TGLViewer::kCameraOrthoXOZ,"OrthoXOZ");
148  m_cameraType.addEntry(TGLViewer::kCameraOrthoZOY,"OrthoZOY" );
149  m_cameraType.addEntry(TGLViewer::kCameraOrthoXnOY,"OrthoXnOY");
150  m_cameraType.addEntry(TGLViewer::kCameraOrthoXnOZ,"OrthoXnOZ");
151  m_cameraType.addEntry(TGLViewer::kCameraOrthoZnOY,"OrthoZnOY" );
152  m_cameraType.changed_.connect(boost::bind(&FW3DViewBase::setCameraType,this, _1));
153 
154  m_clipEnable.changed_.connect(boost::bind(&FW3DViewBase::enableSceneClip,this, _1));
155  m_clipTheta.changed_.connect(boost::bind(&FW3DViewBase::updateClipPlanes,this, false));
156  m_clipPhi.changed_.connect(boost::bind(&FW3DViewBase::updateClipPlanes,this, false));
157  m_clipDelta1.changed_.connect(boost::bind(&FW3DViewBase::updateClipPlanes,this, false));
158  m_clipDelta2.changed_.connect(boost::bind(&FW3DViewBase::updateClipPlanes,this, false));
159  m_clipAppexOffset.changed_.connect(boost::bind(&FW3DViewBase::updateClipPlanes,this, false));
160 
161 
162  m_ecalBarrel = new TEveBoxSet("ecalBarrel");
163  m_ecalBarrel->UseSingleColor();
164  m_ecalBarrel->SetMainColor(kAzure+10);
165  m_ecalBarrel->SetMainTransparency(98);
166  geoScene()->AddElement(m_ecalBarrel);
167 }
168 
170 {
171  delete m_glClip;
172 }
173 
175 {
176  FWEveView::setContext(context);
177 
178  m_geometry = new FW3DViewGeometry(context);
179  geoScene()->AddElement(m_geometry);
180 
186  m_showEcalBarrel.changed_.connect(boost::bind(&FW3DViewBase::showEcalBarrel, this,_1));
187 
188  // don't clip event scene -- ideally, would have TGLClipNoClip in root
189  TGLClipPlane* c=new TGLClipPlane();
190  c->Setup(TGLVector3(1e10,0,0), TGLVector3(-1,0,0));
191  eventScene()->GetGLScene()->SetClip(c);
192 
193  m_DMTline = new TEveLine();
194  m_DMTline->SetLineColor(1016);
195  m_DMTline->SetLineStyle(5);
196 
197 
198  m_DMTline->SetPoint(0, 0, 0, 0);
199  m_DMTline->SetPoint(1, 0, 0, 0);
200  eventScene()->AddElement(m_DMTline);
202 }
203 
205 {
206  if (m_geometry)
207  {
208  m_geometry->showMuonBarrel(x == 1);
210  }
211 }
212 
214 {
215  viewerGL()->RefCamera(TGLViewer::ECameraType(x)).IncTimeStamp();
216  viewerGL()->SetCurrentCamera(TGLViewer::ECameraType(x));
217 
218  //if (viewerGL()->CurrentCamera().IsOrthographic())
219  // ((TGLOrthoCamera*)(&viewerGL()->CurrentCamera()))->SetEnableRotate(1);
220 }
221 
222 void
224 {
225  geoScene()->GetGLScene()->SetStyle(x);
226  viewerGL()->Changed();
227  gEve->Redraw3D();
228 }
229 
230 void
232 {
233  geoScene()->GetGLScene()->SetSelectable(x);
234 }
235 void
237 {
238  if (m_glClip == 0) {
239  m_glClip = new Clipsi(viewerGL()->GetRnrCtx());
240 
241  m_glClip->SetMode(TGLClip::kOutside);
242  }
243 
244  geoScene()->GetGLScene()->SetClip(x ? m_glClip : 0);
245  for (TEveElement::List_i it =gEve->GetScenes()->BeginChildren(); it != gEve->GetScenes()->EndChildren(); ++it )
246  {
247  if (strncmp((*it)->GetElementName(), "TopGeoNodeScene", 15) == 0)
248  ((TEveScene*)(*it))->GetGLScene()->SetClip(x ? m_glClip : 0);
249  }
250  eventScene()->GetGLScene()->SetClip(x ? m_glClip : 0);
251  updateClipPlanes(true);
252  viewerGL()->RequestDraw();
253 }
254 
255 void
257 {
258  // called from popup menu via FWGUIManager
259 
260  // limit to 2 decimals, else TGNumber entry in the view controller shows only last 5 irrelevant digits
261  double base = 100.0;
262  int thetaInt = theta*base;
263  int phiInt = phi*base;
264  m_clipTheta.set(thetaInt/base);
265  m_clipPhi.set(phiInt/base);
266  m_clipEnable.set(true);
267 }
268 
269 namespace {
270 float getBBoxLineLength(TEveScene* scene, TEveVector in)
271 {
272  if (!scene->NumChildren()) return 0;
273 
274  scene->Repaint();
275  scene->GetGLScene()->CalcBoundingBox();
276  const TGLBoundingBox& bb = scene->GetGLScene()->BoundingBox();
277  if (bb.IsEmpty()) return 0;
278 
279  TGLPlaneSet_t ps; bb.PlaneSet(ps);
280  TEveVector inn = in; inn.Normalize();
281  inn *= 10000;
282  TGLLine3 line(TGLVertex3(), TGLVertex3(inn.fX, inn.fY, inn.fZ));
283  std::vector<float> res;
284  for (TGLPlaneSet_i i = ps.begin(); i!= ps.end(); ++i)
285  {
286  std::pair<Bool_t, TGLVertex3> r = Intersection(*i, line, false);
287  if(r.first) {
288  TGLVector3 vr(r.second.X(), r.second.Y(), r.second.Z());
289  res.push_back(vr.Mag());
290  }
291  }
292  std::sort(res.begin(), res.end());
293  return res.front();
294 }
295 
296 void setBBoxClipped(TGLBoundingBox& bbox, TEveVector dir, TEveVector b0, TEveVector b1, float fac)
297 {
298  dir *= fac;
299  b0 *= fac;
300  b1 *= fac;
301 
302  TEveVectorD bb[8];
303  bb[0] += b0; bb[0] += b1;
304  bb[1] -= b0; bb[1] += b1;
305  bb[2] -= b0; bb[2] -= b1;
306  bb[3] += b0; bb[3] -= b1;
307 
308  for (int i = 4; i < 8; ++i)
309  bb[i] = dir;
310 
311  bb[0+4] += b0; bb[0+4] += b1;
312  bb[1+4] -= b0; bb[1+4] += b1;
313  bb[2+4] -= b0; bb[2+4] -= b1;
314  bb[3+4] += b0; bb[3+4] -= b1;
315 
316  TGLVertex3 bbv[8];
317  for (int i = 0; i < 8; ++i) {
318  bbv[i].Set(bb[i].fX, bb[i].fY, bb[i].fZ);
319  }
320  bbox.Set(bbv);
321 }
322 }
323 
324 void
326 {
327  // TEveScene* gs = (TEveScene*)gEve->GetScenes()->FindChild(TString("TopGeoNodeScene"));
328  //printf("node scene %p\n", gs);
329  if (m_clipEnable.value())
330  {
331  float theta = m_clipTheta.value();
332  float phi = m_clipPhi.value();
333  using namespace TMath;
334  TEveVector in(Sin(theta)*Cos(phi), Sin(theta)*Sin(phi), Cos(theta));
335 
336  // one side of cross section plane is paralel to XY plane
337  TEveVector normXY(0., 1., 0);
338  TEveVector b0 = in.Cross(normXY);
339  TEveVector b1 = in.Cross(b0);
340 
341  float delta1 = m_clipDelta1.value();
342  float delta2 = m_clipDelta2.value();
343  b0.Normalize();
344  b0 *= Sin(delta1);
345  b1.Normalize();
346  b1 *= Sin(delta2);
347 
348  TEveVector c[4];
349  c[0] += b0; c[0] += b1;
350  c[1] -= b0; c[1] += b1;
351  c[2] -= b0; c[2] -= b1;
352  c[3] += b0; c[3] -= b1;
353  for (int i = 0; i < 4; ++i)
354  c[i] += in;
355 
356  TEveVector aOff = in; aOff.NegateXYZ();
357  aOff.Normalize(); aOff *= m_clipAppexOffset.value();
358  ((Clipsi*)m_glClip)->SetAppexOffset(aOff);
359 
360 
361  ((Clipsi*)m_glClip)->SetPlaneInfo(&c[0]);
362 
363  if (resetCamera) {
364  TGLBoundingBox bbox;
365  float es = getBBoxLineLength(eventScene(), in);
366  float gs = getBBoxLineLength(geoScene(), in);
367  setBBoxClipped(bbox, in, b0, b1, TMath::Max(es, gs));
368 
369  /*
370  TEvePointSet* bmarker = new TEvePointSet(8);
371  bmarker->Reset(4);
372  bmarker->SetName("bbox");
373  bmarker->SetMarkerColor(kOrange);
374  bmarker->SetMarkerStyle(3);
375  bmarker->SetMarkerSize(0.2);
376  for (int i = 0; i < 8; ++i)
377  bmarker->SetPoint(i, bbox[i].X(), bbox[i].Y(), bbox[i].Z());
378  eventScene()->AddElement(bmarker);
379  */
380 
381 
382  TGLCamera& cam = viewerGL()->CurrentCamera();
383  cam.SetExternalCenter(true);
384  cam.SetCenterVec(bbox.Center().X(), bbox.Center().Y(), bbox.Center().Z());
385  cam.Setup(bbox, true);
386  }
387  else {
388  eventScene()->Repaint();
389  }
390  }
391 
392  gEve->Redraw3D();
393 
394 }
395 
396 //______________________________________________________________________________
397 void
399 {
400  // take care of parameters
401  FWEveView::addTo(iTo);
402  TGLPerspectiveCamera* camera = dynamic_cast<TGLPerspectiveCamera*>(&(viewerGL()->CurrentCamera()));
403  if (camera)
404  addToPerspectiveCamera(camera, "Plain3D", iTo);
405 }
406 
407 //______________________________________________________________________________
408 void
410 {
411  // take care of parameters
412  FWEveView::setFrom(iFrom);
413 
414  TGLPerspectiveCamera* camera = dynamic_cast<TGLPerspectiveCamera*>(&(viewerGL()->CurrentCamera()));
415  if (camera)
416  setFromPerspectiveCamera(camera, "Plain3D", iFrom);
417 
418  if (iFrom.version() < 5)
419  {
420  // transparency moved to common preferences in FWEveView version 5
421  std::string tName("Detector Transparency");
422  std::istringstream s(iFrom.valueForKey(tName)->value());
423  int transp;
424  s>> transp;
425  context().colorManager()->setGeomTransparency(transp, false);
426  }
427 }
428 
430 {
432 }
433 
434 void FW3DViewBase::setCurrentDMTVertex(double x, double y, double z)
435 {
437  printf( "ERROR!!!! FW3DViewBase::setCurrentDMTVertex \n");
438 
439  m_DMTline->SetPoint(m_DMT->m_action, x, y, z);
440  m_DMTline->ElementChanged();
441  viewerGL()->RequestDraw();
442 
443  m_DMT->refCurrentVertex().Set(x, y,z);
444  m_DMT->resetAction();
445 }
446 
447 void
449 {
451 
452 
453  gui.requestTab("Detector").
454  addParam(&m_showMuonBarrel).
455  addParam(&m_showMuonEndcap).
456  addParam(&m_showTrackerBarrel).
457  addParam(&m_showTrackerEndcap).
458  addParam(&m_showPixelBarrel).
459  addParam(&m_showPixelEndcap).
460  addParam(&m_showEcalBarrel).
461  addParam(&m_rnrStyle).
462  addParam(&m_selectable);
463 
464  gui.requestTab("Clipping").
465  addParam(&m_clipEnable).
466  addParam(&m_clipTheta).
467  addParam(&m_clipPhi).
468  addParam(&m_clipDelta1).
469  addParam(&m_clipDelta2).
470  addParam(&m_clipAppexOffset);
471 
472 
473  gui.requestTab("Style").separator();
474  gui.getTabContainer()->AddFrame(new TGTextButton(gui.getTabContainer(), "Root controls",
475  Form("TEveGedEditor::SpawnNewEditor((TGLViewer*)0x%lx)", (unsigned long)viewerGL())));
476 
477  gui.requestTab("Tools").addParam(&m_cameraType).separator();
478  gui.getTabContainer()->AddFrame(m_DMT->buildGUI( gui.getTabContainer()), new TGLayoutHints(kLHintsExpandX, 2, 2, 2, 2));
479 }
480 
482  if (x && m_ecalBarrel->GetPlex()->Size() == 0) {
483  const FWGeometry* geom = context().getGeom();
484  std::vector<unsigned int> ids = geom->getMatchedIds(FWGeometry::Detector::Ecal, FWGeometry::SubDetector::PixelBarrel);
485  m_ecalBarrel->Reset(TEveBoxSet::kBT_FreeBox, true, ids.size() );
486  for (std::vector<unsigned int>::iterator it = ids.begin(); it != ids.end(); ++it) {
487  const float* cor = context().getGeom()->getCorners(*it);
488  m_ecalBarrel->AddBox(cor);
489  }
490  m_ecalBarrel->RefitPlex();
491  }
492 
493  if (m_ecalBarrel->GetRnrSelf() != x) {
494  m_ecalBarrel->SetRnrSelf(x);
495  gEve->Redraw3D();
496  }
497 
498  // disable enable grid in 3DView
499  if (typeId() == FWViewType::k3D) {
500  TEveElement* calo = eventScene()->FindChild("calo barrel");
501  if (calo) {
502  TEveCalo3D* c3d = dynamic_cast<TEveCalo3D*>(calo);
503  c3d->SetRnrFrame(!x, !x);
504  c3d->ElementChanged();
505  }
506  }
507 }
virtual ~FW3DViewBase()
FWDoubleParameter m_clipTheta
Definition: FW3DViewBase.h:96
virtual bool requestGLHandlerPick() const
TEveScene * eventScene()
Definition: FWEveView.h:82
void showEcalBarrel(bool)
void showMuonBarrel(long)
void rnrStyle(long)
virtual void setFrom(const FWConfiguration &)
FWBoolParameter m_showMuonEndcap
Definition: FW3DViewBase.h:81
void showMuonBarrel(bool)
void setClip(float eta, float phi)
const FWGeometry * getGeom() const
Definition: Context.h:83
FWColorManager * colorManager() const
Definition: Context.h:65
ViewerParameterGUI & requestTab(const char *)
void showTrackerEndcap(bool)
ViewerParameterGUI & separator()
Geom::Theta< T > theta() const
TGCompositeFrame * buildGUI(TGCompositeFrame *p)
unsigned int version() const
void setCurrentDMTVertex(double x, double y, double z)
FWBoolParameter m_showTrackerBarrel
Definition: FW3DViewBase.h:84
TGLViewer * viewerGL() const
Definition: FWEveView.cc:190
sigc::signal< void, T > changed_
FW3DViewDistanceMeasureTool * m_DMT
Definition: FW3DViewBase.h:103
void selectable(bool)
void showMuonBarrelFull(bool)
void setCameraType(long)
Definition: Electron.h:4
FWDoubleParameter m_clipPhi
Definition: FW3DViewBase.h:97
void showTrackerBarrel(bool)
void enableSceneClip(bool)
void setGeomTransparency(Color_t idx, bool projectedType)
virtual void addTo(FWConfiguration &) const
virtual void populateController(ViewerParameterGUI &) const
void showMuonEndcap(bool)
FWEnumParameter m_rnrStyle
Definition: FW3DViewBase.h:90
void updateClipPlanes(bool resetCamera)
FWBoolParameter m_clipEnable
Definition: FW3DViewBase.h:95
FWBoolParameter m_showPixelEndcap
Definition: FW3DViewBase.h:83
FWDoubleParameter m_clipDelta1
Definition: FW3DViewBase.h:98
bool addEntry(Long_t id, const std::string &txt)
virtual void setContext(const fireworks::Context &)
virtual void resetCamera()
Definition: FWEveView.cc:264
base
Make Sure CMSSW is Setup ##.
T Max(T a, T b)
Definition: MathUtil.h:44
const std::string & value(unsigned int iIndex=0) const
const fireworks::Context & context()
Definition: FWEveView.h:67
const float * getCorners(unsigned int id) const
Definition: FWGeometry.cc:286
void setFrom(const FWConfiguration &) override
Definition: FWEveView.cc:369
void setFromPerspectiveCamera(TGLPerspectiveCamera *, const std::string &, const FWConfiguration &)
Definition: FWEveView.cc:504
void showPixelBarrel(bool)
FWBoolParameter m_showEcalBarrel
Definition: FW3DViewBase.h:88
TEveScene * geoScene()
Definition: FWEveView.h:83
TGLClip * m_glClip
Definition: FW3DViewBase.h:77
ViewerParameterGUI & addParam(const FWParameterBase *)
TGCompositeFrame * getTabContainer()
void showPixelEndcap(bool)
void addTo(FWConfiguration &) const override
Definition: FWEveView.cc:351
std::vector< unsigned int > getMatchedIds(Detector det, SubDetector subdet) const
Definition: FWGeometry.cc:203
TEveLine * m_DMTline
Definition: FW3DViewBase.h:104
TEveBoxSet * m_ecalBarrel
Definition: FW3DViewBase.h:87
FWEnumParameter m_showMuonBarrel
Definition: FW3DViewBase.h:80
FW3DViewBase(TEveWindowSlot *, FWViewType::EType, unsigned int version=8)
FW3DViewGeometry * m_geometry
Definition: FW3DViewBase.h:76
const FWConfiguration * valueForKey(const std::string &iKey) const
dbl *** dir
Definition: mlp_gen.cc:35
Definition: AbsArchive.cc:53
FWBoolParameter m_selectable
Definition: FW3DViewBase.h:91
FWEnumParameter m_cameraType
Definition: FW3DViewBase.h:93
Definition: memstream.h:15
FWLongParameter m_clipAppexOffset
Definition: FW3DViewBase.h:100
virtual void setContext(const fireworks::Context &x)
Definition: FWEveView.cc:271
FWBoolParameter m_showTrackerEndcap
Definition: FW3DViewBase.h:85
FWDoubleParameter m_clipDelta2
Definition: FW3DViewBase.h:99
void populateController(ViewerParameterGUI &) const override
Definition: FWEveView.cc:556
FWBoolParameter m_showPixelBarrel
Definition: FW3DViewBase.h:82
FWViewType::EType typeId() const
Definition: FWViewBase.h:43
void addToPerspectiveCamera(TGLPerspectiveCamera *, const std::string &, FWConfiguration &) const
Definition: FWEveView.cc:477