Salome HOME
Updated copyright comment
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationVectorField.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 "MEDPresentationVectorField.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 MEDPresentationVectorField::TYPE_NAME = "MEDPresentationVectorField";
32 const std::string MEDPresentationVectorField::PROP_SCALE_FACTOR = "scaleFactor";
33 const std::string MEDPresentationVectorField::PROP_CUSTOM_SCALE_FACTOR = "customScaleFactor";
34
35 MEDPresentationVectorField::MEDPresentationVectorField(const MEDCALC::VectorFieldParameters& params,
36                                                    const MEDCALC::ViewModeType viewMode) :
37     MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange), _params(params)
38 {
39   setDoubleProperty(MEDPresentationVectorField::PROP_SCALE_FACTOR, params.scaleFactor);
40   setIntProperty(MEDPresentationVectorField::PROP_CUSTOM_SCALE_FACTOR, params.customScaleFactor);
41 }
42
43 void
44 MEDPresentationVectorField::initFieldMeshInfos()
45 {
46   MEDPresentation::initFieldMeshInfos();
47   _colorByType = "POINTS";
48 }
49
50 void
51 MEDPresentationVectorField::autoScale()
52 {
53   std::ostringstream oss;
54   oss << "__autoScaleF = 2.0*medcalc.ComputeCellAverageSize(" << _srcObjVar << ")/(" << _rangeVar
55       << "[1]-" << _rangeVar << "[0]);";
56   MEDPyLockWrapper lock;
57   execPyLine(oss.str());
58   PyObject * obj = getPythonObjectFromMain("__autoScaleF");
59   if (obj && PyFloat_Check(obj)) {
60     _params.scaleFactor = PyFloat_AsDouble(obj);
61     setDoubleProperty(MEDPresentationVectorField::PROP_SCALE_FACTOR, _params.scaleFactor);
62   }
63
64 }
65
66 void
67 MEDPresentationVectorField::internalGeneratePipeline()
68 {
69   MEDPresentation::internalGeneratePipeline();
70
71   MEDPyLockWrapper lock;
72
73   createSource();
74   setTimestamp();
75
76   // Populate internal array of available components:
77   fillAvailableFieldComponents();
78   int nbCompo = getIntProperty(MEDPresentation::PROP_NB_COMPONENTS);
79   if (nbCompo <= 1)
80     {
81       const char * msg = "Vector field presentation does not work for scalar field!"; // this message will appear in GUI too
82       STDLOG(msg);
83       throw KERNEL::createSalomeException(msg);
84     }
85
86   setOrCreateRenderView();  // instantiate __viewXXX, needs to be after the exception above otherwise previous elements in the view will be hidden.
87
88   std::ostringstream oss;
89   oss << _objVar << " = pvs.Glyph(Input=" << _srcObjVar << ", GlyphType='Arrow');";
90   pushAndExecPyLine(oss.str()); oss.str("");
91
92   showObject(); // to be done first so that the scale factor computation properly works
93
94   std::string fieldName = nbCompo <= 2 ? _fieldName + "_Vector" : _fieldName;
95
96   oss << _objVar << ".ScaleArray = ['"<< _pvFieldType << "', '" << fieldName << "'];";
97   pushAndExecPyLine(oss.str()); oss.str("");
98
99   oss << _objVar << ".OrientationArray = ['"<< _pvFieldType << "', '" << fieldName << "'];";
100
101   pushAndExecPyLine(oss.str()); oss.str("");
102   oss << _objVar << ".VectorScaleMode = 'Scale by Magnitude';";
103   pushAndExecPyLine(oss.str()); oss.str("");
104   oss << _objVar << ".GlyphMode = 'All Points';";
105   pushAndExecPyLine(oss.str()); oss.str("");
106   oss << _objVar << "GlyphTransform = 'Transform2';";  // not sure this is really needed?
107   pushAndExecPyLine(oss.str()); oss.str("");
108
109   colorBy();    // see initFieldInfo() - necessarily POINTS
110   showScalarBar();
111   selectColorMap();
112   rescaleTransferFunction();
113
114   scale();   // to be called after transfer function so we have the range
115
116   resetCameraAndRender();
117 }
118
119 void
120 MEDPresentationVectorField::updatePipeline(const MEDCALC::VectorFieldParameters& params)
121 {
122   if (params.fieldHandlerId != _params.fieldHandlerId)
123     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
124
125   if (params.scalarBarRange != _params.scalarBarRange ||
126       params.hideDataOutsideCustomRange != _params.hideDataOutsideCustomRange ||
127       params.scalarBarRangeArray[0] != _params.scalarBarRangeArray[0] ||
128       params.scalarBarRangeArray[1] != _params.scalarBarRangeArray[1])
129     {
130       updateScalarBarRange<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.scalarBarRange,
131                                                                                        params.hideDataOutsideCustomRange,
132                                                                                        params.scalarBarRangeArray[0],
133                                                                                        params.scalarBarRangeArray[1]);
134       scale();
135       pushAndExecPyLine("pvs.Render();");
136     }
137   if (params.scaleFactor != _params.scaleFactor ||
138       params.customScaleFactor != _params.customScaleFactor) {
139     updateScaleFactor(params.scaleFactor, params.customScaleFactor);
140   }
141
142   if (params.colorMap != _params.colorMap)
143     updateColorMap<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.colorMap);
144
145   if (params.visibility != _params.visibility)
146     updateVisibility<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.visibility);
147
148   if (params.scalarBarVisibility != _params.scalarBarVisibility)
149     updateScalarBarVisibility<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.scalarBarVisibility);
150
151 }
152
153 void
154 MEDPresentationVectorField::updateScaleFactor(const double scaleFactor, const bool customScaleFactor)
155 {
156   if (customScaleFactor) {
157     _params.scaleFactor = scaleFactor;
158     setDoubleProperty(MEDPresentationVectorField::PROP_SCALE_FACTOR, _params.scaleFactor);
159   }
160
161   _params.customScaleFactor = customScaleFactor;
162
163   setIntProperty(MEDPresentationVectorField::PROP_CUSTOM_SCALE_FACTOR, _params.customScaleFactor);
164   scale();
165 }
166
167 void 
168 MEDPresentationVectorField::scale()
169 {
170   if (!_params.customScaleFactor) {
171     autoScale();
172   }
173   // Update Scale Factor
174   MEDPyLockWrapper lock;
175   std::ostringstream oss;
176   oss << _objVar << ".ScaleFactor = " << _params.scaleFactor << ";";
177   pushAndExecPyLine(oss.str()); oss.str("");
178   pushAndExecPyLine("pvs.Render();");
179 }