Salome HOME
cleaning some comments
[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   }
103   else if (theMode == 0)
104   {
105     DEBTRACE("non highlight mode");  // display the nodes as points with color (HYDROGUI_ShapeBathymetry::UpdateWithColorScale)
106     AIS_PointCloud::Compute( thePresentationManager, thePresentation, AIS_PointCloud::DM_Points );
107
108     QImage qtMark( 4, 4, QImage::Format_RGB888 ); // points marks as 4x4 rectangles
109     Handle(Image_PixMap) aMark = HYDROGUI_Tool::Pixmap( qtMark );
110     DEBTRACE("pnt dims " << aMark->SizeX() << " " << aMark->SizeY());
111     Handle(Graphic3d_MarkerImage) gMark = new Graphic3d_MarkerImage(aMark);
112     Handle(Graphic3d_AspectMarker3d) anAspect = new Graphic3d_AspectMarker3d( Aspect_TOM_USERDEFINED, Quantity_NOC_WHITE, 1.0 );
113     anAspect->SetMarkerImage(gMark);
114
115     DEBTRACE("nb groups " << thePresentation->NumberOfGroups()); // find the group corresponding to the points, change the aspect
116     const Graphic3d_SequenceOfGroup& grps = thePresentation->Groups();
117     Graphic3d_SequenceOfGroup::Iterator anIter( grps );
118     for ( ; anIter.More(); anIter.Next() )
119     {
120       Handle(Graphic3d_Group) aGrp = anIter.Value();
121       DEBTRACE("grp type: " << aGrp->get_type_name());
122       Handle(Graphic3d_Aspects) asp = aGrp->Aspects();
123       if (!asp.IsNull())
124         {
125           DEBTRACE("aspect: " << asp->get_type_name());
126           DEBTRACE("asp->LineType() " << asp->LineType());
127           if (asp->LineType() != Aspect_TOL_DASH)       // find the group which does not correspond to the bounding box
128             aGrp->SetGroupPrimitivesAspect( anAspect );
129         }
130     }
131
132     Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
133     if (!points.IsNull())
134       DEBTRACE("nb points " << points->ItemNumber());
135
136     if( !myTextIndices.empty() && !points.IsNull() ) // points selected ==> altitude labels
137     {
138       DEBTRACE("myTextIndices.size() " << myTextIndices.size());
139       char aBuf[1024];
140       Handle(Prs3d_TextAspect) anAspect = new Prs3d_TextAspect();
141
142       Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
143       foreach( int index, myTextIndices )
144       {
145         gp_Pnt p = points->Vertice( index );
146         sprintf( aBuf, "%.2f", p.Z() );
147         Prs3d_Text::Draw( aGroup, anAspect, aBuf, p );
148       }
149     }
150   }
151   else
152     DEBTRACE("Do nothing");
153 }
154
155 void HYDROGUI_BathymetryPrs::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection,
156                                                const Standard_Integer theMode )
157 {
158   DEBTRACE("ComputeSelection " << theMode);
159   switch (theMode)
160   {
161     case 0:
162     {
163       DEBTRACE("Points selection");
164       AIS_PointCloud::ComputeSelection( theSelection, SM_Points);
165       break;
166     }
167     case 1:
168     {
169       DEBTRACE("Subset of points detection");
170       AIS_PointCloud::ComputeSelection( theSelection, SM_SubsetOfPoints );
171       break;
172     }
173     default:
174     {
175       DEBTRACE("Bounding box selection");
176       AIS_PointCloud::ComputeSelection( theSelection, SM_BndBox);
177       break;
178     }
179   }
180
181   if( theMode == 1 )
182   {
183     Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
184     int n = points.IsNull() ? 0 : points->VertexNumber();
185     for( int i=1; i<=n; i++ )
186     {
187       gp_Pnt p = points->Vertice( i );
188       Handle(HYDROGUI_BathymetryPointOwner) anOwner = new HYDROGUI_BathymetryPointOwner( this, i );
189       Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint( anOwner, p );
190       theSelection->Add( aSensitivePoint );
191     }
192   }
193 }
194
195 int HYDROGUI_BathymetryPrs::NbPoints() const
196 {
197   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
198   int n = points.IsNull() ? 0 : points->VertexNumber();
199   return n;
200 }
201
202 gp_Pnt HYDROGUI_BathymetryPrs::GetPoint( int theIndex ) const
203 {
204   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
205   if( points.IsNull() )
206     return gp_Pnt();
207   else
208     return points->Vertice( theIndex );
209 }
210
211 void HYDROGUI_BathymetryPrs::AddPoint( const Handle(Graphic3d_ArrayOfPoints)& thePoints,
212                                        const Handle(SelectMgr_EntityOwner)& theOwner )
213 {
214   DEBTRACE("AddPoint");
215   Handle(HYDROGUI_BathymetryPointOwner) anOwner = Handle(HYDROGUI_BathymetryPointOwner)::DownCast( theOwner );
216   if( anOwner.IsNull() )
217     return;
218   DEBTRACE("index " << anOwner->GetIndex());
219   gp_Pnt p = GetPoint( anOwner->GetIndex() );
220   DEBTRACE("p: " << p.X() << " " << p.Y() << " " << p.Z());
221   thePoints->AddVertex( p );
222 }
223
224 void HYDROGUI_BathymetryPrs::HilightOwnerWithColor( const Handle(PrsMgr_PresentationManager3d)& thePM,
225                                                     const Handle(Prs3d_Drawer)& theColor,
226                                                     const Handle(SelectMgr_EntityOwner)& theOwner )
227 {
228   DEBTRACE("HilightOwnerWithColor"); // appel si AIS_PointCloud::ComputeSelection( theSelection, SM_Points);
229   Handle(AIS_PointCloudOwner) anOwner = Handle(AIS_PointCloudOwner)::DownCast( theOwner );
230   if (anOwner.IsNull())
231     return;
232   DEBTRACE("---"); // jamais appelĂ© : on n'a pas de AIS_PointCloudOwner
233 }
234
235 void HYDROGUI_BathymetryPrs::HilightSelected( const Handle(PrsMgr_PresentationManager3d)& thePM,
236                                               const SelectMgr_SequenceOfOwner& theOwners )
237 {
238   DEBTRACE("HilightSelected " << theOwners.Size()); // appel si AIS_PointCloud::ComputeSelection( theSelection, SM_SubsetOfPoints );
239   //AIS_PointCloud::HilightSelected(thePM, theOwners); // Non, fige la machine !
240   if (theOwners.Size() == 0)
241     return;
242   Handle(SelectMgr_EntityOwner) owner = theOwners.First();
243   Handle(AIS_PointCloudOwner) anOwner = Handle(AIS_PointCloudOwner)::DownCast( owner );
244   if (!anOwner.IsNull())
245   {
246     DEBTRACE("immediateMode: " << thePM->IsImmediateModeOn()); // semble sans effet
247     const Handle(TColStd_HPackedMapOfInteger)& aSMap = anOwner->SelectedPoints();
248     DEBTRACE("selectedPoints.Extent() :"  << aSMap->Map().Extent());
249     const Handle(TColStd_HPackedMapOfInteger)& aDMap = anOwner->DetectedPoints();
250     DEBTRACE("detectedPoints.Extent() :"  << aDMap->Map().Extent());
251   }
252
253   Handle(Prs3d_Presentation) aSelectPrs = GetSelectPresentation( thePM );
254   aSelectPrs->SetZLayer( Graphic3d_ZLayerId_Topmost );
255   aSelectPrs->Clear();
256
257   //mySelectedPoints.clear();
258   mySelectedPoints.reserve(theOwners.Size());
259   Handle(Graphic3d_ArrayOfPoints) points = new Graphic3d_ArrayOfPoints( theOwners.Size() );
260   for( int i=theOwners.Lower(); i<=theOwners.Upper(); i++ )
261     {
262     AddPoint( points, theOwners.Value( i ) );
263     Handle(HYDROGUI_BathymetryPointOwner) anOwner = Handle(HYDROGUI_BathymetryPointOwner)::DownCast( theOwners.Value( i ) );
264     if (!anOwner.IsNull())
265       mySelectedPoints.append(anOwner->GetIndex());
266     }
267   DEBTRACE("points selected " << points->ItemNumber());
268   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aSelectPrs );
269   //Handle(Graphic3d_Group) aGroup = aSelectPrs->NewGroup();
270   Handle(Graphic3d_AspectMarker3d) anAspect = new Graphic3d_AspectMarker3d( Aspect_TOM_X, Quantity_NOC_WHITE, 1.0 );
271   aGroup->SetGroupPrimitivesAspect( anAspect );
272   aGroup->AddPrimitiveArray( points );
273
274   aSelectPrs->SetDisplayPriority(5);
275   aSelectPrs->Display();
276   //sleep(5);
277 }
278
279 void HYDROGUI_BathymetryPrs::ClearSelected()
280 {
281   DEBTRACE("ClearSelected");
282   Handle(Prs3d_Presentation) aSelectPrs = GetSelectPresentation( NULL );  
283   if( !aSelectPrs.IsNull() )
284     aSelectPrs->Clear(); 
285   //mySelectedPoints.clear();
286 }
287
288 void HYDROGUI_BathymetryPrs::SetTextLabels( const QVector<int>& theTextIndices )
289 {
290   DEBTRACE("SetTextLabels");
291   myTextIndices = theTextIndices;
292 }
293
294
295 HYDROGUI_BathymetryPointOwner::HYDROGUI_BathymetryPointOwner
296   ( const Handle(HYDROGUI_BathymetryPrs)& theBathymetry, int theIndex )
297   : SelectMgr_EntityOwner( Handle(SelectMgr_SelectableObject)::DownCast( theBathymetry ), 0 ),
298     myIndex( theIndex )
299 {
300 }
301
302 HYDROGUI_BathymetryPointOwner::~HYDROGUI_BathymetryPointOwner()
303 {
304 }
305
306 Standard_Boolean HYDROGUI_BathymetryPointOwner::IsAutoHilight() const
307 {
308   return Standard_False;
309 }
310
311 int HYDROGUI_BathymetryPointOwner::GetIndex() const
312 {
313   return myIndex;
314 }