Salome HOME
Updated copyright comment
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationPointSprite.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 "MEDPresentationPointSprite.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 MEDPresentationPointSprite::TYPE_NAME = "MEDPresentationPointSprite";
32
33 MEDPresentationPointSprite::MEDPresentationPointSprite(const MEDCALC::PointSpriteParameters& params,
34                                                    const MEDCALC::ViewModeType viewMode) :
35     MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange), _params(params),
36     _gaussianRadius(0.0)
37 {
38 }
39
40 void
41 MEDPresentationPointSprite::initFieldMeshInfos()
42 {
43   MEDPresentation::initFieldMeshInfos();
44   _colorByType = "POINTS";
45 }
46
47 void
48 MEDPresentationPointSprite::scaleBallRadius()
49 {
50   std::ostringstream oss;
51   int nbCompo = getIntProperty(MEDPresentation::PROP_NB_COMPONENTS);
52
53   // set component or euclidean norm as the array to scale the ball radius
54   if (!_hideDataOutsideCustomRange) {
55     if (nbCompo > 1)
56     {
57       if (_selectedComponentIndex == -1)
58         oss << _dispVar << ".ScaleArrayComponent = 'Magnitude';";
59       else
60         oss << _dispVar << ".ScaleArrayComponent = " << _selectedComponentIndex << ";";
61       pushAndExecPyLine(oss.str()); oss.str("");
62     }
63   }
64   else {
65     oss << getDispVar() << ".SetScaleArray = ['POINTS', '" << getThresholdFieldName() << "'];";
66     oss << getDispVar() << ".ScaleArrayComponent = " << 0 << ";";
67   }
68
69   // rescale transfer function to update scalar bar and get min-max range
70   rescaleTransferFunction();
71
72   // _rangeVar (to get min and max of the fields) is updated in rescaleTransferFunction
73   oss << getDispVar() << ".ScaleTransferFunction.RescaleTransferFunction(" << _rangeVar << "[0], " << _rangeVar << "[1]);";
74   pushAndExecPyLine(oss.str());
75 }
76
77
78 void
79 MEDPresentationPointSprite::internalGeneratePipeline()
80 {
81   MEDPresentation::internalGeneratePipeline();
82
83   MEDPyLockWrapper lock;
84
85   setOrCreateRenderView(); // instantiate __viewXXX
86   createSource();
87   setTimestamp();
88
89   // Populate internal array of available components:
90   fillAvailableFieldComponents();
91   //int nbCompo = getIntProperty(MEDPresentation::PROP_NB_COMPONENTS); // todo: unused
92
93   // Point sprite needs point data:
94   applyCellToPointIfNeeded();
95
96   pushAndExecPyLine(_objVar + " = " + _srcObjVar);
97
98   showObject(); // needs to be done before cell size computation to be sure ParaView has computed bounds, etc ...
99
100   colorBy();    // see initFieldInfo() - necessarily POINTS like in Contour
101
102   std::ostringstream oss;
103
104   // Set point sprite:
105   oss << _dispVar << ".SetRepresentationType('Point Gaussian');";
106   pushAndExecPyLine(oss.str()); oss.str("");
107   oss << _dispVar << ".ScaleByArray = 1;";
108   pushAndExecPyLine(oss.str()); oss.str("");
109   oss << _dispVar << ".SetScaleArray = ['POINTS', '" << _fieldName << "'];";
110   pushAndExecPyLine(oss.str()); oss.str("");
111
112   // Compute the best radius again (like reset using current data value in GUI)
113   oss << "pvs.ResetProperty('GaussianRadius');";
114   pushAndExecPyLine(oss.str()); oss.str("");
115
116   // Make the radius twice the default radius
117   oss << _dispVar << ".GaussianRadius = 2*" << _dispVar << ".GaussianRadius" << ";";
118   oss <<"__gr = "<< _dispVar << ".GaussianRadius";
119   pushAndExecPyLine(oss.str()); oss.str("");
120   
121   PyObject * obj = getPythonObjectFromMain("__gr");
122   if (obj && PyFloat_Check(obj)) {
123     _gaussianRadius = PyFloat_AsDouble(obj);
124   }
125
126   showScalarBar();
127   selectColorMap();
128
129   // Scale the radius array with the component and the scalar map
130   scaleBallRadius();
131
132   resetCameraAndRender();
133 }
134
135 void
136 MEDPresentationPointSprite::updatePipeline(const MEDCALC::PointSpriteParameters& params)
137 {
138   if (params.fieldHandlerId != _params.fieldHandlerId)
139     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
140
141   if (std::string(params.displayedComponent) != std::string(_params.displayedComponent))
142     {
143       updateComponent<MEDPresentationPointSprite, MEDCALC::PointSpriteParameters>(std::string(params.displayedComponent));
144       scaleBallRadius();
145       pushAndExecPyLine("pvs.Render();");
146     }
147   if (params.scalarBarRange != _params.scalarBarRange ||
148       params.hideDataOutsideCustomRange != _params.hideDataOutsideCustomRange ||
149     params.scalarBarRangeArray[0] != _params.scalarBarRangeArray[0] ||
150     params.scalarBarRangeArray[1] != _params.scalarBarRangeArray[1])
151     updateScalarBarRange<MEDPresentationPointSprite, MEDCALC::PointSpriteParameters>(params.scalarBarRange,
152                                                                                      params.hideDataOutsideCustomRange,
153                                                                                      params.scalarBarRangeArray[0],
154                                                                                      params.scalarBarRangeArray[1]);
155   if (params.colorMap != _params.colorMap)
156     updateColorMap<MEDPresentationPointSprite, MEDCALC::PointSpriteParameters>(params.colorMap);
157   if (params.visibility != _params.visibility)
158     updateVisibility<MEDPresentationPointSprite, MEDCALC::PointSpriteParameters>(params.visibility);
159   if (params.scalarBarVisibility != _params.scalarBarVisibility)
160     updateScalarBarVisibility<MEDPresentationPointSprite, MEDCALC::PointSpriteParameters>(params.scalarBarVisibility);
161 }
162
163 std::string 
164 MEDPresentationPointSprite::additionalThresholdVisualizationActions() {
165   if (_hideDataOutsideCustomRange) {
166     // Set point sprite:
167     std::ostringstream oss;
168     std::string aDispVar = getDispVar();
169     oss << aDispVar << ".SetRepresentationType('Point Gaussian');";
170     oss << aDispVar << ".ScaleByArray = 1;";
171     oss << aDispVar << ".SetScaleArray = ['POINTS', '" << getThresholdFieldName() << "'];";
172     oss << aDispVar << ".GaussianRadius = " << _gaussianRadius << ";";
173     oss << aDispVar << ".ScaleTransferFunction.RescaleTransferFunction(" << _rangeVar << "[0], " << _rangeVar << "[1]);";
174     return oss.str();
175   }
176 }
177
178 void
179 MEDPresentationPointSprite::additionalThresholdActions() {
180   scaleBallRadius();
181 }
182
183 void 
184 MEDPresentationPointSprite::additionalUnThresholdActions() {
185   scaleBallRadius();
186 }