2014-03-18 15:55:06 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 CERN
|
2022-02-22 21:00:48 +00:00
|
|
|
* Copyright (C) 2020-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-12-27 00:41:04 +00:00
|
|
|
*
|
2014-03-18 15:55:06 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef EDIT_CONSTRAINTS_H_
|
|
|
|
#define EDIT_CONSTRAINTS_H_
|
|
|
|
|
|
|
|
#include <math/vector2d.h>
|
2022-02-22 21:00:48 +00:00
|
|
|
#include <tool/grid_helper.h>
|
2021-06-02 14:20:41 +00:00
|
|
|
#include <functional>
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
class EDIT_POINT;
|
|
|
|
class EDIT_LINE;
|
|
|
|
class EDIT_POINTS;
|
|
|
|
|
2020-12-03 19:53:39 +00:00
|
|
|
|
|
|
|
enum GRID_CONSTRAINT_TYPE
|
|
|
|
{
|
|
|
|
IGNORE_GRID,
|
|
|
|
SNAP_TO_GRID,
|
|
|
|
SNAP_BY_GRID // Keep it on grid if it started on grid (treat x and y independently)
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-02-22 21:00:48 +00:00
|
|
|
enum SNAP_CONSTRAINT_TYPE
|
|
|
|
{
|
|
|
|
IGNORE_SNAPS,
|
|
|
|
OBJECT_LAYERS,
|
|
|
|
ALL_LAYERS
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-18 15:55:06 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Describe constraints between two edit handles.
|
2014-03-18 15:55:06 +00:00
|
|
|
*
|
2020-12-27 00:41:04 +00:00
|
|
|
* After the constrained handle is changed, Apply() has to be called to fix its coordinates
|
|
|
|
* according to the implemented constraint.
|
2014-03-18 15:55:06 +00:00
|
|
|
*/
|
|
|
|
template<class EDIT_TYPE>
|
|
|
|
class EDIT_CONSTRAINT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aConstrained is EDIT_POINT to which the constraint is applied.
|
|
|
|
*/
|
2022-02-22 21:00:48 +00:00
|
|
|
EDIT_CONSTRAINT( EDIT_TYPE& aConstrained ) :
|
|
|
|
m_constrained( aConstrained )
|
|
|
|
{};
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
virtual ~EDIT_CONSTRAINT() {};
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Correct coordinates of the constrained edit handle.
|
2014-03-18 15:55:06 +00:00
|
|
|
*/
|
2022-02-22 21:00:48 +00:00
|
|
|
virtual void Apply( EDIT_TYPE& aHandle, const GRID_HELPER& aGrid ) = 0;
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* Correct coordinates of the constrained edit handle.
|
2014-03-18 15:55:06 +00:00
|
|
|
*/
|
2022-02-22 21:00:48 +00:00
|
|
|
void Apply( const GRID_HELPER& aGrid )
|
2014-03-18 15:55:06 +00:00
|
|
|
{
|
2022-02-22 21:00:48 +00:00
|
|
|
Apply( m_constrained, aGrid );
|
2014-03-18 15:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-12-27 00:41:04 +00:00
|
|
|
EDIT_TYPE& m_constrained; ///< Point that is constrained by rules implemented by Apply().
|
2014-03-18 15:55:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* #EDIT_CONSTRAINT that imposes a constraint that two points have to have the same X coordinate.
|
2014-03-18 15:55:06 +00:00
|
|
|
*/
|
|
|
|
class EC_VERTICAL : public EDIT_CONSTRAINT<EDIT_POINT>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aConstrained is the point that is put under constrain.
|
|
|
|
* @param aConstrainer is the point that is the source of the constrain.
|
|
|
|
*/
|
|
|
|
EC_VERTICAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
|
2022-02-22 21:00:48 +00:00
|
|
|
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
|
|
|
|
m_constrainer( aConstrainer )
|
2014-03-18 15:55:06 +00:00
|
|
|
{}
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< @copydoc EDIT_CONSTRAINT::Apply()
|
2022-02-22 21:00:48 +00:00
|
|
|
virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const EDIT_POINT& m_constrainer; ///< Point that imposes the constraint.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* #EDIT_CONSTRAINT that imposes a constraint that two points have to have the same Y coordinate.
|
2014-03-18 15:55:06 +00:00
|
|
|
*/
|
|
|
|
class EC_HORIZONTAL : public EDIT_CONSTRAINT<EDIT_POINT>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aConstrained is the point that is put under constrain.
|
|
|
|
* @param aConstrainer is the point that is the source of the constrain.
|
|
|
|
*/
|
|
|
|
EC_HORIZONTAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
|
2022-02-22 21:00:48 +00:00
|
|
|
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
|
|
|
|
m_constrainer( aConstrainer )
|
2014-03-18 15:55:06 +00:00
|
|
|
{}
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< @copydoc EDIT_CONSTRAINT::Apply()
|
2022-02-22 21:00:48 +00:00
|
|
|
virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const EDIT_POINT& m_constrainer; ///< Point that imposes the constraint.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* #EDIT_CONSTRAINT that imposes a constraint that two points have to be located at angle of 45
|
2014-04-02 07:31:35 +00:00
|
|
|
* degree multiplicity.
|
2014-03-18 15:55:06 +00:00
|
|
|
*/
|
|
|
|
class EC_45DEGREE : public EDIT_CONSTRAINT<EDIT_POINT>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aConstrained is the point that is put under constrain.
|
|
|
|
* @param aConstrainer is the point that is the source of the constrain.
|
|
|
|
*/
|
|
|
|
EC_45DEGREE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
|
2022-02-22 21:00:48 +00:00
|
|
|
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
|
|
|
|
m_constrainer( aConstrainer )
|
2014-03-18 15:55:06 +00:00
|
|
|
{}
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< @copydoc EDIT_CONSTRAINT::Apply()
|
2022-02-22 21:00:48 +00:00
|
|
|
virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const EDIT_POINT& m_constrainer; ///< Point that imposes the constraint.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* #EDIT_CONSTRAINT that imposes a constraint that a point has to lie on a line (determined
|
2014-03-18 15:55:06 +00:00
|
|
|
* by 2 points).
|
|
|
|
*/
|
|
|
|
class EC_LINE : public EDIT_CONSTRAINT<EDIT_POINT>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer );
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< @copydoc EDIT_CONSTRAINT::Apply()
|
2022-02-22 21:00:48 +00:00
|
|
|
virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const EDIT_POINT& m_constrainer; ///< Point that imposes the constraint.
|
|
|
|
VECTOR2I m_line; ///< Vector representing the constraining line.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* #EDIT_CONSTRAINT that imposes a constraint that a point has to lie on a circle.
|
2014-03-18 15:55:06 +00:00
|
|
|
*/
|
|
|
|
class EC_CIRCLE : public EDIT_CONSTRAINT<EDIT_POINT>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aConstrained is the point that is put under constrain.
|
2014-11-02 16:25:04 +00:00
|
|
|
* @param aCenter is the point that is the center of the circle.
|
|
|
|
* @param aEnd is the point that decides on the radius of the circle.
|
2014-03-18 15:55:06 +00:00
|
|
|
*/
|
|
|
|
EC_CIRCLE( EDIT_POINT& aConstrained, const EDIT_POINT& aCenter, const EDIT_POINT& aEnd ) :
|
2022-02-22 21:00:48 +00:00
|
|
|
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
|
|
|
|
m_center( aCenter ),
|
|
|
|
m_end( aEnd )
|
2014-03-18 15:55:06 +00:00
|
|
|
{}
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< @copydoc EDIT_CONSTRAINT::Apply()
|
2022-02-22 21:00:48 +00:00
|
|
|
virtual void Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid ) override;
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
private:
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Point that imposes the constraint (center of the circle).
|
2014-03-18 15:55:06 +00:00
|
|
|
const EDIT_POINT& m_center;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Point that imposes the constraint (decides on the radius of the circle).
|
2014-03-18 15:55:06 +00:00
|
|
|
const EDIT_POINT& m_end;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* #EDIT_CONSTRAINT for 3 segments: dragged and two adjacent ones, enforcing to keep their slopes
|
2014-03-18 15:55:06 +00:00
|
|
|
* and allows only to change ending points. Applied to zones.
|
|
|
|
*/
|
2014-04-02 07:31:35 +00:00
|
|
|
class EC_CONVERGING : public EDIT_CONSTRAINT<EDIT_LINE>
|
2014-03-18 15:55:06 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
EC_CONVERGING( EDIT_LINE& aLine, EDIT_POINTS& aPoints );
|
|
|
|
|
|
|
|
virtual ~EC_CONVERGING();
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< @copydoc EDIT_CONSTRAINT::Apply()
|
2022-02-22 21:00:48 +00:00
|
|
|
virtual void Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid ) override;
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
private:
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Constraint for origin side segment.
|
2014-03-18 15:55:06 +00:00
|
|
|
EDIT_CONSTRAINT<EDIT_POINT>* m_originSideConstraint;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Constraint for end side segment.
|
2014-03-18 15:55:06 +00:00
|
|
|
EDIT_CONSTRAINT<EDIT_POINT>* m_endSideConstraint;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Additional constraint, applied when at least two points are collinear. It is a pointer to
|
|
|
|
///< m_[origin/end]SideConstraint, so it should not be freed.
|
2014-03-18 15:55:06 +00:00
|
|
|
EDIT_CONSTRAINT<EDIT_POINT>* m_colinearConstraint;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< EDIT_POINTS instance that stores currently modified lines.
|
2014-03-18 15:55:06 +00:00
|
|
|
EDIT_POINTS& m_editPoints;
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< Vector that represents the initial direction of the dragged segment.
|
2014-03-18 15:55:06 +00:00
|
|
|
VECTOR2I m_draggedVector;
|
|
|
|
};
|
|
|
|
|
2014-04-02 13:57:21 +00:00
|
|
|
|
2020-08-27 17:31:53 +00:00
|
|
|
/**
|
2020-12-27 00:41:04 +00:00
|
|
|
* #EDIT_CONSTRAINT for a EDIT_LINE, that constrains the line to move perpendicular
|
2020-08-27 17:31:53 +00:00
|
|
|
* to the line itself.
|
|
|
|
*/
|
|
|
|
class EC_PERPLINE : public EDIT_CONSTRAINT<EDIT_LINE>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
EC_PERPLINE( EDIT_LINE& aLine );
|
|
|
|
|
|
|
|
virtual ~EC_PERPLINE()
|
|
|
|
{}
|
|
|
|
|
2020-12-27 00:41:04 +00:00
|
|
|
///< @copydoc EDIT_CONSTRAINT::Apply()
|
2022-02-22 21:00:48 +00:00
|
|
|
virtual void Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid ) override;
|
2020-08-27 17:31:53 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
VECTOR2I m_mid;
|
|
|
|
VECTOR2I m_line;
|
|
|
|
};
|
|
|
|
|
2014-03-18 15:55:06 +00:00
|
|
|
#endif /* EDIT_CONSTRAINTS_H_ */
|