]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_Solver.cpp
Salome HOME
SketchSolver library refactoring
[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.failed)
35     delete[] myEquationsSystem.failed;
36 }
37
38 void SketchSolver_Solver::setParameters(Slvs_Param* theParameters, int theSize)
39 {
40   myEquationsSystem.param = theParameters;
41   myEquationsSystem.params = theSize;
42 }
43
44
45 void SketchSolver_Solver::setDraggedParameters(const Slvs_hParam* theDragged)
46 {
47   for (unsigned int i = 0; i < 4; i++)
48     myEquationsSystem.dragged[i] = theDragged[i];
49 }
50
51 void SketchSolver_Solver::setEntities(Slvs_Entity* theEntities, int theSize)
52 {
53   myEquationsSystem.entity = theEntities;
54   myEquationsSystem.entities = theSize;
55 }
56
57 void SketchSolver_Solver::setConstraints(Slvs_Constraint* theConstraints, int theSize)
58 {
59   myEquationsSystem.constraint = theConstraints;
60   myEquationsSystem.constraints = theSize;
61 }
62
63
64 int SketchSolver_Solver::solve()
65 {
66   if (myEquationsSystem.constraints <= 0)
67     return SLVS_RESULT_EMPTY_SET;
68
69   Events_LongOp::start(this);
70   Slvs_Solve(&myEquationsSystem, myGroupID);
71   Events_LongOp::end(this);
72
73   return myEquationsSystem.result;
74 }
75
76 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
77 {
78   if (myEquationsSystem.result != SLVS_RESULT_OKAY)
79     return false;
80
81   if (theParameters.size() != myEquationsSystem.params)
82     return false;  // number of parameters is not the same
83
84   std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
85   for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++) {
86     if (myEquationsSystem.param[i].h != aParamIter->h)
87       return false;  // sequence of parameters was changed
88     aParamIter->val = myEquationsSystem.param[i].val;
89   }
90
91   return true;
92 }