1 // SPDX-FileCopyrightText: 2009 Petr Gajdos <pgajdos@suse.cz> and
2 // Maurizio Paolini <paolini@dmf.unicatt.it>
3 
4 // SPDX-License-Identifier: GPL-2.0-or-later
5 
6 #ifndef KIG_OBJECTS_BEZIER_IMP_H
7 #define KIG_OBJECTS_BEZIER_IMP_H
8 
9 #include "curve_imp.h"
10 
11 #include "object_imp.h"
12 #include "../misc/coordinate.h"
13 #include <vector>
14 
15 /**
16  * An ObjectImp representing polynomial Bézier Curve.
17  */
18 class BezierImp
19   : public CurveImp
20 {
21   uint mnpoints;
22   std::vector<Coordinate> mpoints;
23   Coordinate mcenterofmass;
24 
25   Coordinate deCasteljau( unsigned int m, unsigned int k, double p ) const;
26 
27 public:
28   typedef CurveImp Parent;
29   /**
30    * Returns the ObjectImpType representing the BezierImp type.
31    */
32   static const ObjectImpType* stype();
33   static const ObjectImpType* stype2();
34   static const ObjectImpType* stype3();
35 
36   /**
37    * Constructs a Bézier curve.
38    */
39   explicit BezierImp( const std::vector<Coordinate>& points );
40   ~BezierImp();
41   BezierImp* copy() const override;
42 
43   Coordinate attachPoint() const override;
44   ObjectImp* transform( const Transformation& ) const override;
45 
46   void draw( KigPainter& p ) const override;
47   bool contains( const Coordinate& p, int width, const KigWidget& ) const override;
48   bool inRect( const Rect& r, int width, const KigWidget& ) const override;
49   bool valid() const;
50   Rect surroundingRect() const override;
51 
52   const Coordinate getPoint( double param, const KigDocument& ) const override;
53   bool containsPoint( const Coordinate& p, const KigDocument& doc ) const override;
54   bool internalContainsPoint( const Coordinate& p, double threshold,
55                               const KigDocument& doc ) const;
56 
57   int numberOfProperties() const override;
58   const QByteArrayList properties() const override;
59   const QByteArrayList propertiesInternalNames() const override;
60   ObjectImp* property( int which, const KigDocument& w ) const override;
61   const char* iconForProperty( int which ) const override;
62   const ObjectImpType* impRequirementForProperty( int which ) const override;
63   bool isPropertyDefinedOnOrThroughThisImp( int which ) const override;
64 
65   const ObjectImpType* type() const override;
66   void visit( ObjectImpVisitor* vtor ) const override;
67 
68   /**
69    * Returns the vector with control points.
70    */
71   const std::vector<Coordinate> points() const;
72   /**
73    * Returns the center of mass of the control polygon.
74    */
75   const Coordinate centerOfMass() const;
76   /**
77    * Returns the number of control points.
78    */
79   uint npoints() const;
80 
81   bool equals( const ObjectImp& rhs ) const override;
82 };
83 
84 /**
85  * An ObjectImp representing a rational Bézier curve.
86  */
87 class RationalBezierImp
88   : public CurveImp
89 {
90   uint mnpoints;
91   std::vector<Coordinate> mpoints;
92   std::vector<double> mweights;
93   Coordinate mcenterofmass;
94 
95   Coordinate deCasteljauPoints( unsigned int m, unsigned int k, double p ) const;
96   double deCasteljauWeights( unsigned int m, unsigned int k, double p ) const;
97 
98 public:
99   typedef CurveImp Parent;
100   /**
101    * Returns the ObjectImpType representing the RationalBezierImp type.
102    */
103   static const ObjectImpType* stype();
104   static const ObjectImpType* stype2();
105   static const ObjectImpType* stype3();
106 
107   /**
108    * Constructs a rational Bézier curve.
109    */
110   RationalBezierImp( const std::vector<Coordinate>& points, const std::vector<double>& weights );
111   ~RationalBezierImp();
112   RationalBezierImp* copy() const override;
113 
114   Coordinate attachPoint() const override;
115   ObjectImp* transform( const Transformation& ) const override;
116 
117   void draw( KigPainter& p ) const override;
118   bool contains( const Coordinate& p, int width, const KigWidget& ) const override;
119   bool inRect( const Rect& r, int width, const KigWidget& ) const override;
120   bool valid() const;
121   Rect surroundingRect() const override;
122 
123   const Coordinate getPoint( double param, const KigDocument& ) const override;
124   bool containsPoint( const Coordinate& p, const KigDocument& doc ) const override;
125   bool internalContainsPoint( const Coordinate& p, double threshold,
126                               const KigDocument& doc ) const;
127 
128   int numberOfProperties() const override;
129   const QByteArrayList properties() const override;
130   const QByteArrayList propertiesInternalNames() const override;
131   ObjectImp* property( int which, const KigDocument& w ) const override;
132   const char* iconForProperty( int which ) const override;
133   const ObjectImpType* impRequirementForProperty( int which ) const override;
134   bool isPropertyDefinedOnOrThroughThisImp( int which ) const override;
135 
136   const ObjectImpType* type() const override;
137   void visit( ObjectImpVisitor* vtor ) const override;
138 
139   /**
140    * Returns the vector with control points.
141    */
142   const std::vector<Coordinate> points() const;
143   /**
144    * Returns the center of mass of the control polygon.
145    */
146   const Coordinate centerOfMass() const;
147   /**
148    * Returns the number of control points.
149    */
150   uint npoints() const;
151 
152   bool equals( const ObjectImp& rhs ) const override;
153 };
154 
155 
156 #endif
157