CMS 3D CMS Logo

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 <functional>
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 
44  : m_viewType(t), m_viewContext(c), m_elements(nullptr) {
45  m_elements = new TEveElementList("ProxyProduct");
46  m_elements->IncDenyDestroy();
47 }
48 
50  // remove product from projected scene (RhoPhi or RhoZ)
51  TEveProjectable* pable = dynamic_cast<TEveProjectable*>(m_elements);
52  // don't have to check cast, because TEveElementList is TEveProjectable
53  for (TEveProjectable::ProjList_i i = pable->BeginProjecteds(); i != pable->EndProjecteds(); ++i) {
54  TEveElement* projected = (*i)->GetProjectedAsElement();
55  (*projected->BeginParents())->RemoveElement(projected);
56  }
57 
58  // remove from 3D scenes
59  while (m_elements->HasParents()) {
60  TEveElement* parent = *m_elements->BeginParents();
61  parent->RemoveElement(m_elements);
62  }
63 
64  m_elements->Annihilate();
65 }
66 
67 //______________________________________________________________________________
68 
70  : m_interactionList(nullptr),
71  m_item(nullptr),
75  m_layer(0) {}
76 
78 
79 //
80 // member functions
81 //
82 
83 void FWProxyBuilderBase::setItem(const FWEventItem* iItem) { m_item = iItem; }
84 
86  bool oldValue = m_haveWindow;
87  m_haveWindow = iFlag;
88 
89  if (iFlag && !oldValue) {
90  //this is our first view so may need to rerun our building
91  if (m_mustBuild) {
92  build();
93  }
94  }
95 }
96 
98  m_item = nullptr;
99 
100  cleanLocal();
101 
102  for (Product_it i = m_products.begin(); i != m_products.end(); i++) {
103  (*i)->m_scaleConnection.disconnect();
104  delete (*i);
105  }
106 
107  m_products.clear();
108 }
109 
111  if (m_item) {
112  try {
113  size_t itemSize = m_item->size(); //cashed
114 
115  clean();
116  for (Product_it i = m_products.begin(); i != m_products.end(); ++i) {
117  //printf("build() %s \n", m_item->name().c_str());
118  TEveElementList* elms = (*i)->m_elements;
119  size_t oldSize = elms->NumChildren();
120 
121  if (haveSingleProduct()) {
122  build(m_item, elms, (*i)->m_viewContext);
123  } else {
124  buildViewType(m_item, elms, (*i)->m_viewType, (*i)->m_viewContext);
125  }
126 
127  // Project all children of current product.
128  // If product is not registered into any projection-manager,
129  // this does nothing.
130  TEveProjectable* pable = dynamic_cast<TEveProjectable*>(elms);
131  if (pable->HasProjecteds()) {
132  // loop projected holders
133  for (TEveProjectable::ProjList_i i = pable->BeginProjecteds(); i != pable->EndProjecteds(); ++i) {
134  TEveProjectionManager* pmgr = (*i)->GetManager();
135  Float_t oldDepth = pmgr->GetCurrentDepth();
136  pmgr->SetCurrentDepth(item()->layer());
137  size_t cnt = 0;
138 
139  TEveElement* projectedAsElement = (*i)->GetProjectedAsElement();
140  TEveElement::List_i parentIt = projectedAsElement->BeginChildren();
141  for (TEveElement::List_i prodIt = elms->BeginChildren(); prodIt != elms->EndChildren(); ++prodIt, ++cnt) {
142  // reused projected holder
143  if (cnt < oldSize) {
144  if ((*parentIt)->NumChildren()) {
145  // update projected (mislleading name)
146  for (TEveElement::List_i pci = (*parentIt)->BeginChildren(); pci != (*parentIt)->EndChildren(); pci++)
147  pmgr->ProjectChildrenRecurse(*parentIt);
148  } else {
149  // import projectable
150  pmgr->SubImportChildren(*prodIt, *parentIt);
151  }
152 
153  ++parentIt;
154  } else if (cnt < itemSize) {
155  // new product holder
156  pmgr->SubImportElements(*prodIt, projectedAsElement);
157  } else {
158  break;
159  }
160  }
161  pmgr->SetCurrentDepth(oldDepth);
162  }
163  }
164 
165  if (m_interactionList && itemSize > oldSize) {
166  TEveElement::List_i elIt = elms->BeginChildren();
167  for (size_t cnt = 0; cnt < itemSize; ++cnt, ++elIt) {
168  if (cnt >= oldSize)
169  m_interactionList->added(*elIt, cnt);
170  }
171  }
172  }
173  } catch (const std::runtime_error& iException) {
174  fwLog(fwlog::kError) << "Caught exception in build function for item " << m_item->name() << ":\n"
175  << iException.what() << std::endl;
176  exit(1);
177  }
178  }
179  m_mustBuild = false;
180 }
181 
182 //______________________________________________________________________________
184  TEveElementList* elms = p->m_elements;
185  assert(m_item && static_cast<int>(m_item->size()) <= elms->NumChildren() &&
186  "can not use default modelChanges implementation");
187 
188  TEveElement::List_i itElement = elms->BeginChildren();
189  int index = 0;
190  for (FWModelIds::const_iterator it = iIds.begin(), itEnd = iIds.end(); it != itEnd; ++it, ++itElement, ++index) {
191  assert(itElement != elms->EndChildren());
192  while (index < it->index()) {
193  ++itElement;
194  ++index;
195  assert(itElement != elms->EndChildren());
196  }
197  if (visibilityModelChanges(*it, *itElement, p->m_viewType, p->m_viewContext)) {
198  elms->ProjectChild(*itElement);
199  } else {
200  localModelChanges(*it, *itElement, p->m_viewType, p->m_viewContext);
201  }
202  }
203 }
204 
206  if (m_haveWindow) {
207  for (Product_it i = m_products.begin(); i != m_products.end(); ++i) {
208  modelChanges(iIds, *i);
209  }
210  m_modelsChanged = false;
211  } else {
212  m_modelsChanged = true;
213  }
214 }
215 
216 //______________________________________________________________________________
218  if (iItem->layer() != m_layer)
219  setProjectionLayer(iItem->layer());
220 
221  if (m_haveWindow) {
222  build();
223  } else {
224  m_mustBuild = true;
225  }
226  m_modelsChanged = false;
227 }
228 
229 //______________________________________________________________________________
231  if (m_item)
232  return (item.purpose() == m_item->purpose());
233 
234  return false;
235 }
236 
237 //______________________________________________________________________________
238 
239 TEveElementList* FWProxyBuilderBase::createProduct(const FWViewType::EType viewType, const FWViewContext* viewContext) {
240  if (havePerViewProduct(viewType) == false && m_products.empty() == false) {
241  if (haveSingleProduct()) {
242  return m_products.back()->m_elements;
243  } else {
244  for (Product_it i = m_products.begin(); i != m_products.end(); ++i) {
245  if (viewType == (*i)->m_viewType)
246  return (*i)->m_elements;
247  }
248  }
249  }
250 
251  // printf("new product %s for item %s \n", FWViewType::idToName(viewType).c_str(), item()->name().c_str()); fflush(stdout);
252 
253  Product* product = new Product(viewType, viewContext);
254  m_products.push_back(product);
255  if (viewContext) {
256  product->m_scaleConnection =
257  viewContext->scaleChanged_.connect(std::bind(&FWProxyBuilderBase::scaleChanged, this, std::placeholders::_1));
258  }
259 
260  if (item()) {
261  // debug info in eve browser
262  product->m_elements->SetElementName(item()->name().c_str());
263  }
264  return product->m_elements;
265 }
266 
267 //______________________________________________________________________________
268 
270  for (Product_it i = m_products.begin(); i != m_products.end(); ++i) {
271  if (havePerViewProduct(type) && (*i)->m_viewContext == vc) {
272  if ((*i)->m_elements)
273  (*i)->m_elements->DestroyElements();
274 
275  if ((*i)->m_viewContext)
276  (*i)->m_scaleConnection.disconnect();
277 
278  delete (*i);
279  m_products.erase(i);
280  break;
281  }
282  }
283 }
284 
285 //------------------------------------------------------------------------------
286 
288  // Called if willHandleInteraction() returns false. Purpose ignored by default.
289 
291 }
292 
294  TEveElement*,
296  const FWViewContext*) {
297  return false;
298 }
299 
301  // Nothing to be done in base class.
302  // Visibility, main color and main transparency are handled through FWInteractionList.
303 }
304 
306  for (Product_it i = m_products.begin(); i != m_products.end(); ++i) {
307  if (havePerViewProduct((*i)->m_viewType) && (*i)->m_viewContext == vc) {
308  scaleProduct((*i)->m_elements, (*i)->m_viewType, (*i)->m_viewContext);
309  }
310  }
311  gEve->Redraw3D();
312 }
313 
315  // Cleans local common element list.
316  for (Product_it i = m_products.begin(); i != m_products.end(); ++i) {
317  if ((*i)->m_elements)
318  (*i)->m_elements->DestroyElements();
319  }
320 
321  cleanLocal();
322 }
323 
325  // Cleans local common element list.
326 }
327 
328 void FWProxyBuilderBase::build(const FWEventItem*, TEveElementList*, const FWViewContext*) {
329  assert(
330  "virtual build(const FWEventItem*, TEveElementList*, const FWViewContext*) not implemented by inherited class");
331 }
332 
334  assert(
335  "virtual buildViewType(const FWEventItem*, TEveElementList*, FWViewType::EType, const FWViewContext*) not "
336  "implemented by inherited class");
337 }
338 
340  m_layer = layer;
341  for (Product_it pIt = m_products.begin(); pIt != m_products.end(); ++pIt) {
342  TEveProjectable* pable = static_cast<TEveProjectable*>((*pIt)->m_elements);
343  for (TEveProjectable::ProjList_i i = pable->BeginProjecteds(); i != pable->EndProjecteds(); ++i)
344  (*i)->SetDepth(m_layer);
345  }
346 }
347 
348 //------------------------------------------------------------------------------
349 
350 void FWProxyBuilderBase::setupAddElement(TEveElement* el, TEveElement* parent, bool color) const {
351  setupElement(el, color);
352  parent->AddElement(el);
353 }
354 
358 void FWProxyBuilderBase::setupElement(TEveElement* el, bool color) const {
359  el->CSCTakeAnyParentAsMaster();
360  el->SetPickable(true);
361 
362  if (color) {
363  el->CSCApplyMainColorToMatchingChildren();
364  el->CSCApplyMainTransparencyToMatchingChildren();
365  el->SetMainColor(m_item->defaultDisplayProperties().color());
368  el->SetMainTransparency(m_item->defaultDisplayProperties().transparency());
369  }
370 }
371 
372 //------------------------------------------------------------------------------
373 
374 TEveCompound* FWProxyBuilderBase::createCompound(bool set_color, bool propagate_color_to_all_children) const {
375  TEveCompound* c = new TEveCompound();
376  c->CSCTakeAnyParentAsMaster();
377  c->CSCImplySelectAllChildren();
378  c->SetPickable(true);
379  if (set_color) {
380  c->SetMainColor(m_item->defaultDisplayProperties().color());
381  c->SetMainTransparency(m_item->defaultDisplayProperties().transparency());
382  }
383  if (propagate_color_to_all_children) {
384  c->CSCApplyMainColorToAllChildren();
385  c->CSCApplyMainTransparencyToAllChildren();
386  } else {
387  c->CSCApplyMainColorToMatchingChildren();
388  c->CSCApplyMainTransparencyToMatchingChildren();
389  }
390  return c;
391 }
392 
394  TEveElement* holder,
395  const std::string& name,
396  Char_t transpOffset) {
397  // Helper function to increse transparency of certain components.
398 
399  Char_t transparency = item()->modelInfo(index).displayProperties().transparency();
400  Char_t transp = TMath::Min(100, transpOffset + (100 - transpOffset) * transparency / 100);
401  TEveElement::List_t matches;
402  holder->FindChildren(matches, name.c_str());
403  for (TEveElement::List_i m = matches.begin(); m != matches.end(); ++m) {
404  (*m)->SetMainTransparency(transp);
405  }
406 }
407 
408 //
409 // const member functions
410 //
411 
413 
414 int FWProxyBuilderBase::layer() const { return m_item->layer(); }
415 
416 //
417 // static member functions
418 //
419 
421 
size_t size() const
Definition: FWEventItem.cc:457
virtual void buildViewType(const FWEventItem *iItem, TEveElementList *, FWViewType::EType, const FWViewContext *)
virtual void scaleProduct(TEveElementList *parent, FWViewType::EType, const FWViewContext *vc)
std::vector< Product * >::iterator Product_it
static std::string typeOfBuilder()
Used by the plugin system to determine how the proxy uses the data from FWEventItem.
std::vector< Product * > m_products
void setupAddElement(TEveElement *el, TEveElement *parent, bool set_color=true) const
virtual void itemBeingDestroyed(const FWEventItem *)
sigc::signal< void(const FWViewContext *)> scaleChanged_
Definition: FWViewContext.h:42
virtual bool visibilityModelChanges(const FWModelId &, TEveElement *, FWViewType::EType, const FWViewContext *)
Char_t transparency() const
static bool representsSubPart()
assert(be >=bs)
virtual void cleanLocal()
virtual bool haveSingleProduct() const
void scaleChanged(const FWViewContext *)
const FWDisplayProperties & displayProperties() const
Definition: FWEventItem.h:64
const FWEventItem * m_item
void added(TEveElement *, unsigned int)
const fireworks::Context & context() const
constexpr std::array< uint8_t, layerIndexSize > layer
sigc::connection m_scaleConnection
const fireworks::Context & context() const
Definition: FWEventItem.h:121
virtual void localModelChanges(const FWModelId &iId, TEveElement *iCompound, FWViewType::EType viewType, const FWViewContext *vc)
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
const FWDisplayProperties & defaultDisplayProperties() const
Definition: FWEventItem.cc:403
virtual void setItem(const FWEventItem *iItem)
void removePerViewProduct(FWViewType::EType, const FWViewContext *vc)
void setupElement(TEveElement *el, bool color=true) const
void setHaveWindow(bool iFlag)
int layer() const
Definition: FWEventItem.cc:405
#define fwLog(_level_)
Definition: fwLog.h:45
TEveCompound * createCompound(bool set_color=true, bool propagate_color_to_all_children=false) const
virtual bool canHandle(const FWEventItem &)
const std::string & name() const
Definition: FWEventItem.cc:435
FWInteractionList * m_interactionList
const std::string & purpose() const
Definition: FWEventItem.cc:439
virtual bool havePerViewProduct(FWViewType::EType) const
TEveElementList * createProduct(FWViewType::EType, const FWViewContext *)
ModelInfo modelInfo(int iIndex) const
Definition: FWEventItem.cc:446
const FWEventItem * item() const
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 &)
def exit(msg="")