Salome HOME
Minor changes
[modules/shaper.git] / src / SketchSolver / SketchSolver_Storage.h
index 669f2c0eacc12fd31ef1491d0ef558ed982b3c33..454044c8826f42fea46eee82a5c57d6a89751558 100644 (file)
 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
 
 // File:    SketchSolver_Storage.h
-// Created: 18 Mar 2015
+// Created: 30 Nov 2015
 // Author:  Artem ZHIDKOV
 
 #ifndef SketchSolver_Storage_H_
 #define SketchSolver_Storage_H_
 
-#include "SketchSolver.h"
-#include <SketchSolver_Solver.h>
+#include <SketchSolver.h>
+#include <SketchSolver_IConstraintWrapper.h>
+#include <SketchSolver_IEntityWrapper.h>
+#include <SketchSolver_IParameterWrapper.h>
+#include <SketchSolver_ISolver.h>
+
+#include <ModelAPI_Attribute.h>
+#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_Feature.h>
+#include <SketchPlugin_Constraint.h>
+
+class SketchSolver_ConstraintDistance;
+class SketchSolver_ConstraintFixedArcRadius;
+typedef std::map<EntityWrapperPtr, std::set<EntityWrapperPtr> > CoincidentPointsMap;
 
-#include <list>
-#include <memory>
-#include <set>
-#include <vector>
 
 /** \class   SketchSolver_Storage
  *  \ingroup Plugins
- *  \brief   Contains all necessary data in SolveSpace format to solve a single group of constraints
+ *  \brief   Interface to map SketchPlugin features to the entities of corresponding solver.
  */
 class SketchSolver_Storage
 {
-public:
+private:
   SketchSolver_Storage();
+  SketchSolver_Storage(const SketchSolver_Storage&);
+  SketchSolver_Storage& operator=(const SketchSolver_Storage&);
 
-  /** \brief Add new parameter to the current group
-   *  \param[in] theParam  SolveSpace parameter
-   *  \return the ID of added parameter
-   */
-  Slvs_hParam addParameter(const Slvs_Param& theParam);
-  /** \brief Updates parameter in the current group. If the ID of parameter is zero, the new item will be added
-   *  \param[in] theParam  SolveSpace parameter
-   *  \return the ID of updated/added parameter
-   */
-  Slvs_hParam updateParameter(const Slvs_Param& theParam);
-  /** \brief Removes the parameter by its ID
-   *  \param[in] theParamID  index of parameter to be removed
-   *  \return \c true if the parameter was successfully removed
-   */
-  bool removeParameter(const Slvs_hParam& theParamID);
-  /// \brief Returns the parameter by its ID
-  const Slvs_Param& getParameter(const Slvs_hParam& theParamID) const;
-
-  /** \brief Add new entity to the current group
-   *  \param[in] theEntity  SolveSpace entity
-   *  \return the ID of added entity
-   */
-  Slvs_hEntity addEntity(const Slvs_Entity& theEntity);
-  /** \brief Updates entity in the current group. If the ID of entity is zero, the new item will be added
-   *  \param[in] theEntity  SolveSpace entity
-   *  \return the ID of updated/added entity
-   */
-  Slvs_hEntity updateEntity(const Slvs_Entity& theEntity);
-  /** \brief Removes the entity by its ID. All parameters used in this entity,
-   *         and not used in other constraints, will be removed too.
-   *  \param[in] theEntityID  index of entity to be removed
-   *  \return \c true if the entity was successfully removed
-   */
-  bool removeEntity(const Slvs_hEntity& theEntityID);
-  /// \brief Returns the entity by its ID
-  const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
-  /// \brief Makes a full copy of the given entity
-  Slvs_hEntity copyEntity(const Slvs_hEntity& theCopied);
-  /// \brief Copy one entity to another
-  void copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo);
-
-  /// \brief Verifies the current point or another coincident one is fixed
-  /// \param[in]  thePointID  entity to be checked fixed
-  /// \param[out] theFixed    ID of constraint
-  /// \param[in]  theAccurate if \c true, the calculation will be made for all type of constraints,
-  ///                         if \c false, only the point is verified
-  /// \return \c true if the point is fixed
-  bool isPointFixed(const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate = false) const;
-  /// \brief Verifies the current entity is fully fixed (may not be changed by constraints)
-  /// \param[in] theEntityID entity to be checked fixed
-  /// \param[in] theAccurate if \c true, the calculation will be made for all type of constraints,
-  ///                        if \c false, only points are verified
-  /// \return \c true if the entity is fixed
-  bool isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate = false) const;
-
-  /** \brief Add new constraint to the current group
-   *  \param[in] theConstraint   SolveSpace's constraint
-   *  \return the ID of added constraint
-   */
-  Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
-  /** \brief Updates constraint in the current group.
-   *         If the ID of constraint is zero, the new item will be added
-   *  \param[in] theConstraint  SolveSpace constraint
-   *  \return the ID of updated/added constraint
-   */
-  Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
-  /** \brief Removes the constraint by its ID. All entities and parameters depending on this
-   *         constraint, which are not used in other constraints, will be removed too.
-   *  \param[in] theConstraintID  index of constraint to be removed
-   *  \return \c true if the constraint was successfully removed
-   */
-  bool removeConstraint(const Slvs_hConstraint& theConstraintID);
-  /// \brief Returns the constraint by its ID
-  const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
-  /// \brief Returns list of constraints of specified type
-  std::list<Slvs_Constraint> getConstraintsByType(int theConstraintType) const;
-
-  /// \brief Attach constraint SLVS_C_WHERE_DRAGGED to this storage. It need to make precise calculations
-  void addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID);
-
-  /// \brief Add transient constraint
-  void addTemporaryConstraint(const Slvs_hConstraint& theConstraintID);
-  /// \brief Remove all transient constraints
-  void removeTemporaryConstraints();
-  /// \brief Remove one temporary constraint. Preferable to remove the points under Point-on-Line constraint
-  /// \return Number of remaining temporary constraints
-  int deleteTemporaryConstraint();
-  /// \brief Checks the constraint is temporary
-  bool isTemporary(const Slvs_hConstraint& theConstraintID) const;
+public:
+  SketchSolver_Storage(const GroupID& theGroup)
+    : myGroupID(theGroup),
+      myNeedToResolve(false),
+      myEventsBlocked(false),
+      myExistArc(false)
+  {}
+
+  /// \brief Change mapping between constraint from SketchPlugin and
+  ///        a constraint applicable for corresponding solver.
+  /// \param theConstraint       [in]   original SketchPlugin constraint
+  /// \param theSolverConstraint [in]   solver's constraints
+  SKETCHSOLVER_EXPORT void addConstraint(ConstraintPtr        theConstraint,
+                                         ConstraintWrapperPtr theSolverConstraints);
+  /// \brief Change mapping between constraint from SketchPlugin and
+  ///        the list of constraints applicable for corresponding solver.
+  /// \param theConstraint        [in]   original SketchPlugin constraint
+  /// \param theSolverConstraints [in]   list of solver's constraints
+  SKETCHSOLVER_EXPORT virtual
+    void addConstraint(ConstraintPtr                   theConstraint,
+                       std::list<ConstraintWrapperPtr> theSolverConstraints);
+
+  /// \brief Convert feature to the form applicable for specific solver and map it
+  /// \param theFeature [in]  feature to convert
+  /// \param theGroup   [in]  id of the group where the feature should be placed
+  /// \return \c true if the feature has been created or updated
+  SKETCHSOLVER_EXPORT bool update(FeaturePtr theFeature, const GroupID& theGroup = GID_UNKNOWN);
+  /// \brief Convert attribute to the form applicable for specific solver and map it
+  /// \param theFeature [in]  feature to convert
+  /// \return \c true if the attribute has been created or updated
+  SKETCHSOLVER_EXPORT bool update(AttributePtr theAttribute, const GroupID& theGroup = GID_UNKNOWN);
+
+  /// \brief Returns constraint related to corresponding constraint
+  SKETCHSOLVER_EXPORT
+    const std::list<ConstraintWrapperPtr>& constraint(const ConstraintPtr& theConstraint) const;
+
+  /// \brief Returns entity related to corresponding feature
+  SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const FeaturePtr& theFeature) const;
+  /// \brief Returns entity related to corresponding attribute
+  SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const AttributePtr& theAttribute) const;
+
+  /// \brief Return parsed sketch entity
+  const EntityWrapperPtr& sketch() const;
+  /// \brief Set parsed sketch entity.
+  /// Be careful, this method does not update fields of the storage specific for the solver.
+  /// Does not update if the sketch already exists.
+  void setSketch(const EntityWrapperPtr& theSketch);
+
+  /// \brief Mark two points as coincident
+  virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave) = 0;
+
+  /// \brief Shows the storage has any constraint twice
+  virtual bool hasDuplicatedConstraint() const = 0;
+
+  /// \brief Removes constraint from the storage
+  /// \return \c true if the constraint and all its parameters are removed successfully
+  SKETCHSOLVER_EXPORT bool removeConstraint(ConstraintPtr theConstraint);
+  /// \brief Removes feature from the storage
+  /// \return \c true if the feature and its attributes are removed successfully;
+  ///         \c false if the feature or any it attribute is used by remaining constraints.
+  SKETCHSOLVER_EXPORT bool removeEntity(FeaturePtr theFeature);
+  /// \brief Removes attribute from the storage
+  /// \return \c true if the attribute is not used by remaining features and constraints
+  SKETCHSOLVER_EXPORT bool removeEntity(AttributePtr theAttribute);
+
+  /// \brief Remove all features became invalid
+  SKETCHSOLVER_EXPORT void removeInvalidEntities();
+
+  /// \brief Check whether the feature or its attributes are used by this storage
+  /// \param theFeature [in]  feature to be checked
+  /// \return \c true if the feature interacts with the storage
+  bool isInteract(const FeaturePtr& theFeature) const;
+  /// \brief Check whether the attribute is used by this storage
+  /// \param theAttribute [in]  attribute to be checked
+  /// \return \c true if the attribute interacts with the storage
+  bool isInteract(const AttributePtr& theAttribute) const;
+
+  /// \brief Check the features is not removed
+  bool isConsistent() const;
+
+  /// \brief Check the storage has constraints
+  bool isEmpty() const
+  { return myConstraintMap.empty(); }
+
+  /// \brief Check the entity is fixed.
+  ///        If the point is under verification, all coincident points are checked too.
+  SKETCHSOLVER_EXPORT bool isFixed(EntityWrapperPtr theEntity) const;
 
   /// \brief Shows the sketch should be resolved
