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

Commit

Permalink
Replace set IsValid() functions with constructor assertions (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Jun 19, 2024
1 parent f9cee6f commit e8e2d06
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 68 deletions.
11 changes: 8 additions & 3 deletions include/trajopt/set/ConeSet2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <cassert>
#include <numbers>

#include "trajopt/SymbolExports.h"
Expand All @@ -17,10 +18,14 @@ struct TRAJOPT_DLLEXPORT ConeSet2d {
IntervalSet1d thetaBound;

/**
* Returns true if the set is valid.
* Constructs a ConeSet2d.
*
* @param thetaBound The internal angle of the cone tip. Must be within (0,
* π].
*/
constexpr bool IsValid() const noexcept {
return thetaBound.Range() > 0.0 && thetaBound.Range() <= std::numbers::pi;
explicit constexpr ConeSet2d(const IntervalSet1d& thetaBound)
: thetaBound{thetaBound} {
assert(thetaBound.Range() > 0.0 && thetaBound.Range() <= std::numbers::pi);
}
};

Expand Down
44 changes: 18 additions & 26 deletions include/trajopt/set/EllipticalSet2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#pragma once

#include <limits>
#include <cassert>

#include "trajopt/SymbolExports.h"

Expand All @@ -13,14 +13,14 @@ namespace trajopt {
*/
struct TRAJOPT_DLLEXPORT EllipticalSet2d {
/**
* FIXME What does this do?
* The directionality of the set.
*/
enum class Direction {
/// FIXME What does this do?
/// The set is every point inside the ellipse.
kInside,
/// FIXME What does this do?
/// The set is every point on the border of the ellipse.
kCentered,
/// FIXME What does this do?
/// The set is every point outside the ellipse.
kOutside
};

Expand All @@ -30,9 +30,21 @@ struct TRAJOPT_DLLEXPORT EllipticalSet2d {
/// The y radius.
double yRadius;

/// The direction.
/// The set direction.
Direction direction;

/**
* Construct an EllipticalSet2d.
*
* @param xRadius The ellipse's x radius. Must be greater than zero.
* @param yRadius The ellipse's y radius. Must be greater than zero.
* @param direction The set direction.
*/
constexpr EllipticalSet2d(double xRadius, double yRadius, Direction direction)
: xRadius{xRadius}, yRadius{yRadius}, direction{direction} {
assert(xRadius > 0.0 && yRadius > 0.0);
}

/**
* Construct a circular EllipticalSet2d from a radius.
*
Expand All @@ -43,26 +55,6 @@ struct TRAJOPT_DLLEXPORT EllipticalSet2d {
double radius, Direction direction = Direction::kInside) {
return EllipticalSet2d{radius, radius, direction};
}

/**
* Returns true if the ellipse is a circle.
*/
constexpr bool IsCircular() const noexcept { return xRadius == yRadius; }

/**
* Returns true if the set spans R².
*/
constexpr bool IsR2() const noexcept {
return xRadius >= std::numeric_limits<double>::infinity() &&
yRadius >= std::numeric_limits<double>::infinity();
}

/**
* Returns true if the set is valid.
*/
constexpr bool IsValid() const noexcept {
return xRadius > 0.0 && yRadius > 0.0;
}
};

} // namespace trajopt
36 changes: 10 additions & 26 deletions include/trajopt/set/IntervalSet1d.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <cassert>
#include <limits>

#include "trajopt/SymbolExports.h"
Expand All @@ -20,21 +21,23 @@ struct TRAJOPT_DLLEXPORT IntervalSet1d {
double upper;

/**
* Construct a Scalar Bound between a lower and upper bound.
* Construct a scalar bound between a lower and upper bound.
*
* @param lower The lower bound.
* @param upper The upper bound.
* @param lower The lower bound. Must be less than or equal to upper bound.
* @param upper The upper bound. Must be greater than or equal to lower bound.
*/
constexpr IntervalSet1d(double lower, double upper)
: lower(lower), upper(upper) {}
: lower(lower), upper(upper) {
assert(lower <= upper);
}

/**
* Construct a Scalar Bound that represents the interval [value, value].
* Construct a scalar bound that represents the interval [value, value].
*
* @param value the value to bound the number between.
*/
constexpr IntervalSet1d(double value)
: lower(value), upper(value) {} // NOLINT
constexpr IntervalSet1d(double value) // NOLINT
: IntervalSet1d{value, value} {}

constexpr IntervalSet1d() = default;

Expand Down Expand Up @@ -96,16 +99,6 @@ struct TRAJOPT_DLLEXPORT IntervalSet1d {
*/
constexpr bool IsExact() const noexcept { return lower == upper; }

/**
* Check if this scalar bound only contains 0. This occurs when
* this scalar bound equals 0.0.
*
* @return lower == 0.0 && upper == 0.0
*/
constexpr bool IsZero() const noexcept {
return lower == 0.0 && upper == 0.0;
}

/**
* Returns true if this IntervalSet1d has a lower bound.
*/
Expand All @@ -119,15 +112,6 @@ struct TRAJOPT_DLLEXPORT IntervalSet1d {
constexpr bool IsUpperBounded() const noexcept {
return upper < +std::numeric_limits<double>::infinity();
}

/**
* Check if this scalar bound is valid. A scalar bound is valid
* if and only if the lower bound is less than or equal to the upper
* bound.
*
* @return lower <= upper
*/
constexpr bool IsValid() const noexcept { return lower <= upper; }
};

} // namespace trajopt
14 changes: 1 addition & 13 deletions include/trajopt/set/RectangularSet2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct TRAJOPT_DLLEXPORT RectangularSet2d {
/**
* Construct a RectangularSet2d from polar coordinates.
*
* @param r The distance.
* @param r The distance. Must be greater than zero.
* @param theta The heading.
*/
static RectangularSet2d PolarExactSet2d(double r, double theta) {
Expand All @@ -35,18 +35,6 @@ struct TRAJOPT_DLLEXPORT RectangularSet2d {
static constexpr RectangularSet2d R2() {
return RectangularSet2d{IntervalSet1d::R1(), IntervalSet1d::R1()};
}

/**
* @brief Check if this planar bound is valid. A planar bound is valid when
* the bounds on a0 and a1 are valid, and additionally for planar bounds, a0
* is contained within the interval [0, inf] and a1 is contained within the
* interval [-pi, pi].
*
* @return true if and only if this planar bound is valid
*/
constexpr bool IsValid() const noexcept {
return xBound.IsValid() && yBound.IsValid();
}
};

} // namespace trajopt

0 comments on commit e8e2d06

Please sign in to comment.