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 };
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);
114 case Type::Quadratic:
128 case Type::Quadratic:
164 return cross(normalize(
tangent), normalize(
PN));
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;
272 case Type::Quadratic:
290 hilet[a, b] =
split(0.5f);
315 case Type::Quadratic:
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};
360 return {rhs.type, rhs.P2, rhs.C2, rhs.C1, rhs.P1};
371 for (hilet& curve : v) {
380[[
nodiscard]]
constexpr std::optional<std::vector<std::pair<float, float>>>
383 auto xValues = solveCurvesXByY(v, y);
409constexpr void fillPartialPixels(std::span<uint8_t>
row,
ssize_t const i,
float const startX,
float const endX)
noexcept
417constexpr void fillFullPixels(std::span<uint8_t>
row,
ssize_t const start,
ssize_t const size)
noexcept
420 hilet
end = start + size;
435 auto u64p =
reinterpret_cast<uint64_t *
>(
u8p);
436 auto const *
const u64end =
reinterpret_cast<uint64_t
const *
>(hi::floor(
u8end,
sizeof(uint64_t)));
438 *(
u64p++) += 0x3333333333333333ULL;
442 u8p =
reinterpret_cast<uint8_t *
>(
u64p);
452constexpr void fillRowSpan(std::span<uint8_t>
row,
float const startX,
float const endX)
noexcept
477 for (
float y =
rowY + 0.1f; y < (
rowY + 1); y += 0.2f) {
487 for (hilet& span :
spans) {
488 fillRowSpan(
row, span.first, span.second);
511 return nearest.signed_distance();
529 auto type = bezier_curve::Type::None;
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);
630 }
else if (r.
back().P2 == curve.
P1) {
669 for (
auto y = 0
_uz; y < image.height(); y++) {
670 detail::fillRow(image[y], y,
curves);
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.
@ begin
Start from the beginning of the file.
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.
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
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 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
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
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)