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