Seamly2D
Code documentation
vabstractpiece.h
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
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 3 11, 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 #ifndef VABSTRACTPIECE_H
53 #define VABSTRACTPIECE_H
54 
55 #include <QtGlobal>
56 #include <QSharedDataPointer>
57 #include <QPointF>
58 #include <QDebug>
59 
60 #include "../vmisc/diagnostic.h"
61 #include "../vmisc/def.h"
62 #include "../vgeometry/vgobject.h"
63 
64 template <class T> class QVector;
65 
66 class VAbstractPieceData;
67 
68 QT_WARNING_PUSH
69 QT_WARNING_DISABLE_GCC("-Weffc++")
70 QT_WARNING_DISABLE_GCC("-Wnon-virtual-dtor")
71 
72 /**
73  * @brief The VSAPoint class seam allowance point
74  */
75 class VSAPoint : public QPointF
76 {
77 public:
78  Q_DECL_CONSTEXPR VSAPoint();
79  Q_DECL_CONSTEXPR VSAPoint(qreal xpos, qreal ypos);
80  Q_DECL_CONSTEXPR explicit VSAPoint(const QPointF &p);
81 
82  Q_DECL_CONSTEXPR qreal GetSABefore() const;
83  qreal GetSABefore(qreal width) const;
84  void SetSABefore(qreal value);
85 
86  Q_DECL_CONSTEXPR qreal GetSAAfter() const;
87  qreal GetSAAfter(qreal width) const;
88  void SetSAAfter(qreal value);
89 
90  Q_DECL_CONSTEXPR PieceNodeAngle GetAngleType() const;
91  void SetAngleType(PieceNodeAngle value);
92 
93 private:
94  qreal m_before;
95  qreal m_after;
97 };
98 
99 Q_DECLARE_METATYPE(VSAPoint)
100 Q_DECLARE_TYPEINFO(VSAPoint, Q_MOVABLE_TYPE);
101 
102 //---------------------------------------------------------------------------------------------------------------------
103 Q_DECL_CONSTEXPR inline VSAPoint::VSAPoint()
104  : QPointF(),
105  m_before(-1),
106  m_after(-1),
107  m_angle(PieceNodeAngle::ByLength)
108 {}
109 
110 //---------------------------------------------------------------------------------------------------------------------
111 Q_DECL_CONSTEXPR inline VSAPoint::VSAPoint(qreal xpos, qreal ypos)
112  : QPointF(xpos, ypos),
113  m_before(-1),
114  m_after(-1),
115  m_angle(PieceNodeAngle::ByLength)
116 {}
117 
118 //---------------------------------------------------------------------------------------------------------------------
119 Q_DECL_CONSTEXPR inline VSAPoint::VSAPoint(const QPointF &p)
120  : QPointF(p),
121  m_before(-1),
122  m_after(-1),
123  m_angle(PieceNodeAngle::ByLength)
124 {}
125 
126 //---------------------------------------------------------------------------------------------------------------------
127 Q_DECL_CONSTEXPR inline qreal VSAPoint::GetSABefore() const
128 {
129  return m_before;
130 }
131 
132 //---------------------------------------------------------------------------------------------------------------------
133 inline void VSAPoint::SetSABefore(qreal value)
134 {
135  value < 0 ? m_before = -1 : m_before = value;
136 }
137 
138 //---------------------------------------------------------------------------------------------------------------------
139 Q_DECL_CONSTEXPR inline qreal VSAPoint::GetSAAfter() const
140 {
141  return m_after;
142 }
143 
144 //---------------------------------------------------------------------------------------------------------------------
145 inline void VSAPoint::SetSAAfter(qreal value)
146 {
147  value < 0 ? m_after = -1 : m_after = value;
148 }
149 
150 //---------------------------------------------------------------------------------------------------------------------
151 Q_DECL_CONSTEXPR inline PieceNodeAngle VSAPoint::GetAngleType() const
152 {
153  return m_angle;
154 }
155 
156 //---------------------------------------------------------------------------------------------------------------------
158 {
159  m_angle = value;
160 }
161 
163 
165 {
166 public:
167  VAbstractPiece();
168  VAbstractPiece(const VAbstractPiece &piece);
169  virtual ~VAbstractPiece();
170 
171  VAbstractPiece &operator=(const VAbstractPiece &piece);
172 #ifdef Q_COMPILER_RVALUE_REFS
173  VAbstractPiece &operator=(VAbstractPiece &&piece) Q_DECL_NOTHROW;
174 #endif
175 
176  void Swap(VAbstractPiece &piece) Q_DECL_NOTHROW;
177 
178  QString GetName() const;
179  void SetName(const QString &value);
180 
181  QString getColor() const;
182  void setColor(const QString &value);
183 
184  QString getFill() const;
185  void setFill(const QString &value);
186 
187  bool getLock() const;
188  void setLock(bool value);
189 
190  bool IsForbidFlipping() const;
191  void SetForbidFlipping(bool value);
192 
193  bool IsSeamAllowance() const;
194  void SetSeamAllowance(bool value);
195 
196  bool IsSeamAllowanceBuiltIn() const;
197  void SetSeamAllowanceBuiltIn(bool value);
198 
199  bool isHideSeamLine() const;
200  void setHideSeamLine(bool value);
201 
202  qreal GetSAWidth() const;
203  void SetSAWidth(qreal value);
204 
205  qreal GetMx() const;
206  void SetMx(qreal value);
207 
208  qreal GetMy() const;
209  void SetMy(qreal value);
210 
211  static QVector<QPointF> Equidistant(const QVector<VSAPoint> &points, qreal width);
212  static qreal sumTrapezoids(const QVector<QPointF> &points);
213  static bool isClockwise(const QVector<QPointF> &points);
214  static QVector<QPointF> CheckLoops(const QVector<QPointF> &points);
215  static QVector<QPointF> EkvPoint(const VSAPoint &p1Line1, const VSAPoint &p2Line1,
216  const VSAPoint &p1Line2, const VSAPoint &p2Line2, qreal width);
217  static QLineF createParallelLine(const VSAPoint &p1, const VSAPoint &p2, qreal width);
218  static QLineF createParallelLine(const QPointF &p1, const QPointF &p2, qreal width);
219 
220  template <class T>
221  static QVector<T> CorrectEquidistantPoints(const QVector<T> &points, bool removeFirstAndLast = true);
222 
223 protected:
224  template <class T>
225  static QVector<T> RemoveDublicates(const QVector<T> &points, bool removeFirstAndLast = true);
226  static qreal MaxLocalSA(const VSAPoint &p, qreal width);
227  static bool IsEkvPointOnLine(const QPointF &iPoint, const QPointF &prevPoint, const QPointF &nextPoint);
228  static bool IsEkvPointOnLine(const VSAPoint &iPoint, const VSAPoint &prevPoint, const VSAPoint &nextPoint);
229 
230 private:
231  QSharedDataPointer<VAbstractPieceData> d;
232 
233  static bool CheckIntersection(const QVector<QPointF> &points, int i, int iNext, int j, int jNext,
234  const QPointF &crossPoint);
235  static bool ParallelCrossPoint(const QLineF &line1, const QLineF &line2, QPointF &point);
236  static bool Crossing(const QVector<QPointF> &sub1, const QVector<QPointF> &sub2);
237  static QVector<QPointF> SubPath(const QVector<QPointF> &path, int startIndex, int endIndex);
238  static Q_DECL_CONSTEXPR qreal PointPosition(const QPointF &p, const QLineF &line);
239  static QVector<QPointF> AngleByLength(const QPointF &p2, const QPointF &sp1, const QPointF &sp2, const QPointF &sp3,
240  qreal width);
241  static QVector<QPointF> AngleByIntersection(const QPointF &p1, const QPointF &p2, const QPointF &p3,
242  const QPointF &sp1, const QPointF &sp2, const QPointF &sp3,
243  qreal width);
244  static QVector<QPointF> AngleByFirstSymmetry(const QPointF &p1, const QPointF &p2,
245  const QPointF &sp1, const QPointF &sp2, const QPointF &sp3,
246  qreal width);
247  static QVector<QPointF> AngleBySecondSymmetry(const QPointF &p2, const QPointF &p3,
248  const QPointF &sp1, const QPointF &sp2, const QPointF &sp3,
249  qreal width);
250  static QVector<QPointF> AngleByFirstRightAngle(const QPointF &p1, const QPointF &p2,
251  const QPointF &sp1, const QPointF &sp2, const QPointF &sp3,
252  qreal width);
253  static QVector<QPointF> AngleBySecondRightAngle(const QPointF &p2, const QPointF &p3,
254  const QPointF &sp1, const QPointF &sp2, const QPointF &sp3,
255  qreal width);
256 
257  static QPointF SingleParallelPoint(const QPointF &p1, const QPointF &p2, qreal angle, qreal width);
258  static QLineF BisectorLine(const QPointF &p1, const QPointF &p2, const QPointF &p3);
259  static qreal AngleBetweenBisectors(const QLineF &b1, const QLineF &b2);
260 };
261 
263 
264 //---------------------------------------------------------------------------------------------------------------------
265 /**
266  * @brief CorrectEquidistantPoints clear equivalent points and remove point on line from equdistant.
267  * @param points list of points equdistant.
268  * @return corrected list.
269  */
270 template <class T>
271 QVector<T> VAbstractPiece::CorrectEquidistantPoints(const QVector<T> &points, bool removeFirstAndLast)
272 {
273  if (points.size()<4)//Better don't check if only three points. We can destroy equidistant.
274  {
275  qDebug()<<"Only three points.";
276  return points;
277  }
278 
279  //Clear equivalent points
280  QVector<T> buf1 = RemoveDublicates(points, removeFirstAndLast);
281 
282  if (buf1.size()<3)
283  {
284  return buf1;
285  }
286 
287  int prev = -1;
288 
289  QVector<T> buf2;
290  //Remove point on line
291  for (qint32 i = 0; i < buf1.size(); ++i)
292  {// In this case we alwayse will have bounded intersection, so all is need is to check if point i is on line.
293  // Unfortunatelly QLineF::intersect can't be used in this case because of the floating-point accuraccy problem.
294  if (prev == -1)
295  {
296  i == 0 ? prev = buf1.size() - 1 : prev = i-1;
297  }
298 
299  int next = i+1;
300  if (i == buf1.size() - 1)
301  {
302  next = 0;
303  }
304 
305  const T &iPoint = buf1.at(i);
306  const T &prevPoint = buf1.at(prev);
307  const T &nextPoint = buf1.at(next);
308 
309  if (not (IsEkvPointOnLine(iPoint, prevPoint, nextPoint) && prevPoint == nextPoint/*not zigzag*/)
310  // If RemoveDublicates does not remove these points it is a valid case.
311  // Case where last point equal first point
312  || ((i == 0 || i == buf1.size() - 1) && (iPoint == prevPoint || iPoint == nextPoint)))
313  {
314  buf2.append(iPoint);
315  prev = -1;
316  }
317  }
318 
319  buf2 = RemoveDublicates(buf2, false);
320 
321  return buf2;
322 }
323 
324 //---------------------------------------------------------------------------------------------------------------------
325 template <class T>
326 QVector<T> VAbstractPiece::RemoveDublicates(const QVector<T> &points, bool removeFirstAndLast)
327 {
328  QVector<T> p = points;
329 
330  if (removeFirstAndLast)
331  {
332  if (not p.isEmpty() && p.size() > 1)
333  {
334  // Path can't be closed
335  // See issue #686
336  if ((qAbs(p.first().x() - p.last().x()) < VGObject::accuracyPointOnLine)
337  && (qAbs(p.first().y() - p.last().y()) < VGObject::accuracyPointOnLine))
338  {
339  p.removeLast();
340  }
341  }
342  }
343 
344  for (int i = 0; i < p.size()-1; ++i)
345  {
346  if ((qAbs(p.at(i).x() - p.at(i+1).x()) < VGObject::accuracyPointOnLine)
347  && (qAbs(p.at(i).y() - p.at(i+1).y()) < VGObject::accuracyPointOnLine))
348  {
349  if (not removeFirstAndLast && (i == p.size()-1))
350  {
351  continue;
352  }
353 
354  p.erase(p.begin() + i + 1);
355  --i;
356  continue;
357  }
358  }
359 
360  return p;
361 }
362 
363 #endif // VABSTRACTPIECE_H
static QVector< QPointF > AngleByFirstRightAngle(const QPointF &p1, const QPointF &p2, const QPointF &sp1, const QPointF &sp2, const QPointF &sp3, qreal width)
bool IsSeamAllowanceBuiltIn() const
bool IsSeamAllowance() const
bool isHideSeamLine() const
static bool isClockwise(const QVector< QPointF > &points)
void setFill(const QString &value)
static bool IsEkvPointOnLine(const QPointF &iPoint, const QPointF &prevPoint, const QPointF &nextPoint)
void SetMx(qreal value)
bool getLock() const
static QVector< QPointF > Equidistant(const QVector< VSAPoint > &points, qreal width)
qreal GetSAWidth() const
void setLock(bool value)
static bool ParallelCrossPoint(const QLineF &line1, const QLineF &line2, QPointF &point)
static QVector< QPointF > AngleBySecondRightAngle(const QPointF &p2, const QPointF &p3, const QPointF &sp1, const QPointF &sp2, const QPointF &sp3, qreal width)
QString getColor() const
static QLineF createParallelLine(const VSAPoint &p1, const VSAPoint &p2, qreal width)
static bool Crossing(const QVector< QPointF > &sub1, const QVector< QPointF > &sub2)
static Q_DECL_CONSTEXPR qreal PointPosition(const QPointF &p, const QLineF &line)
static qreal sumTrapezoids(const QVector< QPointF > &points)
qreal GetMx() const
static QVector< QPointF > AngleByLength(const QPointF &p2, const QPointF &sp1, const QPointF &sp2, const QPointF &sp3, qreal width)
QSharedDataPointer< VAbstractPieceData > d
void SetSAWidth(qreal value)
static QVector< QPointF > EkvPoint(const VSAPoint &p1Line1, const VSAPoint &p2Line1, const VSAPoint &p1Line2, const VSAPoint &p2Line2, qreal width)
EkvPoint return seam allowance points in place of intersection of two edges. Last points of two edges...
static QVector< QPointF > AngleBySecondSymmetry(const QPointF &p2, const QPointF &p3, const QPointF &sp1, const QPointF &sp2, const QPointF &sp3, qreal width)
void SetForbidFlipping(bool value)
static qreal AngleBetweenBisectors(const QLineF &b1, const QLineF &b2)
static QPointF SingleParallelPoint(const QPointF &p1, const QPointF &p2, qreal angle, qreal width)
void setColor(const QString &value)
static qreal MaxLocalSA(const VSAPoint &p, qreal width)
static QVector< QPointF > SubPath(const QVector< QPointF > &path, int startIndex, int endIndex)
void setHideSeamLine(bool value)
void SetName(const QString &value)
static QVector< T > RemoveDublicates(const QVector< T > &points, bool removeFirstAndLast=true)
void SetSeamAllowanceBuiltIn(bool value)
void Swap(VAbstractPiece &piece) Q_DECL_NOTHROW
void SetSeamAllowance(bool value)
virtual ~VAbstractPiece()
QString GetName() const
qreal GetMy() const
static QVector< QPointF > CheckLoops(const QVector< QPointF > &points)
CheckLoops seek and delete loops in equidistant.
static QVector< QPointF > AngleByIntersection(const QPointF &p1, const QPointF &p2, const QPointF &p3, const QPointF &sp1, const QPointF &sp2, const QPointF &sp3, qreal width)
QString getFill() const
static QVector< QPointF > AngleByFirstSymmetry(const QPointF &p1, const QPointF &p2, const QPointF &sp1, const QPointF &sp2, const QPointF &sp3, qreal width)
static QVector< T > CorrectEquidistantPoints(const QVector< T > &points, bool removeFirstAndLast=true)
CorrectEquidistantPoints clear equivalent points and remove point on line from equdistant.
bool IsForbidFlipping() const
void SetMy(qreal value)
static QLineF BisectorLine(const QPointF &p1, const QPointF &p2, const QPointF &p3)
static bool CheckIntersection(const QVector< QPointF > &points, int i, int iNext, int j, int jNext, const QPointF &crossPoint)
VAbstractPiece & operator=(const VAbstractPiece &piece)
static const double accuracyPointOnLine
Definition: vgobject.h:126
The VSAPoint class seam allowance point.
qreal m_after
PieceNodeAngle m_angle
Q_DECL_CONSTEXPR VSAPoint()
void SetAngleType(PieceNodeAngle value)
Q_DECL_CONSTEXPR PieceNodeAngle GetAngleType() const
Q_DECL_CONSTEXPR qreal GetSABefore() const
qreal m_before
void SetSABefore(qreal value)
Q_DECL_CONSTEXPR qreal GetSAAfter() const
void SetSAAfter(qreal value)
PieceNodeAngle
Definition: def.h:113
Q_DECLARE_TYPEINFO(VSAPoint, Q_MOVABLE_TYPE)