Skip to content

Commit b8f140b

Browse files
authored
Merge pull request tier4#1059 from tier4/feature/interpreter/entity_selection
Add `EntitySelection`
2 parents 1b458d8 + 33c1d4e commit b8f140b

File tree

74 files changed

+2132
-158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2132
-158
lines changed

openscenario/openscenario_interpreter/include/openscenario_interpreter/functional/equal_to.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <functional>
2020
#include <limits>
2121
#include <type_traits>
22+
#include <valarray>
2223

2324
namespace openscenario_interpreter
2425
{
@@ -37,6 +38,26 @@ struct equal_to<T, typename std::enable_if<std::is_floating_point<T>::value>::ty
3738
return std::abs(lhs - rhs) < std::numeric_limits<typename std::decay<T>::type>::epsilon();
3839
}
3940
};
41+
42+
template <typename T>
43+
struct equal_to<std::valarray<T>, typename std::enable_if<std::is_floating_point<T>::value>::type>
44+
{
45+
constexpr auto operator()(
46+
const std::valarray<T> & lhs, const std::valarray<T> & rhs) const noexcept
47+
{
48+
return std::abs(lhs - rhs) < std::numeric_limits<typename std::decay<T>::type>::epsilon();
49+
}
50+
51+
constexpr auto operator()(const std::valarray<T> & lhs, const T & rhs) const noexcept
52+
{
53+
return std::abs(lhs - rhs) < std::numeric_limits<typename std::decay<T>::type>::epsilon();
54+
}
55+
56+
constexpr auto operator()(const T & lhs, const std::valarray<T> & rhs) const noexcept
57+
{
58+
return std::abs(lhs - rhs) < std::numeric_limits<typename std::decay<T>::type>::epsilon();
59+
}
60+
};
4061
} // namespace syntax
4162
} // namespace openscenario_interpreter
4263

openscenario/openscenario_interpreter/include/openscenario_interpreter/scope.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <memory>
2323
#include <openscenario_interpreter/name.hpp>
2424
#include <openscenario_interpreter/syntax/catalog_locations.hpp>
25-
#include <openscenario_interpreter/syntax/entity_ref.hpp>
25+
#include <openscenario_interpreter/syntax/entity.hpp>
2626
#include <openscenario_interpreter/utility/demangle.hpp>
2727
#include <unordered_map>
2828
#include <utility>
@@ -190,7 +190,7 @@ class Scope
190190
public:
191191
const std::string name;
192192

193-
std::list<EntityRef> actors;
193+
std::list<Entity> actors;
194194

195195
double seed; // NOTE: `seed` is used only for sharing randomSeed in Stochastic now
196196

openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/acceleration_condition.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <openscenario_interpreter/syntax/rule.hpp>
2222
#include <openscenario_interpreter/syntax/triggering_entities.hpp>
2323
#include <pugixml.hpp>
24+
#include <valarray>
2425

2526
namespace openscenario_interpreter
2627
{
@@ -42,7 +43,7 @@ struct AccelerationCondition : private SimulatorCore::ConditionEvaluation
4243

4344
const TriggeringEntities triggering_entities;
4445

45-
std::vector<double> results; // for description
46+
std::vector<std::valarray<double>> results; // for description
4647

4748
explicit AccelerationCondition(const pugi::xml_node &, Scope &, const TriggeringEntities &);
4849

openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/by_type.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#ifndef OPENSCENARIO_INTERPRETER__SYNTAX__BY_TYPE_HPP_
1616
#define OPENSCENARIO_INTERPRETER__SYNTAX__BY_TYPE_HPP_
1717

18+
#include <openscenario_interpreter/scope.hpp>
1819
#include <openscenario_interpreter/syntax/object_type.hpp>
20+
#include <pugixml.hpp>
1921

2022
namespace openscenario_interpreter
2123
{
@@ -28,8 +30,9 @@ inline namespace syntax
2830
* </xsd:complexType>
2931
*
3032
* -------------------------------------------------------------------------- */
31-
struct ByType
33+
struct ByType : public ObjectType
3234
{
35+
explicit ByType(const pugi::xml_node &, Scope &);
3336
};
3437
} // namespace syntax
3538
} // namespace openscenario_interpreter

openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/distance_condition.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <openscenario_interpreter/syntax/rule.hpp>
2727
#include <openscenario_interpreter/syntax/triggering_entities.hpp>
2828
#include <pugixml.hpp>
29+
#include <valarray>
2930

