Skip to content

Commit d84ed41

Browse files
committed
add cpp client initialize tests
1 parent a88eb7d commit d84ed41

File tree

6 files changed

+195
-2
lines changed

6 files changed

+195
-2
lines changed

invertedai_cpp/examples/BUILD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,32 @@ cc_binary(
4444
"@boost//:beast",
4545
"@opencv",
4646
],
47+
)
48+
49+
cc_binary(
50+
name = "initialize_tests",
51+
srcs = ["initialize_tests.cc"],
52+
data = [
53+
"initialize_body.json",
54+
"initialize_sampling_with_types.json",
55+
"initialize_with_states_and_attributes.json",
56+
],
57+
deps = [
58+
"//invertedai:api",
59+
"@boost//:beast",
60+
"@opencv",
61+
],
62+
)
63+
64+
cc_binary(
65+
name = "drive_tests",
66+
srcs = ["drive_tests.cc"],
67+
data = [
68+
"drive_body.json",
69+
],
70+
deps = [
71+
"//invertedai:api",
72+
"@boost//:beast",
73+
"@opencv",
74+
],
4775
)

invertedai_cpp/examples/drive_tests.cc

Whitespace-only changes.

invertedai_cpp/examples/initialize_body.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"location": "iai:ubc_roundabout",
2+
"location": "carla:Town03",
33
"num_agents_to_spawn": 10,
44
"states_history": null,
5-
"agent_attributes": [[], []],
5+
"agent_attributes": null,
66
"traffic_light_state_history": null,
77
"get_birdview": false,
88
"get_infractions": false,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"location": "carla:Town04",
3+
"num_agents_to_spawn": 10,
4+
"states_history": null,
5+
"agent_attributes": [
6+
["car"],
7+
["pedestrian"],
8+
[],
9+
["pedestrian"],
10+
["pedestrian"]
11+
],
12+
"traffic_light_state_history": null,
13+
"get_birdview": true,
14+
"get_infractions": true,
15+
"random_seed": null,
16+
"location_of_interest": null
17+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <cstdlib>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <sstream>
5+
#include <string>
6+
7+
#include <opencv2/highgui.hpp>
8+
#include <opencv2/imgcodecs.hpp>
9+
#include <opencv2/imgproc.hpp>
10+
11+
#include "../invertedai/api.h"
12+
13+
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
14+
using json = nlohmann::json; // from <json.hpp>
15+
16+
int main(int argc, char **argv) {
17+
try {
18+
const std::string location(argv[1]);
19+
const int agent_num = std::stoi(argv[2]);
20+
const std::string api_key(argv[3]);
21+
22+
net::io_context ioc;
23+
ssl::context ctx(ssl::context::tlsv12_client);
24+
// configure connection setting
25+
invertedai::Session session(ioc, ctx);
26+
session.set_api_key(api_key);
27+
session.connect();
28+
29+
// construct request for getting information about the location
30+
invertedai::LocationInfoRequest loc_info_req(invertedai::read_file("examples/location_info_body.json"));
31+
loc_info_req.set_location(location);
32+
33+
// get response of location information
34+
invertedai::LocationInfoResponse loc_info_res = invertedai::location_info(loc_info_req, &session);
35+
36+
// use opencv to decode and save the bird's eye view image of the simulation
37+
auto image = cv::imdecode(loc_info_res.birdview_image(), cv::IMREAD_COLOR);
38+
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
39+
int frame_width = image.rows;
40+
int frame_height = image.cols;
41+
cv::VideoWriter video(
42+
"initialize_test_run.avi",
43+
cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
44+
10,
45+
cv::Size(frame_width, frame_height)
46+
);
47+
48+
// construct request for initializing the simulation (placing NPCs on the
49+
// map)
50+
invertedai::InitializeRequest init_req(invertedai::read_file("examples/initialize_with_states_and_attributes.json"));
51+
// set the location
52+
init_req.set_location(location);
53+
// set the number of agents
54+
init_req.set_num_agents_to_spawn(agent_num);
55+
// get the response of simulation initialization
56+
invertedai::InitializeResponse init_res = invertedai::initialize(init_req, &session);
57+
58+
image = cv::imdecode(init_res.birdview(), cv::IMREAD_COLOR);
59+
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
60+
video.write(image);
61+
video.release();
62+
} catch (std::exception const &e) {
63+
std::cerr << "Error: " << e.what() << std::endl;
64+
return EXIT_FAILURE;
65+
}
66+
return EXIT_SUCCESS;
67+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"location": "canada:ubc_roundabout",
3+
"num_agents_to_spawn": 10,
4+
"states_history": [
5+
[
6+
[
7+
-11.38,
8+
25.95,
9+
-1.09,
10+
9.42
11+
],
12+
[
13+
48.3,
14+
13.65,
15+
0.11,
16+
0.95
17+
],
18+
[
19+
-5.81,
20+
-37.0,
21+
0.15,
22+
0.47
23+
],
24+
[
25+
-11.75,
26+
24.38,
27+
-2.29,
28+
0.98
29+
],
30+
[
31+
36.69,
32+
6.91,
33+
-3.07,
34+
6.18
35+
]
36+
]
37+
],
38+
"agent_attributes": [
39+
[
40+
5.15,
41+
2.43,
42+
1.96,
43+
"car"
44+
],
45+
[
46+
1.2,
47+
1.51,
48+
null,
49+
"pedestrian"
50+
],
51+
[
52+
5.11,
53+
2.17,
54+
1.94,
55+
"car"
56+
],
57+
[
58+
1.16,
59+
1.41,
60+
null,
61+
"pedestrian"
62+
],
63+
[
64+
5.18,
65+
2.22,
66+
1.97,
67+
"car"
68+
],
69+
[
70+
71+
],
72+
[
73+
"pedestrian"
74+
]
75+
],
76+
"traffic_light_state_history": null,
77+
"get_birdview": true,
78+
"get_infractions": true,
79+
"random_seed": null,
80+
"location_of_interest": null
81+
}

0 commit comments

Comments
 (0)