-  bool isNeedToResolve() const
-  { return myNeedToResolve && !myConstraints.empty(); }
-
-  /// \brief Shows the storage has the same constraint twice
-  bool hasDuplicatedConstraint() const
-  { return myDuplicatedConstraint; }
-
+  virtual bool isNeedToResolve()
+  { return myNeedToResolve; }
   /// \brief Changes the flag of group to be resolved
   void setNeedToResolve(bool theFlag)
   { myNeedToResolve = theFlag; }
 
-  /// \brief Returns lists of removed elements
-  void getRemoved(std::set<Slvs_hParam>& theParameters,
-                  std::set<Slvs_hEntity>& theEntities,
-                  std::set<Slvs_hConstraint>& theConstraints);
-
-  /// \brief Initialize constraint solver by the entities collected by current storage
-  void initializeSolver(SketchSolver_Solver& theSolver);
-
-private:
-  /// \brief Store coincident points
-  void addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
-  /// \brief Remove point from lists of coincidence
-  void removeCoincidentPoint(const Slvs_hEntity& thePoint);
-
-public:
-  /// \brief Check two points are coincident
-  bool isCoincident(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
+  /// \brief Initialize solver by constraints, entities and parameters
+  virtual void initializeSolver(SolverPtr theSolver) = 0;
+  /// \brief Return list of conflicting constraints
+  std::set<ObjectPtr> getConflictingConstraints(SolverPtr theSolver) const;
+
+  /// \brief Update SketchPlugin features after resolving constraints
+  /// \param theFixedOnly [in]  if \c true the fixed points will be updated only
+  virtual void refresh(bool theFixedOnly = false) const = 0;
+
+  /// \brief Check if some parameters or entities are returned
+  ///        to the current group after removing temporary constraints
+  virtual void verifyFixed() = 0;
+
+  /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
+  ///        shows the distance from the start point.
+  virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase,
+                                                double theCoeff) = 0;
+
+  /// \brief Block or unblock events when refreshing features
+  SKETCHSOLVER_EXPORT void blockEvents(bool isBlocked);
+  /// \brief Shows the events are blocked for the features in the storage
+  bool isEventsBlocked() const
+  { return myEventsBlocked; }
+
+protected:
+  /// \brief Change mapping feature from SketchPlugin and
+  ///        the entity applicable for corresponding solver.
+  /// \param theFeature      [in]  original SketchPlugin feature
+  /// \param theSolverEntity [in]  solver's entity, created outside
+  SKETCHSOLVER_EXPORT
+    void addEntity(FeaturePtr       theFeature,
+                   EntityWrapperPtr theSolverEntity);
+
+  /// \brief Change mapping attribute of a feature and the entity applicable for corresponding solver.
+  /// \param theAttribute    [in]  original attribute
+  /// \param theSolverEntity [in]  solver's entity, created outside
+  SKETCHSOLVER_EXPORT
+    void addEntity(AttributePtr     theAttribute,
+                   EntityWrapperPtr theSolverEntity);
+
+  /// \brief Update constraint's data
+  /// \return \c true if any value is updated
+  virtual bool update(ConstraintWrapperPtr theConstraint) = 0;
+  /// \brief Update entity's data
+  /// \return \c true if any value is updated
+  virtual bool update(EntityWrapperPtr theEntity) = 0;
+  /// \brief Update parameter's data
+  /// \return \c true if the value of parameter is updated
+  virtual bool update(ParameterWrapperPtr theParameter) = 0;
+
+  /// \brief Remove constraint
+  /// \return \c true if the constraint and all its parameters are removed successfully
+  SKETCHSOLVER_EXPORT virtual bool remove(ConstraintWrapperPtr theConstraint);
+  /// \brief Remove entity
+  /// \return \c true if the entity and all its parameters are removed successfully
+  SKETCHSOLVER_EXPORT virtual bool remove(EntityWrapperPtr theEntity);
+  /// \brief Remove parameter
+  /// \return \c true if the parameter has been removed
+  virtual bool remove(ParameterWrapperPtr theParameter) = 0;
+
+  /// \brief Update the group for the given entity, its sub-entities and parameters
+  virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup) = 0;
+  /// \brief Update the group for the given parameter
+  virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup) = 0;
+
+  /// \brief Verify the feature or any its attribute is used by constraint
+  SKETCHSOLVER_EXPORT bool isUsed(FeaturePtr theFeature) const;
+  /// \brief Verify the attribute is used by constraint
+  SKETCHSOLVER_EXPORT bool isUsed(AttributePtr theAttirubute) const;
+
+  /// \brief Find arcs without corresponding entity applicable for the solver and build them
+  SKETCHSOLVER_EXPORT void processArcs();
 
 private:
