7#include "polynomial.hpp"
8#include "rapid/numeric_array.hpp"
9#include "geometry/point.hpp"
13namespace hi::inline
v1 {
26 return {P1 - C * 2 + P2, (C - P1) * 2, P1};
31constexpr std::array<T, 4> bezierToPolynomial(T P1, T C1, T C2, T P2)
noexcept
33 return {-P1 + C1 * 3 - C2 * 3 + P2, P1 * 3 - C1 * 6 + C2 * 3, P1 * -3 + C1 * 3, P1};
37constexpr geo::point<D> bezierPointAt(geo::point<D> P1, geo::point<D> P2,
float t)
noexcept
39 hilet[a, b] = bezierToPolynomial(
static_cast<f32x4
>(P1),
static_cast<f32x4
>(P2));
40 return geo::point<D>{a * t + b};
44constexpr geo::point<D> bezierPointAt(geo::point<D> P1, geo::point<D> C, geo::point<D> P2,
float t)
noexcept
46 hilet[a, b,
c] = bezierToPolynomial(
static_cast<f32x4
>(P1),
static_cast<f32x4
>(C),
static_cast<f32x4
>(P2));
47 return geo::point<D>{a * t * t + b * t +
c};
51constexpr geo::point<D> bezierPointAt(geo::point<D> P1, geo::point<D> C1, geo::point<D> C2, geo::point<D> P2,
float t)
noexcept
54 bezierToPolynomial(
static_cast<f32x4
>(P1),
static_cast<f32x4
>(C1),
static_cast<f32x4
>(C2),
static_cast<f32x4
>(P2));
55 return geo::point<D>{a * t * t * t + b * t * t +
c * t + d};
59constexpr geo::vector<D> bezierTangentAt(geo::point<D> P1, geo::point<D> P2,
float t)
noexcept
65constexpr geo::vector<D> bezierTangentAt(geo::point<D> P1, geo::point<D> C, geo::point<D> P2,
float t)
noexcept
67 hilet P1_ =
static_cast<f32x4
>(P1);
68 hilet C_ =
static_cast<f32x4
>(C);
69 hilet P2_ =
static_cast<f32x4
>(P2);
71 return geo::vector<D>{2 * t * (P2_ - 2 * C_ + P1_) + 2 * (C_ - P1_)};
75constexpr geo::vector<D> bezierTangentAt(geo::point<D> P1, geo::point<D> C1, geo::point<D> C2, geo::point<D> P2,
float t)
noexcept
77 hilet P1_ =
static_cast<f32x4
>(P1);
78 hilet C1_ =
static_cast<f32x4
>(C1);
79 hilet C2_ =
static_cast<f32x4
>(C2);
80 hilet P2_ =
static_cast<f32x4
>(P2);
82 return geo::vector<D>{3 * t * t * (P2_ - 3 * C2_ + 3 * C1_ - P1_) + 6 * t * (C2_ - 2 * C1_ + P1_) + 3 * (C1_ - P1_)};
85constexpr results<float, 1> bezierFindT(
float P1,
float P2,
float x)
noexcept
87 hilet[a, b] = bezierToPolynomial(P1, P2);
91constexpr results<float, 2> bezierFindT(
float P1,
float C,
float P2,
float x)
noexcept
93 hilet[a, b,
c] = bezierToPolynomial(P1, C, P2);
97hi_force_inline
constexpr results<float, 3> bezierFindT(
float P1,
float C1,
float C2,
float P2,
float x)
noexcept
99 hilet[a, b,
c, d] = bezierToPolynomial(P1, C1, C2, P2);
109 hilet t_above = dot(P - P1, P2 - P1);
110 hilet t_below = dot(P2 - P1, P2 - P1);
111 if (t_below == 0.0) {
112 [[unlikely]]
return {};
114 return {t_above / t_below};
122hi_force_inline
constexpr results<float, 3>
133 hilet a = dot(p2, p2);
134 hilet b = 3 * dot(p1, p2);
135 hilet c = dot(2 * p1, p1) - dot(p2, p);
136 hilet d = -dot(p1, p);
154 for (
hilet t : bezierFindT(P1.y(), P2.y(), y)) {
155 if (t >= 0.0f && t < 1.0f) {
156 r.add(bezierPointAt(P1, P2, t).x());
174 if (y <
std::min({P1.y(), C.y(), P2.y()}) || y >
std::max({P1.y(), C.y(), P2.y()})) {
178 for (
hilet t : bezierFindT(P1.y(), C.y(), P2.y(), y)) {
179 if (t >= 0.0f && t <= 1.0f) {
180 r.add(bezierPointAt(P1, C, P2, t).x());
198 if (y <
std::min({P1.y(), C1.y(), C2.y(), P2.y()}) || y >
std::max({P1.y(), C1.y(), C2.y(), P2.y()})) {
202 for (
hilet t : bezierFindT(P1.y(), C1.y(), C2.y(), P2.y(), y)) {
203 if (t >= 0.0f && t <= 1.0f) {
204 r.add(bezierPointAt(P1, C1, C2, P2, t).x());
225 hilet P1P2 = hypot(P2 - P1);
230 hilet P1C1 = hypot(C - P1);
231 hilet C1P2 = hypot(P2 - C);
232 return P1P2 / (P1C1 + C1P2);
241 hilet P1P2 = hypot(P2 - P1);
246 hilet P1C1 = hypot(C1 - P1);
247 hilet C1C2 = hypot(C2 - C1);
248 hilet C2P2 = hypot(P2 - C2);
249 return P1P2 / (P1C1 + C1C2 + C2P2);
256 return {P1 + n * distance, P2 + n * distance};
271 hilet crossRS = cross(r, s);
272 if (crossRS == 0.0f) {
277 hilet q_min_p = q - p;
278 hilet t = cross(q_min_p, s) / crossRS;
279 hilet u = cross(q_min_p, r) / crossRS;
281 if (t >= 0.0f && t <= 1.0f && u >= 0.0f && u <= 1.0f) {
282 return bezierPointAt(A1, A2, t);
302 hilet crossRS = cross(r, s);
303 if (crossRS == 0.0f) {
308 hilet q_min_p = q - p;
309 hilet t = cross(q_min_p, s) / crossRS;
311 return bezierPointAt(A1, A2, t);
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:15
constexpr results< float, 1 > bezierFindX(point2 P1, point2 P2, float y) noexcept
Definition bezier.hpp:147
std::optional< point2 > getExtrapolatedIntersectionPoint(point2 A1, point2 A2, point2 B1, point2 B2) noexcept
Definition bezier.hpp:292
float bezierFlatness(point2 P1, point2 P2) noexcept
Definition bezier.hpp:214
hi_force_inline constexpr results< float, 1 > bezierFindTForNormalsIntersectingPoint(point2 P1, point2 P2, point2 P) noexcept
Find t on the line P1->P2 which is closest to P.
Definition bezier.hpp:107
std::optional< point2 > getIntersectionPoint(point2 A1, point2 A2, point2 B1, point2 B2) noexcept
Definition bezier.hpp:261
hi_force_inline constexpr results< T, 1 > solvePolynomial(T const &a, T const &b) noexcept
Definition polynomial.hpp:141
Definition polynomial.hpp:14