Salome HOME
fee4659e59a133cb89f096b5503117c35bfa34e8
[modules/med.git] / src / MEDCalc / cmp / MEDPresentationContour.cxx
1 // Copyright (C) 2016  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 #include "MEDPresentationContour.hxx"
21
22 #include <SALOME_KernelServices.hxx>
23 #undef LOG  // should be fixed in KERNEL - double definition
24 #include <Basics_Utils.hxx>
25
26 #include <sstream>
27
28 const std::string MEDPresentationContour::TYPE_NAME = "MEDPresentationContour";
29 const std::string MEDPresentationContour::PROP_NB_CONTOUR = "nbContour";
30
31 MEDPresentationContour::MEDPresentationContour(const MEDCALC::ContourParameters& params,
32                                                const MEDCALC::ViewModeType viewMode) :
33         MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange),
34         _params(params)
35 {
36   setIntProperty(MEDPresentationContour::PROP_NB_CONTOUR, params.nbContours);
37 }
38
39 std::string
40 MEDPresentationContour::getContourCommand() const
41 {
42   std::ostringstream oss1;
43
44   oss1 << "min_max = " << _srcObjVar << ".PointData.GetArray('" << _fieldName << "').GetRange();\n";
45   oss1 << "delta = (min_max[1]-min_max[0])/float(" << _params.nbContours << ");\n";
46   oss1 << _objVar << ".Isosurfaces = [min_max[0]+0.5*delta+i*delta for i in range(" << _params.nbContours << ")];\n";
47   return oss1.str();
48 }
49
50 void
51 MEDPresentationContour::internalGeneratePipeline()
52 {
53   MEDPresentation::internalGeneratePipeline();
54
55   MEDPyLockWrapper lock;
56
57   std::ostringstream oss_o, oss;
58   std::string view(getRenderViewVar());
59
60   oss << _srcObjVar << " = pvs.MEDReader(FileName='" << _fileName << "');";
61   pushAndExecPyLine(oss.str()); oss.str("");
62
63   // Populate internal array of available components:
64   fillAvailableFieldComponents();
65   if (getIntProperty(MEDPresentation::PROP_NB_COMPONENTS) > 1)
66     {
67       const char * msg = "Contour presentation only works for scalar field!"; // this message will appear in GUI too
68       STDLOG(msg);
69       throw KERNEL::createSalomeException(msg);
70     }
71
72   pushAndExecPyLine( getRenderViewCommand() ); // instanciate __viewXXX
73
74   if(_fieldType == "CELLS")
75     {
76       // In case of a CELLS field need to convert to POINT field, and update source
77       STDLOG("Applying CellDatatoPointData filter");
78       std::ostringstream oss2, oss4;
79       // Apply Cell data to point data:
80       oss2 << "__obj" << GeneratePythonId();
81       oss << oss2.str() << " = pvs.CellDatatoPointData(Input=" << _srcObjVar << ");";
82       pushAndExecPyLine(oss.str()); oss.str("");
83       // Now the source becomes the result of the CellDatatoPointData:
84       _srcObjVar = oss2.str();
85     }
86   oss << _objVar << " = pvs.Contour(Input=" << _srcObjVar << ");";
87   pushAndExecPyLine(oss.str()); oss.str("");
88   oss << _objVar << ".ContourBy = ['POINTS', '" << _fieldName << "'];";
89   pushAndExecPyLine(oss.str()); oss.str("");
90
91   // Set number of contours
92   pushAndExecPyLine(getContourCommand());
93
94   oss << _dispVar << " = pvs.Show(" << _objVar << ", " << view << ");";
95   pushAndExecPyLine(oss.str()); oss.str("");
96   oss << "pvs.ColorBy(" << _dispVar << ", ('POINTS', '" << _fieldName << "'));";  // necessarily POINTS
97   pushAndExecPyLine(oss.str()); oss.str("");
98   oss << _dispVar <<  ".SetScalarBarVisibility(" << view << ", True);";
99   pushAndExecPyLine(oss.str()); oss.str("");
100   oss << getRescaleCommand();
101   pushAndExecPyLine(oss.str()); oss.str("");
102   oss << _lutVar << " = pvs.GetColorTransferFunction('" << _fieldName << "');";
103   pushAndExecPyLine(oss.str()); oss.str("");
104   pushAndExecPyLine(getColorMapCommand()); oss.str("");
105   pushAndExecPyLine(getResetCameraCommand());
106   pushAndExecPyLine("pvs.Render();");
107 }
108
109 void
110 MEDPresentationContour::updatePipeline(const MEDCALC::ContourParameters& params)
111 {
112   if (params.fieldHandlerId != _params.fieldHandlerId)
113     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
114
115   if (params.scalarBarRange != _params.scalarBarRange)
116     updateScalarBarRange<MEDPresentationContour, MEDCALC::ContourParameters>(params.scalarBarRange);
117   if (params.colorMap != _params.colorMap)
118     updateColorMap<MEDPresentationContour, MEDCALC::ContourParameters>(params.colorMap);
119
120   if (params.nbContours != _params.nbContours)
121     updateNbContours(params.nbContours);
122 }
123
124 void
125 MEDPresentationContour::updateNbContours(const int nbContours)
126 {
127   _params.nbContours = nbContours;
128
129   // GUI helper:
130   setIntProperty(MEDPresentationContour::PROP_NB_CONTOUR, nbContours);
131
132   // Update the pipeline:
133   {
134     MEDPyLockWrapper lock;
135     std::string cmd = getContourCommand();
136     pushAndExecPyLine(cmd);
137     pushAndExecPyLine("pvs.Render();");
138   }
139 }