Salome HOME
1ddc40100e06a41600855d8c1dd70f7e9131518b
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_Storage.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    PlaneGCSSolver_Storage.h
4 // Created: 14 Dec 2015
5 // Author:  Artem ZHIDKOV
6
7 #ifndef PlaneGCSSolver_Storage_H_
8 #define PlaneGCSSolver_Storage_H_
9
10 #include <PlaneGCSSolver_Defs.h>
11
12 #include <SketchSolver.h>
13 #include <SketchSolver_Storage.h>
14 #include <SketchSolver_ISolver.h>
15
16 ////#include <GeomDataAPI_Point2D.h>
17 ////#include <ModelAPI_Attribute.h>
18 ////#include <ModelAPI_AttributeDouble.h>
19 ////#include <ModelAPI_Feature.h>
20 ////#include <SketchPlugin_Constraint.h>
21 ////
22 ////#include <Geo.h>
23 ////#include <Util.h>
24 ////
25 ////#include <list>
26 ////#include <memory>
27 ////#include <set>
28 ////#include <vector>
29
30 ////#ifndef OBSOLETE
31 ////#include <Constraints.h>
32 ////
33 ////typedef std::shared_ptr<GCS::Point> GCSPointPtr;
34 ////typedef std::shared_ptr<GCS::Curve> GCSCurvePtr;
35 ////typedef GCS::Constraint*            GCSConstraintPtr;
36 ////#endif
37
38 /** \class   PlaneGCSSolver_Storage
39  *  \ingroup Plugins
40  *  \brief   Contains all necessary data in PlaneGCS format to solve a single group of constraints
41  */
42 class PlaneGCSSolver_Storage : public SketchSolver_Storage
43 {
44 public:
45   PlaneGCSSolver_Storage(const GroupID& theGroup);
46
47 // =============   Inherited from SketchSolver_Storage   =============
48
49   /// \brief Change mapping between constraint from SketchPlugin and
50   ///        the list of constraints applicable for corresponding solver.
51   ///        Derived here to update point-point coincidence.
52   /// \param theConstraint        [in]   original SketchPlugin constraint
53   /// \param theSolverConstraints [in]   list of solver's constraints
54   virtual void addConstraint(ConstraintPtr                   theConstraint,
55                              std::list<ConstraintWrapperPtr> theSolverConstraints);
56
57   /// \brief Update constraint's data
58   /// \return \c true if any value is updated
59   virtual bool update(ConstraintWrapperPtr& theConstraint);
60   /// \brief Update entity's data
61   /// \return \c true if any value is updated
62   virtual bool update(EntityWrapperPtr& theEntity);
63   /// \brief Update parameter's data
64   /// \return \c true if the value of parameter is updated
65   virtual bool update(ParameterWrapperPtr& theParameter);
66
67   /// \brief Update SketchPlugin features after resolving constraints
68   /// \param theFixedOnly [in]  if \c true the fixed points will be updated only
69   virtual void refresh(bool theFixedOnly = false) const;
70
71   /// \brief Check if some parameters or entities are returned
72   ///        to the current group after removing temporary constraints
73   virtual void verifyFixed();
74
75   /// \brief Mark two points as coincident
76   virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave);
77
78   /// \brief Shows the storage has the same constraint twice
79   virtual bool hasDuplicatedConstraint() const
80   { return false; }
81
82   /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
83   ///        shows the distance from the start point.
84   virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase, double theCoeff);
85
86   /// \brief Initialize solver by constraints, entities and parameters
87   virtual void initializeSolver(SolverPtr theSolver);
88
89 protected:
90   /// \brief Remove constraint
91   /// \return \c true if the constraint and all its parameters are removed successfully
92   virtual bool remove(ConstraintWrapperPtr theConstraint);
93   /// \brief Remove entity
94   /// \return \c true if the entity and all its parameters are removed successfully
95   virtual bool remove(EntityWrapperPtr theEntity);
96   /// \brief Remove parameter
97   /// \return \c true if the parameter has been removed
98   virtual bool remove(ParameterWrapperPtr theParameter);
99
100   /// \brief Update the group for the given entity, its sub-entities and parameters
101   virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup);
102   /// \brief Update the group for the given parameter
103   virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup);
104
105
106 // =============   Own methods   =============
107
108
109
110 #ifndef OBSOLETE
111
112 ////  /** \brief Add or change constraint and corresponding GCS constraints to the storage
113 ////   *  \param theConstraint     [in] base SketchPlugin constraint
114 ////   *  \param theGCSConstraints [in] list of GCS constraints created by SketchSolver constraint
115 ////   */
116 ////  void changeConstraint(const ConstraintPtr& theConstraint,
117 ////                        const std::vector<GCSConstraintPtr>& theGCSConstraints);
118
119 ////  /** \brief Add GCS constraint not related to any SketchPlugin constraint.
120 ////   *         For example, it may be related to the moved feature.
121 ////   *  \param theConstraint [in] GCS constraint
122 ////   */
123 ////  void addConstraint(GCSConstraintPtr theConstraint);
124
125 ////  /** \brief Convert SketchPlugin_Feature to the corresponding entity in FreeGCS
126 ////   *  \param theEntity [in] feature to convert
127 ////   *  \return ID of created or updated entity
128 ////   */
129 ////  EntityID changeEntity(const FeaturePtr& theEntity);
130 ////  /** \brief Convert ModelAPI_Attribute to the corresponding entity in FreeGCS
131 ////   *  \param theEntity [in] attribute to convert
132 ////   *  \return ID of created or updated entity
133 ////   */
134 ////  EntityID changeEntity(const AttributePtr& theEntity);
135
136 ////  /** \brief Remove constraint and corresponding GCS constraints
137 ////   *  \param theConstraint [in] constraint to remove
138 ////   *  \return \c true, if the constraint is fully removed,
139 ////   *          \c false, if there are available entities used by other constraints
140 ////   */
141 ////  bool removeConstraint(const ConstraintPtr& theConstraint);
142 ////  /** \brief Remove GCS constraint
143 ////   *  \param theConstraint [in] constraint to remove
144 ////   *  \return \c true, if the constraint is fully removed
145 ////   */
146 ////  bool removeConstraint(GCSConstraintPtr theConstraint);
147 ////  /** \brief Remove feature and corresponding GCS entities
148 ////   *  \param theFeature [in] feature to remove
149 ////   *  \return \c true, if the feature is fully removed,
150 ////   *          \c false, if there are available entities used by other constraints or features
151 ////   */
152 ////  bool removeEntity(const FeaturePtr& theFeature);
153 ////  /** \brief Remove attribute and corresponding GCS entities
154 ////   *  \param theAttribute [in] attribute to remove
155 ////   *  \return \c true, if the attribute is removed,
156 ////   *          \c false, if it is used by existent features or constraints
157 ////   */
158 ////  bool removeEntity(const AttributePtr& theAttribute);
159
160   /// \brief Move parameters of the entity to the constants
161   void makeConstant(const EntityWrapperPtr& theEntity);
162   /// \brief Move parameters of the entity to the variables
163   void makeVariable(const EntityWrapperPtr& theEntity);
164
165 ////  /// \brief Return scalar by its ID or null pointer
166 ////  double*      getScalar(const EntityID& theID) const;
167 ////  /// \brief Return point by its ID or null pointer
168 ////  GCS::Point*  getPoint(const EntityID& theID) const;
169 ////  /// \brief Return line by its ID or null pointer
170 ////  GCS::Line*   getLine(const EntityID& theID) const;
171 ////  /// \brief Return circle by its ID or null pointer
172 ////  GCS::Circle* getCircle(const EntityID& theID) const;
173 ////  /// \brief Return arc by its ID or null pointer
174 ////  GCS::Arc*    getArc(const EntityID& theID) const;
175 ////
176 ////  /// \brief Return list of GCS constraints corresponding to the given
177 ////  const std::vector<GCSConstraintPtr>& getConstraint(const ConstraintPtr& theConstraint) const;
178
179 ////  /// \brief Verifies, the attribute is available in current storage
180 ////  bool isInteract(AttributePtr theAttribute) const;
181 ////  /// \brief Verifies, the feature is available in current storage
182 ////  bool isInteract(FeaturePtr theFeature) const;
183
184 private:
185 ////  /** \brief Creates scalar parameter in the list of parameters
186 ////   *  \param theScalar [in] value to be converted
187 ////   *  \return pointer to new scalar
188 ////   */
189 ////  double* createScalar(const AttributeDoublePtr& theScalar = AttributeDoublePtr());
190 ////  /** \brief Creates two parameters in the list of parameters to store point coordinates
191 ////   *  \param thePoint [in] point to be converted
192 ////   *  \return FreeGCS point
193 ////   */
194 ////  GCS::Point createPoint(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint);
195 ////  /** \brief Creates or updates a scalar value
196 ////   *  \param theScalar [in] attribute to update scalar value
197 ////   *  \param theID     [in] ID of scalar to be updated
198 ////   */
199 ////  void updateScalar(const AttributeDoublePtr& theScalar, EntityID& theID);
200 ////  /** \brief Creates or updates an angular value
201 ////   *  \param theAngle [in] attribute to update scalar value
202 ////   *  \param theID    [in] ID of scalar to be updated
203 ////   */
204 ////  void updateAngle(const AttributeDoublePtr& theAngle, EntityID& theID);
205 ////  /** \brief Creates or updates parameters of the point
206 ////   *  \param thePoint [in] point used to update parameters
207 ////   *  \param theID    [in] ID of FreeGCS entity to be updated
208 ////   */
209 ////  void updatePoint(const std::shared_ptr<GeomDataAPI_Point2D>& thePoint,
210 ////                         EntityID&                             theID);
211 ////  /** \brief Creates or updates parameters of the line
212 ////   *  \param theLine [in]     line used to update parameters
213 ////   *  \param theID   [in/out] ID of FreeGCS entity to be updated (cleared if problems)
214 ////   */
215 ////  void updateLine(const FeaturePtr& theLine, EntityID& theID);
216 ////  /** \brief Creates or updates parameters of the circle
217 ////   *  \param theLine [in]     circle used to update parameters
218 ////   *  \param theID   [in/out] ID of FreeGCS entity to be updated (cleared if problems)
219 ////   */
220 ////  void updateCircle(const FeaturePtr& theCircle, EntityID& theID);
221 ////  /** \brief Creates or updates parameters of the arc
222 ////   *  \param theLine [in]     arc used to update parameters
223 ////   *  \param theID   [in/out] ID of FreeGCS entity to be updated (cleared if problems)
224 ////   */
225 ////  void updateArc(const FeaturePtr& theArc, EntityID& theID);
226
227 ////  /// \brief Check the feature is used by constraints
228 ////  bool isUsed(const FeaturePtr& theFeature) const;
229 ////  /// \brief Check the attribute is used by constraints or features
230 ////  bool isUsed(const AttributePtr& theAttribute) const;
231
232 ////  /// \brief Remove given parameter from the lists of parameters and constants
233 ////  void removeParameters(double* theParam1, double* theParam2 = 0);
234
235   /// \brief Move parameters of the entity from the list of variables to the list of constants
236   ///        and vice versa
237   /// \param theEntity [in]  entity to be changed
238   /// \param theFrom   [out] source list
239   /// \param theTo     [out] destination list
240   void toggleEntity(const EntityWrapperPtr& theEntity, GCS::VEC_pD& theFrom, GCS::VEC_pD& theTo);
241
242   /// \brief Create additional constraints for correct processing of arcs
243   /// \param theArc [in]  updated arc
244   void processArc(const EntityWrapperPtr& theArc);
245
246 private:
247   GCS::VEC_pD                      myParameters;         ///< list of parameters
248   GCS::VEC_pD                      myConst;              ///< list of constants
249   EntityID                         myEntityLastID;       ///< identifier of last added entity
250
251 ////  std::map<ConstraintPtr, std::vector<GCSConstraintPtr> >
252 ////                                   myConstraintsMap;     ///< map SketchPlugin constraints to corresponding GCS constraints
253 ////
254 ////  std::map<AttributePtr, EntityID> myAttributeEntityMap; ///< map attributes to corresponding entities
255 ////  std::map<FeaturePtr, EntityID>   myFeatureEntityMap;   ///< map features to corresponding entities
256 ////
257 ////  std::map<EntityID, double*>      myGCSScalars;         ///< list of scalar values and their IDs
258 ////  std::map<EntityID, GCSPointPtr>  myGCSPoints;          ///< list of points and their IDs
259 ////  std::map<EntityID, GCSCurvePtr>  myGCSEntities;        ///< list of lines, circles and arcs and their IDs
260
261   std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >
262                                   myArcConstraintMap;    ///< additional constraints for correct processing of the arcs
263
264 ////  std::list<double*>               myRemovedParameters;  ///< list of removed parameters
265   std::list<GCSConstraintPtr>      myRemovedConstraints; ///< list of removed constraints to notify solver
266
267 #else
268
269 // ================================================================================================
270 // ===================     OBSOLETE     ===========================================================
271 public:
272   PlaneGCSSolver_Storage();
273
274   /** \brief Add new parameter to the current group
275    *  \param[in] theParam  SolveSpace parameter
276    *  \return the ID of added parameter
277    */
278   Slvs_hParam addParameter(const Slvs_Param& theParam);
279   /** \brief Updates parameter in the current group. If the ID of parameter is zero, the new item will be added
280    *  \param[in] theParam  SolveSpace parameter
281    *  \return the ID of updated/added parameter
282    */
283   Slvs_hParam updateParameter(const Slvs_Param& theParam);
284   /** \brief Removes the parameter by its ID
285    *  \param[in] theParamID  index of parameter to be removed
286    *  \return \c true if the parameter was successfully removed
287    */
288   bool removeParameter(const Slvs_hParam& theParamID);
289   /// \brief Returns the parameter by its ID
290   const Slvs_Param& getParameter(const Slvs_hParam& theParamID) const;
291
292   /** \brief Add new entity to the current group
293    *  \param[in] theEntity  SolveSpace entity
294    *  \return the ID of added entity
295    */
296   Slvs_hEntity addEntity(const Slvs_Entity& theEntity);
297   /** \brief Updates entity in the current group. If the ID of entity is zero, the new item will be added
298    *  \param[in] theEntity  SolveSpace entity
299    *  \return the ID of updated/added entity
300    */
301   Slvs_hEntity updateEntity(const Slvs_Entity& theEntity);
302   /** \brief Removes the entity by its ID. All parameters used in this entity,
303    *         and not used in other constraints, will be removed too.
304    *  \param[in] theEntityID  index of entity to be removed
305    *  \return \c true if the entity was successfully removed
306    */
307   bool removeEntity(const Slvs_hEntity& theEntityID);
308   /** \brief Remove all entities, which are not used in constraints
309    */
310   void removeUnusedEntities();
311   /// \brief Returns the entity by its ID
312   const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
313   /// \brief Makes a full copy of the given entity
314   Slvs_hEntity copyEntity(const Slvs_hEntity& theCopied);
315   /// \brief Copy one entity to another
316   void copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo);
317   /// \brief Check the entity is used in constraints
318   bool isUsedByConstraints(const Slvs_hEntity& theEntityID) const;
319
320   /// \brief Verifies the current point or another coincident one is fixed
321   /// \param[in]  thePointID  entity to be checked fixed
322   /// \param[out] theFixed    ID of constraint
323   /// \param[in]  theAccurate if \c true, the calculation will be made for all type of constraints,
324   ///                         if \c false, only the point is verified
325   /// \return \c true if the point is fixed
326   bool isPointFixed(const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate = false) const;
327   /// \brief Verifies the current entity is fully fixed (may not be changed by constraints)
328   /// \param[in] theEntityID entity to be checked fixed
329   /// \param[in] theAccurate if \c true, the calculation will be made for all type of constraints,
330   ///                        if \c false, only points are verified
331   /// \return \c true if the entity is fixed
332   bool isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate = false) const;
333
334   /** \brief Add new constraint to the current group
335    *  \param[in] theConstraint   SolveSpace's constraint
336    *  \return the ID of added constraint
337    */
338   Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
339   /** \brief Updates constraint in the current group.
340    *         If the ID of constraint is zero, the new item will be added
341    *  \param[in] theConstraint  SolveSpace constraint
342    *  \return the ID of updated/added constraint
343    */
344   Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
345   /** \brief Removes the constraint by its ID. All entities and parameters depending on this
346    *         constraint, which are not used in other constraints, will be removed too.
347    *  \param[in] theConstraintID  index of constraint to be removed
348    *  \return \c true if the constraint was successfully removed
349    */
350   bool removeConstraint(const Slvs_hConstraint& theConstraintID);
351   /// \brief Returns the constraint by its ID
352   const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
353   /// \brief Returns list of constraints of specified type
354   std::list<Slvs_Constraint> getConstraintsByType(int theConstraintType) const;
355
356   /// \brief Attach constraint SLVS_C_WHERE_DRAGGED to this storage. It need to make precise calculations
357   void addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID);
358
359   /// \brief Add transient constraint
360   void addTemporaryConstraint(const Slvs_hConstraint& theConstraintID);
361   /// \brief Remove all transient constraints
362   void removeTemporaryConstraints();
363   /// \brief Remove one temporary constraint. Preferable to remove the points under Point-on-Line constraint
364   /// \return Number of remaining temporary constraints
365   int deleteTemporaryConstraint();
366   /// \brief Checks the constraint is temporary
367   bool isTemporary(const Slvs_hConstraint& theConstraintID) const;
368   /// \brief Number of temporary constraints
369   int numberTemporary() const
370   { return (int)myTemporaryConstraints.size(); }
371
372   /// \brief Shows the sketch should be resolved
373   bool isNeedToResolve();
374
375   /// \brief Shows the storage has the same constraint twice
376   bool hasDuplicatedConstraint() const
377   { return myDuplicatedConstraint; }
378
379   /// \brief Changes the flag of group to be resolved
380   void setNeedToResolve(bool theFlag)
381   { myNeedToResolve = theFlag; }
382
383   /// \brief Returns lists of removed elements
384   void getRemoved(std::set<Slvs_hParam>& theParameters,
385                   std::set<Slvs_hEntity>& theEntities,
386                   std::set<Slvs_hConstraint>& theConstraints);
387
388   /// \brief Initialize constraint solver by the entities collected by current storage
389   void initializeSolver(SketchSolver_Solver& theSolver);
390
391 private:
392   /// \brief Store coincident points
393   void addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
394   /// \brief Remove point from lists of coincidence
395   void removeCoincidentPoint(const Slvs_hEntity& thePoint);
396   /// \brief Remove point-point coincidence
397   void removeCoincidence(const Slvs_Constraint& theCoincidence);
398
399 public:
400   /// \brief Check two points are coincident
401   bool isCoincident(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
402
403   /// \brief Check two points are coincident or have same coordinates
404   bool isEqual(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
405
406   /// \brief Check the entity is horizontal of vertical
407   bool isAxisParallel(const Slvs_hEntity& theEntity) const;
408
409   /// \brief Verifies the entity is used in any equal constraint
410   /// \param[in]  theEntity entity to be found
411   /// \param[out] theEqual  constraint, which uses the entity
412   /// \return \c true, if the Equal constrait is found
413   bool isUsedInEqual(const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const;
414
415   /// \brief Fixes specified entity
416   /// \param theEntity  ID of the entity to be fixed
417   /// \return List of created constraints
418   std::vector<Slvs_hConstraint> fixEntity(const Slvs_hEntity& theEntity);
419
420 private:
421   /// \brief Fixes specified point
422   /// \param [in]  thePoint    point to be fixed
423   /// \param [out] theCreated  list of the Fixed constraints created
424   void fixPoint(const Slvs_Entity& thePoint, std::vector<Slvs_hConstraint>& theCreated);
425   /// \brief Fixes specified line
426   /// \param [in]  theLine     line to be fixed
427   /// \param [out] theCreated  list of the Fixed constraints created
428   void fixLine(const Slvs_Entity& theLine, std::vector<Slvs_hConstraint>& theCreated);
429   /// \brief Fixes specified circle
430   /// \param [in]  theCircle   circle to be fixed
431   /// \param [out] theCreated  list of the Fixed constraints created
432   void fixCircle(const Slvs_Entity& theCircle, std::vector<Slvs_hConstraint>& theCreated);
433   /// \brief Fixes specified arc
434   /// \param [in]  theArc      arc to be fixed
435   /// \param [out] theCreated  list of the Fixed constraints created
436   void fixArc(const Slvs_Entity& theArc, std::vector<Slvs_hConstraint>& theCreated);
437
438 private:
439   Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
440   std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
441   Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
442   std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
443   Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
444   std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
445
446   std::vector< std::set<Slvs_hEntity> > myCoincidentPoints; ///< lists of coincident points
447   Slvs_hConstraint myFixed; ///< identifier of one of temporary constraints to fix separate point
448
449   bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
450   bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
451
452   std::set<Slvs_hConstraint> myTemporaryConstraints; ///< list of transient constraints
453   std::set<Slvs_hParam> myRemovedParameters; ///< list of just removed parameters (cleared when returning to applicant)
454   std::set<Slvs_hEntity> myRemovedEntities; ///< list of just removed entities (cleared when returning to applicant)
455   std::set<Slvs_hConstraint> myRemovedConstraints; ///< list of just removed constraints (cleared when returning to applicant)
456   std::set<Slvs_hParam> myUpdatedParameters; ///< list of just updated parameters (cleared when isNeedToResolve() called)
457 #endif
458 };
459
460 #endif