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