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