Salome HOME
[MEDCalc] Proper autoscale for vector field.
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationSlices.cxx
1 // Copyright (C) 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 #include "MEDPresentationSlices.hxx"
21
22 #include <SALOME_KernelServices.hxx>
23 #undef LOG  // should be fixed in KERNEL - double definition
24 #include <Basics_Utils.hxx>
25
26 #include <sstream>
27
28 const std::string MEDPresentationSlices::TYPE_NAME = "MEDPresentationSlices";
29 const std::string MEDPresentationSlices::PROP_NB_SLICES = "nbSlices";
30 const std::string MEDPresentationSlices::PROP_SLICE_ORIENTATION = "slicesOrientation";
31
32 MEDPresentationSlices::MEDPresentationSlices(const MEDCALC::SlicesParameters& params,
33                                                const MEDCALC::ViewModeType viewMode) :
34         MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange),
35         _params(params)
36 {
37   setIntProperty(MEDPresentationSlices::PROP_NB_SLICES, params.nbSlices);
38   setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, params.orientation);
39 }
40
41 void
42 MEDPresentationSlices::generateSlices()
43 {
44   std::ostringstream oss;
45   int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
46   std::string normal = getNormalVector();
47
48   oss << "import medcalc; __origins = medcalc.GetSliceOrigins(" << _srcObjVar << ", " << nbSlices << ", " << normal << ");";
49   pushAndExecPyLine(oss.str()); oss.str("");
50   pushAndExecPyLine("__objLst = [];");
51   oss << "for sliceNum in range(" << nbSlices << "):\n";
52   oss << "  obj = pvs.Slice(Input=" << _srcObjVar << ")\n";
53   oss << "  obj.SliceType = 'Plane'\n";
54   oss << "  obj.SliceType.Normal = " << normal << "\n";
55   oss << "  obj.SliceType.Origin = __origins[sliceNum]\n";
56   oss << "  __objLst.append(obj)\n\n";
57   pushAndExecPyLine(oss.str()); oss.str("");
58
59   oss << _objVar << " = pvs.GroupDatasets(Input=__objLst);";
60   pushAndExecPyLine(oss.str()); oss.str("");
61 }
62
63 void
64 MEDPresentationSlices::generateAndDisplay()
65 {
66   generateSlices();
67   showObject();
68
69   colorBy(_fieldType);
70   showScalarBar();
71   selectColorMap();
72   rescaleTransferFunction();
73   resetCameraAndRender();
74 }
75
76
77 void
78 MEDPresentationSlices::clearPreviousSlices()
79 {
80   std::ostringstream oss;
81
82   pushAndExecPyLine("for sliceNum, _ in enumerate(__objLst):\n  pvs.Delete(__objLst[sliceNum]);");
83   oss <<            "pvs.Delete(" << _objVar << ");";
84   pushAndExecPyLine(oss.str()); oss.str("");
85 }
86
87 std::string
88 MEDPresentationSlices::getNormalVector() const
89 {
90   switch(_params.orientation)
91   {
92     case MEDCALC::SLICE_NORMAL_TO_X:
93       return "[1.0, 0.0, 0.0]";
94     case MEDCALC::SLICE_NORMAL_TO_Y:
95       return "[0.0, 1.0, 0.0]";
96     case MEDCALC::SLICE_NORMAL_TO_Z:
97       return "[0.0, 0.0, 1.0]";
98     case MEDCALC::SLICE_NORMAL_TO_XY:
99       return "[1.0, 1.0, 0.0]";
100     case MEDCALC::SLICE_NORMAL_TO_XZ:
101       return "[1.0, 0.0, 1.0]";
102     case MEDCALC::SLICE_NORMAL_TO_YZ:
103       return "[0.0, 1.0, 1.0]";
104     case MEDCALC::SLICE_NORMAL_TO_XYZ:
105       return "[1.0, 1.0, 1.0]";
106     default:
107       const char * mes = "Unexpected getSliceOrientationCommand() error!";
108       STDLOG(mes);
109       throw KERNEL::createSalomeException(mes);
110   }
111   return ""; // never happens
112 }
113
114 void
115 MEDPresentationSlices::selectSliceOrientation(const std::string & obj)
116 {
117   std::ostringstream oss1;
118   oss1 << obj << ".SliceType.Normal = " << getNormalVector() << ";";
119   pushAndExecPyLine(oss1.str()); oss1.str("");
120 }
121
122 void
123 MEDPresentationSlices::internalGeneratePipeline()
124 {
125   MEDPresentation::internalGeneratePipeline();
126
127   MEDPyLockWrapper lock;
128
129   std::ostringstream oss_o, oss;
130
131   createSource();
132
133   // Populate internal array of available components:
134   fillAvailableFieldComponents();
135   setOrCreateRenderView(); // instanciate __viewXXX
136
137   // Now create the initial number of slices
138   generateAndDisplay();
139 }
140
141 void
142 MEDPresentationSlices::updatePipeline(const MEDCALC::SlicesParameters& params)
143 {
144   if (params.fieldHandlerId != _params.fieldHandlerId)
145     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
146
147   if (std::string(params.displayedComponent) != std::string(_params.displayedComponent))
148     updateComponent<MEDPresentationSlices, MEDCALC::SlicesParameters>(std::string(params.displayedComponent));
149   if (params.scalarBarRange != _params.scalarBarRange)
150     updateScalarBarRange<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.scalarBarRange);
151   if (params.colorMap != _params.colorMap)
152     updateColorMap<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.colorMap);
153
154   if (params.orientation != _params.orientation)
155     updateOrientation(params.orientation);
156   if (params.nbSlices != _params.nbSlices)
157     updateNbSlices(params.nbSlices);
158 }
159
160 void
161 MEDPresentationSlices::updateNbSlices(const int nbSlices)
162 {
163   _params.nbSlices = nbSlices;
164
165   // GUI helper:
166   setIntProperty(MEDPresentationSlices::PROP_NB_SLICES, nbSlices);
167
168   // Update the pipeline:
169   {
170     MEDPyLockWrapper lock;
171     clearPreviousSlices();
172     generateAndDisplay();
173   }
174 }
175
176 void
177 MEDPresentationSlices::updateOrientation(const MEDCALC::SliceOrientationType orientation)
178 {
179   _params.orientation = orientation;
180
181   // GUI helper:
182   setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, static_cast<int>(orientation));
183
184   // Update the pipeline:
185   {
186     MEDPyLockWrapper lock;
187
188     clearPreviousSlices();
189     generateAndDisplay();
190   }
191 }
192