Salome HOME
SketchSolver library refactoring
[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 <memory>
14 #include <set>
15 #include <vector>
16
17 /** \class   SketchSolver_Storage
18  *  \ingroup Plugins
19  *  \brief   Contains all necessary data in SolveSpace format to solve a single group of constraints
20  */
21 class SketchSolver_Storage
22 {
23 public:
24   SketchSolver_Storage();
25
26   /** \brief Add new parameter to the current group
27    *  \param[in] theParam  SolveSpace parameter
28    *  \return the ID of added parameter
29    */
30   Slvs_hParam addParameter(const Slvs_Param& theParam);
31   /** \brief Updates parameter in the current group. If the ID of parameter is zero, the new item will be added
32    *  \param[in] theParam  SolveSpace parameter
33    *  \return the ID of updated/added parameter
34    */
35   Slvs_hParam updateParameter(const Slvs_Param& theParam);
36   /** \brief Removes the parameter by its ID
37    *  \param[in] theParamID  index of parameter to be removed
38    *  \return \c true if the parameter was successfully removed
39    */
40   bool removeParameter(const Slvs_hParam& theParamID);
41   /// \brief Returns the parameter by its ID
42   const Slvs_Param& getParameter(const Slvs_hParam& theParamID) const;
43
44   /** \brief Add new entity to the current group
45    *  \param[in] theEntity  SolveSpace entity
46    *  \return the ID of added entity
47    */
48   Slvs_hEntity addEntity(const Slvs_Entity& theEntity);
49   /** \brief Updates entity in the current group. If the ID of entity is zero, the new item will be added
50    *  \param[in] theEntity  SolveSpace entity
51    *  \return the ID of updated/added entity
52    */
53   Slvs_hEntity updateEntity(const Slvs_Entity& theEntity);
54   /** \brief Removes the entity by its ID. All parameters used in this entity,
55    *         and not used in other constraints, will be removed too.
56    *  \param[in] theEntityID  index of entity to be removed
57    *  \return \c true if the entity was successfully removed
58    */
59   bool removeEntity(const Slvs_hEntity& theEntityID);
60   /// \brief Returns the entity by its ID
61   const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
62
63   /// \brief Verifies the current point or another coincident one is fixed
64   /// \return the ID of the Fixed constraint or SLVS_E_UNKNOWN
65   Slvs_hConstraint isPointFixed(const Slvs_hEntity& thePointID) const;
66
67   /** \brief Add new constraint to the current group
68    *  \param[in] theConstraint   SolveSpace's constraint
69    *  \return the ID of added constraint
70    */
71   Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
72   /** \brief Updates constraint in the current group.
73    *         If the ID of constraint is zero, the new item will be added
74    *  \param[in] theConstraint  SolveSpace constraint
75    *  \return the ID of updated/added constraint
76    */
77   Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
78   /** \brief Removes the constraint by its ID. All entities and parameters depending on this
79    *         constraint, which are not used in other constraints, will be removed too.
80    *  \param[in] theConstraintID  index of constraint to be removed
81    *  \return \c true if the constraint was successfully removed
82    */
83   bool removeConstraint(const Slvs_hConstraint& theConstraintID);
84   /// \brief Returns the constraint by its ID
85   const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
86
87   /// \brief Attach temporary constraint to this storage. It need to make precise calculations
88   void addTemporaryConstraint(const Slvs_hConstraint& theConstraintID);
89
90   /// \brief Shows the sketch should be resolved
91   bool isNeedToResolve() const
92   { return myNeedToResolve; }
93
94   /// \brief Changes the flag of group to be resolved
95   void setNeedToResolve(bool theFlag)
96   { myNeedToResolve = theFlag; }
97
98   /// \brief Returns lists of removed elements
99   void getRemoved(std::set<Slvs_hParam>& theParameters,
100                   std::set<Slvs_hEntity>& theEntities,
101                   std::set<Slvs_hConstraint>& theConstraints);
102
103   /// \brief Initialize constraint solver by the entities collected by current storage
104   void initializeSolver(SketchSolver_Solver& theSolver);
105
106 private:
107   /// \brief Store coincident points
108   void addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
109   /// \brief Remove point from lists of coincidence
110   void removeCoincidentPoint(const Slvs_hEntity& thePoint);
111
112 public:
113   /// \brief Check two points are coincident
114   bool isCoincident(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
115
116 private:
117   Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
118   std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
119   Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
120   std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
121   Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
122   std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
123
124   std::vector< std::set<Slvs_hEntity> > myCoincidentPoints; ///< lists of coincident points
125   Slvs_hConstraint myFixed; ///< identifier of one of temporary constraints to fix separate point
126
127   bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
128
129   std::set<Slvs_hParam> myRemovedParameters; ///< list of just removed parameters (cleared when returning to applicant)
130   std::set<Slvs_hEntity> myRemovedEntities; ///< list of just removed entities (cleared when returning to applicant)
131   std::set<Slvs_hConstraint> myRemovedConstraints; ///< list of just removed constraints (cleared when returning to applicant)
132 };
133
134 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
135
136 #endif