Salome HOME
Updated copyright comment
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationStreamLines.cxx
1 // Copyright (C) 2016-2024  CEA, EDF
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 <<
134       ", SeedType='Point Cloud' if not medcalc.IsPlanarObj(" << _objVar <<
135       ") else 'Line');";
136   pushAndExecPyLine(oss.str()); oss.str("");
137   oss << _objVar << ".IntegrationDirection = '" << dirType << "';";
138   pushAndExecPyLine(oss.str()); oss.str("");
139
140   showObject();
141
142   oss << "pvs.ColorBy(" << getDispVar() << ", ('" << "POINTS" << "', '" << getFieldName() << "'), separate=True);";
143   pushAndExecPyLine(oss.str()); oss.str("");
144   
145   showScalarBar();
146   selectColorMap();
147   rescaleTransferFunction();
148   scalarBarTitle();
149   resetCameraAndRender();
150 }
151
152 void
153 MEDPresentationStreamLines::updatePipeline(const MEDCALC::StreamLinesParameters& params)
154 {
155    if (params.fieldHandlerId != _params.fieldHandlerId)
156      throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
157
158    if (params.colorMap != _params.colorMap)
159      updateColorMap<MEDPresentationStreamLines, MEDCALC::StreamLinesParameters>(params.colorMap);
160
161    if (params.scalarBarRange != _params.scalarBarRange ||
162      params.hideDataOutsideCustomRange != _params.hideDataOutsideCustomRange ||
163      params.scalarBarRangeArray[0] != _params.scalarBarRangeArray[0] ||
164      params.scalarBarRangeArray[1] != _params.scalarBarRangeArray[1])
165      updateScalarBarRange<MEDPresentationStreamLines, MEDCALC::StreamLinesParameters>(params.scalarBarRange,
166        params.hideDataOutsideCustomRange,
167        params.scalarBarRangeArray[0],
168        params.scalarBarRangeArray[1]);
169
170    if (params.integrDir != _params.integrDir)
171      updateIntegrDir(params.integrDir);
172
173    if (params.visibility != _params.visibility)
174      updateVisibility<MEDPresentationStreamLines, MEDCALC::StreamLinesParameters>(params.visibility);
175    if (params.scalarBarVisibility != _params.scalarBarVisibility)
176      updateScalarBarVisibility<MEDPresentationStreamLines, MEDCALC::StreamLinesParameters>(params.scalarBarVisibility);
177 }
178
179 void
180 MEDPresentationStreamLines::updateIntegrDir(const MEDCALC::IntegrationDirType integrDir)
181 {
182   _params.integrDir = integrDir;
183
184   // GUI helper:
185   setIntProperty(MEDPresentationStreamLines::PROP_INTEGR_DIR_TYPE, integrDir);
186
187   // Update the pipeline:
188   {
189     MEDPyLockWrapper lock;
190     std::ostringstream oss;
191     std::string dirType = getIntegrDirType();
192     oss << _objVar << ".IntegrationDirection = '" << dirType << "'";
193     pushAndExecPyLine(oss.str()); oss.str("");
194     pushAndExecPyLine("pvs.Render();");
195   }
196 }
197
198 void
199 MEDPresentationStreamLines::initFieldMeshInfos()
200 {
201   MEDPresentation::initFieldMeshInfos();
202   _colorByType = "POINTS";
203 }
204
205 std::string MEDPresentationStreamLines::additionalThresholdInitializationActions() {
206   if (_hideDataOutsideCustomRange) {
207     std::ostringstream oss;
208     ComponentThresold& currentThreshold = _presentationThresolds[getThresholdIndex()];
209     oss << currentThreshold._thresholdVar << ".AllScalars = 0;";
210     return oss.str();
211   }
212   return "";
213 }
214
215 std::string MEDPresentationStreamLines::getFieldName() const {
216   std::ostringstream oss;
217   oss << _fieldName;
218   if (_nbComponents == 2) {
219     oss << "_CALC";
220   }
221   return oss.str();
222 }
223
224 void
225 MEDPresentationStreamLines::scalarBarTitle()
226 {
227   if (_nbComponents == 2 && !_hideDataOutsideCustomRange) {
228     std::ostringstream oss;
229     oss << "pvs.GetScalarBar(" << getLutVar() << ").Title = '" << _fieldName << "';";
230     pushAndExecPyLine(oss.str());
231   }
232   MEDPresentation::scalarBarTitle();
233 }