-  Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
-  std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
-  Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
-  std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
-  Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
-  std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
-
-  std::vector< std::set<Slvs_hEntity> > myCoincidentPoints; ///< lists of coincident points
-  Slvs_hConstraint myFixed; ///< identifier of one of temporary constraints to fix separate point
-
-  bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
-  bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
-
-  std::set<Slvs_hConstraint> myTemporaryConstraints; ///< list of transient constraints
-  std::set<Slvs_hParam> myRemovedParameters; ///< list of just removed parameters (cleared when returning to applicant)
-  std::set<Slvs_hEntity> myRemovedEntities; ///< list of just removed entities (cleared when returning to applicant)
-  std::set<Slvs_hConstraint> myRemovedConstraints; ///< list of just removed constraints (cleared when returning to applicant)
+  /// \brief Find the normal of the sketch
+  EntityWrapperPtr getNormal() const;
+
+protected:
+  GroupID myGroupID;       ///< identifier of the group, this storage belongs to
+  bool    myNeedToResolve; ///< parameters are changed and group needs to be resolved
+  bool    myEventsBlocked; ///< indicates that features do not send events
+  bool    myExistArc;      ///< the storage has any point of arc but not full arc, need to add it
+
+  /// map SketchPlugin constraint to a list of solver's constraints
+  std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> > myConstraintMap;
+  /// map SketchPlugin feature to solver's entity
+  std::map<FeaturePtr, EntityWrapperPtr>                    myFeatureMap;
+  /// map attribute to solver's entity
+  std::map<AttributePtr, EntityWrapperPtr>                  myAttributeMap;
+
+  CoincidentPointsMap myCoincidentPoints; ///< lists of coincident points (first is a master point, second is a set of slaves)
+
+  // to be able to update entities from constraints
+  friend class SketchSolver_ConstraintDistance;
+  friend class SketchSolver_ConstraintFixedArcRadius;
 };
 
 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;