Salome HOME
Merge branch 'BR_2017_PORTING' into BR_2018_V8_5
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ShapeBathymetry.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_ShapeBathymetry.h>
20 #include <HYDROGUI_OCCDisplayer.h>
21 #include <HYDROGUI_BathymetryPrs.h>
22 #include <HYDROData_Bathymetry.h>
23
24 #include <AIS_InteractiveContext.hxx>
25 #include <AIS_ColorScale.hxx>
26 #include <Prs3d_PointAspect.hxx>
27 #include <OCCViewer_ViewWindow.h>
28 #include <OCCViewer_ViewPort3d.h>
29 #include <V3d_View.hxx>
30 #include <QTime>
31 #include <utilities.h>
32
33 HYDROGUI_ShapeBathymetry::HYDROGUI_ShapeBathymetry( HYDROGUI_OCCDisplayer*                theDisplayer,
34                                                     const Handle(AIS_InteractiveContext)& theContext,
35                                                     const Handle(HYDROData_Bathymetry)&   theBathymetry,
36                                                     const int                             theZLayer )
37 : HYDROGUI_Shape( theContext, theBathymetry, theZLayer ),
38   myDisplayer( theDisplayer ),
39   myMin( 0 ),
40   myMax( 0 ),
41   myRangeInitialized( false )
42 {
43   setDisplayMode( AIS_PointCloud::DM_Points );
44 }
45
46 HYDROGUI_ShapeBathymetry::~HYDROGUI_ShapeBathymetry()
47 {
48   setToUpdateColorScale( true );
49 }
50
51 void HYDROGUI_ShapeBathymetry::update( bool theIsUpdateViewer, bool isDeactivateSelection )
52 {
53   setIsToUpdate( false );
54
55   // Try to retrieve information from object
56
57   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
58
59   if ( !aBath.IsNull() && !aBath->GetAltitudePoints().empty())
60   {
61     buildShape();
62     updateShape( false, false );
63   }
64
65   HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
66 }
67
68 QList<Handle(AIS_InteractiveObject)> HYDROGUI_ShapeBathymetry::createShape() const
69 {
70   QList<Handle(AIS_InteractiveObject)> shapes;
71
72   Handle(AIS_InteractiveObject) aPntCloud;
73
74   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
75   if( !aBath.IsNull() )
76   {
77     aPntCloud = new HYDROGUI_BathymetryPrs( this );
78     //aPntCloud->SetHilightMode( AIS_PointCloud::DM_BndBox );
79     aPntCloud->Attributes()->SetPointAspect (new Prs3d_PointAspect (Aspect_TOM_POINT, Quantity_NOC_WHITE, 2.0));
80
81     const HYDROData_Bathymetry::AltitudePoints& aBathPoints = aBath->GetAltitudePoints();
82     int aLower = 0;
83     int anUpper = (int)aBathPoints.size()-1;
84
85     HYDROGUI_ShapeBathymetry* aThat = const_cast<HYDROGUI_ShapeBathymetry*>( this );
86     aThat->myCoords = new TColgp_HArray1OfPnt( aLower, anUpper );
87     aThat->myColors = new Quantity_HArray1OfColor( aLower, anUpper );
88     for( int i=aLower; i<=anUpper; i++ )
89       aThat->myCoords->SetValue( i, gp_Pnt( aBathPoints[i].X, aBathPoints[i].Y, aBathPoints[i].Z ) );
90
91     shapes.append( aPntCloud );
92   }
93
94   return shapes;
95 }
96
97 void HYDROGUI_ShapeBathymetry::UpdateWithColorScale( const Handle(AIS_ColorScale)& theColorScale )
98 {
99   if (!myCoords || getAISObjects().isEmpty())
100     return;
101
102   for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
103   {
104     double z = myCoords->Value( i ).Z();
105     if( z<myMin )
106       z = myMin;
107     if( z>myMax )
108       z = myMax;
109     Quantity_Color aColor;
110     theColorScale->FindColor( z, aColor );
111     myColors->SetValue( i, aColor );
112   }
113   Handle(HYDROGUI_BathymetryPrs) aPntCloud = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObjects()[0] );
114   aPntCloud->SetPoints( myCoords, myColors );
115   getContext()->RecomputePrsOnly( aPntCloud, Standard_True );
116   getContext()->RecomputeSelectionOnly( aPntCloud );
117 }
118
119 void HYDROGUI_ShapeBathymetry::setVisible( const bool theState,
120                                            const bool theIsUpdateViewer )
121 {
122   if( getAISObjects().isEmpty() )
123     return;
124
125   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
126   bool isChanged = ( isShown != theState );
127   HYDROGUI_Shape::setVisible( theState, theIsUpdateViewer );
128   setToUpdateColorScale( isChanged );
129 }
130
131 void HYDROGUI_ShapeBathymetry::displayShape( const bool theIsUpdateViewer )
132 {
133   if( getAISObjects().isEmpty() )
134     return;
135
136   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
137   bool isChanged = ( !isShown  );
138   HYDROGUI_Shape::displayShape( theIsUpdateViewer );
139   setToUpdateColorScale( isChanged );
140 }
141
142 void HYDROGUI_ShapeBathymetry::display( const bool theIsUpdateViewer )
143 {
144   if( getAISObjects().isEmpty() )
145     return;
146
147   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
148   bool isChanged = ( !isShown  );
149   HYDROGUI_Shape::display( theIsUpdateViewer );
150   setToUpdateColorScale( isChanged );
151 }
152
153 void HYDROGUI_ShapeBathymetry::erase( const bool theIsUpdateViewer )
154 {
155   if( getAISObjects().isEmpty() )
156     return;
157
158   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
159   bool isChanged = ( isShown  );
160   HYDROGUI_Shape::erase( theIsUpdateViewer );
161   setToUpdateColorScale( isChanged );
162 }
163
164 void HYDROGUI_ShapeBathymetry::setToUpdateColorScale( bool isChanged )
165 {
166 #ifndef LIGHT_MODE
167   if( isChanged && myDisplayer )
168     myDisplayer->SetToUpdateColorScale();
169 #endif
170 }
171
172 void HYDROGUI_ShapeBathymetry::GetRange( double& theMin, double& theMax ) const
173 {
174   if( !myRangeInitialized )
175   {
176     HYDROGUI_ShapeBathymetry* that = 
177       const_cast<HYDROGUI_ShapeBathymetry*>( this );
178     that->RescaleDefault();
179     that->myRangeInitialized = true;
180   }
181
182   theMin = myMin;
183   theMax = myMax;
184 }
185
186 void HYDROGUI_ShapeBathymetry::RescaleByVisible( OCCViewer_ViewWindow* theWindow )
187 {
188   QVector<int> visible;
189   visible.reserve( myCoords->Size() );
190
191   OCCViewer_ViewPort3d* vp = theWindow->getViewPort();
192   Handle(V3d_View) v = vp->getView();
193
194   int xp, yp;
195   int w = vp->width();
196   int h = vp->height();
197   int n = myCoords->Upper();
198   bool isVisible;
199
200 #ifdef _DEBUG
201   MESSAGE("RescaleByVisible: " << n);
202   QTime t1;
203   t1.start();
204 #endif
205
206   for( int i=myCoords->Lower(); i<=n; i++ )
207   {
208     gp_Pnt p = myCoords->Value( i );
209     v->Convert( p.X(), p.Y(), p.Z(), xp, yp );
210     isVisible = ( xp>=0 && yp>=0 && xp<w && yp<h );
211     if( isVisible )
212       visible.append( i );
213   }
214
215 #ifdef _DEBUG
216   MESSAGE("Time after visibles search:" << t1.elapsed());
217 #endif
218
219   //TODO: question: empty visible part produce empty bathymetry or complete bathymetry?
220   // For now "complete" is implemented
221   Rescale( visible, visible.isEmpty() );
222
223 #ifdef _DEBUG
224   MESSAGE("Time after rescale:" << t1.elapsed());
225 #endif
226 }
227
228 QVector<int> HYDROGUI_ShapeBathymetry::selected() const
229 {
230   QVector<int> selected;
231   selected.reserve( myCoords->Size() );
232
233   Handle(AIS_InteractiveObject) obj = getAISObjects().first();
234
235   Handle(AIS_InteractiveContext) c = getContext();
236   if( !c.IsNull() )
237   {
238     Handle(AIS_LocalContext) lc = c->LocalContext();
239     if( !lc.IsNull() )
240     {
241       for( lc->InitSelected(); lc->MoreSelected(); lc->NextSelected() )
242       {
243         Handle(HYDROGUI_BathymetryPointOwner) anOwner = 
244           Handle(HYDROGUI_BathymetryPointOwner)::DownCast( lc->SelectedOwner() );
245         if( !anOwner.IsNull() && anOwner->Selectable()==obj )
246           selected.append( anOwner->GetIndex() );
247       }
248     }
249   }
250   return selected;
251 }
252
253 void HYDROGUI_ShapeBathymetry::RescaleBySelection()
254 {
255   QVector<int> selection = selected();
256
257   //TODO: question: empty selection produce empty bathymetry or complete bathymetry?
258   // For now "complete" is implemented
259   Rescale( selection, selection.isEmpty() );
260 }
261
262 void HYDROGUI_ShapeBathymetry::Rescale( double theMin, double theMax )
263 {
264   getContext()->ClearSelected(true);
265   myMin = qMin( theMin, theMax );
266   myMax = qMax( theMin, theMax );
267   setToUpdateColorScale( true );
268
269   if( !getAISObjects().isEmpty() )
270   {
271     getContext()->RecomputePrsOnly( getAISObjects()[0], true );
272     //getAISObjects()[0]->Redisplay();
273 }
274 }
275
276 void HYDROGUI_ShapeBathymetry::RescaleDefault()
277 {
278   Rescale( QVector<int>(), true );
279 }
280
281 void HYDROGUI_ShapeBathymetry::Rescale( const QVector<int>& theIndices, bool isForcedAll )
282 {
283   double aMin = 0, aMax = 0;
284   if( !myCoords.IsNull() )
285   {
286     bool isFirst = true;
287     int n = isForcedAll ? myCoords->Size() : theIndices.size();
288     for( int i=0; i<n; i++ )
289     {
290       int index = isForcedAll ? myCoords->Lower() + i : theIndices[i];
291
292       double aValue = myCoords->Value( index ).Z();
293       if( isFirst || aValue < aMin )
294         aMin = aValue;
295       if( isFirst || aValue > aMax )
296         aMax = aValue;
297       isFirst = false;
298     }
299   }
300   Rescale( aMin, aMax );
301 }
302
303 void HYDROGUI_ShapeBathymetry::Build()
304 {
305   buildShape();
306 }
307
308 void HYDROGUI_ShapeBathymetry::TextLabels( bool isOn, bool isUpdateCurrentViewer )
309 {
310   if( getAISObjects().isEmpty() )
311     return;
312
313   Handle(HYDROGUI_BathymetryPrs) prs = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObjects()[0] );
314   if( prs.IsNull() )
315     return;
316
317   QVector<int> selection;
318   if( isOn )
319     selection = selected();
320
321
322   //getContext()->ClearSelected(true);
323   prs->SetTextLabels( selection );
324   getContext()->RecomputePrsOnly( prs, Standard_False, Standard_False );
325   //prs->Redisplay();
326   if( isUpdateCurrentViewer )
327   getContext()->UpdateCurrentViewer();
328 }