]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintManager.h
Salome HOME
80843c6b814002bb3eaee3f9a8d965f99f70a8ec
[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 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    */
123   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane);
124
125   ~SketchSolver_ConstraintGroup();
126
127   /// \brief Returns group's unique identifier
128   const Slvs_hGroup& getId() const
129   {return myID;}
130
131   /** \brief Adds a constraint into the group
132    *  \param[in] theConstraint constraint to be added
133    *  \return \c true if the constraint added successfully
134    */
135   bool addConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
136
137   /** \brief Removes a constraint into the group
138    *  \param[in] theConstraint constraint to be removed
139    *  \return \c true if the constraint removed successfully
140    */
141   bool removeConstraint(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 workplane is the same as a base workplane for this group
150    *  \param[in] theWorkplane workplane to be compared
151    *  \return \c true if workplanes are the same
152    */
153   bool isBaseWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const;
154
155 protected:
156   /** \brief Adds an entity into the group
157    *
158    *  The parameters of entity will be parsed and added to the list of SolveSpace parameters.
159    *  Parameters of certain entity will be placed sequentially in the list.
160    *
161    *  \param[in] theEntity the object of constraint
162    *  \return identifier of created entity or 0 if entity was not added
163    */
164   Slvs_hEntity addEntity(boost::shared_ptr<ModelAPI_Attribute> theEntity);
165
166   /** \brief Adds a normal into the group
167    *
168    *  Normal is a special entity in SolveSpace, which defines a direction in 3D and 
169    *  a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
170    *
171    *  To define a normal there should be specified two coordinate axis 
172    *  on the plane transversed to created normal.
173    *
174    *  \param[in] theDirX first coordinate axis of the plane
175    *  \param[in] theDirY second coordinate axis of the plane
176    *  \return identifier of created normal
177    */
178   Slvs_hEntity addNormal(boost::shared_ptr<ModelAPI_Attribute> theDirX, 
179                          boost::shared_ptr<ModelAPI_Attribute> theDirY);
180
181   /** \brief Adds a parameter into the group
182    *  \param[in] theParam parameter to be added
183    *  \return identifier of created parameter or 0 if it was not added
184    */
185   Slvs_hParam addParameter(double theParam);
186
187   /** \brief Compute constraint type according to SolveSpace identifiers
188    *  \param[in] theConstraint constraint which type should be determined
189    *  \return identifier of constraint type
190    */
191   int getConstraintType(const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const;
192
193 private:
194   /** \brief Creates a workplane from the sketch parameters
195    *  \param[in] theSketch parameters of workplane are the attributes of this sketch
196    *  \return \c true if success
197    */
198   bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
199
200 private:
201   // SolveSpace entities
202   Slvs_hGroup                  myID;            ///< the index of the group
203   Slvs_Entity                  myWorkplane;     ///< Workplane for the current group
204   std::vector<Slvs_Param>      myParams;        ///< List of parameters of the constraints
205   Slvs_hParam                  myParamMaxID;    ///< Actual maximal ID of parameters (not equal to myParams size)
206   std::vector<Slvs_Entity>     myEntities;      ///< List of entities of the constaints
207   Slvs_hEntity                 myEntityMaxID;   ///< Actual maximal ID of entities (not equal to myEntities size)
208   std::vector<Slvs_Constraint> myConstraints;   ///< List of constraints in SolveSpace format
209   Slvs_hConstraint             myConstrMaxID;   ///< Actual maximal ID of constraints (not equal to myConstraints size)
210   Slvs_System                  myConstrSet;     ///< SolveSpace's set of equations obtained by constraints
211
212   // SketchPlugin entities
213   boost::shared_ptr<SketchPlugin_Sketch> mySketch; ///< Equivalent to workplane
214   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_Constraint> 
215                                myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints
216 };
217
218 #endif