Salome HOME
cad4bc3cc01b927fb79fe2d86d32be679d8c75a5
[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>
30   GeomAlgoAPI_PointBuilder::vertex(const std::shared_ptr<GeomAPI_Pnt> thePoint)
31 {
32   const gp_Pnt& aPnt = thePoint->impl<gp_Pnt>();
33   BRepBuilderAPI_MakeVertex aMaker(aPnt);
34   TopoDS_Vertex aVertex = aMaker.Vertex();
35   std::shared_ptr<GeomAPI_Vertex> aRes(new GeomAPI_Vertex);
36   aRes->setImpl(new TopoDS_Shape(aVertex));
37   return aRes;
38 }
39
40 //==================================================================================================
41 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const double theX,
42                                                                  const double theY,
43                                                                  const double theZ)
44 {
45   const gp_Pnt aPnt(theX, theY, theZ);
46   BRepBuilderAPI_MakeVertex aMaker(aPnt);
47   TopoDS_Vertex aVertex = aMaker.Vertex();
48   std::shared_ptr<GeomAPI_Vertex> aRes(new GeomAPI_Vertex);
49   aRes->setImpl(new TopoDS_Shape(aVertex));
50   return aRes;
51 }
52
53 //==================================================================================================
54 std::shared_ptr<GeomAPI_Pnt>
55   GeomAlgoAPI_PointBuilder::point(const std::shared_ptr<GeomAPI_Shape> theVertex)
56 {
57   TopoDS_Shape aShape = theVertex->impl<TopoDS_Shape>();
58   if ((!aShape.IsNull()) && (aShape.ShapeType() == TopAbs_VERTEX)) {
59     TopoDS_Vertex aVertex = TopoDS::Vertex(aShape);
60     gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
61     std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
62     return aPnt;
63   }
64   return std::shared_ptr<GeomAPI_Pnt>();
65 }
66
67 //==================================================================================================
68 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexOnEdge(
69                                             const std::shared_ptr<GeomAPI_Edge> theEdge,
70                                             const double theValue,
71                                             const bool theIsPercent,
72                                             const bool theIsReverse)
73 {
74   std::shared_ptr<GeomAPI_Vertex> aVertex;
75
76   if(!theEdge.get()) {
77     return aVertex;
78   }
79
80   double aValue = theValue;
81   if(theIsPercent) {
82     aValue = theEdge->length() / 100.0 * aValue;
83   }
84
85   const TopoDS_Edge& anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
86   Standard_Real aUFirst, aULast;
87   Handle(Geom_Curve) anEdgeCurve = BRep_Tool::Curve(anEdge, aUFirst, aULast);
88
89   if(!anEdgeCurve.IsNull() ) {
90     Handle(Geom_Curve) aReOrientedCurve = anEdgeCurve;
91
92     if(theIsReverse) {
93       aReOrientedCurve = anEdgeCurve->Reversed();
94       aUFirst = anEdgeCurve->ReversedParameter(aULast);
95     }
96
97     // Get the point by length
98     GeomAdaptor_Curve anAdapCurve = GeomAdaptor_Curve(aReOrientedCurve);
99     GCPnts_AbscissaPoint anAbsPnt(anAdapCurve, aValue, aUFirst);
100     Standard_Real aParam = anAbsPnt.Parameter();
101     gp_Pnt aPnt = anAdapCurve.Value(aParam);
102     BRepBuilderAPI_MakeVertex aMkVertex(aPnt);
103     const TopoDS_Vertex& aShape = aMkVertex.Vertex();
104     aVertex.reset(new GeomAPI_Vertex());
105     aVertex->setImpl(new TopoDS_Vertex(aShape));
106   }
107
108   return aVertex;
109 }
110
111 //==================================================================================================
112 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
113     const std::shared_ptr<GeomAPI_Vertex> theVertex,
114     const std::shared_ptr<GeomAPI_Face> thePlane)
115 {
116   std::shared_ptr<GeomAPI_Vertex> aVertex;
117
118   if(!theVertex.get() || !thePlane.get() || !thePlane->isPlanar()) {
119     return aVertex;
120   }
121
122   std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
123   std::shared_ptr<GeomAPI_Pln> aProjPln = thePlane->getPlane();
124
125   std::shared_ptr<GeomAPI_Pnt> aPnt = aProjPln->project(aProjPnt);
126
127   if(!aPnt.get()) {
128     return aVertex;
129   }
130
131   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
132
133   return aVertex;
134 }
135
136 //==================================================================================================
137 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
138     const std::shared_ptr<GeomAPI_Edge> theEdge1,
139     const std::shared_ptr<GeomAPI_Edge> theEdge2)
140 {
141   std::shared_ptr<GeomAPI_Vertex> aVertex;
142
143   if(!theEdge1.get() || !theEdge2.get() || !theEdge1->isLine() || !theEdge2->isLine()) {
144     return aVertex;
145   }
146
147   std::shared_ptr<GeomAPI_Lin> aLin1 = theEdge1->line();
148   std::shared_ptr<GeomAPI_Lin> aLin2 = theEdge2->line();
149
150   std::shared_ptr<GeomAPI_Pnt> aPnt = aLin1->intersect(aLin2);
151
152   if(!aPnt.get()) {
153     return aVertex;
154   }
155
156   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
157
158   return aVertex;
159 }
160
161 //==================================================================================================
162 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
163     const std::shared_ptr<GeomAPI_Edge> theEdge,
164     const std::shared_ptr<GeomAPI_Face> theFace)
165 {
166   std::shared_ptr<GeomAPI_Vertex> aVertex;
167
168   if(!theEdge.get() || !theFace.get() || !theEdge->isLine() || !theFace->isPlanar()) {
169     return aVertex;
170   }
171
172   std::shared_ptr<GeomAPI_Lin> aLin = theEdge->line();
173   std::shared_ptr<GeomAPI_Pln> aPln = theFace->getPlane();
174
175   std::shared_ptr<GeomAPI_Pnt> aPnt = aPln->intersect(aLin);
176
177   if(!aPnt.get()) {
178     return aVertex;
179   }
180
181   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
182
183   return aVertex;
184 }