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