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