Salome HOME
First phase of SketchSolver refactoring
[modules/shaper.git] / src / SketchSolver / SolveSpaceSolver / SolveSpaceSolver_Storage.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SolveSpaceSolver_Storage.h
4 // Created: 18 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #ifndef SolveSpaceSolver_Storage_H_
8 #define SolveSpaceSolver_Storage_H_
9
10 #include <SketchSolver_Storage.h>
11 #include <SolveSpaceSolver_Solver.h>
12
13 #include <list>
14 #include <memory>
15 #include <set>
16 #include <vector>
17
18 typedef std::map<EntityWrapperPtr, std::set<EntityWrapperPtr> > CoincidentPointsMap;
19 typedef std::list< std::set<ConstraintWrapperPtr> >             SameConstraintMap;
20
21 /** \class   SolveSpaceSolver_Storage
22  *  \ingroup Plugins
23  *  \brief   Contains all necessary data in SolveSpace format to solve a single group of constraints
24  */
25 class SolveSpaceSolver_Storage : public SketchSolver_Storage
26 {
27 public:
28   SolveSpaceSolver_Storage(const GroupID& theGroup);
29
30 // =============   Inherited from SketchSolver_Storage   =============
31
32   /// \brief Update constraint's data
33   /// \return \c true if any value is updated
34   virtual bool update(ConstraintWrapperPtr& theConstraint);
35   /// \brief Update entity's data
36   /// \return \c true if any value is updated
37   virtual bool update(EntityWrapperPtr& theEntity);
38   /// \brief Update parameter's data
39   /// \return \c true if the value of parameter is updated
40   virtual bool update(ParameterWrapperPtr& theParameter);
41
42   /// \brief Removes constraint from the storage
43   /// \return \c true if the constraint and all its parameters are remove successfully
44   virtual bool removeConstraint(ConstraintPtr theConstraint);
45   /// \brief Removes feature from the storage
46   /// \return \c true if the feature and its attributes are removed successfully;
47   ///         \c false if the feature or any it attribute is used by remaining constraints.
48   virtual bool removeEntity(FeaturePtr theFeature);
49   /// \brief Removes attribute from the storage
50   /// \return \c true if the attribute is not used by remaining features and constraints
51   virtual bool removeEntity(AttributePtr theAttribute);
52
53   /// \brief Update SketchPlugin features after resolving constraints
54   /// \param theFixedOnly [in]  if \c true the fixed points will be updated only
55   virtual void refresh(bool theFixedOnly = false) const;
56
57   /// \brief Check if some parameters or entities are returned
58   ///        to the current group after removing temporary constraints
59   virtual void verifyFixed();
60
61   /// \brief Mark two points as coincident
62   virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave);
63
64   /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
65   ///        shows the distance from the start point.
66   virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase, double theCoeff);
67
68 protected:
69   /// \brief Remove constraint
70   /// \return \c true if the constraint and all its parameters are removed successfully
71   virtual bool remove(ConstraintWrapperPtr theConstraint);
72   /// \brief Remove entity
73   /// \return \c true if the entity and all its parameters are removed successfully
74   virtual bool remove(EntityWrapperPtr theEntity);
75   /// \brief Remove parameter
76   /// \return \c true if the parameter has been removed
77   virtual bool remove(ParameterWrapperPtr theParameter);
78
79   /// \brief Update the group for the given entity, its sub-entities and parameters
80   virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup);
81   /// \brief Update the group for the given parameter
82   virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup);
83
84
85 // =============   Own methods   =============
86 public:
87   /// \brief Obtain and store identifier of sketch
88   void storeWorkplane(EntityWrapperPtr theSketch);
89
90   /** \brief Add new parameter to the current group
91    *  \param[in] theParam  SolveSpace parameter
92    *  \return the ID of added parameter
93    */
94   Slvs_hParam addParameter(const Slvs_Param& theParam);
95   /** \brief Updates parameter in the current group. If the ID of parameter is zero, the new item will be added
96    *  \param[in] theParam  SolveSpace parameter
97    *  \return the ID of updated/added parameter
98    */
99   Slvs_hParam updateParameter(const Slvs_Param& theParam);
100   /** \brief Removes the parameter by its ID
101    *  \param[in] theParamID  index of parameter to be removed
102    *  \return \c true if the parameter was successfully removed
103    */
104   bool removeParameter(const Slvs_hParam& theParamID);
105   /// \brief Returns the parameter by its ID
106   const Slvs_Param& getParameter(const Slvs_hParam& theParamID) const;
107
108   /** \brief Add new entity to the current group
109    *  \param[in] theEntity  SolveSpace entity
110    *  \return the ID of added entity
111    */
112   Slvs_hEntity addEntity(const Slvs_Entity& theEntity);
113   /** \brief Updates entity in the current group. If the ID of entity is zero, the new item will be added
114    *  \param[in] theEntity  SolveSpace entity
115    *  \return the ID of updated/added entity
116    */
117   Slvs_hEntity updateEntity(const Slvs_Entity& theEntity);
118   /** \brief Removes the entity by its ID. All parameters used in this entity,
119    *         and not used in other constraints, will be removed too.
120    *  \param[in] theEntityID  index of entity to be removed
121    *  \return \c true if the entity was successfully removed
122    */
123   bool removeEntity(const Slvs_hEntity& theEntityID);
124   /** \brief Remove all entities, which are not used in constraints
125    */
126   void removeUnusedEntities();
127   /// \brief Returns the entity by its ID
128   const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
129   /// \brief Makes a full copy of the given entity
130   Slvs_hEntity copyEntity(const Slvs_hEntity& theCopied);
131   /// \brief Copy one entity to another
132   void copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo);
133   /// \brief Check the entity is used in constraints
134   bool isUsedByConstraints(const Slvs_hEntity& theEntityID) const;
135   /// \brief Returns maximal ID of entities in this storage
136   const Slvs_hEntity& entityMaxID() const
137   { return myEntityMaxID; }
138
139   /// \brief Verifies the current point or another coincident one is fixed
140   /// \param[in]  thePointID  entity to be checked fixed
141   /// \param[out] theFixed    ID of constraint
142   /// \param[in]  theAccurate if \c true, the calculation will be made for all type of constraints,
143   ///                         if \c false, only the point is verified
144   /// \return \c true if the point is fixed
145   bool isPointFixed(const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate = false) const;
146   /// \brief Verifies the current entity is fully fixed (may not be changed by constraints)
147   /// \param[in] theEntityID entity to be checked fixed
148   /// \param[in] theAccurate if \c true, the calculation will be made for all type of constraints,
149   ///                        if \c false, only points are verified
150   /// \return \c true if the entity is fixed
151   bool isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate = false) const;
152
153   /** \brief Add new constraint to the current group
154    *  \param[in] theConstraint   SolveSpace's constraint
155    *  \return the ID of added constraint
156    */
157   Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
158   /** \brief Updates constraint in the current group.
159    *         If the ID of constraint is zero, the new item will be added
160    *  \param[in] theConstraint  SolveSpace constraint
161    *  \return the ID of updated/added constraint
162    */
163   Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
164   /** \brief Removes the constraint by its ID. All entities and parameters depending on this
165    *         constraint, which are not used in other constraints, will be removed too.
166    *  \param[in] theConstraintID  index of constraint to be removed
167    *  \return \c true if the constraint was successfully removed
168    */
169   bool removeConstraint(const Slvs_hConstraint& theConstraintID);
170   /// \brief Returns the constraint by its ID
171   const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
172   /// \brief Returns list of constraints of specified type
173   std::list<Slvs_Constraint> getConstraintsByType(int theConstraintType) const;
174   /// \brief Returns quantity of constraints in this storage
175   size_t nbConstraints() const
176   { return myConstraints.size(); }
177
178   /// \brief Attach constraint SLVS_C_WHERE_DRAGGED to this storage. It need to make precise calculations
179   void addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID);
180
181   /// \brief Add transient constraint
182   void addTemporaryConstraint(const Slvs_hConstraint& theConstraintID);
183   /// \brief Mark specified constraint as temporary
184   virtual void setTemporary(ConstraintPtr theConstraint);
185   /// \brief Remove all transient constraints
186   void removeAllTemporary();
187   /// \brief Remove temporary constraint s. Preferable to remove the points under Point-on-Line constraint
188   /// \param theNbConstraints [in]  number of temporary constraints to be deleted
189   /// \return Number of remaining temporary constraints
190   virtual size_t removeTemporary(size_t theNbConstraints);
191   /// \brief Checks the constraint is temporary
192   bool isTemporary(const Slvs_hConstraint& theConstraintID) const;
193   /// \brief Number of temporary constraints
194   virtual size_t nbTemporary() const
195   { return myTemporaryConstraints.size(); }
196
197   /// \brief Shows the storage has the same constraint twice
198   virtual bool hasDuplicatedConstraint() const
199   { return myDuplicatedConstraint; }
200
201 ////  /// \brief Returns lists of removed elements
202 ////  void getRemoved(std::set<Slvs_hParam>& theParameters,
203 ////                  std::set<Slvs_hEntity>& theEntities,
204 ////                  std::set<Slvs_hConstraint>& theConstraints);
205
206 ////  /// \brief Shows the sketch should be resolved
207 ////  virtual bool isNeedToResolve();
208
209   /// \brief Initialize constraint solver by the entities collected by current storage
210   virtual void initializeSolver(SolverPtr theSolver);
211
212 ////private:
213 ////  /// \brief Store coincident points
214 ////  void addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
215 ////  /// \brief Remove point from lists of coincidence
216 ////  void removeCoincidentPoint(const Slvs_hEntity& thePoint);
217 ////  /// \brief Remove point-point coincidence
218 ////  void removeCoincidence(const Slvs_Constraint& theCoincidence);
219
220 public:
221 ////  /// \brief Check two points are coincident
222 ////  bool isCoincident(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
223
224   /// \brief Check two points are coincident or have same coordinates
225   bool isEqual(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
226
227   /// \brief Check the entity is horizontal of vertical
228   bool isAxisParallel(const Slvs_hEntity& theEntity) const;
229
230   /// \brief Verifies the entity is used in any equal constraint
231   /// \param[in]  theEntity entity to be found
232   /// \param[out] theEqual  constraint, which uses the entity
233   /// \return \c true, if the Equal constrait is found
234   bool isUsedInEqual(const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const;
235
236   /// \brief Fixes specified entity
237   /// \param theEntity  ID of the entity to be fixed
238   /// \return List of created constraints
239   std::vector<Slvs_hConstraint> fixEntity(const Slvs_hEntity& theEntity);
240
241 private:
242   /// \brief Fixes specified point
243   /// \param [in]  thePoint    point to be fixed
244   /// \param [out] theCreated  list of the Fixed constraints created
245   void fixPoint(const Slvs_Entity& thePoint, std::vector<Slvs_hConstraint>& theCreated);
246   /// \brief Fixes specified line
247   /// \param [in]  theLine     line to be fixed
248   /// \param [out] theCreated  list of the Fixed constraints created
249   void fixLine(const Slvs_Entity& theLine, std::vector<Slvs_hConstraint>& theCreated);
250   /// \brief Fixes specified circle
251   /// \param [in]  theCircle   circle to be fixed
252   /// \param [out] theCreated  list of the Fixed constraints created
253   void fixCircle(const Slvs_Entity& theCircle, std::vector<Slvs_hConstraint>& theCreated);
254   /// \brief Fixes specified arc
255   /// \param [in]  theArc      arc to be fixed
256   /// \param [out] theCreated  list of the Fixed constraints created
257   void fixArc(const Slvs_Entity& theArc, std::vector<Slvs_hConstraint>& theCreated);
258
259   /// \brief Update arc points to be on circle sharp.
260   void adjustArc(const Slvs_Entity& theArc);
261
262   /// \brief Verify the feature or any its attribute is used by constraint
263   bool isUsed(FeaturePtr theFeature) const;
264   /// \brief Verify the attribute is used by constraint
265   bool isUsed(AttributePtr theAttirubute) const;
266
267   /// \brief Replace sub-entity theSource in all features by theDest
268   void replaceInFeatures(EntityWrapperPtr theSource, EntityWrapperPtr theDest);
269   /// \brief Replace constrained entity theSource by theDest in all constraints;
270   void replaceInConstraints(EntityWrapperPtr theSource, EntityWrapperPtr theDest);
271
272   /// \brief Add pair of constraints which have same representation in SolveSpace notation.
273   ///
274   ///        These constraints may be different and become the same after the substitution
275   ///        of point coincidence.
276   void addSameConstraints(ConstraintWrapperPtr theConstraint1, ConstraintWrapperPtr theConstraint2);
277
278 private:
279   Slvs_hEntity myWorkplaneID; ///< identifier of workplane
280
281   Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
282   std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
283   Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
284   std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
285   Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
286   std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
287
288   CoincidentPointsMap myCoincidentPoints; ///< lists of coincident points (first is a master point, second is a set of slaves)
289   Slvs_hConstraint myFixed; ///< identifier of one of temporary constraints to fix separate point
290
291   bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
292
293   std::set<Slvs_hConstraint> myTemporaryConstraints; ///< list of transient constraints
294 ////  std::set<Slvs_hParam> myRemovedParameters; ///< list of just removed parameters (cleared when returning to applicant)
295 ////  std::set<Slvs_hEntity> myRemovedEntities; ///< list of just removed entities (cleared when returning to applicant)
296 ////  std::set<Slvs_hConstraint> myRemovedConstraints; ///< list of just removed constraints (cleared when returning to applicant)
297   std::set<Slvs_hParam> myUpdatedParameters; ///< list of just updated parameters (cleared when isNeedToResolve() called)
298
299   SameConstraintMap myEqualConstraints; ///< list of groups of equal constraints
300 };
301
302 #endif