Salome HOME
refs #1459: re-design of the Z-values mode for bathymetry
[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   MESSAGE("Time after rescale:" << t1.elapsed());
224 }
225
226 QVector<int> HYDROGUI_ShapeBathymetry::selected() const
227 {
228   QVector<int> selected;
229   selected.reserve( myCoords->Size() );
230
231   Handle(AIS_InteractiveContext) c = getContext();
232   if( !c.IsNull() )
233   {
234     Handle(AIS_LocalContext) lc = c->LocalContext();
235     if( !lc.IsNull() )
236     {
237       for( lc->InitSelected(); lc->MoreSelected(); lc->NextSelected() )
238       {
239         Handle(HYDROGUI_BathymetryPointOwner) anOwner = 
240           Handle(HYDROGUI_BathymetryPointOwner)::DownCast( lc->SelectedOwner() );
241         if( anOwner )
242           selected.append( anOwner->GetIndex() );
243       }
244     }
245   }
246   return selected;
247 }
248
249 void HYDROGUI_ShapeBathymetry::RescaleBySelection()
250 {
251   QVector<int> selection = selected();
252
253   //TODO: question: empty selection produce empty bathymetry or complete bathymetry?
254   // For now "complete" is implemented
255   Rescale( selection, selection.isEmpty() );
256 }
257
258 void HYDROGUI_ShapeBathymetry::Rescale( double theMin, double theMax )
259 {
260   getContext()->ClearSelected();
261   myMin = qMin( theMin, theMax );
262   myMax = qMax( theMin, theMax );
263   setToUpdateColorScale( true );
264
265   if( !getAISObjects().isEmpty() )
266   {
267     getContext()->RecomputePrsOnly( getAISObjects()[0] );
268     //getAISObjects()[0]->Redisplay();
269   }
270 }
271
272 void HYDROGUI_ShapeBathymetry::RescaleDefault()
273 {
274   Rescale( QVector<int>(), true );
275 }
276
277 void HYDROGUI_ShapeBathymetry::Rescale( const QVector<int>& theIndices, bool isForcedAll )
278 {
279   double aMin = 0, aMax = 0;
280   if( !myCoords.IsNull() )
281   {
282     bool isFirst = true;
283     int n = isForcedAll ? myCoords->Size() : theIndices.size();
284     for( int i=0; i<n; i++ )
285     {
286       int index = isForcedAll ? myCoords->Lower() + i : theIndices[i];
287
288       double aValue = myCoords->Value( index ).Z();
289       if( isFirst || aValue < aMin )
290         aMin = aValue;
291       if( isFirst || aValue > aMax )
292         aMax = aValue;
293       isFirst = false;
294     }
295   }
296   Rescale( aMin, aMax );
297 }
298
299 void HYDROGUI_ShapeBathymetry::Build()
300 {
301   buildShape();
302 }
303
304 void HYDROGUI_ShapeBathymetry::TextLabels( bool isOn )
305 {
306   if( getAISObjects().isEmpty() )
307     return;
308
309   Handle(HYDROGUI_BathymetryPrs) prs = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObjects()[0] );
310   if( prs.IsNull() )
311     return;
312
313   QVector<int> selection;
314   if( isOn )
315     selection = selected();
316
317   //getContext()->ClearSelected();
318   prs->SetTextLabels( selection );
319   getContext()->RecomputePrsOnly( prs );
320   //prs->Redisplay();
321   getContext()->UpdateCurrentViewer();
322 }