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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
#include "Line.h"
Line::Line() : Branch() {}
Line::~Line() {}
bool Line::Contains(wxPoint2DDouble position) const
{
if(PointToLineDistance(position) < 5.0) {
return true;
}
return false;
}
void Line::Draw(wxPoint2DDouble translation, double scale) const
{
std::vector<wxPoint2DDouble> pointList = m_pointList;
if(!m_inserted && pointList.size() > 0) {
wxPoint2DDouble secondPoint = m_position;
if(pointList.size() > 2) {
secondPoint = pointList[2];
}
pointList[1] = GetSwitchPoint(m_parentList[0], pointList[0], secondPoint);
pointList.push_back(m_position);
}
// Line selected (Layer 1).
if(m_selected) {
glLineWidth(1.5 + m_borderSize * 2.0);
glColor4d(0.0, 0.5, 1.0, 0.5);
DrawLine(pointList);
// Draw nodes selection.
if(pointList.size() > 0) {
DrawCircle(pointList[0], 5.0 + m_borderSize / scale, 10, GL_POLYGON);
if(m_inserted) {
DrawCircle(pointList[pointList.size() - 1], 5.0 + m_borderSize / scale, 10, GL_POLYGON);
}
}
}
// Draw line (Layer 2)
glLineWidth(1.5);
glColor4d(0.2, 0.2, 0.2, 1.0);
DrawLine(pointList);
DrawSwitches();
// Draw nodes.
if(pointList.size() > 0) {
glColor4d(0.2, 0.2, 0.2, 1.0);
DrawCircle(pointList[0], 5.0, 10, GL_POLYGON);
if(m_inserted) {
DrawCircle(pointList[pointList.size() - 1], 5.0, 10, GL_POLYGON);
}
}
// Draw pickboxes (Layer 3).
if(m_showPickbox) {
glPushMatrix();
glLoadIdentity();
for(int i = 2; i < (int)m_pointList.size() - 2; i++) {
DrawPickbox(WorldToScreen(m_pointList[i], translation, scale));
}
glPopMatrix();
}
}
void Line::Move(wxPoint2DDouble position)
{
if(!m_parentList[0]) {
m_pointList[0] = m_movePts[0] + position - m_moveStartPt;
UpdateSwitchesPosition();
}
if(!m_parentList[1]) {
m_pointList[m_pointList.size() - 1] = m_movePts[m_pointList.size() - 1] + position - m_moveStartPt;
UpdateSwitchesPosition();
}
if(!m_parentList[0] && !m_parentList[1]) {
for(int i = 2; i < (int)m_pointList.size() - 2; i++) {
m_pointList[i] = m_movePts[i] + position - m_moveStartPt;
}
}
}
bool Line::AddParent(Element* parent, wxPoint2DDouble position)
{
if(parent) {
// First bus.
if(m_parentList.size() == 0) {
m_position = position;
m_parentList.push_back(parent);
wxPoint2DDouble parentPt = parent->RotateAtPosition(
position, -parent->GetAngle()); // Rotate click to horizontal position.
parentPt.m_y = parent->GetPosition().m_y; // Centralize on bus.
parentPt = parent->RotateAtPosition(parentPt, parent->GetAngle()); // Rotate back.
m_pointList.push_back(parentPt); // First point
m_pointList.push_back(GetSwitchPoint(parent, parentPt, m_position));
wxRect2DDouble genRect(0,0,0,0);
m_switchRect.push_back(genRect);
UpdateSwitches();
return false;
}
// Second bus.
else if(parent != m_parentList[0])
{
m_parentList.push_back(parent);
wxPoint2DDouble parentPt = parent->RotateAtPosition(
position, -parent->GetAngle()); // Rotate click to horizontal position.
parentPt.m_y = parent->GetPosition().m_y; // Centralize on bus.
parentPt = parent->RotateAtPosition(parentPt, parent->GetAngle()); // Rotate back.
// Set first switch point.
wxPoint2DDouble secondPoint = parentPt;
if(m_pointList.size() > 2) {
secondPoint = m_pointList[2];
}
m_pointList[1] = GetSwitchPoint(m_parentList[0], m_pointList[0], secondPoint);
// Set the second switch point.
m_pointList.push_back(GetSwitchPoint(parent, parentPt, m_pointList[m_pointList.size() - 1]));
m_pointList.push_back(parentPt); // Last point.
wxRect2DDouble genRect(0,0,0,0);
m_switchRect.push_back(genRect);
UpdateSwitches();
m_inserted = true;
return true;
}
}
return false;
}
bool Line::Intersects(wxRect2DDouble rect) const
{
for(auto it = m_pointList.begin(); it != m_pointList.end(); ++it) {
if(rect.Contains(*it)) return true;
}
return false;
}
void Line::MovePickbox(wxPoint2DDouble position)
{
if(m_activePickboxID == ID_PB_NONE) return;
for(int i = 2; i < (int)m_pointList.size() - 2; i++) {
if(m_activePickboxID == i) {
m_pointList[i] = m_movePts[i] + position - m_moveStartPt;
UpdateSwitchesPosition();
}
}
}
bool Line::PickboxContains(wxPoint2DDouble position)
{
for(int i = 2; i < (int)m_pointList.size() - 2; i++) {
wxRect2DDouble rect(m_pointList[i].m_x - 5.0, m_pointList[i].m_y - 5.0, 10.0, 10.0);
if(rect.Contains(position)) {
m_activePickboxID = i;
return true;
}
}
return false;
}
void Line::AddPoint(wxPoint2DDouble point)
{
if(m_parentList.size() != 0) {
m_pointList.push_back(point);
}
}
void Line::StartMove(wxPoint2DDouble position)
{
m_moveStartPt = position;
m_movePts = m_pointList;
}
void Line::MoveNode(Element* parent, wxPoint2DDouble position)
{
if(parent) {
// First bus.
if(parent == m_parentList[0]) {
m_pointList[0] = m_movePts[0] + position - m_moveStartPt;
}
// Second bus.
else if(parent == m_parentList[1])
{
m_pointList[m_pointList.size() - 1] = m_movePts[m_pointList.size() - 1] + position - m_moveStartPt;
}
// If the line is selected, move all the points, except the switches and buses points.
if(m_selected) {
for(int i = 2; i < (int)m_pointList.size() - 2; i++) {
m_pointList[i] = m_movePts[i] + position - m_moveStartPt;
}
}
}
else
{
if(m_activeNodeID == 1) {
m_pointList[0] = m_movePts[0] + position - m_moveStartPt;
m_parentList[0] = NULL;
m_online = false;
}
else if(m_activeNodeID == 2)
{
m_pointList[m_pointList.size() - 1] = m_movePts[m_pointList.size() - 1] + position - m_moveStartPt;
m_parentList[1] = NULL;
m_online = false;
}
}
// Recalculate switches positions
UpdateSwitchesPosition();
}
double Line::PointToLineDistance(wxPoint2DDouble point, int* segmentNumber) const
{
//[Ref] http://geomalgorithms.com/a02-_lines.html
double distance = 100.0; // Big initial distance.
wxPoint2DDouble p0 = point;
for(int i = 1; i < (int)m_pointList.size() - 2; i++) {
double d = 0.0;
wxPoint2DDouble p1 = m_pointList[i];
wxPoint2DDouble p2 = m_pointList[i + 1];
wxPoint2DDouble v = p2 - p1;
wxPoint2DDouble w = p0 - p1;
double c1 = w.m_x * v.m_x + w.m_y * v.m_y;
double c2 = v.m_x * v.m_x + v.m_y * v.m_y;
if(c1 <= 0.0) {
d = std::sqrt(std::pow(p0.m_y - p1.m_y, 2) + std::pow(p0.m_x - p1.m_x, 2));
}
else if(c2 <= c1)
{
d = std::sqrt(std::pow(p0.m_y - p2.m_y, 2) + std::pow(p0.m_x - p2.m_x, 2));
}
else
{
d = std::abs((p2.m_y - p1.m_y) * p0.m_x - (p2.m_x - p1.m_x) * p0.m_y + p2.m_x * p1.m_y -
p2.m_y * p1.m_x) /
std::sqrt(std::pow(p2.m_y - p1.m_y, 2) + std::pow(p2.m_x - p1.m_x, 2));
}
if(d < distance) {
distance = d;
if(segmentNumber) *segmentNumber = i;
}
}
return distance;
}
bool Line::GetContextMenu(wxMenu& menu)
{
menu.Append(ID_EDIT_LINE, _("Edit line"));
if(m_activePickboxID == ID_PB_NONE) {
menu.Append(ID_LINE_ADD_NODE, _("Insert node"));
}
else
{
menu.Append(ID_LINE_REMOVE_NODE, _("Remove node"));
}
menu.Append(ID_DELETE, _("Delete"));
return true;
}
void Line::RemoveNode(wxPoint2DDouble point)
{
if(PickboxContains(point)) {
for(int i = 2; i < (int)m_pointList.size() - 2; i++) {
if(m_activePickboxID == i) {
m_pointList.erase(m_pointList.begin() + i);
break;
}
}
}
UpdateSwitchesPosition();
}
void Line::AddNode(wxPoint2DDouble point)
{
int segmentNumber = 0;
PointToLineDistance(point, &segmentNumber);
if(segmentNumber > 0 && segmentNumber < (int)m_pointList.size() - 2) {
m_pointList.insert(m_pointList.begin() + segmentNumber + 1, point);
}
UpdateSwitchesPosition();
}
void Line::CalculateBoundaries(wxPoint2DDouble& leftUp, wxPoint2DDouble& rightBottom) const
{
if(m_pointList.size() > 0) {
// Check points list boundaries.
leftUp = m_pointList[0];
rightBottom = m_pointList[0];
for(int i = 1; i < (int)m_pointList.size(); i++) {
if(m_pointList[i].m_x < leftUp.m_x) leftUp.m_x = m_pointList[i].m_x;
if(m_pointList[i].m_y < leftUp.m_y) leftUp.m_y = m_pointList[i].m_y;
if(m_pointList[i].m_x > rightBottom.m_x) rightBottom.m_x = m_pointList[i].m_x;
if(m_pointList[i].m_y > rightBottom.m_y) rightBottom.m_y = m_pointList[i].m_y;
}
}
}
|