#include #include #include #include #include #include #include namespace lane_route_core { void fresnelSeries(double x, double* s, double* c) { const double half_pi = kPi / 2.0; const double x2 = x * x; const double step = half_pi * half_pi * x2 * x2; double term = x; double sum = 0.0; for (int k = 0; k < 40; ++k) { const double add = term / (4 * k + 1); sum += add; if (std::abs(add) < 1e-19 && k > 1) break; term = -term * step / ((2 * k + 1) * (2 * k + 2)); } *c = sum; term = half_pi * x2 * x; sum = 0.0; for (int k = 0; k < 40; ++k) { const double add = term / (4 * k + 3); sum += add; if (std::abs(add) < 1e-19 && k > 1) break; term = -term * step / ((2 * k + 2) * (2 * k + 3)); } *s = sum; } Path clothoidCorner(double angle, double radius, double sharpness, double spacing) { const double a = std::abs(angle); if (!(a > 0.0 && a < kPi && radius > 0.0 && sharpness > 0.0 && spacing > 0.0)) throw std::invalid_argument("clothoidCorner: angle, radius, sharpness or spacing out of range"); using Complex = std::complex; const Complex unit(0.0, 1.0); const double spiral_length = std::min(1.0 / (radius * sharpness), std::sqrt(a / sharpness)); const double k = sharpness * spiral_length; const double arc_length = std::max(0.0, (a - sharpness * spiral_length * spiral_length) / k); const double length = 2.0 * spiral_length + arc_length; const double scale = std::sqrt(kPi / sharpness); const double to_argument = std::sqrt(sharpness / kPi); const auto spiral = [&](double s) { double fs = 0.0, fc = 0.0; fresnelSeries(s * to_argument, &fs, &fc); return Complex(scale * fc, scale * fs); }; const Complex z1 = spiral(spiral_length); const double h1 = sharpness * spiral_length * spiral_length / 2.0; const Complex za = (std::exp(unit * (h1 + k * arc_length)) - std::exp(unit * h1)) / (unit * k); // Uniform samples plus the phase boundaries, sorted and without near-duplicates. const int count = static_cast(std::ceil(length / spacing)) + 1; std::vector raw; raw.reserve(static_cast(count) + 2); const double uniform = count > 1 ? length / (count - 1) : 0.0; for (int i = 0; i < count; ++i) raw.push_back(i == count - 1 ? length : i * uniform); raw.push_back(spiral_length); raw.push_back(spiral_length + arc_length); std::sort(raw.begin(), raw.end()); raw.erase(std::unique(raw.begin(), raw.end()), raw.end()); std::vector samples; samples.reserve(raw.size()); for (std::size_t i = 0; i < raw.size(); ++i) if (i == 0 || raw[i] - raw[i - 1] > 1e-8) samples.push_back(raw[i]); if (length - samples.back() > 1e-8) samples.push_back(length); else samples.back() = length; const double sign = angle > 0.0 ? 1.0 : -1.0; Path output; output.reserve(samples.size()); for (double s : samples) { Complex z; double heading, curvature; if (s <= spiral_length) { z = spiral(s); heading = sharpness * s * s / 2.0; curvature = sharpness * s; } else if (s <= spiral_length + arc_length) { const double t = s - spiral_length; z = z1 + (std::exp(unit * (h1 + k * t)) - std::exp(unit * h1)) / (unit * k); heading = h1 + k * t; curvature = k; } else { const double t = s - spiral_length - arc_length; const double rest = spiral_length - t; z = z1 + za + std::exp(unit * a) * std::conj(z1 - spiral(rest)); heading = a - sharpness * rest * rest / 2.0; curvature = sharpness * rest; } output.push_back({ z.real(), sign * z.imag(), sign * heading, sign * curvature }); } return output; } double tangentTrim(double angle, const Path& curve) { return curve.back().y / std::sin(angle); } Path placedCorner(const Point2& a, const Point2& b, const Point2& c, double radius, double sharpness, double spacing, double* trim) { const double h0 = headingOf(a, b); const double angle = wrapAngle(headingOf(b, c) - h0); if (std::abs(angle) < 1e-8 || std::abs(angle) >= kPi - 1e-5) return Path(); Path curve = clothoidCorner(angle, radius, sharpness, spacing); *trim = tangentTrim(angle, curve); const double co = std::cos(h0); const double si = std::sin(h0); const double entry_x = b.x - *trim * co; const double entry_y = b.y - *trim * si; for (PathPoint& point : curve) { const double x = point.x; const double y = point.y; point.x = entry_x + co * x - si * y; point.y = entry_y + si * x + co * y; point.yaw += h0; } return curve; } } // namespace lane_route_core