Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_2017' into HEAD
[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 #include <QTime>
31 #include <utilities.h>
32
33 HYDROGUI_ShapeBathymetry::HYDROGUI_ShapeBathymetry( HYDROGUI_OCCDisplayer*                theDisplayer,
34                                                     const Handle(AIS_InteractiveContext)& theContext,
35                                                     const Handle(HYDROData_Bathymetry)&   theBathymetry,
36                                                     const int                             theZLayer )
37 : HYDROGUI_Shape( theContext, theBathymetry, theZLayer ),
38   myDisplayer( theDisplayer ),
39   myMin( 0 ),
40   myMax( 0 ),
41   myRangeInitialized( false )
42 {
43   setDisplayMode( AIS_PointCloud::DM_Points );
44 }
45
46 HYDROGUI_ShapeBathymetry::~HYDROGUI_ShapeBathymetry()
47 {
48   setToUpdateColorScale( true );
49 }
50
51 void HYDROGUI_ShapeBathymetry::update( bool theIsUpdateViewer, bool isDeactivateSelection )
52 {
53   setIsToUpdate( false );
54
55   // Try to retrieve information from object
56
57   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
58
59   if ( !aBath.IsNull() && !aBath->GetAltitudePoints().empty())
60   {
61     buildShape();
62     updateShape( false, false );
63   }
64
65   HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
66 }
67
68 QList<Handle(AIS_InteractiveObject)> HYDROGUI_ShapeBathymetry::createShape() const
69 {
70   QList<Handle(AIS_InteractiveObject)> shapes;
71
72   Handle(AIS_InteractiveObject) aPntCloud;
73
74   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
75   if( !aBath.IsNull() )
76   {
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));
80
81     const HYDROData_Bathymetry::AltitudePoints& aBathPoints = aBath->GetAltitudePoints();
82     int aLower = 0;
83     int anUpper = (int)aBathPoints.size()-1;
84
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 ) );
90
91     shapes.append( aPntCloud );
92   }
93
94   return shapes;
95 }
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>
101
102 class BVHB_dummy: public BVH_QueueBuilder<double,3>
103 {
104   BVHB_dummy();
105   ~BVHB_dummy();
106 public:
107   int* getNbThreads() {return &this->myNumOfThreads;}
108 };
109 void HYDROGUI_ShapeBathymetry::UpdateWithColorScale( const Handle(AIS_ColorScale)& theColorScale )
110 {
111   if (!myCoords || getAISObjects().isEmpty())
112     return;
113
114   for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
115   {
116     double z = myCoords->Value( i ).Z();
117     if( z<myMin )
118       z = myMin;
119     if( z>myMax )
120       z = myMax;
121     Quantity_Color aColor;
122     theColorScale->FindColor( z, aColor );
123     myColors->SetValue( i, aColor );
124   }
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));
136   try 
137   {
138     mapS->Size();
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();
143     if (*nbt == 1)
144       *nbt = 4;
145   }
146   catch (...)
147   {
148
149   }
150
151   //getContext()->LocalSelector();
152   //aPntCloud->ComputeSelection(sel0, 0);
153   getContext()->RecomputeSelectionOnly( aPntCloud );
154 }
155
156 void HYDROGUI_ShapeBathymetry::setVisible( const bool theState,
157                                            const bool theIsUpdateViewer )
158 {
159   if( getAISObjects().isEmpty() )
160     return;
161
162   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
163   bool isChanged = ( isShown != theState );
164   HYDROGUI_Shape::setVisible( theState, theIsUpdateViewer );
165   setToUpdateColorScale( isChanged );
166 }
167
168 void HYDROGUI_ShapeBathymetry::displayShape( const bool theIsUpdateViewer )
169 {
170   if( getAISObjects().isEmpty() )
171     return;
172
173   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
174   bool isChanged = ( !isShown  );
175   HYDROGUI_Shape::displayShape( theIsUpdateViewer );
176   setToUpdateColorScale( isChanged );
177 }
178
179 void HYDROGUI_ShapeBathymetry::display( const bool theIsUpdateViewer )
180 {
181   if( getAISObjects().isEmpty() )
182     return;
183
184   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
185   bool isChanged = ( !isShown  );
186   HYDROGUI_Shape::display( theIsUpdateViewer );
187   setToUpdateColorScale( isChanged );
188 }
189
190 void HYDROGUI_ShapeBathymetry::erase( const bool theIsUpdateViewer )
191 {
192   if( getAISObjects().isEmpty() )
193     return;
194
195   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
196   bool isChanged = ( isShown  );
197   HYDROGUI_Shape::erase( theIsUpdateViewer );
198   setToUpdateColorScale( isChanged );
199 }
200
201 void HYDROGUI_ShapeBathymetry::setToUpdateColorScale( bool isChanged )
202 {
203 #ifndef LIGHT_MODE
204   if( isChanged && myDisplayer )
205     myDisplayer->SetToUpdateColorScale();
206 #endif
207 }
208
209 void HYDROGUI_ShapeBathymetry::GetRange( double& theMin, double& theMax ) const
210 {
211   if( !myRangeInitialized )
212   {
213     HYDROGUI_ShapeBathymetry* that = 
214       const_cast<HYDROGUI_ShapeBathymetry*>( this );
215     that->RescaleDefault();
216     that->myRangeInitialized = true;
217   }
218
219   theMin = myMin;
220   theMax = myMax;
221 }
222
223 void HYDROGUI_ShapeBathymetry::RescaleByVisible( OCCViewer_ViewWindow* theWindow )
224 {
225   QVector<int> visible;
226   visible.reserve( myCoords->Size() );
227
228   OCCViewer_ViewPort3d* vp = theWindow->getViewPort();
229   Handle(V3d_View) v = vp->getView();
230
231   int xp, yp;
232   int w = vp->width();
233   int h = vp->height();
234   int n = myCoords->Upper();
235   bool isVisible;
236
237 #ifdef _DEBUG
238   MESSAGE("RescaleByVisible: " << n);
239   QTime t1;
240   t1.start();
241 #endif
242
243   for( int i=myCoords->Lower(); i<=n; i++ )
244   {
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 );
248     if( isVisible )
249       visible.append( i );
250   }
251
252 #ifdef _DEBUG
253   MESSAGE("Time after visibles search:" << t1.elapsed());
254 #endif
255
256   //TODO: question: empty visible part produce empty bathymetry or complete bathymetry?
257   // For now "complete" is implemented
258   Rescale( visible, visible.isEmpty() );
259
260 #ifdef _DEBUG
261   MESSAGE("Time after rescale:" << t1.elapsed());
262 #endif
263 }
264
265 QVector<int> HYDROGUI_ShapeBathymetry::selected() const
266 {
267   QVector<int> selected;
268   selected.reserve( myCoords->Size() );
269
270   Handle(AIS_InteractiveObject) obj = getAISObjects().first();
271
272   Handle(AIS_InteractiveContext) c = getContext();
273   if( !c.IsNull() )
274   {
275     Handle(AIS_LocalContext) lc = c->LocalContext();
276     if( !lc.IsNull() )
277     {
278       for( lc->InitSelected(); lc->MoreSelected(); lc->NextSelected() )
279       {
280         Handle(HYDROGUI_BathymetryPointOwner) anOwner = 
281           Handle(HYDROGUI_BathymetryPointOwner)::DownCast( lc->SelectedOwner() );
282         if( !anOwner.IsNull() && anOwner->Selectable()==obj )
283           selected.append( anOwner->GetIndex() );
284       }
285     }
286   }
287   return selected;
288 }
289
290 void HYDROGUI_ShapeBathymetry::RescaleBySelection()
291 {
292   QVector<int> selection = selected();
293
294   //TODO: question: empty selection produce empty bathymetry or complete bathymetry?
295   // For now "complete" is implemented
296   Rescale( selection, selection.isEmpty() );
297 }
298
299 void HYDROGUI_ShapeBathymetry::Rescale( double theMin, double theMax )
300 {
301   getContext()->ClearSelected(true);
302   myMin = qMin( theMin, theMax );
303   myMax = qMax( theMin, theMax );
304   setToUpdateColorScale( true );
305
306   if( !getAISObjects().isEmpty() )
307   {
308     getContext()->RecomputePrsOnly( getAISObjects()[0] );
309     //getAISObjects()[0]->Redisplay();
310 }
311 }
312
313 void HYDROGUI_ShapeBathymetry::RescaleDefault()
314 {
315   Rescale( QVector<int>(), true );
316 }
317
318 void HYDROGUI_ShapeBathymetry::Rescale( const QVector<int>& theIndices, bool isForcedAll )
319 {
320   double aMin = 0, aMax = 0;
321   if( !myCoords.IsNull() )
322   {
323     bool isFirst = true;
324     int n = isForcedAll ? myCoords->Size() : theIndices.size();
325     for( int i=0; i<n; i++ )
326     {
327       int index = isForcedAll ? myCoords->Lower() + i : theIndices[i];
328
329       double aValue = myCoords->Value( index ).Z();
330       if( isFirst || aValue < aMin )
331         aMin = aValue;
332       if( isFirst || aValue > aMax )
333         aMax = aValue;
334       isFirst = false;
335     }
336   }
337   Rescale( aMin, aMax );
338 }
339
340 void HYDROGUI_ShapeBathymetry::Build()
341 {
342   buildShape();
343 }
344
345 void HYDROGUI_ShapeBathymetry::TextLabels( bool isOn, bool isUpdateCurrentViewer )
346 {
347   if( getAISObjects().isEmpty() )
348     return;
349
350   Handle(HYDROGUI_BathymetryPrs) prs = Handle(HYDROGUI_BathymetryPrs)::DownCast( getAISObjects()[0] );
351   if( prs.IsNull() )
352     return;
353
354   QVector<int> selection;
355   if( isOn )
356     selection = selected();
357
358
359   getContext()->ClearSelected(true);
360   prs->SetTextLabels( selection );
361   getContext()->RecomputePrsOnly( prs, Standard_False, Standard_False );
362   //prs->Redisplay();
363   if( isUpdateCurrentViewer )
364   getContext()->UpdateCurrentViewer();
365 }