Salome HOME
ee4628505aef623b148b0ff522c007eebc8cff3b
[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 void
40 MEDPresentationContour::setNumberContours()
41 {
42   std::ostringstream oss;
43
44   oss << "min_max = " << _srcObjVar << ".PointData.GetArray('" << _fieldName << "').GetRange();";
45   pushAndExecPyLine(oss.str()); oss.str("");
46   oss << "delta = (min_max[1]-min_max[0])/float(" << _params.nbContours << ");";
47   pushAndExecPyLine(oss.str()); oss.str("");
48   oss << _objVar << ".Isosurfaces = [min_max[0]+0.5*delta+i*delta for i in range(" << _params.nbContours << ")];";
49   pushAndExecPyLine(oss.str()); oss.str("");
50 }
51
52 void
53 MEDPresentationContour::internalGeneratePipeline()
54 {
55   MEDPresentation::internalGeneratePipeline();
56
57   MEDPyLockWrapper lock;
58
59   createSource();
60
61   // Populate internal array of available components:
62   fillAvailableFieldComponents();
63   if (getIntProperty(MEDPresentation::PROP_NB_COMPONENTS) > 1)
64     {
65       const char * msg = "Contour presentation only works for scalar field!"; // this message will appear in GUI too
66       STDLOG(msg);
67       throw KERNEL::createSalomeException(msg);
68     }
69   if (_params.nbContours < 1)
70     {
71       const char * mes = "Invalid number of contours!";
72       STDLOG(mes);
73       throw KERNEL::createSalomeException(mes);
74     }
75
76   setOrCreateRenderView(); // instanciate __viewXXX
77
78   // Contour needs point data:
79   applyCellToPointIfNeeded();
80
81   std::ostringstream oss;
82   oss << _objVar << " = pvs.Contour(Input=" << _srcObjVar << ");";
83   pushAndExecPyLine(oss.str()); oss.str("");
84
85   showObject();
86
87   oss << _objVar << ".ContourBy = ['POINTS', '" << _fieldName << "'];";
88   pushAndExecPyLine(oss.str()); oss.str("");
89
90   // Set number of contours
91   setNumberContours();
92
93   colorBy("POINTS");    // necessarily POINTS because of the conversion above
94   showScalarBar();
95   selectColorMap();
96   rescaleTransferFunction();
97   resetCameraAndRender();
98 }
99
100 void
101 MEDPresentationContour::updatePipeline(const MEDCALC::ContourParameters& params)
102 {
103   if (params.fieldHandlerId != _params.fieldHandlerId)
104     throw KERNEL::createSalomeException("Unexpected updatePipeline error! Mismatching fieldHandlerId!");
105
106   if (params.scalarBarRange != _params.scalarBarRange)
107     updateScalarBarRange<MEDPresentationContour, MEDCALC::ContourParameters>(params.scalarBarRange);
108   if (params.colorMap != _params.colorMap)
109     updateColorMap<MEDPresentationContour, MEDCALC::ContourParameters>(params.colorMap);
110
111   if (params.nbContours != _params.nbContours)
112     {
113       if (params.nbContours < 1)
114         {
115           const char * mes = "Invalid number of contours!";
116           STDLOG(mes);
117           throw KERNEL::createSalomeException(mes);
118         }
119       updateNbContours(params.nbContours);
120     }
121 }
122
123 void
124 MEDPresentationContour::updateNbContours(const int nbContours)
125 {
126   _params.nbContours = nbContours;
127
128   // GUI helper:
129   setIntProperty(MEDPresentationContour::PROP_NB_CONTOUR, nbContours);
130
131   // Update the pipeline:
132   {
133     MEDPyLockWrapper lock;
134     setNumberContours();
135     pushAndExecPyLine("pvs.Render();");
136   }
137 }