Salome HOME
simplification
[modules/smesh.git] / src / Tools / blocFissure / gmu / projettePointSurCourbe.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2014-2021  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 """Projection d'un point sur une courbe."""
21
22 import logging
23
24 from .geomsmesh import geompy
25
26 def projettePointSurCourbe(point, edge):
27   """
28   projection d'un point p sur une courbe c
29   on suppose que la distance (c(u), p) passe par un minimum quand u varie entre 0 et 1
30   et qu'elle presente pas de minimum local
31   """
32   #logging.debug("start")
33
34   dist = list()
35   nb_slices = 50
36   delta = 1.0/float(nb_slices)
37   for i_aux in range(nb_slices + 1):
38     pti = geompy.MakeVertexOnCurve(edge, delta*float(i_aux))
39     dpti = geompy.MinDistance(pti,point)
40     dist.append((dpti,i_aux))
41   dist.sort()
42
43   #logging.debug("dist %s", dist)
44   umin = delta*dist[0][1]
45   umax = delta*dist[1][1]
46   #umin = 0.0
47   #umax = 1.0
48   tol = 1.e-8
49   pmin = geompy.MakeVertexOnCurve(edge, umin)
50   pmax = geompy.MakeVertexOnCurve(edge, umax)
51   dmin = geompy.MinDistance(pmin,point)
52   dmax = geompy.MinDistance(pmax,point)
53   dext = geompy.MinDistance(pmin,pmax)
54   i_aux = 0
55   while ( ( dext > tol ) and ( i_aux < 100 ) ):
56     i_aux += 1
57     utest = (umax + umin) / 2.0
58     ptest = geompy.MakeVertexOnCurve(edge, utest)
59     dtest = geompy.MinDistance(ptest,point)
60     if dmin < dmax:
61       umax = utest
62       pmax = ptest
63       dmax = dtest
64     else:
65       umin = utest
66       pmin = ptest
67       dmin = dtest
68     dext = geompy.MinDistance(pmin,pmax)
69     #logging.debug('umin=%s umax=%s dmin=%s dmax=%s dtest=%s dext=%s', umin,umax,dmin,dmax,dtest,dext)
70
71   if ( abs(utest) < 1.e-7 ):
72     utest = 0.0
73   elif ( abs(1.0-utest) < 1.e-7 ):
74     utest = 1.0
75   logging.debug('u=%s, nbiter=%s dtest=%s dext=%s',utest,i_aux,dtest,dext)
76
77   return utest