-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathNUIE_NodeAlignment.cpp
More file actions
222 lines (200 loc) · 6.52 KB
/
NUIE_NodeAlignment.cpp
File metadata and controls
222 lines (200 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "NUIE_NodeAlignment.hpp"
#include <algorithm>
#include <limits>
namespace NUIE
{
static const double DefaultGapIfOverlap = 20.0;
enum class Direction
{
Horizontal,
Vertical
};
static Rect MinElement (const std::unordered_map<NE::NodeId, Rect>& rects, const std::function<bool (const Rect&, const Rect&)>& comp)
{
auto minElement = std::min_element (rects.begin (), rects.end (), [&] (const std::pair<NE::NodeId, Rect>& a, const std::pair<NE::NodeId, Rect>& b) {
return comp (a.second, b.second);
});
return minElement->second;
}
static Rect MaxElement (const std::unordered_map<NE::NodeId, Rect>& rects, const std::function<bool (const Rect&, const Rect&)>& comp)
{
auto minElement = std::max_element (rects.begin (), rects.end (), [&] (const std::pair<NE::NodeId, Rect>& a, const std::pair<NE::NodeId, Rect>& b) {
return comp (a.second, b.second);
});
return minElement->second;
}
static double GetAverage (const std::unordered_map<NE::NodeId, Rect>& rects, const std::function<double (const Rect&)>& proc)
{
double sum = 0.0;
for (const auto& rect : rects) {
sum += proc (rect.second);
}
return sum / (double) rects.size ();
}
static double GetRectBegin (const Rect& rect, Direction dir)
{
if (dir == Direction::Horizontal) {
return rect.GetLeft ();
} else if (dir == Direction::Vertical) {
return rect.GetTop ();
} else {
DBGBREAK ();
return 0.0;
}
}
static double GetRectEnd (const Rect& rect, Direction dir)
{
if (dir == Direction::Horizontal) {
return rect.GetRight ();
} else if (dir == Direction::Vertical) {
return rect.GetBottom ();
} else {
DBGBREAK ();
return 0.0;
}
}
static double GetRectSize (const Rect& rect, Direction dir)
{
if (dir == Direction::Horizontal) {
return rect.GetWidth ();
} else if (dir == Direction::Vertical) {
return rect.GetHeight ();
} else {
DBGBREAK ();
return 0.0;
}
}
static double GetComponent (const Point& point, Direction dir)
{
if (dir == Direction::Horizontal) {
return point.GetX ();
} else if (dir == Direction::Vertical) {
return point.GetY ();
} else {
DBGBREAK ();
return 0.0;
}
}
static Point CreatePoint (double component, Direction dir)
{
if (dir == Direction::Horizontal) {
return Point (component, 0.0);
} else if (dir == Direction::Vertical) {
return Point (0.0, component);
} else {
DBGBREAK ();
return Point (0.0, 0.0);
}
}
std::unordered_map<NE::NodeId, NUIE::Point> AlignNodes (Alignment alignment, const std::unordered_map<NE::NodeId, Rect>& rects)
{
std::unordered_map<NE::NodeId, NUIE::Point> offsets;
if (DBGERROR (rects.empty ())) {
return offsets;
}
if (alignment == Alignment::HorizontalLeft) {
Rect minRect = MinElement (rects, [&] (const Rect& a, const Rect& b) {
return a.GetLeft () < b.GetLeft ();
});
for (const auto& rect : rects) {
double offsetX = minRect.GetLeft () - rect.second.GetLeft ();
offsets.insert ({ rect.first, Point (offsetX, 0.0) });
}
} else if (alignment == Alignment::HorizontalRight) {
Rect maxRect = MaxElement (rects, [&] (const Rect& a, const Rect& b) {
return a.GetRight () < b.GetRight ();
});
for (const auto& rect : rects) {
double offsetX = maxRect.GetRight () - rect.second.GetRight ();
offsets.insert ({ rect.first, Point (offsetX, 0.0) });
}
} else if (alignment == Alignment::VerticalTop) {
Rect minRect = MinElement (rects, [&] (const Rect& a, const Rect& b) {
return a.GetTop () < b.GetTop ();
});
for (const auto& rect : rects) {
double offsetY = minRect.GetTop () - rect.second.GetTop ();
offsets.insert ({ rect.first, Point (0.0, offsetY) });
}
} else if (alignment == Alignment::VerticalBottom) {
Rect maxRect = MaxElement (rects, [&] (const Rect& a, const Rect& b) {
return a.GetBottom () < b.GetBottom ();
});
for (const auto& rect : rects) {
double offsetY = maxRect.GetBottom () - rect.second.GetBottom ();
offsets.insert ({ rect.first, Point (0.0, offsetY) });
}
} else if (alignment == Alignment::HorizontalCenter) {
double avgCenterX = GetAverage (rects, [&] (const Rect& rect) {
return rect.GetCenter ().GetX ();
});
for (const auto& rect : rects) {
double offsetX = avgCenterX - rect.second.GetCenter ().GetX ();
offsets.insert ({ rect.first, Point (offsetX, 0.0) });
}
} else if (alignment == Alignment::VerticalCenter) {
double avgCenterY = GetAverage (rects, [&] (const Rect& rect) {
return rect.GetCenter ().GetY ();
});
for (const auto& rect : rects) {
double offsetY = avgCenterY - rect.second.GetCenter ().GetY ();
offsets.insert ({ rect.first, Point (0.0, offsetY) });
}
} else {
DBGBREAK ();
}
DBGASSERT (rects.size () == offsets.size ());
return offsets;
}
std::unordered_map<NE::NodeId, NUIE::Point> DistributeNodes (Distribution distribution, const std::unordered_map<NE::NodeId, Rect>& rects)
{
std::unordered_map<NE::NodeId, NUIE::Point> offsets;
if (DBGERROR (rects.empty ())) {
return offsets;
}
Direction dir;
if (distribution == Distribution::Horizontal) {
dir = Direction::Horizontal;
} else if (distribution == Distribution::Vertical) {
dir = Direction::Vertical;
} else {
DBGBREAK ();
return offsets;
}
double min = std::numeric_limits<double>::max ();
double max = std::numeric_limits<double>::min ();
double rectsSumSize = 0.0;
std::vector<NE::NodeId> sortedNodes;
for (const auto& rect : rects) {
const Rect& nodeRect = rect.second;
min = std::min (min, GetRectBegin (nodeRect, dir));
max = std::max (max, GetRectEnd (nodeRect, dir));
rectsSumSize += GetRectSize (nodeRect, dir);
sortedNodes.push_back (rect.first);
}
double rangeSize = max - min;
double gapSize = DefaultGapIfOverlap;
if (IsGreater (rangeSize, rectsSumSize)) {
gapSize = (rangeSize - rectsSumSize) / ((double) rects.size () - 1);
}
std::sort (sortedNodes.begin (), sortedNodes.end (), [&] (const NE::NodeId& a, const NE::NodeId& b) {
return GetRectBegin (rects.at (a), dir) < GetRectBegin (rects.at (b), dir);
});
for (size_t i = 0; i < sortedNodes.size (); i++) {
const NE::NodeId& nodeId = sortedNodes[i];
if (i == 0) {
offsets.insert ({ nodeId, Point (0.0, 0.0) });
} else {
const Rect& nodeRect = rects.at (nodeId);
const NE::NodeId& prevNodeId = sortedNodes[i - 1];
const Rect& prevRect = rects.at (prevNodeId);
const Point& prevOffset = offsets.at (prevNodeId);
double startPosition = GetRectEnd (prevRect, dir) + GetComponent (prevOffset, dir) + gapSize;
double offset = startPosition - GetRectBegin (nodeRect, dir);
offsets.insert ({ nodeId, CreatePoint (offset, dir) });
}
}
DBGASSERT (rects.size () == offsets.size ());
return offsets;
}
}