Salome HOME
Copyright update 2021
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_PointBuilder.cpp
index a76850441fe1e14021b5613d75f9b57d5b76bd9d..ac91af02e5eeaa244383d968ef1663c5437ffb20 100644 (file)
@@ -1,13 +1,27 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        GeomAlgoAPI_PointBuilder.cpp
-// Created:     02 Jun 2014
-// Author:      Mikhail PONIKAROV
+// Copyright (C) 2014-2021  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "GeomAlgoAPI_PointBuilder.h"
 
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Face.h>
+#include <GeomAPI_Lin.h>
 #include <GeomAPI_Pln.h>
 #include <GeomAPI_Pnt.h>
 #include <GeomAPI_Shape.h>
 #include <BRep_Tool.hxx>
 #include <BRepBuilderAPI_MakeVertex.hxx>
 #include <GCPnts_AbscissaPoint.hxx>
+#include <Geom_Line.hxx>
 #include <GeomAdaptor_Curve.hxx>
+#include <GeomAPI_ExtremaCurveCurve.hxx>
+#include <GeomAPI_ProjectPointOnCurve.hxx>
 #include <gp_Pln.hxx>
 #include <gp_Pnt.hxx>
 #include <TopoDS.hxx>
+#include <TopoDS_Edge.hxx>
 #include <TopoDS_Vertex.hxx>
 
 //==================================================================================================
-std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const std::shared_ptr<GeomAPI_Pnt> thePoint)
+std::shared_ptr<GeomAPI_Vertex>
+  GeomAlgoAPI_PointBuilder::vertex(const std::shared_ptr<GeomAPI_Pnt> thePoint)
 {
   const gp_Pnt& aPnt = thePoint->impl<gp_Pnt>();
   BRepBuilderAPI_MakeVertex aMaker(aPnt);
@@ -47,7 +66,8 @@ std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertex(const double th
 }
 
 //==================================================================================================
-std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_PointBuilder::point(const std::shared_ptr<GeomAPI_Shape> theVertex)
+std::shared_ptr<GeomAPI_Pnt>
+  GeomAlgoAPI_PointBuilder::point(const std::shared_ptr<GeomAPI_Shape> theVertex)
 {
   TopoDS_Shape aShape = theVertex->impl<TopoDS_Shape>();
   if ((!aShape.IsNull()) && (aShape.ShapeType() == TopAbs_VERTEX)) {
@@ -60,13 +80,16 @@ std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_PointBuilder::point(const std::shared_p
 }
 
 //==================================================================================================
-std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexOnEdge(const std::shared_ptr<GeomAPI_Edge> theEdge,
-                                                                       const double theValue,
-                                                                       const bool theIsPercent,
-                                                                       const bool theIsReverse)
+std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexOnEdge(
+                                            const std::shared_ptr<GeomAPI_Edge> theEdge,
+                                            const double theValue,
+                                            const bool theIsPercent,
+                                            const bool theIsReverse)
 {
+  std::shared_ptr<GeomAPI_Vertex> aVertex;
+
   if(!theEdge.get()) {
-    return NULL;
+    return aVertex;
   }
 
   double aValue = theValue;
@@ -78,7 +101,6 @@ std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexOnEdge(const std
   Standard_Real aUFirst, aULast;
   Handle(Geom_Curve) anEdgeCurve = BRep_Tool::Curve(anEdge, aUFirst, aULast);
 
-  std::shared_ptr<GeomAPI_Vertex> aVertex;
   if(!anEdgeCurve.IsNull() ) {
     Handle(Geom_Curve) aReOrientedCurve = anEdgeCurve;
 
@@ -103,28 +125,72 @@ std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexOnEdge(const std
 
 //==================================================================================================
 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
-    const std::shared_ptr<GeomAPI_Vertex> theVertex,
-    const std::shared_ptr<GeomAPI_Face> thePlane)
+  const std::shared_ptr<GeomAPI_Vertex> theVertex,
+  const std::shared_ptr<GeomAPI_Edge> theEdge)
 {
-  if(!theVertex.get() || !thePlane.get() || !thePlane->isPlanar()) {
-    return NULL;
+  std::shared_ptr<GeomAPI_Vertex> aVertex;
+
+  if (!theVertex.get() || !theEdge.get()) {
+    return aVertex;
   }
 
-  std::shared_ptr<GeomAPI_Pnt> aGeomPnt = theVertex->point();
-  gp_Pnt aPnt = aGeomPnt->impl<gp_Pnt>();
+  std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
+  gp_Pnt aPnt(aProjPnt->x(), aProjPnt->y(), aProjPnt->z());
 
-  std::shared_ptr<GeomAPI_Pln> aGeomPln = thePlane->getPlane();
-  gp_Pln aPln = aGeomPln->impl<gp_Pln>();
+  TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
+  double aFirstOnCurve, aLastOnCurve;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
+
+  if (aCurve.IsNull()) {
+    return aVertex;
+  }
 
-  gp_Dir aPntAxis = aPnt.XYZ() - aPln.Location().XYZ();
-  gp_Dir aPlnNorm = aPln.Axis().Direction();
+  GeomAPI_ProjectPointOnCurve aProjection(aPnt, aCurve);
 
-  if(aPntAxis * aPlnNorm > 0) {
-    aPlnNorm.Reverse();
+  if (aProjection.NbPoints() != 0) {
+    gp_Pnt aNearestPoint = aProjection.NearestPoint();
+    aVertex.reset(new GeomAPI_Vertex(aNearestPoint.X(), aNearestPoint.Y(), aNearestPoint.Z()));
   }
 
-  double aDistance = aPln.Distance(aPnt);
-  aPnt.Translate(gp_Vec(aPlnNorm) * aDistance);
+  return aVertex;
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
+    const std::shared_ptr<GeomAPI_Vertex> theVertex,
+    const std::shared_ptr<GeomAPI_Face> theFace)
+{
+  std::shared_ptr<GeomAPI_Vertex> aVertex;
 
-  return std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aPnt.X(), aPnt.Y(), aPnt.Z()));
+  if (theVertex.get() && theFace.get() && theFace->isPlanar()) {
+    std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
+    std::shared_ptr<GeomAPI_Pln> aProjPln = theFace->getPlane();
+
+    std::shared_ptr<GeomAPI_Pnt> aPnt = aProjPln->project(aProjPnt);
+
+    if (aPnt.get())
+      aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
+  }
+
+  return aVertex;
+}
+
+//==================================================================================================
+std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
+    const std::shared_ptr<GeomAPI_Edge> theEdge1,
+    const std::shared_ptr<GeomAPI_Edge> theEdge2)
+{
+  std::shared_ptr<GeomAPI_Vertex> aVertex;
+
+  if (theEdge1.get() && theEdge2.get() && theEdge1->isLine() && theEdge2->isLine()) {
+    std::shared_ptr<GeomAPI_Lin> aLin1 = theEdge1->line();
+    std::shared_ptr<GeomAPI_Lin> aLin2 = theEdge2->line();
+
+    std::shared_ptr<GeomAPI_Pnt> aPnt = aLin1->intersect(aLin2);
+
+    if (aPnt.get())
+      aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
+  }
+
+  return aVertex;
 }