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