7#include "rapid/sdf_r8.hpp"
8#include "pixel_map.hpp"
13#include "geometry/vector.hpp"
14#include "geometry/point.hpp"
15#include "geometry/transform.hpp"
16#include "geometry/line_join_style.hpp"
21namespace hi::inline
v1 {
29 enum class Type : uint8_t { None, Linear, Quadratic, Cubic };
30 enum class Color : uint8_t { Yellow, Magenta, Cyan, White };
48 type(Type::Linear),
color(
color), P1(P1), C1(), C2(), P2(P2)
55 type(Type::Quadratic),
color(
color), P1(P1), C1(C1), C2(), P2(P2)
62 type(Type::Cubic),
color(
color), P1(P1), C1(C1), C2(C2), P2(P2)
74 Color
color = Color::White) noexcept :
75 type(type),
color(
color), P1(P1), C1(C1), C2(C2), P2(P2)
79 [[nodiscard]]
bool has_red() const noexcept
81 return color != Color::Cyan;
84 [[nodiscard]]
bool has_green() const noexcept
86 return color != Color::Magenta;
89 [[nodiscard]]
bool has_blue() const noexcept
91 return color != Color::Yellow;
104 case Type::Linear:
return bezierPointAt(P1, P2, t);
105 case Type::Quadratic:
return bezierPointAt(P1, C1, P2, t);
106 case Type::Cubic:
return bezierPointAt(P1, C1, C2, P2, t);
107 default: hi_no_default();
121 case Type::Linear:
return bezierTangentAt(P1, P2, t);
122 case Type::Quadratic:
return bezierTangentAt(P1, C1, P2, t);
123 case Type::Cubic:
return bezierTangentAt(P1, C1, C2, P2, t);
124 default: hi_no_default();
136 case Type::Quadratic:
return bezierFindX(P1, C1, P2, y);
137 case Type::Cubic:
return bezierFindX(P1, C1, C2, P2, y);
138 default: hi_no_default();
147 case Type::Cubic: hi_no_default();
148 default: hi_no_default();
176 [[nodiscard]] hi_force_inline
constexpr float orthogonality() const noexcept
179 return cross(normalize(tangent), normalize(PN));
182 [[nodiscard]] hi_force_inline
float distance() const noexcept
187 [[nodiscard]] hi_force_inline
float signed_distance() const noexcept
189 hilet d = distance();
190 return orthogonality() < 0.0 ? d : -d;
193 [[nodiscard]] hi_force_inline
constexpr bool operator<(sdf_distance_result
const& rhs)
const noexcept
195 if (abs(sq_distance - rhs.sq_distance) < 0.01f) {
196 return abs(orthogonality()) > abs(rhs.orthogonality());
198 return sq_distance < rhs.sq_distance;
215 hilet ts = solveTForNormalsIntersectingPoint(P);
217 t = std::clamp(t, 0.0f, 1.0f);
219 hilet PN = P - pointAt(t);
220 hilet sq_distance = squared_hypot(PN);
221 if (sq_distance < nearest.sq_distance) {
224 nearest.sq_distance = sq_distance;
247 return {{P1, outerA.pointAt(t), innerA.pointAt(t), newPoint}, {newPoint, innerB.pointAt(t), outerB.pointAt(t), P2}};
262 return {{P1, outerA.pointAt(t), newPoint}, {newPoint, outerB.pointAt(t), P2}};
272 hilet newPoint = pointAt(t);
274 return {{P1, newPoint}, {newPoint, P2}};
285 case Type::Linear:
return linearSplit(t);
286 case Type::Quadratic:
return quadraticSplit(t);
287 case Type::Cubic:
return cubicSplit(t);
288 default: hi_no_default();
298 if (flatness() >= minimumFlatness) {
301 hilet[a, b] = split(0.5f);
302 a.subdivideUntilFlat_impl(r, minimumFlatness);
303 b.subdivideUntilFlat_impl(r, minimumFlatness);
314 subdivideUntilFlat_impl(r, 1.0f - tolerance);
327 default: hi_no_default();
337 hilet[newP1, newP2] = parallelLine(P1, P2, offset);
338 return {newP1, newP2};
343 if (lhs.type != rhs.type) {
347 case bezier_curve::Type::Linear:
return (lhs.P1 == rhs.P1) && (lhs.P2 == rhs.P2);
348 case bezier_curve::Type::Quadratic:
return (lhs.P1 == rhs.P1) && (lhs.C1 == rhs.C1) && (lhs.P2 == rhs.P2);
349 case bezier_curve::Type::Cubic:
350 return (lhs.P1 == rhs.P1) && (lhs.C1 == rhs.C1) && (lhs.C2 == rhs.C2) && (lhs.P2 == rhs.P2);
351 default: hi_no_default();
355 [[nodiscard]]
friend bezier_curve operator*(geo::transformer
auto const& lhs, bezier_curve
const& rhs)
noexcept
357 return {rhs.type, lhs * rhs.P1, lhs * rhs.C1, lhs * rhs.C2, lhs * rhs.P2};
364 return {rhs.type, rhs.P2, rhs.C2, rhs.C1, rhs.P1};
375makeContourFromPoints(std::vector<bezier_point>::const_iterator first, std::vector<bezier_point>::const_iterator last)
noexcept;
399 float tolerance)
noexcept;
Utilities used by the HikoGUI library itself.
#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
line_join_style
The way two lines should be joined.
Definition line_join_style.hpp:12
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: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::vector< bezier_curve > makeInverseContour(std::vector< bezier_curve > const &contour) noexcept
Definition bezier_curve.hpp:28
results< float, 3 > solveXByY(float const y) const noexcept
Definition bezier_curve.hpp:132
std::pair< bezier_curve, bezier_curve > quadraticSplit(float const t) const noexcept
Definition bezier_curve.hpp:255
point2 P2
Last point.
Definition bezier_curve.hpp:37
point2 P1
First point.
Definition bezier_curve.hpp:34
float flatness() const noexcept
Definition bezier_curve.hpp:321
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:68
point2 C1
Control point.
Definition bezier_curve.hpp:35
bezier_curve(point2 const P1, point2 const C1, point2 const P2, Color color=Color::White) noexcept
Definition bezier_curve.hpp:54
std::pair< bezier_curve, bezier_curve > cubicSplit(float const t) const noexcept
Definition bezier_curve.hpp:236
std::vector< bezier_curve > subdivideUntilFlat(float const tolerance) const noexcept
Definition bezier_curve.hpp:311
bezier_curve toParallelLine(float const offset) const noexcept
Definition bezier_curve.hpp:335
void subdivideUntilFlat_impl(std::vector< bezier_curve > &r, float const minimumFlatness) const noexcept
Definition bezier_curve.hpp:296
std::pair< bezier_curve, bezier_curve > linearSplit(float const t) const noexcept
Definition bezier_curve.hpp:270
friend bezier_curve operator~(bezier_curve const &rhs) noexcept
Definition bezier_curve.hpp:362
point2 pointAt(float const t) const noexcept
Definition bezier_curve.hpp:101
sdf_distance_result sdf_distance(point2 P) const noexcept
Find the distance from the point to the curve.
Definition bezier_curve.hpp:211
bezier_curve(point2 const P1, point2 const C1, point2 const C2, point2 const P2, Color color=Color::White) noexcept
Definition bezier_curve.hpp:61
std::pair< bezier_curve, bezier_curve > split(float const t) const noexcept
Definition bezier_curve.hpp:282
point2 C2
Control point.
Definition bezier_curve.hpp:36
constexpr vector2 tangentAt(float const t) const noexcept
Definition bezier_curve.hpp:118
Definition bezier_curve.hpp:152
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:176
vector2 PN
The vector between P and N.
Definition bezier_curve.hpp:155
This is a RGBA floating point color.
Definition color.hpp:39
A 2D canvas of pixels.
Definition pixel_map.hpp:111
Definition polynomial.hpp:14