Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
minmaxproperty.cxx
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
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 #include <tulip/Graph.h>
20 #include <tulip/Coord.h>
21 
22 template<typename nodeType, typename edgeType, typename propType>
23 tlp::MinMaxProperty<nodeType, edgeType, propType>::MinMaxProperty(tlp::Graph* graph, std::string name, typename nodeType::RealType NodeMin,
24  typename nodeType::RealType NodeMax, typename edgeType::RealType EdgeMin, typename edgeType::RealType EdgeMax)
25  : AbstractProperty<nodeType, edgeType, propType>(graph, name), nodeValueUptodate(false), edgeValueUptodate(false), _nodeMin(NodeMin), _nodeMax(NodeMax), _edgeMin(EdgeMin), _edgeMax(EdgeMax) {
26 }
27 
28 template<typename nodeType, typename edgeType, typename propType>
30  if(!graph) {
31  graph = this->propType::graph;
32  }
33 
34  unsigned int graphID = graph->getId();
35  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = nodeValueUptodate.find(graphID);
36 
37  if ((it == nodeValueUptodate.end()) || ((*it).second == false))
38  computeMinMaxNode(graph);
39 
40  return minNode[graphID];
41 }
42 
43 template<typename nodeType, typename edgeType, typename propType>
45  if(!graph) {
46  graph = this->propType::graph;
47  }
48 
49  unsigned int graphID = graph->getId();
50  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = nodeValueUptodate.find(graphID);
51 
52  if ((it == nodeValueUptodate.end()) || ((*it).second == false))
53  computeMinMaxNode(graph);
54 
55  return maxNode[graphID];
56 }
57 
58 template<typename nodeType, typename edgeType, typename propType>
60  if(!graph) {
61  graph = this->propType::graph;
62  }
63 
64  unsigned int graphID = graph->getId();
65  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = edgeValueUptodate.find(graphID);
66 
67  if ((it == edgeValueUptodate.end()) || ((*it).second == false))
68  computeMinMaxEdge(graph);
69 
70  return minEdge[graphID];
71 }
72 
73 template<typename nodeType, typename edgeType, typename propType>
75  if(!graph) {
76  graph = this->propType::graph;
77  }
78 
79  unsigned int graphID = graph->getId();
80  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = edgeValueUptodate.find(graphID);
81 
82  if ((it == edgeValueUptodate.end()) || ((*it).second == false))
83  computeMinMaxEdge(graph);
84 
85  return maxEdge[graphID];
86 }
87 
88 template<typename nodeType, typename edgeType, typename propType>
90  if(!graph) {
91  graph = this->propType::graph;
92  }
93 
94  typename nodeType::RealType maxN2 = _nodeMin, minN2 = _nodeMax;
95 
96  Iterator<node>* nodeIterator = graph->getNodes();
97 
98  while (nodeIterator->hasNext()) {
99  node n=nodeIterator->next();
100  typename nodeType::RealType tmp = this->getNodeValue(n);
101 
102  if (tmp > maxN2) {
103  maxN2 = tmp;
104  }
105 
106  if (tmp < minN2) {
107  minN2 = tmp;
108  }
109  }
110 
111  delete nodeIterator;
112 
113  unsigned int sgi = graph->getId();
114 
115  // be careful to empty graph
116  if (maxN2 < minN2)
117  minN2 = maxN2;
118 
119  nodeValueUptodate[sgi]=true;
120  minNode[sgi] = minN2;
121  maxNode[sgi] = maxN2;
122 }
123 
124 template<typename nodeType, typename edgeType, typename propType>
126  typename edgeType::RealType maxE2 = _edgeMin, minE2 = _edgeMax;
127 
128  Iterator<edge>* edgeIterator = graph->getEdges();
129 
130  while (edgeIterator->hasNext()) {
131  edge ite=edgeIterator->next();
132  typename edgeType::RealType tmp = this->getEdgeValue(ite);
133 
134  if (tmp>maxE2)
135  maxE2 = tmp;
136 
137  if (tmp<minE2)
138  minE2 = tmp;
139  }
140 
141  delete edgeIterator;
142 
143  unsigned int sgi = graph->getId();
144 
145  // be careful to no edges graph
146  if (maxE2 < minE2)
147  minE2 = maxE2;
148 
149  edgeValueUptodate[sgi]=true;
150  minEdge[sgi]=minE2;
151  maxEdge[sgi]=maxE2;
152 }
153 
154 template<typename nodeType, typename edgeType, typename propType>
156  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = nodeValueUptodate.begin();
157 
158  if (it != nodeValueUptodate.end()) {
159  typename nodeType::RealType oldV = this->getNodeValue(n);
160 
161  if (newValue != oldV) {
162  // loop on subgraph min/max
163  for(; it != nodeValueUptodate.end(); ++it) {
164  // if min/max is ok for the current subgraph
165  // check if min or max has to be updated
166  if ((*it).second == true) {
167  unsigned int gid = (*it).first;
168  typename nodeType::RealType minV = minNode[gid];
169  typename nodeType::RealType maxV = maxNode[gid];
170 
171  // check if min or max has to be updated
172  if ((newValue < minV) || (newValue > maxV) || (oldV == minV) || (oldV == maxV)) {
173  nodeValueUptodate.clear();
174  break;
175  }
176  }
177  }
178  }
179  }
180 }
181 
182 template<typename nodeType, typename edgeType, typename propType>
184  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = edgeValueUptodate.begin();
185 
186  if (it != edgeValueUptodate.end()) {
187  typename edgeType::RealType oldV = this->getEdgeValue(e);
188 
189  if (newValue != oldV) {
190  // loop on subgraph min/max
191  for(; it != edgeValueUptodate.end(); ++it) {
192  // if min/max is ok for the current subgraph
193  // check if min or max has to be updated
194  if ((*it).second == true) {
195  unsigned int gid = (*it).first;
196  typename edgeType::RealType minV = minEdge[gid];
197  typename edgeType::RealType maxV = maxEdge[gid];
198 
199  // check if min or max has to be updated
200  if ((newValue < minV) || (newValue > maxV) || (oldV == minV) || (oldV == maxV)) {
201  edgeValueUptodate.clear();
202  break;
203  }
204  }
205  }
206  }
207  }
208 }
209 
210 template<typename nodeType, typename edgeType, typename propType>
212  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = nodeValueUptodate.begin();
213 
214  if (it != nodeValueUptodate.end()) {
215  // loop on subgraph min/max
216  for(; it != nodeValueUptodate.end(); ++it) {
217  unsigned int gid = (*it).first;
218  minNode[gid] = maxNode[gid] = newValue;
219  nodeValueUptodate[gid] = true;
220  }
221  }
222 }
223 
224 template<typename nodeType, typename edgeType, typename propType>
226  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = edgeValueUptodate.begin();
227 
228  if (it != edgeValueUptodate.end()) {
229  // loop on subgraph min/max
230  for(; it != edgeValueUptodate.end(); ++it) {
231  unsigned int gid = (*it).first;
232  minEdge[gid] = maxEdge[gid] = newValue;
233  edgeValueUptodate[gid] = true;
234  }
235  }
236 }
237 
238 template<typename nodeType, typename edgeType, typename propType>
240  const GraphEvent* graphEvent = dynamic_cast<const tlp::GraphEvent*>(&ev);
241 
242  if (graphEvent) {
243  tlp::Graph* graph = graphEvent->getGraph();
244 
245  switch(graphEvent->getType()) {
246  case GraphEvent::TLP_ADD_NODE:
247  nodeValueUptodate.clear();
248  break;
249 
250  case GraphEvent::TLP_DEL_NODE: {
251  unsigned int sgi = graph->getId();
252  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = nodeValueUptodate.find(sgi);
253 
254  if (it != nodeValueUptodate.end() && it->second) {
255  typename nodeType::RealType oldV = this->getNodeValue(graphEvent->getNode());
256 
257  // check if min or max has to be updated
258  if ((oldV == minNode[sgi]) || (oldV == maxNode[sgi]))
259  nodeValueUptodate[sgi] = false;
260  }
261 
262  break;
263  }
264 
265  case GraphEvent::TLP_ADD_EDGE:
266  edgeValueUptodate.clear();
267  break;
268 
269  case GraphEvent::TLP_DEL_EDGE: {
270  unsigned int sgi = graph->getId();
271  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = edgeValueUptodate.find(sgi);
272 
273  if (it != edgeValueUptodate.end() && it->second) {
274  typename edgeType::RealType oldV = this->getEdgeValue(graphEvent->getEdge());
275 
276  // check if min or max has to be updated
277  if ((oldV == minEdge[sgi]) || (oldV == maxEdge[sgi])) {
278  edgeValueUptodate[sgi] = false;
279  }
280  }
281 
282  break;
283  }
284 
285  case GraphEvent::TLP_AFTER_ADD_SUBGRAPH:
286  graphEvent->getSubGraph()->addListener(this);
287  break;
288 
289  case GraphEvent::TLP_BEFORE_DEL_SUBGRAPH:
290  graphEvent->getSubGraph()->removeListener(this);
291  break;
292 
293  default:
294  // we don't care about the rest
295  break;
296  }
297  }
298 }