-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
221 lines (192 loc) · 8.47 KB
/
main.cpp
File metadata and controls
221 lines (192 loc) · 8.47 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
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
#include <opencv2/opencv.hpp>
#include <opencv2/ximgproc.hpp>
#include "yaml-cpp/yaml.h"
#include "mapgraph/zonegraph.hpp"
#include "mapgraph/zone_graph_dot.hpp"
#include "mapgraph/zone_graph_draw.hpp"
#include "preparing/mappreprocessing.hpp"
#include "segmentation/segmentation.hpp"
#include "segmentation/labelmapping.hpp"
#include "segmentation/endpoints.h"
#include "segmentation/zoneclassifier.h"
#include "pddl/pddlgenerator.h"
#include "config.hpp"
#include "utils.hpp"
#include "map_processing.hpp"
#include "visualization.hpp"
using namespace mapping;
int main(int argc, char** argv)
{
if(argc < 2) {
std::cerr << "Usage: " << argv[0] << " <map.pgm> [config.yaml]" << std::endl;
return 1;
}
SegmenterConfig segmenterConfig;
std::string pgmFile = argv[1];
// Если передан файл конфигурации, используем его, иначе "default.yml"
std::string configFile = (argc >= 3) ? argv[2] : "default.yml";
double seedClearanceMeters = 0.0;
double seedClearancePx = 0.0;
// Загружаем YAML-конфигурацию
YAML::Node config;
try {
config = YAML::LoadFile(configFile);
} catch(const std::exception &e) {
std::cerr << "Failed to load config file: " << e.what() << std::endl;
return 1;
}
std::cout << "Loaded config from: " << configFile << std::endl;
if (config["segmentation"])
{
auto segNode = config["segmentation"];
if (segNode["seed_clearance_px"])
seedClearancePx = segNode["seed_clearance_px"].as<double>();
if (segNode["seed_clearance_m"])
seedClearanceMeters = segNode["seed_clearance_m"].as<double>();
}
if (seedClearanceMeters <= 0.0 && config["robot"])
{
auto robotNode = config["robot"];
if (robotNode["radius"])
seedClearanceMeters = robotNode["radius"].as<double>();
else if (robotNode["diameter"])
seedClearanceMeters = 0.5 * robotNode["diameter"].as<double>();
}
// Определяем имя map.yaml: то же, что и pgmFile, только с расширением .yaml
std::string mapYamlFile = pgmFile.substr(0, pgmFile.find_last_of('.')) + ".yaml";
bool mapYamlLoaded = false;
std::vector<double> map_origin; // ожидается вектор из 3 элементов: [x, y, theta]
double resolution = 0.1; // значение по умолчанию (метров на пиксель)
if (std::filesystem::exists(mapYamlFile)) {
try {
YAML::Node map_yaml = YAML::LoadFile(mapYamlFile);
std::cout << "Loaded map YAML from: " << mapYamlFile << std::endl;
if (map_yaml["origin"]) {
map_origin = map_yaml["origin"].as<std::vector<double>>();
if(map_origin.size() < 3) {
std::cerr << "Map YAML 'origin' field has insufficient elements (expected at least 3)." << std::endl;
} else {
std::cout << "Map origin: x = " << map_origin[0]
<< ", y = " << map_origin[1]
<< ", theta = " << map_origin[2] << std::endl;
}
} else {
std::cout << "No origin information in map YAML; origin remains unchanged." << std::endl;
}
if (map_yaml["resolution"]) {
resolution = map_yaml["resolution"].as<double>();
std::cout << "Map resolution: " << resolution << " meters/pixel" << std::endl;
} else {
std::cout << "No resolution information in map YAML; using default: " << resolution << std::endl;
}
mapYamlLoaded = true;
} catch (const std::exception &e) {
std::cerr << "Error loading map YAML: " << e.what() << std::endl;
}
} else {
std::cout << "Map YAML file not found (" << mapYamlFile << "); origin remains unchanged." << std::endl;
}
// Если не удалось загрузить map.yaml, используем значения по умолчанию для origin
if(!mapYamlLoaded || map_origin.size() < 3) {
map_origin = {0.0, 0.0, 0.0};
}
// Загружаем карту (PGM) в оттенках серого
cv::Mat raw = cv::imread(pgmFile, cv::IMREAD_GRAYSCALE);
if(raw.empty()){
std::cerr << "Failed to load image from: " << pgmFile << std::endl;
return 1;
}
std::cout << "Loaded image: " << pgmFile << " (size: " << raw.cols << "x" << raw.rows << ")" << std::endl;
showMatDebug("Raw Map", raw);
// Заполнение структуры MapInfo
mapInfo.resolution = resolution;
mapInfo.originX = map_origin[0];
mapInfo.originY = map_origin[1];
mapInfo.theta = map_origin[2];
mapInfo.height = raw.rows;
mapInfo.width = raw.cols;
if (seedClearancePx <= 0.0 && seedClearanceMeters > 0.0 && mapInfo.resolution > 0.0)
seedClearancePx = seedClearanceMeters / mapInfo.resolution;
cv::Mat raw8u;
raw.convertTo(raw8u, CV_8UC1);
// выравнивание карты(поворот)
cv::Mat aligned;
double alignmentAngle = MapPreprocessing::mapAlign(raw8u,
aligned,
segmenterConfig.alignmentConfig);
mapInfo.theta += alignmentAngle;
if (aligned.empty())
aligned = raw8u.clone();
showMatDebug("aligned Map", aligned);
// Этап денойзинга
cv::Mat out;
auto [rank, cropInfo] = MapPreprocessing::generateDenoisedAlone(aligned,
segmenterConfig.denoiseConfig,
mapInfo.resolution);
{
// Crop shifts the origin by removed left/bottom margins; apply to mapInfo.
int newWidth = aligned.cols - cropInfo.left - cropInfo.right;
int newHeight = aligned.rows - cropInfo.top - cropInfo.bottom;
if (newWidth > 0 && newHeight > 0) {
double dx = cropInfo.left * mapInfo.resolution;
double dy = cropInfo.bottom * mapInfo.resolution;
double c = std::cos(mapInfo.theta);
double s = std::sin(mapInfo.theta);
mapInfo.originX += dx * c - dy * s;
mapInfo.originY += dx * s + dy * c;
mapInfo.width = rank.cols;
mapInfo.height = rank.rows;
}
}
rank.convertTo(out, CV_8U, 255);
showMatDebug("Denoised Map", out);
// Настройка параметров сегментации
SegmentationParams segParams;
segParams.maxIter = 50;
segParams.sigmaStep = 0.5;
segParams.threshold = 0.5;
segParams.seedClearancePx = seedClearancePx;
// Получение меток
LabelsInfo labels = LabelMapping::computeLabels(rank, /*invert=*/false);
// Этап сегментации
auto zones = segmentByGaussianThreshold(rank, labels, segParams);
// Создание матрицы зон
cv::Mat1i segmentation = cv::Mat::zeros(rank.size(), CV_32S);
for (const auto &z : zones) {
segmentation.setTo(z.label, z.mask);
}
// Генерация графа связности зон
ZoneGraph graph;
buildGraph(graph, zones, segmentation, mapInfo, labels.centroids);
// Отрисовка графа связности поверх кадрированного/выравненного изображения
cv::Mat vis = renderZonesOverlay(zones, aligned, cropInfo, 0.65);
mapping::drawZoneGraphOnMap(graph, vis, mapInfo);
cv::imwrite("segmentation_overlay.png", vis);
if(!isHeadlessMode())
{
cv::imshow("segmented", vis);
}
PDDLGenerator gen(graph);
std::cerr << "\(define \(problem PROBLEM_NAME)\n"
<< " \(:domain DOMAIN_NAME)\n"
<< gen.objects()
<< gen.init("ROBOT_CUR_ZONE")
<< gen.goal("ROBOT_GOAL_ZONE")
<< ")\n";
// std::ofstream pddlOut("problem_auto.pddl");
// out << "(define (problem demo)\n";
// out << " (:domain floorplan_navigation)\n";
// out << gen.objects();
// out << gen.init("zone_1");
// out << gen.goal("zone_15");
// out << ")\n";
if(!isHeadlessMode())
{
std::cout << "Press any key to exit..." << std::endl;
cv::waitKey(0);
}
}