Salome HOME
4e42c0a92adbf69ce6f31aa3dcc4a9ac8941b748
[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
22 #include <GeomAPI_Ax2.h>
23 #include <GeomAPI_Ellipse.h>
24
25 #include <gp_Pln.hxx>
26 #include <BRepBuilderAPI_MakeEdge.hxx>
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Face.hxx>
29 #include <TopoDS.hxx>
30 #include <BRep_Tool.hxx>
31 #include <Geom_BSplineCurve.hxx>
32 #include <Geom_Plane.hxx>
33 #include <Geom_CylindricalSurface.hxx>
34 #include <Geom_RectangularTrimmedSurface.hxx>
35
36 #include <gp_Ax2.hxx>
37 #include <gp_Circ.hxx>
38 #include <gp_Elips.hxx>
39 #include <Bnd_Box.hxx>
40 #include <BRepBndLib.hxx>
41
42 static GeomEdgePtr createLine(const gp_Pnt& theStart, const gp_Pnt& theEnd)
43 {
44   GeomEdgePtr aRes;
45   if (!theStart.IsEqual(theEnd, Precision::Confusion()) &&
46     Abs(theStart.SquareDistance(theEnd)) < 1.e+100) {
47     BRepBuilderAPI_MakeEdge anEdgeBuilder(theStart, theEnd);
48     TopoDS_Edge anEdge = anEdgeBuilder.Edge();
49     aRes = GeomEdgePtr(new GeomAPI_Edge);
50     aRes->setImpl(new TopoDS_Shape(anEdge));
51   }
52   return aRes;
53 }
54
55 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
56     std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
57 {
58   const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
59   const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
60   return createLine(aStart, anEnd);
61 }
62 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
63     double theDX, double theDY, double theDZ)
64 {
65   const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
66   const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
67   return createLine(aStart, anEnd);
68 }
69
70 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
71     const std::shared_ptr<GeomAPI_Lin> theLin)
72 {
73   GeomEdgePtr aRes;
74   if (theLin.get()) {
75     const gp_Lin& aLin = theLin->impl<gp_Lin>();
76     BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
77     TopoDS_Edge anEdge = anEdgeBuilder.Edge();
78     aRes = GeomEdgePtr(new GeomAPI_Edge);
79     aRes->setImpl(new TopoDS_Shape(anEdge));
80   }
81   return aRes;
82 }
83
84 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
85     std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
86 {
87   std::shared_ptr<GeomAPI_Edge> aResult;
88   const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
89   if (aShape.IsNull())
90     return aResult;
91   TopoDS_Face aFace = TopoDS::Face(aShape);
92   if (aFace.IsNull())
93     return aResult;
94   TopLoc_Location aLoc;
95   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
96   if (aSurf.IsNull())
97     return aResult;
98   Handle(Geom_RectangularTrimmedSurface) aTrimmed =
99     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
100   if (!aTrimmed.IsNull())
101     aSurf = aTrimmed->BasisSurface();
102   Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
103   if (aCyl.IsNull())
104     return aResult;
105   gp_Ax1 anAxis = aCyl->Axis();
106   // compute the start and the end points of the resulting edge by the bounding box of the face
107   // (vertices projected to the axis) plus 10%
108   Bnd_Box aFaceBnd;
109   BRepBndLib::Add(aFace, aFaceBnd);
110   gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
111   bool isFirst = true;
112   double aParamMin = 0, aParamMax = 0;
113   for(int aX = 0; aX < 2; aX++) {
114     for(int aY = 0; aY < 2; aY++) {
115       for(int aZ = 0; aZ < 2; aZ++) {
116         gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
117           aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
118         gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
119         double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
120         if (isFirst) {
121           isFirst = false;
122           aParamMin = aProjParam;
123           aParamMax = aProjParam;
124         } else {
125           if (aParamMin > aProjParam)
126             aParamMin = aProjParam;
127           else if (aParamMax < aProjParam)
128             aParamMax = aProjParam;
129         }
130       }
131     }
132   }
133   // add 10%
134   double aDelta = aParamMax - aParamMin;
135   if (aDelta < 1.e-4) aDelta = 1.e-4;
136   aParamMin -= aDelta * 0.1;
137   aParamMax += aDelta * 0.1;
138
139   gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
140   aStart.Transform(aLoc.Transformation());
141   gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
142   anEnd.Transform(aLoc.Transformation());
143   /*
144   gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
145   // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
146   gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
147   anEnd.Transform(aLoc.Transformation());
148   */
149
150   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
151   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
152   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
153   // an axis is an infinite object
154   anEdge.Infinite(Standard_True);
155   aRes->setImpl(new TopoDS_Shape(anEdge));
156   return aRes;
157 }
158
159 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
160     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
161     double theRadius)
162 {
163   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
164   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
165
166   gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
167
168   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
169   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
170   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
171   aRes->setImpl(new TopoDS_Shape(anEdge));
172   return aRes;
173 }
174
175 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
176     std::shared_ptr<GeomAPI_Circ> theCircle)
177 {
178   GeomEdgePtr aRes;
179   if (theCircle.get()) {
180     const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
181     BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
182     TopoDS_Edge anEdge = anEdgeBuilder.Edge();
183     aRes = GeomEdgePtr(new GeomAPI_Edge);
184     aRes->setImpl(new TopoDS_Shape(anEdge));
185   }
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) || theCenter->isEqual(theEndPoint))
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 }
224
225 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipse(
226     const std::shared_ptr<GeomAPI_Pnt>& theCenter,
227     const std::shared_ptr<GeomAPI_Dir>& theNormal,
228     const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
229     const double                        theMajorRadius,
230     const double                        theMinorRadius)
231 {
232   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
233   const gp_Dir& aNormal = theNormal->impl<gp_Dir>();
234   const gp_Dir& aMajorAxis = theMajorAxis->impl<gp_Dir>();
235
236   gp_Elips anEllipse(gp_Ax2(aCenter, aNormal, aMajorAxis), theMajorRadius, theMinorRadius);
237
238   BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse);
239   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
240   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
241   aRes->setImpl(new TopoDS_Shape(anEdge));
242   return aRes;
243 }
244
245 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipticArc(
246     const std::shared_ptr<GeomAPI_Pnt>& theCenter,
247     const std::shared_ptr<GeomAPI_Dir>& theNormal,
248     const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
249     const double                        theMajorRadius,
250     const double                        theMinorRadius,
251     const std::shared_ptr<GeomAPI_Pnt>& theStart,
252     const std::shared_ptr<GeomAPI_Pnt>& theEnd)
253 {
254   std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(theCenter, theNormal, theMajorAxis));
255   GeomAPI_Ellipse anEllipse(anAx2, theMajorRadius, theMinorRadius);
256
257   GeomPointPtr aStartPnt = anEllipse.project(theStart);
258   GeomPointPtr aEndPnt   = anEllipse.project(theEnd);
259
260   double aStartParam, aEndParam;
261   anEllipse.parameter(aStartPnt, Precision::Confusion(), aStartParam);
262   anEllipse.parameter(aEndPnt, Precision::Confusion(), aEndParam);
263
264   BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse.impl<gp_Elips>(), aStartParam, aEndParam);
265   GeomEdgePtr aRes(new GeomAPI_Edge);
266   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
267   aRes->setImpl(new TopoDS_Shape(anEdge));
268   return aRes;
269 }
270
271 GeomEdgePtr GeomAlgoAPI_EdgeBuilder::bspline(const std::vector<GeomPointPtr>& thePoles,
272                                              const std::vector<double>& theWeights,
273                                              const bool thePeriodic)
274 {
275   int aDegree = 3;
276   if ((int)thePoles.size() <= aDegree)
277     aDegree = (int)thePoles.size() - 1;
278   if (aDegree <= 0)
279     return GeomEdgePtr();
280
281   int aNbKnots = (int)thePoles.size() - aDegree + 1;
282
283   // collect arrays of poles, weights, knots and multiplicities
284   TColgp_Array1OfPnt aPoles(1, (int)thePoles.size());
285   TColStd_Array1OfReal aWeights(1, (int)theWeights.size());
286   TColStd_Array1OfReal aKnots(1, aNbKnots);
287   TColStd_Array1OfInteger aMults(1, aNbKnots);
288
289   int anIndex = 1;
290   for (std::vector<GeomPointPtr>::const_iterator aPIt = thePoles.begin();
291        aPIt != thePoles.end(); ++aPIt, ++anIndex)
292     aPoles.SetValue(anIndex, gp_Pnt((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z()));
293   anIndex = 1;
294   for (std::vector<double>::const_iterator aWIt = theWeights.begin();
295        aWIt != theWeights.end(); ++aWIt, ++anIndex)
296     aWeights.SetValue(anIndex, *aWIt);
297   anIndex = 1;
298   static const double aStartParam = 0.0;
299   static const double aEndParam = 1.0;
300   double aStep = aEndParam / (aNbKnots - 1);
301   for (double aKnot = aStartParam; anIndex < aNbKnots; ++anIndex, aKnot += aStep)
302     aKnots.SetValue(anIndex, aKnot);
303   aKnots.ChangeLast() = aEndParam;
304   anIndex = 1;
305   aMults.SetValue(anIndex, aDegree + 1);
306   for (++anIndex; anIndex < aNbKnots; ++anIndex)
307     aMults.SetValue(anIndex, 1);
308   aMults.SetValue(aNbKnots, aDegree + 1);
309
310   Handle(Geom_BSplineCurve) aCurve =
311       new Geom_BSplineCurve(aPoles, aWeights, aKnots, aMults, aDegree, thePeriodic);
312
313   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCurve, aStartParam, aEndParam);
314   GeomEdgePtr aRes(new GeomAPI_Edge);
315   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
316   aRes->setImpl(new TopoDS_Shape(anEdge));
317   return aRes;
318 }