Salome HOME
Wait cursor for solver operation
[modules/shaper.git] / src / SketchSolver / SketchSolver_Solver.cpp
1 // File:    SketchSolver_Solver.cpp
2 // Created: 07 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchSolver_Solver.h"
6 #include <Events_LongOp.h>
7
8 SketchSolver_Solver::SketchSolver_Solver()
9 {
10   // Nullify all elements of the set of equations
11   myEquationsSystem.param = 0;
12   myEquationsSystem.params = 0;
13   myEquationsSystem.entity = 0;
14   myEquationsSystem.entities = 0;
15   myEquationsSystem.constraint = 0;
16   myEquationsSystem.constraints = 0;
17   myEquationsSystem.failed = 0;
18   myEquationsSystem.faileds = 0;
19
20   myEquationsSystem.dragged[0] = 0;
21   myEquationsSystem.dragged[1] = 0;
22   myEquationsSystem.dragged[2] = 0;
23   myEquationsSystem.dragged[3] = 0;
24
25   // If the set of constraints is inconsistent,
26   // the failed field will contain wrong constraints
27   myEquationsSystem.calculateFaileds = 1;
28 }
29
30 SketchSolver_Solver::~SketchSolver_Solver()
31 {
32   if (myEquationsSystem.param)
33     delete [] myEquationsSystem.param;
34   if (myEquationsSystem.entity)
35     delete [] myEquationsSystem.entity;
36   if (myEquationsSystem.constraint)
37     delete [] myEquationsSystem.constraint;
38   if (myEquationsSystem.failed)
39     delete [] myEquationsSystem.failed;
40 }
41
42 void SketchSolver_Solver::setParameters(const std::vector<Slvs_Param>& theParameters)
43 {
44   if (theParameters.size() != myEquationsSystem.params) // number of parameters was changed => reallocate the memory
45   {
46     if (myEquationsSystem.param)
47       delete [] myEquationsSystem.param;
48     myEquationsSystem.params = theParameters.size();
49     myEquationsSystem.param = new Slvs_Param[theParameters.size()];
50   }
51
52   // Copy data
53   std::vector<Slvs_Param>::const_iterator aParamIter = theParameters.begin();
54   for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
55     myEquationsSystem.param[i] = *aParamIter;
56 }
57
58 void SketchSolver_Solver::setDraggedParameters(const std::vector<Slvs_hParam>& theDragged)
59 {
60   if (theDragged.size() == 0)
61   {
62     myEquationsSystem.dragged[0] = 0;
63     myEquationsSystem.dragged[1] = 0;
64     myEquationsSystem.dragged[2] = 0;
65     myEquationsSystem.dragged[3] = 0;
66     return;
67   }
68   for (unsigned int i = 0; i < theDragged.size(); i++)
69     myEquationsSystem.dragged[i] = theDragged[i];
70 }
71
72 void SketchSolver_Solver::setEntities(const std::vector<Slvs_Entity>& theEntities)
73 {
74   if (theEntities.size() != myEquationsSystem.entities) // number of entities was changed => reallocate the memory
75   {
76     if (myEquationsSystem.entity)
77       delete [] myEquationsSystem.entity;
78     myEquationsSystem.entities = theEntities.size();
79     myEquationsSystem.entity = new Slvs_Entity[theEntities.size()];
80   }
81
82   // Copy data
83   std::vector<Slvs_Entity>::const_iterator aEntIter = theEntities.begin();
84   for (int i = 0; i < myEquationsSystem.entities; i++, aEntIter++)
85     myEquationsSystem.entity[i] = *aEntIter;
86 }
87
88 void SketchSolver_Solver::setConstraints(const std::vector<Slvs_Constraint>& theConstraints)
89 {
90   if (theConstraints.size() != myEquationsSystem.constraints) // number of constraints was changed => reallocate the memory
91   {
92     if (myEquationsSystem.constraint)
93       delete [] myEquationsSystem.constraint;
94     myEquationsSystem.constraints = theConstraints.size();
95     myEquationsSystem.constraint = new Slvs_Constraint[theConstraints.size()];
96
97     // Assign the memory for the failed constraints
98     if (myEquationsSystem.failed)
99       delete [] myEquationsSystem.failed;
100     myEquationsSystem.failed = new Slvs_hConstraint[theConstraints.size()];
101     myEquationsSystem.faileds = theConstraints.size();
102   }
103
104   // Copy data
105   std::vector<Slvs_Constraint>::const_iterator aConstrIter = theConstraints.begin();
106   for (int i = 0; i < myEquationsSystem.constraints; i++, aConstrIter++)
107     myEquationsSystem.constraint[i] = *aConstrIter;
108 }
109
110 int SketchSolver_Solver::solve()
111 {
112   if (myEquationsSystem.constraints <= 0)
113     return SLVS_RESULT_EMPTY_SET;
114
115   Events_LongOp::start(this);
116   Slvs_Solve(&myEquationsSystem, myGroupID);
117   Events_LongOp::end(this);
118
119   return myEquationsSystem.result;
120 }
121
122 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
123 {
124   if (myEquationsSystem.result != SLVS_RESULT_OKAY)
125     return false;
126
127   if (theParameters.size() != myEquationsSystem.params)
128     return false; // number of parameters is not the same
129
130   std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
131   for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
132   {
133     if (myEquationsSystem.param[i].h != aParamIter->h)
134       return false; // sequence of parameters was changed
135     aParamIter->val = myEquationsSystem.param[i].val;
136   }
137
138   return true;
139 }