Salome HOME
a46a7c0b37cbef86b0731cf3151bed645c044516
[modules/shaper.git] / src / SketchSolver / SolveSpaceSolver / SolveSpaceSolver_Solver.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SolveSpaceSolver_Solver.cpp
4 // Created: 07 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SolveSpaceSolver_Solver.h"
8 #include <Events_LongOp.h>
9
10 SolveSpaceSolver_Solver::SolveSpaceSolver_Solver()
11 {
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;
21
22   myEquationsSystem.dragged[0] = 0;
23   myEquationsSystem.dragged[1] = 0;
24   myEquationsSystem.dragged[2] = 0;
25   myEquationsSystem.dragged[3] = 0;
26
27   // If the set of constraints is inconsistent,
28   // the failed field will contain wrong constraints
29   myEquationsSystem.calculateFaileds = 0;
30 }
31
32 SolveSpaceSolver_Solver::~SolveSpaceSolver_Solver()
33 {
34   if (myEquationsSystem.constraint)
35     delete[] myEquationsSystem.constraint;
36   myEquationsSystem.constraint = 0;
37   if (myEquationsSystem.failed)
38     delete[] myEquationsSystem.failed;
39   myEquationsSystem.failed = 0;
40 }
41
42 void SolveSpaceSolver_Solver::setParameters(Slvs_Param* theParameters, int theSize)
43 {
44   myEquationsSystem.param = theParameters;
45   myEquationsSystem.params = theSize;
46 }
47
48
49 void SolveSpaceSolver_Solver::setDraggedParameters(const Slvs_hParam* theDragged)
50 {
51   for (unsigned int i = 0; i < 4; i++)
52     myEquationsSystem.dragged[i] = theDragged[i];
53 }
54
55 void SolveSpaceSolver_Solver::setEntities(Slvs_Entity* theEntities, int theSize)
56 {
57   myEquationsSystem.entity = theEntities;
58   myEquationsSystem.entities = theSize;
59 }
60
61 void SolveSpaceSolver_Solver::setConstraints(Slvs_Constraint* theConstraints, int theSize)
62 {
63   if (!myEquationsSystem.constraint) {
64     myEquationsSystem.constraint = new Slvs_Constraint[theSize];
65     myEquationsSystem.constraints = theSize;
66     myEquationsSystem.failed = new Slvs_hConstraint[theSize];
67   }
68   else if (myEquationsSystem.constraints != theSize) {
69     if (theSize > myEquationsSystem.constraints) {
70       delete[] myEquationsSystem.constraint;
71       myEquationsSystem.constraint = new Slvs_Constraint[theSize];
72       if (myEquationsSystem.failed)
73         delete[] myEquationsSystem.failed;
74       myEquationsSystem.failed = new Slvs_hConstraint[theSize];
75     }
76     myEquationsSystem.constraints = theSize;
77   }
78   memcpy(myEquationsSystem.constraint, theConstraints, theSize * sizeof(Slvs_Constraint));
79   memset(myEquationsSystem.failed, SLVS_C_UNKNOWN, theSize * sizeof(Slvs_hConstraint));
80 }
81
82
83 SketchSolver_SolveStatus SolveSpaceSolver_Solver::solve()
84 {
85   //if (myEquationsSystem.constraints <= 0)
86   //  return STATUS_EMPTYSET;
87
88   myEquationsSystem.calculateFaileds = myFindFaileds ? 1 : 0;
89
90   Events_LongOp::start(this);
91   Slvs_Solve(&myEquationsSystem, myGroup);
92   Events_LongOp::end(this);
93
94   SketchSolver_SolveStatus aStatus;
95   switch (myEquationsSystem.result) {
96   case SLVS_RESULT_OKAY:
97     aStatus = STATUS_OK;
98     break;
99   case SLVS_RESULT_INCONSISTENT:
100   case SLVS_RESULT_DIDNT_CONVERGE:
101   case SLVS_RESULT_TOO_MANY_UNKNOWNS:
102     aStatus = STATUS_INCONSISTENT;
103     break;
104   default:
105     aStatus = STATUS_FAILED;
106   }
107   return aStatus;
108 }