Salome HOME
Merge remote-tracking branch 'origin/BR_1321_ECW' into BR_DEMO
[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 <Prs3d_Root.hxx>
21 #include <Prs3d_LineAspect.hxx>
22 #include <Prs3d_Text.hxx>
23 #include <Prs3d_TextAspect.hxx>
24 #include <Select3D_SensitiveBox.hxx>
25 #include <Select3D_SensitivePoint.hxx>
26 #include <Graphic3d_ArrayOfPolylines.hxx>
27 #include <Graphic3d_ArrayOfPoints.hxx>
28
29 const int BATH_HIGHLIGHT_MODE = 10;
30
31 HYDROGUI_BathymetryPrs::HYDROGUI_BathymetryPrs( const HYDROGUI_ShapeBathymetry* theShape )
32   : myShape( theShape )
33 {
34   SetHilightMode( BATH_HIGHLIGHT_MODE );
35   SetAutoHilight( Standard_True );
36 }
37
38 HYDROGUI_BathymetryPrs::~HYDROGUI_BathymetryPrs()
39 {
40 }
41
42 HYDROGUI_ShapeBathymetry* HYDROGUI_BathymetryPrs::GetShape() const
43 {
44   return const_cast<HYDROGUI_ShapeBathymetry*>( myShape );
45 }
46
47 void HYDROGUI_BathymetryPrs::UpdateBound()
48 {
49   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
50   myBound.SetVoid();
51   if( !points.IsNull() )
52   {
53     int aLower = 1;
54     int anUpper = points->VertexNumber();
55
56     for( int i = aLower; i <= anUpper; i++ )
57     {
58       gp_Pnt p = points->Vertice( i );
59       if( i==aLower )
60         myBound.Set( p );
61       else
62         myBound.Update( p.X(), p.Y(), p.Z() );
63     }
64   }
65 }
66
67 void HYDROGUI_BathymetryPrs::SetPoints( const Handle(TColgp_HArray1OfPnt)&     theCoords,
68                                         const Handle(Quantity_HArray1OfColor)& theColors )
69 {
70   AIS_PointCloud::SetPoints( theCoords, theColors );
71   UpdateBound();
72 }
73
74 void HYDROGUI_BathymetryPrs::Compute( const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
75                                       const Handle(Prs3d_Presentation)& thePresentation,
76                                       const Standard_Integer theMode )
77 {
78   thePresentation->Clear();
79   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
80
81   if( theMode==BATH_HIGHLIGHT_MODE )
82   {
83     if( myBound.IsVoid() )
84       UpdateBound();
85
86
87     if( myBound.IsVoid() || 
88         myBound.IsOpenXmin() || myBound.IsOpenXmax() ||
89         myBound.IsOpenYmin() || myBound.IsOpenYmax() ||
90         myBound.IsOpenZmin() || myBound.IsOpenZmax() )
91       return;
92
93     Standard_Real xmin, xmax, ymin, ymax, zmin, zmax;
94     myBound.Get( xmin, ymin, zmin, xmax, ymax, zmax );
95
96     Handle(Prs3d_LineAspect) aWireAspect = new Prs3d_LineAspect( Quantity_NOC_RED, Aspect_TOL_SOLID, 1.0 );
97     aGroup->SetPrimitivesAspect( aWireAspect->Aspect() );
98
99     Handle(Graphic3d_ArrayOfPolylines) aLines = new Graphic3d_ArrayOfPolylines( 18, 6 );
100     
101     aLines->AddBound( 5 );
102     aLines->AddVertex( xmin, ymin, zmin );
103     aLines->AddVertex( xmax, ymin, zmin );
104     aLines->AddVertex( xmax, ymax, zmin );
105     aLines->AddVertex( xmin, ymax, zmin );
106     aLines->AddVertex( xmin, ymin, zmin );
107     aLines->AddBound( 5 );
108     aLines->AddVertex( xmin, ymin, zmax );
109     aLines->AddVertex( xmax, ymin, zmax );
110     aLines->AddVertex( xmax, ymax, zmax );
111     aLines->AddVertex( xmin, ymax, zmax );
112     aLines->AddVertex( xmin, ymin, zmax );
113     aLines->AddBound( 2 );
114     aLines->AddVertex( xmin, ymin, zmin );
115     aLines->AddVertex( xmin, ymin, zmax );
116     aLines->AddBound( 2 );
117     aLines->AddVertex( xmax, ymin, zmin );
118     aLines->AddVertex( xmax, ymin, zmax );
119     aLines->AddBound( 2 );
120     aLines->AddVertex( xmax, ymax, zmin );
121     aLines->AddVertex( xmax, ymax, zmax );
122     aLines->AddBound( 2 );
123     aLines->AddVertex( xmin, ymax, zmin );
124     aLines->AddVertex( xmin, ymax, zmax );
125     aGroup->AddPrimitiveArray( aLines );
126   }
127   else
128   {
129     AIS_PointCloud::Compute( thePresentationManager, thePresentation, theMode );
130
131     Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
132     if( !myTextIndices.empty() && !points.IsNull() )
133     {
134       char aBuf[1024];
135       Handle(Prs3d_TextAspect) anAspect = new Prs3d_TextAspect();
136
137       foreach( int index, myTextIndices )
138       {
139         gp_Pnt p = points->Vertice( index );
140         sprintf( aBuf, "%.2f", p.Z() );
141         Prs3d_Text::Draw( aGroup, anAspect, aBuf, p );
142       }
143     }
144   }
145 }
146
147 void HYDROGUI_BathymetryPrs::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection,
148                                                const Standard_Integer theMode )
149 {
150   if( theMode == 0 )
151   {
152     if( myBound.IsVoid() )
153       UpdateBound();
154
155     if( myBound.IsVoid() || 
156         myBound.IsOpenXmin() || myBound.IsOpenXmax() ||
157         myBound.IsOpenYmin() || myBound.IsOpenYmax() ||
158         myBound.IsOpenZmin() || myBound.IsOpenZmax() )
159       return;
160
161     Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner( this );
162     Handle(Select3D_SensitiveBox) aSensitiveBox = new Select3D_SensitiveBox( anOwner, myBound );
163     theSelection->Add( aSensitiveBox );
164   }
165   if( theMode == 1 )
166   {
167     Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
168     int n = points.IsNull() ? 0 : points->VertexNumber();
169     for( int i=1; i<=n; i++ )
170     {
171       gp_Pnt p = points->Vertice( i );
172       Handle(HYDROGUI_BathymetryPointOwner) anOwner = new HYDROGUI_BathymetryPointOwner( this, i );
173       Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint( anOwner, p );
174       theSelection->Add( aSensitivePoint );
175     }
176   }
177 }
178
179 int HYDROGUI_BathymetryPrs::NbPoints() const
180 {
181   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
182   int n = points.IsNull() ? 0 : points->VertexNumber();
183   return n;
184 }
185
186 gp_Pnt HYDROGUI_BathymetryPrs::GetPoint( int theIndex ) const
187 {
188   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
189   if( points.IsNull() )
190     return gp_Pnt();
191   else
192     return points->Vertice( theIndex );
193 }
194
195 void HYDROGUI_BathymetryPrs::AddPoint( const Handle(Graphic3d_ArrayOfPoints)& thePoints,
196                                        const Handle(SelectMgr_EntityOwner)& theOwner )
197 {
198   Handle(HYDROGUI_BathymetryPointOwner) anOwner = Handle(HYDROGUI_BathymetryPointOwner)::DownCast( theOwner );
199   if( anOwner.IsNull() )
200     return;
201   gp_Pnt p = GetPoint( anOwner->GetIndex() );
202   thePoints->AddVertex( p );
203 }
204
205 void HYDROGUI_BathymetryPrs::HilightOwnerWithColor( const Handle(PrsMgr_PresentationManager3d)& thePM,
206                                                     const Handle(Graphic3d_HighlightStyle)& theColor,
207                                                     const Handle(SelectMgr_EntityOwner)& theOwner )
208 {
209   Handle(Prs3d_Presentation) aHilightPrs = GetHilightPresentation( thePM );
210   aHilightPrs->SetZLayer( Graphic3d_ZLayerId_Topmost );
211   aHilightPrs->Clear();
212   Handle(Graphic3d_ArrayOfPoints) points = new Graphic3d_ArrayOfPoints( 1 );
213   AddPoint( points, theOwner );
214
215   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aHilightPrs );
216   Handle(Graphic3d_AspectMarker3d) anAspect = new Graphic3d_AspectMarker3d( Aspect_TOM_X, Quantity_NOC_WHITE, 1.0 );
217   aGroup->SetGroupPrimitivesAspect( anAspect );
218   aGroup->AddPrimitiveArray( points );
219
220   if( thePM->IsImmediateModeOn() )
221     thePM->AddToImmediateList( aHilightPrs );
222 }
223
224 void HYDROGUI_BathymetryPrs::HilightSelected( const Handle(PrsMgr_PresentationManager3d)& thePM,
225                                               const SelectMgr_SequenceOfOwner& theOwners )
226 {
227   Handle(Prs3d_Presentation) aSelectPrs = GetSelectPresentation( thePM );
228   aSelectPrs->SetZLayer( Graphic3d_ZLayerId_Topmost );
229   aSelectPrs->Clear();
230
231   Handle(Graphic3d_ArrayOfPoints) points = new Graphic3d_ArrayOfPoints( theOwners.Size() );
232   for( int i=theOwners.Lower(); i<=theOwners.Upper(); i++ )
233     AddPoint( points, theOwners.Value( i ) );
234
235   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aSelectPrs );
236   Handle(Graphic3d_AspectMarker3d) anAspect = new Graphic3d_AspectMarker3d( Aspect_TOM_X, Quantity_NOC_WHITE, 1.0 );
237   aGroup->SetGroupPrimitivesAspect( anAspect );
238   aGroup->AddPrimitiveArray( points );
239
240   aSelectPrs->SetDisplayPriority(9);
241   aSelectPrs->Display();
242 }
243
244 void HYDROGUI_BathymetryPrs::ClearSelected()
245 {
246   Handle(Prs3d_Presentation) aSelectPrs = GetSelectPresentation( NULL );  
247   if( !aSelectPrs.IsNull() )
248     aSelectPrs->Clear(); 
249 }
250
251 void HYDROGUI_BathymetryPrs::SetTextLabels( const QList<int>& theTextIndices )
252 {
253   myTextIndices = theTextIndices;
254 }
255
256
257
258
259
260
261
262 HYDROGUI_BathymetryPointOwner::HYDROGUI_BathymetryPointOwner
263   ( const Handle(HYDROGUI_BathymetryPrs)& theBathymetry, int theIndex )
264   : SelectMgr_EntityOwner( Handle(SelectMgr_SelectableObject)::DownCast( theBathymetry ), 0 ),
265     myIndex( theIndex )
266 {
267 }
268
269 HYDROGUI_BathymetryPointOwner::~HYDROGUI_BathymetryPointOwner()
270 {
271 }
272
273 Standard_Boolean HYDROGUI_BathymetryPointOwner::IsAutoHilight() const
274 {
275   return Standard_False;
276 }
277
278 int HYDROGUI_BathymetryPointOwner::GetIndex() const
279 {
280   return myIndex;
281 }