1 // File: SketchSolver_Solver.cpp
2 // Created: 07 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchSolver_Solver.h"
7 SketchSolver_Solver::SketchSolver_Solver()
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;
19 myEquationsSystem.dragged[0] = 0;
20 myEquationsSystem.dragged[1] = 0;
21 myEquationsSystem.dragged[2] = 0;
22 myEquationsSystem.dragged[3] = 0;
24 // If the set of constraints is inconsistent,
25 // the failed field will contain wrong constraints
26 myEquationsSystem.calculateFaileds = 1;
29 SketchSolver_Solver::~SketchSolver_Solver()
31 if (myEquationsSystem.param)
32 delete [] myEquationsSystem.param;
33 if (myEquationsSystem.entity)
34 delete [] myEquationsSystem.entity;
35 if (myEquationsSystem.constraint)
36 delete [] myEquationsSystem.constraint;
37 if (myEquationsSystem.failed)
38 delete [] myEquationsSystem.failed;
41 void SketchSolver_Solver::setParameters(const std::vector<Slvs_Param>& theParameters)
43 if (theParameters.size() != myEquationsSystem.params) // number of parameters was changed => reallocate the memory
45 if (myEquationsSystem.param)
46 delete [] myEquationsSystem.param;
47 myEquationsSystem.params = theParameters.size();
48 myEquationsSystem.param = new Slvs_Param[theParameters.size()];
52 std::vector<Slvs_Param>::const_iterator aParamIter = theParameters.begin();
53 for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
54 myEquationsSystem.param[i] = *aParamIter;
57 void SketchSolver_Solver::setDraggedParameters(const std::vector<Slvs_hParam>& theDragged)
59 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 = 0;
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 Slvs_Solve(&myEquationsSystem, myGroupID);
116 return myEquationsSystem.result;
119 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
121 if (myEquationsSystem.result != SLVS_RESULT_OKAY)
124 if (theParameters.size() != myEquationsSystem.params)
125 return false; // number of parameters is not the same
127 std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
128 for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
130 if (myEquationsSystem.param[i].h != aParamIter->h)
131 return false; // sequence of parameters was changed
132 aParamIter->val = myEquationsSystem.param[i].val;