Salome HOME
Issue #736: axis size correct computation basing on bounding box of cylindrical face
[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
40 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
41     std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
42 {
43   std::shared_ptr<GeomAPI_Edge> aResult;
44   const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
45   if (aShape.IsNull())
46     return aResult;
47   TopoDS_Face aFace = TopoDS::Face(aShape);
48   if (aFace.IsNull())
49     return aResult;
50   TopLoc_Location aLoc;
51   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
52   if (aSurf.IsNull())
53     return aResult;
54   Handle(Geom_RectangularTrimmedSurface) aTrimmed = 
55     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
56   if (!aTrimmed.IsNull())
57     aSurf = aTrimmed->BasisSurface();
58   Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
59   if (aCyl.IsNull())
60     return aResult;
61   gp_Ax1 anAxis = aCyl->Axis();
62   // compute the start and the end points of the resulting edge by the bounding box of the face
63   // (vertices projected to the axis) plus 10%
64   Bnd_Box aFaceBnd;
65   BRepBndLib::Add(aFace, aFaceBnd);
66   gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
67   bool isFirst = true;
68   double aParamMin = 0, aParamMax = 0;
69   for(int aX = 0; aX < 2; aX++) {
70     for(int aY = 0; aY < 2; aY++) {
71       for(int aZ = 0; aZ < 2; aZ++) {
72         gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(), 
73           aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
74         gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
75         double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
76         if (isFirst) {
77           isFirst = false;
78           aParamMin = aProjParam;
79           aParamMax = aProjParam;
80         } else {
81           if (aParamMin > aProjParam)
82             aParamMin = aProjParam;
83           else if (aParamMax < aProjParam)
84             aParamMax = aProjParam;
85         }
86       }
87     }
88   }
89   // add 10%
90   double aDelta = aParamMax - aParamMin;
91   if (aDelta < 1.e-4) aDelta = 1.e-4;
92   aParamMin -= aDelta * 0.1;
93   aParamMax += aDelta * 0.1;
94
95   gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
96   gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
97   /*
98   gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
99   // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
100   gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
101   anEnd.Transform(aLoc.Transformation());
102   */
103   
104   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
105   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
106   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
107   // an axis is an infinite object
108   anEdge.Infinite(Standard_True);
109   aRes->setImpl(new TopoDS_Shape(anEdge));
110   return aRes;
111 }
112
113 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
114     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
115     double theRadius)
116 {
117   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
118   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
119
120   gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
121
122   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
123   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
124   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
125   aRes->setImpl(new TopoDS_Shape(anEdge));
126   return aRes;
127 }
128
129 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
130     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
131     std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
132 {
133   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
134   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
135
136   double aRadius = theCenter->distance(theStartPoint);
137   gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
138
139   const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
140   const gp_Pnt& anEnd = theEndPoint->impl<gp_Pnt>();
141
142   BRepBuilderAPI_MakeEdge anEdgeBuilder;
143   if (aStart.IsEqual(anEnd, Precision::Confusion())
144       || gp_Pnt(0, 0, 0).IsEqual(anEnd, Precision::Confusion()))
145     anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle);
146   else
147     anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
148
149   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
150   anEdgeBuilder.Build();
151
152   if (anEdgeBuilder.IsDone())
153     aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
154   else
155     aRes = std::shared_ptr<GeomAPI_Edge>();
156   return aRes;
157 }