Salome HOME
Revert "First phase of SketchSolver 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   myGroupID = 0;
13   // Nullify all elements of the set of equations
14   myEquationsSystem.param = 0;
15   myEquationsSystem.params = 0;
16   myEquationsSystem.entity = 0;
17   myEquationsSystem.entities = 0;
18   myEquationsSystem.constraint = 0;
19   myEquationsSystem.constraints = 0;
20   myEquationsSystem.failed = 0;
21   myEquationsSystem.faileds = 0;
22
23   myEquationsSystem.dragged[0] = 0;
24   myEquationsSystem.dragged[1] = 0;
25   myEquationsSystem.dragged[2] = 0;
26   myEquationsSystem.dragged[3] = 0;
27
28   // If the set of constraints is inconsistent,
29   // the failed field will contain wrong constraints
30   myEquationsSystem.calculateFaileds = 0;
31 }
32
33 SketchSolver_Solver::~SketchSolver_Solver()
34 {
35   if (myEquationsSystem.constraint)
36     delete[] myEquationsSystem.constraint;
37   myEquationsSystem.constraint = 0;
38   if (myEquationsSystem.failed)
39     delete[] myEquationsSystem.failed;
40   myEquationsSystem.failed = 0;
41 }
42
43 void SketchSolver_Solver::setParameters(Slvs_Param* theParameters, int theSize)
44 {
45   myEquationsSystem.param = theParameters;
46   myEquationsSystem.params = theSize;
47 }
48
49
50 void SketchSolver_Solver::setDraggedParameters(const Slvs_hParam* theDragged)
51 {
52   for (unsigned int i = 0; i < 4; i++)
53     myEquationsSystem.dragged[i] = theDragged[i];
54 }
55
56 void SketchSolver_Solver::setEntities(Slvs_Entity* theEntities, int theSize)
57 {
58   myEquationsSystem.entity = theEntities;
59   myEquationsSystem.entities = theSize;
60 }
61
62 void SketchSolver_Solver::setConstraints(Slvs_Constraint* theConstraints, int theSize)
63 {
64   if (!myEquationsSystem.constraint) {
65     myEquationsSystem.constraint = new Slvs_Constraint[theSize];
66     myEquationsSystem.constraints = theSize;
67     myEquationsSystem.failed = new Slvs_hConstraint[theSize];
68   }
69   else if (myEquationsSystem.constraints != theSize) {
70     if (theSize > myEquationsSystem.constraints) {
71       delete[] myEquationsSystem.constraint;
72       myEquationsSystem.constraint = new Slvs_Constraint[theSize];
73       if (myEquationsSystem.failed)
74         delete[] myEquationsSystem.failed;
75       myEquationsSystem.failed = new Slvs_hConstraint[theSize];
76     }
77     myEquationsSystem.constraints = theSize;
78   }
79   memcpy(myEquationsSystem.constraint, theConstraints, theSize * sizeof(Slvs_Constraint));
80   memset(myEquationsSystem.failed, SLVS_C_UNKNOWN, theSize * sizeof(Slvs_hConstraint));
81 }
82
83
84 int SketchSolver_Solver::solve()
85 {
86   if (myEquationsSystem.constraints <= 0)
87     return SLVS_RESULT_EMPTY_SET;
88
89   Events_LongOp::start(this);
90   Slvs_Solve(&myEquationsSystem, myGroupID);
91   Events_LongOp::end(this);
92
93   return myEquationsSystem.result;
94 }
95
96 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
97 {
98   if (myEquationsSystem.result != SLVS_RESULT_OKAY)
99     return false;
100
101   if (theParameters.size() != myEquationsSystem.params)
102     return false;  // number of parameters is not the same
103
104   std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
105   for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++) {
106     if (myEquationsSystem.param[i].h != aParamIter->h)
107       return false;  // sequence of parameters was changed
108     aParamIter->val = myEquationsSystem.param[i].val;
109   }
110
111   return true;
112 }