]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_ProfileInterpolateOp.cxx
Salome HOME
Final version of "Profile interpolation".
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ProfileInterpolateOp.cxx
1 // Copyright (C) 2007-2015  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
23 #include <HYDROGUI_ProfileInterpolateOp.h>
24
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_DataObject.h"
28 #include "HYDROGUI_UpdateFlags.h"
29 #include "HYDROGUI_ProfileInterpolateDlg.h"
30
31 #include <HYDROData_Stream.h>
32 #include <HYDROData_Profile.h>
33 #include <HYDROData_Document.h>
34 #include <HYDROData_ProfileUZ.h>
35 #include <HYDROData_IPolyline.h>
36 #include <HYDROData_PolylineXY.h>
37 #include <HYDROData_InterpolatorsFactory.h>
38 #include <HYDROData_IProfilesInterpolator.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_SelectionMgr.h>
42 #include <LightApp_UpdateFlags.h>
43
44 #include <OCCViewer_ViewModel.h>
45 #include <OCCViewer_ViewManager.h>
46 #include <OCCViewer_ViewWindow.h>
47 #include <OCCViewer_AISSelector.h>
48
49 #include <AIS_ListOfInteractive.hxx>
50 #include <AIS_ListIteratorOfListOfInteractive.hxx>
51
52 #include <BRep_Builder.hxx>
53 #include <TopoDS_Compound.hxx>
54
55 #include <QMessageBox>
56
57 HYDROGUI_ProfileInterpolateOp::HYDROGUI_ProfileInterpolateOp( HYDROGUI_Module* theModule )
58     : HYDROGUI_Operation( theModule )
59 {
60     setName( tr( "PROFILE_INTERPOLATION" ) );
61 }
62
63 HYDROGUI_ProfileInterpolateOp::~HYDROGUI_ProfileInterpolateOp()
64 {
65 }
66
67 void HYDROGUI_ProfileInterpolateOp::startOperation()
68 {
69     HYDROGUI_Operation::startOperation();
70
71     HYDROGUI_ProfileInterpolateDlg* aPanel = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
72     if ( aPanel )
73     {
74         aPanel->reset();
75         aPanel->setInterpolators( interpolators() );
76     }
77 }
78
79 void HYDROGUI_ProfileInterpolateOp::abortOperation()
80 {
81     if ( !myPreview.IsNull() )
82     {
83         HYDROGUI_ProfileInterpolateDlg* aDlg = new HYDROGUI_ProfileInterpolateDlg( module(), getName() );
84         if ( aDlg && !aDlg->getAISContext().IsNull() )
85         {
86             aDlg->getAISContext()->Remove( myPreview, false );
87             myPreview.Nullify();
88         }
89     }
90
91     HYDROGUI_Operation::abortOperation();
92 }
93
94 void HYDROGUI_ProfileInterpolateOp::commitOperation()
95 {
96     if ( !myPreview.IsNull() )
97     {
98         HYDROGUI_ProfileInterpolateDlg* aDlg = new HYDROGUI_ProfileInterpolateDlg( module(), getName() );
99         if ( aDlg && !aDlg->getAISContext().IsNull() )
100         {
101             aDlg->getAISContext()->Remove( myPreview, false );
102             myPreview.Nullify();
103         }
104     }
105
106     HYDROGUI_Operation::commitOperation();
107 }
108
109 HYDROGUI_InputPanel* HYDROGUI_ProfileInterpolateOp::createInputPanel() const
110 {
111     HYDROGUI_ProfileInterpolateDlg* aDlg = new HYDROGUI_ProfileInterpolateDlg( module(), getName() );
112
113     connect( aDlg, SIGNAL( riverChanged( const QString& ) ), this, SLOT( onRiverChanged( const QString& ) ) );
114     connect( aDlg, SIGNAL( interpolatorChanged( const QString& ) ), this, SLOT( onInterpolatorChanged( const QString& ) ) );
115     connect( aDlg, SIGNAL( interpolatorParametersChanged( const QString& ) ), this, SLOT( updatePreview() ) );
116     connect( aDlg, SIGNAL( profileNumberChanged( int ) ), this, SLOT( updatePreview() ) );
117     connect( aDlg, SIGNAL( profileStartChanged( const QString& ) ), this, SLOT( updatePreview() ) );
118     connect( aDlg, SIGNAL( profileFinishChanged( const QString& ) ), this, SLOT( updatePreview() ) );
119
120     return aDlg;
121 }
122
123 bool HYDROGUI_ProfileInterpolateOp::processApply( int& theUpdateFlags, QString& theErrorMsg,
124                                                   QStringList& theBrowseObjectsEntries )
125 {
126     HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
127     if ( !aDlg )
128         return false;
129
130     QString errMessage;
131     HYDROData_IProfilesInterpolator* anIterp = interpolator( aDlg->interpolator() );
132     if ( anIterp )
133     {
134         updateInterpolator( anIterp );
135         Handle(HYDROData_Stream) aRiver = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), aDlg->river(), KIND_STREAM ) );
136         if ( !aRiver.IsNull() )
137         {
138             startDocOperation();
139             //aRiver->Interpolate( anIterp );
140             if ( anIterp->GetErrorCode() == OK )
141                 commitDocOperation();
142             else
143             {
144                 errMessage = tr( "CALCULATE_ERROR" ), QString( anIterp->GetErrorMessage().c_str() );
145                 abortDocOperation();
146             }
147         }
148         else
149             errMessage = tr( "CANT_GET_RIVER_OBJECT" ).arg( aDlg->river() );
150
151         if ( !errMessage.isEmpty() )
152             QMessageBox::critical( aDlg->topLevelWidget(), tr( "INTERPOLATION_ERROR" ), errMessage, QMessageBox::Ok );
153         else
154             commit();
155     }
156
157     return errMessage.isEmpty();
158 }
159
160 void HYDROGUI_ProfileInterpolateOp::updatePreview()
161 {
162     HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
163     if ( !aDlg )
164         return;
165
166     Handle(AIS_InteractiveContext) aCtx = aDlg->getAISContext();
167     if ( aCtx.IsNull() )
168         return;
169
170     if ( !myPreview.IsNull() )
171         aCtx->Remove( myPreview, false );
172
173     myPreview.Nullify();
174
175     HYDROData_IProfilesInterpolator* anIterp = interpolator( aDlg->interpolator() );
176     if ( anIterp )
177     {
178         updateInterpolator( anIterp );
179         if ( anIterp->Calculate() == OK )
180         {
181             TopoDS_Shape aShape = previewShape( anIterp, aDlg->profileNumber() );
182             if ( !aShape.IsNull() )
183             {
184                 myPreview = new AIS_Shape( aShape );
185                 myPreview->SetColor( Quantity_NOC_RED );
186                 aCtx->Display( myPreview, 0, -1, false );
187             }
188         }
189     }
190
191     aCtx->UpdateCurrentViewer();
192 }
193
194 void HYDROGUI_ProfileInterpolateOp::updateInterpolator( HYDROData_IProfilesInterpolator* theInt )
195 {
196     HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
197     if ( !aDlg || !theInt )
198         return;
199
200     theInt->Reset();
201     theInt->SetResultProfilesNumber( aDlg->profileNumber() );
202     theInt->SetProfiles( profile( aDlg->profileStart() ), profile( aDlg->profileFinish() ) );
203     ParamsList aParams = parameters( aDlg->interpolatorParameters() );
204     for ( ParamsList::Iterator it = aParams.begin(); it != aParams.end(); ++it )
205         theInt->SetParameter( (*it).first.toStdString(), (*it).second.toStdString() );
206 }
207
208 std::vector<double> HYDROGUI_ProfileInterpolateOp::profile( const QString& theName ) const
209 {
210     std::vector<double> aPoints;
211     Handle(HYDROData_Profile) aProf = Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_PROFILE ) );
212     if ( !aProf.IsNull() )
213     {
214         HYDROData_Profile::ProfilePoints aPntList = aProf->GetProfilePoints();
215         for ( int i = aPntList.Lower(); i <= aPntList.Upper(); i++ )
216         {
217             HYDROData_Profile::ProfilePoint aPnt = aPntList.Value( i );
218             aPoints.push_back( aPnt.X() );
219             aPoints.push_back( aPnt.Y() );
220             aPoints.push_back( aPnt.Z() );
221         }
222     }
223     return aPoints;
224 }
225
226 HYDROGUI_ProfileInterpolateOp::ParamsList HYDROGUI_ProfileInterpolateOp::parameters( const QString& theStr ) const
227 {
228     // Regular expression for parsing parameters string of kind: <param_name> <param_value> <param_name> <param_value> ..."
229     // QRegExp rx( "([A-Za-z_\\d]+)\\s+([A-Za-z_\\d]+)\\s*" );
230
231     // Regular expression for parsing parameters string of kind: <param_name> = <param_value> <param_name> = <param_value> ..."
232     QRegExp rx( "([A-Za-z_\\d]+)\\s*=\\s*([A-Za-z_\\d]+)\\s*" );
233
234     ParamsList aParamList;
235     for ( int i = 0; i >= 0; )
236     {
237         i = rx.indexIn( theStr, i );
238         if ( i >= 0  )
239         {
240             aParamList.append( QPair<QString, QString>( rx.cap( 1 ), rx.cap( 2 ) ) );
241             i += rx.matchedLength();
242         }
243     }
244
245     return aParamList;
246 }
247
248 QString HYDROGUI_ProfileInterpolateOp::parameters( const ParamsList& theParamList ) const
249 {
250     QStringList paramList;
251     for ( ParamsList::const_iterator it = theParamList.begin(); it != theParamList.end(); ++it )
252         paramList.append( QString( "%1 = %2" ).arg( (*it).first ).arg( (*it).second ) );
253     return paramList.join( " " );
254 }
255
256 QStringList HYDROGUI_ProfileInterpolateOp::interpolators() const
257 {
258     HYDROData_InterpolatorsFactory* anIFactory = 0;
259     Handle(HYDROData_Document) aDoc = doc();
260     if ( !aDoc.IsNull() )
261         anIFactory = aDoc->GetInterpolatorsFactory();
262
263     QStringList aNames;
264     if ( anIFactory )
265     {
266         std::vector<std::string> iNames = anIFactory->GetInterpolatorNames();
267         for ( std::vector<std::string>::iterator it = iNames.begin(); it != iNames.end(); ++it )
268             aNames.append( (*it).c_str() );
269     }
270
271     return aNames;
272 }
273
274 HYDROData_IProfilesInterpolator* HYDROGUI_ProfileInterpolateOp::interpolator( const QString& theName ) const
275 {
276     HYDROData_InterpolatorsFactory* anIFactory = 0;
277     Handle(HYDROData_Document) aDoc = doc();
278     if ( !aDoc.IsNull() )
279         anIFactory = aDoc->GetInterpolatorsFactory();
280
281     HYDROData_IProfilesInterpolator* aRes = 0;
282     if ( anIFactory )
283         aRes = anIFactory->GetInterpolator( theName.toStdString() );
284     return aRes;
285 }
286
287 TopoDS_Shape HYDROGUI_ProfileInterpolateOp::previewShape( HYDROData_IProfilesInterpolator* theInterp, int theNumber ) const
288 {
289     TopoDS_Compound aPreviewShape;
290     if ( theInterp )
291     {
292         BRep_Builder aBuilder;
293         aBuilder.MakeCompound( aPreviewShape );
294         for ( int i = 0; i < theNumber; i++ )
295         {
296             NCollection_Sequence<gp_XYZ> pointSeq;
297             std::vector<double> aPoints = theInterp->GetResultProfile( i );
298             for ( int i = 0; i < aPoints.size(); i += 3 )
299                 pointSeq.Append( gp_XYZ( aPoints[i], aPoints[i+1], aPoints[i+2] ) );
300             TopoDS_Shape aWire = HYDROData_PolylineXY::BuildWire( HYDROData_IPolyline::SECTION_SPLINE, false, pointSeq );
301             if ( !aWire.IsNull() )
302                 aBuilder.Add( aWire, aPreviewShape );
303         }
304     }
305
306     return aPreviewShape;
307 }
308
309 void HYDROGUI_ProfileInterpolateOp::onInterpolatorChanged( const QString& theInterpName )
310 {
311     HYDROGUI_ProfileInterpolateDlg* aPanel = dynamic_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
312     HYDROData_IProfilesInterpolator* anInterp = interpolator( theInterpName );
313     if ( !aPanel || !anInterp )
314         return;
315
316     aPanel->setInterpolatorDescription( QString( anInterp->GetDescription().c_str() ) );
317
318     updatePreview();
319 }
320
321 void HYDROGUI_ProfileInterpolateOp::onRiverChanged( const QString& theRiver )
322 {
323     HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
324     if ( !aDlg )
325         return;
326
327     Handle(AIS_InteractiveContext) aCtx = aDlg->getAISContext();
328     if ( !aCtx.IsNull() )
329     {
330         AIS_ListOfInteractive aList;
331         aCtx->DisplayedObjects( aList );
332         for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
333         {
334             Handle(AIS_Shape) anObj = Handle(AIS_Shape)::DownCast( it.Value() );
335             if ( !anObj.IsNull() )
336                 aCtx->Remove( anObj, false );
337         }
338
339         Handle(HYDROData_Stream) aStream = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theRiver, KIND_STREAM ) );
340         if ( !aStream.IsNull() )
341         {
342             HYDROData_SequenceOfObjects aSeq = aStream->GetProfiles();
343             for ( int i = aSeq.Lower(); i <= aSeq.Upper(); i++ )
344             {
345                 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( aSeq.Value( i ) );
346                 if ( !aProfile.IsNull() )
347                 {
348                     Handle(AIS_Shape) aPrs = new AIS_Shape( aProfile->GetShape3D() );
349                     aPrs->SetOwner( aProfile );
350                     aPrs->SetColor( Quantity_NOC_BLACK );
351                     aCtx->Display( aPrs, 0, 0, false );
352                 }
353             }
354         }
355     }
356     updatePreview();
357 }