7#include "../image/module.hpp"
8#include "../geometry/module.hpp"
9#include "../utility/utility.hpp"
11#include "bezier_point.hpp"
12#include "../macros.hpp"
17hi_export_module(hikogui.graphic_path.bezier_curve);
19namespace hi {
inline namespace v1 {
25 enum class Type : uint8_t { None, Linear, Quadratic, Cubic };
33 bezier_curve() noexcept = delete;
34 bezier_curve(bezier_curve const&
other) noexcept = default;
35 bezier_curve(bezier_curve&&
other) noexcept = default;
36 bezier_curve& operator=(bezier_curve const&
other) noexcept = default;
37 bezier_curve& operator=(bezier_curve&&
other) noexcept = default;
41 bezier_curve(point2 const
P1, point2 const
P2) noexcept : type(Type::Linear),
P1(
P1),
C1(),
C2(),
P2(
P2) {}
58 bezier_curve(Type
const type, point2
const P1, point2
const C1, point2
const C2, point2
const P2) noexcept :
70 [[nodiscard]] point2
pointAt(
float const t)
const noexcept
74 return bezierPointAt(
P1,
P2, t);
76 return bezierPointAt(
P1,
C1,
P2, t);
78 return bezierPointAt(
P1,
C1,
C2,
P2, t);
95 return bezierTangentAt(
P1,
P2, t);
97 return bezierTangentAt(
P1,
C1,
P2, t);
99 return bezierTangentAt(
P1,
C1,
C2,
P2, t);
109 [[nodiscard]] results<float, 3>
solveXByY(
float const y)
const noexcept
114 case Type::Quadratic:
123 [[nodiscard]] hi_force_inline results<float, 3> solveTForNormalsIntersectingPoint(point2 P)
const noexcept
128 case Type::Quadratic:
137 struct sdf_distance_result {
142 bezier_curve
const *curve =
nullptr;
152 constexpr sdf_distance_result() noexcept = default;
153 constexpr sdf_distance_result(sdf_distance_result const&) noexcept = default;
154 constexpr sdf_distance_result(sdf_distance_result&&) noexcept = default;
155 constexpr sdf_distance_result& operator=(sdf_distance_result const&) noexcept = default;
156 constexpr sdf_distance_result& operator=(sdf_distance_result&&) noexcept = default;
157 constexpr sdf_distance_result(bezier_curve const *curve) noexcept : curve(curve) {}
161 [[nodiscard]] hi_force_inline
constexpr float orthogonality() const noexcept
163 hilet tangent = curve->tangentAt(
t);
164 return cross(normalize(tangent), normalize(
PN));
167 [[nodiscard]] hi_force_inline
float distance() const noexcept
172 [[nodiscard]] hi_force_inline
float signed_distance() const noexcept
174 hilet d = distance();
178 [[nodiscard]] hi_force_inline
constexpr bool operator<(sdf_distance_result
const& rhs)
const noexcept
200 hilet ts = solveTForNormalsIntersectingPoint(P);
202 t = std::clamp(t, 0.0f, 1.0f);
205 hilet sq_distance = squared_hypot(PN);
206 if (sq_distance < nearest.sq_distance) {
209 nearest.sq_distance = sq_distance;
223 hilet outerA = bezier_curve{
P1,
C1};
224 hilet outerBridge = bezier_curve{
C1,
C2};
225 hilet outerB = bezier_curve{
C2,
P2};
227 hilet innerA = bezier_curve{outerA.pointAt(t), outerBridge.pointAt(t)};
228 hilet innerB = bezier_curve{outerBridge.pointAt(t), outerB.pointAt(t)};
230 hilet newPoint = bezier_curve{innerA.pointAt(t), innerB.pointAt(t)}.pointAt(t);
232 return {{
P1, outerA.pointAt(t), innerA.pointAt(t), newPoint}, {newPoint, innerB.pointAt(t), outerB.pointAt(t),
P2}};
242 hilet outerA = bezier_curve{
P1,
C1};
243 hilet outerB = bezier_curve{
C1,
P2};
245 hilet newPoint = bezier_curve{outerA.pointAt(t), outerB.pointAt(t)}.pointAt(t);
247 return {{
P1, outerA.pointAt(t), newPoint}, {newPoint, outerB.pointAt(t),
P2}};
259 return {{
P1, newPoint}, {newPoint,
P2}};
272 case Type::Quadratic:
287 if (
flatness() >= minimumFlatness) {
290 hilet[a, b] =
split(0.5f);
291 a.subdivideUntilFlat_impl(r, minimumFlatness);
292 b.subdivideUntilFlat_impl(r, minimumFlatness);
315 case Type::Quadratic:
330 hilet[newP1, newP2] = parallelLine(
P1,
P2, offset);
331 return {newP1, newP2};
336 if (lhs.type != rhs.type) {
340 case bezier_curve::Type::Linear:
341 return (lhs.P1 == rhs.P1) && (lhs.P2 == rhs.P2);
342 case bezier_curve::Type::Quadratic:
343 return (lhs.P1 == rhs.P1) && (lhs.C1 == rhs.C1) && (lhs.P2 == rhs.P2);
344 case bezier_curve::Type::Cubic:
345 return (lhs.P1 == rhs.P1) && (lhs.C1 == rhs.C1) && (lhs.C2 == rhs.C2) && (lhs.P2 == rhs.P2);
351 [[nodiscard]]
friend bezier_curve operator*(transformer2
auto const& lhs, bezier_curve
const& rhs)
noexcept
353 return {rhs.type, lhs * rhs.P1, lhs * rhs.C1, lhs * rhs.C2, lhs * rhs.P2};
358 [[nodiscard]]
friend bezier_curve
operator~(bezier_curve
const& rhs)
noexcept
360 return {rhs.type, rhs.P2, rhs.C2, rhs.C1, rhs.P1};
371 for (hilet& curve : v) {
373 for (hilet x : xValues) {
380[[nodiscard]]
constexpr std::optional<std::vector<std::pair<float, float>>>
381getFillSpansAtY(std::vector<bezier_curve>
const& v,
float y)
noexcept
383 auto xValues = solveCurvesXByY(v, y);
386 std::sort(xValues.begin(), xValues.end());
389 hilet uniqueEnd =
std::unique(xValues.begin(), xValues.end());
392 std::size_t
const uniqueValueCount = (uniqueEnd - xValues.begin());
394 if (uniqueValueCount % 2 != 0) {
401 auto r = std::vector<std::pair<float, float>>{};
402 r.
reserve(uniqueValueCount / 2);
403 for (std::size_t i = 0; i < uniqueValueCount; i += 2) {
409constexpr void fillPartialPixels(std::span<uint8_t> row,
ssize_t const i,
float const startX,
float const endX)
noexcept
411 hilet pixelCoverage = std::clamp(endX, i + 0.0f, i + 1.0f) - std::clamp(startX, i + 0.0f, i + 1.0f);
413 auto& pixel = row[i];
414 pixel =
static_cast<uint8_t
>(
std::min(pixelCoverage * 51.0f + pixel, 255.0f));
417constexpr void fillFullPixels(std::span<uint8_t> row,
ssize_t const start,
ssize_t const size)
noexcept
420 hilet
end = start + size;
425 auto u8p = &row[start];
426 hilet u8end = u8p + size;
429 hilet alignedStart = hi::ceil(u8p,
sizeof(uint64_t));
430 while (u8p < alignedStart) {
435 auto u64p =
reinterpret_cast<uint64_t *
>(u8p);
436 auto const *
const u64end =
reinterpret_cast<uint64_t
const *
>(hi::floor(u8end,
sizeof(uint64_t)));
437 while (u64p < u64end) {
438 *(u64p++) += 0x3333333333333333ULL;
442 u8p =
reinterpret_cast<uint8_t *
>(u64p);
443 while (u8p < u8end) {
452constexpr void fillRowSpan(std::span<uint8_t> row,
float const startX,
float const endX)
noexcept
454 if (startX >= row.size() || endX < 0.0f) {
458 hilet startX_int = floor_cast<std::size_t>(startX);
459 hilet endXplusOne = endX + 1.0f;
460 hilet endX_int = floor_cast<std::size_t>(endXplusOne);
461 hilet startColumn =
std::max(startX_int, std::size_t{0});
462 hilet endColumn =
std::min(endX_int, row.size());
463 hilet nrColumns = endColumn - startColumn;
465 if (nrColumns == 1) {
466 fillPartialPixels(row, startColumn, startX, endX);
468 fillPartialPixels(row, startColumn, startX, endX);
469 fillFullPixels(row, startColumn + 1, nrColumns - 2);
470 fillPartialPixels(row, endColumn - 1, startX, endX);
474constexpr void fillRow(std::span<uint8_t> row, std::size_t rowY, std::vector<bezier_curve>
const& curves)
noexcept
477 for (
float y = rowY + 0.1f; y < (rowY + 1); y += 0.2f) {
478 auto optionalSpans = getFillSpansAtY(curves, y);
479 if (!optionalSpans) {
481 optionalSpans = getFillSpansAtY(curves, y + 0.01f);
485 hilet& spans = optionalSpans.value();
487 for (hilet& span : spans) {
488 fillRowSpan(row, span.first, span.second);
494[[nodiscard]]
constexpr float generate_sdf_r8_pixel(point2 point, std::vector<bezier_curve>
const& curves)
noexcept
496 if (curves.empty()) {
500 auto it = curves.cbegin();
501 auto nearest = (it++)->sdf_distance(point);
503 for (; it != curves.cend(); ++it) {
504 hilet
distance = it->sdf_distance(point);
506 if (distance < nearest) {
511 return nearest.signed_distance();
522[[nodiscard]]
constexpr std::vector<bezier_curve>
529 auto type = bezier_curve::Type::None;
534 for (hilet& point :
points) {
535 switch (point.type) {
536 case bezier_point::Type::Anchor:
538 case bezier_curve::Type::None:
540 type = bezier_curve::Type::Linear;
542 case bezier_curve::Type::Linear:
545 type = bezier_curve::Type::Linear;
547 case bezier_curve::Type::Quadratic:
550 type = bezier_curve::Type::Linear;
552 case bezier_curve::Type::Cubic:
555 type = bezier_curve::Type::Linear;
561 case bezier_point::Type::QuadraticControl:
563 type = bezier_curve::Type::Quadratic;
565 case bezier_point::Type::CubicControl1:
567 type = bezier_curve::Type::Cubic;
569 case bezier_point::Type::CubicControl2:
571 hi_assert(type == bezier_curve::Type::Cubic);
592 for (
auto i = contour.rbegin(); i != contour.rend(); i++) {
613 float tolerance)
noexcept
616 for (hilet& curve : contour) {
618 contourAtOffset.push_back(flatCurve.toParallelLine(offset));
624 std::optional<point2> intersectPoint;
626 for (hilet& curve : contourAtOffset) {
630 }
else if (r.
back().P2 == curve.
P1) {
634 r.
back().P2 = intersectPoint.value();
636 r.
back().P1 = intersectPoint.value();
641 r.
back().P2 = intersectPoint.value();
643 r.
back().P1 = intersectPoint.value();
654 r.
back().P2 = r.
front().P1 = intersectPoint.value();
669 for (
auto y = 0_uz; y < image.height(); y++) {
670 detail::fillRow(image[y], y, curves);
680 for (
auto row_nr = 0_uz; row_nr != image.height(); ++row_nr) {
681 hilet row = image[row_nr];
682 hilet y =
static_cast<float>(row_nr);
683 for (
auto column_nr = 0_uz; column_nr != image.width(); ++column_nr) {
684 hilet x =
static_cast<float>(column_nr);
685 row[column_nr] = detail::generate_sdf_r8_pixel(point2(x, y), curves);
@ end
Start from the end of the file.
Definition seek_whence.hpp:17
@ begin
Start from the beginning of the file.
Definition seek_whence.hpp:15
line_join_style
The way two lines should be joined.
Definition line_join_style.hpp:19
@ miter
The outer edge of both lines are extended until they meet to form a sharp corner.
Definition line_join_style.hpp:30
@ other
The gui_event does not have associated data.
Definition gui_event_variant.hpp:22
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
The HikoGUI API version 1.
Definition lookahead_iterator.hpp:6
constexpr results< float, 1 > bezierFindX(point2 P1, point2 P2, float y) noexcept
Definition bezier.hpp:146
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition misc.hpp:33
constexpr void fill(pixmap_span< uint8_t > image, std::vector< bezier_curve > const &curves) noexcept
Fill a linear gray scale image by filling a curve with anti-aliasing.
Definition bezier_curve.hpp:667
constexpr std::vector< bezier_curve > makeContourFromPoints(std::vector< bezier_point >::const_iterator begin, std::vector< bezier_point >::const_iterator end) noexcept
Make a contour of Bezier curves from a list of points.
Definition bezier_curve.hpp:523
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:106
std::optional< point2 > getIntersectionPoint(point2 A1, point2 A2, point2 B1, point2 B2) noexcept
Definition bezier.hpp:260
unit< si_length_tag, double, std::ratio< 254, 720 '000 >::type > points
Points: 1/72 inch.
Definition units.hpp:180
constexpr std::vector< bezier_curve > makeInverseContour(std::vector< bezier_curve > const &contour) noexcept
Inverse a contour.
Definition bezier_curve.hpp:587
std::optional< point2 > getExtrapolatedIntersectionPoint(point2 A1, point2 A2, point2 B1, point2 B2) noexcept
Definition bezier.hpp:291
constexpr std::vector< bezier_curve > makeParallelContour(std::vector< bezier_curve > const &contour, float offset, hi::line_join_style line_join_style, float tolerance) noexcept
Definition bezier_curve.hpp:609
float bezierFlatness(point2 P1, point2 P2) noexcept
Definition bezier.hpp:213
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:19
Definition bezier_curve.hpp:24
point2 pointAt(float const t) const noexcept
Definition bezier_curve.hpp:70
point2 P2
Last point.
Definition bezier_curve.hpp:31
void subdivideUntilFlat_impl(std::vector< bezier_curve > &r, float const minimumFlatness) const noexcept
Definition bezier_curve.hpp:285
std::vector< bezier_curve > subdivideUntilFlat(float const tolerance) const noexcept
Definition bezier_curve.hpp:300
bezier_curve(point2 const P1, point2 const C1, point2 const P2) noexcept
Definition bezier_curve.hpp:45
std::pair< bezier_curve, bezier_curve > linearSplit(float const t) const noexcept
Definition bezier_curve.hpp:255
results< float, 3 > solveXByY(float const y) const noexcept
Definition bezier_curve.hpp:109
constexpr vector2 tangentAt(float const t) const noexcept
Definition bezier_curve.hpp:91
sdf_distance_result sdf_distance(point2 P) const noexcept
Find the distance from the point to the curve.
Definition bezier_curve.hpp:196
bezier_curve(Type const type, point2 const P1, point2 const C1, point2 const C2, point2 const P2) noexcept
Definition bezier_curve.hpp:58
std::pair< bezier_curve, bezier_curve > split(float const t) const noexcept
Definition bezier_curve.hpp:267
point2 C2
Control point.
Definition bezier_curve.hpp:30
std::pair< bezier_curve, bezier_curve > quadraticSplit(float const t) const noexcept
Definition bezier_curve.hpp:240
friend bezier_curve operator~(bezier_curve const &rhs) noexcept
Definition bezier_curve.hpp:358
point2 C1
Control point.
Definition bezier_curve.hpp:29
bezier_curve toParallelLine(float const offset) const noexcept
Definition bezier_curve.hpp:328
bezier_curve(point2 const P1, point2 const C1, point2 const C2, point2 const P2) noexcept
Definition bezier_curve.hpp:51
point2 P1
First point.
Definition bezier_curve.hpp:28
float flatness() const noexcept
Definition bezier_curve.hpp:310
std::pair< bezier_curve, bezier_curve > cubicSplit(float const t) const noexcept
Definition bezier_curve.hpp:221
Definition bezier_curve.hpp:137
vector2 PN
The vector between P and N.
Definition bezier_curve.hpp:140
float t
Linear position on the curve-segment, 0.0 and 1.0 are end-points.
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:161
float sq_distance
The square distance between P and N.
Definition bezier_curve.hpp:150
static constexpr std::vector< bezier_point > normalizePoints(std::vector< bezier_point >::const_iterator const begin, std::vector< bezier_point >::const_iterator const end) noexcept
Definition bezier_point.hpp:41
A non-owning 2D pixel-based image.
Definition pixmap_span.hpp:34
T emplace_back(T... args)