Salome HOME
bos #20430 [CEA 20428] FIELDS : improvement of simplified visualisations
[modules/med.git] / src / MEDCalc / cmp / MEDPresentation.hxx
1 // Copyright (C) 2011-2021  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 // Authors: A Bruneton (CEA), C Aguerre (EdF)
21
22 #ifndef SRC_MEDCALC_CMP_MEDPRESENTATION_HXX_
23 #define SRC_MEDCALC_CMP_MEDPRESENTATION_HXX_
24
25 #include <Python.h>
26 #include "MEDCouplingRefCountObject.hxx"
27 #include "MEDCALC.hxx"
28
29 #include <SALOMEconfig.h>
30 #include CORBA_SERVER_HEADER(MEDDataManager)
31 #include CORBA_SERVER_HEADER(MEDPresentationManager)
32
33 #include <vector>
34 #include <map>
35 #include <string>
36
37 struct ComponentThresold {
38
39   ComponentThresold() {
40     _thresholdVar = "";
41     _thresholdDispVar = "";
42     _thresholdLutVar = "";
43     _thresholdInitialized = false;
44     _active = false;
45   }
46
47   ///! ParaView Threshold filter variable to hide data outside custom range in Python scripting
48   std::string _thresholdVar;
49   ///! ParaView Threshold display variable to hide data outside custom range in Python scripting
50   std::string _thresholdDispVar;
51   ///! ParaView Threshold LUT variable in the Python scripting commands
52   std::string _thresholdLutVar;
53
54   ///! Initialization flag
55   bool _thresholdInitialized;
56   ///! Active flag
57   bool _active;
58
59   void clear() {
60     _thresholdInitialized = false;
61     _active = false;
62   }
63 };
64
65 typedef std::vector<ComponentThresold> PresentationThresolds;
66  
67 class MEDCALC_EXPORT MEDPresentation
68 {
69   friend class MEDPresentationManager_i;
70
71 public:
72   typedef ::CORBA::Long TypeID;
73
74   virtual void initFieldMeshInfos();
75
76   virtual ~MEDPresentation();
77
78   static const std::string PROP_NAME;                  // name of the presentation
79   static const std::string PROP_NB_COMPONENTS;         // number of field components
80   static const std::string PROP_SELECTED_COMPONENT;    // index of the selected component - 0 means Euclidean norm
81   static const std::string PROP_COMPONENT;             // string prefix for all properties storing component names
82   static const std::string PROP_COLOR_MAP;             // color map - TODO: arch fix - should be in params only
83   static const std::string PROP_SCALAR_BAR_RANGE;      // scalar bar range - TODO: arch fix - should be in params only 
84   static const std::string PROP_VISIBILITY;            // presentation visibility
85   static const std::string PROP_SCALAR_BAR_VISIBILITY; // scalar bar visiblity
86   static const std::string PROP_SCALAR_BAR_MIN_VALUE;  // min value of scalar bar range
87   static const std::string PROP_SCALAR_BAR_MAX_VALUE;  // max value of scalar bar range
88   static const std::string PROP_HIDE_DATA_OUTSIDE_CUSTOM_RANGE; // Hide data outside custom range
89
90
91   virtual void setStringProperty(const std::string& propName, const std::string& propValue);
92   const std::string getStringProperty(const std::string& propName) const;
93
94   virtual void setIntProperty(const std::string& propName, const int propValue);
95   int getIntProperty(const std::string& propName) const;
96
97   virtual void setDoubleProperty(const std::string& propName, const double propValue);
98   double getDoubleProperty(const std::string& propName) const;
99
100   // returns True if the view was still alive, False if it must have been recreated
101   // because the user closed it.
102   virtual bool activateView();
103   virtual void recreateViewSetup();
104
105   void dumpIntProperties() const;
106   void dumpStringProperties() const;
107   void dumpDoubleProperties() const;
108
109   std::string paravisDump() const;
110
111   long getPyViewID() const { return _renderViewPyId; }
112   void setPyViewID(long id) { _renderViewPyId = id; }
113
114   static std::string toScriptCellType(const std::string&);
115
116 protected:
117   typedef std::pair<int, PyObject *> PyObjectId;
118   static int GeneratePythonId();
119
120   MEDPresentation(MEDPresentation::TypeID handlerId, const std::string& name,
121                   const MEDCALC::ViewModeType viewMode,
122                   const MEDCALC::ColorMapType colorMap,
123                   const MEDCALC::ScalarBarRangeType sbRange);
124   std::string getRenderViewVar() const;
125
126   // The most common elements of the ParaView pipeline:
127   void setOrCreateRenderView();
128   void createSource();
129   void setTimestamp();
130   void selectFieldComponent();
131   void virtual showObject();
132   void virtual hideObject();
133   void colorBy();
134   virtual void showScalarBar();
135   virtual void hideScalarBar();
136   void scalarBarVisibility();
137   void rescaleTransferFunction();
138   void selectColorMap(const bool updateFieldComponent=true);
139   virtual void scalarBarTitle();
140   void resetCameraAndRender();
141   virtual void visibility();
142   void threshold();
143   void thresholdPresentation();
144   void unThresholdPresentation();
145   void thresholdValues();
146   virtual void additionalThresholdActions() {};
147   virtual std::string additionalThresholdVisualizationActions() { return ""; };
148   virtual std::string additionalThresholdInitializationActions() { return ""; };
149   virtual void additionalUnThresholdActions() {};
150   virtual std::string getFieldName() const;
151   virtual std::string getThresholdFieldName() const;
152
153   std::string getScriptCellType() const;
154   bool isThresoldActive() const;
155
156   virtual void internalGeneratePipeline();
157   virtual void internalGenerateThreshold();
158   const std::string& getDispVar();
159   const std::string& getLutVar();
160   PyObject* getPythonObjectFromMain(const char* var) const;
161   void execPyLine(const std::string & lin);
162   void pushAndExecPyLine(const std::string & lin);
163
164   MEDPresentation::TypeID getID() const { return _handlerId; }
165
166   void fillAvailableFieldComponents();
167   void applyCellToPointIfNeeded();
168   void extractFileName(const std::string& name);
169 //  void convertTo3DVectorField();
170
171   virtual MEDCALC::PresentationVisibility presentationStateInActiveView();
172
173   template<typename PresentationType, typename PresentationParameters>
174   void updateComponent(const std::string& newCompo);
175
176   template<typename PresentationType, typename PresentationParameters>
177   void updateColorMap(MEDCALC::ColorMapType colorMap);
178
179   template<typename PresentationType, typename PresentationParameters>
180   void updateScalarBarRange(MEDCALC::ScalarBarRangeType sbRange, 
181                             bool hideDataoutsideCustomRange, 
182                             double minValue, double maxValue);
183
184   template<typename PresentationType, typename PresentationParameters>
185   void updateVisibility(const bool theVisibility);
186
187   template<typename PresentationType, typename PresentationParameters>
188   void updateScalarBarVisibility(const bool theVisibility);
189
190   template<typename PresentationType, typename PresentationParameters>
191   void getParameters(PresentationParameters& params) const;
192
193   template<typename PresentationType, typename PresentationParameters>
194   void setParameters(const PresentationParameters& params);
195
196   int getThresholdIndex() const;
197
198   void deleteThresholds();
199
200 private:
201   std::string getPVFieldTypeString(MEDCoupling::TypeOfField fieldType) const;
202
203   // The following functions are reserved to friend class MEDPresentationManager
204   void generatePipeline();
205
206   template<typename PresentationType, typename PresentationParameters>
207   void updatePipeline(const PresentationParameters& params);
208
209 protected:
210   std::string _meshName;
211   std::string _fileName;
212   std::string _fieldName;
213
214   ///! MEDCoupling field type (ON_NODES, ON_CELLS, ON_GAUSS_PT, ON_GAUSS_NE
215   MEDCoupling::TypeOfField _mcFieldType;
216   ///! ParaView field type: "CELLS" or "POINTS"
217   std::string _pvFieldType;
218   ///! ParaView field type: "CELLS" or "POINTS" used in the ColorBy method. Not necessarily equal to _pvFieldType.
219   std::string _colorByType;
220
221   MEDPresentation::TypeID _handlerId;  // either a field or a mesh id (a field ID though, most of the time)
222
223   int _selectedComponentIndex;
224   int _nbComponents;
225   int _nbComponentsInThresholdInput;
226   MEDCALC::ViewModeType _viewMode;
227   MEDCALC::ColorMapType _colorMap;
228   MEDCALC::ScalarBarRangeType _sbRange;
229   bool _presentationVisibility;
230   bool _scalarBarVisibility;
231   bool _hideDataOutsideCustomRange;
232   double _scalarBarRangeArray[2];
233
234   ///! Identifier (in the Python dump) of the render view
235   int _renderViewPyId;
236   ///! ParaView object variable in the Python scripting commands (source object)
237   std::string _srcObjVar;
238   ///! ParaView object variable in the Python scripting commands (final object on which all display commands are done)
239   std::string _objVar;
240   ///! ParaView display variable in the Python scripting commands
241   std::string _dispVar;
242   ///! ParaView LUT variable in the Python scripting commands
243   std::string _lutVar;
244   ///! ParaView variable in Python holding the data range
245   std::string _rangeVar;
246   ///! Prorgammable variable in the Python scripting commands
247   std::string _programmableVar;
248   bool _programmableInitialized;
249
250   //Thresolds 
251   PresentationThresolds _presentationThresolds;
252
253
254 private:
255   ///! Presentation properties <key,value>
256   std::map<std::string, std::string> _propertiesStr;
257   std::map<std::string, int> _propertiesInt;
258   std::map<std::string, double> _propertiesDouble;
259
260   std::vector<std::string> _pythonCmds;
261
262   mutable PyObject* _globalDict;
263 };
264
265 #include "MEDPresentation.txx"
266
267 #endif /* SRC_MEDCALC_CMP_MEDPRESENTATION_HXX_ */