Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/fmi/libsumocpp2c.cpp
169665 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2020-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file libsumocpp2c.cpp
15
/// @author Robert Hilbrich
16
/// @author Matthias Schwamborn
17
/// @date Mon, 17 Aug 2020
18
///
19
// Implementation of the libsumo c++ to c wrapper
20
/****************************************************************************/
21
22
#ifdef _MSC_VER
23
// Avoid warnings in windows build because of strcpy instead of strcpy_s,
24
// because the latter is not available on all platforms
25
#define _CRT_SECURE_NO_WARNINGS
26
#pragma warning(disable:4820 4514 5045 4668 4710)
27
#endif
28
29
#include <sstream>
30
#include <iostream>
31
#include <stdlib.h>
32
#include <libsumo/TraCIConstants.h>
33
#include <libsumo/TraCIDefs.h>
34
#include <libsumo/Simulation.h>
35
#include <libsumo/Vehicle.h>
36
37
#include "libsumocpp2c.h"
38
39
#define DELIMITER ' '
40
41
inline char*
42
allocateAndCopyString(ModelInstance* comp, const std::string& s) {
43
char* buf = NULL;
44
buf = (char*)comp->allocateMemory(1 + s.length(), sizeof(char));
45
s.copy(buf, 1 + s.length());
46
return buf;
47
}
48
49
void
50
libsumo_load(char* callOptions) {
51
// Tokenize the string, because Simulation::load expects a vector
52
std::vector<std::string> options;
53
std::stringstream ss(callOptions);
54
std::string temp_str;
55
while (std::getline(ss, temp_str, DELIMITER)) {
56
options.push_back(temp_str);
57
}
58
59
try {
60
libsumo::Simulation::load(options);
61
} catch (const libsumo::TraCIException& e) {
62
std::cerr << "libsumo::Simulation::load() failed - reason: " << e.what() << std::endl;
63
abort();
64
}
65
}
66
67
void
68
libsumo_close(void) {
69
libsumo::Simulation::close();
70
}
71
72
void
73
libsumo_step(double time) {
74
libsumo::Simulation::step(time);
75
}
76
77
int
78
libsumo_vehicle_getIDCount(void) {
79
return libsumo::Vehicle::getIDCount();
80
}
81
82
void
83
libsumo_vehicle_moveToXY(const char* paramString) {
84
try {
85
std::vector<std::string> params;
86
std::stringstream ss(paramString);
87
std::string temp_str;
88
while (std::getline(ss, temp_str, DELIMITER)) {
89
params.push_back(temp_str);
90
}
91
char* pEnd;
92
const std::string vehID = params[0];
93
const std::string edgeID = params[1];
94
int laneIndex = strtol(params[2].c_str(), &pEnd, 10);
95
double x = strtod(params[3].c_str(), &pEnd);
96
double y = strtod(params[4].c_str(), &pEnd);
97
double angle = params.size() >= 6 ? strtod(params[5].c_str(), &pEnd) : libsumo::INVALID_DOUBLE_VALUE;
98
int keepRoute = params.size() >= 7 ? strtol(params[6].c_str(), &pEnd, 10) : 1;
99
double matchThreshold = params.size() >= 8 ? strtod(params[7].c_str(), &pEnd) : 100;
100
libsumo::Vehicle::moveToXY(vehID, edgeID, laneIndex, x, y, angle, keepRoute, matchThreshold);
101
} catch (const std::runtime_error& e) {
102
std::cerr << "libsumo::Vehicle::moveToXY() failed - reason: " << e.what() << std::endl;
103
abort();
104
}
105
}
106
107
void
108
libsumo_vehicle_getParameterWithKey(ModelInstance* comp, const char** result) {
109
try {
110
std::vector<std::string> params;
111
std::stringstream ss(comp->getterParameters);
112
std::string temp_str;
113
while (std::getline(ss, temp_str, DELIMITER)) {
114
params.push_back(temp_str);
115
}
116
117
const std::string vehID = params[0];
118
const std::string key = params[1];
119
std::pair<std::string, std::string> p = libsumo::Vehicle::getParameterWithKey(vehID, key);
120
const std::string resultString = p.first + DELIMITER + p.second;
121
*result = allocateAndCopyString(comp, resultString);
122
} catch (const std::runtime_error& e) {
123
std::cerr << "libsumo::Vehicle::getParameterWithKey() failed - reason: " << e.what() << std::endl;
124
abort();
125
}
126
}
127
128
void
129
libsumo_vehicle_getLaneID(ModelInstance* comp, const char** result) {
130
try {
131
std::vector<std::string> params;
132
std::stringstream ss(comp->getterParameters);
133
std::string temp_str;
134
while (std::getline(ss, temp_str, DELIMITER)) {
135
params.push_back(temp_str);
136
}
137
138
const std::string vehID = params[0];
139
*result = allocateAndCopyString(comp, libsumo::Vehicle::getLaneID(vehID));
140
} catch (const std::runtime_error& e) {
141
std::cerr << "libsumo::Vehicle::getLaneID() failed - reason: " << e.what() << std::endl;
142
abort();
143
}
144
}
145
146
void
147
libsumo_vehicle_getPosition(ModelInstance* comp, const char** result) {
148
try {
149
std::vector<std::string> params;
150
std::stringstream ss(comp->getterParameters);
151
std::string temp_str;
152
while (std::getline(ss, temp_str, DELIMITER)) {
153
params.push_back(temp_str);
154
}
155
156
const std::string vehID = params[0];
157
libsumo::TraCIPosition pos = libsumo::Vehicle::getPosition(vehID);
158
std::ostringstream os;
159
os << pos.x << DELIMITER << pos.y;
160
*result = allocateAndCopyString(comp, os.str());
161
} catch (const std::runtime_error& e) {
162
std::cerr << "libsumo::Vehicle::getPosition() failed - reason: " << e.what() << std::endl;
163
abort();
164
}
165
}
166
167