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>
23 #include <HYDROGUI_Tool.h>
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>
32 #include <utilities.h>
35 #include "HYDRO_trace.hxx"
37 HYDROGUI_ShapeBathymetry::HYDROGUI_ShapeBathymetry( HYDROGUI_OCCDisplayer* theDisplayer,
38 const Handle(AIS_InteractiveContext)& theContext,
39 const Handle(HYDROData_Bathymetry)& theBathymetry,
41 : HYDROGUI_Shape( theContext, theBathymetry, theZLayer ),
42 myDisplayer( theDisplayer ),
45 myRangeInitialized( false )
47 DEBTRACE("HYDROGUI_ShapeBathymetry " << this)
48 setDisplayMode( AIS_PointCloud::DM_Points );
51 HYDROGUI_ShapeBathymetry::~HYDROGUI_ShapeBathymetry()
53 DEBTRACE("~HYDROGUI_ShapeBathymetry " << this);
54 setToUpdateColorScale( true );
57 void HYDROGUI_ShapeBathymetry::update( bool theIsUpdateViewer, bool isDeactivateSelection )
59 setIsToUpdate( false );
61 // Try to retrieve information from object
63 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
65 if ( !aBath.IsNull() && !aBath->GetAltitudePoints().empty())
68 updateShape( false, false );
71 HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
74 QList<Handle(AIS_InteractiveObject)> HYDROGUI_ShapeBathymetry::createShape() const
76 DEBTRACE("createShape");
77 QList<Handle(AIS_InteractiveObject)> shapes;
79 Handle(AIS_InteractiveObject) aPntCloud;
81 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
84 aPntCloud = new HYDROGUI_BathymetryPrs( this );
85 //aPntCloud->SetHilightMode( AIS_PointCloud::DM_BndBox );
86 aPntCloud->Attributes()->SetPointAspect (new Prs3d_PointAspect (Aspect_TOM_POINT, Quantity_NOC_WHITE, 2.0));
88 const HYDROData_Bathymetry::AltitudePoints& aBathPoints = aBath->GetAltitudePoints();
90 int anUpper = (int)aBathPoints.size()-1;
92 HYDROGUI_ShapeBathymetry* aThat = const_cast<HYDROGUI_ShapeBathymetry*>( this );
93 aThat->myCoords = new TColgp_HArray1OfPnt( aLower, anUpper );
94 aThat->myColors = new Quantity_HArray1OfColor( aLower, anUpper );
95 for( int i=aLower; i<=anUpper; i++ )
96 aThat->myCoords->SetValue( i, gp_Pnt( aBathPoints[i].X, aBathPoints[i].Y, aBathPoints[i].Z ) );
98 shapes.append( aPntCloud );
104 void HYDROGUI_ShapeBathymetry::UpdateWithColorScale( const Handle(AIS_ColorScale)& theColorScale )
106 DEBTRACE("UpdateWithColorScale");
107 if (!myCoords || getAISObjects().isEmpty())
110 for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
112 double z = myCoords->Value( i ).Z();
117 Quantity_Color aColor;
118 theColorScale->FindColor( z, aColor );
119 myColors->SetValue( i, aColor );
121 Handle(HYDROGUI_BathymetryPrs) aPntCloud = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObjects()[0] );
122 aPntCloud->SetPoints( myCoords, myColors );
123 getContext()->RecomputePrsOnly( aPntCloud, Standard_True );
124 getContext()->RecomputeSelectionOnly( aPntCloud );
127 void HYDROGUI_ShapeBathymetry::setVisible( const bool theState,
128 const bool theIsUpdateViewer )
130 if( getAISObjects().isEmpty() )
133 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
134 bool isChanged = ( isShown != theState );
135 HYDROGUI_Shape::setVisible( theState, theIsUpdateViewer );
136 setToUpdateColorScale( isChanged );
139 void HYDROGUI_ShapeBathymetry::displayShape( const bool theIsUpdateViewer )
141 if( getAISObjects().isEmpty() )
144 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
145 bool isChanged = ( !isShown );
146 HYDROGUI_Shape::displayShape( theIsUpdateViewer );
147 setToUpdateColorScale( isChanged );
150 void HYDROGUI_ShapeBathymetry::display( const bool theIsUpdateViewer )
152 if( getAISObjects().isEmpty() )
155 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
156 bool isChanged = ( !isShown );
157 HYDROGUI_Shape::display( theIsUpdateViewer );
158 setToUpdateColorScale( isChanged );
161 void HYDROGUI_ShapeBathymetry::erase( const bool theIsUpdateViewer )
163 if( getAISObjects().isEmpty() )
166 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
167 bool isChanged = ( isShown );
168 HYDROGUI_Shape::erase( theIsUpdateViewer );
169 setToUpdateColorScale( isChanged );
172 void HYDROGUI_ShapeBathymetry::setToUpdateColorScale( bool isChanged )
175 if( isChanged && myDisplayer )
176 myDisplayer->SetToUpdateColorScale();
180 void HYDROGUI_ShapeBathymetry::GetRange( double& theMin, double& theMax ) const
182 if( !myRangeInitialized )
184 HYDROGUI_ShapeBathymetry* that =
185 const_cast<HYDROGUI_ShapeBathymetry*>( this );
186 that->RescaleDefault();
187 that->myRangeInitialized = true;
194 void HYDROGUI_ShapeBathymetry::RescaleByVisible( OCCViewer_ViewWindow* theWindow )
196 QVector<int> visible;
197 visible.reserve( myCoords->Size() );
199 OCCViewer_ViewPort3d* vp = theWindow->getViewPort();
200 Handle(V3d_View) v = vp->getView();
204 int h = vp->height();
205 int n = myCoords->Upper();
209 MESSAGE("RescaleByVisible: " << n);
214 for( int i=myCoords->Lower(); i<=n; i++ )
216 gp_Pnt p = myCoords->Value( i );
217 v->Convert( p.X(), p.Y(), p.Z(), xp, yp );
218 isVisible = ( xp>=0 && yp>=0 && xp<w && yp<h );
224 MESSAGE("Time after visibles search:" << t1.elapsed());
227 //TODO: question: empty visible part produce empty bathymetry or complete bathymetry?
228 // For now "complete" is implemented
229 Rescale( visible, visible.isEmpty() );
232 MESSAGE("Time after rescale:" << t1.elapsed());
236 QVector<int> HYDROGUI_ShapeBathymetry::selected() const
238 DEBTRACE("selected");
239 QVector<int> selected;
241 // HYDROGUI_BathymetryPrs::ClearSelected() called before: Nothing left...
243 Handle(HYDROGUI_BathymetryPrs) aPntCloud = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObjects()[0] );
244 if (!aPntCloud.IsNull())
245 selected = aPntCloud->getSelectedPoints();
246 DEBTRACE("selected " << selected.size());
250 void HYDROGUI_ShapeBathymetry::RescaleBySelection()
252 QVector<int> selection = selected();
254 //TODO: question: empty selection produce empty bathymetry or complete bathymetry?
255 // For now "complete" is implemented
256 Rescale( selection, selection.isEmpty() );
259 void HYDROGUI_ShapeBathymetry::Rescale( double theMin, double theMax )
261 getContext()->ClearSelected(true);
262 myMin = qMin( theMin, theMax );
263 myMax = qMax( theMin, theMax );
264 setToUpdateColorScale( true );
266 if( !getAISObjects().isEmpty() )
268 getContext()->RecomputePrsOnly( getAISObjects()[0], true );
272 void HYDROGUI_ShapeBathymetry::RescaleDefault()
274 Rescale( QVector<int>(), true );
277 void HYDROGUI_ShapeBathymetry::Rescale( const QVector<int>& theIndices, bool isForcedAll )
279 double aMin = 0, aMax = 0;
280 if( !myCoords.IsNull() )
283 int n = isForcedAll ? myCoords->Size() : theIndices.size();
284 for( int i=0; i<n; i++ )
286 int index = isForcedAll ? myCoords->Lower() + i : theIndices[i];
288 double aValue = myCoords->Value( index ).Z();
289 if( isFirst || aValue < aMin )
291 if( isFirst || aValue > aMax )
296 Rescale( aMin, aMax );
299 void HYDROGUI_ShapeBathymetry::Build()
304 void HYDROGUI_ShapeBathymetry::TextLabels( bool isOn, bool isUpdateCurrentViewer )
306 DEBTRACE("TextLabels " << isOn << " " << isUpdateCurrentViewer);
307 if( getAISObjects().isEmpty() )
310 Handle(HYDROGUI_BathymetryPrs) prs = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObjects()[0] );
314 QVector<int> selection;
316 selection = selected();
319 prs->SetTextLabels( selection );
320 getContext()->RecomputePrsOnly( prs, Standard_False, Standard_False );
321 if( isUpdateCurrentViewer )
322 getContext()->UpdateCurrentViewer();