7#include "bezier_point.hpp"
8#include "bezier_curve.hpp"
10#include "../utility/utility.hpp"
11#include "../geometry/geometry.hpp"
12#include "../image/image.hpp"
14#include "../macros.hpp"
22hi_warning_ignore_msvc(4459);
24hi_export_module(hikogui.graphic_path);
26hi_export
namespace hi {
inline namespace v1 {
81 if (
color != firstColor) {
98 for (
auto const& point :
points) {
131 [[nodiscard]] std::vector<bezier_point>::const_iterator endContour(
ssize_t contourNr)
const noexcept
150 [[nodiscard]] std::vector<bezier_point> getbezier_pointsOfContour(
ssize_t contourNr)
const noexcept
157 [[nodiscard]] std::vector<bezier_curve> getBeziersOfContour(
ssize_t contourNr)
const noexcept
160 auto last = endContour(contourNr);
162 if (num_points < 3) {
171 [[nodiscard]] std::vector<bezier_curve> getBeziers() const noexcept
175 std::vector<bezier_curve> r;
178 auto const beziers = getBeziersOfContour(contourNr);
179 r.
insert(r.
end(), beziers.begin(), beziers.end());
184 [[nodiscard]] std::pair<graphic_path, color> getLayer(
ssize_t layerNr)
const noexcept
188 auto path = graphic_path{};
190 auto const begin = beginLayer(layerNr);
191 auto const end = endLayer(layerNr);
193 path.addContour(
beginContour(contourNr), endContour(contourNr));
196 return {path, getColorOfLayer(layerNr)};
199 [[nodiscard]]
color getColorOfLayer(
ssize_t layerNr)
const noexcept
204 void setColorOfLayer(
ssize_t layerNr,
color fill_color)
noexcept
280 if (prev_i->second != i->second) {
281 tmp.push_back(*prev_i);
286 tmp.push_back(*prev_i);
309 points.emplace_back(position, bezier_point::Type::Anchor);
321 points.emplace_back(lastPosition + direction, bezier_point::Type::Anchor);
324 void lineTo(point2 position)
noexcept
328 points.emplace_back(position, bezier_point::Type::Anchor);
331 void lineRelativeTo(
vector2 direction)
noexcept
338 void quadraticCurveTo(point2 controlPosition, point2 position)
noexcept
342 points.emplace_back(controlPosition, bezier_point::Type::QuadraticControl);
343 points.emplace_back(position, bezier_point::Type::Anchor);
355 points.emplace_back(p + controlDirection, bezier_point::Type::QuadraticControl);
356 points.emplace_back(p + direction, bezier_point::Type::Anchor);
359 void cubicCurveTo(point2 controlPosition1, point2 controlPosition2, point2 position)
noexcept
363 points.emplace_back(controlPosition1, bezier_point::Type::CubicControl1);
364 points.emplace_back(controlPosition2, bezier_point::Type::CubicControl2);
365 points.emplace_back(position, bezier_point::Type::Anchor);
378 points.emplace_back(p + controlDirection1, bezier_point::Type::CubicControl1);
379 points.emplace_back(p + controlDirection2, bezier_point::Type::CubicControl2);
380 points.emplace_back(p + direction, bezier_point::Type::Anchor);
394 void arcTo(
float radius, point2 position)
noexcept
398 auto const r = std::abs(radius);
400 auto const P2 = position;
401 auto const Pm = midpoint(P1, P2);
403 auto const Vm2 = P2 - Pm;
406 auto const alpha =
std::asin(hypot(Vm2) / r);
409 auto const C = Pm + normal(Vm2) *
std::cos(alpha) * radius;
412 auto const VC1 = P1 - C;
413 auto const VC2 = P2 - C;
415 auto const q1 = squared_hypot(VC1);
416 auto const q2 = q1 + dot(VC1, VC2);
417 auto const k2 = (4.0f / 3.0f) * (
std::sqrt(2.0f * q1 * q2) - q2) / cross(VC1, VC2);
420 auto const C1 = point2{(C.x() + VC1.x()) - k2 * VC1.y(), (C.y() + VC1.y()) + k2 * VC1.x()};
421 auto const C2 = point2{(C.x() + VC2.x()) + k2 * VC2.y(), (C.y() + VC2.y()) - k2 * VC2.x()};
423 cubicCurveTo(C1, C2, P2);
435 auto const bl_radius = std::abs(corners.left_bottom());
436 auto const br_radius = std::abs(corners.right_bottom());
437 auto const tl_radius = std::abs(corners.left_top());
438 auto const tr_radius = std::abs(corners.right_top());
445 auto const blc1 = blc +
vector2{0.0f, bl_radius};
446 auto const blc2 = blc +
vector2{bl_radius, 0.0f};
447 auto const brc1 = brc + vector2{-br_radius, 0.0f};
448 auto const brc2 = brc + vector2{0.0f, br_radius};
449 auto const tlc1 = tlc + vector2{tl_radius, 0.0f};
450 auto const tlc2 = tlc + vector2{0.0f, -tl_radius};
451 auto const trc1 = trc + vector2{0.0f, -tr_radius};
452 auto const trc2 = trc + vector2{-tr_radius, 0.0f};
455 if (corners.left_bottom() > 0.0) {
456 arcTo(bl_radius, blc2);
457 }
else if (corners.left_bottom() < 0.0) {
462 if (corners.right_bottom() > 0.0) {
463 arcTo(br_radius, brc2);
464 }
else if (corners.right_bottom() < 0.0) {
469 if (corners.left_top() > 0.0) {
470 arcTo(tl_radius, tlc2);
471 }
else if (corners.left_top() < 0.0) {
476 if (corners.right_top() > 0.0) {
477 arcTo(tr_radius, trc2);
478 }
else if (corners.right_top() < 0.0) {
493 moveTo(point2{position.x(), position.y() - radius});
494 arcTo(radius, point2{position.x() + radius, position.y()});
495 arcTo(radius, point2{position.x(), position.y() + radius});
496 arcTo(radius, point2{position.x() - radius, position.y()});
497 arcTo(radius, point2{position.x(), position.y() - radius});
508 for (
auto const& curve : contour) {
510 switch (curve.type) {
511 case bezier_curve::Type::Linear:
512 points.emplace_back(curve.P2, bezier_point::Type::Anchor);
514 case bezier_curve::Type::Quadratic:
515 points.emplace_back(curve.C1, bezier_point::Type::QuadraticControl);
516 points.emplace_back(curve.P2, bezier_point::Type::Anchor);
518 case bezier_curve::Type::Cubic:
519 points.emplace_back(curve.C1, bezier_point::Type::CubicControl1);
520 points.emplace_back(curve.C2, bezier_point::Type::CubicControl2);
521 points.emplace_back(curve.P2, bezier_point::Type::Anchor);
535 std::vector<bezier_point>::const_iterator
const&
begin,
536 std::vector<bezier_point>::const_iterator
const&
end)
noexcept
566 float tolerance = 0.05f) noexcept
584 float strokeWidth = 1.0f,
586 float tolerance = 0.05f) const noexcept
593 float starboardOffset = strokeWidth / 2;
594 float portOffset = -starboardOffset;
597 auto const baseContour = getBeziersOfContour(i);
600 r.addContour(starboardContour);
603 r.addContour(portContour);
617 if (bbox.width() <= 0.0 || bbox.height() <= 0.0) {
621 auto const scale =
std::min(max_size.width() / bbox.width(), max_size.height() / bbox.height());
622 bbox =
scale2(scale) * bbox;
624 auto const offset = (point2{} - get<0>(bbox)) + (extent - bbox.size()) * 0.5;
632 hi_assert(!rhs.isContourOpen());
637 auto const pointOffset = ssize(
points);
641 for (
auto const & [ x, fill_color ] : rhs.layerEndContours) {
646 for (
auto const x : rhs.contourEndPoints) {
650 points.insert(
points.end(), rhs.points.begin(), rhs.points.end());
654 [[nodiscard]]
friend graphic_path operator+(graphic_path lhs, graphic_path
const& rhs)
noexcept
659 friend graphic_path operator*(transformer2
auto const& lhs, graphic_path
const& rhs)
noexcept
662 for (
auto& point : rhs_.points) {
675 fill(dst, path.getBeziers());
@ 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:22
@ miter
The outer edge of both lines are extended until they meet to form a sharp corner.
Definition line_join_style.hpp:33
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
@ color
A color value was modified.
Definition style_modify_mask.hpp:27
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition misc.hpp:32
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:698
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:554
constexpr std::vector< bezier_curve > makeInverseContour(std::vector< bezier_curve > const &contour) noexcept
Inverse a contour.
Definition bezier_curve.hpp:618
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:640
This is a RGBA floating point color.
Definition color_intf.hpp:49
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
The 4 radii of the corners of a quad or rectangle.
Definition corner_radii.hpp:26
A high-level geometric extent.
Definition extent2.hpp:32
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:107
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:118
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:25
Definition translate2.hpp:18
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:27
A path is a vector graphics object.
Definition graphic_path.hpp:34
void addContour(std::vector< bezier_point >::const_iterator const &begin, std::vector< bezier_point >::const_iterator const &end) noexcept
Curve with the given bezier curve.
Definition graphic_path.hpp:534
void addRectangle(aarectangle rectangle, corner_radii corners=corner_radii{0.0f, 0.0f, 0.0f, 0.0f}) noexcept
Draw a rectangle.
Definition graphic_path.hpp:431
void clear() noexcept
Clear the path.
Definition graphic_path.hpp:49
void addStroke(graphic_path const &path, color strokeColor, float strokeWidth, hi::line_join_style line_join_style=line_join_style::miter, float tolerance=0.05f) noexcept
Stroke a path and close layer.
Definition graphic_path.hpp:561
std::vector< bezier_point > points
A set of all bezier points describing all bezier curves, contours and layers.
Definition graphic_path.hpp:37
void optimizeLayers() noexcept
Optimize layers.
Definition graphic_path.hpp:268
void closeLayer(color fill_color) noexcept
Close current contour.
Definition graphic_path.hpp:257
std::vector< bezier_point >::const_iterator beginContour(ssize_t contourNr) const noexcept
Return an iterator to the start point of a contour.
Definition graphic_path.hpp:124
void addContour(std::vector< bezier_curve > const &contour) noexcept
Contour with the given bezier curves.
Definition graphic_path.hpp:504
void addContour(std::vector< bezier_point > const &contour) noexcept
Curve with the given bezier curve.
Definition graphic_path.hpp:546
aarectangle boundingBox() const noexcept
Calculate bounding box.
Definition graphic_path.hpp:90
graphic_path toStroke(float strokeWidth=1.0f, line_join_style line_join_style=line_join_style::miter, float tolerance=0.05f) const noexcept
Convert path to stroke-path.
Definition graphic_path.hpp:583
bool allLayersHaveSameColor() const noexcept
Check if all layers have the same color.
Definition graphic_path.hpp:72
ssize_t numberOfLayers() const noexcept
Return the number of closed layers.
Definition graphic_path.hpp:65
void cubicCurveRelativeTo(vector2 controlDirection1, vector2 controlDirection2, vector2 direction) noexcept
Draw curve from the current position to the new direction.
Definition graphic_path.hpp:373
ssize_t numberOfContours() const noexcept
Return the number of closed contours.
Definition graphic_path.hpp:58
std::vector< std::pair< ssize_t, color > > layerEndContours
An color and index into.
Definition graphic_path.hpp:45
void arcTo(float radius, point2 position) noexcept
Draw an circular arc.
Definition graphic_path.hpp:394
void closeContour() noexcept
Close current contour.
Definition graphic_path.hpp:225
void addPath(graphic_path const &path, color fill_color) noexcept
Add path and close layer.
Definition graphic_path.hpp:553
void addCircle(point2 position, float radius) noexcept
Draw a circle.
Definition graphic_path.hpp:489
void moveTo(point2 position) noexcept
Start a new contour at position.
Definition graphic_path.hpp:306
point2 currentPosition() const noexcept
Get the currentPosition of the open contour.
Definition graphic_path.hpp:294
bool hasLayers() const noexcept
This path has layers.
Definition graphic_path.hpp:234
std::vector< ssize_t > contourEndPoints
An index into.
Definition graphic_path.hpp:41
bool isContourOpen() const noexcept
Return true if there is an open contour.
Definition graphic_path.hpp:211
void quadraticCurveRelativeTo(vector2 controlDirection, vector2 direction) noexcept
Draw curve from the current position to the new direction.
Definition graphic_path.hpp:350
bool isLayerOpen() const noexcept
Return true if there is an open layer.
Definition graphic_path.hpp:241
void moveRelativeTo(vector2 direction) noexcept
Start a new contour relative to current position.
Definition graphic_path.hpp:315
graphic_path centerScale(extent2 extent, float padding=0.0) const noexcept
Center and scale a path inside the extent with padding.
Definition graphic_path.hpp:611
void tryRemoveLayers() noexcept
Try to move the layers in a path.
Definition graphic_path.hpp:109
A non-owning 2D pixel-based image.
Definition pixmap_span.hpp:34