Salome HOME
Sources formated according to the codeing standards
[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     myEquationsSystem.dragged[0] = 0;
62     myEquationsSystem.dragged[1] = 0;
63     myEquationsSystem.dragged[2] = 0;
64     myEquationsSystem.dragged[3] = 0;
65     return;
66   }
67   for (unsigned int i = 0; i < theDragged.size(); i++)
68     myEquationsSystem.dragged[i] = theDragged[i];
69 }
70
71 void SketchSolver_Solver::setEntities(const std::vector<Slvs_Entity>& theEntities)
72 {
73   if (theEntities.size() != myEquationsSystem.entities)  // number of entities was changed => reallocate the memory
74       {
75     if (myEquationsSystem.entity)
76       delete[] myEquationsSystem.entity;
77     myEquationsSystem.entities = theEntities.size();
78     myEquationsSystem.entity = new Slvs_Entity[theEntities.size()];
79   }
80
81   // Copy data
82   std::vector<Slvs_Entity>::const_iterator aEntIter = theEntities.begin();
83   for (int i = 0; i < myEquationsSystem.entities; i++, aEntIter++)
84     myEquationsSystem.entity[i] = *aEntIter;
85 }
86
87 void SketchSolver_Solver::setConstraints(const std::vector<Slvs_Constraint>& theConstraints)
88 {
89   if (theConstraints.size() != myEquationsSystem.constraints)  // number of constraints was changed => reallocate the memory
90       {
91     if (myEquationsSystem.constraint)
92       delete[] myEquationsSystem.constraint;
93     myEquationsSystem.constraints = theConstraints.size();
94     myEquationsSystem.constraint = new Slvs_Constraint[theConstraints.size()];
95
96     // Assign the memory for the failed constraints
97     if (myEquationsSystem.failed)
98       delete[] myEquationsSystem.failed;
99     myEquationsSystem.failed = new Slvs_hConstraint[theConstraints.size()];
100     myEquationsSystem.faileds = theConstraints.size();
101   }
102
103   // Copy data
104   std::vector<Slvs_Constraint>::const_iterator aConstrIter = theConstraints.begin();
105   for (int i = 0; i < myEquationsSystem.constraints; i++, aConstrIter++)
106     myEquationsSystem.constraint[i] = *aConstrIter;
107 }
108
109 int SketchSolver_Solver::solve()
110 {
111   if (myEquationsSystem.constraints <= 0)
112     return SLVS_RESULT_EMPTY_SET;
113
114   Events_LongOp::start(this);
115   Slvs_Solve(&myEquationsSystem, myGroupID);
116   Events_LongOp::end(this);
117
118   return myEquationsSystem.result;
119 }
120
121 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
122 {
123   if (myEquationsSystem.result != SLVS_RESULT_OKAY)
124     return false;
125
126   if (theParameters.size() != myEquationsSystem.params)
127     return false;  // number of parameters is not the same
128
129   std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
130   for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++) {
131     if (myEquationsSystem.param[i].h != aParamIter->h)
132       return false;  // sequence of parameters was changed
133     aParamIter->val = myEquationsSystem.param[i].val;
134   }
135
136   return true;
137 }