Salome HOME
Correct processing arcs in SketchSolver with SolveSpace (issue #1144)
[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
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   {}
42
43   /// \brief Change mapping between constraint from SketchPlugin and
44   ///        a constraint applicable for corresponding solver.
45   /// \param theConstraint       [in]   original SketchPlugin constraint
46   /// \param theSolverConstraint [in]   solver's constraints
47   SKETCHSOLVER_EXPORT void addConstraint(ConstraintPtr        theConstraint,
48                                          ConstraintWrapperPtr theSolverConstraints);
49   /// \brief Change mapping between constraint from SketchPlugin and
50   ///        the list of constraints applicable for corresponding solver.
51   /// \param theConstraint        [in]   original SketchPlugin constraint
52   /// \param theSolverConstraints [in]   list of solver's constraints
53   SKETCHSOLVER_EXPORT
54     void addConstraint(ConstraintPtr                   theConstraint,
55                        std::list<ConstraintWrapperPtr> theSolverConstraints);
56
57   /// \brief Convert feature to the form applicable for specific solver and map it
58   /// \param theFeature [in]  feature to convert
59   /// \param theGroup   [in]  id of the group where the feature should be placed
60   /// \return \c true if the feature has been created or updated
61   SKETCHSOLVER_EXPORT bool update(FeaturePtr theFeature, const GroupID& theGroup = GID_UNKNOWN);
62   /// \brief Convert attribute to the form applicable for specific solver and map it
63   /// \param theFeature [in]  feature to convert
64   /// \return \c true if the attribute has been created or updated
65   SKETCHSOLVER_EXPORT bool update(AttributePtr theAttribute, const GroupID& theGroup = GID_UNKNOWN);
66
67   /// \brief Returns constraint related to corresponding constraint
68   SKETCHSOLVER_EXPORT
69     const std::list<ConstraintWrapperPtr>& constraint(const ConstraintPtr& theConstraint) const;
70
71   /// \brief Returns entity related to corresponding feature
72   SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const FeaturePtr& theFeature) const;
73   /// \brief Returns entity related to corresponding attribute
74   SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const AttributePtr& theAttribute) const;
75
76   /// \brief Return parsed sketch entity
77   const EntityWrapperPtr& sketch() const;
78   /// \brief Set parsed sketch entity.
79   /// Be careful, this method does not update fields of the storage specific for the solver.
80   /// Does not update if the sketch already exists.
81   void setSketch(const EntityWrapperPtr& theSketch);
82
83   /// \brief Mark two points as coincident
84   virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave) = 0;
85
86   /// \brief Shows the storage has any constraint twice
87   virtual bool hasDuplicatedConstraint() const = 0;
88
89   /// \brief Removes constraint from the storage
90   /// \return \c true if the constraint and all its parameters are removed successfully
91   SKETCHSOLVER_EXPORT bool removeConstraint(ConstraintPtr theConstraint);
92   /// \brief Removes feature from the storage
93   /// \return \c true if the feature and its attributes are removed successfully;
94   ///         \c false if the feature or any it attribute is used by remaining constraints.
95   SKETCHSOLVER_EXPORT bool removeEntity(FeaturePtr theFeature);
96   /// \brief Removes attribute from the storage
97   /// \return \c true if the attribute is not used by remaining features and constraints
98   SKETCHSOLVER_EXPORT bool removeEntity(AttributePtr theAttribute);
99
100   /// \brief Remove all features became invalid
101   SKETCHSOLVER_EXPORT void removeInvalidEntities();
102
103 ////  /// \brief Mark specified constraint as temporary
104 ////  virtual void setTemporary(ConstraintPtr theConstraint) = 0;
105 ////  /// \brief Returns number of temporary constraints
106 ////  virtual size_t nbTemporary() const = 0;
107 ////  /// \brief Remove temporary constraints
108 ////  /// \param theNbConstraints [in]  number of temporary constraints to be deleted
109 ////  /// \return number of remaining temporary constraints
110 ////  virtual size_t removeTemporary(size_t theNbConstraints = 1) = 0;
111
112   /// \brief Check whether the feature or its attributes are used by this storage
113   /// \param theFeature [in]  feature to be checked
114   /// \return \c true if the feature interacts with the storage
115   bool isInteract(const FeaturePtr& theFeature) const;
116   /// \brief Check whether the attribute is used by this storage
117   /// \param theAttribute [in]  attribute to be checked
118   /// \return \c true if the attribute interacts with the storage
119   bool isInteract(const AttributePtr& theAttribute) const;
120
121   /// \brief Check the features is not removed
122   bool isConsistent() const;
123
124   /// \brief Check the entity is fixed.
125   ///        If the point is under verification, all coincident points are checked too.
126   bool isFixed(EntityWrapperPtr theEntity) const;
127
128   /// \brief Shows the sketch should be resolved
129   virtual bool isNeedToResolve()
130   { return myNeedToResolve; }
131   /// \brief Changes the flag of group to be resolved
132   void setNeedToResolve(bool theFlag)
133   { myNeedToResolve = theFlag; }
134
135   /// \brief Initialize solver by constraints, entities and parameters
136   virtual void initializeSolver(SolverPtr theSolver) = 0;
137
138   /// \brief Update SketchPlugin features after resolving constraints
139   /// \param theFixedOnly [in]  if \c true the fixed points will be updated only
140   virtual void refresh(bool theFixedOnly = false) const = 0;
141
142   /// \brief Check if some parameters or entities are returned
143   ///        to the current group after removing temporary constraints
144   virtual void verifyFixed() = 0;
145
146   /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
147   ///        shows the distance from the start point.
148   virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase,
149                                                 double theCoeff) = 0;
150
151   /// \brief Block or unblock events when refreshing features
152   SKETCHSOLVER_EXPORT void blockEvents(bool isBlocked);
153
154 protected:
155   /// \brief Change mapping feature from SketchPlugin and
156   ///        the entity applicable for corresponding solver.
157   /// \param theFeature      [in]  original SketchPlugin feature
158   /// \param theSolverEntity [in]  solver's entity, created outside
159   SKETCHSOLVER_EXPORT
160     void addEntity(FeaturePtr       theFeature,
161                    EntityWrapperPtr theSolverEntity);
162
163   /// \brief Change mapping attribute of a feature and the entity applicable for corresponding solver.
164   /// \param theAttribute    [in]  original attribute
165   /// \param theSolverEntity [in]  solver's entity, created outside
166   SKETCHSOLVER_EXPORT
167     void addEntity(AttributePtr     theAttribute,
168                    EntityWrapperPtr theSolverEntity);
169
170   /// \brief Update constraint's data
171   /// \return \c true if any value is updated
172   virtual bool update(ConstraintWrapperPtr& theConstraint) = 0;
173   /// \brief Update entity's data
174   /// \return \c true if any value is updated
175   virtual bool update(EntityWrapperPtr& theEntity) = 0;
176   /// \brief Update parameter's data
177   /// \return \c true if the value of parameter is updated
178   virtual bool update(ParameterWrapperPtr& theParameter) = 0;
179
180   /// \brief Remove constraint
181   /// \return \c true if the constraint and all its parameters are removed successfully
182   SKETCHSOLVER_EXPORT virtual bool remove(ConstraintWrapperPtr theConstraint);
183   /// \brief Remove entity
184   /// \return \c true if the entity and all its parameters are removed successfully
185   SKETCHSOLVER_EXPORT virtual bool remove(EntityWrapperPtr theEntity);
186   /// \brief Remove parameter
187   /// \return \c true if the parameter has been removed
188   virtual bool remove(ParameterWrapperPtr theParameter) = 0;
189
190   /// \brief Update the group for the given entity, its sub-entities and parameters
191   virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup) = 0;
192   /// \brief Update the group for the given parameter
193   virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup) = 0;
194
195 private:
196   /// \brief Find the normal of the sketch
197   EntityWrapperPtr getNormal() const;
198
199   /// \brief Verify the feature or any its attribute is used by constraint
200   bool isUsed(FeaturePtr theFeature) const;
201   /// \brief Verify the attribute is used by constraint
202   bool isUsed(AttributePtr theAttirubute) const;
203
204 protected:
205   GroupID myGroupID;       ///< identifier of the group, this storage belongs to
206   bool    myNeedToResolve; ///< parameters are changed and group needs to be resolved
207   bool    myEventsBlocked; ///< indicates that features do not send events
208
209   /// map SketchPlugin constraint to a list of solver's constraints
210   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> > myConstraintMap;
211   /// map SketchPlugin feature to solver's entity
212   std::map<FeaturePtr, EntityWrapperPtr>                    myFeatureMap;
213   /// map attribute to solver's entity
214   std::map<AttributePtr, EntityWrapperPtr>                  myAttributeMap;
215
216   CoincidentPointsMap myCoincidentPoints; ///< lists of coincident points (first is a master point, second is a set of slaves)
217 };
218
219 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
220
221 #endif