You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
7.6 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014 CERN
  5. * @author Maciej Suminski <maciej.suminski@cern.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef EDIT_CONSTRAINTS_H_
  25. #define EDIT_CONSTRAINTS_H_
  26. #include <math/vector2d.h>
  27. #include <boost/function.hpp>
  28. class EDIT_POINT;
  29. class EDIT_LINE;
  30. class EDIT_POINTS;
  31. /**
  32. * Class EDIT_CONSTRAINT
  33. *
  34. * Allows to describe constraints between two edit handles. After the constrained handle is changed,
  35. * Apply() has to be called to fix its coordinates according to the implemented constraint.
  36. */
  37. template<class EDIT_TYPE>
  38. class EDIT_CONSTRAINT
  39. {
  40. public:
  41. /**
  42. * Constructor
  43. *
  44. * @param aConstrained is EDIT_POINT to which the constraint is applied.
  45. */
  46. EDIT_CONSTRAINT( EDIT_TYPE& aConstrained ) : m_constrained( aConstrained ) {};
  47. virtual ~EDIT_CONSTRAINT() {};
  48. /**
  49. * Function Apply()
  50. *
  51. * Corrects coordinates of the constrained edit handle.
  52. */
  53. virtual void Apply( EDIT_TYPE& aHandle ) = 0;
  54. /**
  55. * Function Apply()
  56. *
  57. * Corrects coordinates of the constrained edit handle.
  58. */
  59. void Apply()
  60. {
  61. Apply( m_constrained );
  62. }
  63. protected:
  64. EDIT_TYPE& m_constrained; ///< Point that is constrained by rules implemented by Apply()
  65. };
  66. /**
  67. * Class EC_VERTICAL.
  68. *
  69. * EDIT_CONSTRAINT that imposes a constraint that two points have to have the same X coordinate.
  70. */
  71. class EC_VERTICAL : public EDIT_CONSTRAINT<EDIT_POINT>
  72. {
  73. public:
  74. /**
  75. * Constructor.
  76. *
  77. * @param aConstrained is the point that is put under constrain.
  78. * @param aConstrainer is the point that is the source of the constrain.
  79. */
  80. EC_VERTICAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
  81. EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
  82. {}
  83. ///> @copydoc EDIT_CONSTRAINT::Apply()
  84. virtual void Apply( EDIT_POINT& aHandle );
  85. private:
  86. const EDIT_POINT& m_constrainer; ///< Point that imposes the constraint.
  87. };
  88. /**
  89. * Class EC_HORIZONTAL.
  90. *
  91. * EDIT_CONSTRAINT that imposes a constraint that two points have to have the same Y coordinate.
  92. */
  93. class EC_HORIZONTAL : public EDIT_CONSTRAINT<EDIT_POINT>
  94. {
  95. public:
  96. /**
  97. * Constructor.
  98. *
  99. * @param aConstrained is the point that is put under constrain.
  100. * @param aConstrainer is the point that is the source of the constrain.
  101. */
  102. EC_HORIZONTAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
  103. EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
  104. {}
  105. ///> @copydoc EDIT_CONSTRAINT::Apply()
  106. virtual void Apply( EDIT_POINT& aHandle );
  107. private:
  108. const EDIT_POINT& m_constrainer; ///< Point that imposes the constraint.
  109. };
  110. /**
  111. * Class EC_45DEGREE
  112. *
  113. * EDIT_CONSTRAINT that imposes a constraint that two points have to be located at angle of 45
  114. * degree multiplicity.
  115. */
  116. class EC_45DEGREE : public EDIT_CONSTRAINT<EDIT_POINT>
  117. {
  118. public:
  119. /**
  120. * Constructor.
  121. *
  122. * @param aConstrained is the point that is put under constrain.
  123. * @param aConstrainer is the point that is the source of the constrain.
  124. */
  125. EC_45DEGREE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
  126. EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
  127. {}
  128. ///> @copydoc EDIT_CONSTRAINT::Apply()
  129. virtual void Apply( EDIT_POINT& aHandle );
  130. private:
  131. const EDIT_POINT& m_constrainer; ///< Point that imposes the constraint.
  132. };
  133. /**
  134. * Class EC_LINE
  135. *
  136. * EDIT_CONSTRAINT that imposes a constraint that a point has to lie on a line (determined
  137. * by 2 points).
  138. */
  139. class EC_LINE : public EDIT_CONSTRAINT<EDIT_POINT>
  140. {
  141. public:
  142. EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer );
  143. ///> @copydoc EDIT_CONSTRAINT::Apply()
  144. virtual void Apply( EDIT_POINT& aHandle );
  145. private:
  146. const EDIT_POINT& m_constrainer; ///< Point that imposes the constraint.
  147. VECTOR2I m_line; ///< Vector representing the constraining line.
  148. };
  149. /**
  150. * Class EC_CIRCLE.
  151. *
  152. * EDIT_CONSTRAINT that imposes a constraint that a point has to lie on a circle.
  153. */
  154. class EC_CIRCLE : public EDIT_CONSTRAINT<EDIT_POINT>
  155. {
  156. public:
  157. /**
  158. * Constructor.
  159. *
  160. * @param aConstrained is the point that is put under constrain.
  161. * @param aCenter is the point that is the center of the circle.
  162. * @param aEnd is the point that decides on the radius of the circle.
  163. */
  164. EC_CIRCLE( EDIT_POINT& aConstrained, const EDIT_POINT& aCenter, const EDIT_POINT& aEnd ) :
  165. EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_center( aCenter ), m_end( aEnd )
  166. {}
  167. ///> @copydoc EDIT_CONSTRAINT::Apply()
  168. virtual void Apply( EDIT_POINT& aHandle );
  169. private:
  170. ///> Point that imposes the constraint (center of the circle).
  171. const EDIT_POINT& m_center;
  172. ///> Point that imposes the constraint (decides on the radius of the circle).
  173. const EDIT_POINT& m_end;
  174. };
  175. /**
  176. * Class EC_CONVERGING
  177. *
  178. * EDIT_CONSTRAINT for 3 segments: dragged and two adjacent ones, enforcing to keep their slopes
  179. * and allows only to change ending points. Applied to zones.
  180. */
  181. class EC_CONVERGING : public EDIT_CONSTRAINT<EDIT_LINE>
  182. {
  183. public:
  184. EC_CONVERGING( EDIT_LINE& aLine, EDIT_POINTS& aPoints );
  185. virtual ~EC_CONVERGING();
  186. ///> @copydoc EDIT_CONSTRAINT::Apply()
  187. virtual void Apply( EDIT_LINE& aHandle );
  188. private:
  189. ///> Constraint for origin side segment.
  190. EDIT_CONSTRAINT<EDIT_POINT>* m_originSideConstraint;
  191. ///> Constraint for end side segment.
  192. EDIT_CONSTRAINT<EDIT_POINT>* m_endSideConstraint;
  193. ///> Additional constriant, applied when at least two points are collinear. It is a pointer to
  194. ///> m_[origin/end]SideConstraint, so it should not be freed.
  195. EDIT_CONSTRAINT<EDIT_POINT>* m_colinearConstraint;
  196. ///> EDIT_POINTS instance that stores currently modified lines.
  197. EDIT_POINTS& m_editPoints;
  198. ///> Vector that represents the initial direction of the dragged segment.
  199. VECTOR2I m_draggedVector;
  200. };
  201. /**
  202. * Class EC_SNAPLINE
  203. *
  204. * EDIT_CONSTRAINT for a EDIT_LINE, one of the ends is snapped to a spot determined by a
  205. * transform function passed as parameter (e.g. it can be snapped to a grid), instead of having
  206. * the line center snapped to a point.
  207. */
  208. class EC_SNAPLINE : public EDIT_CONSTRAINT<EDIT_LINE>
  209. {
  210. public:
  211. ///> Typedef for a function that determines snapping point.
  212. typedef boost::function<VECTOR2D (const VECTOR2D&)> V2D_TRANSFORM_FUN;
  213. EC_SNAPLINE( EDIT_LINE& aLine, V2D_TRANSFORM_FUN aSnapFun );
  214. virtual ~EC_SNAPLINE()
  215. {}
  216. ///> @copydoc EDIT_CONSTRAINT::Apply()
  217. virtual void Apply( EDIT_LINE& aHandle );
  218. private:
  219. ///> Function that determines snapping point.
  220. V2D_TRANSFORM_FUN m_snapFun;
  221. };
  222. #endif /* EDIT_CONSTRAINTS_H_ */