Salome HOME
232a0d690d894f03d646a36bc8212a881b5b2931
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Circ2dBuilder.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAlgoAPI_Circ2dBuilder.h>
21 #include <GeomAPI_Ax3.h>
22 #include <GeomAPI_Circ2d.h>
23 #include <GeomAPI_Pnt2d.h>
24 #include <GeomAPI_Dir2d.h>
25 #include <GeomAPI_Shape.h>
26
27 #include <BRep_Tool.hxx>
28 #include <ElCLib.hxx>
29 #include <GccAna_Circ2d2TanRad.hxx>
30 #include <GccAna_Circ2d3Tan.hxx>
31 #include <GccAna_Circ2dTanCen.hxx>
32 #include <GccEnt.hxx>
33 #include <GccEnt_QualifiedCirc.hxx>
34 #include <GccEnt_QualifiedLin.hxx>
35 #include <Geom2dAdaptor_Curve.hxx>
36 #include <Geom_Plane.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Edge.hxx>
39
40 #include <cmath>
41
42 typedef std::shared_ptr<gp_Circ2d> Circ2dPtr;
43 typedef std::shared_ptr<Geom2dAdaptor_Curve> CurveAdaptorPtr;
44 typedef std::vector< std::shared_ptr<GccEnt_QualifiedCirc> > VectorOfGccCirc;
45 typedef std::vector< std::shared_ptr<GccEnt_QualifiedLin> >  VectorOfGccLine;
46
47
48 // Provide different mechanisms to create circle:
49 // * by passing points
50 // * by tangent edges
51 // * with specified radius
52 // * etc.
53 class CircleBuilder
54 {
55 public:
56   CircleBuilder(const std::shared_ptr<GeomAPI_Ax3>& theBasePlane)
57     : myPlane(new Geom_Plane(theBasePlane->impl<gp_Ax3>())),
58       myRadius(0.0)
59   {}
60
61   void setRadius(const double theRadius)
62   { myRadius = theRadius; }
63
64   void setCenter(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter)
65   { myCenter = theCenter; }
66
67   void setTangentCurves(const std::vector< std::shared_ptr<GeomAPI_Shape> >& theEdges)
68   {
69     std::vector< std::shared_ptr<GeomAPI_Shape> >::const_iterator anEdgeIt;
70     for (anEdgeIt = theEdges.begin(); anEdgeIt != theEdges.end(); ++anEdgeIt) {
71       const TopoDS_Edge& anEdge = TopoDS::Edge((*anEdgeIt)->impl<TopoDS_Shape>());
72
73       double aFirst, aLast;
74       TopLoc_Location aLoc;
75       Handle(Geom2d_Curve) aCurve = BRep_Tool::CurveOnSurface(anEdge, myPlane, aLoc, aFirst, aLast);
76       CurveAdaptorPtr aCurveAdaptor(new Geom2dAdaptor_Curve(aCurve, aFirst, aLast));
77
78       // sort curves (circles should become first)
79       if (aCurveAdaptor->GetType() == GeomAbs_Circle)
80         myTangentShapes.insert(myTangentShapes.begin(), aCurveAdaptor);
81       else
82         myTangentShapes.push_back(aCurveAdaptor);
83     }
84   }
85
86   void setPassingPoints(const std::vector< std::shared_ptr<GeomAPI_Pnt2d> >& thePoints)
87   {
88     std::vector< std::shared_ptr<GeomAPI_Pnt2d> >::const_iterator aPIt;
89     for (aPIt = thePoints.begin(); aPIt != thePoints.end(); ++aPIt)
90       myPassingPoints.push_back((*aPIt)->impl<gp_Pnt2d>());
91   }
92
93   void setClosestPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
94   { myClosestPoint = thePoint; }
95
96
97   Circ2dPtr circle()
98   {
99     Circ2dPtr aResult;
100     if (myCenter) {
101       if (myPassingPoints.size() == 1)
102         aResult = circleByCenterAndPassingPoint();
103       else if (myTangentShapes.size() == 1)
104         aResult = circleByCenterAndTangent();
105       else if (myRadius > 0.0)
106         aResult = circleByCenterAndRadius();
107     } else if (myRadius > 0.0) {
108       if (myTangentShapes.size() == 2)
109         aResult = circleByRadiusAndTwoTangentCurves();
110     } else {
111       switch (myPassingPoints.size()) {
112       case 0:
113         aResult = circleByThreeTangentCurves();
114         break;
115       case 1:
116         aResult = circleByPointAndTwoTangentCurves();
117         break;
118       case 2:
119         aResult = circleByTwoPointsAndTangentCurve();
120         break;
121       case 3:
122         aResult = circleByThreePassingPoints();
123         break;
124       default:
125         break;
126       }
127     }
128     return aResult;
129   }
130
131 private:
132   Circ2dPtr circleByCenterAndRadius()
133   {
134     const gp_Pnt2d& aCenter = myCenter->impl<gp_Pnt2d>();
135     return Circ2dPtr(new gp_Circ2d(gp_Ax2d(aCenter, gp::DX2d()), myRadius));
136   }
137
138   Circ2dPtr circleByCenterAndPassingPoint()
139   {
140     const gp_Pnt2d& aCenter = myCenter->impl<gp_Pnt2d>();
141     if (aCenter.SquareDistance(myPassingPoints[0]) > Precision::SquareConfusion()) {
142       GccAna_Circ2dTanCen aBuilder(myPassingPoints[0], aCenter);
143       if (aBuilder.NbSolutions() > 0)
144         return Circ2dPtr(new gp_Circ2d(aBuilder.ThisSolution(1)));
145     }
146     return Circ2dPtr();
147   }
148
149   Circ2dPtr circleByCenterAndTangent()
150   {
151     const gp_Pnt2d& aCenter = myCenter->impl<gp_Pnt2d>();
152     CurveAdaptorPtr aCurve = myTangentShapes[0];
153
154     std::shared_ptr<GccAna_Circ2dTanCen> aCircleBuilder;
155     if (aCurve->GetType() == GeomAbs_Line &&
156         aCurve->Line().Distance(aCenter) > Precision::Confusion()) {
157       aCircleBuilder = std::shared_ptr<GccAna_Circ2dTanCen>(
158           new GccAna_Circ2dTanCen(aCurve->Line(), aCenter));
159     } else if (aCurve->GetType() == GeomAbs_Circle) {
160       aCircleBuilder = std::shared_ptr<GccAna_Circ2dTanCen>(new GccAna_Circ2dTanCen(
161           GccEnt::Unqualified(aCurve->Circle()), aCenter, Precision::Confusion()));
162     }
163
164     return getProperCircle(aCircleBuilder);
165   }
166
167   Circ2dPtr getProperCircle(const std::shared_ptr<GccAna_Circ2dTanCen>& theBuilder) const
168   {
169     if (!theBuilder)
170       return Circ2dPtr();
171
172     CurveAdaptorPtr aCurve = myTangentShapes[0];
173
174     int aNbSol = theBuilder->NbSolutions();
175     if (aNbSol == 0)
176       return Circ2dPtr();
177
178     int anAppropriateSolution = 1;
179     double aMinDist = Precision::Infinite();
180
181     double aParSol, aPonTgCurve;
182     gp_Pnt2d aTgPnt;
183     for (int i = 1; i <= aNbSol && aNbSol > 1 && aCurve; ++i) {
184       theBuilder->Tangency1(i, aParSol, aPonTgCurve, aTgPnt);
185       if (isParamOnCurve(aPonTgCurve, aCurve)) {
186         double aDist = distanceToClosestPoint(theBuilder->ThisSolution(i));
187         if (aDist < aMinDist) {
188           anAppropriateSolution = i;
189           aMinDist = aDist;
190         }
191       }
192     }
193
194     return Circ2dPtr(new gp_Circ2d(theBuilder->ThisSolution(anAppropriateSolution)));
195   }
196
197   double distanceToClosestPoint(const gp_Circ2d& theCirc) const
198   {
199     if (myClosestPoint) {
200       double aDist = myClosestPoint->impl<gp_Pnt2d>().Distance(theCirc.Location());
201       return fabs(aDist - theCirc.Radius());
202     }
203     return 0.0;
204   }
205
206
207   Circ2dPtr circleByThreeTangentCurves()
208   {
209     VectorOfGccCirc aTgCirc;
210     VectorOfGccLine aTgLine;
211     convertTangentCurvesToGccEnt(aTgCirc, aTgLine);
212
213     std::shared_ptr<GccAna_Circ2d3Tan> aCircleBuilder;
214     if (aTgCirc.size() + aTgLine.size() == 3) {
215       switch (aTgLine.size()) {
216       case 0:
217         aCircleBuilder = std::shared_ptr<GccAna_Circ2d3Tan>(new GccAna_Circ2d3Tan(
218             *aTgCirc[0], *aTgCirc[1], *aTgCirc[2], Precision::Confusion()));
219         break;
220       case 1:
221         aCircleBuilder = std::shared_ptr<GccAna_Circ2d3Tan>(new GccAna_Circ2d3Tan(
222             *aTgCirc[0], *aTgCirc[1], *aTgLine[0], Precision::Confusion()));
223         break;
224       case 2:
225         aCircleBuilder = std::shared_ptr<GccAna_Circ2d3Tan>(new GccAna_Circ2d3Tan(
226             *aTgCirc[0], *aTgLine[0], *aTgLine[1], Precision::Confusion()));
227         break;
228       case 3:
229         aCircleBuilder = std::shared_ptr<GccAna_Circ2d3Tan>(new GccAna_Circ2d3Tan(
230             *aTgLine[0], *aTgLine[1], *aTgLine[2], Precision::Confusion()));
231         break;
232       default:
233         break;
234       }
235     }
236
237     return getProperCircle(aCircleBuilder);
238   }
239
240   Circ2dPtr circleByPointAndTwoTangentCurves()
241   {
242     const gp_Pnt2d& aPoint = myPassingPoints[0];
243
244     VectorOfGccCirc aTgCirc;
245     VectorOfGccLine aTgLine;
246     convertTangentCurvesToGccEnt(aTgCirc, aTgLine);
247
248     std::shared_ptr<GccAna_Circ2d3Tan> aCircleBuilder;
249     if (aTgCirc.size() + aTgLine.size() == 2) {
250       switch (aTgLine.size()) {
251       case 0:
252         aCircleBuilder = std::shared_ptr<GccAna_Circ2d3Tan>(new GccAna_Circ2d3Tan(
253             *aTgCirc[0], *aTgCirc[1], aPoint, Precision::Confusion()));
254         break;
255       case 1:
256         aCircleBuilder = std::shared_ptr<GccAna_Circ2d3Tan>(new GccAna_Circ2d3Tan(
257             *aTgCirc[0], *aTgLine[0], aPoint, Precision::Confusion()));
258         break;
259       case 2:
260         aCircleBuilder = std::shared_ptr<GccAna_Circ2d3Tan>(new GccAna_Circ2d3Tan(
261             *aTgLine[0], *aTgLine[1], aPoint, Precision::Confusion()));
262         break;
263       default:
264         break;
265       }
266     }
267
268     return getProperCircle(aCircleBuilder);
269   }
270
271   Circ2dPtr circleByTwoPointsAndTangentCurve()
272   {
273     const gp_Pnt2d& aPoint1 = myPassingPoints[0];
274     const gp_Pnt2d& aPoint2 = myPassingPoints[1];
275     CurveAdaptorPtr aCurve = myTangentShapes[0];
276
277     std::shared_ptr<GccAna_Circ2d3Tan> aCircleBuilder;
278     if (aCurve) {
279       if (aCurve->GetType() == GeomAbs_Line) {
280         aCircleBuilder = std::shared_ptr<GccAna_Circ2d3Tan>(new GccAna_Circ2d3Tan(
281             GccEnt::Unqualified(aCurve->Line()), aPoint1, aPoint2, Precision::Confusion()));
282       }
283       else if (aCurve->GetType() == GeomAbs_Circle) {
284         aCircleBuilder = std::shared_ptr<GccAna_Circ2d3Tan>(new GccAna_Circ2d3Tan(
285             GccEnt::Unqualified(aCurve->Circle()), aPoint1, aPoint2, Precision::Confusion()));
286       }
287     }
288
289     return getProperCircle(aCircleBuilder);
290   }
291
292   Circ2dPtr circleByThreePassingPoints()
293   {
294     GccAna_Circ2d3Tan aCircleBuilder(myPassingPoints[0],
295                                      myPassingPoints[1],
296                                      myPassingPoints[2],
297                                      Precision::Confusion());
298     if (aCircleBuilder.NbSolutions() > 0)
299       return Circ2dPtr(new gp_Circ2d(aCircleBuilder.ThisSolution(1)));
300     return Circ2dPtr();
301   }
302
303   Circ2dPtr getProperCircle(const std::shared_ptr<GccAna_Circ2d3Tan>& theBuilder) const
304   {
305     if (!theBuilder)
306       return Circ2dPtr();
307
308     int aNbSol = theBuilder->NbSolutions();
309     if (aNbSol == 0)
310       return Circ2dPtr();
311
312     int anAppropriateSolution = 1;
313     double aMinDist = Precision::Infinite();
314
315     double aParSol, aPonTgCurve;
316     gp_Pnt2d aTgPnt;
317     for (int i = 1; i <= aNbSol && aNbSol > 1; ++i) {
318       bool isApplicable = false;
319       if (myTangentShapes.size() >= 1) {
320         theBuilder->Tangency1(i, aParSol, aPonTgCurve, aTgPnt);
321         isApplicable = isParamOnCurve(aPonTgCurve, myTangentShapes[0]);
322       }
323       if (myTangentShapes.size() >= 2 && isApplicable) {
324         theBuilder->Tangency2(i, aParSol, aPonTgCurve, aTgPnt);
325         isApplicable = isParamOnCurve(aPonTgCurve, myTangentShapes[1]);
326       }
327       if (myTangentShapes.size() >= 3 && isApplicable) {
328         theBuilder->Tangency3(i, aParSol, aPonTgCurve, aTgPnt);
329         isApplicable = isParamOnCurve(aPonTgCurve, myTangentShapes[2]);
330       }
331
332       if (isApplicable) {
333         double aDist = distanceToClosestPoint(theBuilder->ThisSolution(i));
334         if (aDist < aMinDist) {
335           anAppropriateSolution = i;
336           aMinDist = aDist;
337         }
338       }
339     }
340
341     return Circ2dPtr(new gp_Circ2d(theBuilder->ThisSolution(anAppropriateSolution)));
342   }
343
344
345   Circ2dPtr circleByRadiusAndTwoTangentCurves()
346   {
347     VectorOfGccCirc aTgCirc;
348     VectorOfGccLine aTgLine;
349     convertTangentCurvesToGccEnt(aTgCirc, aTgLine);
350
351     std::shared_ptr<GccAna_Circ2d2TanRad> aCircleBuilder;
352     if (aTgCirc.size() + aTgLine.size() == 2) {
353       switch (aTgLine.size()) {
354       case 0:
355         aCircleBuilder = std::shared_ptr<GccAna_Circ2d2TanRad>(new GccAna_Circ2d2TanRad(
356             *aTgCirc[0], *aTgCirc[1], myRadius, Precision::Confusion()));
357         break;
358       case 1:
359         aCircleBuilder = std::shared_ptr<GccAna_Circ2d2TanRad>(new GccAna_Circ2d2TanRad(
360             *aTgCirc[0], *aTgLine[0], myRadius, Precision::Confusion()));
361         break;
362       case 2:
363         aCircleBuilder = std::shared_ptr<GccAna_Circ2d2TanRad>(new GccAna_Circ2d2TanRad(
364             *aTgLine[0], *aTgLine[1], myRadius, Precision::Confusion()));
365         break;
366       default:
367         break;
368       }
369     }
370
371     return getProperCircle(aCircleBuilder);
372   }
373
374   Circ2dPtr getProperCircle(const std::shared_ptr<GccAna_Circ2d2TanRad>& theBuilder) const
375   {
376     if (!theBuilder)
377       return Circ2dPtr();
378
379     int aNbSol = theBuilder->NbSolutions();
380     if (aNbSol == 0)
381       return Circ2dPtr();
382
383     int anAppropriateSolution = 1;
384     double aMinDist = Precision::Infinite();
385
386     double aParSol, aPonTgCurve;
387     gp_Pnt2d aTgPnt;
388     for (int i = 1; i <= aNbSol && aNbSol > 1; ++i) {
389       bool isApplicable = false;
390       if (myTangentShapes.size() >= 1) {
391         theBuilder->Tangency1(i, aParSol, aPonTgCurve, aTgPnt);
392         isApplicable = isParamInCurve(aPonTgCurve, myTangentShapes[0]);
393       }
394       if (myTangentShapes.size() >= 2 && isApplicable) {
395         theBuilder->Tangency2(i, aParSol, aPonTgCurve, aTgPnt);
396         isApplicable = isParamInCurve(aPonTgCurve, myTangentShapes[1]);
397       }
398
399       if (isApplicable) {
400         double aDist = distanceToClosestPoint(theBuilder->ThisSolution(i));
401         if (aDist < aMinDist) {
402           anAppropriateSolution = i;
403           aMinDist = aDist;
404         }
405       }
406     }
407
408     return Circ2dPtr(new gp_Circ2d(theBuilder->ThisSolution(anAppropriateSolution)));
409   }
410
411
412   void convertTangentCurvesToGccEnt(VectorOfGccCirc& theTangentCircles,
413                                     VectorOfGccLine& theTangentLines)
414   {
415     theTangentCircles.reserve(3);
416     theTangentLines.reserve(3);
417
418     std::vector<CurveAdaptorPtr>::iterator anIt = myTangentShapes.begin();
419     for (; anIt != myTangentShapes.end(); ++anIt) {
420       switch ((*anIt)->GetType()) {
421       case GeomAbs_Line:
422         theTangentLines.push_back(
423             std::shared_ptr<GccEnt_QualifiedLin>(
424             new GccEnt_QualifiedLin((*anIt)->Line(), GccEnt_unqualified))
425         );
426         break;
427       case GeomAbs_Circle:
428         theTangentCircles.push_back(
429             std::shared_ptr<GccEnt_QualifiedCirc>(
430             new GccEnt_QualifiedCirc((*anIt)->Circle(), GccEnt_unqualified))
431         );
432         break;
433       default:
434         break;
435       }
436     }
437   }
438
439
440   static void adjustPeriod(double& theParameter, const CurveAdaptorPtr& theCurve)
441   {
442     if (theCurve->Curve()->IsPeriodic()) {
443       theParameter = ElCLib::InPeriod(theParameter,
444                                       theCurve->FirstParameter(),
445                                       theCurve->FirstParameter() + theCurve->Period());
446     }
447   }
448
449   // boundary parameters of curve are NOT applied
450   static bool isParamInCurve(double& theParameter, const CurveAdaptorPtr& theCurve)
451   {
452     adjustPeriod(theParameter, theCurve);
453     return theParameter > theCurve->FirstParameter() &&
454            theParameter < theCurve->LastParameter();
455   }
456
457   // boundary parameters of curve are applied too
458   static bool isParamOnCurve(double& theParameter, const CurveAdaptorPtr& theCurve)
459   {
460     adjustPeriod(theParameter, theCurve);
461     return theParameter >= theCurve->FirstParameter() &&
462            theParameter <= theCurve->LastParameter();
463   }
464
465 private:
466   Handle(Geom_Plane) myPlane;
467   std::shared_ptr<GeomAPI_Pnt2d> myCenter;
468   std::vector<gp_Pnt2d> myPassingPoints;
469   std::vector<CurveAdaptorPtr> myTangentShapes;
470   double myRadius;
471   std::shared_ptr<GeomAPI_Pnt2d> myClosestPoint;
472 };
473
474
475
476
477
478 GeomAlgoAPI_Circ2dBuilder::GeomAlgoAPI_Circ2dBuilder(const std::shared_ptr<GeomAPI_Ax3>& thePlane)
479   : myPlane(thePlane),
480     myRadius(0.)
481 {
482 }
483
484 void GeomAlgoAPI_Circ2dBuilder::setRadius(const double theRadius)
485 {
486   myRadius = theRadius;
487 }
488
489 void GeomAlgoAPI_Circ2dBuilder::setCenter(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter)
490 {
491   myCenter = theCenter;
492 }
493
494 void GeomAlgoAPI_Circ2dBuilder::addTangentCurve(const std::shared_ptr<GeomAPI_Shape>& theEdge)
495 {
496   if (theEdge->isEdge())
497     myTangentShapes.push_back(theEdge);
498 }
499
500 void GeomAlgoAPI_Circ2dBuilder::addPassingPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
501 {
502   myPassingPoints.push_back(thePoint);
503 }
504
505 void GeomAlgoAPI_Circ2dBuilder::setClosestPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
506 {
507   myClosestPoint = thePoint;
508 }
509
510 std::shared_ptr<GeomAPI_Circ2d> GeomAlgoAPI_Circ2dBuilder::circle(
511     const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
512     const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint,
513     const std::shared_ptr<GeomAPI_Pnt2d>& theThirdPoint)
514 {
515   std::shared_ptr<GeomAPI_Ax3> aPlane(new GeomAPI_Ax3);
516
517   GeomAlgoAPI_Circ2dBuilder aBuilder(aPlane);
518   aBuilder.addPassingPoint(theFirstPoint);
519   aBuilder.addPassingPoint(theSecondPoint);
520   aBuilder.addPassingPoint(theThirdPoint);
521   return aBuilder.circle();
522 }
523
524 std::shared_ptr<GeomAPI_Circ2d> GeomAlgoAPI_Circ2dBuilder::circle()
525 {
526   CircleBuilder aCircleBuilder(myPlane);
527   aCircleBuilder.setCenter(myCenter);
528   aCircleBuilder.setTangentCurves(myTangentShapes);
529   aCircleBuilder.setPassingPoints(myPassingPoints);
530   aCircleBuilder.setClosestPoint(myClosestPoint);
531   aCircleBuilder.setRadius(myRadius);
532   Circ2dPtr aCirc2d = aCircleBuilder.circle();
533
534   std::shared_ptr<GeomAPI_Circ2d> aCircle;
535   if (aCirc2d) {
536     const gp_Pnt2d& aCenter = aCirc2d->Location();
537     const gp_Dir2d& aXAxis = aCirc2d->Position().XDirection();
538
539     std::shared_ptr<GeomAPI_Pnt2d> aCircleCenter(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
540     std::shared_ptr<GeomAPI_Dir2d> aCircleDir(new GeomAPI_Dir2d(aXAxis.X(), aXAxis.Y()));
541
542     aCircle = std::shared_ptr<GeomAPI_Circ2d>(
543         new GeomAPI_Circ2d(aCircleCenter, aCircleDir, aCirc2d->Radius()));
544   }
545   return aCircle;
546 }