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 // If the set of constraints is inconsistent,
20 // the failed field will contain wrong constraints
21 myEquationsSystem.calculateFaileds = 1;
24 SketchSolver_Solver::~SketchSolver_Solver()
26 if (myEquationsSystem.param)
27 delete [] myEquationsSystem.param;
28 if (myEquationsSystem.entity)
29 delete [] myEquationsSystem.entity;
30 if (myEquationsSystem.constraint)
31 delete [] myEquationsSystem.constraint;
32 if (myEquationsSystem.failed)
33 delete [] myEquationsSystem.failed;
36 void SketchSolver_Solver::setParameters(const std::vector<Slvs_Param>& theParameters)
38 if (theParameters.size() != myEquationsSystem.params) // number of parameters was changed => reallocate the memory
40 if (myEquationsSystem.param)
41 delete [] myEquationsSystem.param;
42 myEquationsSystem.params = theParameters.size();
43 myEquationsSystem.param = new Slvs_Param[theParameters.size()];
47 std::vector<Slvs_Param>::const_iterator aParamIter = theParameters.begin();
48 for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
49 myEquationsSystem.param[i] = *aParamIter;
52 void SketchSolver_Solver::setEntities(const std::vector<Slvs_Entity>& theEntities)
54 if (theEntities.size() != myEquationsSystem.entities) // number of entities was changed => reallocate the memory
56 if (myEquationsSystem.entity)
57 delete [] myEquationsSystem.entity;
58 myEquationsSystem.entities = theEntities.size();
59 myEquationsSystem.entity = new Slvs_Entity[theEntities.size()];
63 std::vector<Slvs_Entity>::const_iterator aEntIter = theEntities.begin();
64 for (int i = 0; i < myEquationsSystem.entities; i++, aEntIter++)
65 myEquationsSystem.entity[i] = *aEntIter;
68 void SketchSolver_Solver::setConstraints(const std::vector<Slvs_Constraint>& theConstraints)
70 if (theConstraints.size() != myEquationsSystem.constraints) // number of constraints was changed => reallocate the memory
72 if (myEquationsSystem.constraint)
73 delete [] myEquationsSystem.constraint;
74 myEquationsSystem.constraints = theConstraints.size();
75 myEquationsSystem.constraint = new Slvs_Constraint[theConstraints.size()];
79 std::vector<Slvs_Constraint>::const_iterator aConstrIter = theConstraints.begin();
80 for (int i = 0; i < myEquationsSystem.constraints; i++, aConstrIter++)
81 myEquationsSystem.constraint[i] = *aConstrIter;
84 int SketchSolver_Solver::solve()
86 if (myEquationsSystem.constraints <= 0)
87 return SLVS_RESULT_EMPTY_SET;
89 Slvs_Solve(&myEquationsSystem, myGroupID);
91 return myEquationsSystem.result;
94 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
96 if (myEquationsSystem.result != SLVS_RESULT_OKAY)
99 if (theParameters.size() != myEquationsSystem.params)
100 return false; // number of parameters is not the same
102 std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
103 for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
105 if (myEquationsSystem.param[i].h != aParamIter->h)
106 return false; // sequence of parameters was changed
107 aParamIter->val = myEquationsSystem.param[i].val;