Salome HOME
Issue #2711: Crash when calling split
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintDistance.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 <SketchSolver_ConstraintDistance.h>
22 #include <SketchSolver_Error.h>
23 #include <SketchSolver_Manager.h>
24
25 #include <PlaneGCSSolver_EdgeWrapper.h>
26 #include <PlaneGCSSolver_PointWrapper.h>
27 #include <PlaneGCSSolver_Storage.h>
28 #include <PlaneGCSSolver_Tools.h>
29
30 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
31 #include <SketchPlugin_ConstraintDistanceVertical.h>
32
33 #include <GeomAPI_Dir2d.h>
34 #include <GeomAPI_Lin2d.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_XY.h>
37
38 #include <math.h>
39
40
41 void SketchSolver_ConstraintDistance::getAttributes(
42     EntityWrapperPtr& theValue,
43     std::vector<EntityWrapperPtr>& theAttributes)
44 {
45   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
46   if (!myErrorMsg.empty() || !theAttributes[0]) {
47     theAttributes.clear();
48     return;
49   }
50
51   if (theAttributes[1]) {
52     if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
53       myType = CONSTRAINT_HORIZONTAL_DISTANCE;
54     else if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
55       myType = CONSTRAINT_VERTICAL_DISTANCE;
56     else
57       myType = CONSTRAINT_PT_PT_DISTANCE;
58   } else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
59     myType = CONSTRAINT_PT_LINE_DISTANCE;
60   else
61     theAttributes.clear();
62
63   myPrevValue = 0.0;
64 }
65
66 void SketchSolver_ConstraintDistance::adjustConstraint()
67 {
68   if (getType() == CONSTRAINT_PT_LINE_DISTANCE) {
69     bool isSigned = myBaseConstraint->boolean(SketchPlugin_ConstraintDistance::SIGNED())->value();
70     if (myIsSigned == isSigned)
71       return; // distance type is not changed => nothing to adjust
72
73     // Adjust point-line distance by setting/removing additional constraints
74     if (isSigned)
75       addConstraintsToKeepSign();
76     else
77       removeConstraintsKeepingSign();
78     myIsSigned = isSigned;
79   }
80 }
81
82 void SketchSolver_ConstraintDistance::update()
83 {
84   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
85   myPrevValue = aConstraint->value();
86
87   SketchSolver_Constraint::update();
88 }
89
90 bool SketchSolver_ConstraintDistance::remove()
91 {
92   removeConstraintsKeepingSign();
93   return SketchSolver_Constraint::remove();
94 }
95
96 void SketchSolver_ConstraintDistance::addConstraintsToKeepSign()
97 {
98   std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
99       std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
100
101   ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
102   std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
103
104   // calculate projection of the point on the line and find a sign of a distance
105   EntityWrapperPtr aDistPoint, aDistLine;
106   for (int i = 0; i < 2; ++i) {
107     AttributePtr anAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
108     EntityWrapperPtr anEntity = myStorage->entity(anAttr);
109     if (anEntity->type() == ENTITY_POINT)
110       aDistPoint = anEntity;
111     else if (anEntity->type() == ENTITY_LINE)
112       aDistLine = anEntity;
113   }
114   std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(aDistLine);
115   std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(aDistPoint);
116
117   std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
118   std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
119   if (aLineVec->cross(aPtLineVec) >= 0.)
120     mySignValue = PI/2.0;
121   else
122     mySignValue = - PI/2.0;
123   double aDot = aPtLineVec->dot(aLineVec);
124   std::shared_ptr<GeomAPI_XY> aProjectedPnt =
125       aLine->location()->xy()->added(aLineVec->multiplied(aDot));
126
127   // create auxiliary point on the line and set auxiliary constraints
128   myOddPoint = GCSPointPtr(new GCS::Point);
129   myOddPoint->x = aStorage->createParameter();
130   myOddPoint->y = aStorage->createParameter();
131   *(myOddPoint->x) = aProjectedPnt->x();
132   *(myOddPoint->y) = aProjectedPnt->y();
133
134   PointWrapperPtr aPointWr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aDistPoint);
135   EdgeWrapperPtr anEdgeWr = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(aDistLine);
136   std::shared_ptr<GCS::Line> aGCSLine = std::dynamic_pointer_cast<GCS::Line>(anEdgeWr->entity());
137   // point-on-line
138   GCSConstraintPtr aNewConstraint(new GCS::ConstraintPointOnLine(*myOddPoint, *aGCSLine));
139   aGCSConstraints.push_back(aNewConstraint);
140   // angle which keep orientation
141   aNewConstraint = GCSConstraintPtr(new GCS::ConstraintL2LAngle(
142       aGCSLine->p1, aGCSLine->p2, *myOddPoint, *(aPointWr->point()), &mySignValue));
143   aGCSConstraints.push_back(aNewConstraint);
144
145   aConstraint->setConstraints(aGCSConstraints);
146
147   aStorage->removeConstraint(myBaseConstraint);
148   aStorage->addConstraint(myBaseConstraint, aConstraint);
149 }
150
151 void SketchSolver_ConstraintDistance::removeConstraintsKeepingSign()
152 {
153   if (!myOddPoint)
154     return; // no sign kept => nothing to remove
155
156   std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
157       std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
158
159   ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
160   std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
161
162   aStorage->removeConstraint(myBaseConstraint);
163
164   // remove parameters related to auxiliary point
165   GCS::SET_pD aParams;
166   aParams.insert(myOddPoint->x);
167   aParams.insert(myOddPoint->y);
168   aStorage->removeParameters(aParams);
169
170   aGCSConstraints.pop_back();
171   aGCSConstraints.pop_back();
172   aConstraint->setConstraints(aGCSConstraints);
173   aStorage->addConstraint(myBaseConstraint, aConstraint);
174
175   myIsSigned = false;
176 }