1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_PointBuilder.cpp
4 // Created: 02 Jun 2014
5 // Author: Mikhail PONIKAROV
7 #include "GeomAlgoAPI_PointBuilder.h"
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>
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>
26 #include <TopoDS_Vertex.hxx>
28 //==================================================================================================
29 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const std::shared_ptr<GeomAPI_Pnt> thePoint)
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));
39 //==================================================================================================
40 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const double theX,
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));
52 //==================================================================================================
53 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_PointBuilder::point(const std::shared_ptr<GeomAPI_Shape> theVertex)
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()));
62 return std::shared_ptr<GeomAPI_Pnt>();
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)
71 std::shared_ptr<GeomAPI_Vertex> aVertex;
77 double aValue = theValue;
79 aValue = theEdge->length() / 100.0 * aValue;
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);
86 if(!anEdgeCurve.IsNull() ) {
87 Handle(Geom_Curve) aReOrientedCurve = anEdgeCurve;
90 aReOrientedCurve = anEdgeCurve->Reversed();
91 aUFirst = anEdgeCurve->ReversedParameter(aULast);
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));
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)
113 std::shared_ptr<GeomAPI_Vertex> aVertex;
115 if(!theVertex.get() || !thePlane.get() || !thePlane->isPlanar()) {
119 std::shared_ptr<GeomAPI_Pnt> aGeomPnt = theVertex->point();
120 gp_Pnt aPnt = aGeomPnt->impl<gp_Pnt>();
122 std::shared_ptr<GeomAPI_Pln> aGeomPln = thePlane->getPlane();
123 gp_Pln aPln = aGeomPln->impl<gp_Pln>();
125 gp_Dir aPntAxis = aPnt.XYZ() - aPln.Location().XYZ();
126 gp_Dir aPlnNorm = aPln.Axis().Direction();
128 if(aPntAxis * aPlnNorm > 0) {
132 double aDistance = aPln.Distance(aPnt);
133 aPnt.Translate(gp_Vec(aPlnNorm) * aDistance);
135 aVertex.reset(new GeomAPI_Vertex(aPnt.X(), aPnt.Y(), aPnt.Z()));
140 //==================================================================================================
141 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
142 const std::shared_ptr<GeomAPI_Edge> theEdge1,
143 const std::shared_ptr<GeomAPI_Edge> theEdge2)
145 std::shared_ptr<GeomAPI_Vertex> aVertex;
147 if(!theEdge1.get() || !theEdge2.get() || !theEdge1->isLine() || !theEdge2->isLine()) {
151 gp_Lin aLin1 = theEdge1->line()->impl<gp_Lin>();
152 gp_Lin aLin2 = theEdge2->line()->impl<gp_Lin>();
154 if(aLin1.Distance(aLin2) > Precision::Confusion()) {
158 Handle(Geom_Line) aLine1 = new Geom_Line(aLin1);
159 Handle(Geom_Line) aLine2 = new Geom_Line(aLin2);
161 GeomAPI_ExtremaCurveCurve anExtrema(aLine1, aLine2);
163 Standard_Integer aNbExtrema = anExtrema.NbExtrema();
165 if(aNbExtrema == 0) {
170 for(Standard_Integer anIndex = 1; anIndex <= aNbExtrema; ++anIndex) {
171 if(anExtrema.Distance(anIndex) <= Precision::Confusion()) {
172 anExtrema.Points(anIndex, aPnt1, aPnt2);
176 aVertex.reset(new GeomAPI_Vertex(aPnt1.X(), aPnt1.Y(), aPnt1.Z()));