Salome HOME
limitation of the points number in preview for DTM (stream)
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StreamOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_StreamOp.h"
20
21 #include "HYDROGUI_Module.h"
22 #include <HYDROGUI_DataObject.h>
23 #include "HYDROGUI_Shape.h"
24 #include "HYDROGUI_StreamDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_Tool2.h"
27 #include "HYDROGUI_UpdateFlags.h"
28
29 #include <HYDROData_Document.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_Profile.h>
32 #include <HYDROData_DTM.h>
33
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <LightApp_UpdateFlags.h>
37
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
40 #include <QColor>
41
42 #include <OCCViewer_ViewManager.h>
43 #include <OCCViewer_ViewModel.h>
44 #include <OCCViewer_ViewWindow.h>
45 #include <gp_Ax1.hxx>
46 #include <gp_Ax2.hxx>
47 #include <gp_Ax3.hxx>
48 #include <gp_Vec.hxx>
49 #include <gp_Pnt.hxx>
50 #include <gp_Pln.hxx>
51 #include <gp.hxx>
52 #include <TopoDS_Face.hxx>
53 #include <TopoDS.hxx>
54 #include <BRepBuilderAPI_MakeFace.hxx>
55
56 void insertProfileInToOrder( const QString& theProfileName,
57                              const double&  theProfilePar,
58                              QStringList&   theProfiles,
59                              QList<double>& theProfileParams )
60 {
61   bool anIsInserted = false;
62   for ( int k = 0; k < theProfileParams.length(); ++k )
63   {
64     const double& aParam = theProfileParams.value( k );
65     if ( theProfilePar < aParam )
66     {
67       theProfiles.insert( k, theProfileName );
68       theProfileParams.insert( k, theProfilePar );
69       anIsInserted = true;
70       break;
71     }
72   }
73   
74   if ( !anIsInserted )
75   {
76     theProfiles << theProfileName;
77     theProfileParams << theProfilePar;
78   }
79 }
80
81 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
82 : HYDROGUI_Operation( theModule ), 
83   myIsEdit( theIsEdit ),
84   myPreviewPrs( NULL )
85 {
86   setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
87 }
88
89 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
90 {
91   erasePreview();
92 }
93
94 void HYDROGUI_StreamOp::startOperation()
95 {
96   HYDROGUI_Operation::startOperation();
97
98   if ( !myIsEdit || isApplyAndClose() )
99     myEditedObject.Nullify();
100   myHydAxis.clear();
101   myProfiles.clear();
102   myProfileParams.clear();
103
104   // Get panel and reset its state
105   HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
106   aPanel->reset();
107
108   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
109   if ( myIsEdit )
110   {
111     if ( isApplyAndClose() )
112       myEditedObject =
113         Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
114     if ( !myEditedObject.IsNull() )
115     {
116       anObjectName = myEditedObject->GetName();
117
118       // Hydraulic axis
119       Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
120       
121       TopoDS_Face aPlane;
122       HYDROData_Stream::BuildRefFace( aPlane ) ;
123       if ( !aHydraulicAxis.IsNull() )
124         myHydAxis = aHydraulicAxis->GetName();
125
126       // Stream profiles
127       HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
128       for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
129       {
130         Handle(HYDROData_Profile) aProfile = 
131           Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
132         if ( aProfile.IsNull() )
133           continue;
134
135         QString aProfileName = aProfile->GetName();        
136         myProfiles      << aProfileName;
137
138         if (!aHydraulicAxis.IsNull())
139         {
140           Standard_Real aProfilePar = 0.0;
141           HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
142           myProfileParams << aProfilePar;
143         }
144
145       }      
146
147     }
148   }
149
150   // Update the panel
151   // set the edited object name
152   aPanel->setObjectName( anObjectName );
153
154   // set the existing 2D polylines names to the panel
155   aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
156
157   if (myIsEdit)
158   {
159     aPanel->setDDZ( myEditedObject->GetDDZ() );
160     aPanel->setSpatialStep( myEditedObject->GetSpatialStep() );
161   }
162
163   // synchronize the panel state with the edited object state
164   updatePanelData();
165
166   // Create preview
167   createPreview();
168 }
169
170 void HYDROGUI_StreamOp::abortOperation()
171 {
172   erasePreview();
173   HYDROGUI_Operation::abortOperation();
174 }
175
176 void HYDROGUI_StreamOp::commitOperation()
177 {
178   erasePreview();
179   HYDROGUI_Operation::commitOperation();
180 }
181
182 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
183 {
184   HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
185
186   connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
187   connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ), 
188            this, SLOT( onRemoveProfiles( const QStringList& ) ) );
189   connect( aPanel, SIGNAL( AxisChanged( const QString& ) ), 
190            this, SLOT( onAxisChanged( const QString& ) ) );
191
192   connect( aPanel, SIGNAL( DDZValueChanged( double ) ),  this, SLOT( onDDZValueChanged( double ) ) );
193   connect( aPanel, SIGNAL( SSValueChanged( double ) ),  this, SLOT( onSSValueChanged( double ) ) );
194   return aPanel;
195 }
196
197 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
198                                       QString& theErrorMsg,
199                                       QStringList& theBrowseObjectsEntries )
200 {
201   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
202   if ( !aPanel )
203     return false;
204
205   // Check whether the object name is not empty
206   QString anObjectName = aPanel->getObjectName().simplified();
207   if ( anObjectName.isEmpty() )
208   {
209     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
210     return false;
211   }
212
213   // Check that there are no other objects with the same name in the document
214   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
215   {
216     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
217     if( !anObject.IsNull() )
218     {
219       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
220       return false;
221     }
222   }
223
224   if ( myEditedObject.IsNull() ) // Create new data model object
225     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
226
227   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
228     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
229  //if ( aHydAxis.IsNull() )
230  //{
231  //  theErrorMsg = tr( "AXIS_NOT_DEFINED" );
232  //  return false;
233  //}
234
235   // Check if at least 2 profiles is set
236   HYDROData_SequenceOfObjects aRefProfiles;
237   for ( int i = 0; i < myProfiles.length(); ++i )
238   {
239     QString aProfileName = myProfiles.value( i );
240
241     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
242       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
243     if ( !aProfile.IsNull() )
244       aRefProfiles.Append( aProfile );
245   }
246
247   if ( aRefProfiles.Length() < 2 )
248   {
249     theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
250     return false;
251   }
252
253   bool ToOrder = true;
254
255   if (aHydAxis.IsNull())
256     ToOrder = false;
257
258   myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
259   myEditedObject->SetName( anObjectName );
260
261   if (!aHydAxis.IsNull())
262     myEditedObject->SetHydraulicAxis( aHydAxis );
263   myEditedObject->SetProfiles( aRefProfiles, false );
264   myEditedObject->SetDDZ( aPanel->getDDZ() );
265   myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
266
267   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
268     myEditedObject->Update();
269
270   if ( !myIsEdit )
271   {
272     myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
273     myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
274   }
275
276   // Erase preview
277   erasePreview();
278
279   // Show the object in case of creation mode of the operation
280   if( !myIsEdit ) {
281     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
282     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
283     theBrowseObjectsEntries.append( anEntry );
284   }
285
286   module()->setIsToUpdate( myEditedObject );
287
288   // Set update flags
289   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
290
291   return true;
292 }
293
294 void HYDROGUI_StreamOp::createPreview()
295 {
296   LightApp_Application* anApp = module()->getApp();
297   if ( !getPreviewManager() )
298   {
299     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
300                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
301   }
302
303   OCCViewer_ViewManager* aViewManager = getPreviewManager();
304   if ( aViewManager && !myPreviewPrs )
305   {
306     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
307     {
308       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
309       if ( !aCtx.IsNull() )
310       {
311         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
312
313         QColor aFillingColor = Qt::green;
314         QColor aBorderColor = Qt::transparent;
315         if ( !myEditedObject.IsNull() )
316         {
317           aFillingColor = myEditedObject->GetFillingColor();
318           aBorderColor = myEditedObject->GetBorderColor();
319         }
320
321         myPreviewPrs->setFillingColor( aFillingColor, false, false );
322         myPreviewPrs->setBorderColor( aBorderColor, false, false );
323       }
324     }
325   }
326
327   if ( !aViewManager || !myPreviewPrs )
328     return;
329
330   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
331     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
332
333   HYDROData_SequenceOfObjects aRefProfiles;
334   //std::vector<Handle_HYDROData_Profile> aRefProfiles;
335   int plen = myProfiles.length();
336   for ( int i = 0; i < plen; ++i )
337   {
338     QString aProfileName = myProfiles.value( i );
339
340     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
341       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
342     if ( !aProfile.IsNull() )
343       aRefProfiles.Append( aProfile );
344   }
345
346   HYDROData_Stream::PrsDefinition aPrsDef;
347
348   TopoDS_Shape Out3dPres;
349   TopoDS_Shape Out2dPres;
350   TopoDS_Shape OutLeftB;
351   TopoDS_Shape OutRightB;
352   TopoDS_Shape OutInlet;
353   TopoDS_Shape OutOutlet;
354
355   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
356   double ddz = aPanel->getDDZ();
357   double ss = aPanel->getSpatialStep();
358
359   std::set<int> InvInd;
360
361 #ifdef _DEBUG
362   const int MAX_POINTS_IN_PREVIEW = 50000;
363 #else
364   const int MAX_POINTS_IN_PREVIEW = 500000;
365 #endif
366
367   HYDROData_Bathymetry::AltitudePoints points;
368   HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, points, Out3dPres, Out2dPres, OutLeftB, OutRightB,
369     OutInlet, OutOutlet, true, true, InvInd, MAX_POINTS_IN_PREVIEW );
370
371   aPanel->clearAllBackgroundColorsForProfileList();
372   for (std::set<int>::const_iterator it = InvInd.begin(); it != InvInd.end(); it++)
373     aPanel->setBackgroundColorForProfileList(*it, QColor(Qt::yellow));
374
375   aPrsDef.myInlet = TopoDS::Wire(OutInlet);
376   aPrsDef.myOutlet = TopoDS::Wire(OutOutlet);
377   aPrsDef.myLeftBank = TopoDS::Wire(OutLeftB);
378   aPrsDef.myRightBank = TopoDS::Wire(OutRightB);
379   aPrsDef.myPrs2D = Out2dPres;
380   aPrsDef.myPrs3D = Out3dPres;
381
382   myPreviewPrs->setShape( aPrsDef.myPrs2D );
383 }
384
385 void HYDROGUI_StreamOp::erasePreview()
386 {
387   if( myPreviewPrs )
388   {
389     delete myPreviewPrs;
390     myPreviewPrs = 0;
391   }
392 }
393
394 void HYDROGUI_StreamOp::onAddProfiles()
395 {
396   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
397     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
398   //if ( aHydAxis.IsNull() )
399    // return;
400
401   TopoDS_Face aPlane;
402   HYDROData_Stream::BuildRefFace( aPlane );
403
404   // Get the current profiles list
405   QStringList aCurrentProfiles = myProfiles;
406     
407   // Get the selected profiles ( in the Object Browser )
408   QStringList anInvalidProfiles;
409   QStringList anExistingProfiles;
410   QStringList aHasNoIntersectionProfiles;
411   QStringList aVerifiedProfiles;
412
413   HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() ); 
414
415   for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
416   {
417     Handle(HYDROData_Profile) aProfile = 
418       Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
419     if ( aProfile.IsNull() )
420       continue;
421
422     QString aProfileName = aProfile->GetName();
423     Standard_Real aProfilePar = 0.0;
424
425     // Check the profile, if all is ok - add it to the list
426     if ( !aProfile->IsValid() )
427     {
428       // check whether the profile is valid
429       anInvalidProfiles << aProfileName;
430     }
431     else if ( aCurrentProfiles.contains( aProfileName ) )
432     {
433       // check whether the profile is already added
434       anExistingProfiles << aProfileName;
435     }
436     else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
437     {
438       // check whether the profile has intersection
439       aHasNoIntersectionProfiles << aProfileName;
440     }
441     else
442     {
443       // Insert profile in correct place
444       // if hidr axis is null => the params (myProfileParams) will be igrored. So ordering will be the same as in the aSelectedProfiles
445       insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
446       aVerifiedProfiles << aProfileName;
447     }
448   }
449  
450   // Show message box with the ignored profiles
451   if ( !anInvalidProfiles.isEmpty() ||
452        !anExistingProfiles.isEmpty() ||
453        !aHasNoIntersectionProfiles.isEmpty() )
454   {
455     QString aMessage = tr( "IGNORED_PROFILES" );
456     if ( !anInvalidProfiles.isEmpty() )
457     {
458       aMessage.append( "\n\n" );
459       aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
460     }
461     if ( !anExistingProfiles.isEmpty() )
462     {
463       aMessage.append( "\n\n" );
464       aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
465     }
466     if ( !aHasNoIntersectionProfiles.isEmpty() )
467     {
468       aMessage.append( "\n\n" );
469       aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
470     }
471
472     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
473   }
474
475   if ( aVerifiedProfiles.isEmpty() )
476     return;
477
478   // Update the panel
479   updatePanelData();
480
481   // Update preview
482   createPreview();
483 }
484
485 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
486 {
487   QStringList aToRemove = theProfilesToRemove;
488   
489   aToRemove.removeDuplicates();
490   if ( aToRemove.isEmpty() )
491     return;
492
493   bool isRemoved = false;
494
495   // Remove profiles
496   for ( int i = 0; i < aToRemove.length(); ++i )
497   {
498     const QString& aProfileName = aToRemove.value( i );
499
500     int aProfileId = myProfiles.indexOf( aProfileName );
501     if ( aProfileId < 0 )
502       continue;
503
504     myProfiles.removeAt( aProfileId );
505     myProfileParams.removeAt( aProfileId );
506     isRemoved = true;
507   }
508
509   if ( isRemoved )
510   {
511     // Update the panel
512     updatePanelData();
513
514     // Update preview
515     createPreview();
516   }
517 }
518
519 void HYDROGUI_StreamOp::onDDZValueChanged( double d )
520 {
521    createPreview();
522 }
523
524 void HYDROGUI_StreamOp::onSSValueChanged( double d )
525 {
526    createPreview();
527 }
528
529 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
530 {
531   // Get axis object   
532   Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
533     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
534
535   // Prepare data for intersection check
536   TopoDS_Face aPlane;
537   HYDROData_Stream::BuildRefFace( aPlane );
538   //{
539   //  SUIT_MessageBox::critical( module()->getApp()->desktop(), 
540   //                            tr( "BAD_SELECTED_POLYLINE_TLT" ),
541   //                            tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
542   //  // To restore the old axis
543   //  updatePanelData();
544   //  return;
545   //}
546
547   QStringList   aNewProfiles;
548   QList<double> aNewProfileParams;
549   QStringList   aHasNoIntersectionProfiles;
550
551   // Get list of profiles which do not intersect the axis
552   for ( int i = 0; i < myProfiles.length(); ++i )
553   {
554     QString aProfileName = myProfiles.value( i );
555
556     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
557       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
558     if ( aProfile.IsNull() )
559       continue;
560       
561     Standard_Real aProfilePar = 0.0;
562     if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
563     {
564       // Insert profile in correct place
565       insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
566     }
567     else
568     {
569       aHasNoIntersectionProfiles << aProfile->GetName();
570     }
571   }
572
573   // If there are profiles which don't intersect the new axis - show confirmation message box
574   bool isConfirmed = true;
575   if ( !aHasNoIntersectionProfiles.isEmpty() )
576   {
577     SUIT_MessageBox::StandardButtons aButtons = 
578       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
579
580     QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
581     isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
582                                              tr( "CONFIRMATION" ),
583                                              tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
584                                              aButtons, 
585                                              SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
586   }
587
588   // Check if the user has confirmed axis change
589   if ( !isConfirmed )
590   {
591     // To restore the old axis
592     updatePanelData();
593   }
594   else
595   {
596     // Update data
597     myHydAxis = theNewAxis;
598     myProfiles = aNewProfiles;
599     myProfileParams = aNewProfileParams;
600
601     // Update the panel
602     updatePanelData();
603
604     // Update preview
605     createPreview();
606   }
607 }
608
609 void HYDROGUI_StreamOp::updatePanelData()
610 {
611   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
612   if ( !aPanel )
613     return;
614
615   aPanel->setAxisName( myHydAxis );
616   aPanel->setProfiles( myProfiles );
617 }