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 HYDROGUI_ShapeBathymetry::HYDROGUI_ShapeBathymetry( HYDROGUI_OCCDisplayer* theDisplayer,
32 const Handle(AIS_InteractiveContext)& theContext,
33 const Handle(HYDROData_Bathymetry)& theBathymetry,
35 : HYDROGUI_Shape( theContext, theBathymetry, theZLayer ),
36 myDisplayer( theDisplayer ),
39 myRangeInitialized( false )
41 setDisplayMode( AIS_PointCloud::DM_Points );
44 HYDROGUI_ShapeBathymetry::~HYDROGUI_ShapeBathymetry()
46 setToUpdateColorScale( true );
49 void HYDROGUI_ShapeBathymetry::update( bool theIsUpdateViewer, bool isDeactivateSelection )
51 setIsToUpdate( false );
53 // Try to retrieve information from object
55 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
57 if ( !aBath.IsNull() && !aBath->GetAltitudePoints().empty())
60 updateShape( false, false );
63 HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
66 QList<Handle(AIS_InteractiveObject)> HYDROGUI_ShapeBathymetry::createShape() const
68 QList<Handle(AIS_InteractiveObject)> shapes;
70 Handle(AIS_InteractiveObject) aPntCloud;
72 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
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));
79 const HYDROData_Bathymetry::AltitudePoints& aBathPoints = aBath->GetAltitudePoints();
81 int anUpper = (int)aBathPoints.size()-1;
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 ) );
89 shapes.append( aPntCloud );
95 void HYDROGUI_ShapeBathymetry::UpdateWithColorScale( const Handle(AIS_ColorScale)& theColorScale )
100 for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
102 double z = myCoords->Value( i ).Z();
107 Quantity_Color aColor;
108 theColorScale->FindColor( z, aColor );
109 myColors->SetValue( i, aColor );
111 Handle(HYDROGUI_BathymetryPrs) aPntCloud = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObject()[0] );
112 aPntCloud->SetPoints( myCoords, myColors );
113 getContext()->RecomputePrsOnly( aPntCloud, Standard_True );
114 getContext()->RecomputeSelectionOnly( aPntCloud );
117 void HYDROGUI_ShapeBathymetry::setVisible( const bool theState,
118 const bool theIsUpdateViewer )
120 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
121 bool isChanged = ( isShown != theState );
122 HYDROGUI_Shape::setVisible( theState, theIsUpdateViewer );
123 setToUpdateColorScale( isChanged );
126 void HYDROGUI_ShapeBathymetry::displayShape( const bool theIsUpdateViewer )
128 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
129 bool isChanged = ( !isShown );
130 HYDROGUI_Shape::displayShape( theIsUpdateViewer );
131 setToUpdateColorScale( isChanged );
134 void HYDROGUI_ShapeBathymetry::display( const bool theIsUpdateViewer )
136 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
137 bool isChanged = ( !isShown );
138 HYDROGUI_Shape::display( theIsUpdateViewer );
139 setToUpdateColorScale( isChanged );
142 void HYDROGUI_ShapeBathymetry::erase( const bool theIsUpdateViewer )
144 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
145 bool isChanged = ( isShown );
146 HYDROGUI_Shape::erase( theIsUpdateViewer );
147 setToUpdateColorScale( isChanged );
150 void HYDROGUI_ShapeBathymetry::setToUpdateColorScale( bool isChanged )
153 if( isChanged && myDisplayer )
154 myDisplayer->SetToUpdateColorScale();
158 void HYDROGUI_ShapeBathymetry::GetRange( double& theMin, double& theMax ) const
160 if( !myRangeInitialized )
162 HYDROGUI_ShapeBathymetry* that =
163 const_cast<HYDROGUI_ShapeBathymetry*>( this );
164 that->RescaleDefault();
165 that->myRangeInitialized = true;
172 void HYDROGUI_ShapeBathymetry::RescaleByVisible( OCCViewer_ViewWindow* theWindow )
175 visible.reserve( myCoords->Size() );
177 OCCViewer_ViewPort3d* vp = theWindow->getViewPort();
178 Handle(V3d_View) v = vp->getView();
182 int h = vp->height();
184 for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
186 gp_Pnt p = myCoords->Value( i );
187 v->Convert( p.X(), p.Y(), p.Z(), xp, yp );
188 isVisible = ( xp>=0 && yp>=0 && xp<w && yp<h );
193 //TODO: question: empty visible part produce empty bathymetry or complete bathymetry?
194 // For now "complete" is implemented
195 Rescale( visible, visible.isEmpty() );
198 QList<int> HYDROGUI_ShapeBathymetry::selected() const
201 selected.reserve( myCoords->Size() );
203 Handle(AIS_InteractiveContext) c = getContext();
206 Handle(AIS_LocalContext) lc = c->LocalContext();
209 for( lc->InitSelected(); lc->MoreSelected(); lc->NextSelected() )
211 Handle(HYDROGUI_BathymetryPointOwner) anOwner =
212 Handle(HYDROGUI_BathymetryPointOwner)::DownCast( lc->SelectedOwner() );
214 selected.append( anOwner->GetIndex() );
221 void HYDROGUI_ShapeBathymetry::RescaleBySelection()
223 QList<int> selection = selected();
225 //TODO: question: empty selection produce empty bathymetry or complete bathymetry?
226 // For now "complete" is implemented
227 Rescale( selection, selection.isEmpty() );
230 void HYDROGUI_ShapeBathymetry::Rescale( double theMin, double theMax )
232 getContext()->ClearSelected();
233 myMin = qMin( theMin, theMax );
234 myMax = qMax( theMin, theMax );
235 setToUpdateColorScale( true );
236 getAISObject()->Redisplay();
239 void HYDROGUI_ShapeBathymetry::RescaleDefault()
241 Rescale( QList<int>(), true );
244 void HYDROGUI_ShapeBathymetry::Rescale( const QList<int>& theIndices, bool isForcedAll )
246 double aMin = 0, aMax = 0;
247 if( !myCoords.IsNull() )
250 int n = isForcedAll ? myCoords->Size() : theIndices.size();
251 for( int i=0; i<n; i++ )
253 int index = isForcedAll ? myCoords->Lower() + i : theIndices[i];
255 double aValue = myCoords->Value( index ).Z();
256 if( isFirst || aValue < aMin )
258 if( isFirst || aValue > aMax )
263 Rescale( aMin, aMax );
266 void HYDROGUI_ShapeBathymetry::Build()
271 void HYDROGUI_ShapeBathymetry::TextLabels( bool isOn )
273 Handle(HYDROGUI_BathymetryPrs) prs = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObject() );
277 QList<int> selection;
279 selection = selected();
281 getContext()->ClearSelected();
282 prs->SetTextLabels( selection );
283 getAISObject()->Redisplay();