1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Storage.h
4 // Created: 18 Mar 2015
5 // Author: Artem ZHIDKOV
7 #ifndef SketchSolver_Storage_H_
8 #define SketchSolver_Storage_H_
10 #include "SketchSolver.h"
11 #include <SketchSolver_Solver.h>
18 /** \class SketchSolver_Storage
20 * \brief Contains all necessary data in SolveSpace format to solve a single group of constraints
22 class SketchSolver_Storage
25 SketchSolver_Storage();
27 /** \brief Add new parameter to the current group
28 * \param[in] theParam SolveSpace parameter
29 * \return the ID of added parameter
31 Slvs_hParam addParameter(const Slvs_Param& theParam);
32 /** \brief Updates parameter in the current group. If the ID of parameter is zero, the new item will be added
33 * \param[in] theParam SolveSpace parameter
34 * \return the ID of updated/added parameter
36 Slvs_hParam updateParameter(const Slvs_Param& theParam);
37 /** \brief Removes the parameter by its ID
38 * \param[in] theParamID index of parameter to be removed
39 * \return \c true if the parameter was successfully removed
41 bool removeParameter(const Slvs_hParam& theParamID);
42 /// \brief Returns the parameter by its ID
43 const Slvs_Param& getParameter(const Slvs_hParam& theParamID) const;
45 /** \brief Add new entity to the current group
46 * \param[in] theEntity SolveSpace entity
47 * \return the ID of added entity
49 Slvs_hEntity addEntity(const Slvs_Entity& theEntity);
50 /** \brief Updates entity in the current group. If the ID of entity is zero, the new item will be added
51 * \param[in] theEntity SolveSpace entity
52 * \return the ID of updated/added entity
54 Slvs_hEntity updateEntity(const Slvs_Entity& theEntity);
55 /** \brief Removes the entity by its ID. All parameters used in this entity,
56 * and not used in other constraints, will be removed too.
57 * \param[in] theEntityID index of entity to be removed
58 * \return \c true if the entity was successfully removed
60 bool removeEntity(const Slvs_hEntity& theEntityID);
61 /** \brief Remove all entities, which are not used in constraints
63 void removeUnusedEntities();
64 /// \brief Returns the entity by its ID
65 const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
66 /// \brief Makes a full copy of the given entity
67 Slvs_hEntity copyEntity(const Slvs_hEntity& theCopied);
68 /// \brief Copy one entity to another
69 void copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo);
70 /// \brief Check the entity is used in constraints
71 bool isUsedByConstraints(const Slvs_hEntity& theEntityID) const;
73 /// \brief Verifies the current point or another coincident one is fixed
74 /// \param[in] thePointID entity to be checked fixed
75 /// \param[out] theFixed ID of constraint
76 /// \param[in] theAccurate if \c true, the calculation will be made for all type of constraints,
77 /// if \c false, only the point is verified
78 /// \return \c true if the point is fixed
79 bool isPointFixed(const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate = false) const;
80 /// \brief Verifies the current entity is fully fixed (may not be changed by constraints)
81 /// \param[in] theEntityID entity to be checked fixed
82 /// \param[in] theAccurate if \c true, the calculation will be made for all type of constraints,
83 /// if \c false, only points are verified
84 /// \return \c true if the entity is fixed
85 bool isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate = false) const;
87 /** \brief Add new constraint to the current group
88 * \param[in] theConstraint SolveSpace's constraint
89 * \return the ID of added constraint
91 Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
92 /** \brief Updates constraint in the current group.
93 * If the ID of constraint is zero, the new item will be added
94 * \param[in] theConstraint SolveSpace constraint
95 * \return the ID of updated/added constraint
97 Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
98 /** \brief Removes the constraint by its ID. All entities and parameters depending on this
99 * constraint, which are not used in other constraints, will be removed too.
100 * \param[in] theConstraintID index of constraint to be removed
101 * \return \c true if the constraint was successfully removed
103 bool removeConstraint(const Slvs_hConstraint& theConstraintID);
104 /// \brief Returns the constraint by its ID
105 const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
106 /// \brief Returns list of constraints of specified type
107 std::list<Slvs_Constraint> getConstraintsByType(int theConstraintType) const;
109 /// \brief Attach constraint SLVS_C_WHERE_DRAGGED to this storage. It need to make precise calculations
110 void addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID);
112 /// \brief Add transient constraint
113 void addTemporaryConstraint(const Slvs_hConstraint& theConstraintID);
114 /// \brief Remove all transient constraints
115 void removeTemporaryConstraints();
116 /// \brief Remove one temporary constraint. Preferable to remove the points under Point-on-Line constraint
117 /// \return Number of remaining temporary constraints
118 int deleteTemporaryConstraint();
119 /// \brief Checks the constraint is temporary
120 bool isTemporary(const Slvs_hConstraint& theConstraintID) const;
121 /// \brief Number of temporary constraints
122 int numberTemporary() const
123 { return (int)myTemporaryConstraints.size(); }
125 /// \brief Shows the sketch should be resolved
126 bool isNeedToResolve();
128 /// \brief Shows the storage has the same constraint twice
129 bool hasDuplicatedConstraint() const
130 { return myDuplicatedConstraint; }
132 /// \brief Changes the flag of group to be resolved
133 void setNeedToResolve(bool theFlag)
134 { myNeedToResolve = theFlag; }
136 /// \brief Returns lists of removed elements
137 void getRemoved(std::set<Slvs_hParam>& theParameters,
138 std::set<Slvs_hEntity>& theEntities,
139 std::set<Slvs_hConstraint>& theConstraints);
141 /// \brief Initialize constraint solver by the entities collected by current storage
142 void initializeSolver(SketchSolver_Solver& theSolver);
145 /// \brief Store coincident points
146 void addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
147 /// \brief Remove point from lists of coincidence
148 void removeCoincidentPoint(const Slvs_hEntity& thePoint);
149 /// \brief Remove point-point coincidence
150 void removeCoincidence(const Slvs_Constraint& theCoincidence);
153 /// \brief Check two points are coincident
154 bool isCoincident(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
156 /// \brief Check two points are coincident or have same coordinates
157 bool isEqual(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const;
159 /// \brief Check the entity is horizontal of vertical
160 bool isAxisParallel(const Slvs_hEntity& theEntity) const;
162 /// \brief Verifies the entity is used in any equal constraint
163 /// \param[in] theEntity entity to be found
164 /// \param[out] theEqual constraint, which uses the entity
165 /// \return \c true, if the Equal constrait is found
166 bool isUsedInEqual(const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const;
168 /// \brief Fixes specified entity
169 /// \param theEntity ID of the entity to be fixed
170 /// \return List of created constraints
171 std::vector<Slvs_hConstraint> fixEntity(const Slvs_hEntity& theEntity);
174 /// \brief Fixes specified point
175 /// \param [in] thePoint point to be fixed
176 /// \param [out] theCreated list of the Fixed constraints created
177 void fixPoint(const Slvs_Entity& thePoint, std::vector<Slvs_hConstraint>& theCreated);
178 /// \brief Fixes specified line
179 /// \param [in] theLine line to be fixed
180 /// \param [out] theCreated list of the Fixed constraints created
181 void fixLine(const Slvs_Entity& theLine, std::vector<Slvs_hConstraint>& theCreated);
182 /// \brief Fixes specified circle
183 /// \param [in] theCircle circle to be fixed
184 /// \param [out] theCreated list of the Fixed constraints created
185 void fixCircle(const Slvs_Entity& theCircle, std::vector<Slvs_hConstraint>& theCreated);
186 /// \brief Fixes specified arc
187 /// \param [in] theArc arc to be fixed
188 /// \param [out] theCreated list of the Fixed constraints created
189 void fixArc(const Slvs_Entity& theArc, std::vector<Slvs_hConstraint>& theCreated);
192 Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
193 std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
194 Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
195 std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
196 Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
197 std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
199 std::vector< std::set<Slvs_hEntity> > myCoincidentPoints; ///< lists of coincident points
200 Slvs_hConstraint myFixed; ///< identifier of one of temporary constraints to fix separate point
202 bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
203 bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
205 std::set<Slvs_hConstraint> myTemporaryConstraints; ///< list of transient constraints
206 std::set<Slvs_hParam> myRemovedParameters; ///< list of just removed parameters (cleared when returning to applicant)
207 std::set<Slvs_hEntity> myRemovedEntities; ///< list of just removed entities (cleared when returning to applicant)
208 std::set<Slvs_hConstraint> myRemovedConstraints; ///< list of just removed constraints (cleared when returning to applicant)
209 std::set<Slvs_hParam> myUpdatedParameters; ///< list of just updated parameters (cleared when isNeedToResolve() called)
212 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;