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