1 // Copyright (C) 2016-2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "MEDPyLockWrapper.hxx"
22 #include "MEDPresentationVectorField.hxx"
23 #include "MEDPresentationException.hxx"
25 #include <SALOME_KernelServices.hxx>
26 #undef LOG // should be fixed in KERNEL - double definition
27 #include <Basics_Utils.hxx>
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";
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)
39 setDoubleProperty(MEDPresentationVectorField::PROP_SCALE_FACTOR, params.scaleFactor);
40 setIntProperty(MEDPresentationVectorField::PROP_CUSTOM_SCALE_FACTOR, params.customScaleFactor);
44 MEDPresentationVectorField::initFieldMeshInfos()
46 MEDPresentation::initFieldMeshInfos();
47 _colorByType = "POINTS";
51 MEDPresentationVectorField::autoScale()
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);
67 MEDPresentationVectorField::internalGeneratePipeline()
69 MEDPresentation::internalGeneratePipeline();
71 MEDPyLockWrapper lock;
76 // Populate internal array of available components:
77 fillAvailableFieldComponents();
78 int nbCompo = getIntProperty(MEDPresentation::PROP_NB_COMPONENTS);
81 const char * msg = "Vector field presentation does not work for scalar field!"; // this message will appear in GUI too
83 throw KERNEL::createSalomeException(msg);
86 setOrCreateRenderView(); // instantiate __viewXXX, needs to be after the exception above otherwise previous elements in the view will be hidden.
88 std::ostringstream oss;
89 oss << _objVar << " = pvs.Glyph(Input=" << _srcObjVar << ", GlyphType='Arrow');";
90 pushAndExecPyLine(oss.str()); oss.str("");
92 showObject(); // to be done first so that the scale factor computation properly works
94 std::string fieldName = nbCompo <= 2 ? _fieldName + "_Vector" : _fieldName;
96 oss << _objVar << ".ScaleArray = ['"<< _pvFieldType << "', '" << fieldName << "'];";
97 pushAndExecPyLine(oss.str()); oss.str("");
99 oss << _objVar << ".OrientationArray = ['"<< _pvFieldType << "', '" << fieldName << "'];";
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("");
109 colorBy(); // see initFieldInfo() - necessarily POINTS
112 rescaleTransferFunction();
114 scale(); // to be called after transfer function so we have the range
116 resetCameraAndRender();
120 MEDPresentationVectorField::updatePipeline(const MEDCALC::VectorFieldParameters& params)
122 if (params.fieldHandlerId != _params.fieldHandlerId)
123 throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
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])
130 updateScalarBarRange<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.scalarBarRange,
131 params.hideDataOutsideCustomRange,
132 params.scalarBarRangeArray[0],
133 params.scalarBarRangeArray[1]);
135 pushAndExecPyLine("pvs.Render();");
137 if (params.scaleFactor != _params.scaleFactor ||
138 params.customScaleFactor != _params.customScaleFactor) {
139 updateScaleFactor(params.scaleFactor, params.customScaleFactor);
142 if (params.colorMap != _params.colorMap)
143 updateColorMap<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.colorMap);
145 if (params.visibility != _params.visibility)
146 updateVisibility<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.visibility);
148 if (params.scalarBarVisibility != _params.scalarBarVisibility)
149 updateScalarBarVisibility<MEDPresentationVectorField, MEDCALC::VectorFieldParameters>(params.scalarBarVisibility);
154 MEDPresentationVectorField::updateScaleFactor(const double scaleFactor, const bool customScaleFactor)
156 if (customScaleFactor) {
157 _params.scaleFactor = scaleFactor;
158 setDoubleProperty(MEDPresentationVectorField::PROP_SCALE_FACTOR, _params.scaleFactor);
161 _params.customScaleFactor = customScaleFactor;
163 setIntProperty(MEDPresentationVectorField::PROP_CUSTOM_SCALE_FACTOR, _params.customScaleFactor);
168 MEDPresentationVectorField::scale()
170 if (!_params.customScaleFactor) {
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();");