Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchSolver / SolveSpaceSolver / SolveSpaceSolver_ConstraintWrapper.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <SolveSpaceSolver_ConstraintWrapper.h>
21 #include <SolveSpaceSolver_ConstraintType.h>
22 #include <math.h>
23
24 SolveSpaceSolver_ConstraintWrapper::SolveSpaceSolver_ConstraintWrapper(
25     const ConstraintPtr&    theOriginal,
26     const Slvs_Constraint& theConstraint)
27   : mySlvsConstraint(theConstraint)
28 {
29   myBaseConstraint = theOriginal;
30   myValue = mySlvsConstraint.valA;
31 }
32
33 ConstraintID SolveSpaceSolver_ConstraintWrapper::id() const
34 {
35   return (ConstraintID)mySlvsConstraint.h;
36 }
37
38 void SolveSpaceSolver_ConstraintWrapper::setGroup(const GroupID& theGroup)
39 {
40   mySlvsConstraint.group = (Slvs_hGroup)theGroup;
41   std::list<EntityWrapperPtr>::iterator aSubsIt = myConstrained.begin();
42   for (; aSubsIt != myConstrained.end(); ++aSubsIt)
43     (*aSubsIt)->setGroup(theGroup);
44 }
45
46 SketchSolver_ConstraintType SolveSpaceSolver_ConstraintWrapper::type() const
47 {
48   return ConstraintType::fromSolveSpace(mySlvsConstraint.type);
49 }
50
51 bool SolveSpaceSolver_ConstraintWrapper::isUsed(FeaturePtr theFeature) const
52 {
53   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
54   for (; anIt != myConstrained.end(); ++anIt)
55     if ((*anIt)->isUsed(theFeature))
56       return true;
57   return false;
58 }
59
60 bool SolveSpaceSolver_ConstraintWrapper::isUsed(AttributePtr theAttribute) const
61 {
62   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
63   for (; anIt != myConstrained.end(); ++anIt)
64     if ((*anIt)->isUsed(theAttribute))
65       return true;
66   return false;
67 }
68
69 bool SolveSpaceSolver_ConstraintWrapper::isEqual(const ConstraintWrapperPtr& theOther)
70 {
71   const Slvs_Constraint anOtherConstraint =
72     std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theOther)->constraint();
73   if (mySlvsConstraint.type != anOtherConstraint.type)
74     return false;
75
76   // Verify SolveSpace entities. If they are equal, no need additional checking of parameters.
77   if (mySlvsConstraint.group   == anOtherConstraint.group   &&
78       mySlvsConstraint.ptA     == anOtherConstraint.ptA     &&
79       mySlvsConstraint.ptB     == anOtherConstraint.ptB     &&
80       mySlvsConstraint.entityA == anOtherConstraint.entityA &&
81       mySlvsConstraint.entityB == anOtherConstraint.entityB &&
82       mySlvsConstraint.entityC == anOtherConstraint.entityC &&
83       mySlvsConstraint.entityD == anOtherConstraint.entityD &&
84       fabs(mySlvsConstraint.valA - anOtherConstraint.valA) < tolerance) {
85     return true;
86   }
87
88   // Verify equality of values
89   if (fabs(myValue - theOther->value()) > tolerance)
90     return false;
91
92   // Verify equality of entities
93   const std::list<EntityWrapperPtr>& anOtherSubs = theOther->entities();
94   if (myConstrained.size() != anOtherSubs.size())
95     return false;
96   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = myConstrained.begin();
97   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
98   for (; aMySubsIt != myConstrained.end(); ++aMySubsIt, ++anOtherSubsIt)
99     if (!(*aMySubsIt)->isEqual(*anOtherSubsIt))
100       return false;
101   return true;
102 }
103
104 bool SolveSpaceSolver_ConstraintWrapper::update(const ConstraintWrapperPtr& theOther)
105 {
106   bool isUpdated = false;
107
108   std::list<EntityWrapperPtr> aMySubs = entities();
109   std::list<EntityWrapperPtr> anOtherSubs = theOther->entities();
110   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = aMySubs.begin();
111   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
112   for (; aMySubsIt != aMySubs.end() && anOtherSubsIt != anOtherSubs.end();
113        ++aMySubsIt, ++anOtherSubsIt)
114      isUpdated = (*aMySubsIt)->update(*anOtherSubsIt) || isUpdated;
115
116   if (fabs(value() - theOther->value()) > tolerance) {
117     myValue = theOther->value();
118     isUpdated = true;
119   }
120   return isUpdated;
121 }