scenario_simulator_v2 C++ API
time_to_collision_condition.hpp
Go to the documentation of this file.
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__TIME_TO_COLLISION_CONDITION_HPP_
16 #define OPENSCENARIO_INTERPRETER__SYNTAX__TIME_TO_COLLISION_CONDITION_HPP_
17 
26 
28 {
29 inline namespace syntax
30 {
31 /*
32  TimeToCollisionCondition (OpenSCENARIO XML 1.3.1)
33 
34  The currently predicted time to collision of a triggering entity/entities
35  and either a reference entity or an explicit position is compared to a given
36  time value. Time to collision is calculated as the distance divided by the
37  relative speed. Acceleration of entities is not taken into consideration.
38  For the relative speed calculation the same coordinate system and relative
39  distance type applies as for the distance calculation. If the relative speed
40  is negative, which means the entity is moving away from the position / the
41  entities are moving away from each other, then the time to collision cannot
42  be predicted and the condition evaluates to false. The logical operator for
43  comparison is defined by the rule attribute. The property "alongRoute" is
44  deprecated. If "coordinateSystem" or "relativeDistanceType" are set,
45  "alongRoute" is ignored.
46 
47  <xsd:complexType name="TimeToCollisionCondition">
48  <xsd:all>
49  <xsd:element name="TimeToCollisionConditionTarget" type="TimeToCollisionConditionTarget"/>
50  </xsd:all>
51  <xsd:attribute name="alongRoute" type="Boolean">
52  <xsd:annotation>
53  <xsd:appinfo>
54  deprecated
55  </xsd:appinfo>
56  </xsd:annotation>
57  </xsd:attribute>
58  <xsd:attribute name="freespace" type="Boolean" use="required"/>
59  <xsd:attribute name="rule" type="Rule" use="required"/>
60  <xsd:attribute name="value" type="Double" use="required"/>
61  <xsd:attribute name="relativeDistanceType" type="RelativeDistanceType"/>
62  <xsd:attribute name="coordinateSystem" type="CoordinateSystem"/>
63  <xsd:attribute name="routingAlgorithm" type="RoutingAlgorithm"/>
64  </xsd:complexType>
65 */
67 {
69 
71 
72  const Rule rule;
73 
74  const Double value;
75 
77 
79 
81 
83 
84  std::vector<std::valarray<double>> evaluations;
85 
87  const pugi::xml_node & node, Scope & scope, const TriggeringEntities & triggering_entities)
88  : Scope(scope),
90  readElement<TimeToCollisionConditionTarget>("TimeToCollisionConditionTarget", node, scope)),
91  freespace(readAttribute<Boolean>("freespace", node, scope)),
92  rule(readAttribute<Rule>("rule", node, scope)),
93  value(readAttribute<Double>("value", node, scope)),
95  readAttribute<RelativeDistanceType>("relativeDistanceType", node, scope)),
97  readAttribute<CoordinateSystem>("coordinateSystem", node, scope, CoordinateSystem::entity)),
99  "routingAlgorithm", node, scope, RoutingAlgorithm::undefined)),
101  evaluations(triggering_entities.entity_refs.size(), {Double::nan()})
102  {
103  }
104 
105  auto description() const
106  {
107  auto description = std::stringstream();
108 
109  description << triggering_entities.description() << "'s time-to-collision to given ";
110 
113  } else {
114  description << " position";
115  }
116 
117  description << " = ";
118 
120 
121  description << " " << rule << " " << value << "?";
122 
123  return description.str();
124  }
125 
126  static auto evaluate(
127  const Entities * entities, const Entity & triggering_entity,
131  {
132  auto distance = [&]() {
135  entities, triggering_entity, time_to_collision_condition_target.as<Entity>(),
137  } else {
139  entities, triggering_entity, time_to_collision_condition_target.as<Position>(),
141  }
142  };
143 
144  auto speed = [&]() {
145  auto directional_dimension = [&]() {
146  switch (relative_distance_type) {
148  return std::optional<DirectionalDimension>(DirectionalDimension::longitudinal);
150  return std::optional<DirectionalDimension>(DirectionalDimension::lateral);
151  default:
153  return std::optional<DirectionalDimension>(std::nullopt);
154  };
155  };
158  entities, triggering_entity, time_to_collision_condition_target.as<Entity>(),
159  directional_dimension());
160  } else {
162  entities, triggering_entity, directional_dimension(), Compatibility::standard);
163  }
164  };
165 
166  return distance() / std::max(speed(), 0.0);
167  }
168 
169  auto evaluate()
170  {
171  evaluations.clear();
172 
173  return asBoolean(triggering_entities.apply([this](const auto & triggering_entity) {
174  evaluations.push_back(triggering_entity.apply([this](const auto & triggering_entity) {
175  return evaluate(
176  global().entities, triggering_entity, time_to_collision_condition_target,
177  coordinate_system, relative_distance_type, routing_algorithm, freespace);
178  }));
179  return not evaluations.back().size() or std::invoke(rule, evaluations.back(), value).min();
180  }));
181  }
182 };
183 } // namespace syntax
184 } // namespace openscenario_interpreter
185 
186 #endif // OPENSCENARIO_INTERPRETER__SYNTAX__TIME_TO_COLLISION_CONDITION_HPP_
auto as() const -> U &
Definition: pointer.hpp:109
auto is() const -> bool
Definition: pointer.hpp:97
Definition: scope.hpp:154
auto size(const T &range)
Definition: size.hpp:25
auto readAttribute(const std::string &name, const Node &node, const Scope &scope) -> T
Definition: attribute.hpp:104
auto readElement(const std::string &name, const pugi::xml_node &parent, Scope &scope)
Definition: element.hpp:75
auto asBoolean(bool) -> const Object &
Definition: boolean.cpp:62
auto print_to(std::ostream &os, const T &value) -> std::enable_if_t< concepts::HasStreamOutputOperator< T >::value, std::ostream & >
Definition: print.hpp:29
Definition: hypot.hpp:22
auto distance(const geometry_msgs::Pose &pose1, const geometry_msgs::Pose &pose2)
Definition: detection_sensor.cpp:36
Definition: coordinate_system.hpp:50
@ longitudinal
Definition: directional_dimension.hpp:48
@ lateral
Definition: directional_dimension.hpp:53
auto evaluate() -> Object
Definition: distance_condition.cpp:612
static auto nan() noexcept -> Double
Definition: double.cpp:44
auto evaluate() -> Object
Definition: relative_distance_condition.cpp:332
Definition: relative_distance_type.hpp:55
@ longitudinal
Definition: relative_distance_type.hpp:59
@ euclidianDistance
Definition: relative_distance_type.hpp:61
@ lateral
Definition: relative_distance_type.hpp:60
auto evaluate() -> Object
Definition: relative_speed_condition.cpp:98
Definition: routing_algorithm.hpp:48
auto evaluate() -> Object
Definition: speed_condition.cpp:85
Definition: time_to_collision_condition_target.hpp:39
Definition: time_to_collision_condition.hpp:67
const RelativeDistanceType relative_distance_type
Definition: time_to_collision_condition.hpp:76
const Rule rule
Definition: time_to_collision_condition.hpp:72
const RoutingAlgorithm routing_algorithm
Definition: time_to_collision_condition.hpp:80
const Boolean freespace
Definition: time_to_collision_condition.hpp:70
const CoordinateSystem coordinate_system
Definition: time_to_collision_condition.hpp:78
auto evaluate()
Definition: time_to_collision_condition.hpp:169
const Double value
Definition: time_to_collision_condition.hpp:74
const TimeToCollisionConditionTarget time_to_collision_condition_target
Definition: time_to_collision_condition.hpp:68
auto description() const
Definition: time_to_collision_condition.hpp:105
std::vector< std::valarray< double > > evaluations
Definition: time_to_collision_condition.hpp:84
static auto evaluate(const Entities *entities, const Entity &triggering_entity, const TimeToCollisionConditionTarget &time_to_collision_condition_target, CoordinateSystem coordinate_system, RelativeDistanceType relative_distance_type, RoutingAlgorithm routing_algorithm, Boolean freespace)
Definition: time_to_collision_condition.hpp:126
TimeToCollisionCondition(const pugi::xml_node &node, Scope &scope, const TriggeringEntities &triggering_entities)
Definition: time_to_collision_condition.hpp:86
const TriggeringEntities triggering_entities
Definition: time_to_collision_condition.hpp:82
Definition: triggering_entities.hpp:40
auto apply(Predicate &&predicate) const -> decltype(auto)
Definition: triggering_entities.hpp:48
auto description() const -> String
Definition: triggering_entities.cpp:31