Seamly2D
Code documentation
vcubicbezierpath.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file vcubicbezierpath.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 16 3, 2016
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2016 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "vcubicbezierpath.h"
53 
54 #include <QLineF>
55 #include <QPoint>
56 
57 #include "../ifc/exception/vexception.h"
58 #include "../vmisc/vmath.h"
59 #include "vabstractcurve.h"
60 #include "vcubicbezierpath_p.h"
61 #include "vspline.h"
62 #include "vsplinepoint.h"
63 
64 #ifdef Q_COMPILER_RVALUE_REFS
66 { Swap(curve); return *this; }
67 #endif
68 
69 void VCubicBezierPath::Swap(VCubicBezierPath &curve) Q_DECL_NOTHROW
70 { VAbstractCubicBezierPath::Swap(curve); std::swap(d, curve.d); }
71 
72 //---------------------------------------------------------------------------------------------------------------------
75  , d(new VCubicBezierPathData())
76 {
77 }
78 
79 //---------------------------------------------------------------------------------------------------------------------
82  , d(curve.d)
83 {
84 }
85 
86 //---------------------------------------------------------------------------------------------------------------------
87 VCubicBezierPath::VCubicBezierPath(const QVector<VPointF> &points, quint32 idObject, Draw mode)
89  , d(new VCubicBezierPathData())
90 {
91  if (points.isEmpty())
92  {
93  return;
94  }
95 
96  d->path = points;
97  CreateName();
98 }
99 
100 //---------------------------------------------------------------------------------------------------------------------
102 {
103  if ( &curve == this )
104  {
105  return *this;
106  }
108  d = curve.d;
109  return *this;
110 }
111 
112 //---------------------------------------------------------------------------------------------------------------------
113 VCubicBezierPath VCubicBezierPath::Rotate(const QPointF &originPoint, qreal degrees, const QString &prefix) const
114 {
115  const QVector<VPointF> points = GetCubicPath();
116  VCubicBezierPath curve;
117  for(int i=0; i < points.size(); ++i)
118  {
119  curve.append(points.at(i).Rotate(originPoint, degrees));
120  }
121  curve.setName(name() + prefix);
122  curve.setLineColor(getLineColor());
123  curve.SetPenStyle(GetPenStyle());
124  curve.setLineWeight(getLineWeight());
125  return curve;
126 }
127 
128 //---------------------------------------------------------------------------------------------------------------------
129 VCubicBezierPath VCubicBezierPath::Flip(const QLineF &axis, const QString &prefix) const
130 {
131  const QVector<VPointF> points = GetCubicPath();
132  VCubicBezierPath curve;
133  for(int i=0; i < points.size(); ++i)
134  {
135  curve.append(points.at(i).Flip(axis));
136  }
137  curve.setName(name() + prefix);
138  curve.setLineColor(getLineColor());
139  curve.SetPenStyle(GetPenStyle());
140  curve.setLineWeight(getLineWeight());
141  return curve;
142 }
143 
144 //---------------------------------------------------------------------------------------------------------------------
145 VCubicBezierPath VCubicBezierPath::Move(qreal length, qreal angle, const QString &prefix) const
146 {
147  const QVector<VPointF> points = GetCubicPath();
148  VCubicBezierPath curve;
149  for(int i=0; i < points.size(); ++i)
150  {
151  curve.append(points.at(i).Move(length, angle));
152  }
153  curve.setName(name() + prefix);
154  curve.setLineColor(getLineColor());
155  curve.SetPenStyle(GetPenStyle());
156  curve.setLineWeight(getLineWeight());
157  return curve;
158 }
159 
160 //---------------------------------------------------------------------------------------------------------------------
162 {
163 }
164 
165 //---------------------------------------------------------------------------------------------------------------------
167 {
168  return d->path[indx];
169 }
170 
171 //---------------------------------------------------------------------------------------------------------------------
172 const VPointF &VCubicBezierPath::at(int indx) const
173 {
174  return d->path[indx];
175 }
176 
177 //---------------------------------------------------------------------------------------------------------------------
179 {
180  d->path.append(point);
181  CreateName();
182 }
183 
184 //---------------------------------------------------------------------------------------------------------------------
186 {
187  return CountSubSpl(d->path.size());
188 }
189 
190 //---------------------------------------------------------------------------------------------------------------------
192 {
193  return d->path.size();
194 }
195 
196 //---------------------------------------------------------------------------------------------------------------------
198 {
199  d->path.clear();
200  SetDuplicate(0);
201 }
202 
203 //---------------------------------------------------------------------------------------------------------------------
205 {
206  if (CountPoints() < 4)
207  {
208  throw VException(tr("Not enough points to create the spline."));
209  }
210 
211  if (index < 1 || index > CountSubSpl())
212  {
213  throw VException(tr("This spline does not exist."));
214  }
215 
216  const qint32 base = SubSplOffset(index);
217 
218  // Correction the first control point of each next spline curve except for the first.
219  QPointF p2 = static_cast<QPointF>(d->path.at(base + 1));
220  if (base + 1 > 1)
221  {
222  const QPointF b = static_cast<QPointF>(d->path.at(base));
223  QLineF foot1(b, static_cast<QPointF>(d->path.at(base - 1)));
224  QLineF foot2(b, p2);
225 
226  foot2.setAngle(foot1.angle() + 180);
227  p2 = foot2.p2();
228  }
229 
230  VSpline spl(d->path.at(base), p2, static_cast<QPointF>(d->path.at(base + 2)), d->path.at(base + 3));
231  return spl;
232 }
233 
234 //---------------------------------------------------------------------------------------------------------------------
236 {
237  if (CountSubSpl() > 0)
238  {
239  return GetSpline(1).GetStartAngle();
240  }
241  else
242  {
243  return 0;
244  }
245 }
246 
247 //---------------------------------------------------------------------------------------------------------------------
249 {
250  const qint32 count = CountSubSpl();
251  if (count > 0)
252  {
253  return GetSpline(count).GetEndAngle();
254  }
255  else
256  {
257  return 0;
258  }
259 }
260 
261 //---------------------------------------------------------------------------------------------------------------------
263 {
264  if (CountSubSpl() > 0)
265  {
266  return GetSpline(1).GetC1Length();
267  }
268  else
269  {
270  return 0;
271  }
272 }
273 
274 //---------------------------------------------------------------------------------------------------------------------
276 {
277  const qint32 count = CountSubSpl();
278  if (count > 0)
279  {
280  return GetSpline(count).GetC2Length();
281  }
282  else
283  {
284  return 0;
285  }
286 }
287 
288 //---------------------------------------------------------------------------------------------------------------------
290 {
291  const int size = CountSubSpl();
292  QVector<VSplinePoint> splPoints(size+1);
293 
294  for (qint32 i = 1; i <= size; ++i)
295  {
296  const VSpline spl = GetSpline(i);
297 
298  {
299  VSplinePoint p = splPoints.at(i-1);
300  p.SetP(spl.GetP1());
302  p.SetLength2(spl.GetC1Length(), spl.GetC1LengthFormula());
303  splPoints[i-1] = p;
304  }
305 
306  {
307  VSplinePoint p = splPoints.at(i);
308  p.SetP(spl.GetP4());
309  p.SetAngle1(spl.GetEndAngle(), spl.GetEndAngleFormula());
310  p.SetLength1(spl.GetC2Length(), spl.GetC2LengthFormula());
311  splPoints[i] = p;
312  }
313  }
314  return splPoints;
315 }
316 
317 //---------------------------------------------------------------------------------------------------------------------
319 {
320  return d->path;
321 }
322 
323 //---------------------------------------------------------------------------------------------------------------------
324 qint32 VCubicBezierPath::CountSubSpl(qint32 size)
325 {
326  if (size <= 0)
327  {
328  return 0;
329  }
330  return qFloor(qAbs((size - 4) / 3.0 + 1));
331 }
332 
333 //---------------------------------------------------------------------------------------------------------------------
334 qint32 VCubicBezierPath::SubSplOffset(qint32 subSplIndex)
335 {
336  if (subSplIndex <= 0)
337  {
338  return -1;
339  }
340 
341  return (subSplIndex - 1) * 3;
342 }
343 
344 //---------------------------------------------------------------------------------------------------------------------
345 qint32 VCubicBezierPath::SubSplPointsCount(qint32 countSubSpl)
346 {
347  if (countSubSpl <= 0)
348  {
349  return 0;
350  }
351 
352  return ((countSubSpl - 1) * 3 + 4);
353 }
354 
355 //---------------------------------------------------------------------------------------------------------------------
357 {
358  if (not d->path.isEmpty())
359  {
360  return d->path.first();
361  }
362  else
363  {
364  return VPointF();
365  }
366 }
367 
368 //---------------------------------------------------------------------------------------------------------------------
370 {
371  const qint32 count = CountSubSpl();
372  if (count >= 1)
373  {
374  return d->path.at(SubSplOffset(count) + 3);// Take last point of the last real spline
375  }
376  else
377  {
378  return VPointF();
379  }
380 }
virtual void CreateName() Q_DECL_OVERRIDE
QString getLineWeight() const
getLineWeight return weight of the lines
void SetDuplicate(quint32 number)
void SetPenStyle(const QString &penStyle)
VAbstractCurve & operator=(const VAbstractCurve &curve)
void setLineColor(const QString &color)
void setLineWeight(const QString &lineWeight)
setLineWeight set weight of the lines
QString GetPenStyle() const
void Swap(VAbstractCurve &curve) Q_DECL_NOTHROW
QString getLineColor() const
virtual VPointF FirstPoint() const Q_DECL_OVERRIDE
void append(const VPointF &point)
static qint32 SubSplPointsCount(qint32 countSubSpl)
VCubicBezierPath(quint32 idObject=0, Draw mode=Draw::Calculation)
QVector< VPointF > GetCubicPath() const
virtual qint32 CountPoints() const Q_DECL_OVERRIDE
VCubicBezierPath & operator=(const VCubicBezierPath &curve)
virtual qreal GetC2Length() const Q_DECL_OVERRIDE
virtual qint32 CountSubSpl() const Q_DECL_OVERRIDE
virtual qreal GetC1Length() const Q_DECL_OVERRIDE
const VPointF & at(int indx) const
virtual VPointF LastPoint() const Q_DECL_OVERRIDE
VCubicBezierPath Flip(const QLineF &axis, const QString &prefix=QString()) const
virtual VSpline GetSpline(qint32 index) const Q_DECL_OVERRIDE
virtual QVector< VSplinePoint > GetSplinePath() const Q_DECL_OVERRIDE
QSharedDataPointer< VCubicBezierPathData > d
virtual qreal GetEndAngle() const Q_DECL_OVERRIDE
void Swap(VCubicBezierPath &curve) Q_DECL_NOTHROW
VCubicBezierPath Move(qreal length, qreal angle, const QString &prefix=QString()) const
virtual void Clear() Q_DECL_OVERRIDE
virtual qreal GetStartAngle() const Q_DECL_OVERRIDE
static qint32 SubSplOffset(qint32 subSplIndex)
VCubicBezierPath Rotate(const QPointF &originPoint, qreal degrees, const QString &prefix=QString()) const
VPointF & operator[](int indx)
The VException class parent for all exception. Could be use for abstract exception.
Definition: vexception.h:66
virtual QString name() const
name return name graphical object.
Definition: vgobject.cpp:148
void setName(const QString &name)
setName set name graphical object.
Definition: vgobject.cpp:158
The VPointF class keep data of point.
Definition: vpointf.h:75
The VSplinePoint class keep information about point in spline path. Each point have two angles and tw...
Definition: vsplinepoint.h:108
void SetAngle1(const qreal &value, const QString &angle1F)
void SetP(const VPointF &value)
void SetLength1(const qreal &value, const QString &length1F)
void SetLength2(const qreal &value, const QString &length2F)
void SetAngle2(const qreal &value, const QString &angle2F)
VSpline class that implements the spline.
Definition: vspline.h:75
virtual qreal GetC2Length() const Q_DECL_OVERRIDE
Definition: vspline.cpp:385
virtual VPointF GetP4() const Q_DECL_OVERRIDE
GetP4 return last spline point.
Definition: vspline.cpp:321
QString GetC1LengthFormula() const
Definition: vspline.cpp:391
virtual qreal GetStartAngle() const Q_DECL_OVERRIDE
GetAngle1 return first angle control line.
Definition: vspline.cpp:337
virtual VPointF GetP1() const Q_DECL_OVERRIDE
GetP1 return first spline point.
Definition: vspline.cpp:281
QString GetStartAngleFormula() const
Definition: vspline.cpp:353
QString GetEndAngleFormula() const
Definition: vspline.cpp:359
virtual qreal GetC1Length() const Q_DECL_OVERRIDE
Definition: vspline.cpp:379
QString GetC2LengthFormula() const
Definition: vspline.cpp:397
virtual qreal GetEndAngle() const Q_DECL_OVERRIDE
GetAngle2 return second angle control line.
Definition: vspline.cpp:347
GOType
Definition: vgeometrydef.h:56
@ CubicBezierPath
Draw
Definition: vgeometrydef.h:55