Use other levels of asserts through code
This commit is contained in:
parent
75af0855f4
commit
ebddbb6f39
|
@ -49,7 +49,7 @@ namespace geom {
|
||||||
|
|
||||||
Vector2D MakeUnitVector() const {
|
Vector2D MakeUnitVector() const {
|
||||||
const float len = Length();
|
const float len = Length();
|
||||||
DEBUG_ASSERT(len > 2.0f * std::numeric_limits<float>::epsilon());
|
DEVELOPMENT_ASSERT(len > 2.0f * std::numeric_limits<float>::epsilon());
|
||||||
const float k = 1.0f / len;
|
const float k = 1.0f / len;
|
||||||
return Vector2D(x * k, y * k);
|
return Vector2D(x * k, y * k);
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ namespace geom {
|
||||||
|
|
||||||
Vector3D MakeUnitVector() const {
|
Vector3D MakeUnitVector() const {
|
||||||
const float length = Length();
|
const float length = Length();
|
||||||
DEBUG_ASSERT(length > 2.0f * std::numeric_limits<float>::epsilon());
|
DEVELOPMENT_ASSERT(length > 2.0f * std::numeric_limits<float>::epsilon());
|
||||||
const float k = 1.0f / length;
|
const float k = 1.0f / length;
|
||||||
return Vector3D(x * k, y * k, z * k);
|
return Vector3D(x * k, y * k, z * k);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,16 +25,6 @@ namespace road {
|
||||||
/// sections to avoid floating point precision errors.
|
/// sections to avoid floating point precision errors.
|
||||||
static constexpr double EPSILON = 10.0 * std::numeric_limits<double>::epsilon();
|
static constexpr double EPSILON = 10.0 * std::numeric_limits<double>::epsilon();
|
||||||
|
|
||||||
// ===========================================================================
|
|
||||||
// -- Error handling ---------------------------------------------------------
|
|
||||||
// ===========================================================================
|
|
||||||
|
|
||||||
[[ noreturn ]] static void throw_invalid_input(const char *message) {
|
|
||||||
throw_exception(std::invalid_argument(message));
|
|
||||||
}
|
|
||||||
|
|
||||||
#define THROW_INVALID_INPUT_ASSERT(pred) if (!(pred)) { throw_invalid_input("assert failed: " #pred); }
|
|
||||||
|
|
||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
// -- Static local methods ---------------------------------------------------
|
// -- Static local methods ---------------------------------------------------
|
||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
|
@ -114,7 +104,7 @@ namespace road {
|
||||||
double tangent = 0.0;
|
double tangent = 0.0;
|
||||||
for (const auto &lane : container) {
|
for (const auto &lane : container) {
|
||||||
auto info = lane.second.template GetInfo<RoadInfoLaneWidth>(s);
|
auto info = lane.second.template GetInfo<RoadInfoLaneWidth>(s);
|
||||||
THROW_INVALID_INPUT_ASSERT(info != nullptr);
|
RELEASE_ASSERT(info != nullptr);
|
||||||
const auto current_polynomial = info->GetPolynomial();
|
const auto current_polynomial = info->GetPolynomial();
|
||||||
auto current_dist = current_polynomial.Evaluate(s);
|
auto current_dist = current_polynomial.Evaluate(s);
|
||||||
auto current_tang = current_polynomial.Tangent(s);
|
auto current_tang = current_polynomial.Tangent(s);
|
||||||
|
@ -213,7 +203,7 @@ namespace road {
|
||||||
auto &lane = road.GetLaneByDistance(waypoint.s, waypoint.lane_id);
|
auto &lane = road.GetLaneByDistance(waypoint.s, waypoint.lane_id);
|
||||||
|
|
||||||
const auto lane_section = lane.GetLaneSection();
|
const auto lane_section = lane.GetLaneSection();
|
||||||
THROW_INVALID_INPUT_ASSERT(lane_section != nullptr);
|
RELEASE_ASSERT(lane_section != nullptr);
|
||||||
const auto lane_section_id = lane_section->GetId();
|
const auto lane_section_id = lane_section->GetId();
|
||||||
waypoint.section_id = lane_section_id;
|
waypoint.section_id = lane_section_id;
|
||||||
|
|
||||||
|
@ -243,21 +233,21 @@ namespace road {
|
||||||
|
|
||||||
geom::Transform Map::ComputeTransform(Waypoint waypoint) const {
|
geom::Transform Map::ComputeTransform(Waypoint waypoint) const {
|
||||||
// lane_id can't be 0
|
// lane_id can't be 0
|
||||||
THROW_INVALID_INPUT_ASSERT(waypoint.lane_id != 0);
|
RELEASE_ASSERT(waypoint.lane_id != 0);
|
||||||
|
|
||||||
const auto &road = _data.GetRoad(waypoint.road_id);
|
const auto &road = _data.GetRoad(waypoint.road_id);
|
||||||
|
|
||||||
// must s be smaller (or eq) than road lenght and bigger (or eq) than 0?
|
// must s be smaller (or eq) than road lenght and bigger (or eq) than 0?
|
||||||
THROW_INVALID_INPUT_ASSERT(waypoint.s <= road.GetLength());
|
RELEASE_ASSERT(waypoint.s <= road.GetLength());
|
||||||
THROW_INVALID_INPUT_ASSERT(waypoint.s >= 0.0);
|
RELEASE_ASSERT(waypoint.s >= 0.0);
|
||||||
|
|
||||||
const auto &lane_section = road.GetLaneSectionById(waypoint.section_id);
|
const auto &lane_section = road.GetLaneSectionById(waypoint.section_id);
|
||||||
const std::map<LaneId, Lane> &lanes = lane_section.GetLanes();
|
const std::map<LaneId, Lane> &lanes = lane_section.GetLanes();
|
||||||
|
|
||||||
// check that lane_id exists on the current s
|
// check that lane_id exists on the current s
|
||||||
THROW_INVALID_INPUT_ASSERT(!lanes.empty());
|
RELEASE_ASSERT(!lanes.empty());
|
||||||
THROW_INVALID_INPUT_ASSERT(waypoint.lane_id >= lanes.begin()->first);
|
RELEASE_ASSERT(waypoint.lane_id >= lanes.begin()->first);
|
||||||
THROW_INVALID_INPUT_ASSERT(waypoint.lane_id <= lanes.rbegin()->first);
|
RELEASE_ASSERT(waypoint.lane_id <= lanes.rbegin()->first);
|
||||||
|
|
||||||
double lane_width = 0;
|
double lane_width = 0;
|
||||||
double lane_tangent = 0;
|
double lane_tangent = 0;
|
||||||
|
@ -322,11 +312,11 @@ namespace road {
|
||||||
const auto s = waypoint.s;
|
const auto s = waypoint.s;
|
||||||
|
|
||||||
const auto &lane = GetLane(waypoint);
|
const auto &lane = GetLane(waypoint);
|
||||||
THROW_INVALID_INPUT_ASSERT(lane.GetRoad() != nullptr);
|
RELEASE_ASSERT(lane.GetRoad() != nullptr);
|
||||||
THROW_INVALID_INPUT_ASSERT(s <= lane.GetRoad()->GetLength());
|
RELEASE_ASSERT(s <= lane.GetRoad()->GetLength());
|
||||||
|
|
||||||
const auto lane_width_info = lane.GetInfo<RoadInfoLaneWidth>(s);
|
const auto lane_width_info = lane.GetInfo<RoadInfoLaneWidth>(s);
|
||||||
THROW_INVALID_INPUT_ASSERT(lane_width_info != nullptr);
|
RELEASE_ASSERT(lane_width_info != nullptr);
|
||||||
|
|
||||||
return lane_width_info->GetPolynomial().Evaluate(s);
|
return lane_width_info->GetPolynomial().Evaluate(s);
|
||||||
}
|
}
|
||||||
|
@ -344,8 +334,8 @@ namespace road {
|
||||||
const auto s = waypoint.s;
|
const auto s = waypoint.s;
|
||||||
|
|
||||||
const auto ¤t_lane = GetLane(waypoint);
|
const auto ¤t_lane = GetLane(waypoint);
|
||||||
THROW_INVALID_INPUT_ASSERT(current_lane.GetRoad() != nullptr);
|
RELEASE_ASSERT(current_lane.GetRoad() != nullptr);
|
||||||
THROW_INVALID_INPUT_ASSERT(s <= current_lane.GetRoad()->GetLength());
|
RELEASE_ASSERT(s <= current_lane.GetRoad()->GetLength());
|
||||||
|
|
||||||
const auto inner_lane_id = waypoint.lane_id < 0 ?
|
const auto inner_lane_id = waypoint.lane_id < 0 ?
|
||||||
waypoint.lane_id + 1 :
|
waypoint.lane_id + 1 :
|
||||||
|
@ -374,13 +364,13 @@ namespace road {
|
||||||
std::vector<Waypoint> result;
|
std::vector<Waypoint> result;
|
||||||
result.reserve(next_lanes.size());
|
result.reserve(next_lanes.size());
|
||||||
for (auto *next_lane : next_lanes) {
|
for (auto *next_lane : next_lanes) {
|
||||||
THROW_INVALID_INPUT_ASSERT(next_lane != nullptr);
|
RELEASE_ASSERT(next_lane != nullptr);
|
||||||
const auto lane_id = next_lane->GetId();
|
const auto lane_id = next_lane->GetId();
|
||||||
THROW_INVALID_INPUT_ASSERT(lane_id != 0);
|
RELEASE_ASSERT(lane_id != 0);
|
||||||
const auto *section = next_lane->GetLaneSection();
|
const auto *section = next_lane->GetLaneSection();
|
||||||
THROW_INVALID_INPUT_ASSERT(section != nullptr);
|
RELEASE_ASSERT(section != nullptr);
|
||||||
const auto *road = next_lane->GetRoad();
|
const auto *road = next_lane->GetRoad();
|
||||||
THROW_INVALID_INPUT_ASSERT(road != nullptr);
|
RELEASE_ASSERT(road != nullptr);
|
||||||
const auto distance = GetDistanceAtStartOfLane(*next_lane);
|
const auto distance = GetDistanceAtStartOfLane(*next_lane);
|
||||||
result.emplace_back(Waypoint{road->GetId(), section->GetId(), lane_id, distance});
|
result.emplace_back(Waypoint{road->GetId(), section->GetId(), lane_id, distance});
|
||||||
}
|
}
|
||||||
|
@ -390,7 +380,7 @@ namespace road {
|
||||||
std::vector<Waypoint> Map::GetNext(
|
std::vector<Waypoint> Map::GetNext(
|
||||||
const Waypoint waypoint,
|
const Waypoint waypoint,
|
||||||
const double distance) const {
|
const double distance) const {
|
||||||
THROW_INVALID_INPUT_ASSERT(distance > 0.0);
|
RELEASE_ASSERT(distance > 0.0);
|
||||||
const auto &lane = GetLane(waypoint);
|
const auto &lane = GetLane(waypoint);
|
||||||
const bool forward = (waypoint.lane_id <= 0);
|
const bool forward = (waypoint.lane_id <= 0);
|
||||||
const double signed_distance = forward ? distance : -distance;
|
const double signed_distance = forward ? distance : -distance;
|
||||||
|
@ -404,7 +394,7 @@ namespace road {
|
||||||
Waypoint result = waypoint;
|
Waypoint result = waypoint;
|
||||||
result.s += signed_distance;
|
result.s += signed_distance;
|
||||||
result.s += forward ? -EPSILON : EPSILON;
|
result.s += forward ? -EPSILON : EPSILON;
|
||||||
THROW_INVALID_INPUT_ASSERT(result.s > 0.0);
|
RELEASE_ASSERT(result.s > 0.0);
|
||||||
return { result };
|
return { result };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,7 +411,7 @@ namespace road {
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::optional<Waypoint> Map::GetRight(Waypoint waypoint) const {
|
boost::optional<Waypoint> Map::GetRight(Waypoint waypoint) const {
|
||||||
THROW_INVALID_INPUT_ASSERT(waypoint.lane_id != 0);
|
RELEASE_ASSERT(waypoint.lane_id != 0);
|
||||||
if (waypoint.lane_id > 0) {
|
if (waypoint.lane_id > 0) {
|
||||||
++waypoint.lane_id;
|
++waypoint.lane_id;
|
||||||
} else {
|
} else {
|
||||||
|
@ -431,7 +421,7 @@ namespace road {
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::optional<Waypoint> Map::GetLeft(Waypoint waypoint) const {
|
boost::optional<Waypoint> Map::GetLeft(Waypoint waypoint) const {
|
||||||
THROW_INVALID_INPUT_ASSERT(waypoint.lane_id != 0);
|
RELEASE_ASSERT(waypoint.lane_id != 0);
|
||||||
if (std::abs(waypoint.lane_id) == 1) {
|
if (std::abs(waypoint.lane_id) == 1) {
|
||||||
waypoint.lane_id *= -1;
|
waypoint.lane_id *= -1;
|
||||||
} else if (waypoint.lane_id > 0) {
|
} else if (waypoint.lane_id > 0) {
|
||||||
|
@ -478,5 +468,3 @@ namespace road {
|
||||||
|
|
||||||
} // namespace road
|
} // namespace road
|
||||||
} // namespace carla
|
} // namespace carla
|
||||||
|
|
||||||
#undef THROW_INVALID_INPUT_ASSERT
|
|
||||||
|
|
|
@ -18,8 +18,13 @@ TEST(vector3D, make_unit_vec) {
|
||||||
ASSERT_EQ(Vector3D(0,10,0).MakeUnitVector(), Vector3D(0,1,0));
|
ASSERT_EQ(Vector3D(0,10,0).MakeUnitVector(), Vector3D(0,1,0));
|
||||||
ASSERT_EQ(Vector3D(0,0,512).MakeUnitVector(), Vector3D(0,0,1));
|
ASSERT_EQ(Vector3D(0,0,512).MakeUnitVector(), Vector3D(0,0,1));
|
||||||
ASSERT_NE(Vector3D(0,1,512).MakeUnitVector(), Vector3D(0,0,1));
|
ASSERT_NE(Vector3D(0,1,512).MakeUnitVector(), Vector3D(0,0,1));
|
||||||
#ifndef NDEBUG
|
#ifdef LIBCARLA_NO_EXCEPTIONS
|
||||||
ASSERT_DEATH_IF_SUPPORTED(
|
ASSERT_DEATH_IF_SUPPORTED(
|
||||||
Vector3D().MakeUnitVector(), "length > 2.0f \\* std::numeric_limits<float>::epsilon()");
|
Vector3D().MakeUnitVector(),
|
||||||
#endif // NDEBUG
|
"length > 2.0f \\* std::numeric_limits<float>::epsilon()");
|
||||||
|
#else
|
||||||
|
ASSERT_THROW(
|
||||||
|
Vector3D().MakeUnitVector(),
|
||||||
|
std::runtime_error);
|
||||||
|
#endif // LIBCARLA_NO_EXCEPTIONS
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue