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