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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROGUI_ShapeBathymetry.h>
20 #include <HYDROGUI_OCCDisplayer.h>
21 #include <HYDROGUI_BathymetryPrs.h>
22 #include <HYDROData_Bathymetry.h>
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>
31 #include <utilities.h>
33 HYDROGUI_ShapeBathymetry::HYDROGUI_ShapeBathymetry( HYDROGUI_OCCDisplayer* theDisplayer,
34 const Handle(AIS_InteractiveContext)& theContext,
35 const Handle(HYDROData_Bathymetry)& theBathymetry,
37 : HYDROGUI_Shape( theContext, theBathymetry, theZLayer ),
38 myDisplayer( theDisplayer ),
41 myRangeInitialized( false )
43 setDisplayMode( AIS_PointCloud::DM_Points );
46 HYDROGUI_ShapeBathymetry::~HYDROGUI_ShapeBathymetry()
48 setToUpdateColorScale( true );
51 void HYDROGUI_ShapeBathymetry::update( bool theIsUpdateViewer, bool isDeactivateSelection )
53 setIsToUpdate( false );
55 // Try to retrieve information from object
57 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
59 if ( !aBath.IsNull() && !aBath->GetAltitudePoints().empty())
62 updateShape( false, false );
65 HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
68 QList<Handle(AIS_InteractiveObject)> HYDROGUI_ShapeBathymetry::createShape() const
70 QList<Handle(AIS_InteractiveObject)> shapes;
72 Handle(AIS_InteractiveObject) aPntCloud;
74 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
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));
81 const HYDROData_Bathymetry::AltitudePoints& aBathPoints = aBath->GetAltitudePoints();
83 int anUpper = (int)aBathPoints.size()-1;
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 ) );
91 shapes.append( aPntCloud );
97 void HYDROGUI_ShapeBathymetry::UpdateWithColorScale( const Handle(AIS_ColorScale)& theColorScale )
99 if (!myCoords || getAISObjects().isEmpty())
102 for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
104 double z = myCoords->Value( i ).Z();
109 Quantity_Color aColor;
110 theColorScale->FindColor( z, aColor );
111 myColors->SetValue( i, aColor );
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 );
119 void HYDROGUI_ShapeBathymetry::setVisible( const bool theState,
120 const bool theIsUpdateViewer )
122 if( getAISObjects().isEmpty() )
125 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
126 bool isChanged = ( isShown != theState );
127 HYDROGUI_Shape::setVisible( theState, theIsUpdateViewer );
128 setToUpdateColorScale( isChanged );
131 void HYDROGUI_ShapeBathymetry::displayShape( const bool theIsUpdateViewer )
133 if( getAISObjects().isEmpty() )
136 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
137 bool isChanged = ( !isShown );
138 HYDROGUI_Shape::displayShape( theIsUpdateViewer );
139 setToUpdateColorScale( isChanged );
142 void HYDROGUI_ShapeBathymetry::display( const bool theIsUpdateViewer )
144 if( getAISObjects().isEmpty() )
147 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
148 bool isChanged = ( !isShown );
149 HYDROGUI_Shape::display( theIsUpdateViewer );
150 setToUpdateColorScale( isChanged );
153 void HYDROGUI_ShapeBathymetry::erase( const bool theIsUpdateViewer )
155 if( getAISObjects().isEmpty() )
158 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
159 bool isChanged = ( isShown );
160 HYDROGUI_Shape::erase( theIsUpdateViewer );
161 setToUpdateColorScale( isChanged );
164 void HYDROGUI_ShapeBathymetry::setToUpdateColorScale( bool isChanged )
167 if( isChanged && myDisplayer )
168 myDisplayer->SetToUpdateColorScale();
172 void HYDROGUI_ShapeBathymetry::GetRange( double& theMin, double& theMax ) const
174 if( !myRangeInitialized )
176 HYDROGUI_ShapeBathymetry* that =
177 const_cast<HYDROGUI_ShapeBathymetry*>( this );
178 that->RescaleDefault();
179 that->myRangeInitialized = true;
186 void HYDROGUI_ShapeBathymetry::RescaleByVisible( OCCViewer_ViewWindow* theWindow )
188 QVector<int> visible;
189 visible.reserve( myCoords->Size() );
191 OCCViewer_ViewPort3d* vp = theWindow->getViewPort();
192 Handle(V3d_View) v = vp->getView();
196 int h = vp->height();
197 int n = myCoords->Upper();
201 MESSAGE("RescaleByVisible: " << n);
206 for( int i=myCoords->Lower(); i<=n; i++ )
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 );
216 MESSAGE("Time after visibles search:" << t1.elapsed());
219 //TODO: question: empty visible part produce empty bathymetry or complete bathymetry?
220 // For now "complete" is implemented
221 Rescale( visible, visible.isEmpty() );
224 MESSAGE("Time after rescale:" << t1.elapsed());
228 QVector<int> HYDROGUI_ShapeBathymetry::selected() const
230 QVector<int> selected;
231 selected.reserve( myCoords->Size() );
233 Handle(AIS_InteractiveObject) obj = getAISObjects().first();
235 Handle(AIS_InteractiveContext) c = getContext();
238 Handle(AIS_LocalContext) lc = c->LocalContext();
241 for( lc->InitSelected(); lc->MoreSelected(); lc->NextSelected() )
243 Handle(HYDROGUI_BathymetryPointOwner) anOwner =
244 Handle(HYDROGUI_BathymetryPointOwner)::DownCast( lc->SelectedOwner() );
245 if( !anOwner.IsNull() && anOwner->Selectable()==obj )
246 selected.append( anOwner->GetIndex() );
253 void HYDROGUI_ShapeBathymetry::RescaleBySelection()
255 QVector<int> selection = selected();
257 //TODO: question: empty selection produce empty bathymetry or complete bathymetry?
258 // For now "complete" is implemented
259 Rescale( selection, selection.isEmpty() );
262 void HYDROGUI_ShapeBathymetry::Rescale( double theMin, double theMax )
264 getContext()->ClearSelected(true);
265 myMin = qMin( theMin, theMax );
266 myMax = qMax( theMin, theMax );
267 setToUpdateColorScale( true );
269 if( !getAISObjects().isEmpty() )
271 getContext()->RecomputePrsOnly( getAISObjects()[0], true );
272 //getAISObjects()[0]->Redisplay();
276 void HYDROGUI_ShapeBathymetry::RescaleDefault()
278 Rescale( QVector<int>(), true );
281 void HYDROGUI_ShapeBathymetry::Rescale( const QVector<int>& theIndices, bool isForcedAll )
283 double aMin = 0, aMax = 0;
284 if( !myCoords.IsNull() )
287 int n = isForcedAll ? myCoords->Size() : theIndices.size();
288 for( int i=0; i<n; i++ )
290 int index = isForcedAll ? myCoords->Lower() + i : theIndices[i];
292 double aValue = myCoords->Value( index ).Z();
293 if( isFirst || aValue < aMin )
295 if( isFirst || aValue > aMax )
300 Rescale( aMin, aMax );
303 void HYDROGUI_ShapeBathymetry::Build()
308 void HYDROGUI_ShapeBathymetry::TextLabels( bool isOn, bool isUpdateCurrentViewer )
310 if( getAISObjects().isEmpty() )
313 Handle(HYDROGUI_BathymetryPrs) prs = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObjects()[0] );
317 QVector<int> selection;
319 selection = selected();
322 //getContext()->ClearSelected(true);
323 prs->SetTextLabels( selection );
324 getContext()->RecomputePrsOnly( prs, Standard_False, Standard_False );
326 if( isUpdateCurrentViewer )
327 getContext()->UpdateCurrentViewer();