X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FSketchSolver%2FSketchSolver_Solver.cpp;h=7e3a33de2891d609d769b2505179b7a2f46f91b4;hb=51889d235a27d0ee4b3c3237d21d1ea621063580;hp=bf86e4d44f4b9ee6704d05266d556acc92b11312;hpb=7ecefe4ca74618ab6fc2e7990b6ad50c12bca63c;p=modules%2Fshaper.git diff --git a/src/SketchSolver/SketchSolver_Solver.cpp b/src/SketchSolver/SketchSolver_Solver.cpp index bf86e4d44..7e3a33de2 100644 --- a/src/SketchSolver/SketchSolver_Solver.cpp +++ b/src/SketchSolver/SketchSolver_Solver.cpp @@ -1,5 +1,139 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + // File: SketchSolver_Solver.cpp // Created: 07 May 2014 // Author: Artem ZHIDKOV #include "SketchSolver_Solver.h" +#include + +SketchSolver_Solver::SketchSolver_Solver() +{ + // Nullify all elements of the set of equations + myEquationsSystem.param = 0; + myEquationsSystem.params = 0; + myEquationsSystem.entity = 0; + myEquationsSystem.entities = 0; + myEquationsSystem.constraint = 0; + myEquationsSystem.constraints = 0; + myEquationsSystem.failed = 0; + myEquationsSystem.faileds = 0; + + myEquationsSystem.dragged[0] = 0; + myEquationsSystem.dragged[1] = 0; + myEquationsSystem.dragged[2] = 0; + myEquationsSystem.dragged[3] = 0; + + // If the set of constraints is inconsistent, + // the failed field will contain wrong constraints + myEquationsSystem.calculateFaileds = 1; +} + +SketchSolver_Solver::~SketchSolver_Solver() +{ + if (myEquationsSystem.param) + delete[] myEquationsSystem.param; + if (myEquationsSystem.entity) + delete[] myEquationsSystem.entity; + if (myEquationsSystem.constraint) + delete[] myEquationsSystem.constraint; + if (myEquationsSystem.failed) + delete[] myEquationsSystem.failed; +} + +void SketchSolver_Solver::setParameters(const std::vector& theParameters) +{ + if (theParameters.size() != myEquationsSystem.params) // number of parameters was changed => reallocate the memory + { + if (myEquationsSystem.param) + delete[] myEquationsSystem.param; + myEquationsSystem.params = theParameters.size(); + myEquationsSystem.param = new Slvs_Param[theParameters.size()]; + } + + // Copy data + std::vector::const_iterator aParamIter = theParameters.begin(); + for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++) + myEquationsSystem.param[i] = *aParamIter; +} + +void SketchSolver_Solver::setDraggedParameters(const std::vector& theDragged) +{ + if (theDragged.size() == 0) { + myEquationsSystem.dragged[0] = 0; + myEquationsSystem.dragged[1] = 0; + myEquationsSystem.dragged[2] = 0; + myEquationsSystem.dragged[3] = 0; + return; + } + for (unsigned int i = 0; i < theDragged.size(); i++) + myEquationsSystem.dragged[i] = theDragged[i]; +} + +void SketchSolver_Solver::setEntities(const std::vector& theEntities) +{ + if (theEntities.size() != myEquationsSystem.entities) // number of entities was changed => reallocate the memory + { + if (myEquationsSystem.entity) + delete[] myEquationsSystem.entity; + myEquationsSystem.entities = theEntities.size(); + myEquationsSystem.entity = new Slvs_Entity[theEntities.size()]; + } + + // Copy data + std::vector::const_iterator aEntIter = theEntities.begin(); + for (int i = 0; i < myEquationsSystem.entities; i++, aEntIter++) + myEquationsSystem.entity[i] = *aEntIter; +} + +void SketchSolver_Solver::setConstraints(const std::vector& theConstraints) +{ + if (theConstraints.size() != myEquationsSystem.constraints) // number of constraints was changed => reallocate the memory + { + if (myEquationsSystem.constraint) + delete[] myEquationsSystem.constraint; + myEquationsSystem.constraints = theConstraints.size(); + myEquationsSystem.constraint = new Slvs_Constraint[theConstraints.size()]; + + // Assign the memory for the failed constraints + if (myEquationsSystem.failed) + delete[] myEquationsSystem.failed; + myEquationsSystem.failed = new Slvs_hConstraint[theConstraints.size()]; + myEquationsSystem.faileds = theConstraints.size(); + } + + // Copy data + std::vector::const_iterator aConstrIter = theConstraints.begin(); + for (int i = 0; i < myEquationsSystem.constraints; i++, aConstrIter++) + myEquationsSystem.constraint[i] = *aConstrIter; +} + +int SketchSolver_Solver::solve() +{ + if (myEquationsSystem.constraints <= 0) + return SLVS_RESULT_EMPTY_SET; + + Events_LongOp::start(this); + Slvs_Solve(&myEquationsSystem, myGroupID); + Events_LongOp::end(this); + + return myEquationsSystem.result; +} + +bool SketchSolver_Solver::getResult(std::vector& theParameters) +{ + if (myEquationsSystem.result != SLVS_RESULT_OKAY) + return false; + + if (theParameters.size() != myEquationsSystem.params) + return false; // number of parameters is not the same + + std::vector::iterator aParamIter = theParameters.begin(); + for (int i = 0; i < myEquationsSystem.params; i++, aParamIter++) { + if (myEquationsSystem.param[i].h != aParamIter->h) + return false; // sequence of parameters was changed + aParamIter->val = myEquationsSystem.param[i].val; + } + + return true; +}