Salome HOME
SketchSolver performance improvement (issue #934)
[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
73   /// \brief Verifies the current point or another coincident one is fixed
74   /// \param[in]  thePointID  entity to be checked fixed
75   /// \param[out] theFixed    ID of constraint
76   /// \param[in]  theAccurate if \c true, the calculation will be made for all type of constraints,
77   ///                         if \c false, only the point is verified
78   /// \return \c true if the point is fixed
79   bool isPointFixed(const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate = false) const;
80   /// \brief Verifies the current entity is fully fixed (may not be changed by constraints)
81   /// \param[in] theEntityID entity to be checked fixed
82   /// \param[in] theAccurate if \c true, the calculation will be made for all type of constraints,
83   ///                        if \c false, only points are verified
84   /// \return \c true if the entity is fixed
85   bool isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate = false) const;
86
87   /** \brief Add new constraint to the current group
88    *  \param[in] theConstraint   SolveSpace's constraint
89    *  \return the ID of added constraint
90    */
91   Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
92   /** \brief Updates constraint in the current group.
93    *         If the ID of constraint is zero, the new item will be added
94    *  \param[in] theConstraint  SolveSpace constraint
95    *  \return the ID of updated/added constraint
96    */
97   Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
98   /** \brief Removes the constraint by its ID. All entities and parameters depending on this
99    *         constraint, which are not used in other constraints, will be removed too.
100    *  \param[in] theConstraintID  index of constraint to be removed
101    *  \return \c true if the constraint was successfully removed
102    */
103   bool removeConstraint(const Slvs_hConstraint& theConstraintID);
104   /// \brief Returns the constraint by its ID
105   const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
106   /// \brief Returns list of constraints of specified type
107   std::list<Slvs_Constraint> getConstraintsByType(int theConstraintType) const;
108
109   /// \brief Attach constraint SLVS_C_WHERE_DRAGGED to this storage. It need to make precise calculations
110   void addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID);
111
112   /// \brief Add transient constraint
113   void addTemporaryConstraint(const Slvs_hConstraint& theConstraintID);
114   /// \brief Remove all transient constraints
115   void removeTemporaryConstraints();
116   /// \brief Remove one temporary constraint. Preferable to remove the points under Point-on-Line constraint
117   /// \return Number of remaining temporary constraints
118   int deleteTemporaryConstraint();
119   /// \brief Checks the constraint is temporary
120   bool isTemporary(const Slvs_hConstraint& theConstraintID) const;
121   /// \brief Number of temporary constraints
122   int numberTemporary() const
123   { return (int)myTemporaryConstraints.size(); }
124
125   /// \brief Shows the sketch should be resolved
126   bool isNeedToResolve();
127
128   /// \brief Shows the storage has the same constraint twice
129   bool hasDuplicatedConstraint() const
130   { return myDuplicatedConstraint; }
131
132   /// \brief Changes the flag of group to be resolved
133   void setNeedToResolve(bool theFlag)
134   { myNeedToResolve = theFlag; }
135
136   /// \brief Returns lists of removed elements
137   void getRemoved(std::set<Slvs_hParam>& theParameters,
138                   std::set<Slvs_hEntity>& theEntities,
139                   std::set<Slvs_hConstraint>& theConstraints);
140
141   /// \brief Initialize constraint solver by the entities collected by current storage
142   void initializeSolver(SketchSolver_Solver& theSolver);
143
144 private:
145   /// \brief Store coincident points
146   void addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
147   /// \brief Remove point from lists of coincidence
148   void removeCoincidentPoint(const Slvs_hEntity& thePoint);
149
150 public:
151   /// \brief Check two points are coincident
152   bool isCoincident(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
153
154   /// \brief Check two points are coincident or have same coordinates
155   bool isEqual(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
156
157   /// \brief Check the entity is horizontal of vertical
158   bool isAxisParallel(const Slvs_hEntity& theEntity) const;
159
160   /// \brief Verifies the entity is used in any equal constraint
161   /// \param[in]  theEntity entity to be found
162   /// \param[out] theEqual  constraint, which uses the entity
163   /// \return \c true, if the Equal constrait is found
164   bool isUsedInEqual(const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const;
165
166   /// \brief Fixes specified entity
167   /// \param theEntity  ID of the entity to be fixed
168   /// \return List of created constraints
169   std::vector<Slvs_hConstraint> fixEntity(const Slvs_hEntity& theEntity);
170
171 private:
172   /// \brief Fixes specified point
173   /// \param [in]  thePoint    point to be fixed
174   /// \param [out] theCreated  list of the Fixed constraints created
175   void fixPoint(const Slvs_Entity& thePoint, std::vector<Slvs_hConstraint>& theCreated);
176   /// \brief Fixes specified line
177   /// \param [in]  theLine     line to be fixed
178   /// \param [out] theCreated  list of the Fixed constraints created
179   void fixLine(const Slvs_Entity& theLine, std::vector<Slvs_hConstraint>& theCreated);
180   /// \brief Fixes specified circle
181   /// \param [in]  theCircle   circle to be fixed
182   /// \param [out] theCreated  list of the Fixed constraints created
183   void fixCircle(const Slvs_Entity& theCircle, std::vector<Slvs_hConstraint>& theCreated);
184   /// \brief Fixes specified arc
185   /// \param [in]  theArc      arc to be fixed
186   /// \param [out] theCreated  list of the Fixed constraints created
187   void fixArc(const Slvs_Entity& theArc, std::vector<Slvs_hConstraint>& theCreated);
188
189 private:
190   Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
191   std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
192   Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
193   std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
194   Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
195   std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
196
197   std::vector< std::set<Slvs_hEntity> > myCoincidentPoints; ///< lists of coincident points
198   Slvs_hConstraint myFixed; ///< identifier of one of temporary constraints to fix separate point
199
200   bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
201   bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
202
203   std::set<Slvs_hConstraint> myTemporaryConstraints; ///< list of transient constraints
204   std::set<Slvs_hParam> myRemovedParameters; ///< list of just removed parameters (cleared when returning to applicant)
205   std::set<Slvs_hEntity> myRemovedEntities; ///< list of just removed entities (cleared when returning to applicant)
206   std::set<Slvs_hConstraint> myRemovedConstraints; ///< list of just removed constraints (cleared when returning to applicant)
207   std::set<Slvs_hParam> myUpdatedParameters; ///< list of just updated parameters (cleared when isNeedToResolve() called)
208 };
209
210 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
211
212 #endif