Salome HOME
Merge branch 'Pre_2.8.0_development'
[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 <gp_Pln.hxx>
38 #include <gp_Pnt.hxx>
39 #include <TopoDS.hxx>
40 #include <TopoDS_Vertex.hxx>
41
42 //==================================================================================================
43 std::shared_ptr<GeomAPI_Vertex>
44   GeomAlgoAPI_PointBuilder::vertex(const std::shared_ptr<GeomAPI_Pnt> thePoint)
45 {
46   const gp_Pnt& aPnt = thePoint->impl<gp_Pnt>();
47   BRepBuilderAPI_MakeVertex aMaker(aPnt);
48   TopoDS_Vertex aVertex = aMaker.Vertex();
49   std::shared_ptr<GeomAPI_Vertex> aRes(new GeomAPI_Vertex);
50   aRes->setImpl(new TopoDS_Shape(aVertex));
51   return aRes;
52 }
53
54 //==================================================================================================
55 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const double theX,
56                                                                  const double theY,
57                                                                  const double theZ)
58 {
59   const gp_Pnt aPnt(theX, theY, theZ);
60   BRepBuilderAPI_MakeVertex aMaker(aPnt);
61   TopoDS_Vertex aVertex = aMaker.Vertex();
62   std::shared_ptr<GeomAPI_Vertex> aRes(new GeomAPI_Vertex);
63   aRes->setImpl(new TopoDS_Shape(aVertex));
64   return aRes;
65 }
66
67 //==================================================================================================
68 std::shared_ptr<GeomAPI_Pnt>
69   GeomAlgoAPI_PointBuilder::point(const std::shared_ptr<GeomAPI_Shape> theVertex)
70 {
71   TopoDS_Shape aShape = theVertex->impl<TopoDS_Shape>();
72   if ((!aShape.IsNull()) && (aShape.ShapeType() == TopAbs_VERTEX)) {
73     TopoDS_Vertex aVertex = TopoDS::Vertex(aShape);
74     gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
75     std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
76     return aPnt;
77   }
78   return std::shared_ptr<GeomAPI_Pnt>();
79 }
80
81 //==================================================================================================
82 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexOnEdge(
83                                             const std::shared_ptr<GeomAPI_Edge> theEdge,
84                                             const double theValue,
85                                             const bool theIsPercent,
86                                             const bool theIsReverse)
87 {
88   std::shared_ptr<GeomAPI_Vertex> aVertex;
89
90   if(!theEdge.get()) {
91     return aVertex;
92   }
93
94   double aValue = theValue;
95   if(theIsPercent) {
96     aValue = theEdge->length() / 100.0 * aValue;
97   }
98
99   const TopoDS_Edge& anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
100   Standard_Real aUFirst, aULast;
101   Handle(Geom_Curve) anEdgeCurve = BRep_Tool::Curve(anEdge, aUFirst, aULast);
102
103   if(!anEdgeCurve.IsNull() ) {
104     Handle(Geom_Curve) aReOrientedCurve = anEdgeCurve;
105
106     if(theIsReverse) {
107       aReOrientedCurve = anEdgeCurve->Reversed();
108       aUFirst = anEdgeCurve->ReversedParameter(aULast);
109     }
110
111     // Get the point by length
112     GeomAdaptor_Curve anAdapCurve = GeomAdaptor_Curve(aReOrientedCurve);
113     GCPnts_AbscissaPoint anAbsPnt(anAdapCurve, aValue, aUFirst);
114     Standard_Real aParam = anAbsPnt.Parameter();
115     gp_Pnt aPnt = anAdapCurve.Value(aParam);
116     BRepBuilderAPI_MakeVertex aMkVertex(aPnt);
117     const TopoDS_Vertex& aShape = aMkVertex.Vertex();
118     aVertex.reset(new GeomAPI_Vertex());
119     aVertex->setImpl(new TopoDS_Vertex(aShape));
120   }
121
122   return aVertex;
123 }
124
125 //==================================================================================================
126 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
127     const std::shared_ptr<GeomAPI_Vertex> theVertex,
128     const std::shared_ptr<GeomAPI_Face> thePlane)
129 {
130   std::shared_ptr<GeomAPI_Vertex> aVertex;
131
132   if(!theVertex.get() || !thePlane.get() || !thePlane->isPlanar()) {
133     return aVertex;
134   }
135
136   std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
137   std::shared_ptr<GeomAPI_Pln> aProjPln = thePlane->getPlane();
138
139   std::shared_ptr<GeomAPI_Pnt> aPnt = aProjPln->project(aProjPnt);
140
141   if(!aPnt.get()) {
142     return aVertex;
143   }
144
145   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
146
147   return aVertex;
148 }
149
150 //==================================================================================================
151 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
152     const std::shared_ptr<GeomAPI_Edge> theEdge1,
153     const std::shared_ptr<GeomAPI_Edge> theEdge2)
154 {
155   std::shared_ptr<GeomAPI_Vertex> aVertex;
156
157   if(!theEdge1.get() || !theEdge2.get() || !theEdge1->isLine() || !theEdge2->isLine()) {
158     return aVertex;
159   }
160
161   std::shared_ptr<GeomAPI_Lin> aLin1 = theEdge1->line();
162   std::shared_ptr<GeomAPI_Lin> aLin2 = theEdge2->line();
163
164   std::shared_ptr<GeomAPI_Pnt> aPnt = aLin1->intersect(aLin2);
165
166   if(!aPnt.get()) {
167     return aVertex;
168   }
169
170   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
171
172   return aVertex;
173 }
174
175 //==================================================================================================
176 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
177     const std::shared_ptr<GeomAPI_Edge> theEdge,
178     const std::shared_ptr<GeomAPI_Face> theFace)
179 {
180   std::shared_ptr<GeomAPI_Vertex> aVertex;
181
182   if(!theEdge.get() || !theFace.get() || !theEdge->isLine() || !theFace->isPlanar()) {
183     return aVertex;
184   }
185
186   std::shared_ptr<GeomAPI_Lin> aLin = theEdge->line();
187   std::shared_ptr<GeomAPI_Pln> aPln = theFace->getPlane();
188
189   std::shared_ptr<GeomAPI_Pnt> aPnt = aPln->intersect(aLin);
190
191   if(!aPnt.get()) {
192     return aVertex;
193   }
194
195   aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
196
197   return aVertex;
198 }