Tulip  4.8.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
SortIterator.h
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
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_SORTITERATOR_H
22 #define TULIP_SORTITERATOR_H
23 #include <vector>
24 #include <algorithm>
25 #include <tulip/Iterator.h>
26 #include <tulip/StableIterator.h>
27 #include <tulip/NumericProperty.h>
28 #include <tulip/Graph.h>
29 #include <tulip/Vector.h>
30 
31 namespace tlp {
32 class Graph;
33 
34 #ifndef DOXYGEN_NOTFOR_DEVEL
35 struct LessThan {
36  LessThan(tlp::NumericProperty* m): metric(m) {
37  }
38  tlp::NumericProperty* metric;
39  bool operator() (const node &n1, const node &n2) const {
40  return (metric->getNodeDoubleValue(n1) < metric->getNodeDoubleValue(n2));
41  }
42  bool operator() (const edge &e1, const edge &e2) const {
43  return (metric->getEdgeDoubleValue(e1) < metric->getEdgeDoubleValue(e2));
44  }
45 };
46 
47 struct LessThanEdgeTargetMetric {
48  LessThanEdgeTargetMetric(Graph *sg, tlp::NumericProperty* metric):
49  metric(metric), sg(sg) {
50  }
51  bool operator() (const edge &e1, const edge &e2) const {
52  return (metric->getNodeDoubleValue(sg->target(e1)) < metric->getNodeDoubleValue(sg->target(e2)));
53  }
54 private:
55  tlp::NumericProperty* metric;
56  Graph *sg;
57 };
58 
59 struct LessThanEdgeSourceMetric {
60  LessThanEdgeSourceMetric(Graph *sg, tlp::NumericProperty* metric):
61  metric(metric), sg(sg) {
62  }
63  bool operator() (const edge &e1, const edge &e2) const {
64  return (metric->getNodeDoubleValue(sg->source(e1)) < metric->getNodeDoubleValue(sg->source(e2)));
65  }
66 private:
67  tlp::NumericProperty* metric;
68  Graph *sg;
69 };
70 
71 struct LessThanEdgeExtremitiesMetric {
72  LessThanEdgeExtremitiesMetric(Graph *sg, tlp::NumericProperty* metric):
73  metric(metric), sg(sg) {
74  }
75  bool operator() (const edge &e1, const edge &e2) const {
76  Vec2d v1(metric->getNodeDoubleValue(sg->source(e1)), metric->getNodeDoubleValue(sg->target(e1)));
77  Vec2d v2(metric->getNodeDoubleValue(sg->source(e2)), metric->getNodeDoubleValue(sg->target(e2)));
78  return v1 < v2;
79  }
80 private:
81  tlp::NumericProperty* metric;
82  Graph *sg;
83 };
84 
85 #endif // DOXYGEN_NOTFOR_DEVEL
86 
87 
88 /**
89 * @brief This Iterator sorts the nodes in a sequence based on their values in a NumericProperty.
90 **/
91 struct SortNodeIterator : public StableIterator<tlp::node> {
92  ///
93  SortNodeIterator(Iterator<tlp::node> *itIn, tlp::NumericProperty* metric, bool ascendingOrder = true) : StableIterator<tlp::node>(itIn) {
94  LessThan tmp(metric);
95  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
96 
97  if (!ascendingOrder) {
98  reverse(sequenceCopy.begin(),sequenceCopy.end());
99  }
100 
101  copyIterator=sequenceCopy.begin();
102  }
103  ///
104  ~SortNodeIterator() {}
105 };
106 
107 /**
108 * @brief This Iterator sorts the edges in a sequence based on their values in a NumericProperty.
109 **/
110 struct SortEdgeIterator : public StableIterator<tlp::edge> {
111  ///
112  SortEdgeIterator(Iterator<tlp::edge> *itIn, tlp::NumericProperty* metric, bool ascendingOrder = true) : StableIterator<tlp::edge>(itIn) {
113  LessThan tmp(metric);
114  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
115 
116  if (!ascendingOrder) {
117  reverse(sequenceCopy.begin(),sequenceCopy.end());
118  }
119 
120  copyIterator=sequenceCopy.begin();
121  }
122  ///
123  ~SortEdgeIterator() {}
124 };
125 
126 /**
127 * @brief This Iterator sorts the edges based on the values of their target nodes in a NumericProperty.
128 **/
129 struct SortTargetEdgeIterator : public StableIterator<tlp::edge> {
130  ///
131  SortTargetEdgeIterator(Iterator<tlp::edge> *itIn, Graph* sg, tlp::NumericProperty* metric, bool ascendingOrder = true) : StableIterator<tlp::edge>(itIn) {
132  LessThanEdgeTargetMetric tmp(sg,metric);
133  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
134 
135  if (!ascendingOrder) {
136  reverse(sequenceCopy.begin(),sequenceCopy.end());
137  }
138 
139  copyIterator=sequenceCopy.begin();
140  }
141  ///
142  ~SortTargetEdgeIterator() {}
143 };
144 
145 /**
146 * @brief This Iterator sorts the edges based on the values of their source nodes in a NumericProperty.
147 **/
148 struct SortSourceEdgeIterator : public StableIterator<tlp::edge> {
149  ///
150  SortSourceEdgeIterator(Iterator<tlp::edge> *itIn, Graph* sg, tlp::NumericProperty* metric, bool ascendingOrder = true) : StableIterator<tlp::edge>(itIn) {
151  LessThanEdgeSourceMetric tmp(sg,metric);
152  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
153 
154  if (!ascendingOrder) {
155  reverse(sequenceCopy.begin(),sequenceCopy.end());
156  }
157 
158  copyIterator=sequenceCopy.begin();
159  }
160  ///
161  ~SortSourceEdgeIterator() {}
162 };
163 
164 /**
165 * @brief This Iterator sorts the edges based on the values of their extremities nodes in a NumericProperty.
166 **/
167 struct SortExtremitiesEdgeIterator : public StableIterator<tlp::edge> {
168  ///
169  SortExtremitiesEdgeIterator(Iterator<tlp::edge> *itIn, Graph* sg, tlp::NumericProperty* metric, bool ascendingOrder = true) : StableIterator<tlp::edge>(itIn) {
170  LessThanEdgeExtremitiesMetric tmp(sg,metric);
171  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
172 
173  if (!ascendingOrder) {
174  reverse(sequenceCopy.begin(),sequenceCopy.end());
175  }
176 
177  copyIterator=sequenceCopy.begin();
178  }
179  ///
180  ~SortExtremitiesEdgeIterator() {}
181 };
182 
183 }
184 #endif
185 ///@endcond
Interface all numerical properties. Property values are always returned as double.