File indexing completed on 2024-05-12 15:56:43

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2008-2009 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef KOPATHSEGMENT_H
0008 #define KOPATHSEGMENT_H
0009 
0010 #include "kritaflake_export.h"
0011 #include <QList>
0012 #include <QPair>
0013 
0014 class KoPathPoint;
0015 class QTransform;
0016 class QPointF;
0017 class QRectF;
0018 
0019 /// A KoPathSegment consist of two neighboring KoPathPoints
0020 class KRITAFLAKE_EXPORT KoPathSegment
0021 {
0022 public:
0023     /**
0024     * Creates a new segment from the given path points
0025     * It takes ownership of the path points which do not have a
0026     * parent path shape set.
0027     */
0028     explicit KoPathSegment(KoPathPoint * first = 0, KoPathPoint * second = 0);
0029 
0030     /// Constructs segment by copying another segment
0031     KoPathSegment(const KoPathSegment &segment);
0032 
0033     /// Creates a new line segment
0034     KoPathSegment(const QPointF &p0, const QPointF &p1);
0035     /// Creates a new quadratic segment
0036     KoPathSegment(const QPointF &p0, const QPointF &p1, const QPointF &p2);
0037     /// Creates a new cubic segment
0038     KoPathSegment(const QPointF &p0, const QPointF &p1, const QPointF &p2, const QPointF &p3);
0039 
0040     /// Assigns segment
0041     KoPathSegment& operator=(const KoPathSegment &other);
0042 
0043     /// Destroys the path segment
0044     ~KoPathSegment();
0045 
0046     /// Returns the first point of the segment
0047     KoPathPoint *first() const;
0048 
0049     /// Sets the first segment point
0050     void setFirst(KoPathPoint *first);
0051 
0052     /// Returns the second point of the segment
0053     KoPathPoint *second() const;
0054 
0055     /// Sets the second segment point
0056     void setSecond(KoPathPoint *second);
0057 
0058     /// Returns if segment is valid, e.g. has two valid points
0059     bool isValid() const;
0060 
0061     /// Compare operator
0062     bool operator==(const KoPathSegment &other) const;
0063 
0064     /// Returns the degree of the segment: 1 = line, 2 = quadratic, 3 = cubic, -1 = invalid
0065     int degree() const;
0066 
0067     /// Returns list of intersections with the given path segment
0068     QList<QPointF> intersections(const KoPathSegment &segment) const;
0069 
0070     /// Returns the convex hull polygon of the segment
0071     QList<QPointF> convexHull() const;
0072 
0073     /// Splits segment at given position returning the two resulting segments
0074     QPair<KoPathSegment, KoPathSegment> splitAt(qreal t) const;
0075 
0076     /// Returns point at given t
0077     QPointF pointAt(qreal t) const;
0078 
0079     /// Returns the axis aligned tight bounding rect
0080     QRectF boundingRect() const;
0081 
0082     /// Returns the control point bounding rect
0083     QRectF controlPointRect() const;
0084 
0085     /// Returns transformed segment
0086     KoPathSegment mapped(const QTransform &matrix) const;
0087 
0088     /// Returns cubic bezier curve segment of this segment
0089     KoPathSegment toCubic() const;
0090 
0091     /**
0092      * Returns the length of the path segment
0093      * @param error the error tolerance
0094      */
0095     qreal length(qreal error = 0.005) const;
0096 
0097     /**
0098      * Returns segment length at given parameter
0099      *
0100      * Splits the segment at the given parameter t and calculates
0101      * the length of the first segment of the split.
0102      *
0103      * @param t curve parameter to get length at
0104      * @param error the error tolerance
0105      */
0106     qreal lengthAt(qreal t, qreal error = 0.005) const;
0107 
0108     /**
0109      * Returns the curve parameter at the given length of the segment
0110      *
0111      * If the specified length is negative it returns 0.0.
0112      * If the specified length is bigger that the actual length of the
0113      * segment it return 1.0.
0114      *
0115      * @param length the length to get the curve parameter for
0116      * @param tolerance the length error tolerance
0117      */
0118     qreal paramAtLength(qreal length, qreal tolerance = 0.001) const;
0119 
0120     /**
0121      * Checks if the segment is flat, i.e. the height smaller then the given tolerance
0122      * @param tolerance the flatness tolerance
0123      */
0124     bool isFlat(qreal tolerance = 0.01) const;
0125 
0126     /**
0127      * Returns the parameter for the curve point nearest to the given point
0128      * @param point the point to find nearest point to
0129      * @return the parameter of the curve point nearest to the given point
0130      */
0131     qreal nearestPoint(const QPointF &point) const;
0132 
0133     /// Returns ordered list of control points
0134     QList<QPointF> controlPoints() const;
0135 
0136     /**
0137      * Interpolates quadric bezier curve.
0138      * @return the interpolated bezier segment
0139      */
0140     static KoPathSegment interpolate(const QPointF &p0, const QPointF &p1, const QPointF &p2, qreal t);
0141 
0142 private:
0143     class Private;
0144     Private * const d;
0145 };
0146 
0147 #endif // KOPATHSEGMENT_H