Salome HOME
02ad9e676558b1a93ff0b6e704f895966a7050ff
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationCutSegment.cxx
1 // Copyright (C) 2016-2022  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 "MEDPyLockWrapper.hxx"
21
22 #include "MEDPresentationCutSegment.hxx"
23 #include "MEDPresentationException.hxx"
24
25 #include <SALOME_KernelServices.hxx>
26 #undef LOG  // should be fixed in KERNEL - double definition
27 #include <Basics_Utils.hxx>
28
29 #include <sstream>
30
31 const std::string MEDPresentationCutSegment::TYPE_NAME = "MEDPresentationCutSegment";
32 const std::string MEDPresentationCutSegment::PROP_POINT1_X = "cutPoint1_X";
33 const std::string MEDPresentationCutSegment::PROP_POINT1_Y = "cutPoint1_Y";
34 const std::string MEDPresentationCutSegment::PROP_POINT1_Z = "cutPoint1_Z";
35 const std::string MEDPresentationCutSegment::PROP_POINT2_X = "cutPoint2_X";
36 const std::string MEDPresentationCutSegment::PROP_POINT2_Y = "cutPoint2_Y";
37 const std::string MEDPresentationCutSegment::PROP_POINT2_Z = "cutPoint2_Z";
38
39
40 MEDPresentationCutSegment::MEDPresentationCutSegment(const MEDCALC::CutSegmentParameters& params,
41                                                    const MEDCALC::ViewModeType viewMode) :
42     MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange),
43     _params(params)
44 {
45   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT1_X, params.point1[0]);
46   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT1_Y, params.point1[1]);
47   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT1_Z, params.point1[2]);
48   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT2_X, params.point2[0]);
49   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT2_Y, params.point2[1]);
50   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT2_Z, params.point2[2]);
51   
52   int id = GeneratePythonId();
53   std::ostringstream oss_ch;
54   oss_ch << "__chartViewDisp" << id;
55   _chartViewDispVar = oss_ch.str();
56 }
57
58 MEDPresentationCutSegment::~MEDPresentationCutSegment() {
59   //3D Presentation is erased by base class descructor,
60   // hide only 2D Presentation here:
61   std::ostringstream oss;
62   oss << "pvs.Hide(" << _objVar << ", " << getChartViewVar() << ");";
63   oss << getChartViewVar() << ".Update();";
64   pushAndExecPyLine(oss.str());
65 }
66
67 void
68 MEDPresentationCutSegment::internalGeneratePipeline()
69 {
70   MEDPresentation::internalGeneratePipeline();
71
72   MEDPyLockWrapper lock;
73
74   createSource();
75   setTimestamp();
76
77   fillAvailableFieldComponents();
78   setOrCreateRenderView();
79   setOrCreateChartView();
80
81   std::ostringstream oss;
82   oss << _objVar << "= pvs.PlotOverLine(Input=" << _srcObjVar << ");";
83   pushAndExecPyLine(oss.str()); oss.str("");
84   // Get bouding box "min" and "max" as initial values of 
85   oss << "P1 = " << _objVar << ".Source.Point1.GetData()"; // Source.Point1 is paraview.servermanager.VectorProperty, so call GetData()
86   execPyLine(oss.str()); oss.str("");
87   PyObject * obj1 = getPythonObjectFromMain("P1");
88   if (obj1 && PyList_Check(obj1)) {
89     PyObject* objP0 = PyList_GetItem(obj1, 0);
90     PyObject* objP1 = PyList_GetItem(obj1, 1);
91     PyObject* objP2 = PyList_GetItem(obj1, 2);
92     if (PyFloat_Check(objP0) && PyFloat_Check(objP1) && PyFloat_Check(objP2)) {
93       _params.point1[0] = PyFloat_AsDouble(objP0);
94       _params.point1[1] = PyFloat_AsDouble(objP1);
95       _params.point1[2] = PyFloat_AsDouble(objP2);
96     }
97   }
98
99   oss << "P2 = " << _objVar << ".Source.Point2.GetData()"; // Source.Point2 is paraview.servermanager.VectorProperty, so call GetData()
100   execPyLine(oss.str()); oss.str("");
101   PyObject * obj2 = getPythonObjectFromMain("P2");
102   if (obj2 && PyList_Check(obj2)) {
103     PyObject* objP0 = PyList_GetItem(obj2, 0);
104     PyObject* objP1 = PyList_GetItem(obj2, 1);
105     PyObject* objP2 = PyList_GetItem(obj2, 2);
106     if (PyFloat_Check(objP0) && PyFloat_Check(objP1) && PyFloat_Check(objP2)) {
107       _params.point2[0] = PyFloat_AsDouble(objP0);
108       _params.point2[1] = PyFloat_AsDouble(objP1);
109       _params.point2[2] = PyFloat_AsDouble(objP2);
110     }
111   }
112
113   // To update GUI
114   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT1_X, _params.point1[0]);
115   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT1_Y, _params.point1[1]);
116   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT1_Z, _params.point1[2]);
117   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT2_X, _params.point2[0]);
118   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT2_Y, _params.point2[1]);
119   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT2_Z, _params.point2[2]);
120
121   showObject();
122   colorBy();    // see initFieldInfo() - necessarily POINTS because of the conversion above
123   showScalarBar();
124   selectColorMap();
125   rescaleTransferFunction();
126   resetCameraAndRender();
127 }
128
129 void
130 MEDPresentationCutSegment::updatePipeline(const MEDCALC::CutSegmentParameters& params)
131 {
132   if (params.fieldHandlerId != _params.fieldHandlerId)
133     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
134
135   if (params.colorMap != _params.colorMap)
136     updateColorMap<MEDPresentationCutSegment, MEDCALC::CutSegmentParameters>(params.colorMap);
137
138   if (params.scalarBarRange != _params.scalarBarRange ||
139     params.hideDataOutsideCustomRange != _params.hideDataOutsideCustomRange ||
140     params.scalarBarRangeArray[0] != _params.scalarBarRangeArray[0] ||
141     params.scalarBarRangeArray[1] != _params.scalarBarRangeArray[1])
142     updateScalarBarRange<MEDPresentationCutSegment, MEDCALC::CutSegmentParameters>(params.scalarBarRange,
143       params.hideDataOutsideCustomRange,
144       params.scalarBarRangeArray[0],
145       params.scalarBarRangeArray[1]);
146
147   if (params.point1[0] != _params.point1[0] ||
148       params.point1[2] != _params.point1[1] ||
149       params.point1[1] != _params.point1[2])
150     updatePoint1(params.point1);
151
152   if (params.point2[0] != _params.point2[0] ||
153       params.point2[2] != _params.point2[1] ||
154       params.point2[1] != _params.point2[2])
155     updatePoint2(params.point2);
156
157   if (params.visibility != _params.visibility)
158     updateVisibility<MEDPresentationCutSegment, MEDCALC::CutSegmentParameters>(params.visibility);
159   if (params.scalarBarVisibility != _params.scalarBarVisibility)
160     updateScalarBarVisibility<MEDPresentationCutSegment, MEDCALC::CutSegmentParameters>(params.scalarBarVisibility);
161 }
162
163 void
164 MEDPresentationCutSegment::updatePoint1(MEDCALC::DoubleArray point1)
165 {
166   _params.point1[0] = point1[0];
167   _params.point1[1] = point1[1];
168   _params.point1[2] = point1[2];
169
170   // GUI helper:
171   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT1_X, point1[0]);
172   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT1_Y, point1[1]);
173   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT1_Z, point1[2]);
174   
175   // Update Point1
176   std::ostringstream oss;
177   oss << _objVar << ".Source.Point1 = [" <<  
178       _params.point1[0] << ", " <<
179       _params.point1[1] << ", " <<
180       _params.point1[2] << "];";
181     pushAndExecPyLine(oss.str()); oss.str("");
182     oss << getChartViewVar() << ".Update();";
183     oss << "pvs.Render();";
184     pushAndExecPyLine(oss.str());
185 }
186
187 void
188 MEDPresentationCutSegment::updatePoint2(MEDCALC::DoubleArray point2)
189 {
190   _params.point2[0] = point2[0];
191   _params.point2[1] = point2[1];
192   _params.point2[2] = point2[2];
193
194   // GUI helper:
195   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT2_X, point2[0]);
196   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT2_Y, point2[1]);
197   setDoubleProperty(MEDPresentationCutSegment::PROP_POINT2_Z, point2[2]);
198   
199   // Update Point2
200   std::ostringstream oss;
201   oss << _objVar << ".Source.Point2 = [" <<  
202     _params.point2[0] << ", " <<
203     _params.point2[1] << ", " <<
204     _params.point2[2] << "];";
205   pushAndExecPyLine(oss.str()); oss.str("");
206   oss << getChartViewVar() << ".Update();";
207   oss << "pvs.Render();";
208   pushAndExecPyLine(oss.str());
209 }
210
211 std::string MEDPresentationCutSegment::getChartViewVar() const {
212   std::ostringstream oss;    
213   oss << "__chartView" << getPyViewID();
214   return oss.str();
215 }
216
217 void MEDPresentationCutSegment::setOrCreateChartView() {
218   std::string lineChartViewName = getChartViewVar();
219   std::ostringstream oss;
220   oss << lineChartViewName << " = medcalc.FindOrCreateView('XYChartView');";
221   pushAndExecPyLine(oss.str());
222 }
223
224 void MEDPresentationCutSegment::visibility() {
225   // Base class implementation: hide 3D presentation in the RenderView
226   MEDPresentation::visibility();
227   // Show/Hide 2D Presenation
228   std::ostringstream oss;
229   oss << getChartViewDispVar() << ".Visibility = " << (_presentationVisibility ? "True" : "False") << ";";
230   oss << getChartViewVar() << ".Update();";
231   pushAndExecPyLine(oss.str());
232 }
233
234 void MEDPresentationCutSegment::showObject() {
235   // 3D Presentation:
236   MEDPresentation::showObject();
237   
238   // 2D Presentation:
239   std::ostringstream oss;
240   std::ostringstream oss_series;
241   oss_series << _fieldName;
242   if (_nbComponents > 1) {
243     oss_series << "_Magnitude";
244   }
245   oss << _chartViewDispVar << " = pvs.Show(" << _objVar << ", " << getChartViewVar() << ", 'XYChartRepresentation');";
246   oss << _chartViewDispVar << ".SeriesVisibility = ['" << oss_series.str() << "'];";
247   oss << getChartViewVar() << ".Update();";
248   pushAndExecPyLine(oss.str());
249 }
250
251 void MEDPresentationCutSegment::hideObject() {
252   // 3D Presentation:
253   MEDPresentation::hideObject();
254
255   // 2D Presentation:
256   std::ostringstream oss;
257   oss << "pvs.Hide(" << _objVar << ", " << getChartViewVar() << ");";
258   oss << getChartViewVar() << ".Update();";
259   pushAndExecPyLine(oss.str());
260 }
261
262 std::string MEDPresentationCutSegment::getChartViewDispVar() {
263   return _chartViewDispVar;
264 }
265
266 bool MEDPresentationCutSegment::activateView() {
267   // if XYChartView of current MEDPresentationCutSegment is active, keep it active
268   // Owerwise call impplementation of base class
269   MEDPyLockWrapper lock;
270   execPyLine("__XYChartViewAlive = " + getChartViewDispVar() + " in pvs.GetActiveView()");
271   PyObject * obj = getPythonObjectFromMain("__XYChartViewAlive");
272   bool XYChartViewAlive = true;
273   if (obj && PyBool_Check(obj))
274     XYChartViewAlive = (obj == Py_True);
275   if (!XYChartViewAlive) {
276     return MEDPresentation::activateView();
277   }
278   return XYChartViewAlive;
279 }
280
281 MEDCALC::PresentationVisibility 
282 MEDPresentationCutSegment::presentationStateInActiveView() {
283   MEDPyLockWrapper lock;
284   MEDCALC::PresentationVisibility result = MEDCALC::PRESENTATION_NOT_IN_VIEW;
285
286   execPyLine("__isInView = ( " + getRenderViewVar() + " == pvs.GetActiveView() or "\
287     + getChartViewVar() +" == pvs.GetActiveView())");
288   PyObject * obj = getPythonObjectFromMain("__isInView");
289
290   if (obj && PyBool_Check(obj) && (obj == Py_True)) {
291     result = _presentationVisibility ? MEDCALC::PRESENTATION_VISIBLE : MEDCALC::PRESENTATION_INVISIBLE;
292   }
293   return result;
294 }
295
296 std::string MEDPresentationCutSegment::additionalThresholdInitializationActions() {
297   if (_hideDataOutsideCustomRange) {
298     std::ostringstream oss;
299     ComponentThresold& currentThreshold = _presentationThresolds[getThresholdIndex()];
300     oss << currentThreshold._thresholdVar << ".AllScalars = 0;";
301     return oss.str();
302   }
303   return "";
304 }