Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a adjacency checker for the builder. #45

Merged
merged 5 commits into from Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 21 additions & 21 deletions include/maliput_sparse/parser/validator.h
Expand Up @@ -29,20 +29,17 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once

#include <ostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include "maliput_sparse/parser/parser.h"

namespace maliput_sparse {
namespace parser {

/// ValidatorOptions struct that contains the options for the Validator.
struct ValidatorOptions {
/// Verifies adjacency for lanes in a segment.
bool lane_adjacency{true};
};

/// ValidatorConfig struct that contains the configuration for the Validator.
struct ValidatorConfig {
double linear_tolerance{1e-12};
Expand All @@ -56,13 +53,14 @@ struct ValidatorConfig {
/// severity. It's on the user to decide how to handle the errors.
class Validator {
agalbachicar marked this conversation as resolved.
Show resolved Hide resolved
public:
/// The type of validation.
enum class Type {
kLogicalLaneAdjacency,
kGeometricalLaneAdjacency,
};

/// Error struct that contains the error message, type, and severity.
struct Error {
/// The type of error.
enum class Type {
kLogicalLaneAdjacency,
kGeometricalLaneAdjacency,
};
/// The severity of the error.
enum class Severity {
kWarning,
Expand All @@ -77,36 +75,38 @@ class Validator {
/// Message describing the error.
std::string message;
/// The type of error.
Type type;
Validator::Type type;
/// The severity of the error.
Severity severity;
};

using Types = std::unordered_set<Validator::Type>;

/// Constructor for Validator.
/// During construction, the Validator will perform the validation checks.
////
/// @param parser The maliput_sparse::parser::Parser instance to validate.
/// @param options The maliput_sparse::parser::ValidatorOptions to use.
/// @param types The types of validation to perform.
/// @param config The maliput_sparse::parser::ValidatorConfig to use.
Validator(const Parser* parser, const ValidatorOptions& options, const ValidatorConfig& config);
Validator(const Parser* parser, const Types& types, const ValidatorConfig& config);

/// Returns the errors found during validation.
std::vector<Error> operator()() const;

private:
// Returns the types of validation that are dependent on the given type.
static const std::unordered_map<Type, std::unordered_set<Type>> kDependentTypes;

// The maliput_sparse::parser::Parser instance to validate.
const Parser* parser_{nullptr};
// The maliput_sparse::parser::ValidatorOptions to use.
const ValidatorOptions options_;
// The maliput_sparse::parser::ValidatorConfig to use.
const ValidatorConfig config_;
// The types of validation to perform.
Types types_;
};

/// Validates lane adjacency.
/// @param parser The maliput_sparse::parser::Parser instance to validate.
/// @param config The maliput_sparse::parser::ValidatorConfig to use.
/// @returns A vector of maliput_sparse::parser::Validator::Error.
std::vector<Validator::Error> ValidateLaneAdjacency(const Parser* parser, const ValidatorConfig config);
/// Serialize Validator::Type to ostream.
std::ostream& operator<<(std::ostream& os, const Validator::Type& type);

} // namespace parser
} // namespace maliput_sparse
10 changes: 6 additions & 4 deletions src/loader/road_geometry_loader.cc
Expand Up @@ -59,15 +59,17 @@ RoadGeometryLoader::RoadGeometryLoader(std::unique_ptr<parser::Parser> parser,

std::unique_ptr<const maliput::api::RoadGeometry> RoadGeometryLoader::operator()() {
// Validates the parsed data before building the RoadGeometry.
const auto errors = parser::Validator(parser_.get(), parser::ValidatorOptions{true},
parser::ValidatorConfig{builder_configuration_.linear_tolerance})();
const auto errors = parser::Validator(
parser_.get(),
{parser::Validator::Type::kLogicalLaneAdjacency, parser::Validator::Type::kGeometricalLaneAdjacency},
parser::ValidatorConfig{builder_configuration_.linear_tolerance})();
for (const auto& error : errors) {
switch (error.severity) {
case parser::Validator::Error::Severity::kError:
maliput::log()->error("{}", error.message);
maliput::log()->error("[{}] {}", error.type, error.message);
break;
case parser::Validator::Error::Severity::kWarning:
maliput::log()->warn("{}", error.message);
maliput::log()->warn("[{}] {}", error.type, error.message);
break;
default:
MALIPUT_THROW_MESSAGE("Unknown parser::Validator::Error::Severity value: " + static_cast<int>(error.severity));
Expand Down
49 changes: 49 additions & 0 deletions src/parser/validation_methods.h
@@ -0,0 +1,49 @@
// BSD 3-Clause License
//
// Copyright (c) 2022, Woven Planet.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once

#include <vector>

#include "maliput_sparse/parser/parser.h"
#include "maliput_sparse/parser/validator.h"

namespace maliput_sparse {
namespace parser {

/// Validates lane adjacency.
/// @param parser The maliput_sparse::parser::Parser instance to validate.
/// @param validate_geometric_adjacency Whether to validate geometric adjacency.
/// @param config The maliput_sparse::parser::ValidatorConfig to use.
/// @returns A vector of maliput_sparse::parser::Validator::Error.
std::vector<Validator::Error> ValidateLaneAdjacency(const Parser* parser, const bool& validate_geometric_adjacency,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: const bool& -> bool

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

const ValidatorConfig config);
francocipollone marked this conversation as resolved.
Show resolved Hide resolved

} // namespace parser
} // namespace maliput_sparse