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