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