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