]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintManager.h
Salome HOME
Merge remote-tracking branch 'remotes/origin/SolveSpace'
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintManager.h
1 // File:    SketchSolver_ConstraintManager.h
2 // Created: 08 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #ifndef SketchSolver_ConstraintManager_Headerfile
6 #define SketchSolver_ConstraintManager_Headerfile
7
8 #include "SketchSolver.h"
9 #include <SketchSolver_Solver.h>
10
11 #include <Events_Listener.h>
12 #include <SketchPlugin_Constraint.h>
13
14 #include <string.h>
15 #include <slvs.h>
16
17 #include <map>
18 #include <vector>
19
20
21 // Unknown constraint (for error reporting)
22 #define SLVS_C_UNKNOWN 0
23 // Unknown entity
24 #define SLVS_E_UNKNOWN 0
25
26 /** \class   SketchSolver_ConstraintManager
27  *  \ingroup DataModel
28  *  \brief   Listens the changes of SketchPlugin features and transforms the Constraint 
29  *           feature into the format understandable by SolveSpace library.
30  *
31  *  Constraints created for SolveSpace library are divided into the groups.
32  *  The division order based on connectedness of the features by the constraints.
33  *  The groups may be fused or separated according to the new constraints.
34  *
35  *  \remark This is a singleton.
36  */
37 class SketchSolver_ConstraintManager : public Events_Listener
38 {
39 public:
40   /** \brief Main method to create constraint manager
41    *  \return pointer to the singleton
42    */
43   static SketchSolver_ConstraintManager* Instance();
44
45   /** \brief Implementation of Event Listener method
46    *  \param[in] theMessage the data of the event
47    */
48   virtual void processEvent(const Events_Message* theMessage);
49
50 protected:
51   SketchSolver_ConstraintManager();
52   ~SketchSolver_ConstraintManager();
53
54   /** \brief Adds or updates a constraint in the suitable group
55    *  \param[in] theConstraint constraint to be changed
56    *  \return \c true if the constraint changed successfully
57    */
58   bool changeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
59
60   /** \brief Removes a constraint from the manager
61    *  \param[in] theConstraint constraint to be removed
62    *  \return \c true if the constraint removed successfully
63    */
64   bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
65
66   /** \brief Adds or updates a workplane in the manager
67    *  \param[in] theSketch the feature to create or update workplane
68    *  \return \c true if the workplane cahnged successfully
69    */
70   bool changeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
71
72   /** \brief Removes a workplane from the manager. 
73    *         All groups based on such workplane will be removed too.
74    *  \param[in] theSketch the feature to be removed
75    *  \return \c true if the workplane removed successfully
76    */
77   bool removeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
78
79   /** \brief Updates entity which is neither workplane nor constraint
80    *  \param[in] theFeature entity to be updated
81    */
82   void updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature);
83
84   /** \brief Goes through the list of groups and solve the constraints
85    */
86   void ResolveConstraints();
87
88 private:
89   class SketchSolver_ConstraintGroup;
90
91   /** \brief Searches list of groups which interact with specified constraint
92    *  \param[in]  theConstraint constraint to be found
93    *  \param[out] theGroups     list of group indexes interacted with constraint
94    */
95   void findGroups(boost::shared_ptr<SketchPlugin_Constraint> theConstraint, 
96                   std::vector<Slvs_hGroup>&                  theGroupIDs) const;
97
98   /** \brief Searches in the list of groups the workplane which constains specified constraint
99    *  \param[in] theConstraint constraint to be found
100    *  \return workplane contains the constraint
101    */
102   boost::shared_ptr<SketchPlugin_Sketch> findWorkplaneForConstraint(
103                   boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
104
105 private:
106   static SketchSolver_ConstraintManager*    _self;    ///< Self pointer to implement singleton functionality
107   std::vector<SketchSolver_ConstraintGroup*> myGroups; ///< Groups of constraints
108 };
109
110
111 /** \class   SketchSolver_ConstraintGroup
112  *  \ingroup DataModel
113  *  \brief   Keeps the group of constraints which based on the same entities
114  */
115 class SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup
116 {
117 public:
118   /** \brief New group based on specified workplane
119    */
120   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane);
121
122   ~SketchSolver_ConstraintGroup();
123
124   /// \brief Returns group's unique identifier
125   const Slvs_hGroup& getId() const
126   {return myID;}
127
128   /** \brief Adds or updates a constraint in the group
129    *  \param[in] theConstraint constraint to be changed
130    *  \return \c true if the constraint added or updated successfully
131    */
132   bool changeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
133
134   /** \brief Removes a constraint into the group
135    *  \param[in] theConstraint constraint to be removed
136    *  \return \c true if the constraint removed successfully
137    */
138   bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
139
140   /** \brief Verifies the constraint uses the objects from this group
141    *  \param[in] theConstraint constraint for verification of interaction
142    *  \return \c true if the constrained objects are used in current group
143    */
144   bool isInteract(boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
145
146   /** \brief Verifies the specified workplane is the same as a base workplane for this group
147    *  \param[in] theWorkplane workplane to be compared
148    *  \return \c true if workplanes are the same
149    */
150   bool isBaseWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const;
151
152   boost::shared_ptr<SketchPlugin_Sketch> getWorkplane() const
153   { return mySketch; }
154
155   /** \brief Update parameters of workplane. Should be called when Update event is coming.
156    *  \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
157    */
158   bool updateWorkplane();
159
160   /** \brief If the entity is in this group it will updated
161    *  \param[in] theEntity attribute, which values should update SolveSpace entity
162    */
163   void updateEntityIfPossible(boost::shared_ptr<ModelAPI_Attribute> theEntity);
164
165   /** \brief Start solution procedure if necessary and update attributes of features
166    */
167   void ResolveConstraints();
168
169 protected:
170   /** \brief Adds or updates an entity in the group
171    *
172    *  The parameters of entity will be parsed and added to the list of SolveSpace parameters.
173    *  Parameters of certain entity will be placed sequentially in the list.
174    *
175    *  \param[in] theEntity the object of constraint
176    *  \return identifier of changed entity or 0 if entity could not be changed
177    */
178   Slvs_hEntity changeEntity(boost::shared_ptr<ModelAPI_Attribute> theEntity);
179
180   /** \brief Adds or updates a normal in the group
181    *
182    *  Normal is a special entity in SolveSpace, which defines a direction in 3D and 
183    *  a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
184    *
185    *  To define a normal there should be specified two coordinate axis 
186    *  on the plane transversed to created normal.
187    *
188    *  \param[in] theDirX first coordinate axis of the plane
189    *  \param[in] theDirY second coordinate axis of the plane
190    *  \param[in] theNorm attribute for the normal (used to identify newly created entity)
191    *  \return identifier of created or updated normal
192    */
193   Slvs_hEntity changeNormal(boost::shared_ptr<ModelAPI_Attribute> theDirX, 
194                             boost::shared_ptr<ModelAPI_Attribute> theDirY, 
195                             boost::shared_ptr<ModelAPI_Attribute> theNorm);
196
197   /** \brief Adds or updates a parameter in the group
198    *  \param[in] theParam   the value of parameter
199    *  \param[in] thePrmIter the cell in the list of parameters which should be changed 
200    *                        (the iterator will be increased if it does not reach the end of the list)
201    *  \return identifier of changed parameter; when the parameter cannot be created, returned ID is 0
202    */
203   Slvs_hParam changeParameter(const double& theParam, 
204                               std::vector<Slvs_Param>::const_iterator& thePrmIter);
205
206   /** \brief Compute constraint type according to SolveSpace identifiers
207    *         and verify that constraint parameters are correct
208    *  \param[in] theConstraint constraint which type should be determined
209    *  \return identifier of constraint type or SLVS_C_UNKNOWN if the type is wrong
210    */
211   int getConstraintType(const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const;
212
213   /** \brief Change values of attribute by parameters received from SolveSpace solver
214    *  \param[in,out] theAttribute pointer to the attribute to be changed
215    *  \param[in]     theEntityID  identifier of SolveSpace entity, which contains updated data
216    */
217   void updateAttribute(boost::shared_ptr<ModelAPI_Attribute> theAttribute, const Slvs_hEntity& theEntityID);
218
219 private:
220   /** \brief Creates a workplane from the sketch parameters
221    *  \param[in] theSketch parameters of workplane are the attributes of this sketch
222    *  \return \c true if success, \c false if workplane parameters are not consistent
223    */
224   bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
225
226 private:
227   // SolveSpace entities
228   Slvs_hGroup                  myID;            ///< the index of the group
229   Slvs_Entity                  myWorkplane;     ///< Workplane for the current group
230   std::vector<Slvs_Param>      myParams;        ///< List of parameters of the constraints
231   Slvs_hParam                  myParamMaxID;    ///< Actual maximal ID of parameters (not equal to myParams size)
232   std::vector<Slvs_Entity>     myEntities;      ///< List of entities of the constaints
233   Slvs_hEntity                 myEntityMaxID;   ///< Actual maximal ID of entities (not equal to myEntities size)
234   std::vector<Slvs_Constraint> myConstraints;   ///< List of constraints in SolveSpace format
235   Slvs_hConstraint             myConstrMaxID;   ///< Actual maximal ID of constraints (not equal to myConstraints size)
236   bool                         myNeedToSolve;   ///< Indicator that something changed in the group and constraint system need to be rebuilt
237
238   SketchSolver_Solver          myConstrSolver;  ///< Solver for set of equations obtained by constraints
239
240   // SketchPlugin entities
241   boost::shared_ptr<SketchPlugin_Sketch> mySketch; ///< Equivalent to workplane
242   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint> 
243                                myConstraintMap;    ///< The map between SketchPlugin and SolveSpace constraints
244   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>
245                                myEntityMap;        ///< The map between parameters of constraints and their equivalent SolveSpace entities
246 };
247
248 #endif