Salome HOME
updated copyright message
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationSlices.cxx
1 // Copyright (C) 2016-2023  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   int id = GeneratePythonId();
41   std::ostringstream oss;
42   oss << "__objLst" << id;
43   _sliceListVar = oss.str();
44 }
45
46 void
47 MEDPresentationSlices::setSliceParametersAndGroup()
48 {
49   std::ostringstream oss;
50   int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
51   std::string normal = getNormalVector();
52
53   oss << "__origins = medcalc.GetSliceOrigins(" << _srcObjVar << ", " << nbSlices << ", " << normal << ");";
54   pushAndExecPyLine(oss.str()); oss.str("");
55
56   oss << "for sliceNum in range(" << nbSlices << "):\n";
57   oss << "  " << _sliceListVar << "[sliceNum].SliceType.Normal = " << normal << ";\n";
58   oss << "  " << _sliceListVar << "[sliceNum].SliceType.Origin = __origins[sliceNum];\n";
59   pushAndExecPyLine(oss.str()); oss.str("");
60
61   oss << _objVar << " = pvs.GroupDatasets(Input=" << _sliceListVar << ");";
62   pushAndExecPyLine(oss.str()); oss.str("");
63 }
64
65 void
66 MEDPresentationSlices::deleteGroup()
67 {
68   deleteThresholds();
69   std::ostringstream oss;
70   oss << "pvs.Hide(" << _objVar << ");";
71   oss << "pvs.Delete(" << _objVar << ");del " <<_objVar<<";";
72   pushAndExecPyLine(oss.str()); oss.str("");
73 }
74
75 void
76 MEDPresentationSlices::adaptNumberOfSlices()
77 {
78   std::ostringstream oss;
79   int nbSlices = getIntProperty(MEDPresentationSlices::PROP_NB_SLICES);
80
81   oss << "for _ in range(int(max(len(" << _sliceListVar << ")-" << nbSlices << ", 0))):\n";
82   oss << "  pvs.Delete(" << _sliceListVar << ".pop());\n";
83   pushAndExecPyLine(oss.str()); oss.str("");
84
85   oss << "for _ in range(" << nbSlices << "-int(max(len(" << _sliceListVar << "), 0))):\n";
86   oss << "  obj = pvs.Slice(Input=" << _srcObjVar << ");\n";
87   oss << "  obj.SliceType = 'Plane';\n";
88   oss << "  " << _sliceListVar << ".append(obj);\n";
89   pushAndExecPyLine(oss.str()); oss.str("");
90 }
91
92 void
93 MEDPresentationSlices::generateAndDisplay()
94 {
95   adaptNumberOfSlices();
96   setSliceParametersAndGroup();
97   recreateViewSetup();
98 }
99
100 std::string
101 MEDPresentationSlices::getNormalVector() const
102 {
103   switch(_params.orientation)
104   {
105     case MEDCALC::SLICE_NORMAL_TO_X:
106       return "[1.0, 0.0, 0.0]";
107     case MEDCALC::SLICE_NORMAL_TO_Y:
108       return "[0.0, 1.0, 0.0]";
109     case MEDCALC::SLICE_NORMAL_TO_Z:
110       return "[0.0, 0.0, 1.0]";
111     case MEDCALC::SLICE_NORMAL_TO_XY:
112       return "[1.0, 1.0, 0.0]";
113     case MEDCALC::SLICE_NORMAL_TO_XZ:
114       return "[1.0, 0.0, 1.0]";
115     case MEDCALC::SLICE_NORMAL_TO_YZ:
116       return "[0.0, 1.0, 1.0]";
117     case MEDCALC::SLICE_NORMAL_TO_XYZ:
118       return "[1.0, 1.0, 1.0]";
119     default:
120       const char * mes = "Unexpected getSliceOrientationCommand() error!";
121       STDLOG(mes);
122       throw KERNEL::createSalomeException(mes);
123   }
124   return ""; // never happens
125 }
126
127 void
128 MEDPresentationSlices::selectSliceOrientation(const std::string & obj)
129 {
130   std::ostringstream oss1;
131   oss1 << obj << ".SliceType.Normal = " << getNormalVector() << ";";
132   pushAndExecPyLine(oss1.str()); oss1.str("");
133 }
134
135 void
136 MEDPresentationSlices::internalGeneratePipeline()
137 {
138   MEDPresentation::internalGeneratePipeline();
139
140   MEDPyLockWrapper lock;
141
142   std::ostringstream oss;
143
144   createSource();
145   setTimestamp();
146
147   // Populate internal array of available components:
148   fillAvailableFieldComponents();
149   if (_params.nbSlices < 1)
150   {
151       const char * mes = "Invalid number of slices!";
152       STDLOG(mes);
153       throw KERNEL::createSalomeException(mes);
154   }
155
156   setOrCreateRenderView(); // instantiate __viewXXX
157
158   // Now create the initial slices list
159   oss << _sliceListVar << " = [];";
160   pushAndExecPyLine(oss.str()); oss.str("");
161
162   generateAndDisplay();
163 }
164
165 void
166 MEDPresentationSlices::updatePipeline(const MEDCALC::SlicesParameters& params)
167 {
168   if (params.fieldHandlerId != _params.fieldHandlerId)
169     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
170
171   if (std::string(params.displayedComponent) != std::string(_params.displayedComponent))
172     updateComponent<MEDPresentationSlices, MEDCALC::SlicesParameters>(std::string(params.displayedComponent));
173   if (params.scalarBarRange != _params.scalarBarRange ||
174       params.hideDataOutsideCustomRange != _params.hideDataOutsideCustomRange ||
175       params.scalarBarRangeArray[0] != _params.scalarBarRangeArray[0] ||
176       params.scalarBarRangeArray[1] != _params.scalarBarRangeArray[1])
177     updateScalarBarRange<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.scalarBarRange,
178                                                                            params.hideDataOutsideCustomRange,
179                                                                            params.scalarBarRangeArray[0],
180                                                                            params.scalarBarRangeArray[1]);
181   if (params.colorMap != _params.colorMap)
182     updateColorMap<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.colorMap);
183
184   if (params.orientation != _params.orientation)
185     updateOrientation(params.orientation);
186   if (params.nbSlices != _params.nbSlices)
187     {
188       if (params.nbSlices < 1)
189       {
190           const char * mes = "Invalid number of slices!";
191           STDLOG(mes);
192           throw KERNEL::createSalomeException(mes);
193       }
194       updateNbSlices(params.nbSlices);
195     }
196   if (params.visibility != _params.visibility)
197     updateVisibility<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.visibility);
198
199   if (params.scalarBarVisibility != _params.scalarBarVisibility)
200     updateScalarBarVisibility<MEDPresentationSlices, MEDCALC::SlicesParameters>(params.scalarBarVisibility);
201 }
202
203 void
204 MEDPresentationSlices::updateNbSlices(const int nbSlices)
205 {
206   _params.nbSlices = nbSlices;
207
208   // GUI helper:
209   setIntProperty(MEDPresentationSlices::PROP_NB_SLICES, nbSlices);
210
211   // Update the pipeline:
212   {
213     MEDPyLockWrapper lock;
214     deleteGroup();
215     generateAndDisplay();
216   }
217 }
218
219 void
220 MEDPresentationSlices::updateOrientation(const MEDCALC::SliceOrientationType orientation)
221 {
222   _params.orientation = orientation;
223
224   // GUI helper:
225   setIntProperty(MEDPresentationSlices::PROP_SLICE_ORIENTATION, static_cast<int>(orientation));
226
227   // Update the pipeline:
228   {
229     MEDPyLockWrapper lock;
230
231     deleteGroup();
232     generateAndDisplay();
233   }
234 }
235