Salome HOME
3e3b78e0b0327043e092e0da8f1e799d62782f36
[modules/gui.git] / src / Plot2d / Plot2d_NormalizeAlgorithm.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : Plot2d_NormalizeAlgorithm.cxx
23
24 #include "Plot2d_NormalizeAlgorithm.h"
25 #include <QMap>
26 #include <algorithm>
27 #include "Plot2d_Object.h"
28
29 /*!
30   Constructor
31 */
32 Plot2d_NormalizeAlgorithm::Plot2d_NormalizeAlgorithm(QObject *parent) :
33   Plot2d_Algorithm(parent),
34   myNormalizationMode(NormalizeNone)
35 {
36 }
37
38 /*!
39   Destructor
40 */
41 Plot2d_NormalizeAlgorithm::~Plot2d_NormalizeAlgorithm()
42 {
43 }
44
45 /*!
46   Sets normalozation mode
47 */
48 void Plot2d_NormalizeAlgorithm::setNormalizationMode(NormalizationMode theMode) {
49   if(myNormalizationMode != theMode) {
50     myNormalizationMode = theMode;
51     myDataChanged = true;
52   }
53 }
54
55 /*!
56   Gets normalozation mode
57 */
58 Plot2d_NormalizeAlgorithm::NormalizationMode Plot2d_NormalizeAlgorithm::getNormalizationMode()const {
59   return myNormalizationMode;
60 }
61
62 /*!
63   Gets k normalization coefficient
64 */
65 double Plot2d_NormalizeAlgorithm::getKkoef(Plot2d_Object* theObj) 
66 {
67   QMap<Plot2d_Object*,double>::iterator it = myKkoefs.find(theObj);
68   if(it != myKkoefs.end())
69     return it.value();
70   return 0;
71 }
72
73 /*!
74   Gets b normalization coefficient
75 */
76 double Plot2d_NormalizeAlgorithm::getBkoef(Plot2d_Object* theObj)
77 {
78   QMap<Plot2d_Object*,double>::iterator it = myBkoefs.find(theObj);
79   if(it != myBkoefs.end())
80     return it.value();
81   return 0;
82
83 }
84
85 void  Plot2d_NormalizeAlgorithm::execute() {
86  if (!isDataChanged() || myInuptData.isEmpty())
87     return;
88
89   if (myNormalizationMode != NormalizeNone) {
90     QList<double> yMinLst, yMaxLst;
91     QList<double> aKkoefs,aBkoefs;
92     double _pMin, _pMax;
93     for (int i = 0; i < myInuptData.size(); ++i) {
94       QList<double> aTmpItemValues;
95       Plot2d_Object* object = myInuptData.at(i);
96       double *x, *y;
97       long nb = object->getData( &x, &y );
98       for (int j = 0; j < nb; ++j) {
99         aTmpItemValues<<object->getPoint(j).y;
100       }
101       delete x;
102       delete y;
103       yMaxLst<<*(std::max_element(aTmpItemValues.begin(), aTmpItemValues.end()));
104       yMinLst<<*(std::min_element(aTmpItemValues.begin(), aTmpItemValues.end()));
105     }
106     _pMin = *(std::min_element(yMinLst.begin(), yMinLst.end()));
107     _pMax = *(std::max_element(yMaxLst.begin(), yMaxLst.end()));
108
109     double pMin, pMax, kKoef, bKoef, yMin, yMax;
110     switch( getNormalizationMode() ) {
111     case NormalizeToMin:
112       pMin = _pMin;
113       for (int i = 0; i < yMaxLst.size(); ++i) {
114         yMin = yMinLst.at(i);
115         yMax = yMaxLst.at(i); 
116         pMax = yMax;
117         kKoef = (pMax - pMin)/(yMax - yMin);
118         bKoef = pMin - kKoef * yMin;
119         aBkoefs<<bKoef;
120         aKkoefs<<kKoef;
121       }
122       break;
123     case NormalizeToMax:
124       pMax = _pMax;
125       for (int i = 0; i < yMaxLst.size(); ++i) {
126         yMin = yMinLst.at(i);
127         yMax = yMaxLst.at(i); 
128         pMin = yMin;
129         kKoef = (pMax - pMin)/(yMax - yMin);
130         bKoef = pMin - kKoef * yMin;
131         aBkoefs<<bKoef;
132         aKkoefs<<kKoef;
133       }
134       break;
135     case NormalizeToMinMax:
136       pMax = _pMax;
137       pMin = _pMin;
138       for (int i = 0; i < yMaxLst.size(); ++i) {
139         yMin = yMinLst.at(i);
140         yMax = yMaxLst.at(i); 
141         kKoef = (pMax - pMin)/(yMax - yMin);
142         bKoef = pMin - kKoef * yMin;
143         aBkoefs<<bKoef;
144         aKkoefs<<kKoef;
145       }
146       break;
147     default:
148       break;
149     }
150
151     for (int i = 0; i < myInuptData.size(); ++i) {
152       double yOld,xOld,yNew;
153       AlgoPlot2dItem tmpItem;
154       for (int j = 0; j < myInuptData.at(i)->nbPoints(); ++j) {
155         yOld = myInuptData.at(i)->getPoint(j).y;
156         xOld = myInuptData.at(i)->getPoint(j).x;
157         yNew = aKkoefs.at(i) * yOld + aBkoefs.at(i);
158         tmpItem.append( qMakePair(xOld, yNew) );
159       }
160       myResultData.insert(myInuptData.at(i),tmpItem);
161       myKkoefs.insert(myInuptData.at(i),aKkoefs.at(i));
162       myBkoefs.insert(myInuptData.at(i),aBkoefs.at(i));
163     }
164   } else {
165     for (int i = 0; i < myInuptData.size(); ++i) {
166       double yOld,xOld;
167       AlgoPlot2dItem tmpItem;
168       for (int j = 0; j < myInuptData.at(i)->nbPoints(); ++j) {
169         yOld = myInuptData.at(i)->getPoint(j).y;
170         xOld = myInuptData.at(i)->getPoint(j).x;
171         tmpItem.append( qMakePair(xOld, yOld) );
172       }
173       myResultData.insert(myInuptData.at(i),tmpItem);
174     }
175   }
176   myDataChanged = false; 
177 }
178
179
180 void Plot2d_NormalizeAlgorithm::clear() {
181   Plot2d_Algorithm::clear();
182   myBkoefs.clear();
183   myKkoefs.clear();
184 }