CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWProxyBuilderBase.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWProxyBuilderBase
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones, Matevz Tadel, Alja Mrak-Tadel
10 // Created: Thu Mar 18 14:12:00 CET 2010
11 //
12 
13 // system include files
14 #include <iostream>
15 #include <boost/bind.hpp>
16 
17 // user include files
18 #include "TEveElement.h"
19 #include "TEveCompound.h"
20 #include "TEveManager.h"
21 #include "TEveProjectionManager.h"
22 #include "TEveSelection.h"
23 
30 
31 //
32 // constants, enums and typedefs
33 //
34 
35 //
36 // static data member definitions
37 //
38 
39 //
40 // constructors and destructor
41 //
42 
43 
44 FWProxyBuilderBase::Product::Product(FWViewType::EType t, const FWViewContext* c) : m_viewType(t), m_viewContext(c), m_elements(0)
45 {
46  m_elements = new TEveElementList("ProxyProduct");
47  m_elements->IncDenyDestroy();
48 }
49 
50 
52 {
53  // remove product from projected scene (RhoPhi or RhoZ)
54  TEveProjectable* pable = dynamic_cast<TEveProjectable*>(m_elements);
55  // don't have to check cast, because TEveElementList is TEveProjectable
56  for (TEveProjectable::ProjList_i i = pable->BeginProjecteds(); i != pable->EndProjecteds(); ++i)
57  {
58  TEveElement* projected = (*i)->GetProjectedAsElement();
59  (*projected->BeginParents())->RemoveElement(projected);
60  }
61 
62  // remove from 3D scenes
63  while (m_elements->HasParents())
64  {
65  TEveElement* parent = *m_elements->BeginParents();
66  parent->RemoveElement(m_elements);
67  }
68 
69  m_elements->Annihilate();
70 }
71 
72 //______________________________________________________________________________
73 
76  m_item(0),
80  m_layer(0)
81 {
82 }
83 
85 {
86  m_products.clear();
87 }
88 
89 //
90 // member functions
91 //
92 
93 void
95 {
96  m_item = iItem;
97 }
98 
99 void
101 {
102  bool oldValue = m_haveWindow;
103  m_haveWindow=iFlag;
104 
105  if(iFlag && !oldValue) {
106  //this is our first view so may need to rerun our building
107  if(m_mustBuild) {
108  build();
109  }
110  }
111 }
112 
113 void
115 {
116  m_item=0;
117 
118  cleanLocal();
119 
120  for (Product_it i = m_products.begin(); i!= m_products.end(); i++)
121  {
122 
123  (*i)->m_scaleConnection.disconnect();
124  delete (*i);
125  }
126 
127  m_products.clear();
128 }
129 
130 void
132 {
133  if (m_item)
134  {
135  try
136  {
137  size_t itemSize = m_item->size(); //cashed
138 
139  clean();
140  for (Product_it i = m_products.begin(); i != m_products.end(); ++i)
141  {
142  // printf("build() %s \n", m_item->name().c_str());
143  TEveElementList* elms = (*i)->m_elements;
144  size_t oldSize = elms->NumChildren();
145 
146  if (haveSingleProduct())
147  {
148  build(m_item, elms, (*i)->m_viewContext);
149  }
150  else
151  {
152  buildViewType(m_item, elms, (*i)->m_viewType, (*i)->m_viewContext);
153  }
154 
155  // Project all children of current product.
156  // If product is not registered into any projection-manager,
157  // this does nothing.
158  TEveProjectable* pable = dynamic_cast<TEveProjectable*>(elms);
159  if (pable->HasProjecteds())
160  {
161  for (TEveProjectable::ProjList_i i = pable->BeginProjecteds(); i != pable->EndProjecteds(); ++i)
162  {
163  TEveProjectionManager *pmgr = (*i)->GetManager();
164  Float_t oldDepth = pmgr->GetCurrentDepth();
165  pmgr->SetCurrentDepth(item()->layer());
166  size_t cnt = 0;
167 
168  TEveElement* projectedAsElement = (*i)->GetProjectedAsElement();
169  TEveElement::List_i parentIt = projectedAsElement->BeginChildren();
170  for (TEveElement::List_i prodIt = elms->BeginChildren(); prodIt != elms->EndChildren(); ++prodIt, ++cnt)
171  {
172  if (cnt < oldSize)
173  {
174  // reused projected holder
175  pmgr->SubImportChildren(*prodIt, *parentIt);
176  ++parentIt;
177  }
178  else if (cnt < itemSize)
179  {
180  // new product holder
181  pmgr->SubImportElements(*prodIt, projectedAsElement);
182  }
183  else
184  {
185  break;
186  }
187  }
188  pmgr->SetCurrentDepth(oldDepth);
189  }
190  }
191 
192 
193  if (m_interactionList && itemSize > oldSize)
194  {
195  TEveElement::List_i elIt = elms->BeginChildren();
196  for (size_t cnt = 0; cnt < itemSize; ++cnt, ++elIt)
197  {
198  if (cnt >= oldSize )
199  m_interactionList->added(*elIt, cnt);
200  }
201  }
202  }
203  }
204  catch (const std::runtime_error& iException)
205  {
206  fwLog(fwlog::kError) << "Caught exception in build function for item " << m_item->name() << ":\n"
207  << iException.what() << std::endl;
208  exit(1);
209  }
210  }
211  m_mustBuild = false;
212 }
213 
214 //______________________________________________________________________________
215 void
217 {
218  TEveElementList* elms = p->m_elements;
219  assert(m_item && static_cast<int>(m_item->size()) <= elms->NumChildren() && "can not use default modelChanges implementation");
220 
221  TEveElement::List_i itElement = elms->BeginChildren();
222  int index = 0;
223  for (FWModelIds::const_iterator it = iIds.begin(), itEnd = iIds.end();
224  it != itEnd;
225  ++it,++itElement,++index)
226  {
227  assert(itElement != elms->EndChildren());
228  while (index < it->index())
229  {
230  ++itElement;
231  ++index;
232  assert(itElement != elms->EndChildren());
233  }
234  if (visibilityModelChanges(*it, *itElement, p->m_viewType, p->m_viewContext))
235  {
236  elms->ProjectChild(*itElement);
237  }
238  else
239  {
240  localModelChanges(*it, *itElement, p->m_viewType, p->m_viewContext);
241  }
242  }
243 }
244 
245 void
247 {
248  if(m_haveWindow) {
249  for (Product_it i = m_products.begin(); i!= m_products.end(); ++i)
250  {
251  modelChanges(iIds, *i);
252  }
253  m_modelsChanged=false;
254  } else {
255  m_modelsChanged=true;
256  }
257 }
258 
259 //______________________________________________________________________________
260 void
262 {
263  if (iItem->layer() != m_layer)
264  setProjectionLayer(iItem->layer());
265 
266  if(m_haveWindow) {
267  build();
268  } else {
269  m_mustBuild=true;
270  }
271  m_modelsChanged=false;
272 }
273 
274 //______________________________________________________________________________
275 bool
277 {
278  if (m_item)
279  return (item.purpose() == m_item->purpose());
280 
281  return false;
282 }
283 
284 //______________________________________________________________________________
285 
286 TEveElementList*
288 {
289  if ( havePerViewProduct(viewType) == false && m_products.empty() == false)
290  {
291  if (haveSingleProduct())
292  {
293  return m_products.back()->m_elements;
294  }
295  else
296  {
297  for (Product_it i = m_products.begin(); i!= m_products.end(); ++i)
298  {
299  if (viewType == (*i)->m_viewType)
300  return (*i)->m_elements;
301  }
302  }
303  }
304 
305  // printf("new product %s for item %s \n", FWViewType::idToName(viewType).c_str(), item()->name().c_str()); fflush(stdout);
306 
307  Product* product = new Product(viewType, viewContext);
308  m_products.push_back(product);
309  if (viewContext)
310  {
311  product->m_scaleConnection = viewContext->scaleChanged_.connect(boost::bind(&FWProxyBuilderBase::scaleChanged, this, _1));
312  }
313 
314  if (item())
315  {
316  // debug info in eve browser
317  product->m_elements->SetElementName(item()->name().c_str());
318  }
319  return product->m_elements;
320 }
321 
322 //______________________________________________________________________________
323 
324 void
326 {
327  for (Product_it i = m_products.begin(); i!= m_products.end(); ++i)
328  {
329  if (havePerViewProduct(type) && (*i)->m_viewContext == vc)
330  {
331  if ((*i)->m_elements)
332  (*i)->m_elements->DestroyElements();
333 
334  if ( (*i)->m_viewContext)
335  (*i)->m_scaleConnection.disconnect();
336 
337  delete (*i);
338  m_products.erase(i);
339  break;
340  }
341  }
342 }
343 
344 //------------------------------------------------------------------------------
345 
346 void
348 {
349  // Called if willHandleInteraction() returns false. Purpose ignored by default.
350 
352 }
353 
354 
355 bool
357 {
358  return false;
359 }
360 
361 void
363 {
364  // Nothing to be done in base class.
365  // Visibility, main color and main transparency are handled through FWInteractionList.
366 }
367 
368 
369 void
371 {
372  for (Product_it i = m_products.begin(); i!= m_products.end(); ++i)
373  {
374  if ( havePerViewProduct((*i)->m_viewType) && (*i)->m_viewContext == vc)
375  {
376  scaleProduct((*i)->m_elements, (*i)->m_viewType, (*i)->m_viewContext);
377  }
378  }
379  gEve->Redraw3D();
380 }
381 
382 void
384 {
385  // Cleans local common element list.
386  for (Product_it i = m_products.begin(); i != m_products.end(); ++i)
387  {
388  if ((*i)->m_elements)
389  (*i)->m_elements->DestroyElements();
390  }
391 
392  cleanLocal();
393 }
394 
395 void
397 {
398  // Cleans local common element list.
399 }
400 
401 
402 void
403 FWProxyBuilderBase::build(const FWEventItem*, TEveElementList*, const FWViewContext*)
404 {
405  assert("virtual build(const FWEventItem*, TEveElementList*, const FWViewContext*) not implemented by inherited class");
406 }
407 
408 void
410 {
411  assert("virtual buildViewType(const FWEventItem*, TEveElementList*, FWViewType::EType, const FWViewContext*) not implemented by inherited class");
412 }
413 
414 void
416 {
417  m_layer = layer;
418  for (Product_it pIt = m_products.begin(); pIt != m_products.end(); ++pIt)
419  {
420  TEveProjectable* pable = static_cast<TEveProjectable*>((*pIt)->m_elements);
421  for (TEveProjectable::ProjList_i i = pable->BeginProjecteds(); i != pable->EndProjecteds(); ++i)
422  (*i)->SetDepth(m_layer);
423  }
424 }
425 
426 //------------------------------------------------------------------------------
427 
428 void
429 FWProxyBuilderBase::setupAddElement(TEveElement* el, TEveElement* parent, bool color) const
430 {
431  setupElement(el, color);
432  parent->AddElement(el);
433 }
434 
438 void
439 FWProxyBuilderBase::setupElement(TEveElement* el, bool color) const
440 {
441  el->CSCTakeAnyParentAsMaster();
442  el->SetPickable(true);
443 
444  if (color)
445  {
446  el->CSCApplyMainColorToMatchingChildren();
447  el->CSCApplyMainTransparencyToMatchingChildren();
448  el->SetMainColor(m_item->defaultDisplayProperties().color());
449  assert((m_item->defaultDisplayProperties().transparency() >= 0)
450  && (m_item->defaultDisplayProperties().transparency() <= 100));
451  el->SetMainTransparency(m_item->defaultDisplayProperties().transparency());
452  }
453 }
454 
455 //------------------------------------------------------------------------------
456 
457 TEveCompound*
458 FWProxyBuilderBase::createCompound(bool set_color, bool propagate_color_to_all_children) const
459 {
460  TEveCompound* c = new TEveCompound();
461  c->CSCTakeAnyParentAsMaster();
462  c->CSCImplySelectAllChildren();
463  c->SetPickable(true);
464  if (set_color)
465  {
466  c->SetMainColor(m_item->defaultDisplayProperties().color());
467  c->SetMainTransparency(m_item->defaultDisplayProperties().transparency());
468  }
469  if (propagate_color_to_all_children)
470  {
471  c->CSCApplyMainColorToAllChildren();
472  c->CSCApplyMainTransparencyToAllChildren();
473  }
474  else
475  {
476  c->CSCApplyMainColorToMatchingChildren();
477  c->CSCApplyMainTransparencyToMatchingChildren();
478  }
479  return c;
480 }
481 
482 void
484  const std::string& name, Char_t transpOffset)
485 {
486  // Helper function to increse transparency of certain components.
487 
488  const FWDisplayProperties& dp = item()->modelInfo(index).displayProperties();
489  Char_t transp = TMath::Min(100, transpOffset + (100 - transpOffset) * dp.transparency() / 100);
490  TEveElement::List_t matches;
491  holder->FindChildren(matches, name.c_str());
492  for (TEveElement::List_i m = matches.begin(); m != matches.end(); ++m)
493  {
494  (*m)->SetMainTransparency(transp);
495  }
496 }
497 
498 //
499 // const member functions
500 //
501 
502 const fireworks::Context&
504 {
505  return m_item->context();
506 }
507 
508 int
510 {
511  return m_item->layer();
512 }
513 
514 //
515 // static member functions
516 //
517 
519 {
520  return std::string();
521 }
522 
524 {
525  return false;
526 }
type
Definition: HCALResponse.h:21
const fireworks::Context & context() const
virtual void buildViewType(const FWEventItem *iItem, TEveElementList *, FWViewType::EType, const FWViewContext *)
int i
Definition: DBlmapReader.cc:9
virtual void scaleProduct(TEveElementList *parent, FWViewType::EType, const FWViewContext *vc)
std::vector< Product * >::iterator Product_it
const FWDisplayProperties & defaultDisplayProperties() const
Definition: FWEventItem.cc:451
const FWDisplayProperties & displayProperties() const
Definition: FWEventItem.h:67
static std::string typeOfBuilder()
Used by the plugin system to determine how the proxy uses the data from FWEventItem.
std::vector< Product * > m_products
list parent
Definition: dbtoconf.py:74
virtual void itemBeingDestroyed(const FWEventItem *)
virtual bool havePerViewProduct(FWViewType::EType) const
const std::string & name() const
Definition: FWEventItem.cc:500
virtual bool visibilityModelChanges(const FWModelId &, TEveElement *, FWViewType::EType, const FWViewContext *)
void setupAddElement(TEveElement *el, TEveElement *parent, bool set_color=true) const
static bool representsSubPart()
const FWViewContext * m_viewContext
T Min(T a, T b)
Definition: MathUtil.h:39
virtual void cleanLocal()
void scaleChanged(const FWViewContext *)
const FWEventItem * m_item
void added(TEveElement *, unsigned int)
const FWEventItem * item() const
std::list< Particle > List_t
Definition: Particle.h:143
Color_t color() const
int layer() const
Definition: FWEventItem.cc:457
sigc::connection m_scaleConnection
virtual void localModelChanges(const FWModelId &iId, TEveElement *iCompound, FWViewType::EType viewType, const FWViewContext *vc)
Char_t transparency() const
sigc::signal< void, const FWViewContext * > scaleChanged_
Definition: FWViewContext.h:43
Product(FWViewType::EType t, const FWViewContext *c)
std::set< FWModelId > FWModelIds
void set_color(TEveElement *element, Color_t color, float alpha, unsigned int levels)
Definition: eve_macros.cc:154
virtual void setItem(const FWEventItem *iItem)
const fireworks::Context & context() const
Definition: FWEventItem.h:126
void removePerViewProduct(FWViewType::EType, const FWViewContext *vc)
void setHaveWindow(bool iFlag)
virtual bool haveSingleProduct() const
size_t size() const
Definition: FWEventItem.cc:548
#define fwLog(_level_)
Definition: fwLog.h:50
virtual bool canHandle(const FWEventItem &)
FWInteractionList * m_interactionList
TEveCompound * createCompound(bool set_color=true, bool propagate_color_to_all_children=false) const
TEveElementList * createProduct(FWViewType::EType, const FWViewContext *)
volatile std::atomic< bool > shutdown_flag false
ModelInfo modelInfo(int iIndex) const
Definition: FWEventItem.cc:535
const std::string & purpose() const
Definition: FWEventItem.cc:512
void setProjectionLayer(float)
void increaseComponentTransparency(unsigned int index, TEveElement *holder, const std::string &name, Char_t transpOffset)
void itemChanged(const FWEventItem *)
void modelChanges(const FWModelIds &)
virtual void setInteractionList(FWInteractionList *, const std::string &)
void setupElement(TEveElement *el, bool color=true) const