Salome HOME
19ffaca0924f96d0e8d614f60994cdfe8e0f3c29
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_Solver.h
1 // Copyright (C) 2014-2021  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 email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef PlaneGCSSolver_Solver_H_
21 #define PlaneGCSSolver_Solver_H_
22
23 #include <PlaneGCSSolver_Defs.h>
24 #include <PlaneGCSSolver_ConstraintWrapper.h>
25
26 #include <GCS.h>
27
28 /// \brief The main class that performs the high-level operations for connection to the PlaneGCS.
29 class PlaneGCSSolver_Solver
30 {
31 public:
32   /// The result of constraints solution
33   enum SolveStatus {
34     STATUS_OK,
35     STATUS_INCONSISTENT,
36     STATUS_EMPTYSET,
37     STATUS_DEGENERATED,
38     STATUS_FAILED, // set if no one other status is applicable
39     STATUS_UNKNOWN // set for newly created groups
40   };
41
42   PlaneGCSSolver_Solver();
43   ~PlaneGCSSolver_Solver();
44
45   /// \brief Clear system of equations
46   void clear();
47
48   /// \brief Add constraint to the system of equations
49   /// \param[in] theMultiConstraintID  ID of the multi constraint which may consists of
50   ///                                  several primitive constraints
51   /// \param[in] theConstraints        list of primitive constraints
52   void addConstraint(const ConstraintID& theMultiConstraintID,
53                      const std::list<GCSConstraintPtr>& theConstraints);
54
55   /// \brief Remove constraints from the system of equations
56   void removeConstraint(const ConstraintID& theID);
57
58   /// \brief Initialize memory for new solver's parameter
59   double* createParameter();
60   /// \brief Add parameters created elsewhere
61   void addParameters(const GCS::SET_pD& theParams);
62   /// \brief Release memory occupied by parameters
63   void removeParameters(const GCS::SET_pD& theParams);
64
65   /// \brief Preliminary initialization of solver (useful for moving a feature).
66   ///        When called, the solve() method does not reinitialize a set of constraints.
67   void initialize();
68
69   /// \brief Solve the set of equations
70   /// \return identifier whether solution succeeded
71   SolveStatus solve();
72
73   /// \brief Revert solution to initial values
74   void undo();
75
76   /// \brief Check the constraint is conflicted with others
77   bool isConflicting(const ConstraintID& theConstraint) const;
78
79   /// \brief Check conflicting/redundant constraints and DoF
80   void diagnose(const GCS::Algorithm& theAlgo = GCS::DogLeg);
81
82   /// \brief Return the list of modifiable parameters
83   void getFreeParameters(GCS::SET_pD& theFreeParams);
84
85   /// \brief Degrees of freedom
86   int dof();
87
88 private:
89   void collectConflicting(bool withRedundant = true);
90
91   /// \brief Add fictive constraint if the sketch contains temporary constraints only
92   void addFictiveConstraintIfNecessary();
93   /// \brief Remove previously added fictive constraint
94   void removeFictiveConstraint();
95
96 private:
97   typedef std::map<ConstraintID, std::list<GCSConstraintPtr> > ConstraintMap;
98
99   GCS::VEC_pD                  myParameters;     ///< list of unknowns
100   ConstraintMap                myConstraints;    ///< list of constraints
101
102   std::shared_ptr<GCS::System> myEquationSystem; ///< set of equations for solving in FreeGCS
103   bool                         myDiagnoseBeforeSolve; ///< is the diagnostic necessary
104   bool                         myInitilized;     ///< is the system already initialized
105
106   GCS::SET_I                   myConflictingIDs; ///< list of IDs of conflicting constraints
107   /// specifies the conflicting constraints are already collected
108   bool                         myConfCollected;
109
110   int                          myDOF;            ///< degrees of freedom
111
112   GCS::Constraint*             myFictiveConstraint;
113 };
114
115 typedef std::shared_ptr<PlaneGCSSolver_Solver> SolverPtr;
116
117 #endif