Salome HOME
Issue #2811: Update content of Object node on creation moment
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_PointBuilder.cpp
index b5db2869b32a40415f0e8b317c584bd4f51f40af..ce7be932c4557724520697e7176ff191817a03d1 100644 (file)
@@ -1,8 +1,22 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        GeomAlgoAPI_PointBuilder.cpp
-// Created:     02 Jun 2014
-// Author:      Mikhail PONIKAROV
+// Copyright (C) 2014-2017  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<mailto:webmaster.salome@opencascade.com>
+//
 
 #include "GeomAlgoAPI_PointBuilder.h"
 
 #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);
@@ -50,7 +67,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)) {
@@ -63,10 +81,11 @@ 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;
 
@@ -107,32 +126,60 @@ 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)
 {
   std::shared_ptr<GeomAPI_Vertex> aVertex;
 
-  if(!theVertex.get() || !thePlane.get() || !thePlane->isPlanar()) {
+  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());
+
+  TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
+  double aFirstOnCurve, aLastOnCurve;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
 
-  std::shared_ptr<GeomAPI_Pln> aGeomPln = thePlane->getPlane();
-  gp_Pln aPln = aGeomPln->impl<gp_Pln>();
+  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) {
+    return aVertex;
   }
 
-  double aDistance = aPln.Distance(aPnt);
-  aPnt.Translate(gp_Vec(aPlnNorm) * aDistance);
+  gp_Pnt aNearestPoint = aProjection.NearestPoint();
 
-  aVertex.reset(new GeomAPI_Vertex(aPnt.X(), aPnt.Y(), aPnt.Z()));
+  aVertex.reset(new GeomAPI_Vertex(aNearestPoint.X(), aNearestPoint.Y(), aNearestPoint.Z()));
+
+  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;
+
+  if(!theVertex.get() || !theFace.get() || !theFace->isPlanar()) {
+    return aVertex;
+  }
+
+  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()) {
+    return aVertex;
+  }
+
+  aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
 
   return aVertex;
 }
@@ -148,32 +195,41 @@ std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
     return aVertex;
   }
 
-  gp_Lin aLin1 = theEdge1->line()->impl<gp_Lin>();
-  gp_Lin aLin2 = theEdge2->line()->impl<gp_Lin>();
+  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(aLin1.Distance(aLin2) > Precision::Confusion()) {
+  if(!aPnt.get()) {
     return aVertex;
   }
 
-  Handle(Geom_Line) aLine1 = new Geom_Line(aLin1);
-  Handle(Geom_Line) aLine2 = new Geom_Line(aLin2);
+  aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
 
-  GeomAPI_ExtremaCurveCurve anExtrema(aLine1, aLine2);
+  return aVertex;
+}
 
-  Standard_Integer aNbExtrema = anExtrema.NbExtrema();
+//==================================================================================================
+std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByIntersection(
+    const std::shared_ptr<GeomAPI_Edge> theEdge,
+    const std::shared_ptr<GeomAPI_Face> theFace)
+{
+  std::shared_ptr<GeomAPI_Vertex> aVertex;
 
-  if(aNbExtrema == 0) {
+  if(!theEdge.get() || !theFace.get() || !theEdge->isLine() || !theFace->isPlanar()) {
     return aVertex;
   }
 
-  gp_Pnt aPnt1, aPnt2;
-  for(Standard_Integer anIndex = 1; anIndex <= aNbExtrema; ++anIndex) {
-    if(anExtrema.Distance(anIndex) <= Precision::Confusion()) {
-      anExtrema.Points(anIndex, aPnt1, aPnt2);
-    }
+  std::shared_ptr<GeomAPI_Lin> aLin = theEdge->line();
+  std::shared_ptr<GeomAPI_Pln> aPln = theFace->getPlane();
+
+  std::shared_ptr<GeomAPI_Pnt> aPnt = aPln->intersect(aLin);
+
+  if(!aPnt.get()) {
+    return aVertex;
   }
 
-  aVertex.reset(new GeomAPI_Vertex(aPnt1.X(), aPnt1.Y(), aPnt1.Z()));
+  aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
 
   return aVertex;
 }