Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_EdgeBuilder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_EdgeBuilder.cpp
4 // Created:     23 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <GeomAlgoAPI_EdgeBuilder.h>
8 #include <gp_Pln.hxx>
9 #include <BRepBuilderAPI_MakeEdge.hxx>
10 #include <TopoDS_Edge.hxx>
11 #include <TopoDS_Face.hxx>
12 #include <TopoDS.hxx>
13 #include <BRep_Tool.hxx>
14 #include <Geom_Plane.hxx>
15 #include <Geom_CylindricalSurface.hxx>
16 #include <Geom_RectangularTrimmedSurface.hxx>
17
18 #include <gp_Ax2.hxx>
19 #include <gp_Circ.hxx>
20 #include <Bnd_Box.hxx>
21 #include <BRepBndLib.hxx>
22
23 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
24     std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
25 {
26   const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
27   const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
28
29   if (aStart.IsEqual(anEnd, Precision::Confusion()))
30     return std::shared_ptr<GeomAPI_Edge>();
31   if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
32     return std::shared_ptr<GeomAPI_Edge>();
33   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
34   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
35   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
36   aRes->setImpl(new TopoDS_Shape(anEdge));
37   return aRes;
38 }
39 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
40     double theDX, double theDY, double theDZ)
41 {
42
43   const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
44   const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
45
46   if (aStart.IsEqual(anEnd, Precision::Confusion()))
47     return std::shared_ptr<GeomAPI_Edge>();
48   if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
49     return std::shared_ptr<GeomAPI_Edge>();
50   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
51   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
52   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
53   aRes->setImpl(new TopoDS_Shape(anEdge));
54   return aRes;
55 }
56
57 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
58     const std::shared_ptr<GeomAPI_Lin> theLin)
59 {
60   if(!theLin.get()) {
61     return std::shared_ptr<GeomAPI_Edge>();
62   }
63
64   const gp_Lin& aLin = theLin->impl<gp_Lin>();
65   BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
66   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge());
67   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
68   aRes->setImpl(new TopoDS_Shape(anEdge));
69   return aRes;
70 }
71
72 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
73     std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
74 {
75   std::shared_ptr<GeomAPI_Edge> aResult;
76   const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
77   if (aShape.IsNull())
78     return aResult;
79   TopoDS_Face aFace = TopoDS::Face(aShape);
80   if (aFace.IsNull())
81     return aResult;
82   TopLoc_Location aLoc;
83   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
84   if (aSurf.IsNull())
85     return aResult;
86   Handle(Geom_RectangularTrimmedSurface) aTrimmed =
87     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
88   if (!aTrimmed.IsNull())
89     aSurf = aTrimmed->BasisSurface();
90   Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
91   if (aCyl.IsNull())
92     return aResult;
93   gp_Ax1 anAxis = aCyl->Axis();
94   // compute the start and the end points of the resulting edge by the bounding box of the face
95   // (vertices projected to the axis) plus 10%
96   Bnd_Box aFaceBnd;
97   BRepBndLib::Add(aFace, aFaceBnd);
98   gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
99   bool isFirst = true;
100   double aParamMin = 0, aParamMax = 0;
101   for(int aX = 0; aX < 2; aX++) {
102     for(int aY = 0; aY < 2; aY++) {
103       for(int aZ = 0; aZ < 2; aZ++) {
104         gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
105           aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
106         gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
107         double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
108         if (isFirst) {
109           isFirst = false;
110           aParamMin = aProjParam;
111           aParamMax = aProjParam;
112         } else {
113           if (aParamMin > aProjParam)
114             aParamMin = aProjParam;
115           else if (aParamMax < aProjParam)
116             aParamMax = aProjParam;
117         }
118       }
119     }
120   }
121   // add 10%
122   double aDelta = aParamMax - aParamMin;
123   if (aDelta < 1.e-4) aDelta = 1.e-4;
124   aParamMin -= aDelta * 0.1;
125   aParamMax += aDelta * 0.1;
126
127   gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
128   gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
129   /*
130   gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
131   // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
132   gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
133   anEnd.Transform(aLoc.Transformation());
134   */
135
136   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
137   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
138   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
139   // an axis is an infinite object
140   anEdge.Infinite(Standard_True);
141   aRes->setImpl(new TopoDS_Shape(anEdge));
142   return aRes;
143 }
144
145 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
146     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
147     double theRadius)
148 {
149   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
150   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
151
152   gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
153
154   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
155   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
156   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
157   aRes->setImpl(new TopoDS_Shape(anEdge));
158   return aRes;
159 }
160
161 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
162     std::shared_ptr<GeomAPI_Circ> theCircle)
163 {
164   if(!theCircle.get()) {
165     return std::shared_ptr<GeomAPI_Edge>();
166   }
167
168   const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
169   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
170   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge());
171   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
172   aRes->setImpl(new TopoDS_Shape(anEdge));
173   return aRes;
174 }
175
176 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
177     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
178     std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
179 {
180   std::shared_ptr<GeomAPI_Edge> aRes;
181
182   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
183   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
184
185   /// OCCT creates an edge on a circle with empty radius, but visualization
186   /// is not able to process it
187   if (theCenter->isEqual(theStartPoint))
188     return aRes;
189
190   double aRadius = theCenter->distance(theStartPoint);
191   gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
192
193   const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
194   const gp_Pnt& anEnd = theEndPoint->impl<gp_Pnt>();
195
196   BRepBuilderAPI_MakeEdge anEdgeBuilder;
197   anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
198
199   anEdgeBuilder.Build();
200
201   if (anEdgeBuilder.IsDone()) {
202     aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
203     aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
204   }
205   return aRes;
206 }