Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Give better error message on nonconformant constraint type #213

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions include/trajopt/constraint/Constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#pragma once

#include <utility>
#include <concepts>
#include <type_traits>
#include <variant>

#include <sleipnir/autodiff/Variable.hpp>
Expand All @@ -25,24 +26,37 @@ namespace trajopt {

/**
* ConstraintType concept.
*
* To make TrajoptLib support a new constraint type, do the following in this
* file:
*
* 1. Include the type's header file
* 2. Add a constraint static assert for the type
* 3. Add the type to Constraint's std::variant type list
*/
template <typename T>
concept ConstraintType = requires(T t) {
{
// Parameters:
//
// sleipnir::OptimizationProblem& problem
// const Pose2v& pose
// const Translation2v& linearVelocity
// const sleipnir::Variable& angularVelocity
// const Translation2v& linearAcceleration
// const sleipnir::Variable& angularAcceleration
t.Apply(std::declval<sleipnir::OptimizationProblem>(),
std::declval<Pose2v>(), std::declval<Translation2v>(),
std::declval<sleipnir::Variable>(), std::declval<Translation2v>(),
std::declval<sleipnir::Variable>())
}; // NOLINT(readability/braces)
};
concept ConstraintType =
requires(T self, sleipnir::OptimizationProblem& problem, const Pose2v& pose,
const Translation2v& linearVelocity,
const sleipnir::Variable& angularVelocity,
const Translation2v& linearAcceleration,
const sleipnir::Variable& angularAcceleration) {
{
self.Apply(problem, pose, linearVelocity, angularVelocity,
linearAcceleration, angularAcceleration)
} -> std::same_as<void>;
};

static_assert(ConstraintType<AngularVelocityEqualityConstraint>);
static_assert(ConstraintType<AngularVelocityMaxMagnitudeConstraint>);
static_assert(ConstraintType<LinePointConstraint>);
static_assert(ConstraintType<LinearVelocityDirectionConstraint>);
static_assert(ConstraintType<LinearVelocityMaxMagnitudeConstraint>);
static_assert(ConstraintType<PointAtConstraint>);
static_assert(ConstraintType<PointLineConstraint>);
static_assert(ConstraintType<PointPointConstraint>);
static_assert(ConstraintType<PoseEqualityConstraint>);
static_assert(ConstraintType<TranslationEqualityConstraint>);

using Constraint =
std::variant<AngularVelocityEqualityConstraint,
Expand Down
Loading