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 {
183 auto path = graphic_path{};
191 return {path, getColorOfLayer(
layerNr)};
275 if (
prev_i->second != i->second) {
304 points.emplace_back(position, 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
338 points.emplace_back(position, bezier_point::Type::Anchor);
351 points.emplace_back(p + direction, bezier_point::Type::Anchor);
360 points.emplace_back(position, bezier_point::Type::Anchor);
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;
407 auto const VC1 = P1 - C;
408 auto const VC2 = P2 - C;
410 auto const q1 = squared_hypot(
VC1);
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);
450 if (
corners.left_bottom() > 0.0) {
452 }
else if (
corners.left_bottom() < 0.0) {
457 if (
corners.right_bottom() > 0.0) {
459 }
else if (
corners.right_bottom() < 0.0) {
464 if (
corners.left_top() > 0.0) {
466 }
else if (
corners.left_top() < 0.0) {
471 if (
corners.right_top() > 0.0) {
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
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());
619 auto const offset = (point2{} -
get<0>(
bbox)) + (extent -
bbox.size()) * 0.5;
627 hi_assert(!rhs.isContourOpen());
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
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.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
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
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
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:26
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