Salome HOME
desactive traces
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_BathymetryPrs.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_BathymetryPrs.h>
20 #include <HYDROGUI_ShapeBathymetry.h>
21 #include <HYDROGUI_Tool.h>
22
23 #include <Prs3d_Root.hxx>
24 #include <Prs3d_LineAspect.hxx>
25 #include <Prs3d_Text.hxx>
26 #include <Prs3d_TextAspect.hxx>
27 #include <Select3D_SensitiveBox.hxx>
28 #include <Select3D_SensitivePoint.hxx>
29 #include <Graphic3d_ArrayOfPolylines.hxx>
30 #include <Graphic3d_ArrayOfPoints.hxx>
31 #include <Image_PixMap.hxx>
32 #include <Graphic3d_MarkerImage.hxx>
33 #include <TColStd_HPackedMapOfInteger.hxx>
34 #include <TColStd_PackedMapOfInteger.hxx>
35 #include <unistd.h>
36
37 #include <QImage>
38 #include <QVector>
39
40 //#define _DEVDEBUG_
41 #include "HYDRO_trace.hxx"
42
43 const int BATH_HIGHLIGHT_MODE = 10;
44
45 HYDROGUI_BathymetryPrs::HYDROGUI_BathymetryPrs( const HYDROGUI_ShapeBathymetry* theShape )
46   : myShape( theShape )
47 {
48   DEBTRACE("HYDROGUI_BathymetryPrs");
49   SetHilightMode( BATH_HIGHLIGHT_MODE );
50   SetAutoHilight( Standard_True );
51 }
52
53 HYDROGUI_BathymetryPrs::~HYDROGUI_BathymetryPrs()
54 {
55 }
56
57 HYDROGUI_ShapeBathymetry* HYDROGUI_BathymetryPrs::GetShape() const
58 {
59   return const_cast<HYDROGUI_ShapeBathymetry*>( myShape );
60 }
61
62 void HYDROGUI_BathymetryPrs::UpdateBound()
63 {
64   DEBTRACE("UpdateBound");
65   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
66   myBound.SetVoid();
67   if( !points.IsNull() )
68   {
69     int aLower = 1;
70     int anUpper = points->VertexNumber();
71
72     for( int i = aLower; i <= anUpper; i++ )
73     {
74       gp_Pnt p = points->Vertice( i );
75       if( i==aLower )
76         myBound.Set( p );
77       else
78         myBound.Update( p.X(), p.Y(), p.Z() );
79     }
80   }
81 }
82
83 void HYDROGUI_BathymetryPrs::SetPoints( const Handle(TColgp_HArray1OfPnt)&     theCoords,
84                                         const Handle(Quantity_HArray1OfColor)& theColors,
85                                         const Handle(TColgp_HArray1OfDir)&     theNormals )
86 {
87   AIS_PointCloud::SetPoints( theCoords, theColors, theNormals );
88   //UpdateBound();
89 }
90
91 void HYDROGUI_BathymetryPrs::Compute( const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
92                                       const Handle(Prs3d_Presentation)& thePresentation,
93                                       const Standard_Integer theMode )
94 {
95   DEBTRACE("Compute " << theMode);
96 //  thePresentation->Clear();
97 //  Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
98
99   if( theMode== BATH_HIGHLIGHT_MODE ) // highlight ==> draw bounding box
100   {
101     AIS_PointCloud::Compute( thePresentationManager, thePresentation, AIS_PointCloud::DM_BndBox );
102 //    if( myBound.IsVoid() )
103 //      UpdateBound();
104 //
105 //    if( myBound.IsVoid() ||
106 //        myBound.IsOpenXmin() || myBound.IsOpenXmax() ||
107 //        myBound.IsOpenYmin() || myBound.IsOpenYmax() ||
108 //        myBound.IsOpenZmin() || myBound.IsOpenZmax() )
109 //      return;
110 //
111 //    Standard_Real xmin, xmax, ymin, ymax, zmin, zmax;
112 //    myBound.Get( xmin, ymin, zmin, xmax, ymax, zmax );
113 //
114 //    Handle(Prs3d_LineAspect) aWireAspect = new Prs3d_LineAspect( Quantity_NOC_RED, Aspect_TOL_DASH, 4.0 );
115 //    aGroup->SetPrimitivesAspect( aWireAspect->Aspect() ); // use Aspect_TOL_DASH to find the group by the aspect style
116 //
117 //    Handle(Graphic3d_ArrayOfPolylines) aLines = new Graphic3d_ArrayOfPolylines( 18, 6 );
118 //
119 //    aLines->AddBound( 5 );
120 //    aLines->AddVertex( xmin, ymin, zmin );
121 //    aLines->AddVertex( xmax, ymin, zmin );
122 //    aLines->AddVertex( xmax, ymax, zmin );
123 //    aLines->AddVertex( xmin, ymax, zmin );
124 //    aLines->AddVertex( xmin, ymin, zmin );
125 //    aLines->AddBound( 5 );
126 //    aLines->AddVertex( xmin, ymin, zmax );
127 //    aLines->AddVertex( xmax, ymin, zmax );
128 //    aLines->AddVertex( xmax, ymax, zmax );
129 //    aLines->AddVertex( xmin, ymax, zmax );
130 //    aLines->AddVertex( xmin, ymin, zmax );
131 //    aLines->AddBound( 2 );
132 //    aLines->AddVertex( xmin, ymin, zmin );
133 //    aLines->AddVertex( xmin, ymin, zmax );
134 //    aLines->AddBound( 2 );
135 //    aLines->AddVertex( xmax, ymin, zmin );
136 //    aLines->AddVertex( xmax, ymin, zmax );
137 //    aLines->AddBound( 2 );
138 //    aLines->AddVertex( xmax, ymax, zmin );
139 //    aLines->AddVertex( xmax, ymax, zmax );
140 //    aLines->AddBound( 2 );
141 //    aLines->AddVertex( xmin, ymax, zmin );
142 //    aLines->AddVertex( xmin, ymax, zmax );
143 //    aGroup->AddPrimitiveArray( aLines );
144   }
145   else if (theMode == 0)
146   {
147     DEBTRACE("non highlight mode");  // display the nodes as points with color (HYDROGUI_ShapeBathymetry::UpdateWithColorScale)
148     AIS_PointCloud::Compute( thePresentationManager, thePresentation, AIS_PointCloud::DM_Points );
149
150     QImage qtMark( 4, 4, QImage::Format_RGB888 ); // points marks as 4x4 rectangles
151     Handle(Image_PixMap) aMark = HYDROGUI_Tool::Pixmap( qtMark );
152     DEBTRACE("pnt dims " << aMark->SizeX() << " " << aMark->SizeY());
153     Handle(Graphic3d_MarkerImage) gMark = new Graphic3d_MarkerImage(aMark);
154     Handle(Graphic3d_AspectMarker3d) anAspect = new Graphic3d_AspectMarker3d( Aspect_TOM_USERDEFINED, Quantity_NOC_WHITE, 1.0 );
155     anAspect->SetMarkerImage(gMark);
156
157     DEBTRACE("nb groups " << thePresentation->NumberOfGroups()); // find the group corresponding to the points, change the aspect
158     const Graphic3d_SequenceOfGroup& grps = thePresentation->Groups();
159     Graphic3d_SequenceOfGroup::Iterator anIter( grps );
160     for ( ; anIter.More(); anIter.Next() )
161     {
162       Handle(Graphic3d_Group) aGrp = anIter.Value();
163       DEBTRACE("grp type: " << aGrp->get_type_name());
164       Handle(Graphic3d_Aspects) asp = aGrp->Aspects();
165       if (!asp.IsNull())
166         {
167           DEBTRACE("aspect: " << asp->get_type_name());
168           DEBTRACE("asp->LineType() " << asp->LineType());
169           if (asp->LineType() != Aspect_TOL_DASH)       // find the group which does not correspond to the bounding box
170             aGrp->SetGroupPrimitivesAspect( anAspect );
171         }
172     }
173
174     Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
175     if (!points.IsNull())
176       DEBTRACE("nb points " << points->ItemNumber());
177
178     if( !myTextIndices.empty() && !points.IsNull() ) // points selected ==> altitude labels
179     {
180       DEBTRACE("myTextIndices.size() " << myTextIndices.size());
181       char aBuf[1024];
182       Handle(Prs3d_TextAspect) anAspect = new Prs3d_TextAspect();
183
184       Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
185       foreach( int index, myTextIndices )
186       {
187         gp_Pnt p = points->Vertice( index );
188         sprintf( aBuf, "%.2f", p.Z() );
189         Prs3d_Text::Draw( aGroup, anAspect, aBuf, p );
190       }
191     }
192   }
193   else
194     DEBTRACE("Do nothing");
195 }
196
197 void HYDROGUI_BathymetryPrs::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection,
198                                                const Standard_Integer theMode )
199 {
200   DEBTRACE("ComputeSelection " << theMode);
201   switch (theMode)
202   {
203     case 0:
204     {
205       DEBTRACE("Points selection");
206       AIS_PointCloud::ComputeSelection( theSelection, SM_Points);
207       break;
208     }
209     case 1:
210     {
211       DEBTRACE("Subset of points detection");
212       AIS_PointCloud::ComputeSelection( theSelection, SM_SubsetOfPoints );
213       break;
214     }
215     default:
216     {
217       DEBTRACE("Bounding box selection");
218       AIS_PointCloud::ComputeSelection( theSelection, SM_BndBox);
219       break;
220     }
221   }
222
223 //  if( theMode == 0 )
224 //  {
225 //    if( myBound.IsVoid() )
226 //      UpdateBound();
227 //
228 //    if( myBound.IsVoid() ||
229 //        myBound.IsOpenXmin() || myBound.IsOpenXmax() ||
230 //        myBound.IsOpenYmin() || myBound.IsOpenYmax() ||
231 //        myBound.IsOpenZmin() || myBound.IsOpenZmax() )
232 //      return;
233 //
234 //    Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner( this );
235 //    Handle(Select3D_SensitiveBox) aSensitiveBox = new Select3D_SensitiveBox( anOwner, myBound );
236 //    theSelection->Add( aSensitiveBox );
237 //  }
238   if( theMode == 1 )
239   {
240     Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
241     int n = points.IsNull() ? 0 : points->VertexNumber();
242     for( int i=1; i<=n; i++ )
243     {
244       gp_Pnt p = points->Vertice( i );
245       Handle(HYDROGUI_BathymetryPointOwner) anOwner = new HYDROGUI_BathymetryPointOwner( this, i );
246       Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint( anOwner, p );
247       theSelection->Add( aSensitivePoint );
248     }
249   }
250 }
251
252 int HYDROGUI_BathymetryPrs::NbPoints() const
253 {
254   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
255   int n = points.IsNull() ? 0 : points->VertexNumber();
256   return n;
257 }
258
259 gp_Pnt HYDROGUI_BathymetryPrs::GetPoint( int theIndex ) const
260 {
261   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
262   if( points.IsNull() )
263     return gp_Pnt();
264   else
265     return points->Vertice( theIndex );
266 }
267
268 void HYDROGUI_BathymetryPrs::AddPoint( const Handle(Graphic3d_ArrayOfPoints)& thePoints,
269                                        const Handle(SelectMgr_EntityOwner)& theOwner )
270 {
271   DEBTRACE("AddPoint");
272   Handle(HYDROGUI_BathymetryPointOwner) anOwner = Handle(HYDROGUI_BathymetryPointOwner)::DownCast( theOwner );
273   if( anOwner.IsNull() )
274     return;
275   DEBTRACE("index " << anOwner->GetIndex());
276   gp_Pnt p = GetPoint( anOwner->GetIndex() );
277   DEBTRACE("p: " << p.X() << " " << p.Y() << " " << p.Z());
278   thePoints->AddVertex( p );
279 }
280
281 void HYDROGUI_BathymetryPrs::HilightOwnerWithColor( const Handle(PrsMgr_PresentationManager3d)& thePM,
282                                                     const Handle(Prs3d_Drawer)& theColor,
283                                                     const Handle(SelectMgr_EntityOwner)& theOwner )
284 {
285   DEBTRACE("HilightOwnerWithColor"); // appel si AIS_PointCloud::ComputeSelection( theSelection, SM_Points);
286   Handle(AIS_PointCloudOwner) anOwner = Handle(AIS_PointCloudOwner)::DownCast( theOwner );
287   if (anOwner.IsNull())
288     return;
289   DEBTRACE("---"); // jamais appelĂ© : on n'a pas de AIS_PointCloudOwner
290
291 //  Handle(Prs3d_Presentation) aHilightPrs = GetHilightPresentation( thePM );
292 //  aHilightPrs->SetZLayer( Graphic3d_ZLayerId_Topmost );
293 //  aHilightPrs->Clear();
294 //  Handle(Graphic3d_ArrayOfPoints) points = new Graphic3d_ArrayOfPoints( 1 );
295 //  AddPoint( points, theOwner );
296 //
297 //  Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aHilightPrs );
298 //  Handle(Graphic3d_AspectMarker3d) anAspect = new Graphic3d_AspectMarker3d( Aspect_TOM_X, Quantity_NOC_WHITE, 1.0 );
299 //  aGroup->SetGroupPrimitivesAspect( anAspect );
300 //  aGroup->AddPrimitiveArray( points );
301 //
302 //  if( thePM->IsImmediateModeOn() )
303 //    thePM->AddToImmediateList( aHilightPrs );
304 }
305
306 void HYDROGUI_BathymetryPrs::HilightSelected( const Handle(PrsMgr_PresentationManager3d)& thePM,
307                                               const SelectMgr_SequenceOfOwner& theOwners )
308 {
309   DEBTRACE("HilightSelected " << theOwners.Size()); // appel si AIS_PointCloud::ComputeSelection( theSelection, SM_SubsetOfPoints );
310   //AIS_PointCloud::HilightSelected(thePM, theOwners); // Non, fige la machine !
311   if (theOwners.Size() == 0)
312     return;
313   Handle(SelectMgr_EntityOwner) owner = theOwners.First();
314   Handle(AIS_PointCloudOwner) anOwner = Handle(AIS_PointCloudOwner)::DownCast( owner );
315   if (!anOwner.IsNull())
316   {
317     DEBTRACE("immediateMode: " << thePM->IsImmediateModeOn()); // semble sans effet
318     const Handle(TColStd_HPackedMapOfInteger)& aSMap = anOwner->SelectedPoints();
319     DEBTRACE("selectedPoints.Extent() :"  << aSMap->Map().Extent());
320     const Handle(TColStd_HPackedMapOfInteger)& aDMap = anOwner->DetectedPoints();
321     DEBTRACE("detectedPoints.Extent() :"  << aDMap->Map().Extent());
322   }
323
324   Handle(Prs3d_Presentation) aSelectPrs = GetSelectPresentation( thePM );
325   aSelectPrs->SetZLayer( Graphic3d_ZLayerId_Topmost );
326   aSelectPrs->Clear();
327
328   //mySelectedPoints.clear();
329   mySelectedPoints.reserve(theOwners.Size());
330   Handle(Graphic3d_ArrayOfPoints) points = new Graphic3d_ArrayOfPoints( theOwners.Size() );
331   for( int i=theOwners.Lower(); i<=theOwners.Upper(); i++ )
332     {
333     AddPoint( points, theOwners.Value( i ) );
334     Handle(HYDROGUI_BathymetryPointOwner) anOwner = Handle(HYDROGUI_BathymetryPointOwner)::DownCast( theOwners.Value( i ) );
335     if (!anOwner.IsNull())
336       mySelectedPoints.append(anOwner->GetIndex());
337     }
338   DEBTRACE("points selected " << points->ItemNumber());
339   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aSelectPrs );
340   //Handle(Graphic3d_Group) aGroup = aSelectPrs->NewGroup();
341   Handle(Graphic3d_AspectMarker3d) anAspect = new Graphic3d_AspectMarker3d( Aspect_TOM_X, Quantity_NOC_WHITE, 1.0 );
342   aGroup->SetGroupPrimitivesAspect( anAspect );
343   aGroup->AddPrimitiveArray( points );
344
345   aSelectPrs->SetDisplayPriority(5);
346   aSelectPrs->Display();
347   //sleep(5);
348 }
349
350 void HYDROGUI_BathymetryPrs::ClearSelected()
351 {
352   DEBTRACE("ClearSelected");
353   Handle(Prs3d_Presentation) aSelectPrs = GetSelectPresentation( NULL );  
354   if( !aSelectPrs.IsNull() )
355     aSelectPrs->Clear(); 
356   //mySelectedPoints.clear();
357 }
358
359 void HYDROGUI_BathymetryPrs::SetTextLabels( const QVector<int>& theTextIndices )
360 {
361   DEBTRACE("SetTextLabels");
362   myTextIndices = theTextIndices;
363 }
364
365
366
367
368
369
370
371 HYDROGUI_BathymetryPointOwner::HYDROGUI_BathymetryPointOwner
372   ( const Handle(HYDROGUI_BathymetryPrs)& theBathymetry, int theIndex )
373   : SelectMgr_EntityOwner( Handle(SelectMgr_SelectableObject)::DownCast( theBathymetry ), 0 ),
374     myIndex( theIndex )
375 {
376 }
377
378 HYDROGUI_BathymetryPointOwner::~HYDROGUI_BathymetryPointOwner()
379 {
380 }
381
382 Standard_Boolean HYDROGUI_BathymetryPointOwner::IsAutoHilight() const
383 {
384   return Standard_False;
385 }
386
387 int HYDROGUI_BathymetryPointOwner::GetIndex() const
388 {
389   return myIndex;
390 }