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