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