-
Notifications
You must be signed in to change notification settings - Fork 268
/
environment_preload_system.cc
150 lines (129 loc) · 4.8 KB
/
environment_preload_system.cc
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
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gtest/gtest.h>
#include <gz/common/Filesystem.hh>
#include <gz/utils/ExtraTestMacros.hh>
#include "gz/sim/components/Environment.hh"
#include "gz/sim/Server.hh"
#include "gz/sim/TestFixture.hh"
#include "gz/sim/Util.hh"
#include "test_config.hh"
#include "../helpers/EnvTestFixture.hh"
#include "../helpers/Relay.hh"
using namespace gz;
using namespace sim;
/// \brief Test EnvironmentPreload system
class EnvironmentPreloadTest : public InternalFixture<::testing::Test>
{
};
/////////////////////////////////////////////////
TEST_F(EnvironmentPreloadTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(CanPreload))
{
// Start server
ServerConfig serverConfig;
const auto sdfFile = common::joinPaths(
std::string(PROJECT_BINARY_PATH), "test",
"worlds", "environmental_data.sdf");
serverConfig.SetSdfFile(sdfFile);
Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
bool dataLoaded{false};
// Create a system that looks for environmental data components
test::Relay testSystem;
testSystem.OnPostUpdate(
[&](const sim::UpdateInfo &,
const sim::EntityComponentManager &_ecm)
{
_ecm.EachNew<components::Environment>(
[&](const gz::sim::Entity &,
const components::Environment *_component) -> bool
{
auto data = _component->Data();
EXPECT_TRUE(data->frame.Has("humidity"));
const auto &humidityData = data->frame["humidity"];
auto humiditySession = humidityData.StepTo(
humidityData.CreateSession(), 1658923062.5);
EXPECT_TRUE(humiditySession.has_value());
if (humiditySession.has_value())
{
const math::Vector3d position{36.80079505, -121.789472517, 0.8};
auto humidity =
humidityData.LookUp(humiditySession.value(),
position);
EXPECT_NEAR(89.5, humidity.value_or(0.), 1e-6);
dataLoaded = true;
}
EXPECT_EQ(data->reference, math::SphericalCoordinates::SPHERICAL);
return true;
});
});
server.AddSystem(testSystem.systemPtr);
// Run server
server.RunOnce();
EXPECT_TRUE(dataLoaded);
}
/////////////////////////////////////////////////
TEST_F(EnvironmentPreloadTest,
GZ_UTILS_TEST_DISABLED_ON_WIN32(CorrectSphericalTransform))
{
// Start server
ServerConfig serverConfig;
const auto sdfFile = common::joinPaths(
std::string(PROJECT_BINARY_PATH), "test",
"worlds", "environmental_data.sdf");
serverConfig.SetSdfFile(sdfFile);
Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
bool dataLoaded{false};
// Create a system that looks for environmental data components
test::Relay testSystem;
testSystem.OnPostUpdate(
[&](const sim::UpdateInfo &,
const sim::EntityComponentManager &_ecm)
{
_ecm.EachNew<components::Environment>(
[&](const gz::sim::Entity &,
const components::Environment *_component) -> bool
{
auto data = _component->Data();
EXPECT_TRUE(data->frame.Has("humidity"));
const auto &humidityData = data->frame["humidity"];
auto humiditySession = humidityData.StepTo(
humidityData.CreateSession(), 1658923062.5);
EXPECT_TRUE(humiditySession.has_value());
if (humiditySession.has_value())
{
const math::Vector3d position{0, 0, 0.8};
auto transformedCoordinates =
getGridFieldCoordinates(_ecm, position, data);
auto humidity =
humidityData.LookUp(humiditySession.value(),
transformedCoordinates.value());
EXPECT_NEAR(89.5, humidity.value_or(0.), 1e-6);
dataLoaded = true;
}
EXPECT_EQ(data->reference, math::SphericalCoordinates::SPHERICAL);
return true;
});
});
server.AddSystem(testSystem.systemPtr);
// Run server
server.RunOnce();
EXPECT_TRUE(dataLoaded);
}