Salome HOME
Compilation fix.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_PointBuilder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_PointBuilder.cpp
4 // Created:     02 Jun 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "GeomAlgoAPI_PointBuilder.h"
8
9 #include <GeomAPI_Edge.h>
10 #include <GeomAPI_Face.h>
11 #include <GeomAPI_Lin.h>
12 #include <GeomAPI_Pln.h>
13 #include <GeomAPI_Pnt.h>
14 #include <GeomAPI_Shape.h>
15 #include <GeomAPI_Vertex.h>
16
17 #include <BRep_Tool.hxx>
18 #include <BRepBuilderAPI_MakeVertex.hxx>
19 #include <GCPnts_AbscissaPoint.hxx>
20 #include <Geom_Line.hxx>
21 #include <GeomAdaptor_Curve.hxx>
22 #include <GeomAPI_ExtremaCurveCurve.hxx>
23 #include <gp_Pln.hxx>
24 #include <gp_Pnt.hxx>
25 #include <TopoDS.hxx>
26 #include <TopoDS_Vertex.hxx>
27
28 //==================================================================================================
29 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const std::shared_ptr<GeomAPI_Pnt> thePoint)
30 {
31   const gp_Pnt& aPnt = thePoint->impl<gp_Pnt>();
32   BRepBuilderAPI_MakeVertex aMaker(aPnt);
33   TopoDS_Vertex aVertex = aMaker.Vertex();
34   std::shared_ptr<GeomAPI_Vertex> aRes(new GeomAPI_Vertex);
35   aRes->setImpl(new TopoDS_Shape(aVertex));
36   return aRes;
37 }
38
39 //==================================================================================================
40 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const double theX,
41                                                                  const double theY,
42                                                                  const double theZ)
43 {
44   const gp_Pnt aPnt(theX, theY, theZ);
45   BRepBuilderAPI_MakeVertex aMaker(aPnt);
46   TopoDS_Vertex aVertex = aMaker.Vertex();
47   std::shared_ptr<GeomAPI_Vertex> aRes(new GeomAPI_Vertex);
48   aRes->setImpl(new TopoDS_Shape(aVertex));
49   return aRes;
50 }
51
52 //==================================================================================================
53 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_PointBuilder::point(const std::shared_ptr<GeomAPI_Shape> theVertex)
54 {
55   TopoDS_Shape aShape = theVertex->impl<TopoDS_Shape>();
56   if ((!aShape.IsNull()) && (aShape.ShapeType() == TopAbs_VERTEX)) {
57     TopoDS_Vertex aVertex = TopoDS::Vertex(aShape);
58     gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
59     std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
60     return aPnt;
61   }
62   return std::shared_ptr<GeomAPI_Pnt>();
63 }
64
65 //==================================================================================================
66 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexOnEdge(const std::shared_ptr<GeomAPI_Edge> theEdge,
67                                                                        const double theValue,
68                                                                        const bool theIsPercent,
69                                                                        const bool theIsReverse)
70 {
71   std::shared_ptr<GeomAPI_Vertex> aVertex;
72
73   if(!theEdge.get()) {
74     return aVertex;
75   }
76
77   double aValue = theValue;
78   if(theIsPercent) {
79     aValue = theEdge->length() / 100.0 * aValue;
80   }
81
82   const TopoDS_Edge& anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
83   Standard_Real aUFirst, aULast;
84   Handle(Geom_Curve) anEdgeCurve = BRep_Tool::Curve(anEdge, aUFirst, aULast);
85
86   if(!anEdgeCurve.IsNull() ) {
87     Handle(Geom_Curve) aReOrientedCurve = anEdgeCurve;
88
89     if(theIsReverse) {
90       aReOrientedCurve = anEdgeCurve->Reversed();
91       aUFirst = anEdgeCurve->ReversedParameter(aULast);
92     }
93
94     // Get the point by length
95     GeomAdaptor_Curve anAdapCurve = GeomAdaptor_Curve(aReOrientedCurve);
96     GCPnts_AbscissaPoint anAbsPnt(anAdapCurve, aValue, aUFirst);
97     Standard_Real aParam = anAbsPnt.Parameter();
98     gp_Pnt aPnt = anAdapCurve.Value(aParam);
99     BRepBuilderAPI_MakeVertex aMkVertex(aPnt);
100     const TopoDS_Vertex& aShape = aMkVertex.Vertex();
101     aVertex.reset(new GeomAPI_Vertex());
102     aVertex->setImpl(new TopoDS_Vertex(aShape));
103   }
104
105   return aVertex;
106 }
107
108 //==================================================================================================
109 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
110     const std::shared_ptr<GeomAPI_Vertex> theVertex,
111     const std::shared_ptr<GeomAPI_Face> thePlane)
112 {
113   std::shared_ptr<GeomAPI_Vertex> aVertex;
114
115   if(!theVertex.get() || !thePlane.get() || !thePlane->isPlanar()) {
116     return aVertex;
117   }
118
119   std::shared_ptr<GeomAPI_Pnt> aGeomPnt = theVertex->point();
120   gp_Pnt aPnt = aGeomPnt->impl<gp_Pnt>();
121
122   std::shared_ptr<GeomAPI_Pln> aGeomPln = thePlane->getPlane();
123   gp_Pln aPln = aGeomPln->impl<gp_Pln>();
124
125   gp_Dir aPntAxis = aPnt.XYZ() - aPln.Location().XYZ();
126   gp_Dir aPlnNorm = aPln.Axis().Direction();
127
128   if(aPntAxis * aPlnNorm > 0) {
129     aPlnNorm.Reverse();
130   }
131
132   double aDistance = aPln.Distance(aPnt);
133   aPnt.Translate(gp_Vec(aPlnNorm) * aDistance);
134
135   aVertex.reset(new GeomAPI_Vertex(aPnt.X(), aPnt.Y(), aPnt.Z()));
136
137   return aVertex;
138 }
139
140 //==================================================================================================
141 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
142     const std::shared_ptr<GeomAPI_Edge> theEdge1,
143     const std::shared_ptr<GeomAPI_Edge> theEdge2)
144 {
145   std::shared_ptr<GeomAPI_Vertex> aVertex;
146
147   if(!theEdge1.get() || !theEdge2.get() || !theEdge1->isLine() || !theEdge2->isLine()) {
148     return aVertex;
149   }
150
151   gp_Lin aLin1 = theEdge1->line()->impl<gp_Lin>();
152   gp_Lin aLin2 = theEdge2->line()->impl<gp_Lin>();
153
154   if(aLin1.Distance(aLin2) > Precision::Confusion()) {
155     return aVertex;
156   }
157
158   Handle(Geom_Line) aLine1 = new Geom_Line(aLin1);
159   Handle(Geom_Line) aLine2 = new Geom_Line(aLin2);
160
161   GeomAPI_ExtremaCurveCurve anExtrema(aLine1, aLine2);
162
163   Standard_Integer aNbExtrema = anExtrema.NbExtrema();
164
165   if(aNbExtrema == 0) {
166     return aVertex;
167   }
168
169   gp_Pnt aPnt1, aPnt2;
170   for(Standard_Integer anIndex = 1; anIndex <= aNbExtrema; ++anIndex) {
171     if(anExtrema.Distance(anIndex) <= Precision::Confusion()) {
172       anExtrema.Points(anIndex, aPnt1, aPnt2);
173     }
174   }
175
176   aVertex.reset(new GeomAPI_Vertex(aPnt1.X(), aPnt1.Y(), aPnt1.Z()));
177
178   return aVertex;
179 }