1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_PointBuilder.h"
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>
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>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Vertex.hxx>
44 //==================================================================================================
45 std::shared_ptr<GeomAPI_Vertex>
46 GeomAlgoAPI_PointBuilder::vertex(const std::shared_ptr<GeomAPI_Pnt> thePoint)
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));
56 //==================================================================================================
57 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const double theX,
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));
69 //==================================================================================================
70 std::shared_ptr<GeomAPI_Pnt>
71 GeomAlgoAPI_PointBuilder::point(const std::shared_ptr<GeomAPI_Shape> theVertex)
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()));
80 return std::shared_ptr<GeomAPI_Pnt>();
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)
90 std::shared_ptr<GeomAPI_Vertex> aVertex;
96 double aValue = theValue;
98 aValue = theEdge->length() / 100.0 * aValue;
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);
105 if(!anEdgeCurve.IsNull() ) {
106 Handle(Geom_Curve) aReOrientedCurve = anEdgeCurve;
109 aReOrientedCurve = anEdgeCurve->Reversed();
110 aUFirst = anEdgeCurve->ReversedParameter(aULast);
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));
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)
132 std::shared_ptr<GeomAPI_Vertex> aVertex;
134 if (!theVertex.get() || !theEdge.get()) {
138 std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
139 gp_Pnt aPnt(aProjPnt->x(), aProjPnt->y(), aProjPnt->z());
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);
145 if (aCurve.IsNull()) {
149 GeomAPI_ProjectPointOnCurve aProjection(aPnt, aCurve);
151 if (aProjection.NbPoints() == 0) {
155 gp_Pnt aNearestPoint = aProjection.NearestPoint();
157 aVertex.reset(new GeomAPI_Vertex(aNearestPoint.X(), aNearestPoint.Y(), aNearestPoint.Z()));
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)
167 std::shared_ptr<GeomAPI_Vertex> aVertex;
169 if(!theVertex.get() || !theFace.get() || !theFace->isPlanar()) {
173 std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
174 std::shared_ptr<GeomAPI_Pln> aProjPln = theFace->getPlane();
176 std::shared_ptr<GeomAPI_Pnt> aPnt = aProjPln->project(aProjPnt);
182 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
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)
192 std::shared_ptr<GeomAPI_Vertex> aVertex;
194 if(!theEdge1.get() || !theEdge2.get() || !theEdge1->isLine() || !theEdge2->isLine()) {
198 std::shared_ptr<GeomAPI_Lin> aLin1 = theEdge1->line();
199 std::shared_ptr<GeomAPI_Lin> aLin2 = theEdge2->line();
201 std::shared_ptr<GeomAPI_Pnt> aPnt = aLin1->intersect(aLin2);
207 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
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)
217 std::shared_ptr<GeomAPI_Vertex> aVertex;
219 if(!theEdge.get() || !theFace.get() || !theEdge->isLine() || !theFace->isPlanar()) {
223 std::shared_ptr<GeomAPI_Lin> aLin = theEdge->line();
224 std::shared_ptr<GeomAPI_Pln> aPln = theFace->getPlane();
226 std::shared_ptr<GeomAPI_Pnt> aPnt = aPln->intersect(aLin);
232 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));