-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
624 lines (530 loc) · 20.4 KB
/
main.cpp
File metadata and controls
624 lines (530 loc) · 20.4 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <SFML/Graphics.hpp>
#include "map.pb.h"
#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
class MapVisualizer
{
private:
sf::RenderWindow window;
double scale;
double offset_x, offset_y;
double base_x, base_y;
sf::Font font;
// 鼠标拖拽相关变量
bool isDragging;
sf::Vector2i lastMousePos;
double drag_offset_x, drag_offset_y;
// 保存地图数据引用
const map_data::Map *current_map_data;
public:
MapVisualizer() : window(sf::VideoMode(1200, 800), "Map Visualization"),
scale(1.0), offset_x(0), offset_y(0),
base_x(586000), base_y(4140700),
isDragging(false), lastMousePos(0, 0),
drag_offset_x(0), drag_offset_y(0),
current_map_data(nullptr)
{
// 在构造函数中加载字体
loadFont();
}
// 字体加载函数
bool loadFont()
{
// 尝试多个可能的字体路径
std::vector<std::string> fontPaths = {
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
"/usr/share/fonts/truetype/freefont/FreeSans.ttf",
"C:/Windows/Fonts/arial.ttf", // Windows
"/System/Library/Fonts/Arial.ttf" // macOS
};
for (const auto &path : fontPaths)
{
if (font.loadFromFile(path))
{
std::cout << "成功加载字体: " << path << std::endl;
return true;
}
}
// 如果系统字体都加载失败,创建一个简单的默认字体
std::cout << "无法加载系统字体,使用内置默认字体" << std::endl;
return false;
}
// 修正坐标转换函数
sf::Vector2f transformPoint(double x, double y)
{
// 使用相对坐标,避免大数计算
double relative_x = (x - base_x) * scale;
double relative_y = (base_y - y) * scale; // Y轴翻转
return sf::Vector2f(relative_x + offset_x, relative_y + offset_y);
}
// 绘制实线线段
void drawLineSegment(const map_data::LineSegment &line_segment, sf::Color color)
{
if (line_segment.point_size() < 2)
return;
sf::VertexArray lines(sf::LineStrip, line_segment.point_size());
for (int i = 0; i < line_segment.point_size(); ++i)
{
const auto &point = line_segment.point(i);
sf::Vector2f screen_pos = transformPoint(point.x(), point.y());
lines[i].position = screen_pos;
lines[i].color = color;
}
window.draw(lines);
}
// 绘制点
void drawPoints(const map_data::LineSegment &line_segment, sf::Color color, float radius = 1.5f)
{
for (int i = 0; i < line_segment.point_size(); ++i)
{
const auto &point = line_segment.point(i);
sf::Vector2f screen_pos = transformPoint(point.x(), point.y());
// 放宽范围检查
if (screen_pos.x >= -50 && screen_pos.x <= 1250 && screen_pos.y >= -50 && screen_pos.y <= 850)
{
sf::CircleShape circle(radius);
circle.setFillColor(color);
circle.setPosition(screen_pos.x - radius, screen_pos.y - radius);
window.draw(circle);
}
}
}
// 绘制虚线线段 - 确保每个点之间都有线
void drawDashedLineSegment(const map_data::LineSegment &line_segment, sf::Color color)
{
if (line_segment.point_size() < 2)
return;
// 统计线段信息
int total_segments = line_segment.point_size() - 1;
std::cout << "线段包含 " << line_segment.point_size() << " 个点, " << total_segments << " 条线段" << std::endl;
// 为每对相邻点绘制虚线
for (int i = 0; i < line_segment.point_size() - 1; ++i)
{
const auto &point1 = line_segment.point(i);
const auto &point2 = line_segment.point(i + 1);
sf::Vector2f start = transformPoint(point1.x(), point1.y());
sf::Vector2f end = transformPoint(point2.x(), point2.y());
// 输出线段信息用于调试
std::cout << "线段 " << i << ": (" << point1.x() << ", " << point1.y()
<< ") -> (" << point2.x() << ", " << point2.y() << ")" << std::endl;
std::cout << "屏幕坐标: (" << start.x << ", " << start.y
<< ") -> (" << end.x << ", " << end.y << ")" << std::endl;
// 绘制这条线段上的虚线
drawDashedLineBetweenPoints(start, end, color);
}
}
// 在两个点之间绘制虚线
void drawDashedLineBetweenPoints(const sf::Vector2f &start, const sf::Vector2f &end, sf::Color color)
{
sf::Vector2f direction = end - start;
float line_length = std::sqrt(direction.x * direction.x + direction.y * direction.y);
std::cout << "线段长度: " << line_length << std::endl;
// 如果线段太短,直接绘制实线
if (line_length < 5.0f)
{
sf::VertexArray solid_line(sf::Lines, 2);
solid_line[0].position = start;
solid_line[0].color = color;
solid_line[1].position = end;
solid_line[1].color = color;
window.draw(solid_line);
std::cout << "绘制实线(线段太短)" << std::endl;
return;
}
// 计算虚线参数(根据线段长度自适应)
float dash_length = std::max(5.0f, line_length / 10.0f);
float gap_length = dash_length * 0.6f;
sf::Vector2f unit_direction = direction / line_length;
float drawn_length = 0.0f;
bool draw_dash = true;
int dash_count = 0;
while (drawn_length < line_length)
{
if (draw_dash)
{
// 绘制虚线段
float current_dash_length = std::min(dash_length, line_length - drawn_length);
sf::Vector2f dash_start = start + unit_direction * drawn_length;
sf::Vector2f dash_end = start + unit_direction * (drawn_length + current_dash_length);
if (current_dash_length > 0.1f)
{
sf::VertexArray dash(sf::Lines, 2);
dash[0].position = dash_start;
dash[0].color = color;
dash[1].position = dash_end;
dash[1].color = color;
window.draw(dash);
dash_count++;
}
drawn_length += current_dash_length;
}
else
{
// 跳过间隙
drawn_length += gap_length;
}
draw_dash = !draw_dash;
}
std::cout << "绘制了 " << dash_count << " 段虚线" << std::endl;
}
void drawCurve(const map_data::Curve &curve, sf::Color color)
{
for (int i = 0; i < curve.segment_size(); ++i)
{
const auto &segment = curve.segment(i);
if (segment.has_line_segment())
{
drawLineSegment(segment.line_segment(), color);
}
}
}
// 绘制虚线曲线
void drawDashedCurve(const map_data::Curve &curve, sf::Color color)
{
for (int i = 0; i < curve.segment_size(); ++i)
{
const auto &segment = curve.segment(i);
if (segment.has_line_segment())
{
drawDashedLineSegment(segment.line_segment(), color);
}
}
}
// 绘制曲线上的点
void drawCurvePoints(const map_data::Curve &curve, sf::Color color)
{
int total_points = 0;
for (int i = 0; i < curve.segment_size(); ++i)
{
const auto &segment = curve.segment(i);
if (segment.has_line_segment())
{
total_points += segment.line_segment().point_size();
drawPoints(segment.line_segment(), color, 1.0f);
}
}
std::cout << "中心线总点数: " << total_points << std::endl;
}
// 在drawLane函数中,暂时使用实线来测试
void drawLane(const map_data::Lane &lane)
{
sf::Color gray(128, 128, 128);
// 绘制中心线 - 使用实线确保所有线段都可见
if (lane.has_central_curve())
{
std::cout << "=== 开始绘制中心线 ===" << std::endl;
// 先绘制点
drawCurvePoints(lane.central_curve(), sf::Color::White);
// 使用实线而不是虚线
drawCurve(lane.central_curve(), gray);
std::cout << "=== 中心线绘制完成 ===" << std::endl;
}
// 绘制左边界 - 黄色实线
if (lane.has_left_boundary() && lane.left_boundary().has_curve())
{
sf::Color gray(128, 128, 128);
drawCurve(lane.left_boundary().curve(), gray);
}
// 绘制右边界 - 蓝色实线
if (lane.has_right_boundary() && lane.right_boundary().has_curve())
{
drawCurve(lane.right_boundary().curve(), gray);
}
// 绘制车道宽度采样点
drawWidthSamples(lane);
}
void drawWidthSamples(const map_data::Lane &lane)
{
// 绘制左侧宽度采样点
for (int i = 0; i < lane.left_sample_size(); ++i)
{
const auto &sample = lane.left_sample(i);
// 这里可以根据采样点信息绘制宽度指示线
}
// 绘制右侧宽度采样点
for (int i = 0; i < lane.right_sample_size(); ++i)
{
const auto &sample = lane.right_sample(i);
// 这里可以根据采样点信息绘制宽度指示线
}
}
void visualize(const map_data::Map &map_data)
{
std::cout << "开始可视化地图数据..." << std::endl;
std::cout << "版本: " << map_data.header().version() << std::endl;
std::cout << "日期: " << map_data.header().date() << std::endl;
std::cout << "车道数量: " << map_data.lane_size() << std::endl;
// 保存地图数据引用
current_map_data = &map_data;
calculateViewParameters(map_data);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::KeyPressed)
{
handleKeyPress(event.key.code);
}
else if (event.type == sf::Event::MouseWheelScrolled)
{
handleMouseWheel(event.mouseWheelScroll);
}
else if (event.type == sf::Event::MouseButtonPressed)
{
handleMouseButtonPress(event.mouseButton);
}
else if (event.type == sf::Event::MouseButtonReleased)
{
handleMouseButtonRelease(event.mouseButton);
}
else if (event.type == sf::Event::MouseMoved)
{
handleMouseMove(event.mouseMove);
}
}
window.clear(sf::Color::Black);
// 绘制所有车道
for (int i = 0; i < map_data.lane_size(); ++i)
{
drawLane(map_data.lane(i));
}
// 显示信息
displayInfo(map_data);
window.display();
}
}
private:
void calculateViewParameters(const map_data::Map &map_data)
{
if (map_data.lane_size() == 0)
return;
// 计算数据边界
double min_x = std::numeric_limits<double>::max();
double max_x = std::numeric_limits<double>::lowest();
double min_y = std::numeric_limits<double>::max();
double max_y = std::numeric_limits<double>::lowest();
for (int i = 0; i < map_data.lane_size(); ++i)
{
const auto &lane = map_data.lane(i);
updateBoundsFromCurve(lane.central_curve(), min_x, max_x, min_y, max_y);
if (lane.has_left_boundary() && lane.left_boundary().has_curve())
updateBoundsFromCurve(lane.left_boundary().curve(), min_x, max_x, min_y, max_y);
if (lane.has_right_boundary() && lane.right_boundary().has_curve())
updateBoundsFromCurve(lane.right_boundary().curve(), min_x, max_x, min_y, max_y);
}
std::cout << "数据边界 - X:[" << min_x << ", " << max_x
<< "], Y:[" << min_y << ", " << max_y << "]" << std::endl;
// 计算相对坐标范围(相对于基准坐标)
double rel_min_x = min_x - base_x;
double rel_max_x = max_x - base_x;
double rel_min_y = base_y - max_y; // 注意Y轴翻转
double rel_max_y = base_y - min_y;
std::cout << "相对边界 - X:[" << rel_min_x << ", " << rel_max_x
<< "], Y:[" << rel_min_y << ", " << rel_max_y << "]" << std::endl;
// 计算数据在相对坐标下的宽度和高度
double data_width = rel_max_x - rel_min_x;
double data_height = rel_max_y - rel_min_y;
std::cout << "相对数据宽度: " << data_width << ", 高度: " << data_height << std::endl;
// 计算合适的缩放比例
if (data_width > 0 && data_height > 0)
{
double scale_x = 1000.0 / data_width; // 窗口宽度约1000像素
double scale_y = 600.0 / data_height; // 窗口高度约600像素
scale = std::min(scale_x, scale_y) * 0.8; // 取较小值并留边距
}
else
{
// 如果数据范围异常,使用固定缩放
scale = 10.0;
}
// 计算居中偏移
offset_x = 600 - (rel_min_x + data_width / 2) * scale;
offset_y = 400 - (rel_min_y + data_height / 2) * scale;
std::cout << "自动设置 - 缩放: " << scale << ", 偏移: (" << offset_x << ", " << offset_y << ")" << std::endl;
std::cout << "基准坐标: (" << base_x << ", " << base_y << ")" << std::endl;
}
void updateBoundsFromCurve(const map_data::Curve &curve,
double &min_x, double &max_x,
double &min_y, double &max_y)
{
for (int i = 0; i < curve.segment_size(); ++i)
{
const auto &segment = curve.segment(i);
if (segment.has_line_segment())
{
const auto &line_segment = segment.line_segment();
for (int j = 0; j < line_segment.point_size(); ++j)
{
const auto &point = line_segment.point(j);
min_x = std::min(min_x, point.x());
max_x = std::max(max_x, point.x());
min_y = std::min(min_y, point.y());
max_y = std::max(max_y, point.y());
}
}
}
}
void handleKeyPress(sf::Keyboard::Key key)
{
switch (key)
{
case sf::Keyboard::Equal: // +
scale *= 1.1f;
break;
case sf::Keyboard::Dash: // -
scale /= 1.1f;
break;
case sf::Keyboard::R: // 重置视图
resetView();
break;
default:
break;
}
}
void handleMouseWheel(const sf::Event::MouseWheelScrollEvent &event)
{
// 获取鼠标位置
sf::Vector2f mousePos(event.x, event.y);
// 计算鼠标位置对应的地图坐标(缩放前)
double worldX_before = (mousePos.x - offset_x) / scale + base_x;
double worldY_before = base_y - (mousePos.y - offset_y) / scale;
// 执行缩放
double oldScale = scale;
if (event.delta > 0)
{
scale *= 1.1f;
}
else
{
scale /= 1.1f;
}
// 限制缩放范围,避免过大或过小
scale = std::max(0.01, std::min(scale, 1000.0));
// 计算鼠标位置对应的地图坐标(缩放后)
double worldX_after = (mousePos.x - offset_x) / scale + base_x;
double worldY_after = base_y - (mousePos.y - offset_y) / scale;
// 调整偏移量,使鼠标位置对应的地图点保持不变
offset_x += (worldX_after - worldX_before) * scale;
offset_y -= (worldY_after - worldY_before) * scale; // 注意Y轴方向
}
void handleMouseButtonPress(const sf::Event::MouseButtonEvent &event)
{
if (event.button == sf::Mouse::Right)
{
isDragging = true;
lastMousePos = sf::Vector2i(event.x, event.y);
drag_offset_x = offset_x;
drag_offset_y = offset_y;
}
}
void handleMouseButtonRelease(const sf::Event::MouseButtonEvent &event)
{
if (event.button == sf::Mouse::Right)
{
isDragging = false;
}
}
void handleMouseMove(const sf::Event::MouseMoveEvent &event)
{
if (isDragging)
{
// 计算鼠标移动的偏移量
int deltaX = event.x - lastMousePos.x;
int deltaY = event.y - lastMousePos.y;
// 更新视图偏移
offset_x = drag_offset_x + deltaX;
offset_y = drag_offset_y + deltaY;
}
}
// 重置视图函数
void resetView()
{
if (current_map_data != nullptr)
{
calculateViewParameters(*current_map_data);
std::cout << "视图已重置" << std::endl;
}
else
{
// 如果没有地图数据,使用默认值
scale = 1.0;
offset_x = 0;
offset_y = 0;
std::cout << "视图已重置为默认值" << std::endl;
}
}
void displayInfo(const map_data::Map &map_data)
{
// 使用UTF-8编码的字符串
std::string info_text = "Map Version: " + map_data.header().version() +
"\nLanes: " + std::to_string(map_data.lane_size()) +
"\nScale: " + std::to_string(static_cast<int>(scale * 100)) + "%" +
"\nControls: Mouse Wheel - Zoom, Right Click - Pan, R - Reset";
// 创建文本对象
sf::Text info;
// 如果字体加载成功,使用字体;否则使用默认字体
if (font.getInfo().family != "")
{
info.setFont(font);
}
info.setCharacterSize(16);
info.setFillColor(sf::Color::White);
info.setPosition(10, 10);
info.setString(info_text);
// 添加背景矩形使文字更清晰
sf::FloatRect textBounds = info.getLocalBounds();
sf::RectangleShape background(sf::Vector2f(textBounds.width + 20, textBounds.height + 20));
background.setFillColor(sf::Color(0, 0, 0, 128)); // 半透明黑色
background.setPosition(5, 5);
window.draw(background);
window.draw(info);
}
};
bool loadMapData(const std::string &filename, map_data::Map &map_data)
{
std::ifstream file(filename);
if (!file)
{
std::cerr << "无法打开文件: " << filename << std::endl;
return false;
}
// 使用 TextFormat 解析文本格式的 protobuf 数据
google::protobuf::io::IstreamInputStream input_stream(&file);
if (!google::protobuf::TextFormat::Parse(&input_stream, &map_data))
{
std::cerr << "解析文本格式 protobuf 数据失败" << std::endl;
return false;
}
std::cout << "成功加载地图数据" << std::endl;
std::cout << "版本: " << map_data.header().version() << std::endl;
std::cout << "日期: " << map_data.header().date() << std::endl;
std::cout << "车道数量: " << map_data.lane_size() << std::endl;
return true;
}
int main()
{
// 初始化protobuf
GOOGLE_PROTOBUF_VERIFY_VERSION;
map_data::Map map_data;
// 加载地图数据
if (!loadMapData("sim_map.txt", map_data))
{
std::cerr << "加载地图数据失败" << std::endl;
return -1;
}
// 可视化地图
MapVisualizer visualizer;
visualizer.visualize(map_data);
// 清理protobuf
google::protobuf::ShutdownProtobufLibrary();
return 0;
}