1 // Copyright (C) 2014-2020 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 email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_PointBuilder.h"
22 #include <GeomAPI_Edge.h>
23 #include <GeomAPI_Face.h>
24 #include <GeomAPI_Lin.h>
25 #include <GeomAPI_Pln.h>
26 #include <GeomAPI_Pnt.h>
27 #include <GeomAPI_Shape.h>
28 #include <GeomAPI_Vertex.h>
30 #include <BRep_Tool.hxx>
31 #include <BRepBuilderAPI_MakeVertex.hxx>
32 #include <GCPnts_AbscissaPoint.hxx>
33 #include <Geom_Line.hxx>
34 #include <GeomAdaptor_Curve.hxx>
35 #include <GeomAPI_ExtremaCurveCurve.hxx>
36 #include <GeomAPI_ProjectPointOnCurve.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Vertex.hxx>
43 //==================================================================================================
44 std::shared_ptr<GeomAPI_Vertex>
45 GeomAlgoAPI_PointBuilder::vertex(const std::shared_ptr<GeomAPI_Pnt> thePoint)
47 const gp_Pnt& aPnt = thePoint->impl<gp_Pnt>();
48 BRepBuilderAPI_MakeVertex aMaker(aPnt);
49 TopoDS_Vertex aVertex = aMaker.Vertex();
50 std::shared_ptr<GeomAPI_Vertex> aRes(new GeomAPI_Vertex);
51 aRes->setImpl(new TopoDS_Shape(aVertex));
55 //==================================================================================================
56 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const double theX,
60 const gp_Pnt aPnt(theX, theY, theZ);
61 BRepBuilderAPI_MakeVertex aMaker(aPnt);
62 TopoDS_Vertex aVertex = aMaker.Vertex();
63 std::shared_ptr<GeomAPI_Vertex> aRes(new GeomAPI_Vertex);
64 aRes->setImpl(new TopoDS_Shape(aVertex));
68 //==================================================================================================
69 std::shared_ptr<GeomAPI_Pnt>
70 GeomAlgoAPI_PointBuilder::point(const std::shared_ptr<GeomAPI_Shape> theVertex)
72 TopoDS_Shape aShape = theVertex->impl<TopoDS_Shape>();
73 if ((!aShape.IsNull()) && (aShape.ShapeType() == TopAbs_VERTEX)) {
74 TopoDS_Vertex aVertex = TopoDS::Vertex(aShape);
75 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
76 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
79 return std::shared_ptr<GeomAPI_Pnt>();
82 //==================================================================================================
83 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexOnEdge(
84 const std::shared_ptr<GeomAPI_Edge> theEdge,
85 const double theValue,
86 const bool theIsPercent,
87 const bool theIsReverse)
89 std::shared_ptr<GeomAPI_Vertex> aVertex;
95 double aValue = theValue;
97 aValue = theEdge->length() / 100.0 * aValue;
100 const TopoDS_Edge& anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
101 Standard_Real aUFirst, aULast;
102 Handle(Geom_Curve) anEdgeCurve = BRep_Tool::Curve(anEdge, aUFirst, aULast);
104 if(!anEdgeCurve.IsNull() ) {
105 Handle(Geom_Curve) aReOrientedCurve = anEdgeCurve;
108 aReOrientedCurve = anEdgeCurve->Reversed();
109 aUFirst = anEdgeCurve->ReversedParameter(aULast);
112 // Get the point by length
113 GeomAdaptor_Curve anAdapCurve = GeomAdaptor_Curve(aReOrientedCurve);
114 GCPnts_AbscissaPoint anAbsPnt(anAdapCurve, aValue, aUFirst);
115 Standard_Real aParam = anAbsPnt.Parameter();
116 gp_Pnt aPnt = anAdapCurve.Value(aParam);
117 BRepBuilderAPI_MakeVertex aMkVertex(aPnt);
118 const TopoDS_Vertex& aShape = aMkVertex.Vertex();
119 aVertex.reset(new GeomAPI_Vertex());
120 aVertex->setImpl(new TopoDS_Vertex(aShape));
126 //==================================================================================================
127 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
128 const std::shared_ptr<GeomAPI_Vertex> theVertex,
129 const std::shared_ptr<GeomAPI_Edge> theEdge)
131 std::shared_ptr<GeomAPI_Vertex> aVertex;
133 if (!theVertex.get() || !theEdge.get()) {
137 std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
138 gp_Pnt aPnt(aProjPnt->x(), aProjPnt->y(), aProjPnt->z());
140 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
141 double aFirstOnCurve, aLastOnCurve;
142 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
144 if (aCurve.IsNull()) {
148 GeomAPI_ProjectPointOnCurve aProjection(aPnt, aCurve);
150 if (aProjection.NbPoints() != 0) {
151 gp_Pnt aNearestPoint = aProjection.NearestPoint();
152 aVertex.reset(new GeomAPI_Vertex(aNearestPoint.X(), aNearestPoint.Y(), aNearestPoint.Z()));
158 //==================================================================================================
159 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
160 const std::shared_ptr<GeomAPI_Vertex> theVertex,
161 const std::shared_ptr<GeomAPI_Face> theFace)
163 std::shared_ptr<GeomAPI_Vertex> aVertex;
165 if (theVertex.get() && theFace.get() && theFace->isPlanar()) {
166 std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
167 std::shared_ptr<GeomAPI_Pln> aProjPln = theFace->getPlane();
169 std::shared_ptr<GeomAPI_Pnt> aPnt = aProjPln->project(aProjPnt);
172 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
178 //==================================================================================================
179 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
180 const std::shared_ptr<GeomAPI_Edge> theEdge1,
181 const std::shared_ptr<GeomAPI_Edge> theEdge2)
183 std::shared_ptr<GeomAPI_Vertex> aVertex;
185 if (theEdge1.get() && theEdge2.get() && theEdge1->isLine() && theEdge2->isLine()) {
186 std::shared_ptr<GeomAPI_Lin> aLin1 = theEdge1->line();
187 std::shared_ptr<GeomAPI_Lin> aLin2 = theEdge2->line();
189 std::shared_ptr<GeomAPI_Pnt> aPnt = aLin1->intersect(aLin2);
192 aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));