Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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   // If the set of constraints is inconsistent,
20   // the failed field will contain wrong constraints
21   myEquationsSystem.calculateFaileds = 1;
22 }
23
24 SketchSolver_Solver::~SketchSolver_Solver()
25 {
26   if (myEquationsSystem.param)
27     delete [] myEquationsSystem.param;
28   if (myEquationsSystem.entity)
29     delete [] myEquationsSystem.entity;
30   if (myEquationsSystem.constraint)
31     delete [] myEquationsSystem.constraint;
32   if (myEquationsSystem.failed)
33     delete [] myEquationsSystem.failed;
34 }
35
36 void SketchSolver_Solver::setParameters(const std::vector<Slvs_Param>& theParameters)
37 {
38   if (theParameters.size() != myEquationsSystem.params) // number of parameters was changed => reallocate the memory
39   {
40     if (myEquationsSystem.param)
41       delete [] myEquationsSystem.param;
42     myEquationsSystem.params = theParameters.size();
43     myEquationsSystem.param = new Slvs_Param[theParameters.size()];
44   }
45
46   // Copy data
47   std::vector<Slvs_Param>::const_iterator aParamIter = theParameters.begin();
48   for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
49     myEquationsSystem.param[i] = *aParamIter;
50 }
51
52 void SketchSolver_Solver::setEntities(const std::vector<Slvs_Entity>& theEntities)
53 {
54   if (theEntities.size() != myEquationsSystem.entities) // number of entities was changed => reallocate the memory
55   {
56     if (myEquationsSystem.entity)
57       delete [] myEquationsSystem.entity;
58     myEquationsSystem.entities = theEntities.size();
59     myEquationsSystem.entity = new Slvs_Entity[theEntities.size()];
60   }
61
62   // Copy data
63   std::vector<Slvs_Entity>::const_iterator aEntIter = theEntities.begin();
64   for (int i = 0; i < myEquationsSystem.entities; i++, aEntIter++)
65     myEquationsSystem.entity[i] = *aEntIter;
66 }
67
68 void SketchSolver_Solver::setConstraints(const std::vector<Slvs_Constraint>& theConstraints)
69 {
70   if (theConstraints.size() != myEquationsSystem.constraints) // number of constraints was changed => reallocate the memory
71   {
72     if (myEquationsSystem.constraint)
73       delete [] myEquationsSystem.constraint;
74     myEquationsSystem.constraints = theConstraints.size();
75     myEquationsSystem.constraint = new Slvs_Constraint[theConstraints.size()];
76   }
77
78   // Copy data
79   std::vector<Slvs_Constraint>::const_iterator aConstrIter = theConstraints.begin();
80   for (int i = 0; i < myEquationsSystem.constraints; i++, aConstrIter++)
81     myEquationsSystem.constraint[i] = *aConstrIter;
82 }
83
84 int SketchSolver_Solver::solve()
85 {
86   if (myEquationsSystem.constraints <= 0)
87     return SLVS_RESULT_EMPTY_SET;
88
89   Slvs_Solve(&myEquationsSystem, myGroupID);
90
91   return myEquationsSystem.result;
92 }
93
94 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
95 {
96   if (myEquationsSystem.result != SLVS_RESULT_OKAY)
97     return false;
98
99   if (theParameters.size() != myEquationsSystem.params)
100     return false; // number of parameters is not the same
101
102   std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
103   for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
104   {
105     if (myEquationsSystem.param[i].h != aParamIter->h)
106       return false; // sequence of parameters was changed
107     aParamIter->val = myEquationsSystem.param[i].val;
108   }
109
110   return true;
111 }