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