Salome HOME
a2be38efe0ea444771532bb37d0e1069005be4e9
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_ConstraintWrapper.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    PlaneGCSSolver_ConstraintWrapper.cpp
4 // Created: 14 Dec 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <PlaneGCSSolver_ConstraintWrapper.h>
8
9 #include <math.h>
10
11 PlaneGCSSolver_ConstraintWrapper::PlaneGCSSolver_ConstraintWrapper(
12     const ConstraintPtr& theOriginal,
13     const GCSConstraintPtr& theConstraint,
14     const SketchSolver_ConstraintType& theType)
15   : myGCSConstraints(1, theConstraint),
16     myType(theType),
17     myID(CID_UNKNOWN)
18 {
19   myBaseConstraint = theOriginal;
20   myValue = 0.0;
21 }
22
23 PlaneGCSSolver_ConstraintWrapper::PlaneGCSSolver_ConstraintWrapper(
24     const ConstraintPtr& theOriginal,
25     const std::list<GCSConstraintPtr>& theConstraints,
26     const SketchSolver_ConstraintType& theType)
27   : myGCSConstraints(theConstraints),
28     myType(theType),
29     myID(CID_UNKNOWN)
30 {
31   myBaseConstraint = theOriginal;
32   myValue = 0.0;
33 }
34
35 void PlaneGCSSolver_ConstraintWrapper::setId(const ConstraintID& theID)
36 {
37   myID = theID;
38   std::list<GCSConstraintPtr>::iterator anIt = myGCSConstraints.begin();
39   for (; anIt != myGCSConstraints.end(); ++anIt)
40     (*anIt)->setTag((int)theID);
41 }
42
43 void PlaneGCSSolver_ConstraintWrapper::setValueParameter(const ParameterWrapperPtr& theValue)
44 {
45   myValueParam = theValue;
46   myValue = myValueParam->value();
47 }
48
49 void PlaneGCSSolver_ConstraintWrapper::setValue(const double& theValue)
50 {
51   myValue = theValue;
52   myValueParam->setValue(theValue);
53 }
54
55
56 void PlaneGCSSolver_ConstraintWrapper::setGroup(const GroupID& theGroup)
57 {
58   myGroup = theGroup;
59   std::list<EntityWrapperPtr>::iterator aSubsIt = myConstrained.begin();
60   for (; aSubsIt != myConstrained.end(); ++aSubsIt)
61     (*aSubsIt)->setGroup(theGroup);
62 }
63
64 bool PlaneGCSSolver_ConstraintWrapper::isUsed(FeaturePtr theFeature) const
65 {
66   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
67   for (; anIt != myConstrained.end(); ++anIt)
68     if ((*anIt)->isUsed(theFeature))
69       return true;
70   return false;
71 }
72
73 bool PlaneGCSSolver_ConstraintWrapper::isUsed(AttributePtr theAttribute) const
74 {
75   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
76   for (; anIt != myConstrained.end(); ++anIt)
77     if ((*anIt)->isUsed(theAttribute))
78       return true;
79   return false;
80 }
81
82 bool PlaneGCSSolver_ConstraintWrapper::isEqual(const ConstraintWrapperPtr& theOther)
83 {
84   if (type() != theOther->type())
85     return false;
86
87   // Verify equality of values
88   if (fabs(myValue - theOther->value()) > tolerance)
89     return false;
90
91   // Verify equality of entities
92   const std::list<EntityWrapperPtr>& anOtherSubs = theOther->entities();
93   if (myConstrained.size() != anOtherSubs.size())
94     return false;
95   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = myConstrained.begin();
96   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
97   for (; aMySubsIt != myConstrained.end(); ++aMySubsIt, ++anOtherSubsIt)
98     if (!(*aMySubsIt)->isEqual(*anOtherSubsIt))
99       return false;
100   return true;
101 }
102
103 bool PlaneGCSSolver_ConstraintWrapper::update(const ConstraintWrapperPtr& theOther)
104 {
105   bool isUpdated = false;
106
107   std::list<EntityWrapperPtr> aMySubs = entities();
108   std::list<EntityWrapperPtr> anOtherSubs = theOther->entities();
109   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = aMySubs.begin();
110   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
111   for (; aMySubsIt != aMySubs.end() && anOtherSubsIt != anOtherSubs.end();
112        ++aMySubsIt, ++anOtherSubsIt)
113      isUpdated = (*aMySubsIt)->update(*anOtherSubsIt) || isUpdated;
114
115   if (fabs(value() - theOther->value()) > tolerance) {
116     myValue = theOther->value();
117     isUpdated = true;
118   }
119   return isUpdated;
120 }