Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[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     gp_Pnt aNearestPoint = aProjection.NearestPoint();
153     aVertex.reset(new GeomAPI_Vertex(aNearestPoint.X(), aNearestPoint.Y(), aNearestPoint.Z()));
154   }
155
156   return aVertex;
157 }
158
159 //==================================================================================================
160 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
161     const std::shared_ptr<GeomAPI_Vertex> theVertex,
162     const std::shared_ptr<GeomAPI_Face> theFace)
163 {
164   std::shared_ptr<GeomAPI_Vertex> aVertex;
165
166   if (theVertex.get() && theFace.get() && theFace->isPlanar()) {
167     std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
168     std::shared_ptr<GeomAPI_Pln> aProjPln = theFace->getPlane();
169
170     std::shared_ptr<GeomAPI_Pnt> aPnt = aProjPln->project(aProjPnt);
171
172     if (aPnt.get())
173       aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
174   }
175
176   return aVertex;
177 }
178
179 //==================================================================================================
180 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
181     const std::shared_ptr<GeomAPI_Edge> theEdge1,
182     const std::shared_ptr<GeomAPI_Edge> theEdge2)
183 {
184   std::shared_ptr<GeomAPI_Vertex> aVertex;
185
186   if (theEdge1.get() && theEdge2.get() && theEdge1->isLine() && theEdge2->isLine()) {
187     std::shared_ptr<GeomAPI_Lin> aLin1 = theEdge1->line();
188     std::shared_ptr<GeomAPI_Lin> aLin2 = theEdge2->line();
189
190     std::shared_ptr<GeomAPI_Pnt> aPnt = aLin1->intersect(aLin2);
191
192     if (aPnt.get())
193       aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
194   }
195
196   return aVertex;
197 }