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