Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_EdgeBuilder.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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_EdgeBuilder.h>
21
22 #include <GeomAPI_Ax2.h>
23 #include <GeomAPI_Ax3.h>
24 #include <GeomAPI_BSpline2d.h>
25 #include <GeomAPI_Ellipse.h>
26 #include <GeomAPI_Pnt2d.h>
27
28 #include <gp_Pln.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopoDS_Face.hxx>
32 #include <TopoDS.hxx>
33 #include <BRep_Tool.hxx>
34 #include <Geom2d_BSplineCurve.hxx>
35 #include <Geom_Plane.hxx>
36 #include <Geom_CylindricalSurface.hxx>
37 #include <Geom_RectangularTrimmedSurface.hxx>
38 #include <GeomLib.hxx>
39
40 #include <gp_Ax2.hxx>
41 #include <gp_Circ.hxx>
42 #include <gp_Elips.hxx>
43 #include <Bnd_Box.hxx>
44 #include <BRepBndLib.hxx>
45
46 static GeomEdgePtr createLine(const gp_Pnt& theStart, const gp_Pnt& theEnd)
47 {
48   GeomEdgePtr aRes;
49   if (!theStart.IsEqual(theEnd, Precision::Confusion()) &&
50     Abs(theStart.SquareDistance(theEnd)) < 1.e+100) {
51     BRepBuilderAPI_MakeEdge anEdgeBuilder(theStart, theEnd);
52     TopoDS_Edge anEdge = anEdgeBuilder.Edge();
53     aRes = GeomEdgePtr(new GeomAPI_Edge);
54     aRes->setImpl(new TopoDS_Shape(anEdge));
55   }
56   return aRes;
57 }
58
59 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
60     std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
61 {
62   const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
63   const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
64   return createLine(aStart, anEnd);
65 }
66 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
67     double theDX, double theDY, double theDZ)
68 {
69   const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
70   const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
71   return createLine(aStart, anEnd);
72 }
73
74 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
75     const std::shared_ptr<GeomAPI_Lin> theLin)
76 {
77   GeomEdgePtr aRes;
78   if (theLin.get()) {
79     const gp_Lin& aLin = theLin->impl<gp_Lin>();
80     BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
81     TopoDS_Edge anEdge = anEdgeBuilder.Edge();
82     aRes = GeomEdgePtr(new GeomAPI_Edge);
83     aRes->setImpl(new TopoDS_Shape(anEdge));
84   }
85   return aRes;
86 }
87
88 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
89     std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
90 {
91   std::shared_ptr<GeomAPI_Edge> aResult;
92   const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
93   if (aShape.IsNull())
94     return aResult;
95   TopoDS_Face aFace = TopoDS::Face(aShape);
96   if (aFace.IsNull())
97     return aResult;
98   TopLoc_Location aLoc;
99   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
100   if (aSurf.IsNull())
101     return aResult;
102   Handle(Geom_RectangularTrimmedSurface) aTrimmed =
103     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
104   if (!aTrimmed.IsNull())
105     aSurf = aTrimmed->BasisSurface();
106   Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
107   if (aCyl.IsNull())
108     return aResult;
109   gp_Ax1 anAxis = aCyl->Axis();
110   // compute the start and the end points of the resulting edge by the bounding box of the face
111   // (vertices projected to the axis) plus 10%
112   Bnd_Box aFaceBnd;
113   BRepBndLib::Add(aFace, aFaceBnd);
114   gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
115   bool isFirst = true;
116   double aParamMin = 0, aParamMax = 0;
117   for(int aX = 0; aX < 2; aX++) {
118     for(int aY = 0; aY < 2; aY++) {
119       for(int aZ = 0; aZ < 2; aZ++) {
120         gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
121           aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
122         gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
123         double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
124         if (isFirst) {
125           isFirst = false;
126           aParamMin = aProjParam;
127           aParamMax = aProjParam;
128         } else {
129           if (aParamMin > aProjParam)
130             aParamMin = aProjParam;
131           else if (aParamMax < aProjParam)
132             aParamMax = aProjParam;
133         }
134       }
135     }
136   }
137   // add 10%
138   double aDelta = aParamMax - aParamMin;
139   if (aDelta < 1.e-4) aDelta = 1.e-4;
140   aParamMin -= aDelta * 0.1;
141   aParamMax += aDelta * 0.1;
142
143   gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
144   aStart.Transform(aLoc.Transformation());
145   gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
146   anEnd.Transform(aLoc.Transformation());
147   /*
148   gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
149   // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
150   gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
151   anEnd.Transform(aLoc.Transformation());
152   */
153
154   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
155   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
156   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
157   // an axis is an infinite object
158   anEdge.Infinite(Standard_True);
159   aRes->setImpl(new TopoDS_Shape(anEdge));
160   return aRes;
161 }
162
163 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
164     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
165     double theRadius)
166 {
167   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
168   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
169
170   gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
171
172   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
173   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
174   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
175   aRes->setImpl(new TopoDS_Shape(anEdge));
176   return aRes;
177 }
178
179 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
180     std::shared_ptr<GeomAPI_Circ> theCircle)
181 {
182   GeomEdgePtr aRes;
183   if (theCircle.get()) {
184     const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
185     BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
186     TopoDS_Edge anEdge = anEdgeBuilder.Edge();
187     aRes = GeomEdgePtr(new GeomAPI_Edge);
188     aRes->setImpl(new TopoDS_Shape(anEdge));
189   }
190   return aRes;
191 }
192
193 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
194     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
195     std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
196 {
197   std::shared_ptr<GeomAPI_Edge> aRes;
198
199   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
200   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
201
202   /// OCCT creates an edge on a circle with empty radius, but visualization
203   /// is not able to process it
204   if (theCenter->isEqual(theStartPoint) || theCenter->isEqual(theEndPoint))
205     return aRes;
206
207   double aRadius = theCenter->distance(theStartPoint);
208   gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
209
210   const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
211   const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
212
213   // project end point to a circle
214   gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
215   gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
216
217   BRepBuilderAPI_MakeEdge anEdgeBuilder;
218   anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
219
220   anEdgeBuilder.Build();
221
222   if (anEdgeBuilder.IsDone()) {
223     aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
224     aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
225   }
226   return aRes;
227 }
228
229 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipse(
230     const std::shared_ptr<GeomAPI_Pnt>& theCenter,
231     const std::shared_ptr<GeomAPI_Dir>& theNormal,
232     const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
233     const double                        theMajorRadius,
234     const double                        theMinorRadius)
235 {
236   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
237   const gp_Dir& aNormal = theNormal->impl<gp_Dir>();
238   const gp_Dir& aMajorAxis = theMajorAxis->impl<gp_Dir>();
239
240   gp_Elips anEllipse(gp_Ax2(aCenter, aNormal, aMajorAxis), theMajorRadius, theMinorRadius);
241
242   BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse);
243   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
244   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
245   aRes->setImpl(new TopoDS_Shape(anEdge));
246   return aRes;
247 }
248
249 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipticArc(
250     const std::shared_ptr<GeomAPI_Pnt>& theCenter,
251     const std::shared_ptr<GeomAPI_Dir>& theNormal,
252     const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
253     const double                        theMajorRadius,
254     const double                        theMinorRadius,
255     const std::shared_ptr<GeomAPI_Pnt>& theStart,
256     const std::shared_ptr<GeomAPI_Pnt>& theEnd)
257 {
258   std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(theCenter, theNormal, theMajorAxis));
259   GeomAPI_Ellipse anEllipse(anAx2, theMajorRadius, theMinorRadius);
260
261   GeomPointPtr aStartPnt = anEllipse.project(theStart);
262   GeomPointPtr aEndPnt   = anEllipse.project(theEnd);
263
264   double aStartParam, aEndParam;
265   anEllipse.parameter(aStartPnt, Precision::Confusion(), aStartParam);
266   anEllipse.parameter(aEndPnt, Precision::Confusion(), aEndParam);
267
268   BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse.impl<gp_Elips>(), aStartParam, aEndParam);
269   GeomEdgePtr aRes(new GeomAPI_Edge);
270   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
271   aRes->setImpl(new TopoDS_Shape(anEdge));
272   return aRes;
273 }
274
275 GeomEdgePtr GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(
276     const std::shared_ptr<GeomAPI_Ax3>& thePlane,
277     const std::list<GeomPnt2dPtr>& thePoles,
278     const std::list<double>& theWeights,
279     const std::list<double>& theKnots,
280     const std::list<int>& theMults,
281     const int theDegree,
282     const bool thePeriodic)
283 {
284   std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve(
285       new GeomAPI_BSpline2d(theDegree, thePoles, theWeights, theKnots, theMults, thePeriodic));
286   return bsplineOnPlane(thePlane, aBSplineCurve);
287 }
288
289 GeomEdgePtr GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(
290     const std::shared_ptr<GeomAPI_Ax3>& thePlane,
291     const std::shared_ptr<GeomAPI_BSpline2d>& theCurve)
292 {
293   Handle(Geom_Curve) aCurve3D = GeomLib::To3d(thePlane->impl<gp_Ax3>().Ax2(),
294                                               theCurve->impl<Handle_Geom2d_BSplineCurve>());
295
296   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCurve3D);
297   GeomEdgePtr aRes(new GeomAPI_Edge);
298   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
299   aRes->setImpl(new TopoDS_Shape(anEdge));
300   return aRes;
301 }