Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 <list>
18 #include <map>
19 #include <vector>
20
21
22 // Unknown constraint (for error reporting)
23 #define SLVS_C_UNKNOWN 0
24 // Unknown entity
25 #define SLVS_E_UNKNOWN 0
26
27 /** \class   SketchSolver_ConstraintManager
28  *  \ingroup DataModel
29  *  \brief   Listens the changes of SketchPlugin features and transforms the Constraint 
30  *           feature into the format understandable by SolveSpace library.
31  *
32  *  Constraints created for SolveSpace library are divided into the groups.
33  *  The division order based on connectedness of the features by the constraints.
34  *  The groups may be fused or separated according to the new constraints.
35  *
36  *  \remark This is a singleton.
37  */
38 class SketchSolver_ConstraintManager : public Events_Listener
39 {
40 public:
41   /** \brief Main method to create constraint manager
42    *  \return pointer to the singleton
43    */
44   static SketchSolver_ConstraintManager* Instance();
45
46   /** \brief Implementation of Event Listener method
47    *  \param[in] theMessage the data of the event
48    */
49   virtual void processEvent(const Events_Message* theMessage);
50
51 protected:
52   SketchSolver_ConstraintManager();
53   ~SketchSolver_ConstraintManager();
54
55   /** \brief Adds or updates a constraint in the suitable group
56    *  \param[in] theConstraint constraint to be changed
57    *  \return \c true if the constraint changed successfully
58    */
59   bool changeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
60
61   /** \brief Removes a constraint from the manager
62    *  \param[in] theConstraint constraint to be removed
63    *  \return \c true if the constraint removed successfully
64    */
65   bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
66
67   /** \brief Adds or updates a workplane in the manager
68    *  \param[in] theSketch the feature to create or update workplane
69    *  \return \c true if the workplane cahnged successfully
70    */
71   bool changeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
72
73   /** \brief Removes a workplane from the manager. 
74    *         All groups based on such workplane will be removed too.
75    *  \param[in] theSketch the feature to be removed
76    *  \return \c true if the workplane removed successfully
77    */
78   bool removeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
79
80   /** \brief Updates entity which is neither workplane nor constraint
81    *  \param[in] theFeature entity to be updated
82    */
83   void updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature);
84
85   /** \brief Goes through the list of groups and solve the constraints
86    */
87   void resolveConstraints();
88
89 private:
90   class SketchSolver_ConstraintGroup;
91
92   /** \brief Searches list of groups which interact with specified constraint
93    *  \param[in]  theConstraint constraint to be found
94    *  \param[out] theGroups     list of group indexes interacted with constraint
95    */
96   void findGroups(boost::shared_ptr<SketchPlugin_Constraint> theConstraint, 
97                   std::vector<Slvs_hGroup>&                  theGroupIDs) const;
98
99   /** \brief Searches in the list of groups the workplane which constains specified constraint
100    *  \param[in] theConstraint constraint to be found
101    *  \return workplane contains the constraint
102    */
103   boost::shared_ptr<SketchPlugin_Sketch> findWorkplaneForConstraint(
104                   boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
105
106 private:
107   static SketchSolver_ConstraintManager*    _self;    ///< Self pointer to implement singleton functionality
108   std::vector<SketchSolver_ConstraintGroup*> myGroups; ///< Groups of constraints
109 };
110
111
112 /** \class   SketchSolver_ConstraintGroup
113  *  \ingroup DataModel
114  *  \brief   Keeps the group of constraints which based on the same entities
115  */
116 class SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup
117 {
118 public:
119   /** \brief New group based on specified workplane
120    */
121   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane);
122
123   ~SketchSolver_ConstraintGroup();
124
125   /// \brief Returns group's unique identifier
126   const Slvs_hGroup& getId() const
127   {return myID;}
128
129   /** \brief Adds or updates a constraint in the group
130    *  \param[in] theConstraint constraint to be changed
131    *  \return \c true if the constraint added or updated successfully
132    */
133   bool changeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
134
135   /** \brief Verifies the constraint uses the objects from this group
136    *  \param[in] theConstraint constraint for verification of interaction
137    *  \return \c true if the constrained objects are used in current group
138    */
139   bool isInteract(boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
140
141   /** \brief Verifies the specified workplane is the same as a base workplane for this group
142    *  \param[in] theWorkplane workplane to be compared
143    *  \return \c true if workplanes are the same
144    */
145   bool isBaseWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const;
146
147   boost::shared_ptr<SketchPlugin_Sketch> getWorkplane() const
148   { return mySketch; }
149
150   /** \brief Update parameters of workplane. Should be called when Update event is coming.
151    *  \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
152    */
153   bool updateWorkplane();
154
155   /** \brief If the entity is in this group it will updated
156    *  \param[in] theEntity attribute, which values should update SolveSpace entity
157    */
158   void updateEntityIfPossible(boost::shared_ptr<ModelAPI_Attribute> theEntity);
159
160   /** \brief Searches invalid features and constraints in the group and avoids them
161    *  \return \c true if the group's sketch is invalid and the group should be removed
162    */
163   bool updateGroup();
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   /** \brief Adds a constraint for a point which should not be changed during computations
220    *  \param[in] theEntity the base for the constraint
221    */
222   void addTemporaryConstraintWhereDragged(boost::shared_ptr<ModelAPI_Attribute> theEntity);
223
224   /** \brief Remove all temporary constraint after computation finished
225    */
226   void removeTemporaryConstraints();
227
228 private:
229   /** \brief Creates a workplane from the sketch parameters
230    *  \param[in] theSketch parameters of workplane are the attributes of this sketch
231    *  \return \c true if success, \c false if workplane parameters are not consistent
232    */
233   bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
234
235 private:
236   // SolveSpace entities
237   Slvs_hGroup                  myID;            ///< the index of the group
238   Slvs_Entity                  myWorkplane;     ///< Workplane for the current group
239   std::vector<Slvs_Param>      myParams;        ///< List of parameters of the constraints
240   Slvs_hParam                  myParamMaxID;    ///< Actual maximal ID of parameters (not equal to myParams size)
241   std::vector<Slvs_Entity>     myEntities;      ///< List of entities of the constaints
242   Slvs_hEntity                 myEntityMaxID;   ///< Actual maximal ID of entities (not equal to myEntities size)
243   std::vector<Slvs_Constraint> myConstraints;   ///< List of constraints in SolveSpace format
244   Slvs_hConstraint             myConstrMaxID;   ///< Actual maximal ID of constraints (not equal to myConstraints size)
245   bool                         myNeedToSolve;   ///< Indicator that something changed in the group and constraint system need to be rebuilt
246
247   SketchSolver_Solver          myConstrSolver;  ///< Solver for set of equations obtained by constraints
248
249   // SketchPlugin entities
250   boost::shared_ptr<SketchPlugin_Sketch> mySketch; ///< Equivalent to workplane
251   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint> 
252                                 myConstraintMap;   ///< The map between SketchPlugin and SolveSpace constraints
253   std::list<Slvs_hConstraint> myTempConstraints;   ///< The list of identifiers of temporary constraints
254   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>
255                                 myEntityMap;       ///< The map between parameters of constraints and their equivalent SolveSpace entities
256 };
257
258 #endif