]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_PointBuilder.cpp
Salome HOME
2.3.3.1 Point creation by projection of another point on a line
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_PointBuilder.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_PointBuilder.h"
22
23 #include <GeomAPI_Edge.h>
24 #include <GeomAPI_Face.h>
25 #include <GeomAPI_Lin.h>
26 #include <GeomAPI_Pln.h>
27 #include <GeomAPI_Pnt.h>
28 #include <GeomAPI_Shape.h>
29 #include <GeomAPI_Vertex.h>
30
31 #include <BRep_Tool.hxx>
32 #include <BRepBuilderAPI_MakeVertex.hxx>
33 #include <GCPnts_AbscissaPoint.hxx>
34 #include <Geom_Line.hxx>
35 #include <GeomAdaptor_Curve.hxx>
36 #include <GeomAPI_ExtremaCurveCurve.hxx>
37 #include <GeomAPI_ProjectPointOnCurve.hxx>
38 #include <gp_Pln.hxx>
39 #include <gp_Pnt.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Vertex.hxx>
43
44 //==================================================================================================
45 std::shared_ptr<GeomAPI_Vertex>
46   GeomAlgoAPI_PointBuilder::vertex(const std::shared_ptr<GeomAPI_Pnt> thePoint)
47 {
48   const gp_Pnt& aPnt = thePoint->impl<gp_Pnt>();
49   BRepBuilderAPI_MakeVertex aMaker(aPnt);
50   TopoDS_Vertex aVertex = aMaker.Vertex();
51   std::shared_ptr<GeomAPI_Vertex> aRes(new GeomAPI_Vertex);
52   aRes->setImpl(new TopoDS_Shape(aVertex));
53   return aRes;
54 }
55
56 //==================================================================================================
57 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const double theX,
58                                                                  const double theY,
59                                                                  const double theZ)
60 {
61   const gp_Pnt aPnt(theX, theY, theZ);
62   BRepBuilderAPI_MakeVertex aMaker(aPnt);
63   TopoDS_Vertex aVertex = aMaker.Vertex();
64   std::shared_ptr<GeomAPI_Vertex> aRes(new GeomAPI_Vertex);
65   aRes->setImpl(new TopoDS_Shape(aVertex));
66   return aRes;
67 }
68
69 //==================================================================================================
70 std::shared_ptr<GeomAPI_Pnt>
71   GeomAlgoAPI_PointBuilder::point(const std::shared_ptr<GeomAPI_Shape> theVertex)
72 {
73   TopoDS_Shape aShape = theVertex->impl<TopoDS_Shape>();
74   if ((!aShape.IsNull()) && (aShape.ShapeType() == TopAbs_VERTEX)) {
75     TopoDS_Vertex aVertex = TopoDS::Vertex(aShape);
76     gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
77     std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
78     return aPnt;
79   }
80   return std::shared_ptr<GeomAPI_Pnt>();
81 }
82
83 //==================================================================================================
84 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexOnEdge(
85                                             const std::shared_ptr<GeomAPI_Edge> theEdge,
86                                             const double theValue,
87                                             const bool theIsPercent,
88                                             const bool theIsReverse)
89 {
90   std::shared_ptr<GeomAPI_Vertex> aVertex;
91
92   if(!theEdge.get()) {
93     return aVertex;
94   }
95
96   double aValue = theValue;
97   if(theIsPercent) {
98     aValue = theEdge->length() / 100.0 * aValue;
99   }
100
101   const TopoDS_Edge& anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
102   Standard_Real aUFirst, aULast;
103   Handle(Geom_Curve) anEdgeCurve = BRep_Tool::Curve(anEdge, aUFirst, aULast);
104
105   if(!anEdgeCurve.IsNull() ) {
106     Handle(Geom_Curve) aReOrientedCurve = anEdgeCurve;
107
108     if(theIsReverse) {
109       aReOrientedCurve = anEdgeCurve->Reversed();
110       aUFirst = anEdgeCurve->ReversedParameter(aULast);
111     }
112
113     // Get the point by length
114     GeomAdaptor_Curve anAdapCurve = GeomAdaptor_Curve(aReOrientedCurve);
115     GCPnts_AbscissaPoint anAbsPnt(anAdapCurve, aValue, aUFirst);
116     Standard_Real aParam = anAbsPnt.Parameter();
117     gp_Pnt aPnt = anAdapCurve.Value(aParam);
118     BRepBuilderAPI_MakeVertex aMkVertex(aPnt);
119     const TopoDS_Vertex& aShape = aMkVertex.Vertex();
120     aVertex.reset(new GeomAPI_Vertex());
121     aVertex->setImpl(new TopoDS_Vertex(aShape));
122   }
123
124   return aVertex;
125 }
126
127 //==================================================================================================
128 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
129   const std::shared_ptr<GeomAPI_Vertex> theVertex,
130   const std::shared_ptr<GeomAPI_Edge> theEdge)
131 {
132   std::shared_ptr<GeomAPI_Vertex> aVertex;
133
134   if (!theVertex.get() || !theEdge.get()) {
135     return aVertex;
136   }
137
138   std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
139   gp_Pnt aPnt(aProjPnt->x(), aProjPnt->y(), aProjPnt->z());
140
141   TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
142   double aFirstOnCurve, aLastOnCurve;
143   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
144
145   if (aCurve.IsNull()) {
146     return aVertex;
147   }
148
149   GeomAPI_ProjectPointOnCurve aProjection(aPnt, aCurve);
150
151   if (aProjection.NbPoints() == 0) {
152     return aVertex;
153   }
154
155   gp_Pnt aNearestPoint = aProjection.NearestPoint();
156
157   aVertex.reset(new GeomAPI_Vertex(aNearestPoint.X(), aNearestPoint.Y(), aNearestPoint.Z()));
158
159   return aVertex;
160 }
161
162 //==================================================================================================
163 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
164     const std::shared_ptr<GeomAPI_Vertex> theVertex,
165     const std::shared_ptr<GeomAPI_Face> theFace)
166 {
167   std::shared_ptr<GeomAPI_Vertex> aVertex;
168
169   if(!theVertex.get() || !theFace.get() || !theFace->isPlanar()) {
170     return aVertex;
171   }
172
173   std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
174   std::shared_ptr<GeomAPI_Pln> aProjPln = theFace->getPlane();
175
176   std::shared_ptr<GeomAPI_Pnt> aPnt = aProjPln->project(aProjPnt);
177
178   if(!aPnt.get()) {
179     return aVertex;
180   }
181
182   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
183
184   return aVertex;
185 }
186
187 //==================================================================================================
188 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
189     const std::shared_ptr<GeomAPI_Edge> theEdge1,
190     const std::shared_ptr<GeomAPI_Edge> theEdge2)
191 {
192   std::shared_ptr<GeomAPI_Vertex> aVertex;
193
194   if(!theEdge1.get() || !theEdge2.get() || !theEdge1->isLine() || !theEdge2->isLine()) {
195     return aVertex;
196   }
197
198   std::shared_ptr<GeomAPI_Lin> aLin1 = theEdge1->line();
199   std::shared_ptr<GeomAPI_Lin> aLin2 = theEdge2->line();
200
201   std::shared_ptr<GeomAPI_Pnt> aPnt = aLin1->intersect(aLin2);
202
203   if(!aPnt.get()) {
204     return aVertex;
205   }
206
207   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
208
209   return aVertex;
210 }
211
212 //==================================================================================================
213 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
214     const std::shared_ptr<GeomAPI_Edge> theEdge,
215     const std::shared_ptr<GeomAPI_Face> theFace)
216 {
217   std::shared_ptr<GeomAPI_Vertex> aVertex;
218
219   if(!theEdge.get() || !theFace.get() || !theEdge->isLine() || !theFace->isPlanar()) {
220     return aVertex;
221   }
222
223   std::shared_ptr<GeomAPI_Lin> aLin = theEdge->line();
224   std::shared_ptr<GeomAPI_Pln> aPln = theFace->getPlane();
225
226   std::shared_ptr<GeomAPI_Pnt> aPnt = aPln->intersect(aLin);
227
228   if(!aPnt.get()) {
229     return aVertex;
230   }
231
232   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
233
234   return aVertex;
235 }