Salome HOME
8e2ee6093aa77dd25beb4391f94cc6cd291d8e4a
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_PointBuilder.cpp
1 // Copyright (C) 2014-2023  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_PointBuilder.h"
21
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>
29
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>
37 #include <gp_Pln.hxx>
38 #include <gp_Pnt.hxx>
39 #include <TopoDS.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Vertex.hxx>
42
43 //==================================================================================================
44 std::shared_ptr<GeomAPI_Vertex>
45   GeomAlgoAPI_PointBuilder::vertex(const std::shared_ptr<GeomAPI_Pnt> thePoint)
46 {
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));
52   return aRes;
53 }
54
55 //==================================================================================================
56 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const double theX,
57                                                                  const double theY,
58                                                                  const double theZ)
59 {
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));
65   return aRes;
66 }
67
68 //==================================================================================================
69 std::shared_ptr<GeomAPI_Pnt>
70   GeomAlgoAPI_PointBuilder::point(const std::shared_ptr<GeomAPI_Shape> theVertex)
71 {
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()));
77     return aPnt;
78   }
79   return std::shared_ptr<GeomAPI_Pnt>();
80 }
81
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)
88 {
89   std::shared_ptr<GeomAPI_Vertex> aVertex;
90
91   if(!theEdge.get()) {
92     return aVertex;
93   }
94
95   double aValue = theValue;
96   if(theIsPercent) {
97     aValue = theEdge->length() / 100.0 * aValue;
98   }
99
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);
103
104   if(!anEdgeCurve.IsNull() ) {
105     Handle(Geom_Curve) aReOrientedCurve = anEdgeCurve;
106
107     if(theIsReverse) {
108       aReOrientedCurve = anEdgeCurve->Reversed();
109       aUFirst = anEdgeCurve->ReversedParameter(aULast);
110     }
111
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));
121   }
122
123   return aVertex;
124 }
125
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)
130 {
131   std::shared_ptr<GeomAPI_Vertex> aVertex;
132
133   if (!theVertex.get() || !theEdge.get()) {
134     return aVertex;
135   }
136
137   std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
138   gp_Pnt aPnt(aProjPnt->x(), aProjPnt->y(), aProjPnt->z());
139
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);
143
144   if (aCurve.IsNull()) {
145     return aVertex;
146   }
147
148   GeomAPI_ProjectPointOnCurve aProjection(aPnt, aCurve);
149
150   if (aProjection.NbPoints() != 0) {
151     gp_Pnt aNearestPoint = aProjection.NearestPoint();
152     aVertex.reset(new GeomAPI_Vertex(aNearestPoint.X(), aNearestPoint.Y(), aNearestPoint.Z()));
153   }
154
155   return aVertex;
156 }
157
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)
162 {
163   std::shared_ptr<GeomAPI_Vertex> aVertex;
164
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();
168
169     std::shared_ptr<GeomAPI_Pnt> aPnt = aProjPln->project(aProjPnt);
170
171     if (aPnt.get())
172       aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
173   }
174
175   return aVertex;
176 }
177
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)
182 {
183   std::shared_ptr<GeomAPI_Vertex> aVertex;
184
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();
188
189     std::shared_ptr<GeomAPI_Pnt> aPnt = aLin1->intersect(aLin2);
190
191     if (aPnt.get())
192       aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
193   }
194
195   return aVertex;
196 }