6#include "TTauri/Foundation/SDF8.hpp"
7#include "TTauri/Foundation/PixelMap.hpp"
8#include "TTauri/Foundation/attributes.hpp"
9#include "TTauri/Foundation/math.hpp"
10#include "TTauri/Foundation/bezier.hpp"
11#include "TTauri/Foundation/required.hpp"
12#include "TTauri/Foundation/vec.hpp"
13#include "TTauri/Foundation/mat.hpp"
26 enum class Type : uint8_t { None, Linear, Quadratic, Cubic };
27 enum class Color : uint8_t { Yellow, Magenta, Cyan, White };
45 type(Type::Linear), color(color),
P1(
P1),
C1(),
C2(),
P2(
P2) {
46 tt_assume(
P1.is_point() &&
P2.is_point());
52 type(Type::Quadratic), color(color),
P1(
P1),
C1(
C1),
C2(),
P2(
P2) {
53 tt_assume(
P1.is_point() &&
C1.is_point() &&
P2.is_point());
60 tt_assume(
P1.is_point() &&
C1.is_point() &&
C2.is_point() &&
P2.is_point());
69 tt_assume(
P1.is_point() &&
P2.is_point());
73 tt_assume(
P1.is_point() &&
C1.is_point() &&
P2.is_point());
77 tt_assume(
P1.is_point() &&
C1.is_point() &&
C2.is_point() &&
P2.is_point());
86 [[nodiscard]]
bool has_red() const noexcept {
87 return color != Color::Cyan;
90 [[nodiscard]]
bool has_green() const noexcept {
91 return color != Color::Magenta;
94 [[nodiscard]]
bool has_blue() const noexcept {
95 return color != Color::Yellow;
107 case Type::Linear:
return bezierPointAt(
P1,
P2, t);
108 case Type::Quadratic:
return bezierPointAt(
P1,
C1,
P2, t);
109 case Type::Cubic:
return bezierPointAt(
P1,
C1,
C2,
P2, t);
110 default: tt_no_default;
123 case Type::Linear:
return bezierTangentAt(
P1,
P2, t);
124 case Type::Quadratic:
return bezierTangentAt(
P1,
C1,
P2, t);
125 case Type::Cubic:
return bezierTangentAt(
P1,
C1,
C2,
P2, t);
126 default: tt_no_default;
136 case Type::Linear:
return bezierFindX(
P1,
P2, y);
137 case Type::Quadratic:
return bezierFindX(
P1,
C1,
P2, y);
138 case Type::Cubic:
return bezierFindX(
P1,
C1,
C2,
P2, y);
139 default: tt_no_default;
145 case Type::Linear:
return bezierFindTForNormalsIntersectingPoint(
P1,
P2, P);
146 case Type::Quadratic:
return bezierFindTForNormalsIntersectingPoint(
P1,
C1,
P2, P);
147 case Type::Cubic: tt_no_default;
148 default: tt_no_default;
157 auto min_normal =
vec{0.0f, 1.0f};
159 ttlet ts = solveTForNormalsIntersectingPoint(P);
161 t = std::clamp(t, 0.0f, 1.0f);
164 ttlet square_distance = length_squared(normal);
165 if (square_distance < min_square_distance) {
166 min_square_distance = square_distance;
173 ttlet distance =
std::sqrt(min_square_distance);
174 ttlet sdistance = viktor_cross(tangent, min_normal) < 0.0 ? distance : -distance;
193 return {{
P1, outerA.pointAt(t), innerA.pointAt(t), newPoint }, { newPoint, innerB.pointAt(t), outerB.pointAt(t),
P2 }};
207 return {{
P1, outerA.pointAt(t), newPoint }, { newPoint, outerB.pointAt(t),
P2 }};
218 return {{
P1, newPoint }, { newPoint,
P2 }};
231 default: tt_no_default;
240 if (
flatness() >= minimumFlatness) {
243 ttlet [a, b] =
split(0.5f);
244 a.subdivideUntilFlat_impl(r, minimumFlatness);
245 b.subdivideUntilFlat_impl(r, minimumFlatness);
264 case Type::Linear:
return bezierFlatness(
P1,
P2);
265 case Type::Quadratic:
return bezierFlatness(
P1,
C1,
P2);
266 case Type::Cubic:
return bezierFlatness(
P1,
C1,
C2,
P2);
267 default: tt_no_default;
271 template<
typename M, std::enable_if_t<is_mat_v<M>,
int> = 0>
285 auto [newP1, newP2] = parrallelLine(
P1,
P2, offset);
286 return { newP1, newP2 };
290 if (lhs.type != rhs.type) {
294 case BezierCurve::Type::Linear:
295 return (lhs.P1 == rhs.P1) && (lhs.P2 == rhs.P2);
296 case BezierCurve::Type::Quadratic:
297 return (lhs.P1 == rhs.P1) && (lhs.C1 == rhs.C1) && (lhs.P2 == rhs.P2);
298 case BezierCurve::Type::Cubic:
299 return (lhs.P1 == rhs.P1) && (lhs.C1 == rhs.C1) && (lhs.C2 == rhs.C2) && (lhs.P2 == rhs.P2);
305 template<
typename M, std::enable_if_t<is_mat_v<M>,
int> = 0>
306 [[nodiscard]]
friend BezierCurve operator*(M
const &lhs, BezierCurve
const &rhs)
noexcept {
319 return { rhs.type, rhs.P2, rhs.C2, rhs.C1, rhs.P1 };
331 std::vector<BezierPoint>::const_iterator first,
332 std::vector<BezierPoint>::const_iterator last)
noexcept;
355 LineJoinStyle lineJoinStyle,
356 float tolerance)
noexcept;
Definition BezierCurve.hpp:25
vec P2
Last point.
Definition BezierCurve.hpp:34
results< float, 3 > solveXByY(float const y) const noexcept
Definition BezierCurve.hpp:134
vec tangentAt(float const t) const noexcept
Definition BezierCurve.hpp:121
BezierCurve toParrallelLine(float const offset) const noexcept
Definition BezierCurve.hpp:284
vec C1
Control point.
Definition BezierCurve.hpp:32
std::pair< BezierCurve, BezierCurve > cubicSplit(float const t) const noexcept
Definition BezierCurve.hpp:183
vec C2
Control point.
Definition BezierCurve.hpp:33
float flatness() const noexcept
Definition BezierCurve.hpp:262
float sdf_distance(vec P) const noexcept
Find the distance from the point to the curve.
Definition BezierCurve.hpp:154
std::vector< BezierCurve > subdivideUntilFlat(float const tolerance) const noexcept
Definition BezierCurve.hpp:253
BezierCurve(vec const P1, vec const C1, vec const C2, vec const P2, Color color=Color::White) noexcept
Definition BezierCurve.hpp:58
BezierCurve(vec const P1, vec const C1, vec const P2, Color color=Color::White) noexcept
Definition BezierCurve.hpp:51
friend BezierCurve operator~(BezierCurve const &rhs) noexcept
Definition BezierCurve.hpp:318
vec P1
First point.
Definition BezierCurve.hpp:31
std::pair< BezierCurve, BezierCurve > split(float const t) const noexcept
Definition BezierCurve.hpp:226
std::pair< BezierCurve, BezierCurve > quadraticSplit(float const t) const noexcept
Definition BezierCurve.hpp:201
vec pointAt(float const t) const noexcept
Definition BezierCurve.hpp:105
BezierCurve(Type const type, vec const P1, vec const C1, vec const C2, vec const P2, Color color=Color::White) noexcept
Definition BezierCurve.hpp:65
void subdivideUntilFlat_impl(std::vector< BezierCurve > &r, float const minimumFlatness) const noexcept
Definition BezierCurve.hpp:239
std::pair< BezierCurve, BezierCurve > linearSplit(float const t) const noexcept
Definition BezierCurve.hpp:215
Definition polynomial.hpp:11
A 4D vector.
Definition vec.hpp:37