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