Salome HOME
Distance constraint for equal points (issue #1061)
[modules/shaper.git] / src / SketchSolver / SketchSolver_Storage.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Storage.h
4 // Created: 30 Nov 2015
5 // Author:  Artem ZHIDKOV
6
7 #ifndef SketchSolver_Storage_H_
8 #define SketchSolver_Storage_H_
9
10 #include <SketchSolver.h>
11 #include <SketchSolver_IConstraintWrapper.h>
12 #include <SketchSolver_IEntityWrapper.h>
13 #include <SketchSolver_IParameterWrapper.h>
14 #include <SketchSolver_ISolver.h>
15
16 #include <ModelAPI_Attribute.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_Feature.h>
19 #include <SketchPlugin_Constraint.h>
20
21 class SketchSolver_ConstraintDistance;
22 typedef std::map<EntityWrapperPtr, std::set<EntityWrapperPtr> > CoincidentPointsMap;
23
24
25 /** \class   SketchSolver_Storage
26  *  \ingroup Plugins
27  *  \brief   Interface to map SketchPlugin features to the entities of corresponding solver.
28  */
29 class SketchSolver_Storage
30 {
31 private:
32   SketchSolver_Storage();
33   SketchSolver_Storage(const SketchSolver_Storage&);
34   SketchSolver_Storage& operator=(const SketchSolver_Storage&);
35
36 public:
37   SketchSolver_Storage(const GroupID& theGroup)
38     : myGroupID(theGroup),
39       myNeedToResolve(false),
40       myEventsBlocked(false),
41       myExistArc(false)
42   {}
43
44   /// \brief Change mapping between constraint from SketchPlugin and
45   ///        a constraint applicable for corresponding solver.
46   /// \param theConstraint       [in]   original SketchPlugin constraint
47   /// \param theSolverConstraint [in]   solver's constraints
48   SKETCHSOLVER_EXPORT void addConstraint(ConstraintPtr        theConstraint,
49                                          ConstraintWrapperPtr theSolverConstraints);
50   /// \brief Change mapping between constraint from SketchPlugin and
51   ///        the list of constraints applicable for corresponding solver.
52   /// \param theConstraint        [in]   original SketchPlugin constraint
53   /// \param theSolverConstraints [in]   list of solver's constraints
54   SKETCHSOLVER_EXPORT virtual
55     void addConstraint(ConstraintPtr                   theConstraint,
56                        std::list<ConstraintWrapperPtr> theSolverConstraints);
57
58   /// \brief Convert feature to the form applicable for specific solver and map it
59   /// \param theFeature [in]  feature to convert
60   /// \param theGroup   [in]  id of the group where the feature should be placed
61   /// \return \c true if the feature has been created or updated
62   SKETCHSOLVER_EXPORT bool update(FeaturePtr theFeature, const GroupID& theGroup = GID_UNKNOWN);
63   /// \brief Convert attribute to the form applicable for specific solver and map it
64   /// \param theFeature [in]  feature to convert
65   /// \return \c true if the attribute has been created or updated
66   SKETCHSOLVER_EXPORT bool update(AttributePtr theAttribute, const GroupID& theGroup = GID_UNKNOWN);
67
68   /// \brief Returns constraint related to corresponding constraint
69   SKETCHSOLVER_EXPORT
70     const std::list<ConstraintWrapperPtr>& constraint(const ConstraintPtr& theConstraint) const;
71
72   /// \brief Returns entity related to corresponding feature
73   SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const FeaturePtr& theFeature) const;
74   /// \brief Returns entity related to corresponding attribute
75   SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const AttributePtr& theAttribute) const;
76
77   /// \brief Return parsed sketch entity
78   const EntityWrapperPtr& sketch() const;
79   /// \brief Set parsed sketch entity.
80   /// Be careful, this method does not update fields of the storage specific for the solver.
81   /// Does not update if the sketch already exists.
82   void setSketch(const EntityWrapperPtr& theSketch);
83
84   /// \brief Mark two points as coincident
85   virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave) = 0;
86
87   /// \brief Shows the storage has any constraint twice
88   virtual bool hasDuplicatedConstraint() const = 0;
89
90   /// \brief Removes constraint from the storage
91   /// \return \c true if the constraint and all its parameters are removed successfully
92   SKETCHSOLVER_EXPORT bool removeConstraint(ConstraintPtr theConstraint);
93   /// \brief Removes feature from the storage
94   /// \return \c true if the feature and its attributes are removed successfully;
95   ///         \c false if the feature or any it attribute is used by remaining constraints.
96   SKETCHSOLVER_EXPORT bool removeEntity(FeaturePtr theFeature);
97   /// \brief Removes attribute from the storage
98   /// \return \c true if the attribute is not used by remaining features and constraints
99   SKETCHSOLVER_EXPORT bool removeEntity(AttributePtr theAttribute);
100
101   /// \brief Remove all features became invalid
102   SKETCHSOLVER_EXPORT void removeInvalidEntities();
103
104   /// \brief Check whether the feature or its attributes are used by this storage
105   /// \param theFeature [in]  feature to be checked
106   /// \return \c true if the feature interacts with the storage
107   bool isInteract(const FeaturePtr& theFeature) const;
108   /// \brief Check whether the attribute is used by this storage
109   /// \param theAttribute [in]  attribute to be checked
110   /// \return \c true if the attribute interacts with the storage
111   bool isInteract(const AttributePtr& theAttribute) const;
112
113   /// \brief Check the features is not removed
114   bool isConsistent() const;
115
116   /// \brief Check the entity is fixed.
117   ///        If the point is under verification, all coincident points are checked too.
118   SKETCHSOLVER_EXPORT bool isFixed(EntityWrapperPtr theEntity) const;
119
120   /// \brief Shows the sketch should be resolved
121   virtual bool isNeedToResolve()
122   { return myNeedToResolve; }
123   /// \brief Changes the flag of group to be resolved
124   void setNeedToResolve(bool theFlag)
125   { myNeedToResolve = theFlag; }
126
127   /// \brief Initialize solver by constraints, entities and parameters
128   virtual void initializeSolver(SolverPtr theSolver) = 0;
129
130   /// \brief Update SketchPlugin features after resolving constraints
131   /// \param theFixedOnly [in]  if \c true the fixed points will be updated only
132   virtual void refresh(bool theFixedOnly = false) const = 0;
133
134   /// \brief Check if some parameters or entities are returned
135   ///        to the current group after removing temporary constraints
136   virtual void verifyFixed() = 0;
137
138   /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
139   ///        shows the distance from the start point.
140   virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase,
141                                                 double theCoeff) = 0;
142
143   /// \brief Block or unblock events when refreshing features
144   SKETCHSOLVER_EXPORT void blockEvents(bool isBlocked);
145   /// \brief Shows the events are blocked for the features in the storage
146   bool isEventsBlocked() const
147   { return myEventsBlocked; }
148
149 protected:
150   /// \brief Change mapping feature from SketchPlugin and
151   ///        the entity applicable for corresponding solver.
152   /// \param theFeature      [in]  original SketchPlugin feature
153   /// \param theSolverEntity [in]  solver's entity, created outside
154   SKETCHSOLVER_EXPORT
155     void addEntity(FeaturePtr       theFeature,
156                    EntityWrapperPtr theSolverEntity);
157
158   /// \brief Change mapping attribute of a feature and the entity applicable for corresponding solver.
159   /// \param theAttribute    [in]  original attribute
160   /// \param theSolverEntity [in]  solver's entity, created outside
161   SKETCHSOLVER_EXPORT
162     void addEntity(AttributePtr     theAttribute,
163                    EntityWrapperPtr theSolverEntity);
164
165   /// \brief Update constraint's data
166   /// \return \c true if any value is updated
167   virtual bool update(ConstraintWrapperPtr theConstraint) = 0;
168   /// \brief Update entity's data
169   /// \return \c true if any value is updated
170   virtual bool update(EntityWrapperPtr theEntity) = 0;
171   /// \brief Update parameter's data
172   /// \return \c true if the value of parameter is updated
173   virtual bool update(ParameterWrapperPtr theParameter) = 0;
174
175   /// \brief Remove constraint
176   /// \return \c true if the constraint and all its parameters are removed successfully
177   SKETCHSOLVER_EXPORT virtual bool remove(ConstraintWrapperPtr theConstraint);
178   /// \brief Remove entity
179   /// \return \c true if the entity and all its parameters are removed successfully
180   SKETCHSOLVER_EXPORT virtual bool remove(EntityWrapperPtr theEntity);
181   /// \brief Remove parameter
182   /// \return \c true if the parameter has been removed
183   virtual bool remove(ParameterWrapperPtr theParameter) = 0;
184
185   /// \brief Update the group for the given entity, its sub-entities and parameters
186   virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup) = 0;
187   /// \brief Update the group for the given parameter
188   virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup) = 0;
189
190   /// \brief Verify the feature or any its attribute is used by constraint
191   SKETCHSOLVER_EXPORT bool isUsed(FeaturePtr theFeature) const;
192   /// \brief Verify the attribute is used by constraint
193   SKETCHSOLVER_EXPORT bool isUsed(AttributePtr theAttirubute) const;
194
195   /// \brief Find arcs without corresponding entity applicable for the solver and build them
196   SKETCHSOLVER_EXPORT void processArcs();
197
198 private:
199   /// \brief Find the normal of the sketch
200   EntityWrapperPtr getNormal() const;
201
202 protected:
203   GroupID myGroupID;       ///< identifier of the group, this storage belongs to
204   bool    myNeedToResolve; ///< parameters are changed and group needs to be resolved
205   bool    myEventsBlocked; ///< indicates that features do not send events
206   bool    myExistArc;      ///< the storage has any point of arc but not full arc, need to add it
207
208   /// map SketchPlugin constraint to a list of solver's constraints
209   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> > myConstraintMap;
210   /// map SketchPlugin feature to solver's entity
211   std::map<FeaturePtr, EntityWrapperPtr>                    myFeatureMap;
212   /// map attribute to solver's entity
213   std::map<AttributePtr, EntityWrapperPtr>                  myAttributeMap;
214
215   CoincidentPointsMap myCoincidentPoints; ///< lists of coincident points (first is a master point, second is a set of slaves)
216
217   // to be able to update entities from constraints
218   friend class SketchSolver_ConstraintDistance;
219 };
220
221 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
222
223 #endif