Salome HOME
d93d858529f9c76c94bbf08d86e043de8a70ec44
[modules/med.git] / src / MEDCalc / tui / medpresentation.py
1 # Copyright (C) 2011-2016  CEA/DEN, 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 import medcalc
21 import MEDCALC, SALOME
22 from medcalc.medevents import notifyGui_addPresentation, notifyGui_removePresentation, notifyGui_error, notifyGui_modifyPresentation
23 from functools import reduce
24
25 __manager = medcalc.medcorba.factory.getPresentationManager()
26
27 def MakeMeshView(meshID,
28                  viewMode=MEDCALC.VIEW_MODE_DEFAULT,
29                  meshMode=MEDCALC.MESH_MODE_DEFAULT):
30   # Create the presentation instance in CORBA engine
31   # The engine in turn creates the ParaView pipeline elements
32   params = MEDCALC.MeshViewParameters(meshID, meshMode)
33   try:
34     presentation_id = __manager.makeMeshView(params, viewMode)
35     notifyGui_addPresentation(meshID, presentation_id)
36     return presentation_id
37   except SALOME.SALOME_Exception as e:
38     notifyGui_error("An error occurred while creating the mesh view:\n" + e.details.text)
39     raise Exception(e.details.text)
40
41
42 def MakeScalarMap(proxy,
43                   viewMode=MEDCALC.VIEW_MODE_DEFAULT,
44                   displayedComponent=MEDCALC.DISPLAY_DEFAULT,
45                   scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
46                   colorMap=MEDCALC.COLOR_MAP_DEFAULT
47                   ):
48   # Create the presentation instance in CORBA engine
49   # The engine in turn creates the ParaView pipeline elements
50   params = MEDCALC.ScalarMapParameters(proxy.id, displayedComponent, scalarBarRange, colorMap)
51   try:
52     presentation_id = __manager.makeScalarMap(params, viewMode)
53     notifyGui_addPresentation(proxy.id, presentation_id)
54     return presentation_id
55   except SALOME.SALOME_Exception as e:
56     notifyGui_error("An error occurred while creating the scalar map:\n" + e.details.text)
57     raise Exception(e.details.text)
58
59 def MakeContour(proxy,
60                 viewMode=MEDCALC.VIEW_MODE_DEFAULT,
61                 scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
62                 colorMap=MEDCALC.COLOR_MAP_DEFAULT,
63                 nbContours=MEDCALC.NB_CONTOURS_DEFAULT
64                 ):
65   params = MEDCALC.ContourParameters(proxy.id, scalarBarRange, colorMap, nbContours)
66   try:
67     presentation_id = __manager.makeContour(params, viewMode)
68     notifyGui_addPresentation(proxy.id, presentation_id)
69     return presentation_id
70   except SALOME.SALOME_Exception as e:
71     notifyGui_error("An error occurred while creating the contour:\n" + e.details.text)
72     raise Exception(e.details.text)
73
74 #
75
76 def MakeVectorField(proxy,
77                   viewMode=MEDCALC.VIEW_MODE_DEFAULT,
78                   scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
79                   colorMap=MEDCALC.COLOR_MAP_DEFAULT
80                   ):
81   # Create the presentation instance in CORBA engine
82   # The engine in turn creates the ParaView pipeline elements
83   params = MEDCALC.VectorFieldParameters(proxy.id, scalarBarRange, colorMap)
84   try:
85     presentation_id = __manager.makeVectorField(params, viewMode)
86     notifyGui_addPresentation(proxy.id, presentation_id)
87     return presentation_id
88   except SALOME.SALOME_Exception as e:
89     notifyGui_error("An error occurred while creating the vector field:\n" + e.details.text)
90     raise Exception(e.details.text)
91
92 def MakeSlices(proxy,
93                 viewMode=MEDCALC.VIEW_MODE_DEFAULT,
94                 displayedComponent=MEDCALC.DISPLAY_DEFAULT,
95                 scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
96                 colorMap=MEDCALC.COLOR_MAP_DEFAULT,
97                 sliceOrientation=MEDCALC.SLICE_ORIENTATION_DEFAULT,
98                 nbSlices=MEDCALC.NB_SLICES_DEFAULT):
99   # Create the presentation instance in CORBA engine
100   # The engine in turn creates the ParaView pipeline elements
101   params = MEDCALC.SlicesParameters(proxy.id, displayedComponent,scalarBarRange, colorMap, 
102                                     sliceOrientation, nbSlices)
103   try:
104     presentation_id = __manager.makeSlices(params, viewMode)
105     notifyGui_addPresentation(proxy.id, presentation_id)
106     return presentation_id
107   except SALOME.SALOME_Exception as e:
108     notifyGui_error("An error occurred while creating the slices:\n" + e.details.text)
109     raise Exception(e.details.text)
110
111
112 def MakeDeflectionShape(proxy,
113                   viewMode=MEDCALC.VIEW_MODE_DEFAULT,
114                   scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
115                   colorMap=MEDCALC.COLOR_MAP_DEFAULT
116                   ):
117   # Create the presentation instance in CORBA engine
118   # The engine in turn creates the ParaView pipeline elements
119   params = MEDCALC.DeflectionShapeParameters(proxy.id, scalarBarRange, colorMap)
120   try:
121     presentation_id = __manager.makeDeflectionShape(params, viewMode)
122     notifyGui_addPresentation(proxy.id, presentation_id)
123     return presentation_id
124   except SALOME.SALOME_Exception as e:
125     notifyGui_error("An error occurred while creating the deflection shape:\n" + e.details.text)
126     raise Exception(e.details.text)
127
128
129 def MakePointSprite(proxy,
130                   viewMode=MEDCALC.VIEW_MODE_DEFAULT,
131                   displayedComponent=MEDCALC.DISPLAY_DEFAULT,
132                   scalarBarRange=MEDCALC.SCALAR_BAR_RANGE_DEFAULT,
133                   colorMap=MEDCALC.COLOR_MAP_DEFAULT
134                   ):
135   # Create the presentation instance in CORBA engine
136   # The engine in turn creates the ParaView pipeline elements
137   params = MEDCALC.PointSpriteParameters(proxy.id, displayedComponent, scalarBarRange, colorMap)
138   try:
139     presentation_id = __manager.makePointSprite(params, viewMode)
140     notifyGui_addPresentation(proxy.id, presentation_id)
141     return presentation_id
142   except SALOME.SALOME_Exception as e:
143     notifyGui_error("An error occurred while creating the point sprite:\n" + e.details.text)
144     raise Exception(e.details.text)
145
146 def RemovePresentation(presentation_id):
147   ok = __manager.removePresentation(presentation_id)
148   if ok:
149     notifyGui_removePresentation(presentation_id)
150 #
151
152 def __GetGENERICParameters(tag, presentation_id):
153   exec("params = __manager.get%sParameters(presentation_id)" % tag)
154   return params
155
156 GetMeshViewParameters = lambda pres_id: __GetGENERICParameters("MeshView", pres_id)
157 GetScalarMapParameters = lambda pres_id: __GetGENERICParameters("ScalarMap", pres_id)
158 GetContourParameters = lambda pres_id: __GetGENERICParameters("Contour", pres_id)
159 GetSlicesParameters = lambda pres_id: __GetGENERICParameters("Slices", pres_id)
160 GetPointSpriteParameters = lambda pres_id: __GetGENERICParameters("PointSprite", pres_id)
161 GetVectorFieldParameters = lambda pres_id: __GetGENERICParameters("VectorField", pres_id)
162 GetDeflectionShapeParameters = lambda pres_id: __GetGENERICParameters("DeflectionShape", pres_id)
163
164
165 def __UpdateGENERIC(tag, presentation_id, params):
166   exec("__manager.update%s(presentation_id, params)" % tag)
167   notifyGui_modifyPresentation(presentation_id)
168
169 UpdateMeshView = lambda pres_id, params: __UpdateGENERIC("MeshView", pres_id, params)
170 UpdateScalarMap = lambda pres_id, params: __UpdateGENERIC("ScalarMap", pres_id, params)
171 UpdateContour = lambda pres_id, params: __UpdateGENERIC("Contour", pres_id, params)
172 UpdateSlices = lambda pres_id, params: __UpdateGENERIC("Slices", pres_id, params)
173 UpdateVectorField = lambda pres_id, params: __UpdateGENERIC("VectorField", pres_id, params)
174 UpdatePointSprite = lambda pres_id, params: __UpdateGENERIC("PointSprite", pres_id, params)
175 UpdateDeflectionShape = lambda pres_id, params: __UpdateGENERIC("DeflectionShape", pres_id, params)
176
177 def ComputeCellAverageSize(obj):
178   """
179   @return the average cell size
180   """
181   bb, nCells = obj.GetDataInformation().GetBounds(), obj.GetDataInformation().GetNumberOfCells()
182   bb = list(zip(bb[::2], bb[1::2]))
183   deltas = [x[1]-x[0] for x in bb]
184   ## Filter out null dimensions:
185   avgDelta = sum(deltas) / 3.0
186   deltas = [d for d in deltas if abs(d) > 1.0e-12*avgDelta]
187   ##
188   vol = reduce(lambda x,y:x*y, deltas, 1.0) 
189   cellSize = (vol/nCells)**(1.0/float(len(deltas)))
190   return cellSize
191
192 def GetDomainCenter(obj):
193   """
194   @return the center of the domain as the central point of the bounding box
195   """
196   bb = obj.GetDataInformation().GetBounds()
197   bb = list(zip(bb[::2], bb[1::2]))
198   mids = [x[0] + 0.5*(x[1]-x[0]) for x in bb]
199   return mids
200
201 def GetSliceOrigins(obj, nbSlices, normal):
202   """
203   Compute all origin points for the position of the slices.
204   @param normal is a list of 3 floats either 0 or 1 indicating the normal vector of the slices
205   """
206   from math import sqrt
207   bb = obj.GetDataInformation().GetBounds()
208   bb = list(zip(bb[::2], bb[1::2]))
209   origin = [x[0] + 0.5*(x[1]-x[0]) for x in bb]
210   deltas = [x[1]-x[0] for x in bb]
211   # Compute extent of slices:
212   l = [normal[i]*deltas[i]**2 for i in range(3)]   # either the X extend, or the XY diagonal, or the XYZ diagonal
213   plus = lambda x,y: x+y
214   extent = sqrt(reduce(plus, l, 0.0))
215   norm = sqrt(reduce(plus, normal, 0.0))
216   normal = [normal[i]/norm for i in range(3)]
217   origins = []
218   step = extent/nbSlices
219   for j in range(nbSlices):
220     orig_j = [origin[i]+normal[i]*(-0.5*extent + step*(0.5+j)) for i in range(3)]
221     origins.append(orig_j)
222   return origins
223
224 def SelectSourceField(obj, meshName, fieldName, discretisation):
225   """
226   Properly set the AllArrays property of a MEDReader source to point to the correct field.
227   Setting the fieldName to void string is allowed (meaning we work on the mesh only).
228   """
229   if fieldName == "":
230     return
231   tree = obj.GetProperty("FieldsTreeInfo")[::2]
232   it = None
233   for t in tree:
234     arr = t.split("/")
235     arr = arr[:-1] + arr[-1].split("@@][@@")
236     if arr[1] == meshName and arr[3] == fieldName and arr[4] == discretisation:
237       obj.AllArrays = [t]
238       return
239   raise Exception("Field not found")