1 // File: SketchSolver_Solver.cpp
2 // Created: 07 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchSolver_Solver.h"
6 #include <Events_LongOp.h>
8 SketchSolver_Solver::SketchSolver_Solver()
10 // Nullify all elements of the set of equations
11 myEquationsSystem.param = 0;
12 myEquationsSystem.params = 0;
13 myEquationsSystem.entity = 0;
14 myEquationsSystem.entities = 0;
15 myEquationsSystem.constraint = 0;
16 myEquationsSystem.constraints = 0;
17 myEquationsSystem.failed = 0;
18 myEquationsSystem.faileds = 0;
20 myEquationsSystem.dragged[0] = 0;
21 myEquationsSystem.dragged[1] = 0;
22 myEquationsSystem.dragged[2] = 0;
23 myEquationsSystem.dragged[3] = 0;
25 // If the set of constraints is inconsistent,
26 // the failed field will contain wrong constraints
27 myEquationsSystem.calculateFaileds = 1;
30 SketchSolver_Solver::~SketchSolver_Solver()
32 if (myEquationsSystem.param)
33 delete[] myEquationsSystem.param;
34 if (myEquationsSystem.entity)
35 delete[] myEquationsSystem.entity;
36 if (myEquationsSystem.constraint)
37 delete[] myEquationsSystem.constraint;
38 if (myEquationsSystem.failed)
39 delete[] myEquationsSystem.failed;
42 void SketchSolver_Solver::setParameters(const std::vector<Slvs_Param>& theParameters)
44 if (theParameters.size() != myEquationsSystem.params) // number of parameters was changed => reallocate the memory
46 if (myEquationsSystem.param)
47 delete[] myEquationsSystem.param;
48 myEquationsSystem.params = theParameters.size();
49 myEquationsSystem.param = new Slvs_Param[theParameters.size()];
53 std::vector<Slvs_Param>::const_iterator aParamIter = theParameters.begin();
54 for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
55 myEquationsSystem.param[i] = *aParamIter;
58 void SketchSolver_Solver::setDraggedParameters(const std::vector<Slvs_hParam>& theDragged)
60 if (theDragged.size() == 0) {
61 myEquationsSystem.dragged[0] = 0;
62 myEquationsSystem.dragged[1] = 0;
63 myEquationsSystem.dragged[2] = 0;
64 myEquationsSystem.dragged[3] = 0;
67 for (unsigned int i = 0; i < theDragged.size(); i++)
68 myEquationsSystem.dragged[i] = theDragged[i];
71 void SketchSolver_Solver::setEntities(const std::vector<Slvs_Entity>& theEntities)
73 if (theEntities.size() != myEquationsSystem.entities) // number of entities was changed => reallocate the memory
75 if (myEquationsSystem.entity)
76 delete[] myEquationsSystem.entity;
77 myEquationsSystem.entities = theEntities.size();
78 myEquationsSystem.entity = new Slvs_Entity[theEntities.size()];
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;
87 void SketchSolver_Solver::setConstraints(const std::vector<Slvs_Constraint>& theConstraints)
89 if (theConstraints.size() != myEquationsSystem.constraints) // number of constraints was changed => reallocate the memory
91 if (myEquationsSystem.constraint)
92 delete[] myEquationsSystem.constraint;
93 myEquationsSystem.constraints = theConstraints.size();
94 myEquationsSystem.constraint = new Slvs_Constraint[theConstraints.size()];
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();
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;
109 int SketchSolver_Solver::solve()
111 if (myEquationsSystem.constraints <= 0)
112 return SLVS_RESULT_EMPTY_SET;
114 Events_LongOp::start(this);
115 Slvs_Solve(&myEquationsSystem, myGroupID);
116 Events_LongOp::end(this);
118 return myEquationsSystem.result;
121 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
123 if (myEquationsSystem.result != SLVS_RESULT_OKAY)
126 if (theParameters.size() != myEquationsSystem.params)
127 return false; // number of parameters is not the same
129 std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
130 for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++) {
131 if (myEquationsSystem.param[i].h != aParamIter->h)
132 return false; // sequence of parameters was changed
133 aParamIter->val = myEquationsSystem.param[i].val;