]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintManager.h
Salome HOME
Refactoring of create/update procedure
[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 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    *  \return \c true if the entity updated successfully
83    */
84   bool updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature);
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::vector<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 contains the constraint
99    */
100   boost::shared_ptr<SketchPlugin_Sketch> 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    */
118   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane);
119
120   ~SketchSolver_ConstraintGroup();
121
122   /// \brief Returns group's unique identifier
123   const Slvs_hGroup& getId() const
124   {return myID;}
125
126   /** \brief Adds or updates a constraint in the group
127    *  \param[in] theConstraint constraint to be changed
128    *  \return \c true if the constraint added or updated successfully
129    */
130   bool changeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
131
132   /** \brief Removes a constraint into the group
133    *  \param[in] theConstraint constraint to be removed
134    *  \return \c true if the constraint removed successfully
135    */
136   bool removeConstraint(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 workplane is the same as a base workplane for this group
145    *  \param[in] theWorkplane workplane to be compared
146    *  \return \c true if workplanes are the same
147    */
148   bool isBaseWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const;
149
150   boost::shared_ptr<SketchPlugin_Sketch> 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 protected:
159   /** \brief Adds or updates an entity in the group
160    *
161    *  The parameters of entity will be parsed and added to the list of SolveSpace parameters.
162    *  Parameters of certain entity will be placed sequentially in the list.
163    *
164    *  \param[in] theEntity the object of constraint
165    *  \return identifier of changed entity or 0 if entity could not be changed
166    */
167   Slvs_hEntity changeEntity(boost::shared_ptr<ModelAPI_Attribute> theEntity);
168
169   /** \brief Adds or updates a normal in the group
170    *
171    *  Normal is a special entity in SolveSpace, which defines a direction in 3D and 
172    *  a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
173    *
174    *  To define a normal there should be specified two coordinate axis 
175    *  on the plane transversed to created normal.
176    *
177    *  \param[in] theDirX first coordinate axis of the plane
178    *  \param[in] theDirY second coordinate axis of the plane
179    *  \param[in] theNorm attribute for the normal (used to identify newly created entity)
180    *  \return identifier of created or updated normal
181    */
182   Slvs_hEntity changeNormal(boost::shared_ptr<ModelAPI_Attribute> theDirX, 
183                             boost::shared_ptr<ModelAPI_Attribute> theDirY, 
184                             boost::shared_ptr<ModelAPI_Attribute> theNorm);
185
186   /** \brief Adds or updates a parameter in the group
187    *  \param[in] theParam   the value of parameter
188    *  \param[in] thePrmIter the cell in the list of parameters which should be changed 
189    *                        (the iterator will be increased if it does not reach the end of the list)
190    *  \return identifier of changed parameter; when the parameter cannot be created, returned ID is 0
191    */
192   Slvs_hParam changeParameter(const double& theParam, 
193                               std::vector<Slvs_Param>::const_iterator& thePrmIter);
194
195   /** \brief Compute constraint type according to SolveSpace identifiers
196    *         and verify that constraint parameters are correct
197    *  \param[in] theConstraint constraint which type should be determined
198    *  \return identifier of constraint type or SLVS_C_UNKNOWN if the type is wrong
199    */
200   int getConstraintType(const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const;
201
202 private:
203   /** \brief Creates a workplane from the sketch parameters
204    *  \param[in] theSketch parameters of workplane are the attributes of this sketch
205    *  \return \c true if success, \c false if workplane parameters are not consistent
206    */
207   bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
208
209 private:
210   // SolveSpace entities
211   Slvs_hGroup                  myID;            ///< the index of the group
212   Slvs_Entity                  myWorkplane;     ///< Workplane for the current group
213   std::vector<Slvs_Param>      myParams;        ///< List of parameters of the constraints
214   Slvs_hParam                  myParamMaxID;    ///< Actual maximal ID of parameters (not equal to myParams size)
215   std::vector<Slvs_Entity>     myEntities;      ///< List of entities of the constaints
216   Slvs_hEntity                 myEntityMaxID;   ///< Actual maximal ID of entities (not equal to myEntities size)
217   std::vector<Slvs_Constraint> myConstraints;   ///< List of constraints in SolveSpace format
218   Slvs_hConstraint             myConstrMaxID;   ///< Actual maximal ID of constraints (not equal to myConstraints size)
219   Slvs_System                  myConstrSet;     ///< SolveSpace's set of equations obtained by constraints
220   bool                         myNeedToSolve;   ///< Indicator that something changed in the group and constraint system need to be rebuilt
221
222   // SketchPlugin entities
223   boost::shared_ptr<SketchPlugin_Sketch> mySketch; ///< Equivalent to workplane
224   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_Constraint> 
225                                myConstraintMap;    ///< The map between SketchPlugin and SolveSpace constraints
226   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>
227                                myEntityMap;        ///< The map between parameters of constraints and their equivalent SolveSpace entities
228 };
229
230 #endif