Salome HOME
[PythonAPI / sketcher] added setTangent method and fixed a small bug
[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     // exchange the naming IDs of newly created and old line that become auxiliary
320     sketch()->exchangeIDs(aFeatureA, aNewFeatureA);
321     sketch()->exchangeIDs(aFeatureB, aNewFeatureB);
322   } else {
323     // Update radius value
324     int aNbSubs = sketch()->numberOfSubs();
325     FeaturePtr aSubFeature;
326     for (int aSub = 0; aSub < aNbSubs; aSub++) {
327       aSubFeature = sketch()->subFeature(aSub);
328       if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
329         continue;
330       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
331           aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
332       if (!aRefAttr || !aRefAttr->isObject())
333         continue;
334       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
335       if (aFeature == aNewArc) {
336         AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
337           aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
338         aRadius->setValue(aFilletRadius);
339         break;
340       }
341     }
342   }
343
344   // send events to update the sub-features by the solver
345   if (isUpdateFlushed)
346     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
347
348   // the viewer update should be unblocked in order after the fillet features
349   // are processed by the solver
350   //aMsg = std::shared_ptr<Events_Message>(
351   //              new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
352   //Events_Loop::loop()->send(aMsg);
353 }
354
355 void SketchPlugin_ConstraintFillet::attributeChanged(const std::string& theID)
356 {
357   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
358       theID == SketchPlugin_Constraint::ENTITY_B()) {
359     // clear the list of fillet entities
360     AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
361         data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
362     aRefListOfFillet->clear();
363
364     // remove all produced objects and constraints
365     DocumentPtr aDoc = sketch()->document();
366     std::list<FeaturePtr>::iterator aCIt = myProducedFeatures.begin();
367     for (; aCIt != myProducedFeatures.end(); ++aCIt)
368       aDoc->removeFeature(*aCIt);
369     myProducedFeatures.clear();
370
371     // clear auxiliary flag on initial objects
372     for (aCIt = myBaseObjects.begin(); aCIt != myBaseObjects.end(); ++aCIt)
373       (*aCIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
374     myBaseObjects.clear();
375   }
376 }
377
378 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
379 {
380   if (!sketch())
381     return thePrevious;
382
383   AISObjectPtr anAIS = thePrevious;
384   /// TODO: Equal constraint presentation should be put here
385   return anAIS;
386 }
387
388 bool SketchPlugin_ConstraintFillet::isMacro() const
389 {
390   return true;
391 }
392
393
394 // =========   Auxiliary functions   =================
395 void recalculateAttributes(FeaturePtr theNewArc,  const std::string& theNewArcAttribute,
396                            FeaturePtr theFeature, const std::string& theFeatureAttribute)
397 {
398   std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
399       theNewArc->attribute(theNewArcAttribute))->pnt();
400   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
401       theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
402 }
403
404 /// \brief Find intersections of lines shifted along normal direction
405 void possibleFilletCenterLineLine(
406     std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
407     std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
408     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
409 {
410   std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
411   std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
412   std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
413   double aDet = theDirA->cross(theDirB);
414   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
415     aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
416     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
417       aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
418       double aVX = aDirAT->xy()->dot(aPntA);
419       double aVY = aDirBT->xy()->dot(aPntB);
420       std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
421           (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
422           (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
423       theCenters.push_back(aPoint);
424     }
425   }
426 }
427
428 /// \brief Find intersections of line shifted along normal direction in both sides
429 ///        and a circle with extended radius
430 void possibleFilletCenterLineArc(
431     std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
432     std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
433     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
434 {
435   std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
436   std::shared_ptr<GeomAPI_XY> aPnt;
437   double aDirNorm2 = theDirLine->dot(theDirLine);
438   double aRad = 0.0;
439   double aDirX = theDirLine->x();
440   double aDirX2 = theDirLine->x() * theDirLine->x();
441   double aDirY2 = theDirLine->y() * theDirLine->y();
442   double aDirXY = theDirLine->x() * theDirLine->y();
443   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
444     aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
445     double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
446     double aCoeff2 = aCoeff * aCoeff;
447     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
448       aRad = theRadiusArc + aStepB * theRadius;
449       double aD = aRad * aRad * aDirNorm2 - aCoeff2;
450       if (aD < 0.0)
451         continue;
452       double aDs = sqrt(aD);
453       double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
454       double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
455       double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
456           aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
457       double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
458           aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
459
460       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
461       theCenters.push_back(aPoint1);
462       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
463       theCenters.push_back(aPoint2);
464     }
465   }
466 }
467
468 /// \brief Find intersections of two circles with extended radii
469 void possibleFilletCenterArcArc(
470     std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
471     std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
472     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
473 {
474   std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
475   double aCenterDist2 = aCenterDir->dot(aCenterDir);
476   double aCenterDist = sqrt(aCenterDist2);
477
478   double aRadA, aRadB;
479   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
480     aRadA = theRadiusA + aStepA * theRadius;
481     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
482       aRadB = theRadiusB + aStepB * theRadius;
483       if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
484         continue; // there is no intersections
485
486       double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
487       double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
488
489       double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
490       double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
491
492       double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
493       double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
494
495       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
496       theCenters.push_back(aPoint1);
497       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
498       theCenters.push_back(aPoint2);
499     }
500   }
501 }
502
503 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
504                            double theRadius, bool theNotInversed[2],
505                            std::shared_ptr<GeomAPI_XY>& theCenter,
506                            std::shared_ptr<GeomAPI_XY>& theTangentA,
507                            std::shared_ptr<GeomAPI_XY>& theTangentB)
508 {
509   static const int aNbFeatures = 2;
510   FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
511   std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
512   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
513
514   for (int i = 0; i < aNbFeatures; i++) {
515     if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
516       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
517           aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
518       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
519           aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
520     } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
521       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
522           aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
523       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
524           aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
525       aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
526           aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
527     } else
528       return;
529     aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
530         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
531         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
532     aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
533         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
534         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
535   }
536
537   if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
538       theFeatureB->getKind() == SketchPlugin_Line::ID()) {
539     std::shared_ptr<GeomAPI_Dir2d> aDir[2];
540     std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
541     for (int i = 0; i < aNbFeatures; i++) {
542       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
543       aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
544     }
545
546     // get and filter possible centers
547     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
548     possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
549     double aDot = 0.0;
550     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
551     for (; anIt != aSuspectCenters.end(); anIt++) {
552       aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
553       theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
554       if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
555         continue; // incorrect position
556       aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
557       theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
558       if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
559         continue; // incorrect position
560       // the center is found, stop searching
561       theCenter = *anIt;
562       return;
563     }
564   } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
565       theFeatureB->getKind() == SketchPlugin_Line::ID()) || 
566       (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
567       theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
568     int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
569     double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
570     std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
571         new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
572     std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
573         new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
574
575     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
576         new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
577     std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
578         new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
579     double anArcAngle = aEndArcDir->angle(aStartArcDir);
580
581     // get and filter possible centers
582     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
583     possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
584     double aDot = 0.0;
585     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
586     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
587     for (; anIt != aSuspectCenters.end(); anIt++) {
588       aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
589       aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
590       if (aLineTgPoint->decreased(aStart[aLineInd])->dot(aDirLine->xy()) < 0.0)
591         continue; // incorrect position
592       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
593           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
594       double aCurAngle = aCurDir->angle(aStartArcDir);
595       if (anArcAngle < 0.0) aCurAngle *= -1.0;
596       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
597         continue; // incorrect position
598       // the center is found, stop searching
599       theCenter = *anIt;
600       anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
601       if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
602         theTangentA = aLineTgPoint;
603         theTangentB = anArcTgPoint;
604       } else {
605         theTangentA = anArcTgPoint;
606         theTangentB = aLineTgPoint;
607       }
608       return;
609     }
610   } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
611       theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
612     double anArcRadius[aNbFeatures];
613     double anArcAngle[aNbFeatures];
614     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
615     for (int i = 0; i < aNbFeatures; i++) {
616       anArcRadius[i] = aStart[i]->distance(aCenter[i]);
617       aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
618           new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
619       std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
620           new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
621       anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
622     }
623
624     // get and filter possible centers
625     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
626     possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
627     double aDot = 0.0;
628     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
629     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
630     for (; anIt != aSuspectCenters.end(); anIt++) {
631       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
632           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
633       double aCurAngle = aCurDir->angle(aStartArcDir[0]);
634       if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
635       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
636         continue; // incorrect position
637       theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
638
639       aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
640       aCurAngle = aCurDir->angle(aStartArcDir[1]);
641       if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
642       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
643         continue; // incorrect position
644       theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
645
646       // the center is found, stop searching
647       theCenter = *anIt;
648       return;
649     }
650   }
651 }