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 all 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
17 changes: 17 additions & 0 deletions include/maliput_sparse/geometry/utility/geometry.h
Expand Up @@ -120,6 +120,23 @@ ClosestPointResult GetClosestPoint(const std::pair<maliput::math::Vector3, malip
/// and the p coordinate in the LineString3d matching the closest point.
ClosestPointResult GetClosestPoint(const LineString3d& line_string, const maliput::math::Vector3& xyz);

/// Computes the distance between two LineString3d.
/// The distance is calculated as the sum of distances between corresponding points between both line strings divided by
/// the number of points. Some notes: 1 - The evaluation points to be used are the ones from the line string with more
/// points. 2 - If the line strings have the same number of points, the evaluation points are the ones from the first
/// line string. 3 - The closest point is calculated for each point in the line string with less points using #ref
/// GetClosestPoint method.
///
/// This algorithm is not the most precise one, but it is the most intuitive one and for the expected use cases it is
/// good enough.
///
/// TODO(#CreateIssue): Use Frechet distance algorithm instead.
///
/// @param lhs A LineString3d.
/// @param rhs A LineString3d.
/// @return The distance between the two line strings defined as above.
double ComputeDistance(const LineString3d& lhs, const LineString3d& rhs);

} // namespace utility
} // namespace geometry
} // namespace maliput_sparse
112 changes: 112 additions & 0 deletions include/maliput_sparse/parser/validator.h
@@ -0,0 +1,112 @@
// 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 <ostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include "maliput_sparse/parser/parser.h"

namespace maliput_sparse {
namespace parser {

/// ValidatorConfig struct that contains the configuration for the Validator.
struct ValidatorConfig {
double linear_tolerance{1e-12};
};

/// After parsing a road network, the Validator can be used to check for errors before
/// creating a maliput::api::RoadNetwork via the maliput_sparse::loader::RoadNetworkLoader.
/// The Validator can be configured to check for different types of errors and provide an interface to retrieve
/// the errors found.
/// The errors are stored in a vector of Error structs. The Error struct contains a message, the type of error, and the
/// 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 severity of the error.
enum class Severity {
kWarning,
kError,
};

/// Equality operator for Error.
bool operator==(const Error& other) const;
/// Inequality operator for Error.
bool operator!=(const Error& other) const;

/// Message describing the error.
std::string message;
/// The type of error.
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 types The types of validation to perform.
/// @param config The maliput_sparse::parser::ValidatorConfig to use.
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::ValidatorConfig to use.
const ValidatorConfig config_;
// The types of validation to perform.
Types types_;
};

/// Serialize Validator::Type to ostream.
std::ostream& operator<<(std::ostream& os, const Validator::Type& type);

} // namespace parser
} // namespace maliput_sparse
12 changes: 12 additions & 0 deletions src/geometry/utility/geometry.cc
Expand Up @@ -60,6 +60,7 @@

#include <algorithm>
#include <cmath>
#include <numeric>

namespace maliput_sparse {
namespace geometry {
Expand Down Expand Up @@ -374,6 +375,17 @@ ClosestPointResult GetClosestPoint(const LineString3d& line_string, const malipu
return result;
}

double ComputeDistance(const LineString3d& lhs, const LineString3d& rhs) {
const LineString3d& base_ls = rhs.size() > lhs.size() ? rhs : lhs;
const LineString3d& other_ls = rhs.size() > lhs.size() ? lhs : rhs;
const double sum_distances =
std::accumulate(base_ls.begin(), base_ls.end(), 0.0, [&other_ls](double sum, const auto& base_point) {
const auto closest_point_res = GetClosestPoint(other_ls, base_point);
return sum + closest_point_res.distance;
});
return sum_distances / static_cast<double>(base_ls.size());
}

} // namespace utility
} // namespace geometry
} // namespace maliput_sparse
29 changes: 29 additions & 0 deletions src/loader/road_geometry_loader.cc
Expand Up @@ -29,7 +29,10 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "maliput_sparse/loader/road_geometry_loader.h"

#include <maliput/common/logger.h>

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

namespace maliput_sparse {
namespace loader {
Expand All @@ -55,6 +58,32 @@ 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::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.type, error.message);
break;
case parser::Validator::Error::Severity::kWarning:
maliput::log()->warn("[{}] {}", error.type, error.message);
break;
default:
MALIPUT_THROW_MESSAGE("Unknown parser::Validator::Error::Severity value: " + static_cast<int>(error.severity));
}
}

if (std::find_if(errors.begin(), errors.end(), [](const parser::Validator::Error& error) {
return error.severity == parser::Validator::Error::Severity::kError;
}) != errors.end()) {
MALIPUT_THROW_MESSAGE("Errors(" + std::to_string(static_cast<int>(errors.size())) +
") found during validation. Aborting.");
}

// Builds the RoadGeometry.
const std::unordered_map<parser::Junction::Id, parser::Junction>& junctions = parser_->GetJunctions();
const std::vector<parser::Connection>& connections = parser_->GetConnections();

Expand Down
2 changes: 2 additions & 0 deletions src/parser/CMakeLists.txt
Expand Up @@ -4,6 +4,7 @@
add_library(parser
connection.cc
lane.cc
validator.cc
)

add_library(maliput_sparse::parser ALIAS parser)
Expand All @@ -23,6 +24,7 @@ target_include_directories(
target_link_libraries(parser
PRIVATE
maliput::common
maliput_sparse::geometry
)

install(TARGETS parser
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, bool validate_geometric_adjacency,
const ValidatorConfig& config);

} // namespace parser
} // namespace maliput_sparse