Salome HOME
Update copyrights
[modules/smesh.git] / src / Tools / blocFissure / gmu / whichSide.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2014-2019  CEA/DEN, 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 import logging
22 from .geomsmesh import geompy
23
24 # -----------------------------------------------------------------------------
25 # --- calcul de la position d'une shape par rapport à une face (dessus, dessous, sur la surface même)
26
27 def whichSide(face, obj, tol = 1.e-3):
28   """
29   Position d'un objet par rapport a une face (non nécessairement plane).
30   L'objet est supposé situé entierement d'un coté de la face,
31   ou lui appartenant totalement (objets traversants non pris en compte)
32   renvoie 1 si 'objet est du coté de la normale à la face,
33   -1 de l'autre coté, 0 si il est sur la face
34   """
35   logging.debug('start')
36   side = 0
37   logging.debug("shape info %s", geompy.ShapeInfo(obj))
38   nbEdges = geompy.NbShapes(obj, geompy.ShapeType["EDGE"]) # --- attention ! pour une seule edge presente, renvoie 2
39   logging.debug("   nbEdges %s", nbEdges)
40   nbFaces = geompy.NbShapes(obj, geompy.ShapeType["FACE"]) # --- attention ! pour une seule face presente, renvoie 2
41   logging.debug("   nbFaces %s", nbFaces)
42   vertices = geompy.ExtractShapes(obj, geompy.ShapeType["VERTEX"], False)
43   if nbEdges > 0 and nbFaces == 0: # --- edges
44     if nbEdges <= 2:
45       point = geompy.MakeVertexOnCurve(obj, 0.5)
46       vertices.append(point)
47     else:
48       edges = geompy.ExtractShapes(obj, geompy.ShapeType["EDGE"], False)
49       for anEdge in edges:
50         point = geompy.MakeVertexOnCurve(anEdge, 0.5)
51         vertices.append(point)
52   elif nbFaces >0: # --- faces
53     if nbFaces <=2:
54       point = geompy.MakeVertexOnSurface(obj, 0.5, 0.5)
55       vertices.append(point)
56     if nbFaces > 2:
57       faces = geompy.ExtractShapes(obj, geompy.ShapeType["FACE"], False)
58       for aFace in faces:
59         point = geompy.MakeVertexOnSurface(aFace, 0.5, 0.5)
60         vertices.append(point)
61   else: # --- vertices
62     vertices = [obj]
63   for vertex in vertices:
64     distance = geompy.MinDistance(vertex, face)
65     logging.debug("    distance %s", distance)
66     if distance > tol:
67       projection = geompy.MakeProjection(vertex, face)
68       normal = geompy.GetNormal(face, projection)
69       vect = geompy.MakeVector(projection, vertex)
70       angle = geompy.GetAngle(normal, vect)
71       logging.debug("  angle %s", angle)
72       side = 1
73       if abs(angle) > 10:
74         side = -1
75       break
76   logging.debug("  side %s", side)
77   return side
78