1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SolveSpaceSolver_Storage.h
4 // Created: 18 Mar 2015
5 // Author: Artem ZHIDKOV
7 #ifndef SolveSpaceSolver_Storage_H_
8 #define SolveSpaceSolver_Storage_H_
10 #include <SketchSolver_Storage.h>
11 #include <SolveSpaceSolver_Solver.h>
18 typedef std::map<EntityWrapperPtr, std::set<EntityWrapperPtr> > CoincidentPointsMap;
19 typedef std::list< std::set<ConstraintWrapperPtr> > SameConstraintMap;
21 /** \class SolveSpaceSolver_Storage
23 * \brief Contains all necessary data in SolveSpace format to solve a single group of constraints
25 class SolveSpaceSolver_Storage : public SketchSolver_Storage
28 SolveSpaceSolver_Storage(const GroupID& theGroup);
30 // ============= Inherited from SketchSolver_Storage =============
32 /// \brief Update constraint's data
33 /// \return \c true if any value is updated
34 virtual bool update(ConstraintWrapperPtr& theConstraint);
35 /// \brief Update entity's data
36 /// \return \c true if any value is updated
37 virtual bool update(EntityWrapperPtr& theEntity);
38 /// \brief Update parameter's data
39 /// \return \c true if the value of parameter is updated
40 virtual bool update(ParameterWrapperPtr& theParameter);
42 /// \brief Removes constraint from the storage
43 /// \return \c true if the constraint and all its parameters are remove successfully
44 virtual bool removeConstraint(ConstraintPtr theConstraint);
45 /// \brief Removes feature from the storage
46 /// \return \c true if the feature and its attributes are removed successfully;
47 /// \c false if the feature or any it attribute is used by remaining constraints.
48 virtual bool removeEntity(FeaturePtr theFeature);
49 /// \brief Removes attribute from the storage
50 /// \return \c true if the attribute is not used by remaining features and constraints
51 virtual bool removeEntity(AttributePtr theAttribute);
53 /// \brief Update SketchPlugin features after resolving constraints
54 /// \param theFixedOnly [in] if \c true the fixed points will be updated only
55 virtual void refresh(bool theFixedOnly = false) const;
57 /// \brief Check if some parameters or entities are returned
58 /// to the current group after removing temporary constraints
59 virtual void verifyFixed();
61 /// \brief Mark two points as coincident
62 virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave);
64 /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
65 /// shows the distance from the start point.
66 virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase, double theCoeff);
69 /// \brief Remove constraint
70 /// \return \c true if the constraint and all its parameters are removed successfully
71 virtual bool remove(ConstraintWrapperPtr theConstraint);
72 /// \brief Remove entity
73 /// \return \c true if the entity and all its parameters are removed successfully
74 virtual bool remove(EntityWrapperPtr theEntity);
75 /// \brief Remove parameter
76 /// \return \c true if the parameter has been removed
77 virtual bool remove(ParameterWrapperPtr theParameter);
79 /// \brief Update the group for the given entity, its sub-entities and parameters
80 virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup);
81 /// \brief Update the group for the given parameter
82 virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup);
85 // ============= Own methods =============
87 /// \brief Obtain and store identifier of sketch
88 void storeWorkplane(EntityWrapperPtr theSketch);
90 /** \brief Add new parameter to the current group
91 * \param[in] theParam SolveSpace parameter
92 * \return the ID of added parameter
94 Slvs_hParam addParameter(const Slvs_Param& theParam);
95 /** \brief Updates parameter in the current group. If the ID of parameter is zero, the new item will be added
96 * \param[in] theParam SolveSpace parameter
97 * \return the ID of updated/added parameter
99 Slvs_hParam updateParameter(const Slvs_Param& theParam);
100 /** \brief Removes the parameter by its ID
101 * \param[in] theParamID index of parameter to be removed
102 * \return \c true if the parameter was successfully removed
104 bool removeParameter(const Slvs_hParam& theParamID);
105 /// \brief Returns the parameter by its ID
106 const Slvs_Param& getParameter(const Slvs_hParam& theParamID) const;
108 /** \brief Add new entity to the current group
109 * \param[in] theEntity SolveSpace entity
110 * \return the ID of added entity
112 Slvs_hEntity addEntity(const Slvs_Entity& theEntity);
113 /** \brief Updates entity in the current group. If the ID of entity is zero, the new item will be added
114 * \param[in] theEntity SolveSpace entity
115 * \return the ID of updated/added entity
117 Slvs_hEntity updateEntity(const Slvs_Entity& theEntity);
118 /** \brief Removes the entity by its ID. All parameters used in this entity,
119 * and not used in other constraints, will be removed too.
120 * \param[in] theEntityID index of entity to be removed
121 * \return \c true if the entity was successfully removed
123 bool removeEntity(const Slvs_hEntity& theEntityID);
124 /** \brief Remove all entities, which are not used in constraints
126 void removeUnusedEntities();
127 /// \brief Returns the entity by its ID
128 const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
129 /// \brief Makes a full copy of the given entity
130 Slvs_hEntity copyEntity(const Slvs_hEntity& theCopied);
131 /// \brief Copy one entity to another
132 void copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo);
133 /// \brief Check the entity is used in constraints
134 bool isUsedByConstraints(const Slvs_hEntity& theEntityID) const;
135 /// \brief Returns maximal ID of entities in this storage
136 const Slvs_hEntity& entityMaxID() const
137 { return myEntityMaxID; }
139 /// \brief Verifies the current point or another coincident one is fixed
140 /// \param[in] thePointID entity to be checked fixed
141 /// \param[out] theFixed ID of constraint
142 /// \param[in] theAccurate if \c true, the calculation will be made for all type of constraints,
143 /// if \c false, only the point is verified
144 /// \return \c true if the point is fixed
145 bool isPointFixed(const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate = false) const;
146 /// \brief Verifies the current entity is fully fixed (may not be changed by constraints)
147 /// \param[in] theEntityID entity to be checked fixed
148 /// \param[in] theAccurate if \c true, the calculation will be made for all type of constraints,
149 /// if \c false, only points are verified
150 /// \return \c true if the entity is fixed
151 bool isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate = false) const;
153 /** \brief Add new constraint to the current group
154 * \param[in] theConstraint SolveSpace's constraint
155 * \return the ID of added constraint
157 Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
158 /** \brief Updates constraint in the current group.
159 * If the ID of constraint is zero, the new item will be added
160 * \param[in] theConstraint SolveSpace constraint
161 * \return the ID of updated/added constraint
163 Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
164 /** \brief Removes the constraint by its ID. All entities and parameters depending on this
165 * constraint, which are not used in other constraints, will be removed too.
166 * \param[in] theConstraintID index of constraint to be removed
167 * \return \c true if the constraint was successfully removed
169 bool removeConstraint(const Slvs_hConstraint& theConstraintID);
170 /// \brief Returns the constraint by its ID
171 const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
172 /// \brief Returns list of constraints of specified type
173 std::list<Slvs_Constraint> getConstraintsByType(int theConstraintType) const;
174 /// \brief Returns quantity of constraints in this storage
175 size_t nbConstraints() const
176 { return myConstraints.size(); }
178 /// \brief Attach constraint SLVS_C_WHERE_DRAGGED to this storage. It need to make precise calculations
179 void addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID);
181 /// \brief Add transient constraint
182 void addTemporaryConstraint(const Slvs_hConstraint& theConstraintID);
183 /// \brief Mark specified constraint as temporary
184 virtual void setTemporary(ConstraintPtr theConstraint);
185 /// \brief Remove all transient constraints
186 void removeAllTemporary();
187 /// \brief Remove temporary constraint s. Preferable to remove the points under Point-on-Line constraint
188 /// \param theNbConstraints [in] number of temporary constraints to be deleted
189 /// \return Number of remaining temporary constraints
190 virtual size_t removeTemporary(size_t theNbConstraints);
191 /// \brief Checks the constraint is temporary
192 bool isTemporary(const Slvs_hConstraint& theConstraintID) const;
193 /// \brief Number of temporary constraints
194 virtual size_t nbTemporary() const
195 { return myTemporaryConstraints.size(); }
197 /// \brief Shows the storage has the same constraint twice
198 virtual bool hasDuplicatedConstraint() const
199 { return myDuplicatedConstraint; }
201 /// \brief Initialize constraint solver by the entities collected by current storage
202 virtual void initializeSolver(SolverPtr theSolver);
205 /// \brief Check two points are coincident or have same coordinates
206 bool isEqual(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
208 /// \brief Check the entity is horizontal of vertical
209 bool isAxisParallel(const Slvs_hEntity& theEntity) const;
211 /// \brief Verifies the entity is used in any equal constraint
212 /// \param[in] theEntity entity to be found
213 /// \param[out] theEqual constraint, which uses the entity
214 /// \return \c true, if the Equal constrait is found
215 bool isUsedInEqual(const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const;
217 /// \brief Fixes specified entity
218 /// \param theEntity ID of the entity to be fixed
219 /// \return List of created constraints
220 std::vector<Slvs_hConstraint> fixEntity(const Slvs_hEntity& theEntity);
223 /// \brief Fixes specified point
224 /// \param [in] thePoint point to be fixed
225 /// \param [out] theCreated list of the Fixed constraints created
226 void fixPoint(const Slvs_Entity& thePoint, std::vector<Slvs_hConstraint>& theCreated);
227 /// \brief Fixes specified line
228 /// \param [in] theLine line to be fixed
229 /// \param [out] theCreated list of the Fixed constraints created
230 void fixLine(const Slvs_Entity& theLine, std::vector<Slvs_hConstraint>& theCreated);
231 /// \brief Fixes specified circle
232 /// \param [in] theCircle circle to be fixed
233 /// \param [out] theCreated list of the Fixed constraints created
234 void fixCircle(const Slvs_Entity& theCircle, std::vector<Slvs_hConstraint>& theCreated);
235 /// \brief Fixes specified arc
236 /// \param [in] theArc arc to be fixed
237 /// \param [out] theCreated list of the Fixed constraints created
238 void fixArc(const Slvs_Entity& theArc, std::vector<Slvs_hConstraint>& theCreated);
240 /// \brief Update arc points to be on circle sharp.
241 void adjustArc(const Slvs_Entity& theArc);
243 /// \brief Verify the feature or any its attribute is used by constraint
244 bool isUsed(FeaturePtr theFeature) const;
245 /// \brief Verify the attribute is used by constraint
246 bool isUsed(AttributePtr theAttirubute) const;
248 /// \brief Replace sub-entity theSource in all features by theDest
249 void replaceInFeatures(EntityWrapperPtr theSource, EntityWrapperPtr theDest);
250 /// \brief Replace constrained entity theSource by theDest in all constraints;
251 void replaceInConstraints(EntityWrapperPtr theSource, EntityWrapperPtr theDest);
253 /// \brief Add pair of constraints which have same representation in SolveSpace notation.
255 /// These constraints may be different and become the same after the substitution
256 /// of point coincidence.
257 void addSameConstraints(ConstraintWrapperPtr theConstraint1, ConstraintWrapperPtr theConstraint2);
260 Slvs_hEntity myWorkplaneID; ///< identifier of workplane
262 Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
263 std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
264 Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
265 std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
266 Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
267 std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
269 CoincidentPointsMap myCoincidentPoints; ///< lists of coincident points (first is a master point, second is a set of slaves)
270 Slvs_hConstraint myFixed; ///< identifier of one of temporary constraints to fix separate point
272 bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
274 std::set<Slvs_hConstraint> myTemporaryConstraints; ///< list of transient constraints
275 std::set<Slvs_hParam> myUpdatedParameters; ///< list of just updated parameters (cleared when isNeedToResolve() called)
277 SameConstraintMap myEqualConstraints; ///< list of groups of equal constraints