1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Solver.cpp
4 // Created: 07 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchSolver_Solver.h"
8 #include <Events_LongOp.h>
10 SketchSolver_Solver::SketchSolver_Solver()
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;
23 myEquationsSystem.dragged[0] = 0;
24 myEquationsSystem.dragged[1] = 0;
25 myEquationsSystem.dragged[2] = 0;
26 myEquationsSystem.dragged[3] = 0;
28 // If the set of constraints is inconsistent,
29 // the failed field will contain wrong constraints
30 myEquationsSystem.calculateFaileds = 0;
33 SketchSolver_Solver::~SketchSolver_Solver()
35 if (myEquationsSystem.constraint)
36 delete[] myEquationsSystem.constraint;
37 myEquationsSystem.constraint = 0;
38 if (myEquationsSystem.failed)
39 delete[] myEquationsSystem.failed;
40 myEquationsSystem.failed = 0;
43 void SketchSolver_Solver::setParameters(Slvs_Param* theParameters, int theSize)
45 myEquationsSystem.param = theParameters;
46 myEquationsSystem.params = theSize;
50 void SketchSolver_Solver::setDraggedParameters(const Slvs_hParam* theDragged)
52 for (unsigned int i = 0; i < 4; i++)
53 myEquationsSystem.dragged[i] = theDragged[i];
56 void SketchSolver_Solver::setEntities(Slvs_Entity* theEntities, int theSize)
58 myEquationsSystem.entity = theEntities;
59 myEquationsSystem.entities = theSize;
62 void SketchSolver_Solver::setConstraints(Slvs_Constraint* theConstraints, int theSize)
64 if (!myEquationsSystem.constraint) {
65 myEquationsSystem.constraint = new Slvs_Constraint[theSize];
66 myEquationsSystem.constraints = theSize;
67 myEquationsSystem.failed = new Slvs_hConstraint[theSize];
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];
77 myEquationsSystem.constraints = theSize;
79 memcpy(myEquationsSystem.constraint, theConstraints, theSize * sizeof(Slvs_Constraint));
80 memset(myEquationsSystem.failed, SLVS_C_UNKNOWN, theSize * sizeof(Slvs_hConstraint));
84 int SketchSolver_Solver::solve()
86 if (myEquationsSystem.constraints <= 0)
87 return SLVS_RESULT_EMPTY_SET;
89 Events_LongOp::start(this);
90 Slvs_Solve(&myEquationsSystem, myGroupID);
91 Events_LongOp::end(this);
93 return myEquationsSystem.result;
96 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
98 if (myEquationsSystem.result != SLVS_RESULT_OKAY)
101 if (theParameters.size() != myEquationsSystem.params)
102 return false; // number of parameters is not the same
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;