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