7#include "image/module.hpp"
8#include "geometry/module.hpp"
9#include "utility/module.hpp"
15namespace hi::inline
v1 {
23 enum class Type : uint8_t { None, Linear, Quadratic, Cubic };
24 enum class Color : uint8_t { Yellow, Magenta, Cyan, White };
42 type(Type::Linear), color(color), P1(P1), C1(), C2(), P2(P2)
49 type(Type::Quadratic), color(color), P1(P1), C1(C1), C2(), P2(P2)
56 type(Type::Cubic), color(color), P1(P1), C1(C1), C2(C2), P2(P2)
68 Color color = Color::White) noexcept :
69 type(type), color(color), P1(P1), C1(C1), C2(C2), P2(P2)
73 [[nodiscard]]
bool has_red() const noexcept
75 return color != Color::Cyan;
78 [[nodiscard]]
bool has_green() const noexcept
80 return color != Color::Magenta;
83 [[nodiscard]]
bool has_blue() const noexcept
85 return color != Color::Yellow;
98 case Type::Linear:
return bezierPointAt(P1, P2, t);
99 case Type::Quadratic:
return bezierPointAt(P1, C1, P2, t);
100 case Type::Cubic:
return bezierPointAt(P1, C1, C2, P2, t);
115 case Type::Linear:
return bezierTangentAt(P1, P2, t);
116 case Type::Quadratic:
return bezierTangentAt(P1, C1, P2, t);
117 case Type::Cubic:
return bezierTangentAt(P1, C1, C2, P2, t);
130 case Type::Quadratic:
return bezierFindX(P1, C1, P2, y);
131 case Type::Cubic:
return bezierFindX(P1, C1, C2, P2, y);
170 [[nodiscard]] hi_force_inline
constexpr float orthogonality() const noexcept
173 return cross(normalize(tangent), normalize(PN));
176 [[nodiscard]] hi_force_inline
float distance() const noexcept
181 [[nodiscard]] hi_force_inline
float signed_distance() const noexcept
183 hilet d = distance();
184 return orthogonality() < 0.0 ? d : -d;
187 [[nodiscard]] hi_force_inline
constexpr bool operator<(sdf_distance_result
const& rhs)
const noexcept
189 if (abs(sq_distance - rhs.sq_distance) < 0.01f) {
190 return abs(orthogonality()) > abs(rhs.orthogonality());
192 return sq_distance < rhs.sq_distance;
209 hilet ts = solveTForNormalsIntersectingPoint(P);
211 t = std::clamp(t, 0.0f, 1.0f);
213 hilet PN = P - pointAt(t);
214 hilet sq_distance = squared_hypot(PN);
215 if (sq_distance < nearest.sq_distance) {
218 nearest.sq_distance = sq_distance;
241 return {{P1, outerA.pointAt(t), innerA.pointAt(t), newPoint}, {newPoint, innerB.pointAt(t), outerB.pointAt(t), P2}};
256 return {{P1, outerA.pointAt(t), newPoint}, {newPoint, outerB.pointAt(t), P2}};
266 hilet newPoint = pointAt(t);
268 return {{P1, newPoint}, {newPoint, P2}};
279 case Type::Linear:
return linearSplit(t);
280 case Type::Quadratic:
return quadraticSplit(t);
281 case Type::Cubic:
return cubicSplit(t);
292 if (flatness() >= minimumFlatness) {
295 hilet[a, b] = split(0.5f);
296 a.subdivideUntilFlat_impl(r, minimumFlatness);
297 b.subdivideUntilFlat_impl(r, minimumFlatness);
308 subdivideUntilFlat_impl(r, 1.0f - tolerance);
331 hilet[newP1, newP2] = parallelLine(P1, P2, offset);
332 return {newP1, newP2};
337 if (lhs.type != rhs.type) {
341 case bezier_curve::Type::Linear:
return (lhs.P1 == rhs.P1) && (lhs.P2 == rhs.P2);
342 case bezier_curve::Type::Quadratic:
return (lhs.P1 == rhs.P1) && (lhs.C1 == rhs.C1) && (lhs.P2 == rhs.P2);
343 case bezier_curve::Type::Cubic:
344 return (lhs.P1 == rhs.P1) && (lhs.C1 == rhs.C1) && (lhs.C2 == rhs.C2) && (lhs.P2 == rhs.P2);
349 [[nodiscard]]
friend bezier_curve operator*(geo::transformer
auto const& lhs, bezier_curve
const& rhs)
noexcept
351 return {rhs.type, lhs * rhs.P1, lhs * rhs.C1, lhs * rhs.C2, lhs * rhs.P2};
358 return {rhs.type, rhs.P2, rhs.C2, rhs.C1, rhs.P1};
369makeContourFromPoints(std::vector<bezier_point>::const_iterator first, std::vector<bezier_point>::const_iterator last)
noexcept;
393 float tolerance)
noexcept;
#define hi_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:279
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
line_join_style
The way two lines should be joined.
Definition line_join_style.hpp:18
DOXYGEN BUG.
Definition algorithm.hpp:13
constexpr results< float, 1 > bezierFindX(point2 P1, point2 P2, float y) noexcept
Definition bezier.hpp:159
std::vector< bezier_curve > makeContourFromPoints(std::vector< bezier_point >::const_iterator first, std::vector< bezier_point >::const_iterator last) noexcept
std::vector< bezier_curve > makeParallelContour(std::vector< bezier_curve > const &contour, float offset, hi::line_join_style line_join_style, float tolerance) noexcept
float bezierFlatness(point2 P1, point2 P2) noexcept
Definition bezier.hpp:226
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:119
std::vector< bezier_curve > makeInverseContour(std::vector< bezier_curve > const &contour) noexcept
Definition bezier_curve.hpp:22
results< float, 3 > solveXByY(float const y) const noexcept
Definition bezier_curve.hpp:126
std::pair< bezier_curve, bezier_curve > quadraticSplit(float const t) const noexcept
Definition bezier_curve.hpp:249
point2 P2
Last point.
Definition bezier_curve.hpp:31
point2 P1
First point.
Definition bezier_curve.hpp:28
float flatness() const noexcept
Definition bezier_curve.hpp:315
bezier_curve(Type const type, point2 const P1, point2 const C1, point2 const C2, point2 const P2, Color color=Color::White) noexcept
Definition bezier_curve.hpp:62
point2 C1
Control point.
Definition bezier_curve.hpp:29
bezier_curve(point2 const P1, point2 const C1, point2 const P2, Color color=Color::White) noexcept
Definition bezier_curve.hpp:48
std::pair< bezier_curve, bezier_curve > cubicSplit(float const t) const noexcept
Definition bezier_curve.hpp:230
std::vector< bezier_curve > subdivideUntilFlat(float const tolerance) const noexcept
Definition bezier_curve.hpp:305
bezier_curve toParallelLine(float const offset) const noexcept
Definition bezier_curve.hpp:329
void subdivideUntilFlat_impl(std::vector< bezier_curve > &r, float const minimumFlatness) const noexcept
Definition bezier_curve.hpp:290
std::pair< bezier_curve, bezier_curve > linearSplit(float const t) const noexcept
Definition bezier_curve.hpp:264
friend bezier_curve operator~(bezier_curve const &rhs) noexcept
Definition bezier_curve.hpp:356
point2 pointAt(float const t) const noexcept
Definition bezier_curve.hpp:95
sdf_distance_result sdf_distance(point2 P) const noexcept
Find the distance from the point to the curve.
Definition bezier_curve.hpp:205
bezier_curve(point2 const P1, point2 const C1, point2 const C2, point2 const P2, Color color=Color::White) noexcept
Definition bezier_curve.hpp:55
std::pair< bezier_curve, bezier_curve > split(float const t) const noexcept
Definition bezier_curve.hpp:276
point2 C2
Control point.
Definition bezier_curve.hpp:30
constexpr vector2 tangentAt(float const t) const noexcept
Definition bezier_curve.hpp:112
Definition bezier_curve.hpp:146
hi_force_inline constexpr float orthogonality() const noexcept
The orthogonality of the line PN and the tangent of the curve at N.
Definition bezier_curve.hpp:170
vector2 PN
The vector between P and N.
Definition bezier_curve.hpp:149
Definition graphic_path.hpp:18
Definition polynomial.hpp:14