]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[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   gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
136   /*
137   gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
138   // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
139   gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
140   anEnd.Transform(aLoc.Transformation());
141   */
142
143   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
144   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
145   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
146   // an axis is an infinite object
147   anEdge.Infinite(Standard_True);
148   aRes->setImpl(new TopoDS_Shape(anEdge));
149   return aRes;
150 }
151
152 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
153     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
154     double theRadius)
155 {
156   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
157   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
158
159   gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
160
161   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
162   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
163   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
164   aRes->setImpl(new TopoDS_Shape(anEdge));
165   return aRes;
166 }
167
168 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
169     std::shared_ptr<GeomAPI_Circ> theCircle)
170 {
171   GeomEdgePtr aRes;
172   if (theCircle.get()) {
173     const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
174     BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
175     TopoDS_Edge anEdge = anEdgeBuilder.Edge();
176     aRes = GeomEdgePtr(new GeomAPI_Edge);
177     aRes->setImpl(new TopoDS_Shape(anEdge));
178   }
179   return aRes;
180 }
181
182 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
183     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
184     std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
185 {
186   std::shared_ptr<GeomAPI_Edge> aRes;
187
188   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
189   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
190
191   /// OCCT creates an edge on a circle with empty radius, but visualization
192   /// is not able to process it
193   if (theCenter->isEqual(theStartPoint) || theCenter->isEqual(theEndPoint))
194     return aRes;
195
196   double aRadius = theCenter->distance(theStartPoint);
197   gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
198
199   const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
200   const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
201
202   // project end point to a circle
203   gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
204   gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
205
206   BRepBuilderAPI_MakeEdge anEdgeBuilder;
207   anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
208
209   anEdgeBuilder.Build();
210
211   if (anEdgeBuilder.IsDone()) {
212     aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
213     aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
214   }
215   return aRes;
216 }
217
218 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipse(
219     const std::shared_ptr<GeomAPI_Pnt>& theCenter,
220     const std::shared_ptr<GeomAPI_Dir>& theNormal,
221     const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
222     const double                        theMajorRadius,
223     const double                        theMinorRadius)
224 {
225   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
226   const gp_Dir& aNormal = theNormal->impl<gp_Dir>();
227   const gp_Dir& aMajorAxis = theMajorAxis->impl<gp_Dir>();
228
229   gp_Elips anEllipse(gp_Ax2(aCenter, aNormal, aMajorAxis), theMajorRadius, theMinorRadius);
230
231   BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse);
232   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
233   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
234   aRes->setImpl(new TopoDS_Shape(anEdge));
235   return aRes;
236 }