Salome HOME
Added option to create Construction Point by intersection of line and plane.
[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> aProjPnt = theVertex->point();
120   std::shared_ptr<GeomAPI_Pln> aProjPln = thePlane->getPlane();
121
122   std::shared_ptr<GeomAPI_Pnt> aPnt = aProjPln->project(aProjPnt);
123
124   if(!aPnt.get()) {
125     return aVertex;
126   }
127
128   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
129
130   return aVertex;
131 }
132
133 //==================================================================================================
134 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
135     const std::shared_ptr<GeomAPI_Edge> theEdge1,
136     const std::shared_ptr<GeomAPI_Edge> theEdge2)
137 {
138   std::shared_ptr<GeomAPI_Vertex> aVertex;
139
140   if(!theEdge1.get() || !theEdge2.get() || !theEdge1->isLine() || !theEdge2->isLine()) {
141     return aVertex;
142   }
143
144   std::shared_ptr<GeomAPI_Lin> aLin1 = theEdge1->line();
145   std::shared_ptr<GeomAPI_Lin> aLin2 = theEdge2->line();
146
147   std::shared_ptr<GeomAPI_Pnt> aPnt = aLin1->intersect(aLin2);
148
149   if(!aPnt.get()) {
150     return aVertex;
151   }
152
153   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
154
155   return aVertex;
156 }
157
158 //==================================================================================================
159 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
160     const std::shared_ptr<GeomAPI_Edge> theEdge,
161     const std::shared_ptr<GeomAPI_Face> theFace)
162 {
163   std::shared_ptr<GeomAPI_Vertex> aVertex;
164
165   if(!theEdge.get() || !theFace.get() || !theEdge->isLine() || !theFace->isPlanar()) {
166     return aVertex;
167   }
168
169   std::shared_ptr<GeomAPI_Lin> aLin = theEdge->line();
170   std::shared_ptr<GeomAPI_Pln> aPln = theFace->getPlane();
171
172   std::shared_ptr<GeomAPI_Pnt> aPnt = aPln->intersect(aLin);
173
174   if(!aPnt.get()) {
175     return aVertex;
176   }
177
178   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
179
180   return aVertex;
181 }