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

Commit

Permalink
Give better error message on nonconformant constraint type (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Jun 25, 2024
1 parent aea70fe commit 80b6b4b
Showing 1 changed file with 31 additions and 17 deletions.
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

0 comments on commit 80b6b4b

Please sign in to comment.