Salome HOME
Copyright update 2022
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationStreamLines.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 "MEDPresentationStreamLines.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 MEDPresentationStreamLines::TYPE_NAME = "MEDPresentationStreamLines";
32 const std::string MEDPresentationStreamLines::PROP_INTEGR_DIR_TYPE = "intDirType";
33
34 MEDPresentationStreamLines::MEDPresentationStreamLines(const MEDCALC::StreamLinesParameters& params,
35                                                    const MEDCALC::ViewModeType viewMode) :
36     MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange),
37     _params(params)
38 {
39   setIntProperty(MEDPresentationStreamLines::PROP_INTEGR_DIR_TYPE, params.integrDir);
40 }
41
42 std::string
43 MEDPresentationStreamLines::getIntegrDirType() const
44 {
45   switch(_params.integrDir)
46   {
47     case MEDCALC::INTEGRATION_DIR_BOTH:
48       return "BOTH";
49     case MEDCALC::INTEGRATION_DIR_FORWARD:
50       return "FORWARD";
51     case MEDCALC::INTEGRATION_DIR_BACKWARD:
52       return "BACKWARD";
53     default:
54       const char * mes = "Unexpected getIntegrDirType() error!";
55       STDLOG(mes);
56       throw KERNEL::createSalomeException(mes);
57   }
58   return ""; // never happens
59 }
60
61 void
62 MEDPresentationStreamLines::addThirdZeroComponent()
63 {
64   std::ostringstream oss;
65   
66   oss << _objStreamCalc << " = pvs.Calculator(Input=" << _objStreamCalc << ");";
67   pushAndExecPyLine(oss.str()); oss.str("");
68   std::string typ = "Point Data"; // Because CellDatatoPointData filter has been applied erlier
69   oss << _objStreamCalc << ".AttributeType = '" <<  typ << "';";
70   pushAndExecPyLine(oss.str()); oss.str("");
71   oss << _objStreamCalc << ".ResultArrayName = '" <<  _fieldName << "_CALC';";
72   pushAndExecPyLine(oss.str()); oss.str("");
73   oss << _objStreamCalc << ".Function = '" <<  _fieldName << "_X*iHat + " << _fieldName << "_Y*jHat + 0.0*kHat';";
74   pushAndExecPyLine(oss.str()); oss.str("");
75   /*
76   oss << _dispVar << " = pvs.Show(" << _objStreamCalc << ", " << getRenderViewVar() << ");";
77   oss << _lutVar << " = pvs.GetColorTransferFunction('" << _fieldName << "', " << _dispVar << ", separate=True);";
78   execPyLine(oss.str()); oss.str("");
79   oss << "pvs.ColorBy(" << getDispVar() << ", ('" << "POINTS" << "', '" << _fieldName << "_CALC'), separate=True);";
80   execPyLine(oss.str()); oss.str("");
81   oss << "pvs.Hide(" << _objStreamCalc << ", " << getRenderViewVar() << ");";
82   execPyLine(oss.str()); oss.str("");
83   */
84 }
85
86 void
87 MEDPresentationStreamLines::internalGeneratePipeline()
88 {
89   MEDPresentation::internalGeneratePipeline();
90
91   MEDPyLockWrapper lock;
92   
93   createSource();
94   setTimestamp();
95
96   fillAvailableFieldComponents();
97   int nbCompo = getIntProperty(MEDPresentation::PROP_NB_COMPONENTS);
98   if (nbCompo <= 1)
99   {
100     const char * msg = "Stream Lines field presentation does not work for scalar field!";
101     STDLOG(msg);
102     throw KERNEL::createSalomeException(msg);
103   }
104
105   setOrCreateRenderView();
106
107   std::ostringstream oss;
108   if (_pvFieldType == "CELLS")
109   {
110     oss << "__objStreamCalc";
111     _objStreamCalc = oss.str(); oss.str("");
112     oss << _objStreamCalc << " = pvs.CellDatatoPointData(" << _srcObjVar << ");";
113     oss << _objStreamCalc << ".PassCellData = 1;";
114     pushAndExecPyLine(oss.str()); oss.str("");
115   }
116   else
117   {
118     oss << "__objStreamCalc";
119     _objStreamCalc = oss.str(); oss.str("");
120     oss << _objStreamCalc << "=" << _srcObjVar;
121     pushAndExecPyLine(oss.str()); oss.str("");
122   }
123
124   if (_nbComponents == 2)
125     addThirdZeroComponent();
126
127   std::string dirType = getIntegrDirType();
128
129   oss.str("");
130   oss << _objVar << " = " << _objStreamCalc << ";";
131   oss << _objVar << ".UpdatePipeline();";
132   pushAndExecPyLine(oss.str()); oss.str("");
133   oss << _objVar << " = pvs.StreamTracer(Input=" << _objVar << ", SeedType='Point Cloud' if pvs.GetParaViewVersion().GetVersion() > 5.8 else 'Point Source');";
134   pushAndExecPyLine(oss.str()); oss.str("");
135   oss << _objVar << ".IntegrationDirection = '" << dirType << "';";
136   pushAndExecPyLine(oss.str()); oss.str("");
137
138   showObject();
139
140   oss << "pvs.ColorBy(" << getDispVar() << ", ('" << "POINTS" << "', '" << getFieldName() << "'), separate=True);";
141   pushAndExecPyLine(oss.str()); oss.str("");
142   
143   showScalarBar();
144   selectColorMap();
145   rescaleTransferFunction();
146   scalarBarTitle();
147   resetCameraAndRender();
148 }
149
150 void
151 MEDPresentationStreamLines::updatePipeline(const MEDCALC::StreamLinesParameters& params)
152 {
153    if (params.fieldHandlerId != _params.fieldHandlerId)
154      throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
155
156    if (params.colorMap != _params.colorMap)
157      updateColorMap<MEDPresentationStreamLines, MEDCALC::StreamLinesParameters>(params.colorMap);
158
159    if (params.scalarBarRange != _params.scalarBarRange ||
160      params.hideDataOutsideCustomRange != _params.hideDataOutsideCustomRange ||
161      params.scalarBarRangeArray[0] != _params.scalarBarRangeArray[0] ||
162      params.scalarBarRangeArray[1] != _params.scalarBarRangeArray[1])
163      updateScalarBarRange<MEDPresentationStreamLines, MEDCALC::StreamLinesParameters>(params.scalarBarRange,
164        params.hideDataOutsideCustomRange,
165        params.scalarBarRangeArray[0],
166        params.scalarBarRangeArray[1]);
167
168    if (params.integrDir != _params.integrDir)
169      updateIntegrDir(params.integrDir);
170
171    if (params.visibility != _params.visibility)
172      updateVisibility<MEDPresentationStreamLines, MEDCALC::StreamLinesParameters>(params.visibility);
173    if (params.scalarBarVisibility != _params.scalarBarVisibility)
174      updateScalarBarVisibility<MEDPresentationStreamLines, MEDCALC::StreamLinesParameters>(params.scalarBarVisibility);
175 }
176
177 void
178 MEDPresentationStreamLines::updateIntegrDir(const MEDCALC::IntegrationDirType integrDir)
179 {
180   _params.integrDir = integrDir;
181
182   // GUI helper:
183   setIntProperty(MEDPresentationStreamLines::PROP_INTEGR_DIR_TYPE, integrDir);
184
185   // Update the pipeline:
186   {
187     MEDPyLockWrapper lock;
188     std::ostringstream oss;
189     std::string dirType = getIntegrDirType();
190     oss << _objVar << ".IntegrationDirection = '" << dirType << "'";
191     pushAndExecPyLine(oss.str()); oss.str("");
192     pushAndExecPyLine("pvs.Render();");
193   }
194 }
195
196 void
197 MEDPresentationStreamLines::initFieldMeshInfos()
198 {
199   MEDPresentation::initFieldMeshInfos();
200   _colorByType = "POINTS";
201 }
202
203 std::string MEDPresentationStreamLines::additionalThresholdInitializationActions() {
204   if (_hideDataOutsideCustomRange) {
205     std::ostringstream oss;
206     ComponentThresold& currentThreshold = _presentationThresolds[getThresholdIndex()];
207     oss << currentThreshold._thresholdVar << ".AllScalars = 0;";
208     return oss.str();
209   }
210   return "";
211 }
212
213 std::string MEDPresentationStreamLines::getFieldName() const {
214   std::ostringstream oss;
215   oss << _fieldName;
216   if (_nbComponents == 2) {
217     oss << "_CALC";
218   }
219   return oss.str();
220 }
221
222 void
223 MEDPresentationStreamLines::scalarBarTitle()
224 {
225   if (_nbComponents == 2 && !_hideDataOutsideCustomRange) {
226     std::ostringstream oss;
227     oss << "pvs.GetScalarBar(" << getLutVar() << ").Title = '" << _fieldName << "';";
228     pushAndExecPyLine(oss.str());
229   }
230   MEDPresentation::scalarBarTitle();
231 }