Salome HOME
addUniqueNamedCopiedFeature to use unique names for copies features.
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintFillet.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintFillet.cpp
4 // Created: 19 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintFillet.h"
8
9 #include <GeomAPI_Dir2d.h>
10 #include <GeomAPI_Pnt2d.h>
11 #include <GeomAPI_XY.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeRefAttr.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_ResultConstruction.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_Line.h>
24 #include <SketchPlugin_Sketch.h>
25 #include <SketchPlugin_ConstraintCoincidence.h>
26 #include <SketchPlugin_ConstraintTangent.h>
27 #include <SketchPlugin_ConstraintRadius.h>
28
29 #include <SketcherPrs_Factory.h>
30
31 #include <Config_PropManager.h>
32 #include <Events_Loop.h>
33
34 #include <math.h>
35
36 static const std::string PREVIOUS_VALUE("FilletPreviousRadius");
37
38 /// \brief Attract specified point on theNewArc to the attribute of theFeature
39 static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
40   FeaturePtr theFeature, const std::string& theFeatureAttribute);
41
42 /// \brief Calculates center of fillet arc and coordinates of tangency points
43 static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
44                                   double theRadius, bool theNotInversed[2],
45                                   std::shared_ptr<GeomAPI_XY>& theCenter,
46                                   std::shared_ptr<GeomAPI_XY>& theTangentA,
47                                   std::shared_ptr<GeomAPI_XY>& theTangentB);
48
49
50 SketchPlugin_ConstraintFillet::SketchPlugin_ConstraintFillet()
51 {
52 }
53
54 void SketchPlugin_ConstraintFillet::initAttributes()
55 {
56   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
57   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
58   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
59   data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
60   data()->addAttribute(PREVIOUS_VALUE, ModelAPI_AttributeDouble::typeId());
61   // initialize attribute not applicable for user
62   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
63   data()->attribute(PREVIOUS_VALUE)->setInitialized();
64   std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(PREVIOUS_VALUE))->setValue(0.0);
65 }
66
67 void SketchPlugin_ConstraintFillet::execute()
68 {
69   // the viewer update should be blocked in order to avoid the temporaty fillet sub-features visualization
70   // before they are processed by the solver
71   //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
72   //    new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
73   //Events_Loop::loop()->send(aMsg);
74
75   std::shared_ptr<ModelAPI_Data> aData = data();
76   ResultConstructionPtr aRC;
77   // Check the base objects are initialized
78   double aFilletRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
79       aData->attribute(SketchPlugin_Constraint::VALUE()))->value();
80   AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
81       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
82   AttributeRefAttrPtr aBaseB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
83       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
84   if (!aBaseA->isInitialized() || !aBaseB->isInitialized() ||
85       !aBaseA->isObject() || !aBaseB->isObject())
86     return;
87   // Check the fillet shapes is not initialized yet
88   AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
89       aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
90   bool needNewObjects = aRefListOfFillet->size() == 0;
91
92   // Obtain features for the base objects
93   FeaturePtr aFeatureA, aFeatureB;
94   aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseA->object());
95   if (aRC) aFeatureA = aRC->document()->feature(aRC);
96   aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseB->object());
97   if (aRC) aFeatureB = aRC->document()->feature(aRC);
98   if (!aFeatureA || !aFeatureB)
99     return;
100
101   FeaturePtr aNewFeatureA, aNewFeatureB, aNewArc;
102   if (needNewObjects) {
103     // Create list of objects composing a fillet
104     // copy aFeatureA
105     aNewFeatureA = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeatureA, sketch());
106     // copy aFeatureB
107     aNewFeatureB = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeatureB, sketch());
108     // create filleting arc (it will be attached to the list later)
109     aNewArc = sketch()->addFeature(SketchPlugin_Arc::ID());
110   } else {
111     // Obtain features from the list
112     std::list<ObjectPtr> aNewFeatList = aRefListOfFillet->list();
113     std::list<ObjectPtr>::iterator aFeatIt = aNewFeatList.begin();
114     aNewFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
115     aNewFeatureB = ModelAPI_Feature::feature(*aFeatIt++);
116     aNewArc = ModelAPI_Feature::feature(*aFeatIt);
117   }
118
119   // Calculate arc attributes
120   static const int aNbFeatures = 2;
121   FeaturePtr aFeature[aNbFeatures] = {aFeatureA, aFeatureB};
122   FeaturePtr aNewFeature[aNbFeatures] = {aNewFeatureA, aNewFeatureB};
123   std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
124   bool isStart[aNbFeatures]; // indicates which point the features share
125   std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair -  to second
126   std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
127   for (int i = 0; i < aNbFeatures; i++) {
128     std::string aStartAttr, aEndAttr;
129     if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
130       aStartAttr = SketchPlugin_Line::START_ID();
131       aEndAttr = SketchPlugin_Line::END_ID();
132     } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
133       aStartAttr = SketchPlugin_Arc::START_ID();
134       aEndAttr = SketchPlugin_Arc::END_ID();
135     } else { // wrong argument
136       aRefListOfFillet->remove(aNewFeatureA);
137       aRefListOfFillet->remove(aNewFeatureB);
138       aRefListOfFillet->remove(aNewArc);
139       return;
140     }
141     aFeatAttributes[2*i] = aStartAttr;
142     aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
143         aFeature[i]->attribute(aStartAttr))->pnt();
144     aFeatAttributes[2*i+1] = aEndAttr;
145     aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
146         aFeature[i]->attribute(aEndAttr))->pnt();
147   }
148   for (int i = 0; i < aNbFeatures; i++) {
149     int j = aNbFeatures;
150     for (; j < 2 * aNbFeatures; j++)
151       if (aStartEndPnt[i]->distance(aStartEndPnt[j]) < 1.e-10) {
152         isStart[0] = i==0;
153         isStart[1] = j==aNbFeatures;
154         break;
155       }
156     if (j < 2 * aNbFeatures)
157       break;
158   }
159   // tangent directions of the features
160   for (int i = 0; i < aNbFeatures; i++) {
161     std::shared_ptr<GeomAPI_XY> aDir;
162     if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
163       aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
164       if (!isStart[i])
165         aDir = aDir->multiplied(-1.0);
166     } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
167       std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
168           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
169           aNewFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
170       aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
171       aDir = aDir->decreased(aCenterPoint->xy());
172
173       double x = aDir->x();
174       double y = aDir->y();
175       aDir->setX(-y);
176       aDir->setY(x);
177       if (!isStart[i])
178         aDir = aDir->multiplied(-1.0);
179     }
180     aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
181   }
182
183   // Wait all constraints being created, then send update events
184   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
185   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
186   if (isUpdateFlushed)
187     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
188
189   // By default, the start point of fillet arc is connected to FeatureA,
190   // and the end point - to FeatureB. But when the angle between TangentDirA and
191   // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
192   double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
193   bool isReversed = cosBA > 0.0;
194
195   // Calculate fillet arc parameters
196   std::shared_ptr<GeomAPI_XY> aCenter, aTangentPntA, aTangentPntB;
197   calculateFilletCenter(aFeatureA, aFeatureB, aFilletRadius, isStart, aCenter, aTangentPntA, aTangentPntB);
198   // update features
199   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
200       aNewFeatureA->attribute(aFeatAttributes[isStart[0] ? 0 : 1]))->setValue(
201       aTangentPntA->x(), aTangentPntA->y());
202   aNewFeatureA->execute();
203   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204       aNewFeatureB->attribute(aFeatAttributes[2 + (isStart[1] ? 0 : 1)]))->setValue(
205       aTangentPntB->x(), aTangentPntB->y());
206   aNewFeatureB->execute();
207   // update fillet arc
208   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
209       aNewArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(
210       aCenter->x(), aCenter->y());
211   if (isReversed) {
212     std::shared_ptr<GeomAPI_XY> aTmp = aTangentPntA;
213     aTangentPntA = aTangentPntB;
214     aTangentPntB = aTmp;
215   }
216   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
217       aNewArc->attribute(SketchPlugin_Arc::START_ID()))->setValue(aTangentPntA->x(), aTangentPntA->y());
218   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
219       aNewArc->attribute(SketchPlugin_Arc::END_ID()))->setValue(aTangentPntB->x(), aTangentPntB->y());
220   aNewArc->execute();
221
222   if (needNewObjects) {
223     // attach new arc to the list
224     aRefListOfFillet->append(aNewFeatureA->lastResult());
225     aRefListOfFillet->append(aNewFeatureB->lastResult());
226     aRefListOfFillet->append(aNewArc->lastResult());
227
228     // Create list of additional constraints:
229     // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
230     // 1.1. coincidence
231     FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
232     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
233         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
234     aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::START_ID()));
235     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
236         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
237     int aFeatInd = isReversed ? 1 : 0;
238     int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
239     aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
240     recalculateAttributes(aNewArc, SketchPlugin_Arc::START_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
241     aConstraint->execute();
242     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
243     // 1.2. coincidence
244     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
245     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
246         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
247     aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::END_ID()));
248     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
249         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
250     aFeatInd = isReversed ? 0 : 1;
251     anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
252     aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
253     recalculateAttributes(aNewArc, SketchPlugin_Arc::END_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
254     aConstraint->execute();
255     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
256     // 2. Fillet arc radius
257     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
258     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
259         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
260     aRefAttr->setObject(aNewArc->lastResult());
261     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
262         aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
263     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
264         aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
265         isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
266     aConstraint->execute();
267     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
268     // 3. Tangency of fillet arc and features
269     for (int i = 0; i < aNbFeatures; i++) {
270       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
271       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
272           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
273       aRefAttr->setObject(aNewArc->lastResult());
274       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
275           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
276       bool isArc = aNewFeature[i]->getKind() == SketchPlugin_Arc::ID();
277       aRefAttr->setObject(isArc ? aNewFeature[i]->lastResult() : aNewFeature[i]->firstResult());
278       aConstraint->execute();
279       ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
280     }
281     // 4. Coincidence of free boundaries of base and copied features
282     for (int i = 0; i < aNbFeatures; i++) {
283       anAttrInd = 2*i + (isStart[i] ? 1 : 0);
284       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
285       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
286           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
287       aRefAttr->setAttr(aFeature[i]->attribute(aFeatAttributes[anAttrInd]));
288       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
289           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
290       aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
291     }
292     // 5. Tangent points should be placed on the base features
293     for (int i = 0; i < aNbFeatures; i++) {
294       anAttrInd = 2*i + (isStart[i] ? 0 : 1);
295       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
296       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
297           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
298       aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
299       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
300           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
301       aRefAttr->setObject(aFeature[i]->lastResult());
302     }
303     // make base features auxiliary
304     aFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
305     aFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
306   } else {
307     // Update radius value
308     int aNbSubs = sketch()->numberOfSubs();
309     FeaturePtr aSubFeature;
310     for (int aSub = 0; aSub < aNbSubs; aSub++) {
311       aSubFeature = sketch()->subFeature(aSub);
312       if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
313         continue;
314       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
315           aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
316       if (!aRefAttr || !aRefAttr->isObject())
317         continue;
318       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
319       if (aFeature == aNewArc) {
320         AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
321           aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
322         aRadius->setValue(aFilletRadius);
323         break;
324       }
325     }
326   }
327
328   // send events to update the sub-features by the solver
329   if (isUpdateFlushed)
330     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
331
332   // the viewer update should be unblocked in order after the fillet features
333   // are processed by the solver
334   //aMsg = std::shared_ptr<Events_Message>(
335   //              new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
336   //Events_Loop::loop()->send(aMsg);
337 }
338
339 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
340 {
341   if (!sketch())
342     return thePrevious;
343
344   AISObjectPtr anAIS = thePrevious;
345   /// TODO: Equal constraint presentation should be put here
346   return anAIS;
347 }
348
349 bool SketchPlugin_ConstraintFillet::isMacro() const
350 {
351   return true;
352 }
353
354
355 // =========   Auxiliary functions   =================
356 void recalculateAttributes(FeaturePtr theNewArc,  const std::string& theNewArcAttribute,
357                            FeaturePtr theFeature, const std::string& theFeatureAttribute)
358 {
359   std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
360       theNewArc->attribute(theNewArcAttribute))->pnt();
361   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
362       theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
363 }
364
365 /// \brief Find intersections of lines shifted along normal direction
366 void possibleFilletCenterLineLine(
367     std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
368     std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
369     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
370 {
371   std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
372   std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
373   std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
374   double aDet = theDirA->cross(theDirB);
375   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
376     aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
377     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
378       aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
379       double aVX = aDirAT->xy()->dot(aPntA);
380       double aVY = aDirBT->xy()->dot(aPntB);
381       std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
382           (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
383           (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
384       theCenters.push_back(aPoint);
385     }
386   }
387 }
388
389 /// \brief Find intersections of line shifted along normal direction in both sides
390 ///        and a circle with extended radius
391 void possibleFilletCenterLineArc(
392     std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
393     std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
394     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
395 {
396   std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
397   std::shared_ptr<GeomAPI_XY> aPnt;
398   double aDirNorm2 = theDirLine->dot(theDirLine);
399   double aRad = 0.0;
400   double aDirX = theDirLine->x();
401   double aDirX2 = theDirLine->x() * theDirLine->x();
402   double aDirY2 = theDirLine->y() * theDirLine->y();
403   double aDirXY = theDirLine->x() * theDirLine->y();
404   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
405     aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
406     double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
407     double aCoeff2 = aCoeff * aCoeff;
408     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
409       aRad = theRadiusArc + aStepB * theRadius;
410       double aD = aRad * aRad * aDirNorm2 - aCoeff2;
411       if (aD < 0.0)
412         continue;
413       double aDs = sqrt(aD);
414       double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
415       double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
416       double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
417           aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
418       double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
419           aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
420
421       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
422       theCenters.push_back(aPoint1);
423       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
424       theCenters.push_back(aPoint2);
425     }
426   }
427 }
428
429 /// \brief Find intersections of two circles with extended radii
430 void possibleFilletCenterArcArc(
431     std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
432     std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
433     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
434 {
435   std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
436   double aCenterDist2 = aCenterDir->dot(aCenterDir);
437   double aCenterDist = sqrt(aCenterDist2);
438
439   double aRadA, aRadB;
440   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
441     aRadA = theRadiusA + aStepA * theRadius;
442     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
443       aRadB = theRadiusB + aStepB * theRadius;
444       if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
445         continue; // there is no intersections
446
447       double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
448       double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
449
450       double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
451       double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
452
453       double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
454       double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
455
456       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
457       theCenters.push_back(aPoint1);
458       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
459       theCenters.push_back(aPoint2);
460     }
461   }
462 }
463
464 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
465                            double theRadius, bool theNotInversed[2],
466                            std::shared_ptr<GeomAPI_XY>& theCenter,
467                            std::shared_ptr<GeomAPI_XY>& theTangentA,
468                            std::shared_ptr<GeomAPI_XY>& theTangentB)
469 {
470   static const int aNbFeatures = 2;
471   FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
472   std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
473   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
474
475   for (int i = 0; i < aNbFeatures; i++) {
476     if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
477       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
478           aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
479       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
480           aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
481     } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
482       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
483           aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
484       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
485           aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
486       aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
487           aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
488     } else
489       return;
490     aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
491         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
492         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
493     aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
494         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
495         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
496   }
497
498   if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
499       theFeatureB->getKind() == SketchPlugin_Line::ID()) {
500     std::shared_ptr<GeomAPI_Dir2d> aDir[2];
501     std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
502     for (int i = 0; i < aNbFeatures; i++) {
503       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
504       aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
505     }
506
507     // get and filter possible centers
508     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
509     possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
510     double aDot = 0.0;
511     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
512     for (; anIt != aSuspectCenters.end(); anIt++) {
513       aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
514       theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
515       if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
516         continue; // incorrect position
517       aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
518       theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
519       if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
520         continue; // incorrect position
521       // the center is found, stop searching
522       theCenter = *anIt;
523       return;
524     }
525   } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
526       theFeatureB->getKind() == SketchPlugin_Line::ID()) || 
527       (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
528       theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
529     int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
530     double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
531     std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
532         new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
533     std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
534         new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
535
536     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
537         new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
538     std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
539         new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
540     double anArcAngle = aEndArcDir->angle(aStartArcDir);
541
542     // get and filter possible centers
543     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
544     possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
545     double aDot = 0.0;
546     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
547     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
548     for (; anIt != aSuspectCenters.end(); anIt++) {
549       aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
550       aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
551       if (aLineTgPoint->decreased(aStart[aLineInd])->dot(aDirLine->xy()) < 0.0)
552         continue; // incorrect position
553       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
554           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
555       double aCurAngle = aCurDir->angle(aStartArcDir);
556       if (anArcAngle < 0.0) aCurAngle *= -1.0;
557       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
558         continue; // incorrect position
559       // the center is found, stop searching
560       theCenter = *anIt;
561       anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
562       if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
563         theTangentA = aLineTgPoint;
564         theTangentB = anArcTgPoint;
565       } else {
566         theTangentA = anArcTgPoint;
567         theTangentB = aLineTgPoint;
568       }
569       return;
570     }
571   } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
572       theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
573     double anArcRadius[aNbFeatures];
574     double anArcAngle[aNbFeatures];
575     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
576     for (int i = 0; i < aNbFeatures; i++) {
577       anArcRadius[i] = aStart[i]->distance(aCenter[i]);
578       aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
579           new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
580       std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
581           new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
582       anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
583     }
584
585     // get and filter possible centers
586     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
587     possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
588     double aDot = 0.0;
589     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
590     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
591     for (; anIt != aSuspectCenters.end(); anIt++) {
592       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
593           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
594       double aCurAngle = aCurDir->angle(aStartArcDir[0]);
595       if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
596       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
597         continue; // incorrect position
598       theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
599
600       aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
601       aCurAngle = aCurDir->angle(aStartArcDir[1]);
602       if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
603       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
604         continue; // incorrect position
605       theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
606
607       // the center is found, stop searching
608       theCenter = *anIt;
609       return;
610     }
611   }
612 }