3031
namespace openscenario_interpreter
3132
{
@@ -105,7 +106,7 @@ struct DistanceCondition : private Scope, private SimulatorCore::ConditionEvalua
105106

106107
const TriggeringEntities triggering_entities;
107108

108-
std::vector<Double> results; // for description
109+
std::vector<std::valarray<double>> results; // for description
109110

110111
const bool consider_z;
111112

openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/entities.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define OPENSCENARIO_INTERPRETER__SYNTAX__ENTITIES_HPP_
1717

1818
#include <openscenario_interpreter/scope.hpp>
19+
#include <openscenario_interpreter/syntax/entity.hpp>
1920
#include <openscenario_interpreter/syntax/entity_ref.hpp>
2021
#include <pugixml.hpp>
2122

@@ -37,7 +38,7 @@ struct Entities : public std::unordered_map<std::string, Object> // TODO to be
3738
{
3839
explicit Entities(const pugi::xml_node &, Scope &);
3940

40-
auto isAdded(const EntityRef &) const -> bool;
41+
auto isAdded(const Entity &) const -> bool;
4142

4243
auto ref(const EntityRef &) const -> Object;
4344
};
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2015 TIER IV, Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef OPENSCENARIO_INTERPRETER__SYNTAX__ENTITY_HPP_
16+
#define OPENSCENARIO_INTERPRETER__SYNTAX__ENTITY_HPP_
17+
18+
#include <algorithm>
19+
#include <cstddef>
20+
#include <functional>
21+
#include <iterator>
22+
#include <openscenario_interpreter/object.hpp>
23+
#include <openscenario_interpreter/syntax/entity_ref.hpp>
24+
#include <openscenario_interpreter/syntax/object_type.hpp>
25+
#include <openscenario_interpreter/syntax/string.hpp>
26+
#include <pugixml.hpp>
27+
#include <scenario_simulator_exception/exception.hpp>
28+
#include <set>
29+
#include <type_traits>
30+
#include <valarray>
31+
#include <vector>
32+
33+
namespace openscenario_interpreter
34+
{
35+
struct Scope;
36+
37+
inline namespace syntax
38+
{
39+
struct Entities;
40+
41+
struct ScenarioObject;
42+
43+
struct EntitySelection;
44+
45+
struct Entity : public Object
46+
{
47+
Entity() = default;
48+
49+
Entity(const ScenarioObject &);
50+
51+
Entity(const EntitySelection &);
52+
53+
Entity(const String &, const Scope &);
54+
55+
Entity(const pugi::xml_node &, const Scope &);
56+
57+
auto name() const -> String;
58+
59+
auto objects() const -> std::set<Entity>;
60+
61+
auto objectTypes() const -> std::set<ObjectType::value_type>;
62+
63+
template <typename Function>
64+
auto apply(const Function & function) const
65+
{
66+
using Result = std::invoke_result_t<Function, Entity>;
67+
auto objects = this->objects();
68+
if constexpr (std::is_same_v<Result, void>) {
69+
std::for_each(std::begin(objects), std::end(objects), function);
70+
} else {
71+
auto results = std::valarray<Result>(objects.size());
72+
std::transform(std::begin(objects), std::end(objects), std::begin(results), function);
73+
return results;
74+
}
75+
}
76+
77+
/**
78+
* This function is for ScenarioObject only.
79+
* @throws std::runtime_error if the entity is not a ScenarioObject.
80+
* @note To iterate over all objects, use `apply` function instead.
81+
* @note To get the name of the entity, use `name` function instead.
82+
*/
83+
operator EntityRef() const;
84+
};
85+
86+
auto operator==(const Entity &, const Entity &) -> bool;
87+
88+
auto operator<<(std::ostream &, const Entity &) -> std::ostream &;
89+
} // namespace syntax
90+
} // namespace openscenario_interpreter
91+
92+
template <>
93+
struct std::hash<openscenario_interpreter::Entity>
94+
{
95+
auto operator()(const openscenario_interpreter::Entity & entity) const -> std::size_t
96+
{
97+
return std::hash<void *>{}(entity.get());
98+
}
99+
};
100+
101+
#endif // OPENSCENARIO_INTERPRETER__SYNTAX__ENTITY_HPP_

openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/entity_action.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <openscenario_interpreter/scope.hpp>
1919
#include <openscenario_interpreter/syntax/add_entity_action.hpp>
2020
#include <openscenario_interpreter/syntax/delete_entity_action.hpp>
21+
#include <openscenario_interpreter/syntax/entity.hpp>
2122
#include <openscenario_interpreter/syntax/string.hpp>
2223
#include <pugixml.hpp>
2324

@@ -38,7 +39,7 @@ inline namespace syntax
3839
* -------------------------------------------------------------------------- */
3940
struct EntityAction : public ComplexType
4041
{
41-
const String entity_ref;
42+
const Entity entity_ref;
4243

4344
explicit EntityAction(const pugi::xml_node &, Scope &);
4445

openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/entity_object.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define OPENSCENARIO_INTERPRETER__SYNTAX__ENTITY_OBJECT_HPP_
1717

1818
#include <openscenario_interpreter/syntax/misc_object.hpp>
19+
#include <openscenario_interpreter/syntax/object_type.hpp>
1920
#include <openscenario_interpreter/syntax/pedestrian.hpp>
2021
#include <openscenario_interpreter/syntax/vehicle.hpp>
2122

@@ -38,8 +39,18 @@ inline namespace syntax
3839
struct EntityObject : public Group
3940
{
4041
explicit EntityObject(const pugi::xml_node &, Scope &);
42+
43+
auto objectType() const -> ObjectType::value_type;
4144
};
4245

46+
DEFINE_LAZY_VISITOR(
47+
const EntityObject,
48+
// CASE(CatalogReference), //
49+
CASE(Vehicle), //
50+
CASE(Pedestrian), //
51+
CASE(MiscObject), //
52+
);
53+
4354
DEFINE_LAZY_VISITOR(
4455
EntityObject,
4556
// CASE(CatalogReference), //

openscenario/openscenario_interpreter/include/openscenario_interpreter/syntax/entity_ref.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ inline namespace syntax
3333
*/
3434
struct EntityRef : public String
3535
{
36-
template <typename... Ts>
37-
EntityRef(Ts &&... xs) : String(std::forward<decltype(xs)>(xs)...)
38-
{
39-
}
36+
EntityRef(const String & string) : String(string) {}
4037

4138
template <typename Node, typename Scope>
4239
explicit EntityRef(const Node & node, Scope & scope)

0 commit comments

Comments
 (0)