Salome HOME
Useful methods for sketch operations managements
[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
10 #include <Events_Listener.h>
11 #include <SketchPlugin_Constraint.h>
12
13 // Need to be defined before including SolveSpace to avoid additional dependances on Windows platform
14 #if defined(WIN32) && !defined(HAVE_C99_INTEGER_TYPES)
15 typedef unsigned int UINT32;
16 #endif
17 #include <string.h>
18 #include <slvs.h>
19
20 #include <map>
21 #include <vector>
22
23
24 // Unknown constraint (for error reporting)
25 #define SLVS_C_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 a constraint into the manager
56    *  \param[in] theConstraint constraint to be added
57    *  \return \c true if the constraint added successfully
58    */
59   bool addConstraint(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 Updates a constraint
68    *  \param[in] theConstraint constraint to be updated
69    *  \return \c true if the constraint was updated
70    */
71   bool updateConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
72
73   /** \brief Adds a workplane into the manager
74    *  \param[in] theSketch the feature to create workplane
75    *  \return \c true if the workplane added successfully
76    */
77   bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
78
79   /** \brief Removes a workplane from the manager. 
80    *         All groups based on such workplane will be removed too.
81    *  \param[in] theSketch the feature to be removed
82    *  \return \c true if the workplane removed successfully
83    */
84   bool removeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
85
86   /** \brief Updates a workplane
87    *  \param[in] theSketch workplane to be updated
88    *  \return \c true if the workplane was updated
89    */
90   bool updateWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
91
92   /** \brief Updates entity which is neither workplane nor constraint
93    *  \param[in] theFeature entity to be updated
94    *  \return \c true if the entity updated successfully
95    */
96   bool updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature);
97
98 private:
99   class SketchSolver_ConstraintGroup;
100
101   /** \brief Searches list of groups which interact with specified constraint
102    *  \param[in]  theConstraint constraint to be found
103    *  \param[out] theGroups     list of group indexes interacted with constraint
104    */
105   void findGroups(boost::shared_ptr<SketchPlugin_Constraint> theConstraint, 
106                   std::vector<Slvs_hGroup>&                  theGroupIDs) const;
107
108   /** \brief Searches in the list of groups the workplane which constains specified constraint
109    *  \param[in] theConstraint constraint to be found
110    *  \return workplane contains the constraint
111    */
112   boost::shared_ptr<SketchPlugin_Sketch> findWorkplaneForConstraint(
113                   boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
114
115 private:
116   static SketchSolver_ConstraintManager*    _self;    ///< Self pointer to implement singleton functionality
117   std::vector<SketchSolver_ConstraintGroup> myGroups; ///< Groups of constraints
118 };
119
120
121 /** \class   SketchSolver_ConstraintGroup
122  *  \ingroup DataModel
123  *  \brief   Keeps the group of constraints which based on the same entities
124  */
125 class SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup
126 {
127 public:
128   /** \brief New group based on specified workplane
129    */
130   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane);
131
132   ~SketchSolver_ConstraintGroup();
133
134   /// \brief Returns group's unique identifier
135   const Slvs_hGroup& getId() const
136   {return myID;}
137
138   /** \brief Adds a constraint into the group
139    *  \param[in] theConstraint constraint to be added
140    *  \return \c true if the constraint added successfully
141    */
142   bool addConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
143
144   /** \brief Removes a constraint into the group
145    *  \param[in] theConstraint constraint to be removed
146    *  \return \c true if the constraint removed successfully
147    */
148   bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
149
150   /** \brief Verifies the constraint uses the objects from this group
151    *  \param[in] theConstraint constraint for verification of interaction
152    *  \return \c true if the constrained objects are used in current group
153    */
154   bool isInteract(boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
155
156   /** \brief Verifies the specified workplane is the same as a base workplane for this group
157    *  \param[in] theWorkplane workplane to be compared
158    *  \return \c true if workplanes are the same
159    */
160   bool isBaseWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const;
161
162   boost::shared_ptr<SketchPlugin_Sketch> getWorkplane() const
163   { return mySketch; }
164
165   /** \brief Update parameters of workplane. Should be called when Update event is coming
166    *  \param[in] theWorkplane workplane to be updated
167    *  \return \c true if workplane updated successfully
168    */
169   bool updateWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane);
170
171 protected:
172   /** \brief Adds an entity into the group
173    *
174    *  The parameters of entity will be parsed and added to the list of SolveSpace parameters.
175    *  Parameters of certain entity will be placed sequentially in the list.
176    *
177    *  \param[in] theEntity the object of constraint
178    *  \return identifier of created entity or 0 if entity was not added
179    */
180   Slvs_hEntity addEntity(boost::shared_ptr<ModelAPI_Attribute> theEntity);
181
182   /** \brief Adds a normal into the group
183    *
184    *  Normal is a special entity in SolveSpace, which defines a direction in 3D and 
185    *  a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
186    *
187    *  To define a normal there should be specified two coordinate axis 
188    *  on the plane transversed to created normal.
189    *
190    *  \param[in] theDirX first coordinate axis of the plane
191    *  \param[in] theDirY second coordinate axis of the plane
192    *  \return identifier of created normal
193    */
194   Slvs_hEntity addNormal(boost::shared_ptr<ModelAPI_Attribute> theDirX, 
195                          boost::shared_ptr<ModelAPI_Attribute> theDirY);
196
197   /** \brief Adds a parameter into the group
198    *  \param[in] theParam parameter to be added
199    *  \return identifier of created parameter or 0 if it was not added
200    */
201   Slvs_hParam addParameter(double theParam);
202
203   /** \brief Compute constraint type according to SolveSpace identifiers
204    *         and verify that constraint parameters are correct
205    *  \param[in] theConstraint constraint which type should be determined
206    *  \return identifier of constraint type or SLVS_C_UNKNOWN if the type is wrong
207    */
208   int getConstraintType(const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const;
209
210 private:
211   /** \brief Creates a workplane from the sketch parameters
212    *  \param[in] theSketch parameters of workplane are the attributes of this sketch
213    *  \return \c true if success
214    */
215   bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
216
217 private:
218   // SolveSpace entities
219   Slvs_hGroup                  myID;            ///< the index of the group
220   Slvs_Entity                  myWorkplane;     ///< Workplane for the current group
221   std::vector<Slvs_Param>      myParams;        ///< List of parameters of the constraints
222   Slvs_hParam                  myParamMaxID;    ///< Actual maximal ID of parameters (not equal to myParams size)
223   std::vector<Slvs_Entity>     myEntities;      ///< List of entities of the constaints
224   Slvs_hEntity                 myEntityMaxID;   ///< Actual maximal ID of entities (not equal to myEntities size)
225   std::vector<Slvs_Constraint> myConstraints;   ///< List of constraints in SolveSpace format
226   Slvs_hConstraint             myConstrMaxID;   ///< Actual maximal ID of constraints (not equal to myConstraints size)
227   Slvs_System                  myConstrSet;     ///< SolveSpace's set of equations obtained by constraints
228
229   // SketchPlugin entities
230   boost::shared_ptr<SketchPlugin_Sketch> mySketch; ///< Equivalent to workplane
231   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_Constraint> 
232                                myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints
233 };
234
235 #endif