Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchSolver / SolveSpaceSolver / SolveSpaceSolver_ConstraintWrapper.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <SolveSpaceSolver_ConstraintWrapper.h>
22 #include <SolveSpaceSolver_ConstraintType.h>
23 #include <math.h>
24
25 SolveSpaceSolver_ConstraintWrapper::SolveSpaceSolver_ConstraintWrapper(
26     const ConstraintPtr&    theOriginal,
27     const Slvs_Constraint& theConstraint)
28   : mySlvsConstraint(theConstraint)
29 {
30   myBaseConstraint = theOriginal;
31   myValue = mySlvsConstraint.valA;
32 }
33
34 ConstraintID SolveSpaceSolver_ConstraintWrapper::id() const
35 {
36   return (ConstraintID)mySlvsConstraint.h;
37 }
38
39 void SolveSpaceSolver_ConstraintWrapper::setGroup(const GroupID& theGroup)
40 {
41   mySlvsConstraint.group = (Slvs_hGroup)theGroup;
42   std::list<EntityWrapperPtr>::iterator aSubsIt = myConstrained.begin();
43   for (; aSubsIt != myConstrained.end(); ++aSubsIt)
44     (*aSubsIt)->setGroup(theGroup);
45 }
46
47 SketchSolver_ConstraintType SolveSpaceSolver_ConstraintWrapper::type() const
48 {
49   return ConstraintType::fromSolveSpace(mySlvsConstraint.type);
50 }
51
52 bool SolveSpaceSolver_ConstraintWrapper::isUsed(FeaturePtr theFeature) const
53 {
54   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
55   for (; anIt != myConstrained.end(); ++anIt)
56     if ((*anIt)->isUsed(theFeature))
57       return true;
58   return false;
59 }
60
61 bool SolveSpaceSolver_ConstraintWrapper::isUsed(AttributePtr theAttribute) const
62 {
63   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
64   for (; anIt != myConstrained.end(); ++anIt)
65     if ((*anIt)->isUsed(theAttribute))
66       return true;
67   return false;
68 }
69
70 bool SolveSpaceSolver_ConstraintWrapper::isEqual(const ConstraintWrapperPtr& theOther)
71 {
72   const Slvs_Constraint anOtherConstraint =
73     std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theOther)->constraint();
74   if (mySlvsConstraint.type != anOtherConstraint.type)
75     return false;
76
77   // Verify SolveSpace entities. If they are equal, no need additional checking of parameters.
78   if (mySlvsConstraint.group   == anOtherConstraint.group   &&
79       mySlvsConstraint.ptA     == anOtherConstraint.ptA     &&
80       mySlvsConstraint.ptB     == anOtherConstraint.ptB     &&
81       mySlvsConstraint.entityA == anOtherConstraint.entityA &&
82       mySlvsConstraint.entityB == anOtherConstraint.entityB &&
83       mySlvsConstraint.entityC == anOtherConstraint.entityC &&
84       mySlvsConstraint.entityD == anOtherConstraint.entityD &&
85       fabs(mySlvsConstraint.valA - anOtherConstraint.valA) < tolerance) {
86     return true;
87   }
88
89   // Verify equality of values
90   if (fabs(myValue - theOther->value()) > tolerance)
91     return false;
92
93   // Verify equality of entities
94   const std::list<EntityWrapperPtr>& anOtherSubs = theOther->entities();
95   if (myConstrained.size() != anOtherSubs.size())
96     return false;
97   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = myConstrained.begin();
98   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
99   for (; aMySubsIt != myConstrained.end(); ++aMySubsIt, ++anOtherSubsIt)
100     if (!(*aMySubsIt)->isEqual(*anOtherSubsIt))
101       return false;
102   return true;
103 }
104
105 bool SolveSpaceSolver_ConstraintWrapper::update(const ConstraintWrapperPtr& theOther)
106 {
107   bool isUpdated = false;
108
109   std::list<EntityWrapperPtr> aMySubs = entities();
110   std::list<EntityWrapperPtr> anOtherSubs = theOther->entities();
111   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = aMySubs.begin();
112   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
113   for (; aMySubsIt != aMySubs.end() && anOtherSubsIt != anOtherSubs.end();
114        ++aMySubsIt, ++anOtherSubsIt)
115      isUpdated = (*aMySubsIt)->update(*anOtherSubsIt) || isUpdated;
116
117   if (fabs(value() - theOther->value()) > tolerance) {
118     myValue = theOther->value();
119     isUpdated = true;
120   }
121   return isUpdated;
122 }