Salome HOME
Copyrights update 2015.
[modules/smesh.git] / src / Tools / MacMesh / MacMesh / SharpAngle.py
1 # Copyright (C) 2014-2015  EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20
21
22 # This is an automation of the sharp angle object, with a corner at (X0,Y0), side length : Extension and a fine local meshing : LocalMeshing
23 # The corner orientation is defined as NE (North-East) , NW (North-West), SE, or SW. The object's "arm" is 8/14 of Extension
24 #                       |      |     8      6
25 #                -------        ---------
26 #       ---->   |                        |   <----
27 #               |   NW             NE    |  oo
28 #          _____|                        |_____
29
30 import sys, math, commands
31 CWD = commands.getoutput('pwd')
32 sys.path.append(CWD)
33
34 from MacObject import *
35 from CompositeBox import *
36 import Config, GenFunctions
37
38 def SharpAngleOut (X0 , Y0 , DX , DY , DLocal, LocalMeshing , CornerOrientation , NLevels, **args) : 
39         if DLocal == 'auto' : DLocal = float(min(DX,DY))
40
41         BoxSide = DLocal/(2.**(NLevels+1))
42         InternalMeshing = int(math.ceil(BoxSide/(3*LocalMeshing)))
43         InternalMeshing = InternalMeshing+InternalMeshing%2     # An even number is needed, otherwise the objects would not be compatible once created
44         if InternalMeshing == 0 : InternalMeshing = 2           # This sets a minimum meshing condition in order to avoid an error. The user is notified of the value considered for the local meshing
45         print "Possible Local meshing is :", BoxSide/(3*InternalMeshing), "\nThis value is returned by this function for your convenience"
46
47         DirPar =     {'NE' : lambda : ['NE', 'NW', 'SE', 'EW', 'NW', 'SN', 'SN', 'NE', 'WE', 'WE', 'SE', 'NS'],
48                       'NW' : lambda : ['NW', 'NE', 'SW', 'WE', 'NE', 'SN', 'SN', 'NW', 'EW', 'EW', 'SW', 'NS'],
49                       'SE' : lambda : ['SE', 'SW', 'NE', 'EW', 'SW', 'NS', 'NS', 'SE', 'WE', 'WE', 'NE', 'SN'],
50                       'SW' : lambda : ['SW', 'SE', 'NW', 'WE', 'SE', 'NS', 'NS', 'SW', 'EW', 'EW', 'NW', 'SN'], }[CornerOrientation]()
51
52         CoefVer =   {'NE' : lambda :  1,
53                      'NW' : lambda :  1,
54                      'SE' : lambda : -1,
55                      'SW' : lambda : -1, }[CornerOrientation]()
56
57         CoefHor =   {'NE' : lambda :  1,
58                      'NW' : lambda : -1,
59                      'SE' : lambda :  1,
60                      'SW' : lambda : -1, }[CornerOrientation]()
61                      
62         ToLook  = {'NE' : lambda : [0,2,1,3],
63                    'NW' : lambda : [0,3,1,2],
64                    'SE' : lambda : [1,2,0,3],
65                    'SW' : lambda : [1,3,0,2], }[CornerOrientation]()        
66        
67         if args.__contains__('groups') :
68                 GroupNames = args['groups']
69         else : GroupNames = [None, None, None, None, None, None]
70         
71         GN00 = GroupArray(ToLook[0],GroupNames[0])
72         GN01 = GroupArray(ToLook[1],GroupNames[1])
73         
74         GN1 = GroupArray([ToLook[0],ToLook[1]],[GroupNames[0],GroupNames[5]])
75         GN7 = GroupArray([ToLook[0],ToLook[1]],[GroupNames[4],GroupNames[1]])
76         
77         if DY == DLocal :
78                 GN2 = GroupArray([ToLook[1],ToLook[2]],[GroupNames[5],GroupNames[2]])
79                 GN3 = GroupArray(ToLook[2],GroupNames[2])
80                 if DX == DLocal:
81                         GN4 = GroupArray([ToLook[2],ToLook[3]],[GroupNames[2],GroupNames[3]])
82                         GN5 = GroupArray(ToLook[3],GroupNames[3])
83                         GN6 = GroupArray([ToLook[3],ToLook[0]],[GroupNames[3],GroupNames[4]])
84                 else :
85                         GN4 = GroupArray(ToLook[2],GroupNames[2])
86                         GN5 = [None,None,None,None]
87                         GN6 = GroupArray(ToLook[0],GroupNames[4])
88                         GN21 = GroupArray([ToLook[3],ToLook[0],ToLook[2]],[GroupNames[3],GroupNames[4],GroupNames[2]])
89         else :
90                 GN2 = GroupArray(ToLook[1],GroupNames[5])
91                 GN3 = [None,None,None,None]
92                 if DX == DLocal:
93                         GN4 = GroupArray(ToLook[3],GroupNames[3])
94                         GN5 = GroupArray(ToLook[3],GroupNames[3])
95                         GN6 = GroupArray([ToLook[3],ToLook[0]],[GroupNames[3],GroupNames[4]])
96                         GN22 = GroupArray([ToLook[1],ToLook[2],ToLook[3]],[GroupNames[5],GroupNames[2],GroupNames[3]])
97                 else :
98                         GN4 = [None,None,None,None]
99                         GN5 = [None,None,None,None]
100                         GN6 = GroupArray(ToLook[0],GroupNames[4])
101                         GN21 = GroupArray([ToLook[3],ToLook[0]],[GroupNames[3],GroupNames[4]])
102                         GN22 = GroupArray([ToLook[1],ToLook[2]],[GroupNames[5],GroupNames[2]])
103                         GN23 = GroupArray([ToLook[2],ToLook[3]],[GroupNames[2],GroupNames[3]])
104                         
105         Obj = []
106
107         Obj.append(MacObject('BoxAng32',[(X0+CoefHor*BoxSide/2,Y0+CoefVer*BoxSide/2),(BoxSide,BoxSide)],[InternalMeshing,DirPar[0]]))
108         Obj.append(MacObject('BoxAng32',[(X0-CoefHor*BoxSide/2,Y0+CoefVer*BoxSide/2),(BoxSide,BoxSide)],['auto',DirPar[1]], groups = GroupArray(ToLook[0],GroupNames[0])))
109         Obj.append(MacObject('BoxAng32',[(X0+CoefHor*BoxSide/2,Y0-CoefVer*BoxSide/2),(BoxSide,BoxSide)],['auto',DirPar[2]], groups = GroupArray(ToLook[1],GroupNames[1])))
110         
111         for N in range (1,NLevels+1):
112                 n = N-1
113                 if N < NLevels : 
114                        Obj.append(MacObject('Box42',[(X0-CoefHor*BoxSide*(2**n)*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[3]]      , groups = GN00))
115                        Obj.append(MacObject('BoxAng32',[(X0-CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[4]]               ))
116                        Obj.append(MacObject('Box42',[(X0-CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[5]]                    ))
117                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[6]]                    ))
118                        Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[7]]               ))
119                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[8]]                    ))
120                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0-CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[9]]                    ))
121                        Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0-CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[10]]              ))
122                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0-CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[11]]     , groups = GN01))
123                 else :
124                        Obj.append(MacObject('Box42',[(X0-CoefHor*BoxSide*(2**n)*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[3]]      , groups = GN1))
125                        Obj.append(MacObject('BoxAng32',[(X0-CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[4]] , groups = GN2))
126                        Obj.append(MacObject('Box42',[(X0-CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[5]]      , groups = GN3))
127                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[6]]      , groups = GN3))
128                        Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[7]] , groups = GN4))
129                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[8]]      , groups = GN5))
130                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0-CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[9]]      , groups = GN5))
131                        Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0-CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[10]], groups = GN6))
132                        Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0-CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[11]]     , groups = GN7))
133                        
134         OuterMeshing = (3/2)*InternalMeshing*2**(NLevels-1)
135         OuterSegLength = (DLocal/OuterMeshing)
136
137         if DX > DLocal :
138                 dX = DX - DLocal
139                 Obj.append(MacObject('CompBoxF',[(X0+CoefHor*(DX)/2.,Y0),(dX,DLocal)],['auto'], groups = GN21))
140         if DY > DLocal :
141                 dY = DY - DLocal
142                 if DX > DLocal : 
143                         Obj.append(MacObject('CompBoxF',[(X0+CoefHor*DX/2.,Y0+CoefVer*(DY)/2.),(DX-DLocal,dY)],['auto'], groups = GN23))
144
145                 Obj.append(MacObject('CompBoxF',[(X0,Y0+CoefVer*(DY)/2.),(DLocal,dY)],['auto'], groups = GN22))
146                 
147         return Obj
148
149 def SharpAngleIn (X0 , Y0 , DX , DY , DLocal, LocalMeshing , CornerOrientation , NLevels, **args) : 
150         if DLocal == 'auto' : DLocal = float(min(DX,DY))
151
152         BoxSide = DLocal/(2.**(NLevels))
153         InternalMeshing = int(math.ceil(BoxSide/(3*LocalMeshing)))
154         InternalMeshing = InternalMeshing+InternalMeshing%2     # An even number is needed, otherwise the objects would not be compatible once created
155         if InternalMeshing == 0 : InternalMeshing = 2           # This sets a minimum meshing condition in order to avoid an error. The user is notified of the value considered for the local meshing
156         print "Possible Local meshing is :", BoxSide/(3*InternalMeshing), "\nThis value is returned by this function for your convenience..."
157
158         DirPar =     {'NE' : lambda : ['NE', 'SN', 'NE', 'WE'],
159                       'NW' : lambda : ['NW', 'SN', 'NW', 'EW'],
160                       'SE' : lambda : ['SE', 'NS', 'SE', 'WE'],
161                       'SW' : lambda : ['SW', 'NS', 'SW', 'EW'], }[CornerOrientation]()
162
163         CoefVer =   {'NE' : lambda :  1,
164                      'NW' : lambda :  1,
165                      'SE' : lambda : -1,
166                      'SW' : lambda : -1, }[CornerOrientation]()
167
168         CoefHor =   {'NE' : lambda :  1,
169                      'NW' : lambda : -1,
170                      'SE' : lambda :  1,
171                      'SW' : lambda : -1, }[CornerOrientation]()
172                      
173         ToLook  = {'NE' : lambda : [0,2,1,3],
174                    'NW' : lambda : [0,3,1,2],
175                    'SE' : lambda : [1,2,0,3],
176                    'SW' : lambda : [1,3,0,2], }[CornerOrientation]()
177                                                
178         if args.__contains__('groups') :
179                 GroupNames = args['groups']
180         else : GroupNames = [None, None, None, None]
181         
182         GN01 = GroupArray([ToLook[0],ToLook[1]],[GroupNames[ToLook[0]],GroupNames[ToLook[1]]])
183         GN02 = GroupArray(ToLook[1],GroupNames[ToLook[1]])        
184         GN03 = [None, None, None, None]
185         GN04 = GroupArray(ToLook[0],GroupNames[ToLook[0]])  
186
187         if DY == DLocal :
188                 GN05 = GroupArray([ToLook[1],ToLook[2]],[GroupNames[ToLook[1]],GroupNames[ToLook[2]]])
189                 GN08 = GroupArray([ToLook[0],ToLook[2],ToLook[3]],[GroupNames[ToLook[0]],GroupNames[ToLook[2]],GroupNames[ToLook[3]]])
190                 if DX == DLocal:
191                         GN06 = GroupArray([ToLook[2],ToLook[3]],[GroupNames[ToLook[2]],GroupNames[ToLook[3]]])
192                         GN07 = GroupArray([ToLook[0],ToLook[3]],[GroupNames[ToLook[0]],GroupNames[ToLook[3]]])
193                 else :
194                         GN06 = GroupArray(ToLook[2],GroupNames[ToLook[2]])
195                         GN07 = GroupArray(ToLook[0],GroupNames[ToLook[0]])
196         else :
197                 GN05 = GroupArray(ToLook[1],GroupNames[ToLook[1]])
198                 if DX == DLocal : 
199                         GN06 = GroupArray(ToLook[3],GroupNames[ToLook[3]])
200                         GN07 = GroupArray([ToLook[0],ToLook[3]],[GroupNames[ToLook[0]],GroupNames[ToLook[3]]])
201                         GN10 = GroupArray([ToLook[1],ToLook[2],ToLook[3]],[GroupNames[ToLook[1]],GroupNames[ToLook[2]],GroupNames[ToLook[3]]])
202                 else :
203                         GN06 = [None, None, None, None]
204                         GN07 = GroupArray(ToLook[0],GroupNames[ToLook[0]])
205                         GN08 = GroupArray([ToLook[0],ToLook[3]],[GroupNames[ToLook[0]],GroupNames[ToLook[3]]])
206                         GN09 = GroupArray([ToLook[2],ToLook[3]],[GroupNames[ToLook[2]],GroupNames[ToLook[3]]])
207                         GN10 = GroupArray([ToLook[1],ToLook[2]],[GroupNames[ToLook[1]],GroupNames[ToLook[2]]])
208          
209         Obj = []
210
211         Obj.append(MacObject('BoxAng32',[(X0+CoefHor*BoxSide/2,Y0+CoefVer*BoxSide/2),(BoxSide,BoxSide)],[InternalMeshing,DirPar[0]],groups = GN01))
212         
213         for N in range (1,NLevels+1):
214                 n = N-1
215                 if N < NLevels : 
216                         Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[1]],groups = GN02))
217                         Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[2]],groups = GN03))
218                         Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[3]],groups = GN04))
219                 else :
220                         Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[1]],groups = GN05))
221                         Obj.append(MacObject('BoxAng32',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide*3/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[2]],groups = GN06))
222                         Obj.append(MacObject('Box42',[(X0+CoefHor*(2**n)*BoxSide*3/2,Y0+CoefVer*(2**n)*BoxSide/2),((2**n)*BoxSide,(2**n)*BoxSide)],['auto',DirPar[3]],groups = GN07))
223  
224         OuterMeshing = (3/2)*InternalMeshing*2**(NLevels-1)
225         OuterSegLength = (DLocal/OuterMeshing)
226
227         if DX > DLocal :
228                 dX = DX - DLocal
229                 Obj = Obj + CompositeBox(X0+CoefHor*(DLocal+dX/2.),Y0+CoefVer*(DLocal)/2.,dX,DLocal, groups = GN08)
230         if DY > DLocal :
231                 dY = DY - DLocal
232
233                 if DX > DLocal : 
234                         Obj = Obj + CompositeBox(X0+CoefHor*(DLocal+(DX-DLocal)/2.),Y0+CoefVer*(DLocal+dY/2.),DX-DLocal,dY, groups = GN09)
235
236                 Obj = Obj + CompositeBox(X0+CoefHor*DLocal/2,Y0+CoefVer*(DLocal+dY/2.),DLocal,dY,groups = GN10)
237
238         return Obj
239
240 def GroupArray(indices, GroupNames) :
241         if type(indices) is int : 
242                 indices = [indices]
243                 GroupNames = [GroupNames]
244         Output = [None,None,None,None]
245         for i, ind in enumerate(indices) : 
246                 Output[ind] = GroupNames[i]
247         return Output