Salome HOME
Imported using TkCVS
[modules/filter.git] / src / FILTERGUI / SelectParams.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <stdlib.h>
22 #include <math.h>
23 #include "SelectParams.h"
24 #include "utilities.h"
25
26 #include <SUIT_FileDlg.h>
27
28 #include "Utils_SALOME_Exception.hxx"
29 #include <SalomeApp_Tools.h>
30
31 #include <qlabel.h>
32 #include <qgroupbox.h>
33 #include <qframe.h>
34 #include <qlayout.h>
35 #include <qlineedit.h>
36 #include <qbuttongroup.h>
37 #include <qradiobutton.h>
38 #include <qpushbutton.h>
39 #include <qfiledialog.h>
40
41 SelectParams::SelectParams(SelectField *sel)
42   : _size(1024),
43   _myExpr( 0 ),
44   _myFunc( 0 ),
45   _myFieldB( 0 ),
46   _myLinear( 0 ),
47   _myHisto( 0 ),
48   _myNbThresh( 0 ),
49   _myCutNeg( 0 ),
50   _myOneThresh( 0 ),
51   _myTwoThresh( 0 ),
52   _myArea( 0 ),
53   _myVThresh( 0 ),
54   _myOutFile( 0 ),
55   _myOFB( 0 ),
56   _myProc( 0 ),
57   _myFScale( 0 ),
58   _myGroupLayout( 0 ),
59   _myGroupLayout2( 0 ),
60   _myPlot( 0 ),
61   _GroupC1( 0 ),
62   _GroupC2( 0 ),
63   _lay( 0 ),
64   _fr( 0 )
65 {
66   _x = new double[_size];
67   _y = new double[_size];
68   if(sel){
69     _inputFile = sel->getFile();
70     _inputMesh = sel->getMesh();
71     _inputField = sel->getField();
72     _inputTS = sel->getTimeStep();
73     if( _inputMesh.isNull() || _inputField.isNull() ){
74       MESSAGE("Select an input Field in MED file before filtering!!");
75       throw SALOME_Exception("Salome Exception");
76     }
77   }
78   else{
79     MESSAGE("Select an input Field in MED file before filtering!!");
80     throw SALOME_Exception("Salome Exception");
81   }
82
83 }
84
85 SelectParams::~SelectParams()
86 {
87   cout << "destructor called" << endl;
88 //   delete _x;
89 //   delete _y;
90 //   delete _myExpr;
91 //   delete _myFunc;
92 //   delete _myFieldB;
93 //   delete _myLinear;
94 //   delete _myHisto;
95 //   delete _myNbThresh;
96 //   delete _myCutNeg;
97 //   delete _myOneThresh;
98 //   delete _myTwoThresh;
99 //   delete _myArea;
100 //   delete _myVThresh;
101 //   delete _myOutFile;
102 //   delete _myOFB;
103 //   delete _myProc;
104 //   delete _myFScale;
105 //   delete _myGroupLayout;
106 //   delete _myGroupLayout2;
107 //   delete _myPlot;
108 //   delete _GroupC1;
109 //   delete _GroupC2;
110 //   delete _lay;
111 //   delete _fr;
112 }
113
114 QFrame* SelectParams::buildFrame()
115 {
116   QFrame* _fr = new QFrame( 0, "myframe" );
117   QGridLayout* _lay = new QGridLayout( _fr, 1, 2 );
118
119   QGroupBox* _GroupC1 = new QGroupBox( _fr, "GroupC1" );
120   _lay->addWidget( _GroupC1,0,0 );
121
122   _GroupC1->setTitle( tr( "Filtering parameters"  ) );
123   _GroupC1->setColumnLayout(0, Qt::Vertical );
124   _GroupC1->layout()->setSpacing( 0 );
125   _GroupC1->layout()->setMargin( 0 );
126   _myGroupLayout = new QGridLayout( _GroupC1->layout() );
127   _myGroupLayout->setAlignment( Qt::AlignTop );
128   _myGroupLayout->setSpacing( 6 );
129   _myGroupLayout->setMargin( 11 );
130   _myGroupLayout->setColStretch( 0, 0 );
131   _myGroupLayout->setColStretch( 1, 1 );
132
133   int row = 0;
134
135   QString qs1("Input File : ");
136   qs1.append(basename(_inputFile));
137   _myGroupLayout->addWidget( new QLabel( qs1, _GroupC1 ), row, 0 );
138   row++;
139
140   QString qs2("Input Mesh : ");
141   qs2.append(_inputMesh);
142   _myGroupLayout->addWidget( new QLabel( qs2, _GroupC1 ), row, 0 );
143   row++;
144
145   QString qs3("Input Field : ");
146   qs3.append(_inputField);
147   _myGroupLayout->addWidget( new QLabel( qs3, _GroupC1 ), row, 0 );
148   row++;
149
150   char strTS[128];
151   sprintf(strTS,"Input Time Step : %d\0",_inputTS);
152   QString qs4(strTS);
153   _myGroupLayout->addWidget( new QLabel( qs4, _GroupC1 ), row, 0 );
154   row++;
155
156   // 0)  field function to calculate histogram (radiogroup)
157   _myFunc = new QButtonGroup( "Select function on field", _GroupC1 );
158   _myFunc->setExclusive( true );
159   _myFunc->setColumnLayout( 0, Qt::Horizontal );
160
161   _myFieldB = new QRadioButton( "Field", _myFunc );
162   _myFieldB->setChecked(true);
163
164   QGridLayout* convLay = new QGridLayout( _myFunc->layout() );
165   convLay->addWidget( _myFieldB, 0, 0 );
166   convLay->addWidget( _myCutNeg = new QRadioButton( "Gradient", _myFunc ), 0, 1 );
167   _myGroupLayout->addWidget( _myFunc, row, 0 );
168   row++;
169
170   // 1)  scale of histogram (radiogroup)
171   _myFScale = new QButtonGroup( "Type of display", _GroupC1 );
172   _myFScale->setExclusive( true );
173   _myFScale->setColumnLayout( 0, Qt::Horizontal );
174
175   _myLinear = new QRadioButton( "Linear", _myFScale );
176   _myLinear->setChecked(true);
177
178   QGridLayout* scaleLay = new QGridLayout( _myFScale->layout() );
179   scaleLay->addWidget( _myLinear, 0, 0 );
180   scaleLay->addWidget( new QRadioButton( "Logarithm", _myFScale ), 0, 1 );
181   _myGroupLayout->addWidget( _myFScale, row, 0 );
182   row++;
183
184   // 2)  display histogram button (pushbutton)
185   _myHisto = new QPushButton( "Display Histogram", _GroupC1 );
186   _myHisto->setText("Display Histogram");
187   _myHisto->setAutoDefault(TRUE);
188   _myHisto->setDefault(TRUE);
189   _myGroupLayout->addWidget( _myHisto, row, 0 );
190   row++;
191
192   // 3)  number of thresholds (radiogroup)
193   _myNbThresh = new QButtonGroup( "Select number of thresholds", _GroupC1 );
194   _myNbThresh->setExclusive( true );
195   _myNbThresh->setColumnLayout( 0, Qt::Horizontal );
196   QGridLayout* nbtLay = new QGridLayout( _myNbThresh->layout() );
197   nbtLay->addWidget( _myOneThresh = new QRadioButton( "One threshold", _myNbThresh ), 0, 0 );
198   nbtLay->addWidget( _myTwoThresh = new QRadioButton( "Two thresholds", _myNbThresh ), 0, 1 );
199   _myGroupLayout->addWidget( _myNbThresh, row, 0 );
200
201 //   _myOneThresh->setChecked(true);
202   _myNbThresh->hide();
203   row++;
204
205   // 4)  reference area on thresholds (radiogroup)
206   _myArea = new QButtonGroup( "Select reference area", _GroupC1 );
207   _myArea->setExclusive( true );
208   _myArea->setColumnLayout( 0, Qt::Horizontal );
209   QGridLayout* areaLay = new QGridLayout( _myArea->layout() );
210   areaLay->addWidget( _myInt = new QRadioButton( "Interior", _myArea ), 0, 0 );
211   areaLay->addWidget( _myExt = new QRadioButton( "Exterior", _myArea ), 0, 1 );
212   _myGroupLayout->addWidget( _myArea, row, 0 );
213
214   _myExt->setChecked(true);
215   _myArea->hide();
216   row++;
217
218   // 5)  threshold values (line edit)
219   _myVThresh = new QButtonGroup( "Threshold values", _GroupC1 );
220   _myVThresh->setExclusive( true );
221   _myVThresh->setColumnLayout( 0, Qt::Horizontal );
222   QGridLayout* ftLay = new QGridLayout( _myVThresh->layout() );
223   ftLay->addWidget( _myLFT = new QLabel( "threshold value   " , _myVThresh ), 0, 0 );
224   ftLay->addWidget( _myLEFT = new QLineEdit( "", _myVThresh ), 0, 1 );
225   ftLay->addWidget( _myLST = new QLabel( "2d threshold value" , _myVThresh ), 1, 0 );
226   ftLay->addWidget( _myLEST = new QLineEdit( "", _myVThresh ), 1, 1 );
227   _myGroupLayout->addWidget( _myVThresh, row, 0 );
228
229   _myVThresh->hide();
230   _myLST->hide();
231   _myLEST->hide();
232   row++;
233
234   // 6)  output file name (line edit)
235   _myOutFile = new QButtonGroup( "Output file name", _GroupC1 );
236   _myOutFile->setExclusive( true );
237   _myOutFile->setColumnLayout( 0, Qt::Horizontal );
238
239   _myOFB = new QPushButton( "Browse", _myOutFile );
240   _myOFB->setText("Browse");
241   _myOFB->setAutoDefault(TRUE);
242
243   QGridLayout* outLay = new QGridLayout( _myOutFile->layout() );
244   outLay->addWidget( _myOFB, 0, 0 );
245   outLay->addWidget( _myOFN = new QLineEdit( "", _myOutFile ), 0, 1 );
246   _myGroupLayout->addWidget( _myOutFile, row, 0 );
247
248   _myOutFile->hide();
249   row++;
250
251   // 8)  process button (pushbutton)
252   _myProc = new QPushButton( "Process", _GroupC1 );
253   _myProc->setText("Process");
254   _myProc->setAutoDefault(TRUE);
255   _myGroupLayout->addWidget( _myProc, row, 0 );
256   _myProc->hide();
257   row++;
258
259   _GroupC2 = new QGroupBox( _fr, "GroupC2" );
260   _lay->addWidget( _GroupC2,0,1 );
261
262   _GroupC2->setTitle( tr( "Histogram"  ) );
263   _GroupC2->setColumnLayout(0, Qt::Vertical );
264   _GroupC2->layout()->setSpacing( 0 );
265   _GroupC2->layout()->setMargin( 0 );
266   _myGroupLayout2 = new QGridLayout( _GroupC2->layout() );
267
268   _myPlot = new QwtPlot(_GroupC2);
269   _myHistoCurve = _myPlot->insertCurve( QString() );
270   _myPlot->setCurvePen( _myHistoCurve, QPen( Qt::red, 1 ) );
271   _myPlot->setCurveTitle( _myHistoCurve, "Histogram" );
272
273   _myGroupLayout2->addWidget( _myPlot, 0, 0 );
274
275   // 9)  reduction rate (label)
276   _myGroupLayout2->addWidget( _myLRR = new QLabel( "reduction rate = 0.23", _GroupC2 ), 1, 0 );
277
278   _GroupC2->hide();
279   _myLRR->hide();
280
281   _myHistoFThresh = _myPlot->insertCurve( QString() );
282   _myPlot->setCurvePen( _myHistoFThresh, QPen( Qt::black, 1 ) );
283   _myHistoSThresh = _myPlot->insertCurve( QString() );
284   _myPlot->setCurvePen( _myHistoSThresh, QPen( Qt::black, 1 ) );
285
286   connect( _myHisto, SIGNAL(clicked()), this, SLOT(displayHisto()));
287   connect( _myOneThresh, SIGNAL(clicked()), this, SLOT(nbThreshSelected()));
288   connect( _myTwoThresh, SIGNAL(clicked()), this, SLOT(nbThreshSelected()));
289   connect( _myInt, SIGNAL(clicked()), this, SLOT(areaSelected()));
290   connect( _myExt, SIGNAL(clicked()), this, SLOT(areaSelected()));
291   connect( _myLEFT, SIGNAL(returnPressed()), this, SLOT(displayFThresh()));
292   connect( _myLEST, SIGNAL(returnPressed()), this, SLOT(displaySThresh()));
293   connect( _myPlot, SIGNAL(plotMouseMoved(const QMouseEvent &)), this, SLOT(moveThresh(const QMouseEvent &)));
294   connect( _myOFB, SIGNAL(clicked()), this, SLOT(getOutFileName()));
295   connect( _myProc, SIGNAL(clicked()), this, SLOT(process()));
296
297   calcHisto();
298
299   return _fr;
300 }
301
302 void SelectParams::nbThreshSelected()
303 {
304   if( _myOneThresh->isChecked() ){
305     _myInt->setText("bottom");
306     _myExt->setText("up");
307     _myLFT->setText("threshold value   ");
308     _myLST->hide();
309     _myLEST->hide();
310     displayFThresh();
311     clearSThresh();
312   }
313   else{
314     _myInt->setText("interior");
315     _myExt->setText("exterior");
316     _myLFT->setText("1st threshold value");
317     if(_myLST->isHidden())
318       _myLST->show();
319     if(_myLEST->isHidden())
320       _myLEST->show();
321     displayFThresh();
322     displaySThresh();
323   }
324   calcRateRed();
325   if(_myArea->isHidden())
326     _myArea->show();
327   if(_myVThresh->isHidden())
328     _myVThresh->show();
329   if(_myLRR->isHidden())
330     _myLRR->show();
331   if(_myOutFile->isHidden())
332     _myOutFile->show();
333   if(_myProc->isHidden())
334     _myProc->show();
335 }
336
337 void SelectParams::areaSelected()
338 {
339   calcRateRed();
340 }
341
342 void SelectParams::calcHisto()
343 {
344   char strth[128];
345   _xmin = -50.0;
346   _xmax = 50.0;
347   _ymax = 100.0;
348   _ymin = _ymax;
349
350   for(int i=0;i<_size;i++){
351     _x[i]=_xmin+(i*(_xmax-_xmin))/_size;
352     _y[i]=80.*exp(-_x[i]*_x[i]/400.);
353     if( (_y[i] < _ymin) && (_y[i] != 0.0) )
354       _ymin = _y[i];
355   }
356   _myPlot->setAxisScale( _myPlot->curveXAxis( _myHistoCurve ), _xmin, _xmax );
357   _myPlot->setAxisScale( _myPlot->curveYAxis( _myHistoCurve ), _ymin, _ymax );
358   _myPlot->setCurveData( _myHistoCurve, _x, _y, _size );
359   _fthresh = (_xmin + _xmax)/2.0;
360   _sthresh = (_xmin + 3.*_xmax)/4.0;
361   sprintf(strth,"%g",_fthresh);
362   _myLEFT->setText(QString(strth));
363   sprintf(strth,"%g",_sthresh);
364   _myLEST->setText(QString(strth));
365 }
366
367 void SelectParams::displayHisto()
368 {
369   _qmap = _myPlot->canvasMap(_myPlot->curveXAxis( _myHistoCurve ));
370   _qmap.setDblRange(_xmin,_xmax);
371   if( _myLinear->isChecked() )
372     _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::None );
373   else
374     _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::Logarithmic );
375   _myPlot->replot();
376   if(_GroupC2->isHidden())
377     _GroupC2->show();
378   if(_myNbThresh->isHidden())
379     _myNbThresh->show();
380 }
381
382 void SelectParams::displayFThresh()
383 {
384   _fthresh = atof(_myLEFT->text());
385
386   for(int i=0;i<100;i++){
387     _xft[i]=_fthresh;
388     _yft[i]=((i-1)*_ymax)/100.0;
389   }
390   _myPlot->setCurveData( _myHistoFThresh, _xft, _yft, 100 );
391   _myPlot->replot();
392 }
393
394 void SelectParams::displaySThresh()
395 {
396   _sthresh = atof(_myLEST->text());
397
398   for(int i=0;i<100;i++){
399     _xst[i]=_sthresh;
400     _yst[i]=((i-1)*_ymax)/100.0;
401   }
402   _myPlot->setCurveData( _myHistoSThresh, _xst, _yst, 100 );
403   _myPlot->replot();
404 }
405
406 void SelectParams::clearSThresh()
407 {
408   _sthresh = atof(_myLEST->text());
409
410   for(int i=0;i<100;i++){
411     _xst[i]=_sthresh;
412     _yst[i]=0.0;
413   }
414   _myPlot->setCurveData( _myHistoSThresh, _xst, _yst, 100 );
415   _myPlot->replot();
416 }
417
418 void SelectParams::moveThresh(const QMouseEvent &e)
419 {
420   char strth[128];
421   int delta = abs(e.x()-_qmap.transform(_fthresh));
422   if( delta < 5 ){
423     sprintf(strth,"%g",_qmap.invTransform(e.x()));
424     _myLEFT->setText(QString(strth));
425     displayFThresh();
426   }
427   else if( _myTwoThresh->isChecked() ){
428     delta = abs(e.x()-_qmap.transform(_sthresh));
429     if( delta < 5 ){
430       sprintf(strth,"%g",_qmap.invTransform(e.x()));
431       _myLEST->setText(QString(strth));
432       displaySThresh();
433     }
434   }
435   calcRateRed();
436 }
437
438 void SelectParams::getOutFileName()
439 {
440   QString file = QFileDialog::getSaveFileName("/home",
441                                               "*.med",
442                                               _myOFB,
443                                               "save file dialog",
444                                               "Choose a file to save" );
445   if(!file.isEmpty())
446     _myOFN->setText(file);
447
448   // Selection du Fichier
449 //   file = SUIT_FileDlg::getFileName(application()->desktop(),
450 //                                 "",
451 //                                 filtersList,
452 //                                 "Output MED file name",
453 //                                 true);
454 }
455
456 void SelectParams::process()
457 {
458   string command;
459
460   MESSAGE("Input MED File : "<<_inputFile);
461   MESSAGE("Input Mesh : "<<_inputMesh);
462   MESSAGE("Input Field : "<<_inputField);
463   MESSAGE("Input Time Step : "<<_inputTS);
464   MESSAGE("Output file name: " << _myOFN->text() );
465   command = "cd /export/home/secher/filtoo/example;filtoo -f s -o sf > /tmp/filter.log";
466   MESSAGE(command);
467   system(command.c_str());
468 //   delete this;
469 }
470
471 void SelectParams::calcRateRed()
472 {
473   int i1, i2, i;
474   int atot=0, asel=0, atot1;
475   double rateRed=0.0;
476   char strth[128];
477
478   i1 = (int)((double)_size * ( _fthresh - _xmin ) / ( _xmax - _xmin ));
479   if( _myOneThresh->isChecked() ){
480     for(i=0;i<i1;i++)
481       atot += (int)_y[i];
482     asel = atot;
483     for(i=i1;i<_size;i++)
484       atot += (int)_y[i];
485     if( _myExt->isChecked() )
486       asel = atot - asel;
487   }
488   else{
489     i2 = (int)((double)_size * ( _sthresh - _xmin ) / ( _xmax - _xmin ));
490     if( i2 < i1 ){
491       i=i1;
492       i1=i2;
493       i2=i;
494     }
495     for(i=0;i<i1;i++)
496       atot += (int)_y[i];
497     atot1=atot;
498     for(i=i1;i<i2;i++)
499       atot += (int)_y[i];
500     asel = atot - atot1;
501     for(i=i2;i<_size;i++)
502       atot += (int)_y[i];
503     if( _myExt->isChecked() )
504       asel = atot - asel;
505   }
506   rateRed = (double)asel / (double) atot;
507   sprintf(strth,"reduction rate = %4.2g",rateRed);
508   _myLRR->setText( strth );
509
510
511 }
512