Salome HOME
Copyright update 2020
[modules/smesh.git] / src / Tools / blocFissure / gmu / projettePointSurCourbe.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2014-2020  EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 from .geomsmesh import geompy
22 import logging
23 import math
24
25 # -----------------------------------------------------------------------------
26 # --- projection d'un point sur une courbe.
27
28 def projettePointSurCourbe(pt, edge):
29   """
30   projection d'un point p sur une courbe c
31   on suppose que la distance (c(u), p) passe par un minimum quand u varie entre 0 et 1
32   et qu'elle presente pas de minimum local 
33   """
34   #logging.debug("start")
35
36   dist = []
37   nbSlices = 50
38   du = 1.0/nbSlices
39   for i in range(nbSlices + 1):
40     p = geompy.MakeVertexOnCurve(edge, du*i)
41     d = geompy.MinDistance(p,pt)
42     dist.append((d,i))
43   dist.sort()
44   #logging.debug("dist %s", dist)
45   umin = du*dist[0][1]
46   umax = du*dist[1][1]
47   #umin = 0.0
48   #umax = 1.0
49   tol = 1.e-8
50   pmin = geompy.MakeVertexOnCurve(edge, umin)
51   pmax = geompy.MakeVertexOnCurve(edge, umax)
52   dmin = geompy.MinDistance(pmin,pt)
53   dmax = geompy.MinDistance(pmax,pt)
54   dext = geompy.MinDistance(pmin,pmax)
55   i=0
56   while dext > tol and i < 100 :
57     i = i+1
58     utest = (umax + umin) / 2.0
59     ptest = geompy.MakeVertexOnCurve(edge, utest)   
60     dtest = geompy.MinDistance(ptest,pt)
61     if dmin < dmax:
62       umax = utest
63       pmax = ptest
64       dmax = dtest
65     else:
66       umin = utest
67       pmin = ptest
68       dmin = dtest
69     dext = geompy.MinDistance(pmin,pmax)
70     #logging.debug('umin=%s umax=%s dmin=%s dmax=%s dtest=%s dext=%s', umin,umax,dmin,dmax,dtest,dext)
71   if abs(utest) < 1.e-7:
72     utest = 0.0
73   if abs(1.0-utest) < 1.e-7:
74     utest = 1.0
75   logging.debug('u=%s, nbiter=%s dtest=%s dext=%s',utest,i,dtest,dext)
76   return utest
77