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()
12 // Nullify all elements of the set of equations
13 myEquationsSystem.param = 0;
14 myEquationsSystem.params = 0;
15 myEquationsSystem.entity = 0;
16 myEquationsSystem.entities = 0;
17 myEquationsSystem.constraint = 0;
18 myEquationsSystem.constraints = 0;
19 myEquationsSystem.failed = 0;
20 myEquationsSystem.faileds = 0;
22 myEquationsSystem.dragged[0] = 0;
23 myEquationsSystem.dragged[1] = 0;
24 myEquationsSystem.dragged[2] = 0;
25 myEquationsSystem.dragged[3] = 0;
27 // If the set of constraints is inconsistent,
28 // the failed field will contain wrong constraints
29 myEquationsSystem.calculateFaileds = 1;
32 SketchSolver_Solver::~SketchSolver_Solver()
34 if (myEquationsSystem.param)
35 delete[] myEquationsSystem.param;
36 if (myEquationsSystem.entity)
37 delete[] myEquationsSystem.entity;
38 if (myEquationsSystem.constraint)
39 delete[] myEquationsSystem.constraint;
40 if (myEquationsSystem.failed)
41 delete[] myEquationsSystem.failed;
44 void SketchSolver_Solver::setParameters(const std::vector<Slvs_Param>& theParameters)
46 if (theParameters.size() != myEquationsSystem.params) // number of parameters was changed => reallocate the memory
48 if (myEquationsSystem.param)
49 delete[] myEquationsSystem.param;
50 myEquationsSystem.params = theParameters.size();
51 myEquationsSystem.param = new Slvs_Param[theParameters.size()];
55 std::vector<Slvs_Param>::const_iterator aParamIter = theParameters.begin();
56 for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++)
57 myEquationsSystem.param[i] = *aParamIter;
60 void SketchSolver_Solver::setDraggedParameters(const std::vector<Slvs_hParam>& theDragged)
62 if (theDragged.size() == 0) {
63 myEquationsSystem.dragged[0] = 0;
64 myEquationsSystem.dragged[1] = 0;
65 myEquationsSystem.dragged[2] = 0;
66 myEquationsSystem.dragged[3] = 0;
69 for (unsigned int i = 0; i < theDragged.size(); i++)
70 myEquationsSystem.dragged[i] = theDragged[i];
73 void SketchSolver_Solver::setEntities(const std::vector<Slvs_Entity>& theEntities)
75 if (theEntities.size() != myEquationsSystem.entities) // number of entities was changed => reallocate the memory
77 if (myEquationsSystem.entity)
78 delete[] myEquationsSystem.entity;
79 myEquationsSystem.entities = theEntities.size();
80 myEquationsSystem.entity = new Slvs_Entity[theEntities.size()];
84 std::vector<Slvs_Entity>::const_iterator aEntIter = theEntities.begin();
85 for (int i = 0; i < myEquationsSystem.entities; i++, aEntIter++)
86 myEquationsSystem.entity[i] = *aEntIter;
89 void SketchSolver_Solver::setConstraints(const std::vector<Slvs_Constraint>& theConstraints)
91 if (theConstraints.size() != myEquationsSystem.constraints) // number of constraints was changed => reallocate the memory
93 if (myEquationsSystem.constraint)
94 delete[] myEquationsSystem.constraint;
95 myEquationsSystem.constraints = theConstraints.size();
96 myEquationsSystem.constraint = new Slvs_Constraint[theConstraints.size()];
98 // Assign the memory for the failed constraints
99 if (myEquationsSystem.failed)
100 delete[] myEquationsSystem.failed;
101 myEquationsSystem.failed = new Slvs_hConstraint[theConstraints.size()];
102 myEquationsSystem.faileds = theConstraints.size();
106 std::vector<Slvs_Constraint>::const_iterator aConstrIter = theConstraints.begin();
107 for (int i = 0; i < myEquationsSystem.constraints; i++, aConstrIter++)
108 myEquationsSystem.constraint[i] = *aConstrIter;
111 int SketchSolver_Solver::solve()
113 if (myEquationsSystem.constraints <= 0)
114 return SLVS_RESULT_EMPTY_SET;
116 Events_LongOp::start(this);
117 Slvs_Solve(&myEquationsSystem, myGroupID);
118 Events_LongOp::end(this);
120 return myEquationsSystem.result;
123 bool SketchSolver_Solver::getResult(std::vector<Slvs_Param>& theParameters)
125 if (myEquationsSystem.result != SLVS_RESULT_OKAY)
128 if (theParameters.size() != myEquationsSystem.params)
129 return false; // number of parameters is not the same
131 std::vector<Slvs_Param>::iterator aParamIter = theParameters.begin();
132 for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++) {
133 if (myEquationsSystem.param[i].h != aParamIter->h)
134 return false; // sequence of parameters was changed
135 aParamIter->val = myEquationsSystem.param[i].val;