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