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"
19hi_export_module(hikogui.graphic_path);
21hi_export
namespace hi {
inline namespace v1 {
76 if (
color != firstColor) {
93 for (
auto const& point :
points) {
126 [[nodiscard]] std::vector<bezier_point>::const_iterator endContour(
ssize_t contourNr)
const noexcept
155 auto last = endContour(contourNr);
157 if (num_points < 3) {
173 auto const beziers = getBeziersOfContour(contourNr);
174 r.
insert(r.
end(), beziers.begin(), beziers.end());
183 auto path = graphic_path{};
185 auto const begin = beginLayer(layerNr);
186 auto const end = endLayer(layerNr);
188 path.addContour(
beginContour(contourNr), endContour(contourNr));
191 return {path, getColorOfLayer(layerNr)};
194 [[nodiscard]] color getColorOfLayer(
ssize_t layerNr)
const noexcept
199 void setColorOfLayer(
ssize_t layerNr, color fill_color)
noexcept
275 if (prev_i->second != i->second) {
276 tmp.push_back(*prev_i);
281 tmp.push_back(*prev_i);
304 points.emplace_back(position, bezier_point::Type::Anchor);
316 points.emplace_back(lastPosition + direction, bezier_point::Type::Anchor);
319 void lineTo(point2 position)
noexcept
323 points.emplace_back(position, bezier_point::Type::Anchor);
326 void lineRelativeTo(
vector2 direction)
noexcept
333 void quadraticCurveTo(point2 controlPosition, point2 position)
noexcept
337 points.emplace_back(controlPosition, bezier_point::Type::QuadraticControl);
338 points.emplace_back(position, bezier_point::Type::Anchor);
350 points.emplace_back(p + controlDirection, bezier_point::Type::QuadraticControl);
351 points.emplace_back(p + direction, bezier_point::Type::Anchor);
354 void cubicCurveTo(point2 controlPosition1, point2 controlPosition2, point2 position)
noexcept
358 points.emplace_back(controlPosition1, bezier_point::Type::CubicControl1);
359 points.emplace_back(controlPosition2, bezier_point::Type::CubicControl2);
360 points.emplace_back(position, bezier_point::Type::Anchor);
373 points.emplace_back(p + controlDirection1, bezier_point::Type::CubicControl1);
374 points.emplace_back(p + controlDirection2, bezier_point::Type::CubicControl2);
375 points.emplace_back(p + direction, bezier_point::Type::Anchor);
389 void arcTo(
float radius, point2 position)
noexcept
393 auto const r = std::abs(radius);
395 auto const P2 = position;
396 auto const Pm = midpoint(P1, P2);
398 auto const Vm2 = P2 - Pm;
401 auto const alpha =
std::asin(hypot(Vm2) / r);
404 auto const C = Pm + normal(Vm2) *
std::cos(alpha) * radius;
407 auto const VC1 = P1 - C;
408 auto const VC2 = P2 - C;
410 auto const q1 = squared_hypot(VC1);
411 auto const q2 = q1 + dot(VC1, VC2);
412 auto const k2 = (4.0f / 3.0f) * (
std::sqrt(2.0f * q1 * q2) - q2) / cross(VC1, VC2);
415 auto const C1 = point2{(C.x() + VC1.x()) - k2 * VC1.y(), (C.y() + VC1.y()) + k2 * VC1.x()};
416 auto const C2 = point2{(C.x() + VC2.x()) + k2 * VC2.y(), (C.y() + VC2.y()) - k2 * VC2.x()};
418 cubicCurveTo(C1, C2, P2);
430 auto const bl_radius = std::abs(corners.left_bottom());
431 auto const br_radius = std::abs(corners.right_bottom());
432 auto const tl_radius = std::abs(corners.left_top());
433 auto const tr_radius = std::abs(corners.right_top());
440 auto const blc1 = blc +
vector2{0.0f, bl_radius};
441 auto const blc2 = blc +
vector2{bl_radius, 0.0f};
442 auto const brc1 = brc + vector2{-br_radius, 0.0f};
443 auto const brc2 = brc + vector2{0.0f, br_radius};
444 auto const tlc1 = tlc + vector2{tl_radius, 0.0f};
445 auto const tlc2 = tlc + vector2{0.0f, -tl_radius};
446 auto const trc1 = trc + vector2{0.0f, -tr_radius};
447 auto const trc2 = trc + vector2{-tr_radius, 0.0f};
450 if (corners.left_bottom() > 0.0) {
451 arcTo(bl_radius, blc2);
452 }
else if (corners.left_bottom() < 0.0) {
457 if (corners.right_bottom() > 0.0) {
458 arcTo(br_radius, brc2);
459 }
else if (corners.right_bottom() < 0.0) {
464 if (corners.left_top() > 0.0) {
465 arcTo(tl_radius, tlc2);
466 }
else if (corners.left_top() < 0.0) {
471 if (corners.right_top() > 0.0) {
472 arcTo(tr_radius, trc2);
473 }
else if (corners.right_top() < 0.0) {
488 moveTo(point2{position.x(), position.y() - radius});
489 arcTo(radius, point2{position.x() + radius, position.y()});
490 arcTo(radius, point2{position.x(), position.y() + radius});
491 arcTo(radius, point2{position.x() - radius, position.y()});
492 arcTo(radius, point2{position.x(), position.y() - radius});
503 for (
auto const& curve : contour) {
505 switch (curve.type) {
506 case bezier_curve::Type::Linear:
507 points.emplace_back(curve.P2, bezier_point::Type::Anchor);
509 case bezier_curve::Type::Quadratic:
510 points.emplace_back(curve.C1, bezier_point::Type::QuadraticControl);
511 points.emplace_back(curve.P2, bezier_point::Type::Anchor);
513 case bezier_curve::Type::Cubic:
514 points.emplace_back(curve.C1, bezier_point::Type::CubicControl1);
515 points.emplace_back(curve.C2, bezier_point::Type::CubicControl2);
516 points.emplace_back(curve.P2, bezier_point::Type::Anchor);
530 std::vector<bezier_point>::const_iterator
const&
begin,
531 std::vector<bezier_point>::const_iterator
const&
end)
noexcept
561 float tolerance = 0.05f) noexcept
579 float strokeWidth = 1.0f,
581 float tolerance = 0.05f) const noexcept
588 float starboardOffset = strokeWidth / 2;
589 float portOffset = -starboardOffset;
592 auto const baseContour = getBeziersOfContour(i);
595 r.addContour(starboardContour);
598 r.addContour(portContour);
612 if (bbox.width() <= 0.0 || bbox.height() <= 0.0) {
616 auto const scale =
std::min(max_size.width() / bbox.width(), max_size.height() / bbox.height());
617 bbox =
scale2(scale) * bbox;
619 auto const offset = (point2{} - get<0>(bbox)) + (extent - bbox.size()) * 0.5;
627 hi_assert(!rhs.isContourOpen());
632 auto const pointOffset = ssize(
points);
645 points.insert(
points.end(), rhs.points.begin(), rhs.points.end());
649 [[nodiscard]]
friend graphic_path operator+(graphic_path lhs, graphic_path
const& rhs)
noexcept
654 friend graphic_path operator*(transformer2
auto const& lhs, graphic_path
const& rhs)
noexcept
657 for (
auto& point : rhs_.
points) {
670 fill(dst, path.getBeziers());
@ 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:22
@ miter
The outer edge of both lines are extended until they meet to form a sharp corner.
The HikoGUI namespace.
Definition array_generic.hpp:20
std::ptrdiff_t ssize_t
Signed size/index into an array.
Definition misc.hpp:32
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:549
constexpr std::vector< bezier_curve > makeInverseContour(std::vector< bezier_curve > const &contour) noexcept
Inverse a contour.
Definition bezier_curve.hpp:613
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:635
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
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:29
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:529
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:426
void clear() noexcept
Clear the path.
Definition graphic_path.hpp:44
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:556
std::vector< bezier_point > points
A set of all bezier points describing all bezier curves, contours and layers.
Definition graphic_path.hpp:32
void optimizeLayers() noexcept
Optimize layers.
Definition graphic_path.hpp:263
void closeLayer(color fill_color) noexcept
Close current contour.
Definition graphic_path.hpp:252
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:119
void addContour(std::vector< bezier_curve > const &contour) noexcept
Contour with the given bezier curves.
Definition graphic_path.hpp:499
void addContour(std::vector< bezier_point > const &contour) noexcept
Curve with the given bezier curve.
Definition graphic_path.hpp:541
aarectangle boundingBox() const noexcept
Calculate bounding box.
Definition graphic_path.hpp:85
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:578
bool allLayersHaveSameColor() const noexcept
Check if all layers have the same color.
Definition graphic_path.hpp:67
ssize_t numberOfLayers() const noexcept
Return the number of closed layers.
Definition graphic_path.hpp:60
void cubicCurveRelativeTo(vector2 controlDirection1, vector2 controlDirection2, vector2 direction) noexcept
Draw curve from the current position to the new direction.
Definition graphic_path.hpp:368
ssize_t numberOfContours() const noexcept
Return the number of closed contours.
Definition graphic_path.hpp:53
std::vector< std::pair< ssize_t, color > > layerEndContours
An color and index into.
Definition graphic_path.hpp:40
void arcTo(float radius, point2 position) noexcept
Draw an circular arc.
Definition graphic_path.hpp:389
void closeContour() noexcept
Close current contour.
Definition graphic_path.hpp:220
void addPath(graphic_path const &path, color fill_color) noexcept
Add path and close layer.
Definition graphic_path.hpp:548
void addCircle(point2 position, float radius) noexcept
Draw a circle.
Definition graphic_path.hpp:484
void moveTo(point2 position) noexcept
Start a new contour at position.
Definition graphic_path.hpp:301
point2 currentPosition() const noexcept
Get the currentPosition of the open contour.
Definition graphic_path.hpp:289
bool hasLayers() const noexcept
This path has layers.
Definition graphic_path.hpp:229
std::vector< ssize_t > contourEndPoints
An index into.
Definition graphic_path.hpp:36
bool isContourOpen() const noexcept
Return true if there is an open contour.
Definition graphic_path.hpp:206
void quadraticCurveRelativeTo(vector2 controlDirection, vector2 direction) noexcept
Draw curve from the current position to the new direction.
Definition graphic_path.hpp:345
bool isLayerOpen() const noexcept
Return true if there is an open layer.
Definition graphic_path.hpp:236
void moveRelativeTo(vector2 direction) noexcept
Start a new contour relative to current position.
Definition graphic_path.hpp:310
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:606
void tryRemoveLayers() noexcept
Try to move the layers in a path.
Definition graphic_path.hpp:104
A non-owning 2D pixel-based image.
Definition pixmap_span.hpp:34