Salome HOME
Adaptation to new data structure
[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
7 SketchSolver_Solver::SketchSolver_Solver()
8 {
9   // Nullify all elements of the set of equations
10   myEquationsSystem.param = 0;
11   myEquationsSystem.params = 0;
12   myEquationsSystem.entity = 0;
13   myEquationsSystem.entities = 0;
14   myEquationsSystem.constraint = 0;
15   myEquationsSystem.constraints = 0;
16   myEquationsSystem.failed = 0;
17   myEquationsSystem.faileds = 0;
18
19   myEquationsSystem.dragged[0] = 0;
20   myEquationsSystem.dragged[1] = 0;
21   myEquationsSystem.dragged[2] = 0;
22   myEquationsSystem.dragged[3] = 0;
23
24   // If the set of constraints is inconsistent,
25   // the failed field will contain wrong constraints
26   myEquationsSystem.calculateFaileds = 1;
27 }
28
29 SketchSolver_Solver::~SketchSolver_Solver()
30 {
31   if (myEquationsSystem.param)
32     delete [] myEquationsSystem.param;
33   if (myEquationsSystem.entity)
34     delete [] myEquationsSystem.entity;
35   if (myEquationsSystem.constraint)
36     delete [] myEquationsSystem.constraint;
37   if (myEquationsSystem.failed)
38     delete [] myEquationsSystem.failed;
39 }
40
41 void SketchSolver_Solver::setParameters(const std::vector<Slvs_Param>& theParameters)
42 {
43   if (theParameters.size() != myEquationsSystem.params) // number of parameters was changed => reallocate the memory
44   {
45     if (myEquationsSystem.param)
46       delete [] myEquationsSystem.param;
47     myEquationsSystem.params = theParameters.size();
48     myEquationsSystem.param = new Slvs_Param[theParameters.size()];
49   }
50
51   // Copy data
52   std::vector<Slvs_Param>::const_iterator aParamIter = theParameters.begin();
53   for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
54     myEquationsSystem.param[i] = *aParamIter;
55 }
56
57 void SketchSolver_Solver::setDraggedParameters(const std::vector<Slvs_hParam>& theDragged)
58 {
59   if (theDragged.size() == 0)
60   {
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   Slvs_Solve(&myEquationsSystem, myGroupID);
115
116   return myEquationsSystem.result;
117 }
118
119 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
120 {
121   if (myEquationsSystem.result != SLVS_RESULT_OKAY)
122     return false;
123
124   if (theParameters.size() != myEquationsSystem.params)
125     return false; // number of parameters is not the same
126
127   std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
128   for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
129   {
130     if (myEquationsSystem.param[i].h != aParamIter->h)
131       return false; // sequence of parameters was changed
132     aParamIter->val = myEquationsSystem.param[i].val;
133   }
134
135   return true;
136 }