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