1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <SketchSolver_ConstraintDistance.h>
21 #include <SketchSolver_Error.h>
22 #include <SketchSolver_Manager.h>
24 #include <PlaneGCSSolver_EdgeWrapper.h>
25 #include <PlaneGCSSolver_PointWrapper.h>
26 #include <PlaneGCSSolver_Storage.h>
27 #include <PlaneGCSSolver_Tools.h>
28 #include <PlaneGCSSolver_UpdateCoincidence.h>
30 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
31 #include <SketchPlugin_ConstraintDistanceVertical.h>
33 #include <GeomAPI_Dir2d.h>
34 #include <GeomAPI_Lin2d.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_XY.h>
41 static void getPointAndLine(const ConstraintPtr& theConstraint, const StoragePtr& theStorage,
42 EntityWrapperPtr& thePoint, EntityWrapperPtr& theLine)
44 for (int i = 0; i < 2; ++i) {
45 AttributePtr anAttr = theConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
46 EntityWrapperPtr anEntity = theStorage->entity(anAttr);
47 if (anEntity->type() == ENTITY_POINT)
49 else if (anEntity->type() == ENTITY_LINE)
54 static void adjustOddPoint(const EntityWrapperPtr& theDistPoint,
55 const EntityWrapperPtr& theDistLine,
56 GCSPointPtr& theOddPoint)
61 std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(theDistLine);
62 std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(theDistPoint);
64 std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
65 std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
67 double aDot = aPtLineVec->dot(aLineVec);
68 std::shared_ptr<GeomAPI_XY> aProjectedPnt =
69 aLine->location()->xy()->added(aLineVec->multiplied(aDot));
71 *(theOddPoint->x) = aProjectedPnt->x();
72 *(theOddPoint->y) = aProjectedPnt->y();
75 static FeaturePtr getFeature(AttributeRefAttrPtr theRefAttr)
78 if (theRefAttr->isObject())
79 anObj = theRefAttr->object();
81 anObj = theRefAttr->attr()->owner();
82 return ModelAPI_Feature::feature(anObj);
85 static void calculateDistanceDirection(const ConstraintPtr& theConstraint,
86 const StoragePtr& theStorage,
87 double& theDirX, double& theDirY)
89 std::shared_ptr<GeomDataAPI_Dir> aDistDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
90 theConstraint->attribute(SketchPlugin_ConstraintDistance::DIRECTION_ID()));
91 if (aDistDir && aDistDir->isInitialized()) {
92 theDirX = aDistDir->x();
93 theDirY = aDistDir->y();
94 if (fabs(theDirX) > tolerance || fabs(theDirY) > tolerance)
98 AttributeRefAttrPtr aRefAttrA = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
99 AttributeRefAttrPtr aRefAttrB = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
101 EntityWrapperPtr aEntityA = theStorage->entity(aRefAttrA);
102 EntityWrapperPtr aEntityB = theStorage->entity(aRefAttrB);
105 if (aEntityA->type() != ENTITY_LINE && aEntityB->type() != ENTITY_LINE) {
106 aPoint = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aEntityA)->point();
110 EdgeWrapperPtr anEdgeA = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
111 theStorage->entity(getFeature(aRefAttrA)));
112 EdgeWrapperPtr anEdgeB = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
113 theStorage->entity(getFeature(aRefAttrB)));
115 if (anEdgeA && anEdgeB) {
116 GCS::DeriVector2 aDirA = anEdgeA->entity()->CalculateNormal(*aPoint);
117 GCS::DeriVector2 aDirB = anEdgeB->entity()->CalculateNormal(*aPoint);
118 double x = -aDirA.x + aDirB.x;
119 double y = -aDirA.y + aDirB.y;
120 double norm = sqrt(x*x + y*y);
121 if (norm > tolerance) {
129 static void moveEntity(const ConstraintPtr& theConstraint,
130 const StoragePtr& theStorage,
131 const double theDX, const double theDY)
133 static const double THE_SHIFT = 1.e-4;
135 AttributeRefAttrPtr aRefAttrA = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
136 AttributeRefAttrPtr aRefAttrB = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
138 EntityWrapperPtr aEntityA = theStorage->entity(aRefAttrA);
139 EntityWrapperPtr aEntityB = theStorage->entity(aRefAttrB);
141 PointWrapperPtr aPointA = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aEntityA);
142 PointWrapperPtr aPointB = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aEntityB);
145 *aPointA->point()->x -= THE_SHIFT * theDX;
146 *aPointA->point()->y -= THE_SHIFT * theDY;
149 *aPointB->point()->x += THE_SHIFT * theDX;
150 *aPointB->point()->y += THE_SHIFT * theDY;
156 void SketchSolver_ConstraintDistance::getAttributes(
157 EntityWrapperPtr& theValue,
158 std::vector<EntityWrapperPtr>& theAttributes)
160 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
161 if (!myErrorMsg.empty() || !theAttributes[0]) {
162 theAttributes.clear();
166 ScalarWrapperPtr aValue = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(theValue);
167 bool isCoincidence = fabs(aValue->value()) < tolerance;
169 if (theAttributes[1]) {
170 if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
171 myType = CONSTRAINT_HORIZONTAL_DISTANCE;
172 else if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
173 myType = CONSTRAINT_VERTICAL_DISTANCE;
175 myType = isCoincidence ? CONSTRAINT_PT_PT_COINCIDENT : CONSTRAINT_PT_PT_DISTANCE;
176 } else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
177 myType = isCoincidence ? CONSTRAINT_PT_ON_CURVE : CONSTRAINT_PT_LINE_DISTANCE;
179 theAttributes.clear();
181 if (myType == CONSTRAINT_HORIZONTAL_DISTANCE || myType == CONSTRAINT_VERTICAL_DISTANCE)
182 mySignValue = aValue->value() < 0.0 ? -1.0 : 1.0;
187 myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
189 myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateFeature::GROUP());
192 void SketchSolver_ConstraintDistance::adjustConstraint()
194 if (getType() == CONSTRAINT_PT_LINE_DISTANCE) {
195 bool isSigned = myBaseConstraint->boolean(SketchPlugin_ConstraintDistance::SIGNED())->value();
196 if (myIsSigned == isSigned) {
197 // adjust auxiliary point for sign-keeping
199 EntityWrapperPtr aDistPoint, aDistLine;
200 getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
201 adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
205 // Adjust point-line distance by setting/removing additional constraints
207 addConstraintsToKeepSign();
209 removeConstraintsKeepingSign();
211 myIsSigned = isSigned;
215 void SketchSolver_ConstraintDistance::update()
217 ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
218 myPrevValue = aConstraint->value();
220 bool isDistanceAlognDir =
221 myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID() ||
222 myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID();
224 AttributeDoublePtr aCurValue = myBaseConstraint->real(SketchPlugin_Constraint::VALUE());
225 bool isZeroSwitch = fabs(myPrevValue) < tolerance && fabs(aCurValue->value()) > tolerance;
226 bool isNonZeroSwitch = fabs(myPrevValue) > tolerance && fabs(aCurValue->value()) < tolerance;
228 if (!isDistanceAlognDir && (isZeroSwitch || isNonZeroSwitch)) {
229 // the value is changed from non-zero to zero or vice versa
233 // move entities to avoid conflicting constraints
236 // calculate the direction basing on the distanced objects
237 calculateDistanceDirection(myBaseConstraint, myStorage, aDirX, aDirY);
238 moveEntity(myBaseConstraint, myStorage, aDirX, aDirY);
241 removeConstraintsKeepingSign();
242 addConstraintsToKeepSign();
247 SketchSolver_Constraint::update();
248 if (isDistanceAlognDir && mySignValue * aConstraint->value() < 0.0) {
250 aConstraint->setValue(-aConstraint->value());
257 bool SketchSolver_ConstraintDistance::remove()
259 removeConstraintsKeepingSign();
260 return SketchSolver_Constraint::remove();
263 void SketchSolver_ConstraintDistance::addConstraintsToKeepSign()
265 std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
266 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
268 ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
269 std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
271 // calculate projection of the point on the line and find a sign of a distance
272 EntityWrapperPtr aDistPoint, aDistLine;
273 getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
275 std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(aDistLine);
276 std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(aDistPoint);
278 std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
279 std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
280 if (aLineVec->cross(aPtLineVec) >= 0.)
281 mySignValue = PI/2.0;
283 mySignValue = - PI/2.0;
285 // create auxiliary point on the line and set auxiliary constraints
286 myOddPoint = GCSPointPtr(new GCS::Point);
287 myOddPoint->x = aStorage->createParameter();
288 myOddPoint->y = aStorage->createParameter();
289 adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
291 PointWrapperPtr aPointWr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aDistPoint);
292 EdgeWrapperPtr anEdgeWr = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(aDistLine);
293 std::shared_ptr<GCS::Line> aGCSLine = std::dynamic_pointer_cast<GCS::Line>(anEdgeWr->entity());
295 GCSConstraintPtr aNewConstraint(new GCS::ConstraintPointOnLine(*myOddPoint, *aGCSLine));
296 aGCSConstraints.push_back(aNewConstraint);
297 // angle which keep orientation
298 aNewConstraint = GCSConstraintPtr(new GCS::ConstraintL2LAngle(
299 aGCSLine->p1, aGCSLine->p2, *myOddPoint, *(aPointWr->point()), &mySignValue));
300 aGCSConstraints.push_back(aNewConstraint);
302 aConstraint->setConstraints(aGCSConstraints);
304 aStorage->removeConstraint(myBaseConstraint);
305 aStorage->addConstraint(myBaseConstraint, aConstraint);
308 void SketchSolver_ConstraintDistance::removeConstraintsKeepingSign()
311 return; // no sign kept => nothing to remove
313 std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
314 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
316 ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
317 std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
319 aStorage->removeConstraint(myBaseConstraint);
321 // remove parameters related to auxiliary point
323 aParams.insert(myOddPoint->x);
324 aParams.insert(myOddPoint->y);
325 aStorage->removeParameters(aParams);
327 // remove constraints keeping sign of point-line distance,
328 // not more than 2 additional constraints is possible
329 if (!aGCSConstraints.empty())
330 aGCSConstraints.pop_back();
331 if (!aGCSConstraints.empty())
332 aGCSConstraints.pop_back();
333 aConstraint->setConstraints(aGCSConstraints);
334 aStorage->addConstraint(myBaseConstraint, aConstraint);
339 void SketchSolver_ConstraintDistance::notify(const FeaturePtr& theFeature,
340 PlaneGCSSolver_Update*)
342 if (getType() == CONSTRAINT_PT_LINE_DISTANCE && myIsSigned &&
343 theFeature->getKind() == SketchPlugin_Sketch::ID()) {
344 // the sketch plane was updated, recalculate auxiliary constraints
345 removeConstraintsKeepingSign();
346 addConstraintsToKeepSign();
347 myIsSigned = true; // reset it, due to changing by removeConstraintsKeepingSign()