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