Salome HOME
Crash when searching duplicated constraints
[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 Update SketchPlugin features after resolving constraints
42   /// \param theFixedOnly [in]  if \c true the fixed points will be updated only
43   virtual void refresh(bool theFixedOnly = false) const;
44
45   /// \brief Check if some parameters or entities are returned
46   ///        to the current group after removing temporary constraints
47   virtual void verifyFixed();
48
49   /// \brief Mark two points as coincident
50   virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave);
51
52   /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
53   ///        shows the distance from the start point.
54   virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase, double theCoeff);
55
56 protected:
57   /// \brief Remove constraint
58   /// \return \c true if the constraint and all its parameters are removed successfully
59   virtual bool remove(ConstraintWrapperPtr theConstraint);
60   /// \brief Remove entity
61   /// \return \c true if the entity and all its parameters are removed successfully
62   virtual bool remove(EntityWrapperPtr theEntity);
63   /// \brief Remove parameter
64   /// \return \c true if the parameter has been removed
65   virtual bool remove(ParameterWrapperPtr theParameter);
66
67   /// \brief Remove point-point coincidence
68   bool removeCoincidence(ConstraintWrapperPtr theConstraint);
69
70   /// \brief Update the group for the given entity, its sub-entities and parameters
71   virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup);
72   /// \brief Update the group for the given parameter
73   virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup);
74
75
76 // =============   Own methods   =============
77 public:
78   /// \brief Obtain and store identifier of sketch
79   void storeWorkplane(EntityWrapperPtr theSketch);
80
81   /** \brief Add new parameter to the current group
82    *  \param[in] theParam  SolveSpace parameter
83    *  \return the ID of added parameter
84    */
85   Slvs_hParam addParameter(const Slvs_Param& theParam);
86   /** \brief Updates parameter in the current group. If the ID of parameter is zero, the new item will be added
87    *  \param[in] theParam  SolveSpace parameter
88    *  \return the ID of updated/added parameter
89    */
90   Slvs_hParam updateParameter(const Slvs_Param& theParam);
91   /** \brief Removes the parameter by its ID
92    *  \param[in] theParamID  index of parameter to be removed
93    *  \return \c true if the parameter was successfully removed
94    */
95   bool removeParameter(const Slvs_hParam& theParamID);
96   /// \brief Returns the parameter by its ID
97   const Slvs_Param& getParameter(const Slvs_hParam& theParamID) const;
98
99   /** \brief Add new entity to the current group
100    *  \param[in] theEntity  SolveSpace entity
101    *  \return the ID of added entity
102    */
103   Slvs_hEntity addEntity(const Slvs_Entity& theEntity);
104   /** \brief Updates entity in the current group. If the ID of entity is zero, the new item will be added
105    *  \param[in] theEntity  SolveSpace entity
106    *  \return the ID of updated/added entity
107    */
108   Slvs_hEntity updateEntity(const Slvs_Entity& theEntity);
109   /** \brief Removes the entity by its ID. All parameters used in this entity,
110    *         and not used in other constraints, will be removed too.
111    *  \param[in] theEntityID  index of entity to be removed
112    *  \return \c true if the entity was successfully removed
113    */
114   bool removeEntity(const Slvs_hEntity& theEntityID);
115   /** \brief Remove all entities, which are not used in constraints
116    */
117   void removeUnusedEntities();
118   /// \brief Returns the entity by its ID
119   const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
120   /// \brief Makes a full copy of the given entity
121   Slvs_hEntity copyEntity(const Slvs_hEntity& theCopied);
122   /// \brief Copy one entity to another
123   void copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo);
124   /// \brief Check the entity is used in constraints
125   bool isUsedByConstraints(const Slvs_hEntity& theEntityID) const;
126   /// \brief Returns maximal ID of entities in this storage
127   const Slvs_hEntity& entityMaxID() const
128   { return myEntityMaxID; }
129
130   /** \brief Add new constraint to the current group
131    *  \param[in] theConstraint   SolveSpace's constraint
132    *  \return the ID of added constraint
133    */
134   Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
135   /** \brief Updates constraint in the current group.
136    *         If the ID of constraint is zero, the new item will be added
137    *  \param[in] theConstraint  SolveSpace constraint
138    *  \return the ID of updated/added constraint
139    */
140   Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
141   /** \brief Removes the constraint by its ID. All entities and parameters depending on this
142    *         constraint, which are not used in other constraints, will be removed too.
143    *  \param[in] theConstraintID  index of constraint to be removed
144    *  \return \c true if the constraint was successfully removed
145    */
146   bool removeConstraint(const Slvs_hConstraint& theConstraintID);
147   /// \brief Returns the constraint by its ID
148   const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
149   /// \brief Returns quantity of constraints in this storage
150   size_t nbConstraints() const
151   { return myConstraints.size(); }
152
153   /// \brief Shows the storage has the same constraint twice
154   virtual bool hasDuplicatedConstraint() const
155   { return myDuplicatedConstraint; }
156
157   /// \brief Initialize constraint solver by the entities collected by current storage
158   virtual void initializeSolver(SolverPtr theSolver);
159
160 private:
161   /// \brief Update arc points to be on circle sharp.
162   void adjustArc(const Slvs_Entity& theArc);
163
164   /// \brief Replace sub-entity theSource in all features by theDest
165   void replaceInFeatures(EntityWrapperPtr theSource, EntityWrapperPtr theDest);
166   /// \brief Replace constrained entity theSource by theDest in all constraints;
167   void replaceInConstraints(EntityWrapperPtr theSource, EntityWrapperPtr theDest);
168
169   /// \brief Add pair of constraints which have same representation in SolveSpace notation.
170   ///
171   ///        These constraints may be different and become the same after the substitution
172   ///        of point coincidence.
173   void addSameConstraints(ConstraintWrapperPtr theConstraint1, ConstraintWrapperPtr theConstraint2);
174
175   /// \brief Search constraint equal to the given in terms of SolveSpace notation
176   bool findSameConstraint(ConstraintWrapperPtr theConstraint);
177
178 private:
179   Slvs_hEntity myWorkplaneID; ///< identifier of workplane
180
181   Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
182   std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
183   Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
184   std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
185   Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
186   std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
187
188   bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
189
190   std::set<Slvs_hParam> myUpdatedParameters; ///< list of just updated parameters (cleared when isNeedToResolve() called)
191
192   SameConstraintMap myEqualConstraints; ///< list of groups of equal constraints
193 };
194
195 #endif