Salome HOME
08a0beb8d0fbb3e48655253d9ab1ea74253e9379
[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 "MEDMEM_MedMeshDriver.hxx"
25 #include "MEDMEM_EnsightMeshDriver.hxx"
26
27 #include <SUIT_FileDlg.h>
28
29 #include "Utils_SALOME_Exception.hxx"
30 #include <SalomeApp_Tools.h>
31
32 #include <qlabel.h>
33 #include <qgroupbox.h>
34 #include <qframe.h>
35 #include <qlayout.h>
36 #include <qlineedit.h>
37 #include <qbuttongroup.h>
38 #include <qradiobutton.h>
39 #include <qpushbutton.h>
40 #include <qfiledialog.h>
41 #include <qmessagebox.h>
42 #include <qcursor.h>
43
44 SelectParams::SelectParams(FilterGUI* theModule,SelectField *sel,
45                            const char* name,
46                            bool modal, WFlags fl) throw(SALOME_Exception)
47   : QDialog(FILTER::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),_size(1024),_sel(sel),_criteria(NULL),_myGradient(NULL)
48 {
49   if(sel){
50
51     // read reference field values
52     _med = sel->getMED();
53
54     // Get reference field and time step
55     _inputFile = sel->getFile();
56     _inputMesh = sel->getMesh();
57     _inputField = sel->getField();
58     _inputTS = sel->getTimeStep();
59
60     // if no reference field selection: throw exception
61     if( _inputMesh.isNull() || _inputField.isNull() ){
62       MESSAGE("Select an input Field in MED file before filtering!!");
63       delete sel;
64       throw SALOME_Exception("Salome Exception");
65     }
66
67     // read of input mesh
68     _mesh = _med->getMesh(_inputMesh);
69     _mesh->read();
70
71     // read of input field
72     deque<DT_IT_> myIteration = _med->getFieldIteration (_inputField);
73     MEDMEM::FIELD_* field = _med->getField(_inputField,myIteration[_inputTS].dt,myIteration[_inputTS].it);
74     if (dynamic_cast<MEDMEM::FIELD<double>*>(field)){
75       _myDField = (MEDMEM::FIELD<double>*)field;
76       _myDField->read();
77       _myIField = NULL;
78     }
79     else{
80       _myIField = (MEDMEM::FIELD<int>*)field;
81       _myIField->read();
82       _myDField = NULL;
83     }
84
85     // Build QT widgets
86     buildFrame();
87   }
88   // if no reference field selection: throw exception
89   else{
90     MESSAGE("Select an input Field in MED file before filtering!!");
91     throw SALOME_Exception("Salome Exception");
92   }
93
94   // Allocate histogram arrays
95   _x = new double[_size];
96   _y = new double[_size];
97
98 }
99
100 SelectParams::~SelectParams()
101 {
102   cout << "SelectParams: destructor called" << endl;
103
104   // destrcution of histogram arrays
105   delete _x;
106   delete _y;
107
108   // destruction of gradient field
109   if(_myGradient)
110     delete _myGradient;
111
112   // destruction of support of reference field: reference field is destroyed
113   // by destruction of med object in _sel destruction
114   if(_myIField)
115     delete _myIField->getSupport();
116   if(_myDField)
117     delete _myDField->getSupport();
118
119  // destruction of criteria: support and field
120   if(_criteria){
121     delete _criteria->getSupport();
122     delete _criteria;
123   }
124   
125   // destruction of SelectField object and MED object in it
126   delete _sel;
127 }
128
129 void SelectParams::buildFrame()
130 {
131   // build widgets for select filtering parameters
132   QGridLayout* _lay = new QGridLayout( this, 1, 2 );
133
134   QGroupBox* _GroupC1 = new QGroupBox( this, "GroupC1" );
135   _lay->addWidget( _GroupC1,0,0 );
136
137   _GroupC1->setTitle( tr( "FILTER_PARAMS"  ) );
138   _GroupC1->setColumnLayout(0, Qt::Vertical );
139   _GroupC1->layout()->setSpacing( 0 );
140   _GroupC1->layout()->setMargin( 0 );
141   _myGroupLayout = new QGridLayout( _GroupC1->layout() );
142   _myGroupLayout->setAlignment( Qt::AlignTop );
143   _myGroupLayout->setSpacing( 6 );
144   _myGroupLayout->setMargin( 11 );
145   _myGroupLayout->setColStretch( 0, 0 );
146   _myGroupLayout->setColStretch( 1, 1 );
147
148   int row = 0;
149
150   QString qs1(tr("FILTER_INPUT_FILE"));
151   qs1.append(basename(_inputFile));
152   _myGroupLayout->addWidget( new QLabel( qs1, _GroupC1 ), row, 0 );
153   row++;
154
155   QString qs2(tr("FILTER_INPUT_MESH"));
156   qs2.append(_inputMesh);
157   _myGroupLayout->addWidget( new QLabel( qs2, _GroupC1 ), row, 0 );
158   row++;
159
160   QString qs3(tr("FILTER_INPUT_FIELD"));
161   qs3.append(_inputField);
162   _myGroupLayout->addWidget( new QLabel( qs3, _GroupC1 ), row, 0 );
163   row++;
164
165   QString qs4(tr("FILTER_INPUT_TS"));
166   char strTS[128];
167   sprintf(strTS,"%d\0",_inputTS);
168   qs4.append(strTS);
169   _myGroupLayout->addWidget( new QLabel( qs4, _GroupC1 ), row, 0 );
170   row++;
171
172   // 0)  field function to calculate histogram (radiogroup)
173   _myFunc = new QButtonGroup( tr("FILTER_SELECT_FUNC"), _GroupC1 );
174   _myFunc->setExclusive( true );
175   _myFunc->setColumnLayout( 0, Qt::Horizontal );
176
177   _myFieldB = new QRadioButton( tr("FILTER_FIELD"), _myFunc );
178   _myFieldB->setChecked(true);
179
180   QGridLayout* convLay = new QGridLayout( _myFunc->layout() );
181   convLay->addWidget( _myFieldB, 0, 0 );
182   convLay->addWidget( _myGradB = new QRadioButton( tr("FILTER_GRADIENT"), _myFunc ), 0, 1 );
183   _myGroupLayout->addWidget( _myFunc, row, 0 );
184   row++;
185
186   // 01)  histogram size (line edit)
187   _myHSize = new QButtonGroup( tr(""), _GroupC1 );
188   _myHSize->setExclusive( true );
189   _myHSize->setColumnLayout( 0, Qt::Horizontal );
190   QGridLayout* shLay = new QGridLayout( _myHSize->layout() );
191   shLay->addWidget( _myLSH = new QLabel( tr("FILTER_SIZE_HISTO") , _myHSize ), 0, 0 );
192   shLay->addWidget( _myLESH = new QLineEdit( "1024", _myHSize ), 0, 1 );
193   _myGroupLayout->addWidget( _myHSize, row, 0 );
194   row++;
195
196   // 1)  display histogram button (pushbutton)
197   _myHisto = new QPushButton( "", _GroupC1 );
198   _myHisto->setText(tr("FILTER_DISPLAY_HISTO"));
199   _myHisto->setAutoDefault(TRUE);
200   _myHisto->setDefault(TRUE);
201   _myGroupLayout->addWidget( _myHisto, row, 0 );
202   row++;
203
204   // 2)  scale of histogram (radiogroup)
205   _myFScale = new QButtonGroup( tr("FILTER_TYPE_DISPLAY"), _GroupC1 );
206   _myFScale->setExclusive( true );
207   _myFScale->setColumnLayout( 0, Qt::Horizontal );
208
209   _myLinear = new QRadioButton( tr("FILTER_LINEAR"), _myFScale );
210   _myLinear->setChecked(true);
211   _myLog = new QRadioButton( tr("FILTER_LOG"), _myFScale );
212   _myFScale->setDisabled(true);
213
214   QGridLayout* scaleLay = new QGridLayout( _myFScale->layout() );
215   scaleLay->addWidget( _myLinear, 0, 0 );
216   scaleLay->addWidget( _myLog, 0, 1 );
217   _myGroupLayout->addWidget( _myFScale, row, 0 );
218   row++;
219
220   // 3)  number of thresholds (radiogroup)
221   _myNbThresh = new QButtonGroup( tr("FILTER_SEL_THRESH"), _GroupC1 );
222   _myNbThresh->setExclusive( true );
223   _myNbThresh->setColumnLayout( 0, Qt::Horizontal );
224   QGridLayout* nbtLay = new QGridLayout( _myNbThresh->layout() );
225   nbtLay->addWidget( _myOneThresh = new QRadioButton( tr("FILTER_ONE_THRESH"), _myNbThresh ), 0, 0 );
226   nbtLay->addWidget( _myTwoThresh = new QRadioButton( tr("FILTER_TWO_THRESH"), _myNbThresh ), 0, 1 );
227   _myGroupLayout->addWidget( _myNbThresh, row, 0 );
228
229   _myOneThresh->setChecked(true);
230   _myNbThresh->setDisabled(true);
231   row++;
232
233   // 4)  reference area on thresholds (radiogroup)
234   _myArea = new QButtonGroup( tr("FILTER_REF_AREA"), _GroupC1 );
235   _myArea->setExclusive( true );
236   _myArea->setColumnLayout( 0, Qt::Horizontal );
237   QGridLayout* areaLay = new QGridLayout( _myArea->layout() );
238   areaLay->addWidget( _myInt = new QRadioButton( tr("FILTER_BOTTOM"), _myArea ), 0, 0 );
239   areaLay->addWidget( _myExt = new QRadioButton( tr("FILTER_UP"), _myArea ), 0, 1 );
240   _myGroupLayout->addWidget( _myArea, row, 0 );
241
242   _myExt->setChecked(true);
243   _myArea->setDisabled(true);
244   row++;
245
246   // 5)  threshold values (line edit)
247   _myVThresh = new QButtonGroup( tr("FILTER_TRESH_VAL"), _GroupC1 );
248   _myVThresh->setExclusive( true );
249   _myVThresh->setColumnLayout( 0, Qt::Horizontal );
250   QGridLayout* ftLay = new QGridLayout( _myVThresh->layout() );
251   ftLay->addWidget( _myLFT = new QLabel( tr("FILTER_VAL_TRESH") , _myVThresh ), 0, 0 );
252   ftLay->addWidget( _myLEFT = new QLineEdit( "", _myVThresh ), 0, 1 );
253   ftLay->addWidget( _myLST = new QLabel( tr("FILTER_VAL_2_TRESH") , _myVThresh ), 1, 0 );
254   ftLay->addWidget( _myLEST = new QLineEdit( "", _myVThresh ), 1, 1 );
255   _myGroupLayout->addWidget( _myVThresh, row, 0 );
256
257   _myVThresh->setDisabled(true);
258   _myLST->hide();
259   _myLEST->hide();
260   row++;
261
262   // 6)  output file name (line edit)
263   _myOutFile = new QButtonGroup( tr("FILTER_OUT_FILE"), _GroupC1 );
264   _myOutFile->setExclusive( true );
265   _myOutFile->setColumnLayout( 0, Qt::Horizontal );
266
267   _myOFB = new QPushButton( "", _myOutFile );
268   _myOFB->setText(tr("FILTER_BROWSE"));
269   _myOFB->setAutoDefault(TRUE);
270
271   QGridLayout* outLay = new QGridLayout( _myOutFile->layout() );
272   outLay->addWidget( _myOFB, 0, 0 );
273   outLay->addWidget( _myOFN = new QLineEdit( "", _myOutFile ), 0, 1 );
274   _myGroupLayout->addWidget( _myOutFile, row, 0 );
275
276   _myOutFile->setDisabled(true);
277   row++;
278
279   // 8)  buttons Process, Close and Help 
280   _GroupButtons = new QGroupBox(_GroupC1, "GroupButtons");
281   _GroupButtons->setSizePolicy(QSizePolicy((QSizePolicy::SizeType)7, (QSizePolicy::SizeType)0, 0, 0, _GroupButtons->sizePolicy().hasHeightForWidth()));
282   _GroupButtons->setTitle(tr("" ));
283   _GroupButtons->setColumnLayout(0, Qt::Vertical);
284   _GroupButtons->layout()->setSpacing(0);
285   _GroupButtons->layout()->setMargin(0);
286   _GroupButtonsLayout = new QGridLayout(_GroupButtons->layout());
287   _GroupButtonsLayout->setAlignment(Qt::AlignTop);
288   _GroupButtonsLayout->setSpacing(6);
289   _GroupButtonsLayout->setMargin(11);
290   _myProc = new QPushButton(_GroupButtons, "buttonProcess");
291   _myProc->setText(tr("FILTER_PROCESS"));
292   _myProc->setAutoDefault(TRUE);
293   _GroupButtonsLayout->addWidget(_myProc, 0, 0);
294   _buttonHelp = new QPushButton(_GroupButtons, "buttonHelp");
295   _buttonHelp->setText(tr("FILTER_BUT_HELP" ));
296   _buttonHelp->setAutoDefault(TRUE);
297   _GroupButtonsLayout->addWidget(_buttonHelp, 0, 2);
298   _buttonCancel = new QPushButton(_GroupButtons, "buttonCancel");
299   _buttonCancel->setText(tr("FILTER_BUT_CANCEL" ));
300   _buttonCancel->setAutoDefault(TRUE);
301   _GroupButtonsLayout->addWidget(_buttonCancel, 0, 1);
302   _myGroupLayout->addWidget( _GroupButtons, row, 0 );
303   _GroupButtons->setDisabled(true);
304   row++;
305
306   _GroupC2 = new QGroupBox( this, "GroupC2" );
307   _lay->addWidget( _GroupC2,0,1 );
308
309   _GroupC2->setTitle( tr( "FILTER_HISTO" ) );
310   _GroupC2->setColumnLayout(0, Qt::Vertical );
311   _GroupC2->layout()->setSpacing( 0 );
312   _GroupC2->layout()->setMargin( 0 );
313   _myGroupLayout2 = new QGridLayout( _GroupC2->layout() );
314
315   // 9)  histogram curve
316   _myPlot = new QwtPlot(_GroupC2);
317   _myHistoCurve = _myPlot->insertCurve( QString() );
318   _myPlot->setCurvePen( _myHistoCurve, QPen( Qt::red, 1 ) );
319   _myPlot->setCurveTitle( _myHistoCurve, "Histogram" );
320
321   _myGroupLayout2->addWidget( _myPlot, 0, 0 );
322
323   // 10)  reduction rate (label)
324   QString qs5(tr("FILTER_RED_RATE"));
325   qs5.append(" = 0.5");
326   _myLRR = new QLabel( qs5, _GroupC2 );
327   _myGroupLayout2->addWidget( _myLRR, 1, 0 );
328
329   _GroupC2->hide();
330
331   _myHistoFThresh = _myPlot->insertCurve( QString() );
332   _myPlot->setCurvePen( _myHistoFThresh, QPen( Qt::black, 1 ) );
333   _myHistoSThresh = _myPlot->insertCurve( QString() );
334   _myPlot->setCurvePen( _myHistoSThresh, QPen( Qt::black, 1 ) );
335
336   connect( _myGradB, SIGNAL(clicked()), this, SLOT(gradSelected()));
337   connect( _myLESH, SIGNAL(returnPressed()), this, SLOT(enterSHisto()));
338   connect( _myHisto, SIGNAL(clicked()), this, SLOT(updateHisto()));
339   connect( _myLinear, SIGNAL(clicked()), this, SLOT(scaleSelected()));
340   connect( _myLog, SIGNAL(clicked()), this, SLOT(scaleSelected()));
341   connect( _myOneThresh, SIGNAL(clicked()), this, SLOT(nbThreshSelected()));
342   connect( _myTwoThresh, SIGNAL(clicked()), this, SLOT(nbThreshSelected()));
343   connect( _myInt, SIGNAL(clicked()), this, SLOT(areaSelected()));
344   connect( _myExt, SIGNAL(clicked()), this, SLOT(areaSelected()));
345   connect( _myLEFT, SIGNAL(returnPressed()), this, SLOT(enterFThresh()));
346   connect( _myLEST, SIGNAL(returnPressed()), this, SLOT(enterSThresh()));
347   connect( _myPlot, SIGNAL(plotMouseMoved(const QMouseEvent &)), this, SLOT(moveThresh(const QMouseEvent &)));
348   connect( _myOFB, SIGNAL(clicked()), this, SLOT(getOutFileName()));
349   connect( _myProc, SIGNAL(clicked()), this, SLOT(process()));
350   connect(_buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
351   connect(_buttonHelp, SIGNAL(clicked()),   this, SLOT(ClickOnHelp()));
352
353   _GroupC2->setMinimumSize( 500, 500 );
354   _GroupC2->setMaximumSize( 500, 500 );
355   setMinimumSize( 750, 500 );
356   setMaximumSize( 750, 500 );
357
358 }
359
360 void SelectParams::gradSelected()
361 {
362   if(!_myGradient){
363     FIELD<double> * gradient;
364     try{
365       if(_myDField)
366         gradient = _myDField->buildGradient();
367       else
368         gradient = _myIField->buildGradient();
369       _myGradient = gradient->buildNorm2Field();
370       delete gradient;
371     }
372     catch(MEDEXCEPTION& Mex){
373       _myFieldB->setChecked(true);
374       QMessageBox::information( this,
375                                 "Filtering",
376                                 "Unable to calculate gradient on vector field.\n"
377                                 "You must select a reference scalar field." );
378     }
379   }
380 }
381
382 void SelectParams::enterSHisto()
383 {
384   // Deallocate old histogram arrays
385   delete _x;
386   delete _y;
387
388   // get new histogram size
389   _size = atoi(_myLESH->text());
390
391   // Allocate new histogram arrays
392   _x = new double[_size];
393   _y = new double[_size];
394 }
395
396 void SelectParams::scaleSelected()
397 {
398   // draw linear or log Y scale depend on user
399   if( _myLinear->isChecked() ){
400     _ymin = 0.0;
401     _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::None );
402   }
403   else{
404     // set min to 0.1 for log scale
405     _ymin = 0.1;
406     _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::Logarithmic );
407   }
408   _myPlot->setAxisScale( _myPlot->curveYAxis( _myHistoCurve ), _ymin, _ymax );
409   _myPlot->replot();
410 }
411
412 void SelectParams::nbThreshSelected()
413 {
414   if( _myOneThresh->isChecked() ){
415     // if one threshold choice between bottom and up for reference area
416     _myInt->setText(tr("FILTER_BOTTOM"));
417     _myExt->setText(tr("FILTER_UP"));
418     _myLFT->setText(tr("FILTER_VAL_TRESH"));
419     _myLST->hide();
420     _myLEST->hide();
421     // draw first threshold
422     displayFThresh();
423     // erase second threshold
424     clearSThresh();
425   }
426   else{
427     // if two thresholds choice between interior and exterior fir reference area
428     _myInt->setText(tr("FILTER_INT"));
429     _myExt->setText(tr("FILTER_EXT"));
430     _myLFT->setText(tr("FILTER_VAL_1_TRESH"));
431     if(_myLST->isHidden())
432       _myLST->show();
433     if(_myLEST->isHidden())
434       _myLEST->show();
435     // draw two thresholds
436     displayFThresh();
437     displaySThresh();
438   }
439   calcRateRed();
440 }
441
442 void SelectParams::areaSelected()
443 {
444   // calculate reduction rate after thresholds selection
445   calcRateRed();
446 }
447
448 void SelectParams::enterFThresh()
449 {
450   displayFThresh();
451   calcRateRed();
452 }
453
454 void SelectParams::enterSThresh()
455 {
456   displaySThresh();
457   calcRateRed();
458 }
459
460 void SelectParams::calcHisto()
461 {
462   char strth[128];
463   vector<int> myh;
464
465   if( _myFieldB->isChecked() ){
466     // calculate histogram values on field
467     if (_myDField){
468       _myDField->getMinMax(_xmin,_xmax);
469       myh = _myDField->getHistogram(_size);
470     }
471     else{
472       int xmin, xmax;
473       _myIField->getMinMax(xmin,xmax);
474       _xmin = (double)xmin;
475       _xmax = (double)xmax;
476       myh = _myIField->getHistogram(_size);
477     }
478   }
479   else{
480     // calculate histogram values on gradient
481     _myGradient->getMinMax(_xmin,_xmax);
482     myh = _myGradient->getHistogram(_size);
483   }
484   if( _myLinear->isChecked() )
485     _ymin = 0.0;
486   else
487     _ymin = 0.1;
488   _ymax = 0.0;
489
490   for(int i=0;i<_size;i++){
491     // calculate absisses for histogram values
492     _x[i]=_xmin+(i*(_xmax-_xmin))/_size;
493     // set zero to 0.01 because pb of zeros in log display with qwt
494     if(myh[i])
495       _y[i]=(double)myh[i];
496     else
497       _y[i]=0.01;
498     if( _y[i] > _ymax )
499       _ymax = _y[i];
500   }
501
502   // init thresholds values
503   _fthresh = (_xmin + _xmax)/2.0;
504   _sthresh = (_xmin + 3.*_xmax)/4.0;
505   sprintf(strth,"%g",_fthresh);
506   _myLEFT->setText(QString(strth));
507   sprintf(strth,"%g",_sthresh);
508   _myLEST->setText(QString(strth));
509 }
510
511 void SelectParams::displayHisto()
512 {
513   // associate values to curve
514   _myPlot->setCurveData( _myHistoCurve, _x, _y, _size );
515   // give extrema values for each axis
516   _myPlot->setAxisScale( _myPlot->curveXAxis( _myHistoCurve ), _xmin, _xmax );
517   _myPlot->setAxisScale( _myPlot->curveYAxis( _myHistoCurve ), _ymin, _ymax );
518   if( _myLinear->isChecked() )
519     _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::None );
520   else
521     _myPlot->setAxisOptions(_myPlot->curveYAxis( _myHistoCurve ), QwtAutoScale::Logarithmic );
522   // associate mapping to plot to move thresholds on display
523   _qmap = _myPlot->canvasMap(_myPlot->curveXAxis( _myHistoCurve ));
524   _qmap.setDblRange(_xmin,_xmax);
525   _myPlot->replot();
526 }
527
528 void SelectParams::enableWidgets()
529 {
530   if(_GroupC2->isHidden()){
531     _myFScale->setEnabled(true);
532     _myNbThresh->setEnabled(true);
533     _myArea->setEnabled(true);
534     _myVThresh->setEnabled(true);
535     _myOutFile->setEnabled(true);
536     _GroupButtons->setEnabled(true);
537     _GroupC2->show();
538   }
539 }
540
541 void SelectParams::updateHisto()
542 {
543   calcHisto();
544   displayHisto();
545   displayFThresh();
546   if( _myTwoThresh->isChecked() )
547     displaySThresh();
548   calcRateRed();
549   enableWidgets();
550 }
551
552 void SelectParams::displayFThresh()
553 {
554   _fthresh = atof(_myLEFT->text());
555
556   // draw first threshold curve
557   for(int i=0;i<100;i++){
558     _xft[i]=_fthresh;
559     _yft[i]=((i-1)*_ymax)/100.0;
560   }
561   _myPlot->setCurveData( _myHistoFThresh, _xft, _yft, 100 );
562   _myPlot->replot();
563 }
564
565 void SelectParams::displaySThresh()
566 {
567   _sthresh = atof(_myLEST->text());
568
569   // draw second threshold curve
570   for(int i=0;i<100;i++){
571     _xst[i]=_sthresh;
572     _yst[i]=((i-1)*_ymax)/100.0;
573   }
574   _myPlot->setCurveData( _myHistoSThresh, _xst, _yst, 100 );
575   _myPlot->replot();
576 }
577
578 void SelectParams::clearSThresh()
579 {
580   _sthresh = atof(_myLEST->text());
581
582   // erase second threshold curve
583   for(int i=0;i<100;i++){
584     _xst[i]=_sthresh;
585     _yst[i]=0.0;
586   }
587   _myPlot->setCurveData( _myHistoSThresh, _xst, _yst, 100 );
588   _myPlot->replot();
589 }
590
591 void SelectParams::moveThresh(const QMouseEvent &e)
592 {
593   char strth[128];
594
595   // move threshold curve with mouse
596   int delta = abs(e.x()-_qmap.transform(_fthresh));
597   if( delta < 5 ){
598     // moving first threshold curve
599     sprintf(strth,"%g",_qmap.invTransform(e.x()));
600     _myLEFT->setText(QString(strth));
601     displayFThresh();
602   }
603   else if( _myTwoThresh->isChecked() ){
604     delta = abs(e.x()-_qmap.transform(_sthresh));
605     if( delta < 5 ){
606       // moving second threshold curve
607       sprintf(strth,"%g",_qmap.invTransform(e.x()));
608       _myLEST->setText(QString(strth));
609       displaySThresh();
610     }
611   }
612   calcRateRed();
613 }
614
615 void SelectParams::getOutFileName()
616 {
617   // get output MED file name
618   QString file = QFileDialog::getSaveFileName("",
619                                               "*.med",
620                                               _myOFB,
621                                               "save file dialog",
622                                               "Choose a file to save" );
623   if(!file.isEmpty())
624     _myOFN->setText(file);
625
626   // Selection du Fichier
627 //   file = SUIT_FileDlg::getFileName(application()->desktop(),
628 //                                 "",
629 //                                 filtersList,
630 //                                 "Output MED file name",
631 //                                 true);
632 }
633
634 void SelectParams::process()
635 {
636   string command;
637
638   MESSAGE("Input MED File : "<<_inputFile);
639   MESSAGE("Input Mesh : "<<_inputMesh);
640   MESSAGE("Input Field : "<<_inputField);
641   MESSAGE("Input Time Step : "<<_inputTS);
642   MESSAGE("Output file name: " << _myOFN->text() );
643
644   setCursor(QCursor(Qt::WaitCursor));
645
646   // generate MED integer field (0 or 1) for filtoo input
647   try{
648     generateCriteria();
649   }
650   catch ( SALOME_Exception& S_ex ){
651     QMessageBox::information( this,
652                               "Filtering",
653                               "Unable to process filtering.\n"
654                               "You must select a reference field on nodes." );
655     reject();
656     return;
657   }
658
659   // generate MED boundary of geometry
660   SUPPORT *supB = _mesh->getBoundaryElements(MED_FACE);
661
662   // call ensight driver MED to generate input ensight mesh ,
663   // input ensight boundary mesh and input criteria ensight field for filtoo
664
665   int id;
666   ENSIGHT_MESH_DRIVER myMeshDriver("/tmp/input.geom",_mesh);
667   myMeshDriver.addSupport(supB);
668   id=_mesh->addDriver(myMeshDriver);
669   _mesh->write(id);
670   ENSIGHT_FIELD_DRIVER<int> myFieldDriver("/tmp/input.data",_criteria);
671   id=_criteria->addDriver(myFieldDriver);
672   _criteria->write(id);
673
674   // send filtoo command
675   command = "cd /tmp;filtoo -f input -o output > /tmp/filter.log";
676   MESSAGE(command);
677   system(command.c_str());
678
679   // destroy filtoo input files
680 //   command = "cd /tmp;rm -f input.*";
681 //   MESSAGE(command);
682 //   system(command.c_str());
683
684   // have to call ensight driver MED to generate output MED file from filtoo output
685
686   // create fields on new mesh
687   // if new nodes on new mesh: interpolation
688   // if input fields on cell, transform it on nodes
689
690   // destroy filtoo output files
691
692   // create new MED file with new mesh and fields
693
694   // close the window
695   accept();
696
697 }
698
699 void SelectParams::generateCriteria() throw(SALOME_Exception)
700 {
701   double val, min, max;
702   bool isGVal;
703   MED_EN::medEntityMesh typ;
704
705   // if reference field is on elements create reference field on nodes
706   if(_myDField)
707     typ = _myDField->getSupport()->getEntity();
708   else
709     typ = _myIField->getSupport()->getEntity();
710
711   switch(typ){
712   case MED_CELL:
713     MESSAGE("REFERENCE FIELD ON CELL");
714     throw SALOME_Exception("Filter run only on reference field on nodes");
715     break;
716   case MED_FACE:
717     MESSAGE("REFERENCE FIELD ON FACE");
718     throw SALOME_Exception("Filter run only on reference field on nodes");
719     break;
720   case MED_EDGE:
721     MESSAGE("REFERENCE FIELD ON EDGE");
722     throw SALOME_Exception("Filter run only on reference field on nodes");
723     break;
724   case MED_NODE:
725     MESSAGE("REFERENCE FIELD ON NODES");
726     break;
727   case MED_ALL_ENTITIES:
728     MESSAGE("REFERENCE FIELD ON ALL ELEMENTS");
729     throw SALOME_Exception("Filter run only on reference field on nodes");
730     break;
731   }
732
733   // create support on nodes
734   SUPPORT *sup = new SUPPORT(_mesh,"Support",MED_NODE);
735
736   // create integer field on nodes
737   _criteria = new FIELD<int>(sup,1);
738
739   _criteria->setName("Criteria");
740
741   // read number of nodes
742   int NumberOf = sup->getNumberOfElements(MED_ALL_ELEMENTS);
743
744   for (int i=1; i<NumberOf+1; i++){
745
746     // read reference field value
747     if(_myDField)
748       val = _myDField->getValueIJ(i,1);
749     else
750       val = (double)_myIField->getValueIJ(i,1);
751
752     // set criteria field value
753     if( _myOneThresh->isChecked() ){
754       if( _myExt->isChecked() )
755         if( val >= _fthresh ) isGVal = true;
756         else isGVal = false;
757       else
758         if( val <= _fthresh ) isGVal = true;
759         else isGVal = false;
760     }
761     else{
762       min = _fthresh;
763       max = _sthresh;
764       if(_sthresh < _fthresh){ 
765         min = _sthresh;
766         max = _fthresh;
767       }
768       if( _myExt->isChecked() )
769         if( (val <= min) || (val >= max) ) isGVal = true;
770         else isGVal = false;    
771       else
772         if( (val >= min) && (val <= max) ) isGVal = true;
773         else isGVal = false;    
774     }
775     if( isGVal )
776       _criteria->setValueIJ(i,1,1);
777     else
778       _criteria->setValueIJ(i,1,0);
779   }
780 }
781
782 void SelectParams::calcRateRed()
783 {
784   int i1, i2, i;
785   int atot=0, asel=0, atot1;
786   double rateRed=0.0;
787
788   // calculate reduction rate depend on reference area defined by threshold values
789   i1 = (int)((double)_size * ( _fthresh - _xmin ) / ( _xmax - _xmin ));
790   if( _myOneThresh->isChecked() ){
791     for(i=0;i<i1;i++)
792       atot += (int)_y[i];
793     asel = atot;
794     for(i=i1;i<_size;i++)
795       atot += (int)_y[i];
796     if( _myExt->isChecked() )
797       asel = atot - asel;
798   }
799   else{
800     i2 = (int)((double)_size * ( _sthresh - _xmin ) / ( _xmax - _xmin ));
801     if( i2 < i1 ){
802       i=i1;
803       i1=i2;
804       i2=i;
805     }
806     for(i=0;i<i1;i++)
807       atot += (int)_y[i];
808     atot1=atot;
809     for(i=i1;i<i2;i++)
810       atot += (int)_y[i];
811     asel = atot - atot1;
812     for(i=i2;i<_size;i++)
813       atot += (int)_y[i];
814     if( _myExt->isChecked() )
815       asel = atot - asel;
816   }
817   rateRed = (double)asel / (double) atot;
818
819   // display reduction rate value
820   QString qs(tr("FILTER_RED_RATE"));
821   char str[128];
822   sprintf(str," = %4.2g",rateRed);
823   qs.append(str);
824   _myLRR->setText( qs );
825
826 }
827
828 void SelectParams::ClickOnCancel()
829 {
830   MESSAGE("click on Cancel");
831   reject();
832 }
833
834 void SelectParams::ClickOnHelp()
835 {
836   MESSAGE("click on Help");
837 }