Salome HOME
Second phase of SketchSolver refactoring
[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::setValueParameter(const ParameterWrapperPtr& theValue)
36 {
37   myValueParam = theValue;
38   myValue = myValueParam->value();
39 }
40
41 void PlaneGCSSolver_ConstraintWrapper::setValue(const double& theValue)
42 {
43   myValue = theValue;
44   myValueParam->setValue(theValue);
45 }
46
47
48 void PlaneGCSSolver_ConstraintWrapper::setGroup(const GroupID& theGroup)
49 {
50   myGroup = theGroup;
51   std::list<EntityWrapperPtr>::iterator aSubsIt = myConstrained.begin();
52   for (; aSubsIt != myConstrained.end(); ++aSubsIt)
53     (*aSubsIt)->setGroup(theGroup);
54 }
55
56 bool PlaneGCSSolver_ConstraintWrapper::isUsed(FeaturePtr theFeature) const
57 {
58   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
59   for (; anIt != myConstrained.end(); ++anIt)
60     if ((*anIt)->isUsed(theFeature))
61       return true;
62   return false;
63 }
64
65 bool PlaneGCSSolver_ConstraintWrapper::isUsed(AttributePtr theAttribute) const
66 {
67   std::list<EntityWrapperPtr>::const_iterator anIt = myConstrained.begin();
68   for (; anIt != myConstrained.end(); ++anIt)
69     if ((*anIt)->isUsed(theAttribute))
70       return true;
71   return false;
72 }
73
74 bool PlaneGCSSolver_ConstraintWrapper::isEqual(const ConstraintWrapperPtr& theOther)
75 {
76   if (type() != theOther->type())
77     return false;
78 ////  const Slvs_Constraint anOtherConstraint = 
79 ////    std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theOther)->constraint();
80 ////  if (mySlvsConstraint.type != anOtherConstraint.type)
81 ////    return false;
82 ////
83 ////  // Verify SolveSpace entities. If they are equal, no need additional checking of parameters.
84 ////  if (mySlvsConstraint.group   == anOtherConstraint.group   &&
85 ////      mySlvsConstraint.ptA     == anOtherConstraint.ptA     &&
86 ////      mySlvsConstraint.ptB     == anOtherConstraint.ptB     &&
87 ////      mySlvsConstraint.entityA == anOtherConstraint.entityA &&
88 ////      mySlvsConstraint.entityB == anOtherConstraint.entityB &&
89 ////      mySlvsConstraint.entityC == anOtherConstraint.entityC &&
90 ////      mySlvsConstraint.entityD == anOtherConstraint.entityD &&
91 ////      fabs(mySlvsConstraint.valA - anOtherConstraint.valA) < tolerance) {
92 ////    return true;
93 ////  }
94
95   // Verify equality of values
96   if (fabs(myValue - theOther->value()) > tolerance)
97     return false;
98
99   // Verify equality of entities
100   const std::list<EntityWrapperPtr>& anOtherSubs = theOther->entities();
101   if (myConstrained.size() != anOtherSubs.size())
102     return false;
103   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = myConstrained.begin();
104   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
105   for (; aMySubsIt != myConstrained.end(); ++aMySubsIt, ++anOtherSubsIt)
106     if (!(*aMySubsIt)->isEqual(*anOtherSubsIt))
107       return false;
108   return true;
109 }
110
111 bool PlaneGCSSolver_ConstraintWrapper::update(const ConstraintWrapperPtr& theOther)
112 {
113   bool isUpdated = false;
114
115   std::list<EntityWrapperPtr> aMySubs = entities();
116   std::list<EntityWrapperPtr> anOtherSubs = theOther->entities();
117   std::list<EntityWrapperPtr>::const_iterator aMySubsIt = aMySubs.begin();
118   std::list<EntityWrapperPtr>::const_iterator anOtherSubsIt = anOtherSubs.begin();
119   for (; aMySubsIt != aMySubs.end() && anOtherSubsIt != anOtherSubs.end();
120        ++aMySubsIt, ++anOtherSubsIt)
121      isUpdated = (*aMySubsIt)->update(*anOtherSubsIt) || isUpdated;
122
123   if (fabs(value() - theOther->value()) > tolerance) {
124     myValue = theOther->value();
125     isUpdated = true;
126   }
127   return isUpdated;
128 }