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 );
96 #include <SelectMgr_Selection.hxx>
97 #include <SelectMgr_ViewerSelector.hxx>
98 #include <SelectMgr_SensitiveEntitySet.hxx>
99 #include <BVH_PrimitiveSet.hxx>
100 #include <BVH_QueueBuilder.hxx>
102 class BVHB_dummy: public BVH_QueueBuilder<double,3>
107 int* getNbThreads() {return &this->myNumOfThreads;}
109 void HYDROGUI_ShapeBathymetry::UpdateWithColorScale( const Handle(AIS_ColorScale)& theColorScale )
111 if (!myCoords || getAISObjects().isEmpty())
114 for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
116 double z = myCoords->Value( i ).Z();
121 Quantity_Color aColor;
122 theColorScale->FindColor( z, aColor );
123 myColors->SetValue( i, aColor );
125 Handle(HYDROGUI_BathymetryPrs) aPntCloud = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObjects()[0] );
126 //const Handle(SelectMgr_Selection)& sel0 = aPntCloud->Selection(0);
127 //const Handle(SelectMgr_SensitiveEntity)& sens = sel0->Sensitive();
128 aPntCloud->SetPoints( myCoords, myColors );
129 getContext()->RecomputePrsOnly( aPntCloud, Standard_True );
130 Handle(StdSelect_ViewerSelector3d) VS = getContext()->MainSelector();
131 Handle(SelectMgr_ViewerSelector) VSD = Handle(SelectMgr_ViewerSelector)::DownCast(VS);
132 //size_t VSDsize = sizeof(VSD);
133 size_t VSDsize1 = sizeof(SelectMgr_ViewerSelector);
134 size_t mapSensSize = sizeof(SelectMgr_MapOfObjectSensitives);
135 SelectMgr_MapOfObjectSensitives* mapS = (SelectMgr_MapOfObjectSensitives*)((char*)VSD.get() + (VSDsize1 - mapSensSize));
139 NCollection_Handle<SelectMgr_SensitiveEntitySet>& anEntitySet = mapS->ChangeFind (aPntCloud);
140 NCollection_Handle<BVH_Builder<double,3>> BVH_Builder = anEntitySet->Builder();
141 BVH_QueueBuilder<double,3>* BVH_QB = dynamic_cast<BVH_QueueBuilder<double,3>*>(BVH_Builder.get());
142 int* nbt = static_cast<BVHB_dummy*>(BVH_QB)->getNbThreads();
151 //getContext()->LocalSelector();
152 //aPntCloud->ComputeSelection(sel0, 0);
153 getContext()->RecomputeSelectionOnly( aPntCloud );
156 void HYDROGUI_ShapeBathymetry::setVisible( const bool theState,
157 const bool theIsUpdateViewer )
159 if( getAISObjects().isEmpty() )
162 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
163 bool isChanged = ( isShown != theState );
164 HYDROGUI_Shape::setVisible( theState, theIsUpdateViewer );
165 setToUpdateColorScale( isChanged );
168 void HYDROGUI_ShapeBathymetry::displayShape( const bool theIsUpdateViewer )
170 if( getAISObjects().isEmpty() )
173 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
174 bool isChanged = ( !isShown );
175 HYDROGUI_Shape::displayShape( theIsUpdateViewer );
176 setToUpdateColorScale( isChanged );
179 void HYDROGUI_ShapeBathymetry::display( const bool theIsUpdateViewer )
181 if( getAISObjects().isEmpty() )
184 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
185 bool isChanged = ( !isShown );
186 HYDROGUI_Shape::display( theIsUpdateViewer );
187 setToUpdateColorScale( isChanged );
190 void HYDROGUI_ShapeBathymetry::erase( const bool theIsUpdateViewer )
192 if( getAISObjects().isEmpty() )
195 bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
196 bool isChanged = ( isShown );
197 HYDROGUI_Shape::erase( theIsUpdateViewer );
198 setToUpdateColorScale( isChanged );
201 void HYDROGUI_ShapeBathymetry::setToUpdateColorScale( bool isChanged )
204 if( isChanged && myDisplayer )
205 myDisplayer->SetToUpdateColorScale();
209 void HYDROGUI_ShapeBathymetry::GetRange( double& theMin, double& theMax ) const
211 if( !myRangeInitialized )
213 HYDROGUI_ShapeBathymetry* that =
214 const_cast<HYDROGUI_ShapeBathymetry*>( this );
215 that->RescaleDefault();
216 that->myRangeInitialized = true;
223 void HYDROGUI_ShapeBathymetry::RescaleByVisible( OCCViewer_ViewWindow* theWindow )
225 QVector<int> visible;
226 visible.reserve( myCoords->Size() );
228 OCCViewer_ViewPort3d* vp = theWindow->getViewPort();
229 Handle(V3d_View) v = vp->getView();
233 int h = vp->height();
234 int n = myCoords->Upper();
238 MESSAGE("RescaleByVisible: " << n);
243 for( int i=myCoords->Lower(); i<=n; i++ )
245 gp_Pnt p = myCoords->Value( i );
246 v->Convert( p.X(), p.Y(), p.Z(), xp, yp );
247 isVisible = ( xp>=0 && yp>=0 && xp<w && yp<h );
253 MESSAGE("Time after visibles search:" << t1.elapsed());
256 //TODO: question: empty visible part produce empty bathymetry or complete bathymetry?
257 // For now "complete" is implemented
258 Rescale( visible, visible.isEmpty() );
260 MESSAGE("Time after rescale:" << t1.elapsed());
263 QVector<int> HYDROGUI_ShapeBathymetry::selected() const
265 QVector<int> selected;
266 selected.reserve( myCoords->Size() );
268 Handle(AIS_InteractiveContext) c = getContext();
271 Handle(AIS_LocalContext) lc = c->LocalContext();
274 for( lc->InitSelected(); lc->MoreSelected(); lc->NextSelected() )
276 Handle(HYDROGUI_BathymetryPointOwner) anOwner =
277 Handle(HYDROGUI_BathymetryPointOwner)::DownCast( lc->SelectedOwner() );
279 selected.append( anOwner->GetIndex() );
286 void HYDROGUI_ShapeBathymetry::RescaleBySelection()
288 QVector<int> selection = selected();
290 //TODO: question: empty selection produce empty bathymetry or complete bathymetry?
291 // For now "complete" is implemented
292 Rescale( selection, selection.isEmpty() );
295 void HYDROGUI_ShapeBathymetry::Rescale( double theMin, double theMax )
297 getContext()->ClearSelected();
298 myMin = qMin( theMin, theMax );
299 myMax = qMax( theMin, theMax );
300 setToUpdateColorScale( true );
302 if( !getAISObjects().isEmpty() )
304 getContext()->RecomputePrsOnly( getAISObjects()[0] );
305 //getAISObjects()[0]->Redisplay();
309 void HYDROGUI_ShapeBathymetry::RescaleDefault()
311 Rescale( QVector<int>(), true );
314 void HYDROGUI_ShapeBathymetry::Rescale( const QVector<int>& theIndices, bool isForcedAll )
316 double aMin = 0, aMax = 0;
317 if( !myCoords.IsNull() )
320 int n = isForcedAll ? myCoords->Size() : theIndices.size();
321 for( int i=0; i<n; i++ )
323 int index = isForcedAll ? myCoords->Lower() + i : theIndices[i];
325 double aValue = myCoords->Value( index ).Z();
326 if( isFirst || aValue < aMin )
328 if( isFirst || aValue > aMax )
333 Rescale( aMin, aMax );
336 void HYDROGUI_ShapeBathymetry::Build()
341 void HYDROGUI_ShapeBathymetry::TextLabels( bool isOn )
343 if( getAISObjects().isEmpty() )
346 Handle(HYDROGUI_BathymetryPrs) prs = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObjects()[0] );
350 QVector<int> selection;
352 selection = selected();
354 getContext()->ClearSelected();
355 prs->SetTextLabels( selection );
356 getContext()->RecomputePrsOnly( prs );
358 getContext()->UpdateCurrentViewer();