Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[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 Returns the entity by its ID
62   const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
63
64   /// \brief Verifies the current point or another coincident one is fixed
65   /// \return the ID of the Fixed constraint or SLVS_E_UNKNOWN
66   Slvs_hConstraint isPointFixed(const Slvs_hEntity& thePointID) const;
67
68   /** \brief Add new constraint to the current group
69    *  \param[in] theConstraint   SolveSpace's constraint
70    *  \return the ID of added constraint
71    */
72   Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
73   /** \brief Updates constraint in the current group.
74    *         If the ID of constraint is zero, the new item will be added
75    *  \param[in] theConstraint  SolveSpace constraint
76    *  \return the ID of updated/added constraint
77    */
78   Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
79   /** \brief Removes the constraint by its ID. All entities and parameters depending on this
80    *         constraint, which are not used in other constraints, will be removed too.
81    *  \param[in] theConstraintID  index of constraint to be removed
82    *  \return \c true if the constraint was successfully removed
83    */
84   bool removeConstraint(const Slvs_hConstraint& theConstraintID);
85   /// \brief Returns the constraint by its ID
86   const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
87   /// \brief Returns list of constraints of specified type
88   std::list<Slvs_Constraint> getConstraintsByType(int theConstraintType) const;
89
90   /// \brief Attach constraint SLVS_C_WHERE_DRAGGED to this storage. It need to make precise calculations
91   void addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID);
92
93   /// \brief Add transient constraint
94   void addTemporaryConstraint(const Slvs_hConstraint& theConstraintID);
95   /// \brief Remove all transient constraints
96   void removeTemporaryConstraints();
97   /// \brief Checks the constraint is temporary
98   bool isTemporary(const Slvs_hConstraint& theConstraintID) const;
99
100   /// \brief Shows the sketch should be resolved
101   bool isNeedToResolve() const
102   { return myNeedToResolve; }
103
104   /// \brief Changes the flag of group to be resolved
105   void setNeedToResolve(bool theFlag)
106   { myNeedToResolve = theFlag; }
107
108   /// \brief Returns lists of removed elements
109   void getRemoved(std::set<Slvs_hParam>& theParameters,
110                   std::set<Slvs_hEntity>& theEntities,
111                   std::set<Slvs_hConstraint>& theConstraints);
112
113   /// \brief Initialize constraint solver by the entities collected by current storage
114   void initializeSolver(SketchSolver_Solver& theSolver);
115
116 private:
117   /// \brief Store coincident points
118   void addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
119   /// \brief Remove point from lists of coincidence
120   void removeCoincidentPoint(const Slvs_hEntity& thePoint);
121
122 public:
123   /// \brief Check two points are coincident
124   bool isCoincident(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
125
126 private:
127   Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
128   std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
129   Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
130   std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
131   Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
132   std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
133
134   std::vector< std::set<Slvs_hEntity> > myCoincidentPoints; ///< lists of coincident points
135   Slvs_hConstraint myFixed; ///< identifier of one of temporary constraints to fix separate point
136
137   bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
138
139   std::set<Slvs_hConstraint> myTemporaryConstraints; ///< list of transient constraints
140   std::set<Slvs_hParam> myRemovedParameters; ///< list of just removed parameters (cleared when returning to applicant)
141   std::set<Slvs_hEntity> myRemovedEntities; ///< list of just removed entities (cleared when returning to applicant)
142   std::set<Slvs_hConstraint> myRemovedConstraints; ///< list of just removed constraints (cleared when returning to applicant)
143 };
144
145 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
146
147 #endif