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