Salome HOME
Issue #901 - It is possible to define empty name for parameter
[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     myProducedFeatures.push_back(aNewFeatureA);
229     myProducedFeatures.push_back(aNewFeatureB);
230     myProducedFeatures.push_back(aNewArc);
231
232     // Create list of additional constraints:
233     // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
234     // 1.1. coincidence
235     FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
236     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
237         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
238     aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::START_ID()));
239     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
240         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
241     int aFeatInd = isReversed ? 1 : 0;
242     int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
243     aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
244     recalculateAttributes(aNewArc, SketchPlugin_Arc::START_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
245     aConstraint->execute();
246     myProducedFeatures.push_back(aConstraint);
247     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
248     // 1.2. coincidence
249     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
250     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
251         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
252     aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::END_ID()));
253     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
254         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
255     aFeatInd = isReversed ? 0 : 1;
256     anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
257     aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
258     recalculateAttributes(aNewArc, SketchPlugin_Arc::END_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
259     aConstraint->execute();
260     myProducedFeatures.push_back(aConstraint);
261     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
262     // 2. Fillet arc radius
263     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
264     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
265         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
266     aRefAttr->setObject(aNewArc->lastResult());
267     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
268         aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
269     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
270         aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
271         isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
272     aConstraint->execute();
273     myProducedFeatures.push_back(aConstraint);
274     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
275     // 3. Tangency of fillet arc and features
276     for (int i = 0; i < aNbFeatures; i++) {
277       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
278       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
279           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
280       aRefAttr->setObject(aNewArc->lastResult());
281       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
282           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
283       bool isArc = aNewFeature[i]->getKind() == SketchPlugin_Arc::ID();
284       aRefAttr->setObject(isArc ? aNewFeature[i]->lastResult() : aNewFeature[i]->firstResult());
285       aConstraint->execute();
286       myProducedFeatures.push_back(aConstraint);
287       ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
288     }
289     // 4. Coincidence of free boundaries of base and copied features
290     for (int i = 0; i < aNbFeatures; i++) {
291       anAttrInd = 2*i + (isStart[i] ? 1 : 0);
292       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
293       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
294           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
295       aRefAttr->setAttr(aFeature[i]->attribute(aFeatAttributes[anAttrInd]));
296       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
297           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
298       aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
299       myProducedFeatures.push_back(aConstraint);
300     }
301     // 5. Tangent points should be placed on the base features
302     for (int i = 0; i < aNbFeatures; i++) {
303       anAttrInd = 2*i + (isStart[i] ? 0 : 1);
304       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
305       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
306           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
307       aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
308       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
309           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
310       aRefAttr->setObject(aFeature[i]->lastResult());
311       myProducedFeatures.push_back(aConstraint);
312     }
313     // make base features auxiliary
314     aFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
315     aFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
316     myBaseObjects.clear();
317     myBaseObjects.push_back(aFeatureA);
318     myBaseObjects.push_back(aFeatureB);
319   } else {
320     // Update radius value
321     int aNbSubs = sketch()->numberOfSubs();
322     FeaturePtr aSubFeature;
323     for (int aSub = 0; aSub < aNbSubs; aSub++) {
324       aSubFeature = sketch()->subFeature(aSub);
325       if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
326         continue;
327       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
328           aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
329       if (!aRefAttr || !aRefAttr->isObject())
330         continue;
331       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
332       if (aFeature == aNewArc) {
333         AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
334           aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
335         aRadius->setValue(aFilletRadius);
336         break;
337       }
338     }
339   }
340
341   // send events to update the sub-features by the solver
342   if (isUpdateFlushed)
343     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
344
345   // the viewer update should be unblocked in order after the fillet features
346   // are processed by the solver
347   //aMsg = std::shared_ptr<Events_Message>(
348   //              new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
349   //Events_Loop::loop()->send(aMsg);
350 }
351
352 void SketchPlugin_ConstraintFillet::attributeChanged(const std::string& theID)
353 {
354   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
355       theID == SketchPlugin_Constraint::ENTITY_B()) {
356     // clear the list of fillet entities
357     AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
358         data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
359     aRefListOfFillet->clear();
360
361     // remove all produced objects and constraints
362     DocumentPtr aDoc = sketch()->document();
363     std::list<FeaturePtr>::iterator aCIt = myProducedFeatures.begin();
364     for (; aCIt != myProducedFeatures.end(); ++aCIt)
365       aDoc->removeFeature(*aCIt);
366     myProducedFeatures.clear();
367
368     // clear auxiliary flag on initial objects
369     for (aCIt = myBaseObjects.begin(); aCIt != myBaseObjects.end(); ++aCIt)
370       (*aCIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
371     myBaseObjects.clear();
372   }
373 }
374
375 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
376 {
377   if (!sketch())
378     return thePrevious;
379
380   AISObjectPtr anAIS = thePrevious;
381   /// TODO: Equal constraint presentation should be put here
382   return anAIS;
383 }
384
385 bool SketchPlugin_ConstraintFillet::isMacro() const
386 {
387   return true;
388 }
389
390
391 // =========   Auxiliary functions   =================
392 void recalculateAttributes(FeaturePtr theNewArc,  const std::string& theNewArcAttribute,
393                            FeaturePtr theFeature, const std::string& theFeatureAttribute)
394 {
395   std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
396       theNewArc->attribute(theNewArcAttribute))->pnt();
397   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
398       theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
399 }
400
401 /// \brief Find intersections of lines shifted along normal direction
402 void possibleFilletCenterLineLine(
403     std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
404     std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
405     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
406 {
407   std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
408   std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
409   std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
410   double aDet = theDirA->cross(theDirB);
411   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
412     aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
413     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
414       aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
415       double aVX = aDirAT->xy()->dot(aPntA);
416       double aVY = aDirBT->xy()->dot(aPntB);
417       std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
418           (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
419           (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
420       theCenters.push_back(aPoint);
421     }
422   }
423 }
424
425 /// \brief Find intersections of line shifted along normal direction in both sides
426 ///        and a circle with extended radius
427 void possibleFilletCenterLineArc(
428     std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
429     std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
430     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
431 {
432   std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
433   std::shared_ptr<GeomAPI_XY> aPnt;
434   double aDirNorm2 = theDirLine->dot(theDirLine);
435   double aRad = 0.0;
436   double aDirX = theDirLine->x();
437   double aDirX2 = theDirLine->x() * theDirLine->x();
438   double aDirY2 = theDirLine->y() * theDirLine->y();
439   double aDirXY = theDirLine->x() * theDirLine->y();
440   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
441     aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
442     double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
443     double aCoeff2 = aCoeff * aCoeff;
444     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
445       aRad = theRadiusArc + aStepB * theRadius;
446       double aD = aRad * aRad * aDirNorm2 - aCoeff2;
447       if (aD < 0.0)
448         continue;
449       double aDs = sqrt(aD);
450       double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
451       double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
452       double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
453           aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
454       double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
455           aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
456
457       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
458       theCenters.push_back(aPoint1);
459       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
460       theCenters.push_back(aPoint2);
461     }
462   }
463 }
464
465 /// \brief Find intersections of two circles with extended radii
466 void possibleFilletCenterArcArc(
467     std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
468     std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
469     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
470 {
471   std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
472   double aCenterDist2 = aCenterDir->dot(aCenterDir);
473   double aCenterDist = sqrt(aCenterDist2);
474
475   double aRadA, aRadB;
476   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
477     aRadA = theRadiusA + aStepA * theRadius;
478     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
479       aRadB = theRadiusB + aStepB * theRadius;
480       if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
481         continue; // there is no intersections
482
483       double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
484       double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
485
486       double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
487       double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
488
489       double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
490       double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
491
492       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
493       theCenters.push_back(aPoint1);
494       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
495       theCenters.push_back(aPoint2);
496     }
497   }
498 }
499
500 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
501                            double theRadius, bool theNotInversed[2],
502                            std::shared_ptr<GeomAPI_XY>& theCenter,
503                            std::shared_ptr<GeomAPI_XY>& theTangentA,
504                            std::shared_ptr<GeomAPI_XY>& theTangentB)
505 {
506   static const int aNbFeatures = 2;
507   FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
508   std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
509   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
510
511   for (int i = 0; i < aNbFeatures; i++) {
512     if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
513       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
514           aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
515       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
516           aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
517     } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
518       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
519           aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
520       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
521           aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
522       aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
523           aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
524     } else
525       return;
526     aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
527         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
528         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
529     aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
530         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
531         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
532   }
533
534   if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
535       theFeatureB->getKind() == SketchPlugin_Line::ID()) {
536     std::shared_ptr<GeomAPI_Dir2d> aDir[2];
537     std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
538     for (int i = 0; i < aNbFeatures; i++) {
539       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
540       aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
541     }
542
543     // get and filter possible centers
544     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
545     possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
546     double aDot = 0.0;
547     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
548     for (; anIt != aSuspectCenters.end(); anIt++) {
549       aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
550       theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
551       if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
552         continue; // incorrect position
553       aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
554       theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
555       if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
556         continue; // incorrect position
557       // the center is found, stop searching
558       theCenter = *anIt;
559       return;
560     }
561   } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
562       theFeatureB->getKind() == SketchPlugin_Line::ID()) || 
563       (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
564       theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
565     int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
566     double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
567     std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
568         new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
569     std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
570         new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
571
572     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
573         new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
574     std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
575         new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
576     double anArcAngle = aEndArcDir->angle(aStartArcDir);
577
578     // get and filter possible centers
579     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
580     possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
581     double aDot = 0.0;
582     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
583     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
584     for (; anIt != aSuspectCenters.end(); anIt++) {
585       aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
586       aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
587       if (aLineTgPoint->decreased(aStart[aLineInd])->dot(aDirLine->xy()) < 0.0)
588         continue; // incorrect position
589       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
590           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
591       double aCurAngle = aCurDir->angle(aStartArcDir);
592       if (anArcAngle < 0.0) aCurAngle *= -1.0;
593       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
594         continue; // incorrect position
595       // the center is found, stop searching
596       theCenter = *anIt;
597       anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
598       if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
599         theTangentA = aLineTgPoint;
600         theTangentB = anArcTgPoint;
601       } else {
602         theTangentA = anArcTgPoint;
603         theTangentB = aLineTgPoint;
604       }
605       return;
606     }
607   } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
608       theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
609     double anArcRadius[aNbFeatures];
610     double anArcAngle[aNbFeatures];
611     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
612     for (int i = 0; i < aNbFeatures; i++) {
613       anArcRadius[i] = aStart[i]->distance(aCenter[i]);
614       aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
615           new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
616       std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
617           new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
618       anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
619     }
620
621     // get and filter possible centers
622     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
623     possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
624     double aDot = 0.0;
625     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
626     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
627     for (; anIt != aSuspectCenters.end(); anIt++) {
628       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
629           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
630       double aCurAngle = aCurDir->angle(aStartArcDir[0]);
631       if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
632       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
633         continue; // incorrect position
634       theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
635
636       aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
637       aCurAngle = aCurDir->angle(aStartArcDir[1]);
638       if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
639       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
640         continue; // incorrect position
641       theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
642
643       // the center is found, stop searching
644       theCenter = *anIt;
645       return;
646     }
647   }
648 }