File size: 4,912 Bytes
9897e20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "gtest/gtest.h"
#include "consts.hpp"
#include "mgr.hpp"
#include "test_utils.hpp"

#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <random>


using namespace madrona;
using nlohmann::json;

class ObservationsTest : public ::testing::Test {
protected:    
    uint32_t num_worlds = 1;
    
    madrona_gpudrive::Manager mgr = madrona_gpudrive::Manager({
        .execMode = ExecMode::CPU,
        .gpuID = 0,
        .scenes = {"testJsons/test.json"},
        .params = {
            .polylineReductionThreshold = 0.0,
            .observationRadius = 100.0,
            .collisionBehaviour = madrona_gpudrive::CollisionBehaviour::Ignore,
            .roadObservationAlgorithm = madrona_gpudrive::FindRoadObservationsWith::KNearestEntitiesWithRadiusFiltering
        }
    });


    std::pair<float, float> mean = {0, 0};

    std::ifstream data = std::ifstream("testJsons/test.json");
    std::vector<std::vector<std::pair<float, float>>> roadGeoms;
    std::vector<float> roadTypes;

    void SetUp() override {
        json rawJson;
        data >> rawJson;
        mean = test_utils::calcMean(rawJson);

        std::cout<<"CTEST Mean x: "<<mean.first<<" Mean y: "<<mean.second<<std::endl;

        int64_t n_roads = 0;
        for (const auto &obj : rawJson["roads"]) {
            std::vector<std::pair<float, float>> roadGeom;
            for (const auto &point: obj["geometry"])
            {
                roadGeom.push_back({point["x"], point["y"]});
            }
            roadGeoms.push_back(roadGeom);

            if(obj["type"] == "road_edge")
            {
                roadTypes.push_back((float)madrona_gpudrive::EntityType::RoadEdge);
            }
            else if(obj["type"] == "road_line")
            {
                roadTypes.push_back((float)madrona_gpudrive::EntityType::RoadLine);
            }
            else if(obj["type"] == "lane")
            {
                roadTypes.push_back((float)madrona_gpudrive::EntityType::RoadLane);
            }
            else if(obj["type"] == "crosswalk")
            {
                roadTypes.push_back((float)madrona_gpudrive::EntityType::CrossWalk);
            }
            else if(obj["type"] == "speed_bump")
            {
                roadTypes.push_back((float)madrona_gpudrive::EntityType::SpeedBump);
            }
            else if(obj["type"] == "stop_sign")
            {
                roadTypes.push_back((float)madrona_gpudrive::EntityType::StopSign);
            }
            else if(obj["type"] == "invalid")
            {
                roadTypes.push_back(0);
            }
        }
    }
};

TEST_F(ObservationsTest, TestObservations) {
    auto obs = mgr.mapObservationTensor();
    auto flat_obs = test_utils::flatten_obs(obs);

    int64_t idx = 0;
    for(int64_t i = 0; i < roadGeoms.size(); i++)
    {
        std::vector<std::pair<float, float>> roadGeom = roadGeoms[i];
        float roadType = roadTypes[i];
        for(int64_t j = 0; j < roadGeom.size() - 1; j++)
        {
            if(roadType > (float)madrona_gpudrive::EntityType::RoadLane && roadType < (float)madrona_gpudrive::EntityType::StopSign)
            {
                float x = (roadGeom[j].first + roadGeom[j+1].first + roadGeom[j+2].first + roadGeom[j+3].first)/4 - mean.first;
                float y = (roadGeom[j].second + roadGeom[j+1].second + roadGeom[j+2].second + roadGeom[j+3].second)/4 - mean.second;

                ASSERT_NEAR(flat_obs[idx], x, test_utils::EPSILON);
                ASSERT_NEAR(flat_obs[idx+1], y, test_utils::EPSILON);
                ASSERT_EQ(flat_obs[idx+6], roadType);
                idx += madrona_gpudrive::MapObservationExportSize;
                break;
            }
            else if(roadType == (float)madrona_gpudrive::EntityType::StopSign)
            {
                float x = roadGeom[j].first - mean.first;
                float y = roadGeom[j].second - mean.second;

                ASSERT_NEAR(flat_obs[idx], x, test_utils::EPSILON);
                ASSERT_NEAR(flat_obs[idx+1], y, test_utils::EPSILON);
                ASSERT_EQ(flat_obs[idx+6], roadType);
                idx += 7;
                break;
            }

            float x1 = roadGeom[j].first - mean.first;
            float y1 = roadGeom[j].second - mean.second;
            float x2 = roadGeom[j+1].first - mean.first;
            float y2 = roadGeom[j+1].second - mean.second;
            float dx = (x2 + x1)/2;
            float dy = (y2 + y1)/2;

            ASSERT_NEAR(flat_obs[idx], dx, test_utils::EPSILON);
            ASSERT_NEAR(flat_obs[idx+1], dy, test_utils::EPSILON);
            ASSERT_FLOAT_EQ(flat_obs[idx+6], roadType) << "i = " << i << " j = " << j << " idx = " << idx;
            idx += madrona_gpudrive::MapObservationExportSize;
        }
        if(idx >= flat_obs.size())
        {
            break;
        }
    }
}