Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchSolver / SolveSpaceSolver / SolveSpaceSolver_Storage.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef SolveSpaceSolver_Storage_H_
22 #define SolveSpaceSolver_Storage_H_
23
24 #include <SketchSolver_Storage.h>
25 #include <SolveSpaceSolver_Solver.h>
26
27 #include <list>
28 #include <memory>
29 #include <set>
30 #include <vector>
31
32 typedef std::list< std::set<ConstraintWrapperPtr> >             SameConstraintMap;
33
34 /** \class   SolveSpaceSolver_Storage
35  *  \ingroup Plugins
36  *  \brief   Contains all necessary data in SolveSpace format to solve a single 
37  *  group of constraints
38  */
39 class SolveSpaceSolver_Storage : public SketchSolver_Storage
40 {
41 public:
42   SolveSpaceSolver_Storage(const GroupID& theGroup);
43
44 // =============   Inherited from SketchSolver_Storage   =============
45
46   /// \brief Update constraint's data
47   /// \return \c true if any value is updated
48   virtual bool update(ConstraintWrapperPtr theConstraint);
49   /// \brief Update entity's data
50   /// \return \c true if any value is updated
51   virtual bool update(EntityWrapperPtr theEntity);
52   /// \brief Update parameter's data
53   /// \return \c true if the value of parameter is updated
54   virtual bool update(ParameterWrapperPtr theParameter);
55
56   /// \brief Update SketchPlugin features after resolving constraints
57   /// \param theFixedOnly [in]  if \c true the fixed points will be updated only
58   virtual void refresh(bool theFixedOnly = false) const;
59
60   /// \brief Check if some parameters or entities are returned
61   ///        to the current group after removing temporary constraints
62   virtual void verifyFixed();
63
64   /// \brief Mark two points as coincident
65   virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave);
66
67   /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
68   ///        shows the distance from the start point.
69   virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase, double theCoeff);
70
71 protected:
72   /// \brief Remove constraint
73   /// \return \c true if the constraint and all its parameters are removed successfully
74   virtual bool remove(ConstraintWrapperPtr theConstraint);
75   /// \brief Remove entity
76   /// \return \c true if the entity and all its parameters are removed successfully
77   virtual bool remove(EntityWrapperPtr theEntity);
78   /// \brief Remove parameter
79   /// \return \c true if the parameter has been removed
80   virtual bool remove(ParameterWrapperPtr theParameter);
81
82   /// \brief Update the group for the given entity, its sub-entities and parameters
83   virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup);
84   /// \brief Update the group for the given parameter
85   virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup);
86
87
88 // =============   Own methods   =============
89 public:
90   /// \brief Obtain and store identifier of sketch
91   void storeWorkplane(EntityWrapperPtr theSketch);
92
93   /** \brief Add new parameter to the current group
94    *  \param[in] theParam  SolveSpace parameter
95    *  \return the ID of added parameter
96    */
97   Slvs_hParam addParameter(const Slvs_Param& theParam);
98   /** \brief Updates parameter in the current group. If the ID of parameter is zero, the new item will be added
99    *  \param[in] theParam  SolveSpace parameter
100    *  \return the ID of updated/added parameter
101    */
102   Slvs_hParam updateParameter(const Slvs_Param& theParam);
103   /** \brief Removes the parameter by its ID
104    *  \param[in] theParamID  index of parameter to be removed
105    *  \return \c true if the parameter was successfully removed
106    */
107   bool removeParameter(const Slvs_hParam& theParamID);
108   /// \brief Returns the parameter by its ID
109   const Slvs_Param& getParameter(const Slvs_hParam& theParamID) const;
110
111   /** \brief Add new entity to the current group
112    *  \param[in] theEntity  SolveSpace entity
113    *  \return the ID of added entity
114    */
115   Slvs_hEntity addEntity(const Slvs_Entity& theEntity);
116   /** \brief Updates entity in the current group. If the ID of entity is zero, the new item will be added
117    *  \param[in] theEntity  SolveSpace entity
118    *  \return the ID of updated/added entity
119    */
120   Slvs_hEntity updateEntity(const Slvs_Entity& theEntity);
121   /** \brief Removes the entity by its ID. All parameters used in this entity,
122    *         and not used in other constraints, will be removed too.
123    *  \param[in] theEntityID  index of entity to be removed
124    *  \return \c true if the entity was successfully removed
125    */
126   bool removeEntity(const Slvs_hEntity& theEntityID);
127   /// \brief Returns the entity by its ID
128   const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
129   /// \brief Returns maximal ID of entities in this storage
130   const Slvs_hEntity& entityMaxID() const
131   { return myEntityMaxID; }
132
133   /** \brief Add new constraint to the current group
134    *  \param[in] theConstraint   SolveSpace's constraint
135    *  \return the ID of added constraint
136    */
137   Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
138   /** \brief Updates constraint in the current group.
139    *         If the ID of constraint is zero, the new item will be added
140    *  \param[in] theConstraint  SolveSpace constraint
141    *  \return the ID of updated/added constraint
142    */
143   Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
144   /** \brief Removes the constraint by its ID. All entities and parameters depending on this
145    *         constraint, which are not used in other constraints, will be removed too.
146    *  \param[in] theConstraintID  index of constraint to be removed
147    *  \return \c true if the constraint was successfully removed
148    */
149   bool removeConstraint(const Slvs_hConstraint& theConstraintID);
150   /// \brief Returns the constraint by its ID
151   const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
152   /// \brief Returns quantity of constraints in this storage
153   size_t nbConstraints() const
154   { return myConstraints.size(); }
155
156   /// \brief Shows the storage has the same constraint twice
157   virtual bool hasDuplicatedConstraint() const
158   { return myDuplicatedConstraint; }
159
160   /// \brief Initialize constraint solver by the entities collected by current storage
161   virtual void initializeSolver(SolverPtr theSolver);
162
163 private:
164   /// \brief Update arc points to be on circle sharp.
165   void adjustArc(const Slvs_Entity& theArc);
166
167   /// \brief Replace sub-entity theSource in all features by theDest
168   void replaceInFeatures(EntityWrapperPtr theSource, EntityWrapperPtr theDest);
169   /// \brief Replace constrained entity theSource by theDest in all constraints;
170   void replaceInConstraints(EntityWrapperPtr theSource, EntityWrapperPtr theDest);
171
172   /// \brief Add pair of constraints which have same representation in SolveSpace notation.
173   ///
174   ///        These constraints may be different and become the same after the substitution
175   ///        of point coincidence.
176   void addSameConstraints(ConstraintWrapperPtr theConstraint1,
177                           ConstraintWrapperPtr theConstraint2);
178
179   /// \brief Search constraint equal to the given in terms of SolveSpace notation
180   bool findSameConstraint(ConstraintWrapperPtr theConstraint);
181
182 private:
183   Slvs_hEntity myWorkplaneID; ///< identifier of workplane
184
185   /// current parameter index (may differs with the number of parameters)
186   Slvs_hParam myParamMaxID;
187
188   /// list of parameters used in the current group of constraints (sorted by the identifier)
189   std::vector<Slvs_Param> myParameters;
190   Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
191
192   /// list of entities used in the current group of constraints (sorted by the identifier)
193   std::vector<Slvs_Entity> myEntities;
194
195   /// current constraint index (may differs with the number of constraints)
196   Slvs_hConstraint myConstrMaxID;
197
198   /// list of constraints used in the current group (sorted by the identifier)
199   std::vector<Slvs_Constraint> myConstraints;
200
201   bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
202
203   SameConstraintMap myEqualConstraints; ///< list of groups of equal constraints
204 };
205
206 #endif