Salome HOME
Make v1.1.0 compilable under linux
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintGroup.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_ConstraintGroup.h
4 // Created: 27 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #ifndef SketchSolver_ConstraintGroup_H_
8 #define SketchSolver_ConstraintGroup_H_
9
10 #include "SketchSolver.h"
11 #include <SketchSolver_Solver.h>
12
13 #include <SketchPlugin_Constraint.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Feature.h>
16
17
18 #include <memory>
19 #include <list>
20 #include <map>
21 #include <vector>
22 #include <set>
23
24 typedef std::map< std::shared_ptr<SketchPlugin_Constraint>, std::vector<Slvs_hConstraint> >
25   ConstraintMap;
26
27 /** \class   SketchSolver_ConstraintGroup
28  *  \ingroup Plugins
29  *  \brief   Keeps the group of constraints which based on the same entities
30  */
31 class SketchSolver_ConstraintGroup
32 {
33  public:
34   /** \brief New group based on specified workplane.
35    *         Throws an exception if theWorkplane is not an object of SketchPlugin_Sketch type
36    *  \remark Type of theSketch is not verified inside
37    */
38   SketchSolver_ConstraintGroup(std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane);
39
40   ~SketchSolver_ConstraintGroup();
41
42   /// \brief Returns group's unique identifier
43   inline const Slvs_hGroup& getId() const
44   {
45     return myID;
46   }
47
48   /// \brief Returns true if the group has no constraints yet
49   inline bool isEmpty() const
50   {
51     return myConstraints.empty();
52   }
53
54   /// \brief Check for valid sketch data
55   inline bool isWorkplaneValid() const
56   {
57     return mySketch->data() && mySketch->data()->isValid();
58   }
59
60   /** \brief Adds or updates a constraint in the group
61    *  \param[in] theConstraint constraint to be changed
62    *  \return \c true if the constraint added or updated successfully
63    */
64   bool changeConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
65   /** \brief Adds or updates a rigid constraint in the group
66    *  \param[in] theConstraint constraint to be changed
67    *  \return \c true if the constraint added or updated successfully
68    */
69   bool changeRigidConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
70   /** \brief Adds or updates a mirror constraint in the group
71    *  \param[in] theConstraint constraint to be changed
72    *  \return \c true if the constraint added or updated successfully
73    */
74   bool changeMirrorConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
75   /** \brief Adds or updates a fillet constraint in the group
76    *  \param[in] theConstraint constraint to be changed
77    *  \return \c true if the constraint added or updated successfully
78    */
79   bool changeFilletConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
80
81   /** \brief Verifies the feature attributes are used in this group
82    *  \param[in] theFeature constraint or any other object for verification of interaction
83    *  \return \c true if some of attributes are used in current group
84    */
85   bool isInteract(std::shared_ptr<SketchPlugin_Feature> theFeature) const;
86
87   /** \brief Verifies the specified feature is equal to the base workplane for this group
88    *  \param[in] theWorkplane the feature to be compared with base workplane
89    *  \return \c true if workplanes are the same
90    */
91   bool isBaseWorkplane(std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane) const;
92
93   /// Returns the current workplane
94   std::shared_ptr<ModelAPI_CompositeFeature> getWorkplane() const
95   {
96     return mySketch;
97   }
98
99   /** \brief Update parameters of workplane. Should be called when Update event is coming.
100    *  \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
101    */
102   bool updateWorkplane();
103
104   /** \brief If the entity is in this group it will updated
105    *  \param[in] theEntity attribute, which values should update SolveSpace entity
106    */
107   void updateEntityIfPossible(std::shared_ptr<ModelAPI_Attribute> theEntity);
108
109   /** \brief Searches invalid features and constraints in the group and avoids them
110    *  \return \c true if the group several constraints were removed
111    */
112   bool updateGroup();
113
114   /** \brief Add specified group to this one
115    *  \param[in] theGroup group of constraint to be added
116    */
117   void mergeGroups(const SketchSolver_ConstraintGroup& theGroup);
118
119   /** \brief Cut from the group several subgroups, which are not connected to the current one by any constraint
120    *  \param[out] theCuts enlarge this list by newly created groups
121    */
122   void splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts);
123
124   /** \brief Start solution procedure if necessary and update attributes of features
125    *  \return \c false when no need to solve constraints
126    */
127   bool resolveConstraints();
128
129   /** \brief Searches the constraints built on the entity and emit the signal to update them
130    *  \param[in] theEntity attribute of the constraint
131    */
132   void updateRelatedConstraints(std::shared_ptr<ModelAPI_Attribute> theEntity) const;
133   /** \brief Searches the constraints built on the entity and emit the signal to update them
134    *  \param[in] theFeature feature of the constraint
135    */
136   void updateRelatedConstraintsFeature(std::shared_ptr<ModelAPI_Feature> theFeature) const;
137
138   /** \brief Adds or updates an entity in the group
139    *
140    *  The parameters of entity will be parsed and added to the list of SolveSpace parameters.
141    *  Parameters of certain entity will be placed sequentially in the list.
142    *
143    *  \param[in] theEntity the object of constraint
144    *  \return identifier of changed entity or 0 if entity could not be changed
145    */
146   Slvs_hEntity changeEntity(std::shared_ptr<ModelAPI_Attribute> theEntity);
147   Slvs_hEntity changeEntityFeature(std::shared_ptr<ModelAPI_Feature> theEntity);
148
149 protected:
150   /** \brief Adds or updates a normal in the group
151    *
152    *  Normal is a special entity in SolveSpace, which defines a direction in 3D and
153    *  a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
154    *
155    *  To define a normal there should be specified two coordinate axis
156    *  on the plane transversed to created normal.
157    *
158    *  \param[in] theDirX first coordinate axis of the plane
159    *  \param[in] theNorm attribute for the normal (used to identify newly created entity)
160    *  \return identifier of created or updated normal
161    */
162   Slvs_hEntity changeNormal(std::shared_ptr<ModelAPI_Attribute> theDirX,
163                             std::shared_ptr<ModelAPI_Attribute> theNorm);
164
165   /** \brief Adds or updates a parameter in the group
166    *  \param[in] theParam   the value of parameter
167    *  \param[in] thePrmIter the cell in the list of parameters which should be changed
168    *                        (the iterator will be increased if it does not reach the end of the list)
169    *  \return identifier of changed parameter; when the parameter cannot be created, returned ID is 0
170    */
171   Slvs_hParam changeParameter(double theParam,
172                               std::vector<Slvs_Param>::iterator& thePrmIter);
173
174   /** \brief Removes specified entities and their parameters
175    *  \param[in] theEntities  list of IDs of the entities to be removed
176    */
177   void removeEntitiesById(const std::set<Slvs_hEntity>& theEntities);
178
179   /** \brief Removes constraints from the group
180    *  \param[in] theConstraint constraint to be removed
181    */
182   void removeConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
183
184   /** \brief Change values of attribute by parameters received from SolveSpace solver
185    *  \param[in,out] theAttribute pointer to the attribute to be changed
186    *  \param[in]     theEntityID  identifier of SolveSpace entity, which contains updated data
187    *  \return \c true if the attribute's value has changed
188    */
189   bool updateAttribute(std::shared_ptr<ModelAPI_Attribute> theAttribute,
190                        const Slvs_hEntity& theEntityID);
191
192   /// \brief Update arc of fillet to be less than 180 degree
193   void updateFilletConstraints();
194
195   /** \brief Adds a constraint for a point which should not be changed during computations
196    *  \param[in] theEntity     the base for the constraint
197    *  \param[in] theAllowToFit this flag shows that the entity may be placed into 
198    *                           the 'dragged' field of SolveSpace solver, so this entity 
199    *                           may be changed a little during solution
200    */
201   void addTemporaryConstraintWhereDragged(std::shared_ptr<ModelAPI_Attribute> theEntity,
202                                           bool theAllowToFit = true);
203
204   /** \brief Remove all temporary constraint after computation finished
205    *  \param[in] theRemoved  indexes of constraints to be removed. If empty, all temporary constraints should be deleted
206    */
207   void removeTemporaryConstraints(const std::set<Slvs_hConstraint>& theRemoved =
208                                                                  std::set<Slvs_hConstraint>());
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, \c false if workplane parameters are not consistent
214    */
215   bool addWorkplane(std::shared_ptr<ModelAPI_CompositeFeature> theSketch);
216
217   /** \brief Add the entities of constraint for points coincidence into the appropriate list
218    *  \param[in] thePoint1 identifier of the first point
219    *  \param[in] thePoint2 identifier of the second point
220    *  \return \c true if the points are added successfully, and 
221    *          \c false if the constraint is the extra one (should not be created in SolveSpace)
222    */
223   bool addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
224
225   /** \brief Verifies and changes parameters of constriant, 
226    *         e.g. sign of the distance between line and point
227    *  \param[in,out] theConstraint SolveSpace constraint to be verified
228    */
229   void checkConstraintConsistence(Slvs_Constraint& theConstraint);
230
231   /** \brief Change entities parameters to make them symmetric relating to the mirror line
232    *  \param[in] theBase        entity to be mirrored
233    *  \param[in] theMirror      a mirrored object
234    *  \param[in] theMirrorLine  a mirror line
235    */
236   void makeMirrorEntity(const Slvs_hEntity& theBase,
237                         const Slvs_hEntity& theMirror,
238                         const Slvs_hEntity& theMirrorLine);
239
240   /** \brief Creates/updates mirror constraint for two points
241    *  \param[in]     theBasePoint   ID of initial point
242    *  \param[in]     theMirrorPoint ID of the mirroring point
243    *  \param[in]     theMirrorLine  ID of the mirror line
244    *  \param[in]     thePrevConstr  list of previous constraints (the items will be deleted from the list if they are updated)
245    *  \param[in,out] thePrevMirror  list of previously mirrored points (the items will be deleted from the list if they are updated)
246    *  \return ID of created/updated constraint
247    */
248   Slvs_hConstraint changeMirrorPoints(const Slvs_hEntity& theBasePoint,
249                                       const Slvs_hEntity& theMirrorPoint,
250                                       const Slvs_hEntity& theMirrorLine,
251                                       std::vector<Slvs_Constraint>&  thePrevConstr,
252                                       std::map<Slvs_hEntity, Slvs_hEntity>& thePrevMirror);
253
254   /** \brief Calculates middle point on line or arc
255    *  \param[in]  theEntity  identifier of line or arc
256    *  \param[out] theX       X value of middle point
257    *  \param[out] theY       Y value of middle point
258    */
259   void calculateMiddlePoint(const Slvs_hEntity& theEntity,
260                             double& theX, double& theY) const;
261
262  private:
263   // SolveSpace entities
264   Slvs_hGroup myID;            ///< the index of the group
265   Slvs_Entity myWorkplane;     ///< Workplane for the current group
266   std::vector<Slvs_Param> myParams;        ///< List of parameters of the constraints
267   Slvs_hParam myParamMaxID;    ///< Actual maximal ID of parameters (not equal to myParams size)
268   std::vector<Slvs_Entity> myEntities;      ///< List of entities of the constaints
269   std::vector<bool> myEntOfConstr; ///< Flags show that certain entity used in constraints
270   Slvs_hEntity myEntityMaxID;   ///< Actual maximal ID of entities (not equal to myEntities size)
271   std::vector<Slvs_Constraint> myConstraints;   ///< List of constraints in SolveSpace format
272   Slvs_hConstraint myConstrMaxID;  ///< Actual maximal ID of constraints (not equal to myConstraints size)
273   bool myNeedToSolve;  ///< Indicator that something changed in the group and constraint system need to be rebuilt
274
275   SketchSolver_Solver myConstrSolver;  ///< Solver for set of equations obtained by constraints
276
277   std::vector<Slvs_hParam> myTempPointWhereDragged;  ///< Parameters of one of the points which is moved by user
278   Slvs_hEntity myTempPointWDrgdID;      ///< Identifier of such point
279   std::list<Slvs_hConstraint> myTempConstraints;  ///< The list of identifiers of temporary constraints (SLVS_C_WHERE_DRAGGED) applied for all other points moved by user
280
281   // SketchPlugin entities
282   std::shared_ptr<ModelAPI_CompositeFeature> mySketch;        ///< Equivalent to workplane
283   ConstraintMap myConstraintMap;  ///< The map between SketchPlugin and SolveSpace constraints
284   std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity> myEntityAttrMap;  ///< The map between "attribute" parameters of constraints and their equivalent SolveSpace entities
285   std::map<FeaturePtr, Slvs_hEntity> myEntityFeatMap;  ///< The map between "feature" parameters of constraints and their equivalent SolveSpace entities
286
287   // Conincident items
288   std::vector<std::set<Slvs_hEntity> > myCoincidentPoints;  ///< Stores the lists of identifiers of coincident points (to avoid unnecessary coincidence constraints)
289   std::set<std::shared_ptr<SketchPlugin_Constraint> > myExtraCoincidence;  ///< Additional coincidence constraints which are not necessary (coincidence between points already done
290                                                                              ///< by other constraints) but created by GUI tools. Useful when some coincidence constraints were removed
291 };
292
293 #endif