1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <GeomAlgoAPI_EdgeBuilder.h>
22 #include <GeomAPI_Ax2.h>
23 #include <GeomAPI_Ellipse.h>
26 #include <BRepBuilderAPI_MakeEdge.hxx>
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Face.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>
37 #include <gp_Circ.hxx>
38 #include <gp_Elips.hxx>
39 #include <Bnd_Box.hxx>
40 #include <BRepBndLib.hxx>
42 static GeomEdgePtr createLine(const gp_Pnt& theStart, const gp_Pnt& theEnd)
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));
55 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
56 std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
58 const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
59 const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
60 return createLine(aStart, anEnd);
62 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
63 double theDX, double theDY, double theDZ)
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);
70 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
71 const std::shared_ptr<GeomAPI_Lin> theLin)
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));
84 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
85 std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
87 std::shared_ptr<GeomAPI_Edge> aResult;
88 const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
91 TopoDS_Face aFace = TopoDS::Face(aShape);
95 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
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);
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%
109 BRepBndLib::Add(aFace, aFaceBnd);
110 gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
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());
122 aParamMin = aProjParam;
123 aParamMax = aProjParam;
125 if (aParamMin > aProjParam)
126 aParamMin = aProjParam;
127 else if (aParamMax < aProjParam)
128 aParamMax = aProjParam;
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;
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());
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());
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));
159 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
160 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
163 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
164 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
166 gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
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));
175 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
176 std::shared_ptr<GeomAPI_Circ> theCircle)
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));
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)
193 std::shared_ptr<GeomAPI_Edge> aRes;
195 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
196 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
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))
203 double aRadius = theCenter->distance(theStartPoint);
204 gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
206 const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
207 const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
209 // project end point to a circle
210 gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
211 gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
213 BRepBuilderAPI_MakeEdge anEdgeBuilder;
214 anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
216 anEdgeBuilder.Build();
218 if (anEdgeBuilder.IsDone()) {
219 aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
220 aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
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)
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>();
236 gp_Elips anEllipse(gp_Ax2(aCenter, aNormal, aMajorAxis), theMajorRadius, theMinorRadius);
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));
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)
254 std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(theCenter, theNormal, theMajorAxis));
255 GeomAPI_Ellipse anEllipse(anAx2, theMajorRadius, theMinorRadius);
257 GeomPointPtr aStartPnt = anEllipse.project(theStart);
258 GeomPointPtr aEndPnt = anEllipse.project(theEnd);
260 double aStartParam, aEndParam;
261 anEllipse.parameter(aStartPnt, Precision::Confusion(), aStartParam);
262 anEllipse.parameter(aEndPnt, Precision::Confusion(), aEndParam);
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));
271 GeomEdgePtr GeomAlgoAPI_EdgeBuilder::bspline(const std::vector<GeomPointPtr>& thePoles,
272 const std::vector<double>& theWeights,
273 const bool thePeriodic)
276 if ((int)thePoles.size() <= aDegree)
277 aDegree = (int)thePoles.size() - 1;
279 return GeomEdgePtr();
281 int aNbKnots = (int)thePoles.size() - aDegree + 1;
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);
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()));
294 for (std::vector<double>::const_iterator aWIt = theWeights.begin();
295 aWIt != theWeights.end(); ++aWIt, ++anIndex)
296 aWeights.SetValue(anIndex, *aWIt);
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;
305 aMults.SetValue(anIndex, aDegree + 1);
306 for (++anIndex; anIndex < aNbKnots; ++anIndex)
307 aMults.SetValue(anIndex, 1);
308 aMults.SetValue(aNbKnots, aDegree + 1);
310 Handle(Geom_BSplineCurve) aCurve =
311 new Geom_BSplineCurve(aPoles, aWeights, aKnots, aMults, aDegree, thePeriodic);
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));