Salome HOME
Issue #1834: Fix length of lines
[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 bool SolveSpaceSolver_ConstraintWrapper::isUsed(FeaturePtr theFeature) const
39 {
40   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
41   for (; anIt != myConstrained.end(); ++anIt)
42     if ((*anIt)->isUsed(theFeature))
43       return true;
44   return false;
45 }
46
47 bool SolveSpaceSolver_ConstraintWrapper::isUsed(AttributePtr theAttribute) const
48 {
49   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
50   for (; anIt != myConstrained.end(); ++anIt)
51     if ((*anIt)->isUsed(theAttribute))
52       return true;
53   return false;
54 }
55
56 bool SolveSpaceSolver_ConstraintWrapper::isEqual(const ConstraintWrapperPtr& theOther)
57 {
58   const Slvs_Constraint anOtherConstraint = 
59     std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theOther)->constraint();
60   if (mySlvsConstraint.type != anOtherConstraint.type)
61     return false;
62
63   // Verify SolveSpace entities. If they are equal, no need additional checking of parameters.
64   if (mySlvsConstraint.group   == anOtherConstraint.group   &&
65       mySlvsConstraint.ptA     == anOtherConstraint.ptA     &&
66       mySlvsConstraint.ptB     == anOtherConstraint.ptB     &&
67       mySlvsConstraint.entityA == anOtherConstraint.entityA &&
68       mySlvsConstraint.entityB == anOtherConstraint.entityB &&
69       mySlvsConstraint.entityC == anOtherConstraint.entityC &&
70       mySlvsConstraint.entityD == anOtherConstraint.entityD &&
71       fabs(mySlvsConstraint.valA - anOtherConstraint.valA) < tolerance) {
72     return true;
73   }
74
75   // Verify equality of values
76   if (fabs(myValue - theOther->value()) > tolerance)
77     return false;
78
79   // Verify equality of entities
80   const std::list<EntityWrapperPtr>& anOtherSubs = theOther->entities();
81   if (myConstrained.size() != anOtherSubs.size())
82     return false;
83   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = myConstrained.begin();
84   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
85   for (; aMySubsIt != myConstrained.end(); ++aMySubsIt, ++anOtherSubsIt)
86     if (!(*aMySubsIt)->isEqual(*anOtherSubsIt))
87       return false;
88   return true;
89 }
90
91 bool SolveSpaceSolver_ConstraintWrapper::update(const ConstraintWrapperPtr& theOther)
92 {
93   bool isUpdated = false;
94
95   std::list<EntityWrapperPtr> aMySubs = entities();
96   std::list<EntityWrapperPtr> anOtherSubs = theOther->entities();
97   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = aMySubs.begin();
98   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
99   for (; aMySubsIt != aMySubs.end() && anOtherSubsIt != anOtherSubs.end();
100        ++aMySubsIt, ++anOtherSubsIt)
101      isUpdated = (*aMySubsIt)->update(*anOtherSubsIt) || isUpdated;
102
103   if (fabs(value() - theOther->value()) > tolerance) {
104     myValue = theOther->value();
105     isUpdated = true;
106   }
107   return isUpdated;
108 }