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