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