Salome HOME
Do not allow user to move fixed entities (issue #1043)
[modules/shaper.git] / src / SketchSolver / SketchSolver_Storage.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Storage.h
4 // Created: 18 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #ifndef SketchSolver_Storage_H_
8 #define SketchSolver_Storage_H_
9
10 #include "SketchSolver.h"
11 #include <SketchSolver_Solver.h>
12
13 #include <list>
14 #include <memory>
15 #include <set>
16 #include <vector>
17
18 /** \class   SketchSolver_Storage
19  *  \ingroup Plugins
20  *  \brief   Contains all necessary data in SolveSpace format to solve a single group of constraints
21  */
22 class SketchSolver_Storage
23 {
24 public:
25   SketchSolver_Storage();
26
27   /** \brief Add new parameter to the current group
28    *  \param[in] theParam  SolveSpace parameter
29    *  \return the ID of added parameter
30    */
31   Slvs_hParam addParameter(const Slvs_Param& theParam);
32   /** \brief Updates parameter in the current group. If the ID of parameter is zero, the new item will be added
33    *  \param[in] theParam  SolveSpace parameter
34    *  \return the ID of updated/added parameter
35    */
36   Slvs_hParam updateParameter(const Slvs_Param& theParam);
37   /** \brief Removes the parameter by its ID
38    *  \param[in] theParamID  index of parameter to be removed
39    *  \return \c true if the parameter was successfully removed
40    */
41   bool removeParameter(const Slvs_hParam& theParamID);
42   /// \brief Returns the parameter by its ID
43   const Slvs_Param& getParameter(const Slvs_hParam& theParamID) const;
44
45   /** \brief Add new entity to the current group
46    *  \param[in] theEntity  SolveSpace entity
47    *  \return the ID of added entity
48    */
49   Slvs_hEntity addEntity(const Slvs_Entity& theEntity);
50   /** \brief Updates entity in the current group. If the ID of entity is zero, the new item will be added
51    *  \param[in] theEntity  SolveSpace entity
52    *  \return the ID of updated/added entity
53    */
54   Slvs_hEntity updateEntity(const Slvs_Entity& theEntity);
55   /** \brief Removes the entity by its ID. All parameters used in this entity,
56    *         and not used in other constraints, will be removed too.
57    *  \param[in] theEntityID  index of entity to be removed
58    *  \return \c true if the entity was successfully removed
59    */
60   bool removeEntity(const Slvs_hEntity& theEntityID);
61   /** \brief Remove all entities, which are not used in constraints
62    */
63   void removeUnusedEntities();
64   /// \brief Returns the entity by its ID
65   const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
66   /// \brief Makes a full copy of the given entity
67   Slvs_hEntity copyEntity(const Slvs_hEntity& theCopied);
68   /// \brief Copy one entity to another
69   void copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo);
70   /// \brief Check the entity is used in constraints
71   bool isUsedByConstraints(const Slvs_hEntity& theEntityID) const;
72   /// \brief Returns maximal ID of entities in this storage
73   const Slvs_hEntity& entityMaxID() const
74   { return myEntityMaxID; }
75
76   /// \brief Verifies the current point or another coincident one is fixed
77   /// \param[in]  thePointID  entity to be checked fixed
78   /// \param[out] theFixed    ID of constraint
79   /// \param[in]  theAccurate if \c true, the calculation will be made for all type of constraints,
80   ///                         if \c false, only the point is verified
81   /// \return \c true if the point is fixed
82   bool isPointFixed(const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate = false) const;
83   /// \brief Verifies the current entity is fully fixed (may not be changed by constraints)
84   /// \param[in] theEntityID entity to be checked fixed
85   /// \param[in] theAccurate if \c true, the calculation will be made for all type of constraints,
86   ///                        if \c false, only points are verified
87   /// \return \c true if the entity is fixed
88   bool isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate = false) const;
89
90   /** \brief Add new constraint to the current group
91    *  \param[in] theConstraint   SolveSpace's constraint
92    *  \return the ID of added constraint
93    */
94   Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
95   /** \brief Updates constraint in the current group.
96    *         If the ID of constraint is zero, the new item will be added
97    *  \param[in] theConstraint  SolveSpace constraint
98    *  \return the ID of updated/added constraint
99    */
100   Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
101   /** \brief Removes the constraint by its ID. All entities and parameters depending on this
102    *         constraint, which are not used in other constraints, will be removed too.
103    *  \param[in] theConstraintID  index of constraint to be removed
104    *  \return \c true if the constraint was successfully removed
105    */
106   bool removeConstraint(const Slvs_hConstraint& theConstraintID);
107   /// \brief Returns the constraint by its ID
108   const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
109   /// \brief Returns list of constraints of specified type
110   std::list<Slvs_Constraint> getConstraintsByType(int theConstraintType) const;
111   /// \brief Returns quantity of constraints in this storage
112   size_t nbConstraints() const
113   { return myConstraints.size(); }
114
115   /// \brief Attach constraint SLVS_C_WHERE_DRAGGED to this storage. It need to make precise calculations
116   void addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID);
117
118   /// \brief Add transient constraint
119   void addTemporaryConstraint(const Slvs_hConstraint& theConstraintID);
120   /// \brief Remove all transient constraints
121   void removeTemporaryConstraints();
122   /// \brief Remove one temporary constraint. Preferable to remove the points under Point-on-Line constraint
123   /// \return Number of remaining temporary constraints
124   int deleteTemporaryConstraint();
125   /// \brief Checks the constraint is temporary
126   bool isTemporary(const Slvs_hConstraint& theConstraintID) const;
127   /// \brief Number of temporary constraints
128   int numberTemporary() const
129   { return (int)myTemporaryConstraints.size(); }
130
131   /// \brief Shows the sketch should be resolved
132   bool isNeedToResolve();
133
134   /// \brief Shows the storage has the same constraint twice
135   bool hasDuplicatedConstraint() const
136   { return myDuplicatedConstraint; }
137
138   /// \brief Changes the flag of group to be resolved
139   void setNeedToResolve(bool theFlag)
140   { myNeedToResolve = theFlag; }
141
142   /// \brief Returns lists of removed elements
143   void getRemoved(std::set<Slvs_hParam>& theParameters,
144                   std::set<Slvs_hEntity>& theEntities,
145                   std::set<Slvs_hConstraint>& theConstraints);
146
147   /// \brief Initialize constraint solver by the entities collected by current storage
148   void initializeSolver(SketchSolver_Solver& theSolver);
149
150 private:
151   /// \brief Store coincident points
152   void addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
153   /// \brief Remove point from lists of coincidence
154   void removeCoincidentPoint(const Slvs_hEntity& thePoint);
155   /// \brief Remove point-point coincidence
156   void removeCoincidence(const Slvs_Constraint& theCoincidence);
157
158 public:
159   /// \brief Check two points are coincident
160   bool isCoincident(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
161
162   /// \brief Check two points are coincident or have same coordinates
163   bool isEqual(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
164
165   /// \brief Check the entity is horizontal of vertical
166   bool isAxisParallel(const Slvs_hEntity& theEntity) const;
167
168   /// \brief Verifies the entity is used in any equal constraint
169   /// \param[in]  theEntity entity to be found
170   /// \param[out] theEqual  constraint, which uses the entity
171   /// \return \c true, if the Equal constrait is found
172   bool isUsedInEqual(const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const;
173
174   /// \brief Fixes specified entity
175   /// \param theEntity  ID of the entity to be fixed
176   /// \return List of created constraints
177   std::vector<Slvs_hConstraint> fixEntity(const Slvs_hEntity& theEntity);
178
179 private:
180   /// \brief Fixes specified point
181   /// \param [in]  thePoint    point to be fixed
182   /// \param [out] theCreated  list of the Fixed constraints created
183   void fixPoint(const Slvs_Entity& thePoint, std::vector<Slvs_hConstraint>& theCreated);
184   /// \brief Fixes specified line
185   /// \param [in]  theLine     line to be fixed
186   /// \param [out] theCreated  list of the Fixed constraints created
187   void fixLine(const Slvs_Entity& theLine, std::vector<Slvs_hConstraint>& theCreated);
188   /// \brief Fixes specified circle
189   /// \param [in]  theCircle   circle to be fixed
190   /// \param [out] theCreated  list of the Fixed constraints created
191   void fixCircle(const Slvs_Entity& theCircle, std::vector<Slvs_hConstraint>& theCreated);
192   /// \brief Fixes specified arc
193   /// \param [in]  theArc      arc to be fixed
194   /// \param [out] theCreated  list of the Fixed constraints created
195   void fixArc(const Slvs_Entity& theArc, std::vector<Slvs_hConstraint>& theCreated);
196
197 private:
198   Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
199   std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
200   Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
201   std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
202   Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
203   std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
204
205   std::vector< std::set<Slvs_hEntity> > myCoincidentPoints; ///< lists of coincident points
206   Slvs_hConstraint myFixed; ///< identifier of one of temporary constraints to fix separate point
207
208   bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
209   bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
210
211   std::set<Slvs_hConstraint> myTemporaryConstraints; ///< list of transient constraints
212   std::set<Slvs_hParam> myRemovedParameters; ///< list of just removed parameters (cleared when returning to applicant)
213   std::set<Slvs_hEntity> myRemovedEntities; ///< list of just removed entities (cleared when returning to applicant)
214   std::set<Slvs_hConstraint> myRemovedConstraints; ///< list of just removed constraints (cleared when returning to applicant)
215   std::set<Slvs_hParam> myUpdatedParameters; ///< list of just updated parameters (cleared when isNeedToResolve() called)
216 };
217
218 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
219
220 #endif