Tulip  5.2.0
Large graphs analysis and drawing
GlLODCalculator.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef Tulip_GLLODCALCULATOR_H
22 #define Tulip_GLLODCALCULATOR_H
23 #ifndef DOXYGEN_NOTFOR_DEVEL
24 
25 #include <climits>
26 #include <vector>
27 
28 #include <tulip/BoundingBox.h>
29 #include <tulip/GlSceneVisitor.h>
30 #include <tulip/GlSimpleEntity.h>
31 #include <tulip/GlNode.h>
32 #include <tulip/GlEdge.h>
33 #include <tulip/GlLayer.h>
34 
35 namespace tlp {
36 
37 class Camera;
38 class GlEntity;
39 class GlSimpleEntity;
40 class GlScene;
41 class GlGraphInputData;
42 
43 enum RenderingEntitiesFlag {
44  RenderingSimpleEntities = 1,
45  RenderingNodes = 2,
46  RenderingEdges = 4,
47  RenderingAll = 7,
48  RenderingWithoutRemove = 8
49 };
50 
51 struct EntityLODUnit {
52  EntityLODUnit() : lod(-1) {}
53  EntityLODUnit(const BoundingBox &boundingBox) : boundingBox(boundingBox), lod(-1) {}
54  BoundingBox boundingBox;
55  float lod;
56 };
57 
58 // struct to store simple entity lod
59 struct SimpleEntityLODUnit : public EntityLODUnit {
60  SimpleEntityLODUnit(GlSimpleEntity *entity = nullptr) : EntityLODUnit(), entity(entity) {}
61  SimpleEntityLODUnit(GlSimpleEntity *entity, const BoundingBox &boundingBox)
62  : EntityLODUnit(boundingBox), entity(entity) {}
63  GlSimpleEntity *entity;
64 
65  void init(GlSimpleEntity *e, const BoundingBox &bb) {
66  entity = e;
67  boundingBox = bb;
68  }
69 };
70 
71 // struct to store complex entity (nodes/edges) lod
72 struct ComplexEntityLODUnit : public EntityLODUnit {
73  ComplexEntityLODUnit(unsigned int id = UINT_MAX, unsigned int pos = UINT_MAX)
74  : EntityLODUnit(), id(id), pos(pos) {}
75  ComplexEntityLODUnit(unsigned int id, unsigned int pos, const BoundingBox &boundingBox)
76  : EntityLODUnit(boundingBox), id(id), pos(pos) {}
77 
78  void init(unsigned int i, unsigned int p, const BoundingBox &bb) {
79  id = i;
80  pos = p;
81  boundingBox = bb;
82  }
83 
84  unsigned int id;
85  unsigned int pos;
86 };
87 
88 struct LayerLODUnit {
89  std::vector<SimpleEntityLODUnit> simpleEntitiesLODVector;
90  std::vector<ComplexEntityLODUnit> nodesLODVector;
91  std::vector<ComplexEntityLODUnit> edgesLODVector;
92  Camera *camera;
93 };
94 
95 typedef std::vector<LayerLODUnit> LayersLODVector;
96 
97 /**
98  * Class use to calculate lod of scene entities
99  */
100 class TLP_GL_SCOPE GlLODCalculator : public GlSceneVisitor {
101 
102 public:
103  GlLODCalculator() : glScene(nullptr), inputData(nullptr), attachedLODCalculator(nullptr) {}
104  virtual ~GlLODCalculator() {}
105  virtual GlLODCalculator *clone() = 0;
106 
107  /**
108  * Visit a GlSimpleEntity
109  */
110  void visit(GlSimpleEntity *entity) override {
111  addSimpleEntityBoundingBox(entity, entity->getBoundingBox());
112  }
113 
114  /**
115  * Visit a node
116  */
117  void visit(GlNode *glNode) override {
118  addNodeBoundingBox(glNode->id, glNode->pos, glNode->getBoundingBox(inputData));
119  }
120  /**
121  * Visit an Edge
122  */
123  void visit(GlEdge *glEdge) override {
124  addEdgeBoundingBox(glEdge->id, glEdge->pos, glEdge->getBoundingBox(inputData));
125  }
126  /**
127  * Visit a layer
128  */
129  void visit(GlLayer *layer) override {
130  beginNewCamera(&layer->getCamera());
131  }
132 
133  /**
134  * Reserve memory to store nodes and edges LOD
135  */
136  void reserveMemoryForGraphElts(unsigned int /*nbNodes*/, unsigned int /*nbEdges*/) override {}
137  /**
138  * Set scene use by this LOD calculator
139  */
140  virtual void setScene(GlScene &scene) {
141  glScene = &scene;
142  }
143 
144  /**
145  * Set input data use to render
146  */
147  virtual void setInputData(const GlGraphInputData *inputData) {
148  this->inputData = inputData;
149  }
150 
151  /**
152  * Set RenderingEntitiesFlag to :
153  * RenderingSimpleEntities,RenderingNodes,RenderingEdges,RenderingAll,RenderingWithoutRemove
154  */
155  virtual void setRenderingEntitiesFlag(RenderingEntitiesFlag flag) {
156  renderingEntitiesFlag = flag;
157  }
158 
159  /**
160  * Return if the LODCalculator need to have entities to compute
161  */
162  virtual bool needEntities() {
163  return true;
164  }
165  /**
166  * Set if the LODCalculator need to have entities to compute
167  */
168  virtual void setNeedEntities(bool) {}
169  /**
170  * Begin a new camera
171  */
172  virtual void beginNewCamera(Camera *camera) = 0;
173  /**
174  * Record a new simple entity in current camera context
175  */
176  virtual void addSimpleEntityBoundingBox(GlSimpleEntity *entity, const BoundingBox &bb) = 0;
177  /**
178  * Record a new node in current camera context
179  */
180  virtual void addNodeBoundingBox(unsigned int id, unsigned int pos, const BoundingBox &bb) = 0;
181  /**
182  * Record a new edge in current camera context
183  */
184  virtual void addEdgeBoundingBox(unsigned int id, unsigned int pos, const BoundingBox &bb) = 0;
185 
186  /**
187  * Compute all lod
188  */
189  virtual void compute(const Vector<int, 4> &globalViewport,
190  const Vector<int, 4> &currentViewport) = 0;
191 
192  /**
193  * Return a pointer on LOD result
194  */
195  LayersLODVector &getResult() {
196  return layersLODVector;
197  }
198 
199  /**
200  * Clear class data
201  */
202  virtual void clear() {
203  layersLODVector.clear();
204  }
205 
206  virtual BoundingBox getSceneBoundingBox() = 0;
207 
208  void setAttachedLODCalculator(GlLODCalculator *calculator) {
209  attachedLODCalculator = calculator;
210  }
211 
212 protected:
213  GlScene *glScene;
214  const GlGraphInputData *inputData;
215 
216  RenderingEntitiesFlag renderingEntitiesFlag;
217 
218  LayersLODVector layersLODVector;
219 
220  GlLODCalculator *attachedLODCalculator;
221 };
222 }
223 
224 #endif // DOXYGEN_NOTFOR_DEVEL
225 
226 #endif // Tulip_GLLODCALCULATOR_H
227 ///@endcond