Back to index

salome-gui  6.5.0
Plot2d_NormalizeAlgorithm.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : Plot2d_NormalizeAlgorithm.cxx
00023 
00024 #include "Plot2d_NormalizeAlgorithm.h"
00025 #include <QMap>
00026 #include <algorithm>
00027 #include "Plot2d_Object.h"
00028 
00032 Plot2d_NormalizeAlgorithm::Plot2d_NormalizeAlgorithm(QObject *parent) :
00033   Plot2d_Algorithm(parent),
00034   myNormalizationMode(NormalizeNone)
00035 {
00036 }
00037 
00041 Plot2d_NormalizeAlgorithm::~Plot2d_NormalizeAlgorithm()
00042 {
00043 }
00044 
00048 void Plot2d_NormalizeAlgorithm::setNormalizationMode(NormalizationMode theMode) {
00049   if(myNormalizationMode != theMode) {
00050     myNormalizationMode = theMode;
00051     myDataChanged = true;
00052   }
00053 }
00054 
00058 Plot2d_NormalizeAlgorithm::NormalizationMode Plot2d_NormalizeAlgorithm::getNormalizationMode()const {
00059   return myNormalizationMode;
00060 }
00061 
00065 double Plot2d_NormalizeAlgorithm::getKkoef(Plot2d_Object* theObj) 
00066 {
00067   QMap<Plot2d_Object*,double>::iterator it = myKkoefs.find(theObj);
00068   if(it != myKkoefs.end())
00069     return it.value();
00070   return 0;
00071 }
00072 
00076 double Plot2d_NormalizeAlgorithm::getBkoef(Plot2d_Object* theObj)
00077 {
00078   QMap<Plot2d_Object*,double>::iterator it = myBkoefs.find(theObj);
00079   if(it != myBkoefs.end())
00080     return it.value();
00081   return 0;
00082 
00083 }
00084 
00085 void  Plot2d_NormalizeAlgorithm::execute() {
00086  if (!isDataChanged() || myInuptData.isEmpty())
00087     return;
00088 
00089   if (myNormalizationMode != NormalizeNone) {
00090     QList<double> yMinLst, yMaxLst;
00091     QList<double> aKkoefs,aBkoefs;
00092     double _pMin, _pMax;
00093     for (int i = 0; i < myInuptData.size(); ++i) {
00094       QList<double> aTmpItemValues;
00095       Plot2d_Object* object = myInuptData.at(i);
00096       double *x, *y;
00097       long nb = object->getData( &x, &y );
00098       for (int j = 0; j < nb; ++j) {
00099         aTmpItemValues<<object->getPoint(j).y;
00100       }
00101       delete x;
00102       delete y;
00103       yMaxLst<<*(std::max_element(aTmpItemValues.begin(), aTmpItemValues.end()));
00104       yMinLst<<*(std::min_element(aTmpItemValues.begin(), aTmpItemValues.end()));
00105     }
00106     _pMin = *(std::min_element(yMinLst.begin(), yMinLst.end()));
00107     _pMax = *(std::max_element(yMaxLst.begin(), yMaxLst.end()));
00108 
00109     double pMin, pMax, kKoef, bKoef, yMin, yMax;
00110     switch( getNormalizationMode() ) {
00111       case NormalizeToMin:
00112       pMin = _pMin;
00113       for (int i = 0; i < yMaxLst.size(); ++i) {
00114         yMin = yMinLst.at(i);
00115         yMax = yMaxLst.at(i); 
00116         pMax = yMax;
00117         kKoef = (pMax - pMin)/(yMax - yMin);
00118         bKoef = pMin - kKoef * yMin;
00119         aBkoefs<<bKoef;
00120         aKkoefs<<kKoef;
00121       }
00122       break;
00123     case NormalizeToMax:
00124       pMax = _pMax;
00125       for (int i = 0; i < yMaxLst.size(); ++i) {
00126         yMin = yMinLst.at(i);
00127         yMax = yMaxLst.at(i); 
00128         pMin = yMin;
00129         kKoef = (pMax - pMin)/(yMax - yMin);
00130         bKoef = pMin - kKoef * yMin;
00131         aBkoefs<<bKoef;
00132         aKkoefs<<kKoef;
00133       }
00134       break;
00135     case NormalizeToMinMax:
00136       pMax = _pMax;
00137       pMin = _pMin;
00138       for (int i = 0; i < yMaxLst.size(); ++i) {
00139         yMin = yMinLst.at(i);
00140         yMax = yMaxLst.at(i); 
00141         kKoef = (pMax - pMin)/(yMax - yMin);
00142         bKoef = pMin - kKoef * yMin;
00143         aBkoefs<<bKoef;
00144         aKkoefs<<kKoef;
00145       }
00146       break;
00147     }
00148 
00149     for (int i = 0; i < myInuptData.size(); ++i) {
00150       double yOld,xOld,yNew;
00151       AlgoPlot2dItem tmpItem;
00152       for (int j = 0; j < myInuptData.at(i)->nbPoints(); ++j) {
00153         yOld = myInuptData.at(i)->getPoint(j).y;
00154         xOld = myInuptData.at(i)->getPoint(j).x;
00155         yNew = aKkoefs.at(i) * yOld + aBkoefs.at(i);
00156         tmpItem.append( qMakePair(xOld, yNew) );
00157       }
00158       myResultData.insert(myInuptData.at(i),tmpItem);
00159       myKkoefs.insert(myInuptData.at(i),aKkoefs.at(i));
00160       myBkoefs.insert(myInuptData.at(i),aBkoefs.at(i));
00161     }
00162   } else {
00163     for (int i = 0; i < myInuptData.size(); ++i) {
00164       double yOld,xOld;
00165       AlgoPlot2dItem tmpItem;
00166       for (int j = 0; j < myInuptData.at(i)->nbPoints(); ++j) {
00167         yOld = myInuptData.at(i)->getPoint(j).y;
00168         xOld = myInuptData.at(i)->getPoint(j).x;
00169         tmpItem.append( qMakePair(xOld, yOld) );
00170       }
00171       myResultData.insert(myInuptData.at(i),tmpItem);
00172     }
00173   }
00174   myDataChanged = false; 
00175 }
00176 
00177 
00178 void Plot2d_NormalizeAlgorithm::clear() {
00179   Plot2d_Algorithm::clear();
00180   myBkoefs.clear();
00181   myKkoefs.clear();
00182 }