Salome HOME
577e7851df89d6bdccb6893ab6bf183acb313eee
[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 email : webmaster.salome@opencascade.com<mailto: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 <Bnd_Box.hxx>
34 #include <BRepBndLib.hxx>
35
36 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
37     std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
38 {
39   const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
40   const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
41
42   if (aStart.IsEqual(anEnd, Precision::Confusion()))
43     return std::shared_ptr<GeomAPI_Edge>();
44   if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
45     return std::shared_ptr<GeomAPI_Edge>();
46   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
47   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
48   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
49   aRes->setImpl(new TopoDS_Shape(anEdge));
50   return aRes;
51 }
52 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
53     double theDX, double theDY, double theDZ)
54 {
55
56   const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
57   const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
58
59   if (aStart.IsEqual(anEnd, Precision::Confusion()))
60     return std::shared_ptr<GeomAPI_Edge>();
61   if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
62     return std::shared_ptr<GeomAPI_Edge>();
63   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
64   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
65   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
66   aRes->setImpl(new TopoDS_Shape(anEdge));
67   return aRes;
68 }
69
70 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
71     const std::shared_ptr<GeomAPI_Lin> theLin)
72 {
73   if(!theLin.get()) {
74     return std::shared_ptr<GeomAPI_Edge>();
75   }
76
77   const gp_Lin& aLin = theLin->impl<gp_Lin>();
78   BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
79   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge());
80   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
81   aRes->setImpl(new TopoDS_Shape(anEdge));
82   return aRes;
83 }
84
85 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
86     std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
87 {
88   std::shared_ptr<GeomAPI_Edge> aResult;
89   const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
90   if (aShape.IsNull())
91     return aResult;
92   TopoDS_Face aFace = TopoDS::Face(aShape);
93   if (aFace.IsNull())
94     return aResult;
95   TopLoc_Location aLoc;
96   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
97   if (aSurf.IsNull())
98     return aResult;
99   Handle(Geom_RectangularTrimmedSurface) aTrimmed =
100     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
101   if (!aTrimmed.IsNull())
102     aSurf = aTrimmed->BasisSurface();
103   Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
104   if (aCyl.IsNull())
105     return aResult;
106   gp_Ax1 anAxis = aCyl->Axis();
107   // compute the start and the end points of the resulting edge by the bounding box of the face
108   // (vertices projected to the axis) plus 10%
109   Bnd_Box aFaceBnd;
110   BRepBndLib::Add(aFace, aFaceBnd);
111   gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
112   bool isFirst = true;
113   double aParamMin = 0, aParamMax = 0;
114   for(int aX = 0; aX < 2; aX++) {
115     for(int aY = 0; aY < 2; aY++) {
116       for(int aZ = 0; aZ < 2; aZ++) {
117         gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
118           aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
119         gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
120         double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
121         if (isFirst) {
122           isFirst = false;
123           aParamMin = aProjParam;
124           aParamMax = aProjParam;
125         } else {
126           if (aParamMin > aProjParam)
127             aParamMin = aProjParam;
128           else if (aParamMax < aProjParam)
129             aParamMax = aProjParam;
130         }
131       }
132     }
133   }
134   // add 10%
135   double aDelta = aParamMax - aParamMin;
136   if (aDelta < 1.e-4) aDelta = 1.e-4;
137   aParamMin -= aDelta * 0.1;
138   aParamMax += aDelta * 0.1;
139
140   gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
141   gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
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   if(!theCircle.get()) {
178     return std::shared_ptr<GeomAPI_Edge>();
179   }
180
181   const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
182   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
183   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge());
184   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
185   aRes->setImpl(new TopoDS_Shape(anEdge));
186   return aRes;
187 }
188
189 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
190     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
191     std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
192 {
193   std::shared_ptr<GeomAPI_Edge> aRes;
194
195   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
196   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
197
198   /// OCCT creates an edge on a circle with empty radius, but visualization
199   /// is not able to process it
200   if (theCenter->isEqual(theStartPoint))
201     return aRes;
202
203   double aRadius = theCenter->distance(theStartPoint);
204   gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
205
206   const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
207   const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
208
209   // project end point to a circle
210   gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
211   gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
212
213   BRepBuilderAPI_MakeEdge anEdgeBuilder;
214   anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
215
216   anEdgeBuilder.Build();
217
218   if (anEdgeBuilder.IsDone()) {
219     aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
220     aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
221   }
222   return aRes;
223 }