Salome HOME
Remove unused entities in sketch solver while changing number of copies in multi...
[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
71   /// \brief Verifies the current point or another coincident one is fixed
72   /// \param[in]  thePointID  entity to be checked fixed
73   /// \param[out] theFixed    ID of constraint
74   /// \param[in]  theAccurate if \c true, the calculation will be made for all type of constraints,
75   ///                         if \c false, only the point is verified
76   /// \return \c true if the point is fixed
77   bool isPointFixed(const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate = false) const;
78   /// \brief Verifies the current entity is fully fixed (may not be changed by constraints)
79   /// \param[in] theEntityID entity to be checked fixed
80   /// \param[in] theAccurate if \c true, the calculation will be made for all type of constraints,
81   ///                        if \c false, only points are verified
82   /// \return \c true if the entity is fixed
83   bool isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate = false) const;
84
85   /** \brief Add new constraint to the current group
86    *  \param[in] theConstraint   SolveSpace's constraint
87    *  \return the ID of added constraint
88    */
89   Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
90   /** \brief Updates constraint in the current group.
91    *         If the ID of constraint is zero, the new item will be added
92    *  \param[in] theConstraint  SolveSpace constraint
93    *  \return the ID of updated/added constraint
94    */
95   Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
96   /** \brief Removes the constraint by its ID. All entities and parameters depending on this
97    *         constraint, which are not used in other constraints, will be removed too.
98    *  \param[in] theConstraintID  index of constraint to be removed
99    *  \return \c true if the constraint was successfully removed
100    */
101   bool removeConstraint(const Slvs_hConstraint& theConstraintID);
102   /// \brief Returns the constraint by its ID
103   const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
104   /// \brief Returns list of constraints of specified type
105   std::list<Slvs_Constraint> getConstraintsByType(int theConstraintType) const;
106
107   /// \brief Attach constraint SLVS_C_WHERE_DRAGGED to this storage. It need to make precise calculations
108   void addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID);
109
110   /// \brief Add transient constraint
111   void addTemporaryConstraint(const Slvs_hConstraint& theConstraintID);
112   /// \brief Remove all transient constraints
113   void removeTemporaryConstraints();
114   /// \brief Remove one temporary constraint. Preferable to remove the points under Point-on-Line constraint
115   /// \return Number of remaining temporary constraints
116   int deleteTemporaryConstraint();
117   /// \brief Checks the constraint is temporary
118   bool isTemporary(const Slvs_hConstraint& theConstraintID) const;
119   /// \brief Number of temporary constraints
120   int numberTemporary() const
121   { return (int)myTemporaryConstraints.size(); }
122
123   /// \brief Shows the sketch should be resolved
124   bool isNeedToResolve() const
125   { return myNeedToResolve && !myConstraints.empty(); }
126
127   /// \brief Shows the storage has the same constraint twice
128   bool hasDuplicatedConstraint() const
129   { return myDuplicatedConstraint; }
130
131   /// \brief Changes the flag of group to be resolved
132   void setNeedToResolve(bool theFlag)
133   { myNeedToResolve = theFlag; }
134
135   /// \brief Returns lists of removed elements
136   void getRemoved(std::set<Slvs_hParam>& theParameters,
137                   std::set<Slvs_hEntity>& theEntities,
138                   std::set<Slvs_hConstraint>& theConstraints);
139
140   /// \brief Initialize constraint solver by the entities collected by current storage
141   void initializeSolver(SketchSolver_Solver& theSolver);
142
143 private:
144   /// \brief Store coincident points
145   void addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
146   /// \brief Remove point from lists of coincidence
147   void removeCoincidentPoint(const Slvs_hEntity& thePoint);
148
149 public:
150   /// \brief Check two points are coincident
151   bool isCoincident(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
152
153 private:
154   Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
155   std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
156   Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
157   std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
158   Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
159   std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
160
161   std::vector< std::set<Slvs_hEntity> > myCoincidentPoints; ///< lists of coincident points
162   Slvs_hConstraint myFixed; ///< identifier of one of temporary constraints to fix separate point
163
164   bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
165   bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
166
167   std::set<Slvs_hConstraint> myTemporaryConstraints; ///< list of transient constraints
168   std::set<Slvs_hParam> myRemovedParameters; ///< list of just removed parameters (cleared when returning to applicant)
169   std::set<Slvs_hEntity> myRemovedEntities; ///< list of just removed entities (cleared when returning to applicant)
170   std::set<Slvs_hConstraint> myRemovedConstraints; ///< list of just removed constraints (cleared when returning to applicant)
171 };
172
173 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
174
175 #endif