Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
REveElement.hxx
Go to the documentation of this file.
1 // @(#)root/eve7:$Id$
2 // Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007, 2018
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT7_REveElement_hxx
13 #define ROOT7_REveElement_hxx
14 
15 #include <ROOT/REveTypes.hxx>
16 #include <ROOT/REveVector.hxx>
18 
19 #include <memory>
20 
21 class TGeoMatrix;
22 
23 /// use temporary solution for forwarding of nlohmann::json
24 /// after version of 3.1.0 it is included in official releases
25 /// see https://github.com/nlohmann/json/issues/314
26 
27 #include <cstdint> // int64_t, uint64_t
28 #include <map> // map
29 #include <memory> // allocator
30 #include <string> // string
31 #include <vector> // vector
32 
33 namespace nlohmann {
34 
35  template<typename T, typename SFINAE>
36  struct adl_serializer;
37 
38  template<template<typename U, typename V, typename... Args> class ObjectType,
39  template<typename U, typename... Args> class ArrayType,
40  class StringType,
41  class BooleanType,
42  class NumberIntegerType,
43  class NumberUnsignedType,
44  class NumberFloatType,
45  template<typename U> class AllocatorType,
46  template<typename T, typename SFINAE> class JSONSerializer>
47  class basic_json;
48 
49  using json = basic_json<std::map, std::vector, std::string,
50  bool, std::int64_t, std::uint64_t, double,
51  std::allocator, adl_serializer>;
52 }
53 
54 namespace ROOT {
55 namespace Experimental {
56 
57 class REveAunt;
58 class REveScene;
59 class REveCompound;
60 class REveTrans;
61 class REveRenderData;
62 
63 //==============================================================================
64 // REveElement
65 // Base class for ROOT Event Visualization Environment (EVE)
66 // providing hierarchy management and selection and rendering control.
67 //==============================================================================
68 
69 class REveElement
70 {
71  friend class REveManager;
72  friend class REveScene;
73 
74  REveElement& operator=(const REveElement&); // Not implemented
75 
76 public:
77  typedef std::list<REveElement*> List_t;
78 
79  typedef std::set<REveElement*> Set_t;
80 
81  typedef std::list<REveAunt*> AuntList_t;
82 
83 private:
84  ElementId_t fElementId{0}; // Unique ID of an element.
85 
86 protected:
87  REveElement *fMother {nullptr};
88  REveScene *fScene {nullptr};
89  REveElement *fSelectionMaster {nullptr};
90 
91  ElementId_t get_mother_id() const;
92  ElementId_t get_scene_id() const;
93 
94  void assign_element_id_recurisvely();
95  void assign_scene_recursively(REveScene* s);
96 
97 protected:
98  std::string fName; // Element name
99  std::string fTitle; // Element title / tooltip
100  AuntList_t fAunts; // List of aunts.
101  List_t fChildren; // List of children.
102  TClass *fChildClass {nullptr}; // Class of acceptable children, others are rejected.
103  REveCompound *fCompound {nullptr}; // Compound this object belongs to.
104  REveElement *fVizModel {nullptr}; //! Element used as model from VizDB.
105  TString fVizTag; // Tag used to query VizDB for model element.
106 
107  Int_t fDenyDestroy{0}; //! Deny-destroy count.
108  Bool_t fDestroyOnZeroRefCnt{kTRUE}; // Auto-destruct when ref-count reaches zero.
109 
110  Bool_t fRnrSelf{kTRUE}; // Render this element.
111  Bool_t fRnrChildren{kTRUE}; // Render children of this element.
112  Bool_t fCanEditMainColor{kFALSE}; // Allow editing of main color.
113  Bool_t fCanEditMainTransparency{kFALSE}; // Allow editing of main transparency.
114  Bool_t fCanEditMainTrans{kFALSE}; // Allow editing of main transformation.
115 
116  Char_t fMainTransparency{0}; // Main-transparency variable.
117  Color_t fDefaultColor{kPink}; // Default color for sub-classes that enable it.
118  Color_t *fMainColorPtr{nullptr};// Pointer to main-color variable.
119  std::unique_ptr<REveTrans> fMainTrans; // Pointer to main transformation matrix.
120 
121  void *fUserData{nullptr}; //! Externally assigned and controlled user data.
122 
123  std::unique_ptr<REveRenderData> fRenderData;//! Vertex / normal / triangle index information for rendering.
124 
125  virtual void PreDeleteElement();
126  virtual void RemoveElementsInternal();
127  virtual void AnnihilateRecursively();
128 
129  static const std::string& ToString(Bool_t b);
130 
131 public:
132  REveElement(const std::string &name = "", const std::string &title = "");
133  REveElement(const REveElement& e);
134  virtual ~REveElement();
135 
136  ElementId_t GetElementId() const { return fElementId; }
137 
138  virtual REveElement* CloneElement() const;
139  virtual REveElement* CloneElementRecurse(Int_t level = 0) const;
140  virtual void CloneChildrenRecurse(REveElement *dest, Int_t level = 0) const;
141 
142  std::string GetName() const { return fName; }
143  const char* GetCName() const { return fName.c_str(); }
144  std::string GetTitle() const { return fTitle; }
145  const char* GetCTitle() const { return fTitle.c_str(); }
146 
147  virtual std::string GetHighlightTooltip() const { return fTitle; }
148 
149  void SetName (const std::string &name);
150  void SetTitle(const std::string &title);
151  void SetNameTitle(const std::string &name, const std::string &title);
152  virtual void NameTitleChanged();
153 
154  const TString& GetVizTag() const { return fVizTag; }
155  void SetVizTag(const TString& tag) { fVizTag = tag; }
156 
157  REveElement *GetVizModel() const { return fVizModel; }
158  void SetVizModel(REveElement* model);
159  Bool_t SetVizModelByTag();
160 
161  Bool_t ApplyVizTag(const TString& tag, const TString& fallback_tag="");
162 
163  virtual void PropagateVizParamsToProjecteds();
164  virtual void PropagateVizParamsToChildren(REveElement* el = nullptr);
165  virtual void CopyVizParams(const REveElement* el);
166  virtual void CopyVizParamsFromDB();
167  void SaveVizParams (std::ostream &out, const TString &tag, const TString &var);
168  virtual void WriteVizParams(std::ostream &out, const TString &var);
169 
170  REveCompound* GetCompound() { return fCompound; }
171  void SetCompound(REveCompound* c) { fCompound = c; }
172 
173  bool HasScene() { return fScene != nullptr; }
174  bool HasMother() { return fMother != nullptr; }
175 
176  REveScene* GetScene() { return fScene; }
177  REveElement* GetMother() { return fMother; }
178 
179  virtual void AddAunt(REveAunt *au);
180  virtual void RemoveAunt(REveAunt *au);
181  virtual void CheckReferenceCount(const std::string &from = "<unknown>");
182 
183  AuntList_t &RefAunts() { return fAunts; }
184  const AuntList_t &RefAunts() const { return fAunts; }
185  Int_t NumAunts() const { return fAunts.size(); }
186  Bool_t HasAunts() const { return !fAunts.empty(); }
187 
188  TClass* GetChildClass() const { return fChildClass; }
189  void SetChildClass(TClass* c) { fChildClass = c; }
190 
191  List_t &RefChildren() { return fChildren; }
192  const List_t &RefChildren() const { return fChildren; }
193  Int_t NumChildren() const { return fChildren.size(); }
194  Bool_t HasChildren() const { return !fChildren.empty(); }
195 
196  Bool_t HasChild(REveElement *el);
197  REveElement *FindChild(const TString &name, const TClass *cls = nullptr);
198  REveElement *FindChild(TPRegexp &regexp, const TClass *cls = nullptr);
199  Int_t FindChildren(List_t &matches, const TString& name, const TClass *cls = nullptr);
200  Int_t FindChildren(List_t &matches, TPRegexp& regexp, const TClass* cls = nullptr);
201  REveElement *FirstChild() const;
202  REveElement *LastChild () const;
203 
204  void EnableListElements(Bool_t rnr_self = kTRUE, Bool_t rnr_children = kTRUE); // *MENU*
205  void DisableListElements(Bool_t rnr_self = kFALSE, Bool_t rnr_children = kFALSE); // *MENU*
206 
207  Bool_t GetDestroyOnZeroRefCnt() const;
208  void SetDestroyOnZeroRefCnt(Bool_t d);
209 
210  Int_t GetDenyDestroy() const;
211  void IncDenyDestroy();
212  void DecDenyDestroy();
213 
214  // --------------------------------
215 
216  TClass *IsA() const;
217 
218  virtual void ExportToCINT(const char *var_name); // *MENU*
219 
220  virtual Bool_t AcceptElement(REveElement *el);
221 
222  virtual void AddElement(REveElement *el);
223  virtual void RemoveElement(REveElement *el);
224  virtual void RemoveElementLocal(REveElement *el);
225  virtual void RemoveElements();
226  virtual void RemoveElementsLocal();
227 
228  virtual void AnnihilateElements();
229  virtual void Annihilate();
230 
231  virtual void ProjectChild(REveElement *el, Bool_t same_depth = kTRUE);
232  virtual void ProjectAllChildren(Bool_t same_depth = kTRUE);
233 
234  virtual void Destroy(); // *MENU*
235  virtual void DestroyOrWarn();
236  virtual void DestroyElements(); // *MENU*
237 
238  virtual Bool_t CanEditElement() const { return kTRUE; }
239  virtual Bool_t SingleRnrState() const { return kFALSE; }
240  virtual Bool_t GetRnrSelf() const { return fRnrSelf; }
241  virtual Bool_t GetRnrChildren() const { return fRnrChildren; }
242  virtual Bool_t GetRnrState() const { return fRnrSelf && fRnrChildren; }
243  virtual Bool_t GetRnrAnything() const { return fRnrSelf || (fRnrChildren && HasChildren()); }
244  virtual Bool_t SetRnrSelf(Bool_t rnr);
245  virtual Bool_t SetRnrChildren(Bool_t rnr);
246  virtual Bool_t SetRnrSelfChildren(Bool_t rnr_self, Bool_t rnr_children);
247  virtual Bool_t SetRnrState(Bool_t rnr);
248  virtual void PropagateRnrStateToProjecteds();
249 
250  void SetupDefaultColorAndTransparency(Color_t col, Bool_t can_edit_color, Bool_t can_edit_transparency);
251 
252  virtual Bool_t CanEditMainColor() const { return fCanEditMainColor; }
253  void SetEditMainColor(Bool_t x) { fCanEditMainColor = x; }
254  Color_t *GetMainColorPtr() const { return fMainColorPtr; }
255  void SetMainColorPtr(Color_t *colptr) { fMainColorPtr = colptr; }
256 
257  virtual Bool_t HasMainColor() const { return fMainColorPtr != nullptr; }
258  virtual Color_t GetMainColor() const { return fMainColorPtr ? *fMainColorPtr : 0; }
259  virtual void SetMainColor(Color_t color);
260  void SetMainColorPixel(Pixel_t pixel);
261  void SetMainColorRGB(UChar_t r, UChar_t g, UChar_t b);
262  void SetMainColorRGB(Float_t r, Float_t g, Float_t b);
263  virtual void PropagateMainColorToProjecteds(Color_t color, Color_t old_color);
264 
265  virtual Bool_t CanEditMainTransparency() const { return fCanEditMainTransparency; }
266  void SetEditMainTransparency(Bool_t x) { fCanEditMainTransparency = x; }
267  virtual Char_t GetMainTransparency() const { return fMainTransparency; }
268  virtual void SetMainTransparency(Char_t t);
269  void SetMainAlpha(Float_t alpha);
270  virtual void PropagateMainTransparencyToProjecteds(Char_t t, Char_t old_t);
271 
272  virtual Bool_t CanEditMainTrans() const { return fCanEditMainTrans; }
273  virtual Bool_t HasMainTrans() const { return fMainTrans.get() != nullptr; }
274  virtual REveTrans* PtrMainTrans(Bool_t create=kTRUE);
275  virtual REveTrans& RefMainTrans();
276  virtual void InitMainTrans(Bool_t can_edit=kTRUE);
277  virtual void DestroyMainTrans();
278 
279  virtual void SetTransMatrix(Double_t *carr);
280  virtual void SetTransMatrix(const TGeoMatrix &mat);
281 
282  virtual Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset);
283  virtual void BuildRenderData();
284 
285  void* GetUserData() const { return fUserData; }
286  void SetUserData(void* ud) { fUserData = ud; }
287 
288  REveRenderData *GetRenderData() const { return fRenderData.get(); }
289 
290 
291  // Selection state and management
292  //--------------------------------
293 
294 protected:
295 
296  enum ECompoundSelectionColorBits
297  {
298  kCSCBImplySelectAllChildren = BIT(0), // compound will select all children
299  kCSCBTakeMotherAsMaster = BIT(1), // element will take its mother as master
300  kCSCBApplyMainColorToAllChildren = BIT(2), // compound will apply color change to all children
301  kCSCBApplyMainColorToMatchingChildren = BIT(3), // compound will apply color change to all children with matching color
302  kCSCBApplyMainTransparencyToAllChildren = BIT(4), // compound will apply transparency change to all children
303  kCSCBApplyMainTransparencyToMatchingChildren = BIT(5) // compound will apply transparency change to all children with matching color
304  };
305 
306  enum EDestruct
307  {
308  kNone,
309  kStandard,
310  kAnnihilate
311  };
312 
313  Short_t fImpliedSelected{0}; // How many times the element is implied selected -- needed during destruction.
314  Bool_t fPickable{0}; // Can element be selected.
315  UChar_t fCSCBits{0}; // Compound Selection Color flags.
316 
317 public:
318  Bool_t IsPickable() const { return fPickable; }
319  void SetPickable(Bool_t p) { fPickable = p; }
320  void SetPickableRecursively(Bool_t p);
321 
322  REveElement* GetSelectionMaster();
323  void SetSelectionMaster(REveElement *el) { fSelectionMaster = el; }
324 
325  virtual void FillImpliedSelectedSet(Set_t& impSelSet);
326 
327  void IncImpliedSelected() { ++fImpliedSelected; }
328  void DecImpliedSelected() { --fImpliedSelected; }
329  int GetImpliedSelected() { return fImpliedSelected; }
330 
331  void RecheckImpliedSelections();
332 
333  void SetCSCBits(UChar_t f) { fCSCBits |= f; }
334  void ResetCSCBits(UChar_t f) { fCSCBits &= ~f; }
335  Bool_t TestCSCBits(UChar_t f) const { return (fCSCBits & f) != 0; }
336 
337  void ResetAllCSCBits() { fCSCBits = 0; }
338  void CSCImplySelectAllChildren() { fCSCBits |= kCSCBImplySelectAllChildren; }
339  void CSCTakeMotherAsMaster() { fCSCBits |= kCSCBTakeMotherAsMaster; }
340  void CSCApplyMainColorToAllChildren() { fCSCBits |= kCSCBApplyMainColorToAllChildren; }
341  void CSCApplyMainColorToMatchingChildren() { fCSCBits |= kCSCBApplyMainColorToMatchingChildren; }
342  void CSCApplyMainTransparencyToAllChildren() { fCSCBits |= kCSCBApplyMainTransparencyToAllChildren; }
343  void CSCApplyMainTransparencyToMatchingChildren() { fCSCBits |= kCSCBApplyMainTransparencyToMatchingChildren; }
344 
345 
346  // Change-stamping and change bits
347  //---------------------------------
348 
349  enum EChangeBits
350  {
351  kCBColorSelection = BIT(0), // Main color or select/hilite state changed.
352  kCBTransBBox = BIT(1), // Transformation matrix or bounding-box changed.
353  kCBObjProps = BIT(2), // Object changed, requires dropping its display-lists.
354  kCBVisibility = BIT(3), // Rendering of self/children changed.
355  kCBElementAdded = BIT(4) // Element was added to a new parent.
356  // kCBElementRemoved = BIT() // Element was removed from a parent.
357 
358  // Deletions are handled in a special way in REveManager::PreDeleteElement().
359  };
360 
361 protected:
362  UChar_t fChangeBits{0}; //!
363  Char_t fDestructing{kNone}; //!
364 
365 public:
366  void StampColorSelection() { AddStamp(kCBColorSelection); }
367  void StampTransBBox() { AddStamp(kCBTransBBox); }
368  void StampObjProps() { AddStamp(kCBObjProps); }
369  void StampObjPropsPreChk() { if ( ! (fChangeBits & kCBObjProps)) AddStamp(kCBObjProps); }
370  void StampVisibility() { AddStamp(kCBVisibility); }
371  void StampElementAdded() { AddStamp(kCBElementAdded); }
372  // void StampElementRemoved() { AddStamp(kCBElementRemoved); }
373  virtual void AddStamp(UChar_t bits);
374  virtual void ClearStamps() { fChangeBits = 0; }
375 
376  UChar_t GetChangeBits() const { return fChangeBits; }
377 
378  // Menu entries for VizDB communication (here so they are last in the menu).
379 
380  void VizDB_Apply(const std::string& tag); // *MENU*
381  void VizDB_Reapply(); // *MENU*
382  void VizDB_UpdateModel(Bool_t update=kTRUE); // *MENU*
383  void VizDB_Insert(const std::string& tag, Bool_t replace=kTRUE, Bool_t update=kTRUE); // *MENU*
384 };
385 
386 
387 //==============================================================================
388 // REveAunt
389 //==============================================================================
390 
391 class REveAunt
392 {
393 public:
394  virtual ~REveAunt() {}
395 
396  virtual bool HasNiece(REveElement *el) const = 0;
397  virtual bool HasNieces() const = 0;
398 
399  virtual bool AcceptNiece(REveElement *) { return true; }
400 
401  virtual void AddNiece(REveElement *el)
402  {
403  // XXXX Check AcceptNiece() -- throw if not !!!!
404  el->AddAunt(this);
405  AddNieceInternal(el);
406  }
407  virtual void AddNieceInternal(REveElement *el) = 0;
408 
409  virtual void RemoveNiece(REveElement *el)
410  {
411  RemoveNieceInternal(el);
412  el->RemoveAunt(this);
413  }
414  virtual void RemoveNieceInternal(REveElement *el) = 0;
415 
416  virtual void RemoveNieces() = 0;
417 };
418 
419 
420 //==============================================================================
421 // REveAuntAsList
422 //==============================================================================
423 
424 class REveAuntAsList : public REveAunt
425 {
426 protected:
427  REveElement::List_t fNieces;
428 
429 public:
430  virtual ~REveAuntAsList()
431  {
432  for (auto &n : fNieces) n->RemoveAunt(this);
433  }
434 
435  bool HasNiece(REveElement *el) const override
436  {
437  return std::find(fNieces.begin(), fNieces.end(), el) != fNieces.end();
438  }
439 
440  bool HasNieces() const override
441  {
442  return ! fNieces.empty();
443  }
444 
445  void AddNieceInternal(REveElement *el) override
446  {
447  fNieces.push_back(el);
448  }
449 
450  void RemoveNieceInternal(REveElement *el) override
451  {
452  fNieces.remove(el);
453  }
454 
455  void RemoveNieces() override
456  {
457  for (auto &n : fNieces) n->RemoveAunt(this);
458  fNieces.clear();
459  }
460 };
461 
462 } // namespace Experimental
463 } // namespace ROOT
464 
465 #endif