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