-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1423 from tier4/feature/by_object_type
Support `ByType` element in `CollisionCondition`
- Loading branch information
Showing
5 changed files
with
560 additions
and
7 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...nario/openscenario_interpreter/include/openscenario_interpreter/syntax/by_object_type.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2015 TIER IV, Inc. All rights reserved. | ||
// | ||
// 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. | ||
|
||
#ifndef OPENSCENARIO_INTERPRETER__SYNTAX__BY_OBJECT_TYPE_HPP_ | ||
#define OPENSCENARIO_INTERPRETER__SYNTAX__BY_OBJECT_TYPE_HPP_ | ||
|
||
#include <algorithm> | ||
#include <iterator> | ||
#include <openscenario_interpreter/object.hpp> | ||
#include <openscenario_interpreter/scope.hpp> | ||
#include <openscenario_interpreter/syntax/object_type.hpp> | ||
#include <openscenario_interpreter/syntax/string.hpp> | ||
#include <pugixml.hpp> | ||
#include <scenario_simulator_exception/exception.hpp> | ||
#include <set> | ||
#include <type_traits> | ||
#include <valarray> | ||
#include <vector> | ||
|
||
namespace openscenario_interpreter | ||
{ | ||
inline namespace syntax | ||
{ | ||
/* | ||
OpenSCENARIO XML 1.3 ByObjectType | ||
Defines an object type to select entities. | ||
<xsd:complexType name="ByObjectType"> | ||
<xsd:attribute name="type" type="ObjectType" use="required"/> | ||
</xsd:complexType> | ||
*/ | ||
struct ByObjectType : public Scope | ||
{ | ||
ObjectType type; | ||
|
||
explicit ByObjectType(const pugi::xml_node &, Scope &); | ||
|
||
auto objects() const -> std::set<Entity>; | ||
|
||
template <typename Function> | ||
auto apply(const Function & function) const | ||
{ | ||
using Result = std::invoke_result_t<Function, Entity>; | ||
auto objects = this->objects(); | ||
if constexpr (std::is_same_v<Result, void>) { | ||
std::for_each(std::begin(objects), std::end(objects), function); | ||
} else { | ||
auto results = std::valarray<Result>(objects.size()); | ||
std::transform(std::begin(objects), std::end(objects), std::begin(results), function); | ||
return results; | ||
} | ||
} | ||
}; | ||
} // namespace syntax | ||
} // namespace openscenario_interpreter | ||
|
||
#endif // OPENSCENARIO_INTERPRETER__SYNTAX__BY_OBJECT_TYPE_HPP_ |
44 changes: 44 additions & 0 deletions
44
openscenario/openscenario_interpreter/src/syntax/by_object_type.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2015 TIER IV, Inc. All rights reserved. | ||
// | ||
// 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 <openscenario_interpreter/syntax/by_object_type.hpp> | ||
#include <openscenario_interpreter/syntax/entities.hpp> | ||
#include <openscenario_interpreter/syntax/entity.hpp> | ||
#include <openscenario_interpreter/syntax/scenario_object.hpp> | ||
#include <scenario_simulator_exception/exception.hpp> | ||
|
||
namespace openscenario_interpreter | ||
{ | ||
inline namespace syntax | ||
{ | ||
ByObjectType::ByObjectType(const pugi::xml_node & node, Scope & scope) | ||
: Scope(scope), type(readAttribute<ObjectType>("type", node, scope)) | ||
{ | ||
if (type.value == ObjectType::external) { | ||
THROW_SEMANTIC_ERROR("ObjectType::external does not support yet"); | ||
} | ||
} | ||
|
||
auto ByObjectType::objects() const -> std::set<Entity> | ||
{ | ||
std::set<Entity> result; | ||
for (const auto & [name, object] : *global().entities) { | ||
if (object.is<ScenarioObject>() and object.as<ScenarioObject>().objectType() == type) { | ||
result.emplace(name, *this); | ||
} | ||
} | ||
return result; | ||
} | ||
} // namespace syntax | ||
} // namespace openscenario_interpreter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.