Salome HOME
First phase of SketchSolver refactoring
[modules/shaper.git] / src / SketchSolver / SolveSpaceSolver / SolveSpaceSolver_ConstraintWrapper.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SolveSpaceSolver_ConstraintWrapper.cpp
4 // Created: 2 Dec 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <SolveSpaceSolver_ConstraintWrapper.h>
8 #include <SolveSpaceSolver_ConstraintType.h>
9 #include <math.h>
10
11 SolveSpaceSolver_ConstraintWrapper::SolveSpaceSolver_ConstraintWrapper(
12     const ConstraintPtr&    theOriginal,
13     const Slvs_Constraint& theConstraint)
14   : mySlvsConstraint(theConstraint)
15 {
16   myBaseConstraint = theOriginal;
17   myValue = mySlvsConstraint.valA;
18 }
19
20 ConstraintID SolveSpaceSolver_ConstraintWrapper::id() const
21 {
22   return (ConstraintID)mySlvsConstraint.h;
23 }
24
25 void SolveSpaceSolver_ConstraintWrapper::setGroup(const GroupID& theGroup)
26 {
27   mySlvsConstraint.group = (Slvs_hGroup)theGroup;
28   std::list<EntityWrapperPtr>::iterator aSubsIt = myConstrained.begin();
29   for (; aSubsIt != myConstrained.end(); ++aSubsIt)
30     (*aSubsIt)->setGroup(theGroup);
31 }
32
33 SketchSolver_ConstraintType SolveSpaceSolver_ConstraintWrapper::type() const
34 {
35   return ConstraintType::fromSolveSpace(mySlvsConstraint.type);
36 }
37
38 void SolveSpaceSolver_ConstraintWrapper::setValue(const double& theValue)
39 {
40   double aValue = theValue;
41   if (type() == CONSTRAINT_RADIUS)
42     aValue *= 2.0; // NOTE: SolveSpace uses constraint DIAMETER
43
44   SketchSolver_IConstraintWrapper::setValue(aValue);
45 }
46
47 bool SolveSpaceSolver_ConstraintWrapper::isUsed(FeaturePtr theFeature) const
48 {
49   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
50   for (; anIt != myConstrained.end(); ++anIt)
51     if ((*anIt)->isUsed(theFeature))
52       return true;
53   return false;
54 }
55
56 bool SolveSpaceSolver_ConstraintWrapper::isUsed(AttributePtr theAttribute) const
57 {
58   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
59   for (; anIt != myConstrained.end(); ++anIt)
60     if ((*anIt)->isUsed(theAttribute))
61       return true;
62   return false;
63 }
64
65 bool SolveSpaceSolver_ConstraintWrapper::isEqual(const ConstraintWrapperPtr& theOther)
66 {
67   const Slvs_Constraint anOtherConstraint = 
68     std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theOther)->constraint();
69   if (mySlvsConstraint.type != anOtherConstraint.type)
70     return false;
71
72   // Verify SolveSpace entities. If they are equal, no need additional checking of parameters.
73   if (mySlvsConstraint.group   == anOtherConstraint.group   &&
74       mySlvsConstraint.ptA     == anOtherConstraint.ptA     &&
75       mySlvsConstraint.ptB     == anOtherConstraint.ptB     &&
76       mySlvsConstraint.entityA == anOtherConstraint.entityA &&
77       mySlvsConstraint.entityB == anOtherConstraint.entityB &&
78       mySlvsConstraint.entityC == anOtherConstraint.entityC &&
79       mySlvsConstraint.entityD == anOtherConstraint.entityD &&
80       fabs(mySlvsConstraint.valA - anOtherConstraint.valA) < tolerance) {
81     return true;
82   }
83
84   // Verify equality of values
85   if (fabs(myValue - theOther->value()) > tolerance)
86     return false;
87
88   // Verify equality of entities
89   const std::list<EntityWrapperPtr>& anOtherSubs = theOther->entities();
90   if (myConstrained.size() != anOtherSubs.size())
91     return false;
92   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = myConstrained.begin();
93   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
94   for (; aMySubsIt != myConstrained.end(); ++aMySubsIt, ++anOtherSubsIt)
95     if (!(*aMySubsIt)->isEqual(*anOtherSubsIt))
96       return false;
97   return true;
98 }
99
100 bool SolveSpaceSolver_ConstraintWrapper::update(const ConstraintWrapperPtr& theOther)
101 {
102   bool isUpdated = false;
103
104   std::list<EntityWrapperPtr> aMySubs = entities();
105   std::list<EntityWrapperPtr> anOtherSubs = theOther->entities();
106   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = aMySubs.begin();
107   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
108   for (; aMySubsIt != aMySubs.end() && anOtherSubsIt != anOtherSubs.end();
109        ++aMySubsIt, ++anOtherSubsIt)
110      isUpdated = (*aMySubsIt)->update(*anOtherSubsIt) || isUpdated;
111
112   if (fabs(value() - theOther->value()) > tolerance) {
113     myValue = theOther->value();
114     isUpdated = true;
115   }
116   return isUpdated;
117 }